You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

411 lines
22 KiB

<?php
require_once "../../mkt/conn.php";
date_default_timezone_set('Asia/Taipei');
$vol_no = $_POST['vol_no'];
$apply_key = $_POST['apply_key'];
$customer = $_POST['customer'];
$address = $_POST['address'];
$case_name = $_POST['case_name'];
$brand = $_POST['brand'];
$num = $_POST['num'];
$salesman = $_POST['salesman'];
$contract_begin_date = $_POST['contract_begin_date'];
$contract_end_date = $_POST['contract_end_date'];
$contract_kind = $_POST['contract_kind'];
$progress_remark = $_POST['progress_status'];
$serviceFee = $_POST['serviceFee'];
$penalty = $_POST['penalty'];
$deposit_rate = $_POST['deposit_rate'];
$keep_rate = $_POST['keep_rate'];
$warranty_rate = $_POST['warranty_rate'];
$remark = $_POST['remark'];
$creater = $_POST['creater'];
$pay_method = $_POST['pay_method'];
$status = $_POST['status'];
$elevators = json_decode($_POST['elevators'], true);
$otherOptions = json_decode($_POST['otherOptions'], true);
$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";
$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);
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)";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':apply_key', $apply_key);
$stmt->bindParam(':vol_no', $vol_no);
$stmt->bindParam(':customer', $customer);
$stmt->bindParam(':address', $address);
$stmt->bindParam(':case_name', $case_name);
$stmt->bindParam(':brand', $brand);
$stmt->bindParam(':num', $num);
$stmt->bindParam(':salesman', $salesman);
$stmt->bindParam(':maintain_kind', $maintain_kind);
$stmt->bindParam(':contract_begin_date', $contract_begin_date);
$stmt->bindParam(':contract_end_date', $contract_end_date);
$stmt->bindParam(':contract_kind', $contract_kind);
$stmt->bindParam(':payment_kind', $payment_kind);
$stmt->bindParam(':progress_remark', $progress_remark);
$stmt->bindParam(':service_fee', $serviceFee);
$stmt->bindParam(':total_price', $total_price);
$stmt->bindParam(':total_sale_price', $total_sale_price);
$stmt->bindParam(':penalty', $penalty);
$stmt->bindParam(':deposit_rate', $deposit_rate);
$stmt->bindParam(':keep_rate', $keep_rate);
$stmt->bindParam(':warranty_rate', $warranty_rate);
$stmt->bindParam(':memo', $remark);
$stmt->bindParam(':status', $status);
$stmt->bindParam(':creater', $creater);
$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) ";
foreach($elevators as $elevator){
if(empty($elevator['permitNumber'])) $register_code = "A";
if($elevator['spec'] == "MAE100") $elevator_kind = "A";
if($elevator['spec'] == "MAF100") $elevator_kind = "B";
if($elevator['spec'] == "MAM200") $elevator_kind = "D";
if($elevator['spec'] == "MAH100") $elevator_kind = "E";
$speed_governors_check_expense = null;
$maintain_times = 1;
$commission_expense = null;
$management_expense = null;
$service_expense = ($elevator['sale_price'] / $total_sale_price) * $serviceFee;
$cmstatus = "Y";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':apply_key', $apply_key);
$stmt->bindParam(':item_no', $elevator['id']);
$stmt->bindParam(':register_code', $register_code);
$stmt->bindParam(':elevator_brand', $elevator['brand']);
$stmt->bindParam(':elevator_kind', $elevator_kind);
$stmt->bindParam(':spec', $elevator['spec']);
$stmt->bindParam(':weight', $elevator['weight']);
$stmt->bindParam(':speed', $elevator['speed']);
$stmt->bindParam(':stop', $elevator['stop']);
$stmt->bindParam(':floors', $elevator['stop']);
$stmt->bindParam(':persons', $elevator['person']);
$stmt->bindParam(':elevator_num', $elevator['qty']);
$stmt->bindParam(':useful_years', $elevator['checkYear']);
$stmt->bindParam(':last_check_date', $elevator['lastDate']);
$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_method', $elevator['method']);
$stmt->bindParam(':stand_price', $elevator['price']);
$stmt->bindParam(':contract_price', $elevator['sale_price']);
$stmt->bindParam(':sold_price', $elevator['sale_price']);
$stmt->bindParam(':commission_expense', $elevator['commission_expense']);
$stmt->bindParam(':management_expense', $elevator['management_expense']);
$stmt->bindParam(':annual_survey_expense', $elevator['yearCheckFee']);
$stmt->bindParam(':service_expense', $elevator['service_expense']);
$stmt->bindParam(':cmstatus', $cmstatus);
$stmt->bindParam(':updated_at', $updated_at);
$stmt->bindParam(':creater', $creater);
$stmt->bindParam(':created_at', $created_at);
$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)";
foreach($otherOptions as $option){
$item_price_bp = $option['price']*$option['num'];
$option_relate_spec = $option['toElevator'][0]['id'];
$item_group = "E";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $apply_key);
$stmt->bindParam(':item_no', $option['pr_no']);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $option['name']);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
$stmt->bindParam(':item_unit_price', $option['price']);
$stmt->bindParam(':item_qty', $option['num']);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->bindParam(':memo', $option['memo']);
$stmt->execute();
}
}else{
$sql_str = "UPDATE con_maintance_examine_apply SET
case_name=:case_name,
num=:num,
maintain_kind=:maintain_kind,
contract_kind=:contract_kind,
payment_kind=:payment_kind,
contract_begin_date=:contract_begin_date,
contract_end_date=:contract_end_date,
service_fee=:service_fee,
total_price=:total_price,
total_sale_price=:total_sale_price,
penalty=:penalty,
deposit_rate=:deposit_rate,
keep_rate=:keep_rate,
warranty_rate=:warranty_rate,
remark=:remark,
status=:status,
updated_at=:updated_at
progress_remark=:progress_remark
WHERE apply_key=:apply_key AND vol_no=:vol_no";
$stmt->bindParam(':apply_key', $apply_key);
$stmt->bindParam(':vol_no', $vol_no);
$stmt->bindParam(':case_name', $case_name);
$stmt->bindParam(':num', $num);
$stmt->bindParam(':maintain_kind', $maintain_kind);
$stmt->bindParam(':contract_begin_date', $contract_begin_date);
$stmt->bindParam(':contract_end_date', $contract_end_date);
$stmt->bindParam(':contract_kind', $contract_kind);
$stmt->bindParam(':payment_kind', $payment_kind);
$stmt->bindParam(':progress_remark', $progress_remark);
$stmt->bindParam(':service_fee', $serviceFee);
$stmt->bindParam(':total_price', $total_price);
$stmt->bindParam(':total_sale_price', $total_sale_price);
$stmt->bindParam(':penalty', $penalty);
$stmt->bindParam(':deposit_rate', $deposit_rate);
$stmt->bindParam(':keep_rate', $keep_rate);
$stmt->bindParam(':warranty_rate', $warranty_rate);
$stmt->bindParam(':memo', $remark);
$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";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':apply_key', $apply_key);
$stmt->bindParam(':vol_no', $vol_no);
$stmt->execute();
$items = $stmt->fetchAll(PDO::FETCH_ASSOC);
$oldItemsNo = (array)array_column($oldElevators, 'item_no');
$newItemsNo = (array)array_column($elevators, 'id');
$onlyInOldIds = array_values(array_diff($oldItemsNo, $newItemsNo)); // 只有舊的有,新傳入的沒有,所以刪除(delete)
$onlyInNewIds = array_values(array_diff($newItemsNo, $oldItemsNo)); //只有新傳入的有,舊的沒有,所以新增(insert)
$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";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':apply_key', $apply_key);
$stmt->bindParam(':item_no', $oldidx);
$stmt->bindParam(':vol_no', $vol_no);
$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) ";
$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";
if($elevator['spec'] == "MAF100") $elevator_kind = "B";
if($elevator['spec'] == "MAM200") $elevator_kind = "D";
if($elevator['spec'] == "MAH100") $elevator_kind = "E";
$speed_governors_check_expense = null;
$maintain_times = 1;
$commission_expense = null;
$management_expense = null;
$service_expense = ($elevator['sale_price'] / $total_sale_price) * $serviceFee;
$cmstatus = "Y";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':apply_key', $apply_key);
$stmt->bindParam(':item_no', $elevator['id']);
$stmt->bindParam(':register_code', $register_code);
$stmt->bindParam(':elevator_brand', $elevator['brand']);
$stmt->bindParam(':elevator_kind', $elevator_kind);
$stmt->bindParam(':spec', $elevator['spec']);
$stmt->bindParam(':weight', $elevator['weight']);
$stmt->bindParam(':speed', $elevator['speed']);
$stmt->bindParam(':stop', $elevator['stop']);
$stmt->bindParam(':floors', $elevator['stop']);
$stmt->bindParam(':persons', $elevator['person']);
$stmt->bindParam(':elevator_num', $elevator['qty']);
$stmt->bindParam(':useful_years', $elevator['checkYear']);
$stmt->bindParam(':last_check_date', $elevator['lastDate']);
$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_method', $elevator['method']);
$stmt->bindParam(':stand_price', $elevator['price']);
$stmt->bindParam(':contract_price', $elevator['sale_price']);
$stmt->bindParam(':sold_price', $elevator['sale_price']);
$stmt->bindParam(':commission_expense', $elevator['commission_expense']);
$stmt->bindParam(':management_expense', $elevator['management_expense']);
$stmt->bindParam(':annual_survey_expense', $elevator['yearCheckFee']);
$stmt->bindParam(':service_expense', $elevator['service_expense']);
$stmt->bindParam(':cmstatus', $cmstatus);
$stmt->bindParam(':updated_at', $updated_at);
$stmt->bindParam(':creater', $creater);
$stmt->bindParam(':created_at', $created_at);
$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,
elevator_kind=:elevator_kind,
spec=:spec,
weight=:weight,
speed=:speed,
stop=:stop,
floors=:floors,
persons=:persons,
elevator_num=:elevator_num,
useful_years=:useful_years,
last_check_date=:last_check_date,
speed_governors_check_expense=:speed_governors_check_expense,
maintain_times=:maintain_times,
is_m1_bundle=:is_m1_bundle,
maintain__months=:maintain__months,
maintain_period=:maintain_period,
maintain_method=:maintain_method,
stand_price=:stand_price,
contract_price=:contract_price,
sold_price=:sold_price,
commission_expense=:commission_expense,
management_expense=:management_expense,
annual_survey_expense=:annual_survey_expense,
service_expense=:service_expense,
cmstatus=:cmstatus,
updated_at=:updated_at
WHERE apply_key=:apply_key AND item_no=:item_no";
$elevator = array_values(array_filter($elevators, fn($el)=> $el['id'] == $bothidx))[0];
$register_code = empty($elevator['permitNumber']) ? "A" : $elevator['permitNumber'];
if($elevator['spec'] == "MAE100") $elevator_kind = "A";
if($elevator['spec'] == "MAF100") $elevator_kind = "B";
if($elevator['spec'] == "MAM200") $elevator_kind = "D";
if($elevator['spec'] == "MAH100") $elevator_kind = "E";
$speed_governors_check_expense = null;
$maintain_times = 1;
$commission_expense = null;
$management_expense = null;
$service_expense = ($elevator['sale_price'] / $total_sale_price) * $serviceFee;
$cmstatus = "Y";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':apply_key', $apply_key);
$stmt->bindParam(':register_code', $register_code);
$stmt->bindParam(':elevator_brand', $elevator['brand']);
$stmt->bindParam(':elevator_kind', $elevator_kind);
$stmt->bindParam(':spec', $elevator['spec']);
$stmt->bindParam(':weight', $elevator['weight']);
$stmt->bindParam(':speed', $elevator['speed']);
$stmt->bindParam(':stop', $elevator['stop']);
$stmt->bindParam(':floors', $elevator['stop']);
$stmt->bindParam(':persons', $elevator['person']);
$stmt->bindParam(':elevator_num', $elevator['qty']);
$stmt->bindParam(':useful_years', $elevator['checkYear']);
$stmt->bindParam(':last_check_date', $elevator['lastDate']);
$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_method', $elevator['method']);
$stmt->bindParam(':stand_price', $elevator['price']);
$stmt->bindParam(':contract_price', $elevator['sale_price']);
$stmt->bindParam(':sold_price', $elevator['sale_price']);
$stmt->bindParam(':commission_expense', $elevator['commission_expense']);
$stmt->bindParam(':management_expense', $elevator['management_expense']);
$stmt->bindParam(':annual_survey_expense', $elevator['yearCheckFee']);
$stmt->bindParam(':service_expense', $elevator['service_expense']);
$stmt->bindParam(':cmstatus', $cmstatus);
$stmt->bindParam(':updated_at', $updated_at);
$stmt->bindParam(':item_no', $bothidx);
$stmt->execute();
}
//除外項目
$sql_str = "SELECT * FROM pricereview_maintain_item WHERE mid = :apply_key AND item_group = 'E'";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $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));
$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'";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $apply_key);
$stmt->bindParam(':item_no', $oldotheroptionidx);
$stmt->execute();
}
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)";
$item_price_bp = $option['price']*$option['num'];
$option_relate_spec = $option['toElevator'][0]['id'];
$item_group = "E";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $apply_key);
$stmt->bindParam(':item_no', $option['pr_no']);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $option['name']);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
$stmt->bindParam(':item_unit_price', $option['price']);
$stmt->bindParam(':item_qty', $option['num']);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->bindParam(':memo', $option['memo']);
$stmt->execute();
}
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'";
$item_group = "E";
$item_price_bp = $option['price']*$option['num'];
$option_relate_spec = $option['toElevator'][0]['id'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $apply_key);
$stmt->bindParam(':item_no', $option['pr_no']);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $option['name']);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
$stmt->bindParam(':item_unit_price', $option['price']);
$stmt->bindParam(':item_qty', $option['num']);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->bindParam(':memo', $option['memo']);
$stmt->execute();
}
}
if($status == "YS"){
}
echo "success";