prepare($sql_str);
$stmt->bindParam(':contractno', $contractno);
$stmt->execute();
$main = $stmt->fetch(PDO::FETCH_ASSOC);
// -----處理檔案上傳 START-----
if(!empty($attatch1)){
$file = $attatch1;
$file_name = $file['name'];
$file_type = $file['type'];
$tmp_name = $file['tmp_name'];
$file_size = $file['size'];
$error = $file['error'];
$rand = strval(rand(1000,1000000));
$now = (string)date('YmdHis');
$imgsrc = 'm'.$now.$rand.$file_name;
$allow_ext = array('jpeg', 'jpg', 'png', 'gif','JPG','JPEG','PNG','pdf','PDF','docx', 'doc', 'DOCX', 'DOC', 'xlsx', 'xls', 'XLSX', 'XLS');
//設定上傳位置
$path = './pricereview-uploads/';
$attatch1_src = $path.$imgsrc;
if (!file_exists($path)) { mkdir($path); }
if ($error == 0) {
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
if (!in_array($ext, $allow_ext)) {
echo "";
exit('檔案類型不符合,請選擇 jpg、png、excel、word 、pdf檔案');
}
$result = move_uploaded_file($tmp_name, '.'.$attatch1_src);
} else {
//這裡表示上傳有錯誤, 匹配錯誤編號顯示對應的訊息
switch ($error) {
case 1: echo '上傳檔案超過 upload_max_filesize 容量最大值'; break;
case 2: echo '上傳檔案超過 post_max_size 總容量最大值'; break;
case 3: echo '檔案只有部份被上傳'; break;
case 4: echo '沒有檔案被上傳'; break;
case 6: echo '找不到主機端暫存檔案的目錄位置'; break;
case 7: echo '檔案寫入失敗'; break;
case 8: echo '上傳檔案被PHP程式中斷,表示主機端系統錯誤'; break;
}
}
}else{
if($main){
$attatch1_src = $main['attatch1'];
}
}
if(!empty($attatch2)){
$file = $attatch2;
$file_name = $file['name'];
$file_type = $file['type'];
$tmp_name = $file['tmp_name'];
$file_size = $file['size'];
$error = $file['error'];
$rand = strval(rand(1000,1000000));
$now = (string)date('YmdHis');
$imgsrc = 'm'.$now.$rand.$file_name;
$allow_ext = array('jpeg', 'jpg', 'png', 'gif','JPG','JPEG','PNG','pdf','PDF','docx', 'doc', 'DOCX', 'DOC', 'xlsx', 'xls', 'XLSX', 'XLS');
//設定上傳位置
$path = './pricereview-uploads/';
$attatch2_src = $path.$imgsrc;
if (!file_exists($path)) { mkdir($path); }
if ($error == 0) {
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
if (!in_array($ext, $allow_ext)) {
echo "";
exit('檔案類型不符合,請選擇 jpg、png、excel、word 、pdf檔案');
}
$result = move_uploaded_file($tmp_name, '.'.$attatch2_src);
} else {
//這裡表示上傳有錯誤, 匹配錯誤編號顯示對應的訊息
switch ($error) {
case 1: echo '上傳檔案超過 upload_max_filesize 容量最大值'; break;
case 2: echo '上傳檔案超過 post_max_size 總容量最大值'; break;
case 3: echo '檔案只有部份被上傳'; break;
case 4: echo '沒有檔案被上傳'; break;
case 6: echo '找不到主機端暫存檔案的目錄位置'; break;
case 7: echo '檔案寫入失敗'; break;
case 8: echo '上傳檔案被PHP程式中斷,表示主機端系統錯誤'; break;
}
}
}else{
if($main){
$attatch2_src = $main['attatch2'];
}
}
// -----處理檔案上傳 END-----
try{
$conn->beginTransaction();
if($main){
$mid = $main["id"];
$sql_str = "UPDATE pricereview_main SET price_lowest=:price_lowest, price_total=:price_total, price_rate=:price_rate, special_fee=:special_fee, predeal_date=:predeal_date, facilitok_date=:facilitok_date, penalty=:penalty, deposit_rate=:deposit_rate, keep_rate=:keep_rate, warranty_rate=:warranty_rate, memo=:memo,status=:status, attatch1=:attatch1, attatch2=:attatch2 WHERE id = :id ";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':id', $mid);
$stmt->bindParam(':price_lowest', $price_lowest);
$stmt->bindParam(':price_total', $price_total);
$stmt->bindParam(':price_rate', $price_rate);
$stmt->bindParam(':special_fee', $special_fee);
$stmt->bindParam(':predeal_date', $predeal_date);
$stmt->bindParam(':facilitok_date', $facilitok_date);
$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', $memo);
$stmt->bindParam(':status', $status);
$stmt->bindParam(':attatch1', $attatch1_src);
$stmt->bindParam(':attatch2', $attatch2_src);
$stmt->execute();
//整機單價
$sql_str = "SELECT * FROM pricereview_item WHERE mid = :mid AND item_group = 'A'";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->execute();
$items = $stmt->fetchAll(PDO::FETCH_ASSOC);
$oldElevators = array_filter($items, fn($el)=> $el['item_group'] == "A");
$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 pricereview_item WHERE mid = :mid AND item_no = :item_no AND item_group='A'";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':item_no', $oldidx);
$stmt->execute();
}
foreach($onlyInNewIds as $newidx){
$sql_str = "INSERT INTO pricereview_item (mid, price_id, item_no, item_group, item_spec, item_weight, item_op, item_unit_price, item_qty, item_price_bp, item_price_ct, note)
VALUES (:mid, :price_id, :item_no, :item_group, :item_spec, :item_weight, :item_op, :item_unit_price, :item_qty, :item_price_bp, :item_price_ct, :note)";
$elevator = array_values(array_filter($elevators, fn($el)=> $el['id'] == $newidx))[0];
$price_id = $elevator['facility_id'];
$item_no = $elevator['id'];
$item_group = "A";
// $item_spec = $elevator['specifications'] . "-" . $elevator['person'] . "*" . $elevator['stop'] . "-" . $elevator['open_converted'] . $elevator['speed'];
$item_spec = getSpec($elevator['specifications'], $elevator['person'], $elevator['stop'], $elevator['open_converted'], $elevator['speed']);
$item_weight = $elevator['weight'];
$item_op = $elevator['open_converted'];
$item_unit_price = $elevator['price'];
$item_qty = (int)$elevator['spec_num'];
$item_price_bp = (int)$elevator['price'] * (int)$elevator['spec_num'];
$item_price_ct = $elevator['spec_price'];
$note = $elevator['specifications'] . ',' . $elevator['person'] . ',' . $elevator['stop'] . ',' . $elevator['speed'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':price_id', $price_id);
$stmt->bindParam(':item_no', $item_no);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $item_spec);
$stmt->bindParam(':item_weight', $item_weight);
$stmt->bindParam(':item_op', $item_op);
$stmt->bindParam(':item_unit_price', $item_unit_price);
$stmt->bindParam(':item_qty', $item_qty);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->bindParam(':item_price_ct', $item_price_ct);
$stmt->bindParam(':note', $note);
$stmt->execute();
}
foreach($inBothIds as $bothidx){
$elevator = array_values(array_filter($elevators, fn($el)=> $el['id'] == $bothidx))[0];
$price_id = $elevator['facility_id'];
$item_no = $elevator['id'];
$item_group = "A";
// $item_spec = $elevator['specifications'] . "-" . $elevator['person'] . "*" . $elevator['stop'] . "-" . $elevator['open_converted'] . $elevator['speed'];
$item_spec = getSpec($elevator['specifications'], $elevator['person'], $elevator['stop'], $elevator['open_converted'], $elevator['speed']);
$item_weight = $elevator['weight'];
$item_op = $elevator['open_converted'];
$item_unit_price = $elevator['price'];
$item_qty = (int)$elevator['spec_num'];
$item_price_bp = (int)$elevator['price'] * (int)$elevator['spec_num'];
$item_price_ct = $elevator['spec_price'];
$note = $elevator['specifications'] . ',' . $elevator['person'] . ',' . $elevator['stop'] . ',' . $elevator['speed'];
$sql_str = "UPDATE pricereview_item SET price_id=:price_id, item_spec=:item_spec, item_weight=:item_weight, item_op=:item_op, item_unit_price=:item_unit_price, item_qty=:item_qty, item_price_bp=:item_price_bp, item_price_ct=:item_price_ct, note=:note WHERE item_no = :item_no AND mid = :mid AND item_group='A' ";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':item_no', $bothidx);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':price_id', $price_id);
$stmt->bindParam(':item_spec', $item_spec);
$stmt->bindParam(':item_weight', $item_weight);
$stmt->bindParam(':item_op', $item_op);
$stmt->bindParam(':item_unit_price', $item_unit_price);
$stmt->bindParam(':item_qty', $item_qty);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->bindParam(':item_price_ct', $item_price_ct);
$stmt->bindParam(':note', $note);
$stmt->execute();
}
//Option加價
$sql_str = "SELECT * FROM pricereview_item WHERE mid = :mid AND item_group = 'B'";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->execute();
$item_options = $stmt->fetchAll(PDO::FETCH_ASSOC);
$oldOptionsNo = (array)array_column($item_options, 'item_no');
$newOptioinNo = (array)array_column($options, 'key');
$onlyInOptionOldIds = array_values(array_diff($oldOptionsNo, $newOptioinNo));
$onlyInOptionNewIds = array_values(array_diff($newOptioinNo, $oldOptionsNo));
$inBothOptionIds = array_values(array_intersect($oldOptionsNo, $newOptioinNo));
foreach($onlyInOptionOldIds as $oldoptionidx){
$sql_str = "DELETE FROM pricereview_item WHERE mid = :mid AND item_no = :item_no AND item_group='B'";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':item_no', $oldoptionidx);
$stmt->execute();
}
foreach($onlyInOptionNewIds as $newoptionidx){
$option = array_values(array_filter($options, fn($el)=> $el['key'] == $newoptionidx))[0];
$sql_str = "INSERT INTO pricereview_item (mid, price_id, item_no, item_group, item_spec, option_relate_spec, item_unit_price, item_qty, item_price_bp)
VALUES (:mid, :price_id, :item_no, :item_group, :item_spec, :option_relate_spec, :item_unit_price, :item_qty, :item_price_bp)";
$price_id = $option['id'];
$item_no = $option['key'];
$item_group = "B";
$item_spec = $option['name'] . $option['memo'];
$option_relate_spec = $option['toElevator'][0]['id'];
$item_unit_price = $option['price'];
$item_qty = $option['qty'];
$item_price_bp = $option['price'] * $option['qty'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':price_id', $price_id);
$stmt->bindParam(':item_no', $item_no);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $item_spec);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
$stmt->bindParam(':item_unit_price', $item_unit_price);
$stmt->bindParam(':item_qty', $item_qty);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->execute();
}
foreach($inBothOptionIds as $bothoptionidx){
$option = array_values(array_filter($options, fn($el)=> $el['key'] == $bothoptionidx))[0];
$price_id = $option['id'];
$item_no = $option['key'];
$item_group = "B";
$item_spec = $option['name'] . $option['memo'];
$option_relate_spec = $option['toElevator'][0]['id'];
$item_unit_price = $option['price'];
$item_qty = $option['qty'];
$item_price_bp = $option['price'] * $option['qty'];
$sql_str = "UPDATE pricereview_item SET price_id = :price_id, item_spec=:item_spec, item_unit_price = :item_unit_price, item_qty = :item_qty, item_price_bp = :item_price_bp, option_relate_spec=:option_relate_spec WHERE mid = :mid AND item_no = :item_no AND item_group='B'";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':price_id', $price_id);
$stmt->bindParam(':item_no', $item_no);
$stmt->bindParam(':item_spec', $item_spec);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
$stmt->bindParam(':item_unit_price', $item_unit_price);
$stmt->bindParam(':item_qty', $item_qty);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->execute();
}
//除外項目
$sql_str = "SELECT * FROM pricereview_item WHERE mid = :mid AND item_group = 'E'";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$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');
$onlyInOtherOptionOldIds = array_values(array_diff($oldOtherOptionsNo, $newOtherOptioinNo));
$onlyInOtherOptionNewIds = array_values(array_diff($newOtherOptioinNo, $oldOtherOptionsNo));
$inBothOtherOptionIds = array_values(array_intersect($oldOtherOptionsNo, $newOtherOptioinNo));
// echo "old:".json_encode($onlyInOtherOptionOldIds); //delete
// echo "new:".json_encode($onlyInOtherOptionNewIds); //insert
// echo "both:".json_encode($inBothOtherOptionIds); // update
foreach($onlyInOtherOptionOldIds as $oldotheroptionidx){
$sql_str = "DELETE FROM pricereview_item WHERE mid = :mid AND item_no = :item_no AND item_group='E'";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$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_item (mid, item_no, item_group, item_spec, option_relate_spec, item_unit_price, item_qty, item_price_bp, note)
VALUES (:mid, :item_no, :item_group, :item_spec, :option_relate_spec, :item_unit_price, :item_qty, :item_price_bp, :note)";
$item_no = $option['id'];
$item_group = "E";
$item_spec = $option['name'];
$option_relate_spec = $option['toElevator'][0]['id'];
$item_unit_price = $option['price'];
$item_qty = $option['num'];
$item_price_bp = $option['price'] * $option['num'];
$note = $option['pr_no'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':item_no', $item_no);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $item_spec);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
$stmt->bindParam(':item_unit_price', $item_unit_price);
$stmt->bindParam(':item_qty', $item_qty);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->bindParam(':note', $note);
$stmt->execute();
}
foreach($inBothOtherOptionIds as $bothotheroptionidx){
$option = array_values(array_filter($otherOptions, fn($el)=> $el['id'] == $bothotheroptionidx))[0];
$item_no = $option['id'];
$item_group = "E";
$item_spec = $option['name'];
$option_relate_spec = $option['toElevator'][0]['id'];
$item_unit_price = $option['price'];
$item_qty = $option['num'];
$item_price_bp = $option['price'] * $option['num'];
$note = $option['pr_no'];
$sql_str = "UPDATE pricereview_item SET item_spec=:item_spec, item_unit_price = :item_unit_price, item_qty = :item_qty, item_price_bp = :item_price_bp, option_relate_spec=:option_relate_spec, note=:note WHERE mid = :mid AND item_no = :item_no AND item_group='E'";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':item_no', $item_no);
$stmt->bindParam(':item_spec', $item_spec);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
$stmt->bindParam(':item_unit_price', $item_unit_price);
$stmt->bindParam(':item_qty', $item_qty);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->bindParam(':note', $note);
$stmt->execute();
}
//拆梯價格
$sql_str = "SELECT * FROM pricereview_item WHERE mid = :mid AND item_group = 'C'";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->execute();
$item_demolish_options = $stmt->fetchAll(PDO::FETCH_ASSOC);
$oldDemolishOptionsNo = (array)array_column($item_demolish_options, 'item_no');
$newDemolishOptioinNo = (array)array_column($demolishOptions, 'id');
$onlyInDemolishOptionOldIds = array_values(array_diff($oldDemolishOptionsNo, $newDemolishOptioinNo));
$onlyInDemolishOptionNewIds = array_values(array_diff($newDemolishOptioinNo, $oldDemolishOptionsNo));
$inBothDemolishOptionIds = array_values(array_intersect($oldDemolishOptionsNo, $newDemolishOptioinNo));
// echo "old:".json_encode($onlyInDemolishOptionOldIds); //delete
// echo "new:".json_encode($onlyInDemolishOptionNewIds); //insert
// echo "both:".json_encode($inBothDemolishOptionIds); // update
foreach($onlyInDemolishOptionOldIds as $olddemolishoptionidx){
$sql_str = "DELETE FROM pricereview_item WHERE mid = :mid AND item_no = :item_no AND item_group='C'";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':item_no', $olddemolishoptionidx);
$stmt->execute();
}
foreach($onlyInDemolishOptionNewIds as $newdemolishoptionidx){
$option = array_values(array_filter($demolishOptions, fn($el)=> $el['id'] == $newdemolishoptionidx))[0];
$sql_str = "INSERT INTO pricereview_item (mid, price_id, item_no, item_group, item_spec, option_relate_spec, item_unit_price, item_qty, item_price_bp, note)
VALUES (:mid, :price_id, :item_no, :item_group, :item_spec, :option_relate_spec, :item_unit_price, :item_qty, :item_price_bp, :note)";
$item_no = $option['id'];
$item_group = "C";
$item_spec = $option['person'];
$option_relate_spec = $option['toElevator'][0]['id'];
$item_unit_price = $option['price'];
$item_qty = (int)$option['qty'];
$item_price_bp = (int)$option['price'] * (int)$option['qty'];
$note = $option['floor'];
$price_id = $option['demolish_id'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':item_no', $item_no);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $item_spec);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
$stmt->bindParam(':item_unit_price', $item_unit_price);
$stmt->bindParam(':item_qty', $item_qty);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->bindParam(':note', $note);
$stmt->bindParam(':price_id', $price_id);
$stmt->execute();
}
foreach($inBothDemolishOptionIds as $bothdemolishoptionidx){
$option = array_values(array_filter($demolishOptions, fn($el)=> $el['id'] == $bothdemolishoptionidx))[0];
$item_no = $option['id'];
$item_group = "C";
$item_spec = $option['person'];
$option_relate_spec = $option['toElevator'][0]['id'];
$item_unit_price = $option['price'];
$item_qty = $option['qty'];
$item_price_bp = $option['price'] * $option['qty'];
$note = $option['floor'];
$price_id = $option['demolish_id'];
$sql_str = "UPDATE pricereview_item SET item_spec=:item_spec, price_id=:price_id, item_unit_price = :item_unit_price, item_qty = :item_qty, item_price_bp = :item_price_bp, option_relate_spec=:option_relate_spec, note=:note WHERE mid = :mid AND item_no = :item_no AND item_group='C'";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':item_no', $item_no);
$stmt->bindParam(':item_spec', $item_spec);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
$stmt->bindParam(':item_unit_price', $item_unit_price);
$stmt->bindParam(':item_qty', $item_qty);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->bindParam(':note', $note);
$stmt->bindParam(':price_id', $price_id);
$stmt->execute();
}
$sql_str = "UPDATE pricereview_pay SET pay_scale = :pay_scale, pay_amount = :pay_amount, pay_period = :pay_period WHERE mid = :mid AND pay_kind = :pay_kind";
foreach($paymentRatio as $key=>$pay){
$pay_kind = $key +1;
$pay_scale = $pay['scale'] == '' ? 0 : $pay['scale'];
$pay_amount = $pay['scale']/100 * $price_total;
$pay_period = ($pay['scale'] == '' || $pay['scale'] == 0) ? '' :$pay['ticket'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':pay_kind', $pay_kind);
$stmt->bindParam(':pay_scale', $pay_scale);
$stmt->bindParam(':pay_amount', $pay_amount);
$stmt->bindParam(':pay_period', $pay_period);
$stmt->execute();
}
}else{
$sql_str = "INSERT INTO pricereview_main (contractno, ekind, person, company, case_name, address, compete, price_lowest, price_total, price_rate, special_fee, predeal_date, facilitok_date, penalty, deposit_rate, keep_rate, warranty_rate, memo, status, attatch1, attatch2, creater, create_at)
VALUES (:contractno, :ekind, :person, :company, :case_name, :address, :compete, :price_lowest, :price_total, :price_rate, :special_fee, :predeal_date, :facilitok_date, :penalty, :deposit_rate, :keep_rate, :warranty_rate, :memo, :status, :attatch1, :attatch2, :creater, :create_at)";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':contractno', $contractno);
$stmt->bindParam(':ekind', $ekind);
$stmt->bindParam(':person', $person);
$stmt->bindParam(':company', $company);
$stmt->bindParam(':case_name', $case_name);
$stmt->bindParam(':address', $address);
$stmt->bindParam(':compete', $compete);
$stmt->bindParam(':price_lowest', $price_lowest);
$stmt->bindParam(':price_total', $price_total);
$stmt->bindParam(':price_rate', $price_rate);
$stmt->bindParam(':special_fee', $special_fee);
$stmt->bindParam(':predeal_date', $predeal_date);
$stmt->bindParam(':facilitok_date', $facilitok_date);
$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', $memo);
$stmt->bindParam(':status', $status);
$stmt->bindParam(':attatch1', $attatch1_src);
$stmt->bindParam(':attatch2', $attatch2_src);
$stmt->bindParam(':creater', $creater);
$stmt->bindParam(':create_at', $create_at);
$stmt->execute();
$mid = $conn->lastInsertId();
//insert 整機單價
$sql_str = "INSERT INTO pricereview_item (mid, price_id, item_no, item_group, item_spec, item_weight, item_op, item_unit_price, item_qty, item_price_bp, item_price_ct, note)
VALUES (:mid, :price_id, :item_no, :item_group, :item_spec, :item_weight, :item_op, :item_unit_price, :item_qty, :item_price_bp, :item_price_ct, :note)";
foreach($elevators as $elevator){
$price_id = $elevator['facility_id'];
$item_no = $elevator['id'];
$item_group = "A";
// $item_spec = $elevator['specifications'] . "-" . $elevator['person'] . "*" . $elevator['stop'] . "-" . $elevator['open_converted'] . $elevator['speed'];
$item_op = $elevator['open_converted'];
$item_weight = $elevator['weight'];
$item_spec = getSpec($elevator['specifications'], $elevator['person'], $elevator['stop'], $elevator['open_converted'], $elevator['speed']);
$item_unit_price = $elevator['price'];
$item_qty = (int)$elevator['spec_num'];
$item_price_bp = (int)$elevator['price'] * (int)$elevator['spec_num'];
$item_price_ct = $elevator['spec_price'];
$note = $elevator['specifications'] . ',' . $elevator['person'] . ',' . $elevator['stop'] . ',' . $elevator['speed'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':price_id', $price_id);
$stmt->bindParam(':item_no', $item_no);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $item_spec);
$stmt->bindParam(':item_weight', $item_weight);
$stmt->bindParam(':item_op', $item_op);
$stmt->bindParam(':item_unit_price', $item_unit_price);
$stmt->bindParam(':item_qty', $item_qty);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->bindParam(':item_price_ct', $item_price_ct);
$stmt->bindParam(':note', $note);
$stmt->execute();
}
//insert options 加價
$sql_str = "INSERT INTO pricereview_item (mid, price_id, item_no, item_group, item_spec, option_relate_spec, item_unit_price, item_qty, item_price_bp)
VALUES (:mid, :price_id, :item_no, :item_group, :item_spec, :option_relate_spec, :item_unit_price, :item_qty, :item_price_bp)";
foreach($options as $option){
$price_id = $option['id'];
$item_no = $option['key'];
$item_group = "B";
$item_spec = $option['name'] . $option['memo'];
$option_relate_spec = $option['toElevator'][0]['id'];
$item_unit_price = $option['price'];
$item_qty = $option['qty'];
$item_price_bp = $option['price'] * $option['qty'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':price_id', $price_id);
$stmt->bindParam(':item_no', $item_no);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $item_spec);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
$stmt->bindParam(':item_unit_price', $item_unit_price);
$stmt->bindParam(':item_qty', $item_qty);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->execute();
}
//insert 除外項目
$sql_str = "INSERT INTO pricereview_item (mid, item_no, item_group, item_spec, option_relate_spec, item_unit_price, item_qty, item_price_bp, note)
VALUES (:mid, :item_no, :item_group, :item_spec, :option_relate_spec, :item_unit_price, :item_qty, :item_price_bp, :note)";
foreach($otherOptions as $option){
$item_no = $option['id'];
$item_group = "E";
$item_spec = $option['name'];
$option_relate_spec = $option['toElevator'][0]['id'];
$item_unit_price = $option['price'];
$item_qty = $option['num'];
$item_price_bp = $option['price'] * $option['num'];
$note = $option['pr_no'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':item_no', $item_no);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $item_spec);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
$stmt->bindParam(':item_unit_price', $item_unit_price);
$stmt->bindParam(':item_qty', $item_qty);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->bindParam(':note', $note);
$stmt->execute();
}
$sql_str = "INSERT INTO pricereview_item (mid, item_no, item_group, item_spec, option_relate_spec, item_unit_price, item_qty, item_price_bp)
VALUES (:mid, :item_no, :item_group, :item_spec, :option_relate_spec, :item_unit_price, :item_qty, :item_price_bp)";
//insert 保養延長
foreach(json_decode($maintainOptions, true) as $option){
$item_no = $option['id'] + 1;
$item_group = "D";
$item_spec = $option['toElevator'][0]['model'];
$option_relate_spec = $option['toElevator'][0]['id'];
$item_unit_price = $option['price'];
$item_qty = $option['num'];
$item_price_bp = $option['price'] * $option['num'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':item_no', $item_no);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $item_spec);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
$stmt->bindParam(':item_unit_price', $item_unit_price);
$stmt->bindParam(':item_qty', $item_qty);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->execute();
}
//insert 拆梯價格 demolishOptions
$sql_str = "INSERT INTO pricereview_item (mid, price_id, item_no, item_group, item_spec, option_relate_spec, item_unit_price, item_qty, item_price_bp, note)
VALUES (:mid, :price_id, :item_no, :item_group, :item_spec, :option_relate_spec, :item_unit_price, :item_qty, :item_price_bp, :note)";
foreach($demolishOptions as $option){
$item_no = $option['id'];
$item_group = "C";
$item_spec = $option['person'];
$option_relate_spec = $option['toElevator'][0]['id'];
$item_unit_price = $option['price'];
$item_qty = (int)$option['qty'];
$item_price_bp = $option['price'] * (int)$option['qty'];
$note = $option['floor'];
$price_id = $option['demolish_id'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':item_no', $item_no);
$stmt->bindParam(':item_group', $item_group);
$stmt->bindParam(':item_spec', $item_spec);
$stmt->bindParam(':option_relate_spec', $option_relate_spec);
$stmt->bindParam(':item_unit_price', $item_unit_price);
$stmt->bindParam(':item_qty', $item_qty);
$stmt->bindParam(':item_price_bp', $item_price_bp);
$stmt->bindParam(':note', $note);
$stmt->bindParam(':price_id', $price_id);
$stmt->execute();
}
//insert 付款比例
$sql_str = "INSERT INTO pricereview_pay (mid, pay_kind, pay_scale, pay_amount, pay_period)
VALUES (:mid, :pay_kind, :pay_scale, :pay_amount, :pay_period)";
foreach($paymentRatio as $key=>$pay){
$pay_kind = $key +1;
$pay_scale = $pay['scale'] == '' ? 0 : $pay['scale'];
$pay_amount = $pay['scale']/100 * $price_total;
$pay_period = ($pay['scale'] == '' || $pay['scale'] == 0) ? '' :$pay['ticket'];
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':pay_kind', $pay_kind);
$stmt->bindParam(':pay_scale', $pay_scale);
$stmt->bindParam(':pay_amount', $pay_amount);
$stmt->bindParam(':pay_period', $pay_period);
$stmt->execute();
}
}
if($status == 'YS'){
$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'];
if($manager == "M0029" && $ekind == "新梯"){
$manager = "M0137";
}
$sign1 = "$manager,,"; //區處長
$sign2 =$ekind == '汰改'? "M0012,," : NULL; // 業務承辦人
$sign3 = $ekind == '汰改' ? "M0008,," : "M0060,,"; //業務部協理
$signArr = [
$manager,
$sign2,
$sign3,
];
$sign_total = count(array_filter($signArr, fn($item)=> $item != NULL));
//是否呈核至總經理(M0006)
if($price_rate < 80 || $special_fee > 0){
$sign4 = "M0006,,";
$sign_total += 1;
}else{
$sign4 = NULL;
}
$sql_str = "INSERT INTO pricereview_sign (mid, sign1, sign2, sign3, sign4, sign_total)
VALUES (:mid, :sign1, :sign2, :sign3, :sign4, :sign_total)";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':mid', $mid);
$stmt->bindParam(':sign1', $sign1);
$stmt->bindParam(':sign2', $sign2);
$stmt->bindParam(':sign3', $sign3);
$stmt->bindParam(':sign4', $sign4);
$stmt->bindParam(':sign_total', $sign_total);
$stmt->execute();
//發送系統通知
include_once "./postSystemNotice.php";
$sql_str = "SELECT * FROM pricereview_main WHERE id = :id ORDER BY id DESC LIMIT 1";
$stmt = $conn->prepare($sql_str);
$stmt->bindParam(':id', $mid);
$stmt->execute();
$contract = $stmt->fetch(PDO::FETCH_ASSOC);
$kind = 1;
$related_id = $mid;
$title = $ekind . "價審待簽通知(".$contract['contractno']."," .$contract['company']. ")";
$content = "待簽核";
$content = "合約號:".$contract['contractno'] . "
";
$content .= "客戶名稱:" . $contract['company'] . "
";
$content .= "案件名稱:" . $contract['case_name'] . "
";
$content .= "時間" . date("Y-m-d H:i:s");
// $content .= "getMessage());
}
function getSpec($spec, $person, $stop, $open, $speed){
$weightArr = array(
6 => 450,
8 => 550,
9 => 600,
10 => 700,
11 => 750,
12 => 800,
13 => 900,
15 => 1000,
17 => 1150,
20 => 1350,
24 => 1600,
);
if($spec == 'M1' || $spec=='M1+M4(1:1)' || $spec == 'M1+M4(2:1)'){
$model = "P" . $person . "-" . $weightArr[$person] . "-" . $open . "-" . $speed . "-" . $stop;
}else{
$model = $spec . "-" . $person . "*" . $stop . "-" . $open . $speed;
}
return $model;
}