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.
 
 
 
 
 
 

135 lines
6.5 KiB

<?php
require_once("../conn.php");
include_once("./upload_chk.php");
ini_set ( 'date.timezone' , 'Asia/Taipei' );
// echo json_encode(explode(',', $_POST['deletefiles']));
if(isset($_POST['contractno']) && $_POST['contractno']!="" && isset($_POST["id"]) && $_POST['id']!=""){
try{
$created_at = date('Y-m-d H:i:s');
$created_by = $_POST['user_id'];
$id = $_POST["id"];
$contract_no = !empty($_POST['contractno']) ? $_POST['contractno'] : null;
$customer = !empty($_POST['customer']) ? $_POST['customer'] : null;
$manager = !empty($_POST['manager']) ? $_POST['manager'] : null;
$vat = !empty($_POST['vat']) ? $_POST['vat'] : null;
$case_name = !empty($_POST['case_name']) ? $_POST['case_name'] : null;
$linkman = !empty($_POST['linkman']) ? $_POST['linkman'] : null;
$lm_tel = !empty($_POST['lm_tel']) ? $_POST['lm_tel'] : null;
$address = !empty($_POST['address']) ? $_POST['address'] : null;
$salesman = !empty($_POST['salesman']) ? $_POST['salesman'] : null;
$qc = !empty($_POST['qc']) ? $_POST['qc'] : null;
$deletefiles = !empty($_POST['deletefiles']) ? $_POST['deletefiles'] : null;
$files_id = !empty($_POST['files_id']) ? $_POST['files_id'] : null;
$files = !empty($_FILES['files']) ? $_FILES['files'] : null;
$deletefilesArr = explode(',', $_POST['deletefiles']);
$fail_arr = [];
if(empty($contract_no)) $fail_arr[] = '合約號為必填';
if(empty($customer)) $fail_arr[] = '客戶名稱為必填';
if(empty($manager)) $fail_arr[] = '負責人為必填';
if(empty($vat)) $fail_arr[] = '統編/身分證為必填';
if(empty($case_name)) $fail_arr[] = '案名為必填';
if(empty($linkman)) $fail_arr[] = '聯絡人為必填';
if(empty($lm_tel)) $fail_arr[] = '聯絡人電話為必填';
if(empty($address)) $fail_arr[] = '地址為必填';
if(empty($salesman)) $fail_arr[] = '營業員為必填';
if(empty($qc)) $fail_arr[] = '請選擇QC或管檢';
if(count($fail_arr) > 0) {
header("HTTP/1.1 422 Unprocessable Entity");
echo json_encode($fail_arr);
exit();
}
$conn->beginTransaction();
$sql_str = "UPDATE contract_m_signed_back SET contract_no=:contract_no, customer=:customer, manager=:manager, vat=:vat, case_name=:case_name, linkman=:linkman, lm_tel=:lm_tel, address=:address, salesman=:salesman, qc_official_type=:qc WHERE id = :id";
$stmt = $conn -> prepare($sql_str);
$stmt -> bindParam(':contract_no' ,$contract_no);
$stmt -> bindParam(':customer' ,$customer);
$stmt -> bindParam(':manager' ,$manager);
$stmt -> bindParam(':vat' ,$vat);
$stmt -> bindParam(':case_name' ,$case_name);
$stmt -> bindParam(':linkman' ,$linkman);
$stmt -> bindParam(':lm_tel' ,$lm_tel);
$stmt -> bindParam(':address' ,$address);
$stmt -> bindParam(':salesman' ,$salesman);
$stmt -> bindParam(':qc' ,$qc);
$stmt -> bindParam(':id' ,$id);
$stmt -> execute();
if(!empty($deletefiles)){
$sql_str = "DELETE FROM contract_back_files WHERE id IN ($deletefiles)";
$stmt = $conn -> prepare($sql_str);
$stmt -> execute();
}
if(!empty($files)){
$englisharr = range('a', 'z');
$files = $_FILES['files'];
$newfiles = [];
foreach( $files as $file ){
$i = 0; //新陣列的索引編號
foreach( $file as $key => $val ){
$newfiles[$i]['name'] = $files['name'][$key];
$newfiles[$i]['type'] = $files['type'][$key];
$newfiles[$i]['tmp_name'] = $files['tmp_name'][$key];
$newfiles[$i]['error'] = $files['error'][$key];
$newfiles[$i]['size'] = $files['size'][$key];
$i++;
} //foreach 第2層 end
}
$max_size = 4096*4096; //設定允許上傳檔案容量的最大值(1M)
$allow_ext = array('jpeg', 'jpg', 'png','JPG','JPEG','PNG','GIF'); //設定允許上傳檔案的類型
$path = '../images/contracts/';
if (!file_exists($path)) { mkdir($path); }
$msg_result = ''; //負責接收所有檔案檢測後的回傳訊息
$datetime = (string)date('YmdHis');
$files_id = ($files_id !== null ) ? $files_id : 'm' . $datetime; // 新梯=>m + 日期時間
foreach( $newfiles as $key => $file ){
$randNum = rand(1000,9999);
$randEnglish = $englisharr[rand(0,25)];
$file_name = 'm' . (string)date('YmdHis') . $randNum . $randEnglish . $randNum.$file['name'];
$msg = upload_chk( $file,$path, $max_size, $allow_ext, $file_name );
if($msg==1){
$msg = '檔案傳送成功!';
$sql_str = "INSERT INTO contract_back_files (files_id, file_name, file_mime, file_size, created_at, created_by) VALUES (:files_id, :file_name, :file_mime, :file_size, :created_at, :created_by)";
$stmt = $conn -> prepare($sql_str);
$stmt -> bindParam(':files_id' ,$files_id);
$stmt -> bindParam(':file_name' ,$file_name);
$stmt -> bindParam(':file_mime' ,$file['type']);
$stmt -> bindParam(':file_size' ,$file['size']);
$stmt -> bindParam(':created_at' ,$created_at);
$stmt -> bindParam(':created_by' ,$created_by);
$stmt ->execute();
}else{
throw new PDOException('檔案上傳失敗:' . $msg);
}
$msg_result .= '第' . ($key+1) . '個上傳檔案的結果:' . $msg . '<br/>';
$src_name = $path.$file['name'];
if( file_exists($src_name) ){
//副檔名
$extname = pathinfo($src_name, PATHINFO_EXTENSION);
//主檔名
$basename = basename($src_name, '.'.$extname);
}
}
}else{
$files = null;
}
$conn->commit();
}catch(PDOException $e) {
$conn->rollback();
header("HTTP/1.1 500 Internal Server Error");
die('Error!:'.$e->getMessage());
}
}