@ -25,16 +25,22 @@ $pay_method = $_POST['pay_method'];
$status = $_POST['status'];
$elevators = json_decode($_POST['elevators'], true);
$otherOptions = json_decode($_POST['otherOptions'], true);
$payment_kind = $_POST['pay_method'];
$current_date = date("Y-M-d H:i:s");
$sql_str = "SELECT status FROM con_maintance_examine_apply WHERE vol_no = :vol_no AND apply_key = :apply_key";
$total_price = $_POST['total_price'];
$total_sale_price = $_POST['total_sale_price'];
$sql_str = "SELECT status FROM con_maintance_examine_apply WHERE (vol_no, apply_key) = (:vol_no, :apply_key) ";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':vol_no', $vol_no);
$stmt->bindParam(':apply_key', $apply_key);
$stmt->execute();
$contract = $stmt->fetch(PDO::FETCH_ASSOC);
$conn->beginTransaction();
try{
if(!$contract){
$sql_str = "INSERT INTO con_maintance_examine_apply (apply_key, vol_no, customer, address, case_name ,brand, num, salesman, maintain_kind, contract_begin_date, contract_end_date, contract_kind, payment_kind, progress_remark, service_fee, total_price, total_sale_price, penalty, deposit_rate, keep_rate, warranty_rate, memo, status, creater, create_at)
VALUES (:apply_key, :vol_no, :customer, :address, :case_name ,:brand, :num, :salesman, :maintain_kind, :contract_begin_date, :contract_end_date, :contract_kind, :payment_kind, :progress_remark, :service_fee, :total_price, :total_sale_price, :penalty, :deposit_rate, :keep_rate, :warranty_rate, :memo, :status, :creater, :create_at)";
@ -66,8 +72,8 @@ if(!$contract){
$stmt->bindParam(':create_at', $current_date);
$stmt->execute();
$sql_str = "INSERT INTO con_maintance_examine_clear (apply_key, register_code, elevator_brand, elevator_kind, spec, weight, speed, stop, floors, persons, elevator_num, useful_years, last_check_date, speed_governors_check_expense, maintain_times, is_m1_bundle, maintain_ _months, maintain_period, maintain_method, stand_price, contract_price, sold_price, commission_expense, management_expense, annual_survey_expense, service_expense, cmstatus, updated_at, creater, created_at)
VALUES (:apply_key, :register_code, :elevator_brand, :elevator_kind, :spec, :weight, :speed, :stop, :floors, :persons, :elevator_num, :useful_years, :last_check_date, :speed_governors_check_expense, :maintain_times, :is_m1_bundle, :maintain_ _months, :maintain_period, :maintain_method, :stand_price, :contract_price, :sold_price, :commission_expense, :management_expense, :annual_survey_expense, :service_expense, :cmstatus, :updated_at, :creater, :created_at) ";
$sql_str = "INSERT INTO con_maintance_examine_clear (apply_key, item_no , register_code, elevator_brand, elevator_kind, spec, weight, speed, stop, floors, persons, elevator_num, useful_years, last_check_date, speed_governors_check_expense, maintain_times, is_m1_bundle, maintain_months, maintain_period, maintain_method, stand_price, contract_price, sold_price, commission_expense, management_expense, annual_survey_expense, service_expense, cmstatus, updated_at, creater, created_at)
VALUES (:apply_key, :item_no , :register_code, :elevator_brand, :elevator_kind, :spec, :weight, :speed, :stop, :floors, :persons, :elevator_num, :useful_years, :last_check_date, :speed_governors_check_expense, :maintain_times, :is_m1_bundle, :maintain_months, :maintain_period, :maintain_method, :stand_price, :contract_price, :sold_price, :commission_expense, :management_expense, :annual_survey_expense, :service_expense, :cmstatus, :updated_at, :creater, :created_at) ";
foreach($elevators as $elevator){
if(empty($elevator['permitNumber'])) $register_code = "A";
@ -83,7 +89,7 @@ if(!$contract){
$management_expense = null;
$service_expense = ($elevator['sale_price'] / $total_sale_price) * $serviceFee;
$cmstatus = "Y";
$cycle = (int)$elevator['cycle'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':apply_key', $apply_key);
$stmt->bindParam(':item_no', $elevator['id']);
@ -102,8 +108,8 @@ if(!$contract){
$stmt->bindParam(':speed_governors_check_expense', $speed_governors_check_expense);
$stmt->bindParam(':maintain_times', $maintain_times);
$stmt->bindParam(':is_m1_bundle', $elevator['m1']);
$stmt->bindParam(':maintain_ _months', $elevator['months']);
$stmt->bindParam(':maintain_period', $elevator[' cycle'] );
$stmt->bindParam(':maintain_months', $elevator['months']);
$stmt->bindParam(':maintain_period', $cycle);
$stmt->bindParam(':maintain_method', $elevator['method']);
$stmt->bindParam(':stand_price', $elevator['price']);
$stmt->bindParam(':contract_price', $elevator['sale_price']);
@ -115,20 +121,21 @@ if(!$contract){
$stmt->bindParam(':cmstatus', $cmstatus);
$stmt->bindParam(':updated_at', $updated_at);
$stmt->bindParam(':creater', $creater);
$stmt->bindParam(':created_at', $created_at );
$stmt->bindParam(':created_at', $current_date );
$stmt->execute();
}
$sql_str = "INSERT INTO pricereview_maintain_item (mid, item_no, item_group, item_spec, option_relate_spec, item_unit_price, item_qty, item_price_bp, memo)
VALUES (:mid, :item_no, :item_group, :item_spec, :option_relate_spec, :item_unit_price, :item_qty, :item_price_bp, :memo)";
$sql_str = "INSERT INTO pricereview_maintain_item (mid, price_ id, item_no, item_group, item_spec, option_relate_spec, item_unit_price, item_qty, item_price_bp, memo)
VALUES (:mid, :price_ id, :item_no, :item_group, :item_spec, :option_relate_spec, :item_unit_price, :item_qty, :item_price_bp, :memo)";
foreach($otherOptions as $option){
$item_price_bp = $option['price']*$option['num'];
$option_relate_spec = $option['toElevator'][0]['id'];
$option_relate_spec = (count($option['toElevator']) > 0) ? $option['toElevator'][0]['id'] : null ;
$item_group = "E";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $apply_key);
$stmt->bindParam(':item_no', $option['pr_no']);
$stmt->bindParam(':price_id', $option['pr_no']);
$stmt->bindParam(':item_no', $option['id']);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $option['name']);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
@ -154,12 +161,13 @@ if(!$contract){
deposit_rate=:deposit_rate,
keep_rate=:keep_rate,
warranty_rate=:warranty_rate,
remark=:remark,
memo=:memo,
brand=:brand,
status=:status,
updated_at=:updated_at
updated_at=:updated_at,
progress_remark=:progress_remark
WHERE apply_key=:apply_key AND vol_no=:vol_no";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':apply_key', $apply_key);
$stmt->bindParam(':vol_no', $vol_no);
$stmt->bindParam(':case_name', $case_name);
@ -178,17 +186,17 @@ if(!$contract){
$stmt->bindParam(':keep_rate', $keep_rate);
$stmt->bindParam(':warranty_rate', $warranty_rate);
$stmt->bindParam(':memo', $remark);
$stmt->bindParam(':brand', $brand);
$stmt->bindParam(':status', $status);
$stmt->bindParam(':updated_at', $current_date);
$stmt->execute();
//整機單價
$sql_str = "SELECT * FROM con_maintance_examine_clear WHERE apply_key=:apply_key AND vol_no=:vol_no ";
$sql_str = "SELECT * FROM con_maintance_examine_clear WHERE (apply_key, cmstatus) = (:apply_key, 'Y') ";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':apply_key', $apply_key);
$stmt->bindParam(':vol_no', $vol_no);
$stmt->execute();
$item s = $stmt->fetchAll(PDO::FETCH_ASSOC);
$oldElevator s = $stmt->fetchAll(PDO::FETCH_ASSOC);
$oldItemsNo = (array)array_column($oldElevators, 'item_no');
$newItemsNo = (array)array_column($elevators, 'id');
@ -197,16 +205,16 @@ if(!$contract){
$inBothIds = array_values(array_intersect($oldItemsNo, $newItemsNo)); //舊的跟新的都有,所以要更新(update)
foreach($onlyInOldIds as $oldIdx){
$sql_str = "DELETE FROM con_maintance_examine_clear WHERE apply_key = :apply_key AND item_no = :item_no AND vol_no=:vol_no";
// $sql_str = "DELETE FROM con_maintance_examine_clear WHERE apply_key=:apply_key AND item_no=:item_no";
$sql_str = "UPDATE con_maintance_examine_clear SET cmstatus = 'D' WHERE apply_key=:apply_key AND item_no=:item_no";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':apply_key', $apply_key);
$stmt->bindParam(':item_no', $oldidx);
$stmt->bindParam(':vol_no', $vol_no);
$stmt->bindParam(':item_no', $oldIdx);
$stmt->execute();
}
foreach($onlyInNewIds as $newidx){
$sql_str = "INSERT INTO con_maintance_examine_clear (apply_key, register_code, elevator_brand, elevator_kind, spec, weight, speed, stop, floors, persons, elevator_num, useful_years, last_check_date, speed_governors_check_expense, maintain_times, is_m1_bundle, maintain_ _months, maintain_period, maintain_method, stand_price, contract_price, sold_price, commission_expense, management_expense, annual_survey_expense, service_expense, cmstatus, updated_at, creater, created_at)
VALUES (:apply_key, :register_code, :elevator_brand, :elevator_kind, :spec, :weight, :speed, :stop, :floors, :persons, :elevator_num, :useful_years, :last_check_date, :speed_governors_check_expense, :maintain_times, :is_m1_bundle, :maintain_ _months, :maintain_period, :maintain_method, :stand_price, :contract_price, :sold_price, :commission_expense, :management_expense, :annual_survey_expense, :service_expense, :cmstatus, :updated_at, :creater, :created_at) ";
$sql_str = "INSERT INTO con_maintance_examine_clear (apply_key, item_no , register_code, elevator_brand, elevator_kind, spec, weight, speed, stop, floors, persons, elevator_num, useful_years, last_check_date, speed_governors_check_expense, maintain_times, is_m1_bundle, maintain_months, maintain_period, maintain_method, stand_price, contract_price, sold_price, commission_expense, management_expense, annual_survey_expense, service_expense, cmstatus, updated_at, creater, created_at)
VALUES (:apply_key, :item_no , :register_code, :elevator_brand, :elevator_kind, :spec, :weight, :speed, :stop, :floors, :persons, :elevator_num, :useful_years, :last_check_date, :speed_governors_check_expense, :maintain_times, :is_m1_bundle, :maintain_months, :maintain_period, :maintain_method, :stand_price, :contract_price, :sold_price, :commission_expense, :management_expense, :annual_survey_expense, :service_expense, :cmstatus, :updated_at, :creater, :created_at) ";
$elevator = array_values(array_filter($elevators, fn($el)=> $el['id'] == $newidx))[0];
$register_code = empty($elevator['permitNumber']) ? "A" : $elevator['permitNumber'];
if($elevator['spec'] == "MAE100") $elevator_kind = "A";
@ -220,7 +228,7 @@ if(!$contract){
$management_expense = null;
$service_expense = ($elevator['sale_price'] / $total_sale_price) * $serviceFee;
$cmstatus = "Y";
$cycle = (int)$elevator['cycle'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':apply_key', $apply_key);
$stmt->bindParam(':item_no', $elevator['id']);
@ -239,8 +247,8 @@ if(!$contract){
$stmt->bindParam(':speed_governors_check_expense', $speed_governors_check_expense);
$stmt->bindParam(':maintain_times', $maintain_times);
$stmt->bindParam(':is_m1_bundle', $elevator['m1']);
$stmt->bindParam(':maintain_ _months', $elevator['months']);
$stmt->bindParam(':maintain_period', $elevator[' cycle'] );
$stmt->bindParam(':maintain_months', $elevator['months']);
$stmt->bindParam(':maintain_period', $cycle);
$stmt->bindParam(':maintain_method', $elevator['method']);
$stmt->bindParam(':stand_price', $elevator['price']);
$stmt->bindParam(':contract_price', $elevator['sale_price']);
@ -252,13 +260,10 @@ if(!$contract){
$stmt->bindParam(':cmstatus', $cmstatus);
$stmt->bindParam(':updated_at', $updated_at);
$stmt->bindParam(':creater', $creater);
$stmt->bindParam(':created_at', $created_at );
$stmt->bindParam(':created_at', $current_date );
$stmt->execute();
}
foreach($inBothIds as $bothidx){
$sql_str = "INSERT INTO con_maintance_examine_clear (apply_key, register_code, elevator_brand, elevator_kind, spec, weight, speed, stop, floors, persons, elevator_num, useful_years, last_check_date, speed_governors_check_expense, maintain_times, is_m1_bundle, maintain__months, maintain_period, maintain_method, stand_price, contract_price, sold_price, commission_expense, management_expense, annual_survey_expense, service_expense, cmstatus, updated_at, creater, creater, created_at)
VALUES (:apply_key, :register_code, :elevator_brand, :elevator_kind, :spec, :weight, :speed, :stop, :floors, :persons, :elevator_num, :useful_years, :last_check_date, :speed_governors_check_expense, :maintain_times, :is_m1_bundle, :maintain__months, :maintain_period, :maintain_method, :stand_price, :contract_price, :sold_price, :commission_expense, :management_expense, :annual_survey_expense, :service_expense, :cmstatus, :updated_at, :creater, :creater, :created_at) ";
$sql_str = "UPDATE con_maintance_examine_clear SET
register_code=:register_code,
elevator_brand=:elevator_brand,
@ -275,7 +280,7 @@ if(!$contract){
speed_governors_check_expense=:speed_governors_check_expense,
maintain_times=:maintain_times,
is_m1_bundle=:is_m1_bundle,
maintain_ _months=:maintain_ _months,
maintain_months=:maintain_months,
maintain_period=:maintain_period,
maintain_method=:maintain_method,
stand_price=:stand_price,
@ -287,7 +292,7 @@ if(!$contract){
service_expense=:service_expense,
cmstatus=:cmstatus,
updated_at=:updated_at
WHERE apply_key=:apply_key AND item_no=:item_no ";
WHERE (apply_key, item_no) = (:apply_key, :item_no) ";
$elevator = array_values(array_filter($elevators, fn($el)=> $el['id'] == $bothidx))[0];
$register_code = empty($elevator['permitNumber']) ? "A" : $elevator['permitNumber'];
@ -302,6 +307,7 @@ if(!$contract){
$management_expense = null;
$service_expense = ($elevator['sale_price'] / $total_sale_price) * $serviceFee;
$cmstatus = "Y";
$cycle = (int)$elevator['cycle'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':apply_key', $apply_key);
@ -320,8 +326,8 @@ if(!$contract){
$stmt->bindParam(':speed_governors_check_expense', $speed_governors_check_expense);
$stmt->bindParam(':maintain_times', $maintain_times);
$stmt->bindParam(':is_m1_bundle', $elevator['m1']);
$stmt->bindParam(':maintain_ _months', $elevator['months']);
$stmt->bindParam(':maintain_period', $elevator[' cycle'] );
$stmt->bindParam(':maintain_months', $elevator['months']);
$stmt->bindParam(':maintain_period', $cycle);
$stmt->bindParam(':maintain_method', $elevator['method']);
$stmt->bindParam(':stand_price', $elevator['price']);
$stmt->bindParam(':contract_price', $elevator['sale_price']);
@ -338,22 +344,22 @@ if(!$contract){
}
//除外項目
$sql_str = "SELECT * FROM pricereview_maintain_item WHERE mid = :apply_key AND item_group = 'E' ";
$sql_str = "SELECT * FROM pricereview_maintain_item WHERE (mid, item_group) = (:apply_key, 'E') ";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid ', $apply_key);
$stmt->bindParam(':apply_key ', $apply_key);
$stmt->execute();
$item_other_options = $stmt->fetchAll(PDO::FETCH_ASSOC);
$oldOtherOptionsNo = (array)array_column($item_other_options, 'item_no');
$newOtherOptioinNo = (array)array_column($otherOptions, 'id');
$nlyInOtherOptionOldIds = array_values(array_diff($oldOtherOptionsNo, $newOtherOptioinNo));
$o nlyInOtherOptionOldIds = array_values(array_diff($oldOtherOptionsNo, $newOtherOptioinNo));
$onlyInOtherOptionNewIds = array_values(array_diff($newOtherOptioinNo, $oldOtherOptionsNo));
$inBothOtherOptionIds = array_values(array_intersect($oldOtherOptionsNo, $newOtherOptioinNo));
foreach($onlyInOtherOptionOldIds as $oldotheroptionidx){
$sql_str = "DELETE FROM pricereview_maintain_item WHERE mid = :apply_key AND item_no = :item_no AND item_group = 'E' ";
$sql_str = "DELETE FROM pricereview_maintain_item WHERE (mid, item_no, item_group) = (:apply_key, :item_no, 'E') ";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid ', $apply_key);
$stmt->bindParam(':apply_key ', $apply_key);
$stmt->bindParam(':item_no', $oldotheroptionidx);
$stmt->execute();
}
@ -361,15 +367,16 @@ if(!$contract){
foreach($onlyInOtherOptionNewIds as $newotheroptionidx){
$option = array_values(array_filter($otherOptions, fn($el)=> $el['id'] == $newotheroptionidx))[0];
$sql_str = "INSERT INTO pricereview_maintain_item (mid, item_no, item_group, item_spec, option_relate_spec, item_unit_price, item_qty, item_price_bp, memo)
VALUES (:mid, :item_no, :item_group, :item_spec, :option_relate_spec, :item_unit_price, :item_qty, :item_price_bp, :memo)";
$sql_str = "INSERT INTO pricereview_maintain_item (mid, price_ id, item_no, item_group, item_spec, option_relate_spec, item_unit_price, item_qty, item_price_bp, memo)
VALUES (:mid, :price_ id, :item_no, :item_group, :item_spec, :option_relate_spec, :item_unit_price, :item_qty, :item_price_bp, :memo)";
$item_price_bp = $option['price']*$option['num'];
$option_relate_spec = $option['toElevator'][0]['id'];
$option_relate_spec = (count($option['toElevator']) > 0) ? $option['toElevator'][0]['id'] : null ;
$item_group = "E";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $apply_key);
$stmt->bindParam(':item_no', $option['pr_no']);
$stmt->bindParam(':price_id', $option['pr_no']);
$stmt->bindParam(':item_no', $option['id']);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $option['name']);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
@ -382,16 +389,17 @@ if(!$contract){
foreach($inBothOtherOptionIds as $bothotheroptionidx){
$option = array_values(array_filter($otherOptions, fn($el)=> $el['id'] == $bothotheroptionidx))[0];
$sql_str = "UPDATE pricereview_maintain_item SET item_spec=:item_spec, option_relate_spec=:option_relate_spec, item_unit_price = :item_unit_price, item_qty = :item_qty, item_price_bp = :item_price_bp, memo = :memo
WHERE mid = :apply_key AND item_no = :item_no AND item_group = 'E' ";
$sql_str = "UPDATE pricereview_maintain_item SET item_spec=:item_spec, price_id=:price_id , option_relate_spec=:option_relate_spec, item_unit_price = :item_unit_price, item_qty = :item_qty, item_price_bp = :item_price_bp, memo = :memo
WHERE (mid, item_no, item_group) = (:apply_key, :item_no, :item_group) ";
$item_group = "E";
$item_price_bp = $option['price']*$option['num'];
$option_relate_spec = $option['toElevator'][0]['id'];
$option_relate_spec = (count($option['toElevator']) > 0) ? $option['toElevator'][0]['id'] : null ;
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $apply_key);
$stmt->bindParam(':item_no', $option['pr_no']);
$stmt->bindParam(':apply_key', $apply_key);
$stmt->bindParam(':item_no', $option['id']);
$stmt->bindParam(':price_id', $option['pr_no']);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $option['name']);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
@ -402,10 +410,75 @@ if(!$contract){
$stmt->execute();
}
}
if($status == "YS"){
$sql_str = "INSERT INTO pricereview_maintain_sign ()"
$sql_str = "SELECT * FROM account WHERE accountid = :accountid";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':accountid', $creater);
$stmt->execute();
$account = $stmt->fetch(PDO::FETCH_ASSOC);
$manager = $account['manager'];
$sign1 = $manager . ",,";
$sign2 = "M0012,,";
$sign3 = "M0008,,";
$sign_total = 3;
//服務費大於0 或是 價率低於0.8送至總經理(M0006)
if(false & & ($serviceFee > 0 || ($total_sale_price / $total_price) < 0.8 ) ) {
$sign4 = "M0006,,";
$sign_total = 4;
}
$sql_str = "INSERT INTO pricereview_maintain_sign (mid, sign1, sign1_note, sign2, sign2_note, sign3, sign3_note, sign4, sign4_note, sign_total, created_at, created_by)
VALUES (:mid, :sign1, :sign1_note, :sign2, :sign2_note, :sign3, :sign3_note, :sign4, :sign4_note, :sign_total, :created_at, :created_by)";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $apply_key);
$stmt->bindParam(':sign1', $sign1);
$stmt->bindParam(':sign1_note', $sign1_note);
$stmt->bindParam(':sign2', $sign2);
$stmt->bindParam(':sign2_note', $sign2_note);
$stmt->bindParam(':sign3', $sign3);
$stmt->bindParam(':sign3_note', $sign3_note);
$stmt->bindParam(':sign4', $sign4);
$stmt->bindParam(':sign4_note', $sign4_note);
$stmt->bindParam(':sign_total', $sign_total);
$stmt->bindParam(':created_at', $current_date);
$stmt->bindParam(':created_by', $creater);
$stmt->execute();
include_once "./postFlow.php";
$form_key = null;
$createFlow = createFlow($apply_key, $manager, "A", "保養", $form_key);
include_once "./postSystemNotice.php";
$sql_str = "SELECT * FROM con_maintance_examine_apply WHERE apply_key = :apply_key ORDER BY create_at DESC LIMIT 1";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':apply_key', $apply_key);
$stmt->execute();
$contract = $stmt->fetch(PDO::FETCH_ASSOC);
$kind = 1;
$related_id = $apply_key;
$title = "保養價審待簽通知(".$vol_no."," .$customer. ")";
$content = "待簽核";
$content = "合約號:".$vol_no . "< br > ";
$content .= "客戶名稱:" . $customer . "< br > ";
$content .= "案件名稱:" . $case_name . "< br > ";
$content .= "時間" . $current_date;
// $content .= "< a href = './mkt/pricreviewCheck.php?id=".$mid." > 前往查看< / a > ";
$havaread = null;
$permissions = [$manager];
$creater = "system";
$create_at = $current_date;
$url = "cont/pricereviewCheck.php?id=" . $apply_key;
sendSystemNotice($kind, $related_id, $title, $content, $havaread, $permissions, $creater, $create_at, $url);
}
echo "success";
$conn->commit();
} catch (PDOException $e) {
$conn->rollBack();
echo $e->getMessage();
}