0) { header("HTTP/1.1 422 Unprocessable Entity"); echo json_encode($fail_arr); exit(); } //create account table $accounttype = "A"; $accountid = $vat; $pwd = "123"; $name = $partyA; $tel = $phone ?? ''; $repairerid = $mworker; $creater = $user_id; $create_at = date('Y-m-d H:i:s'); $conn->beginTransaction(); $sql_str = "INSERT INTO account (accounttype, accountid, pwd, name, tel, address, email, repairerid, creater, create_at) VALUES (:accounttype, :accountid, :pwd, :name, :tel, :address, :email, :repairerid, :creater, :create_at)"; $stmt = $conn->prepare($sql_str); $stmt->bindParam(':accounttype', $accounttype); $stmt->bindParam(':accountid', $accountid); $stmt->bindParam(':pwd', $pwd); $stmt->bindParam(':name', $name); $stmt->bindParam(':tel', $tel); $stmt->bindParam(':address', $address); $stmt->bindParam(':email', $email); $stmt->bindParam(':repairerid', $repairerid); $stmt->bindParam(':creater', $creater); $stmt->bindParam(':create_at', $create_at); $stmt->execute(); //create contract table $contracttype = $mtype; $company = $partyA; $taxid = $vat; $tel = $phone; $promiser = $partyA; $contractperson = $partyA; $contractaddress = $address; $contracttel = $phone; $contractemail = $email; $contract_employee = $salesman; $start_date = $contract_begin_date; $end_date = $contract_end_date; $sql_str = "INSERT INTO contract (contracttype, contractno, company, taxid, address, tel, promiser, contractperson, contractaddress, contracttel, contractemail, contract_employee, start_date, end_date, creater, create_at) VALUES (:contracttype, :contractno, :company, :taxid, :address, :tel, :promiser, :contractperson, :contractaddress, :contracttel, :contractemail, :contract_employee, :start_date, :end_date, :creater, :create_at)"; $stmt = $conn->prepare($sql_str); $stmt->bindParam(':contracttype', $contracttype); $stmt->bindParam(':contractno', $contractno); $stmt->bindParam(':company', $company); $stmt->bindParam(':taxid', $taxid); $stmt->bindParam(':address', $address); $stmt->bindParam(':tel', $tel); $stmt->bindParam(':promiser', $promiser); $stmt->bindParam(':contractperson', $contractperson); $stmt->bindParam(':contractaddress', $contractaddress); $stmt->bindParam(':contracttel', $contracttel); $stmt->bindParam(':contractemail', $contractemail); $stmt->bindParam(':contract_employee', $contract_employee); $stmt->bindParam(':start_date', $start_date); $stmt->bindParam(':end_date', $end_date); $stmt->bindParam(':creater', $creater); $stmt->bindParam(':create_at', $create_at); $stmt->execute(); //create facility table $createFacilityNo = new CreateFacilityNo(); $dailyNecessities = [ 'MAE100' => 'X', 'MAM200' => 'W', 'MAH100' => 'H', 'MAQ100' => 'Z', 'MAF100' => 'F', 'MAZ100' => 'B', ]; $facility_arr = []; foreach ($elevators as $elevator) { $facility_arr[] = $dailyNecessities[$elevator['spec']]; } echo json_encode($facility_arr); // 電梯做番號 $facilityno = $createFacilityNo->makeBFacilityNo("T", $facility_arr, (int)$num); // if ($user_id == 'M0225') { // echo '
';
        //     print_r($facilityno);
        //     echo '
'; // exit(); // } echo json_encode($facilityno); echo '-------'; $sql_str = "SELECT accountid, name FROM account WHERE accountid = :accountid"; $stmt = $conn->prepare($sql_str); $stmt->bindParam(':accountid', $mworker); $stmt->execute(); $worker = $stmt->fetch(PDO::FETCH_ASSOC); $customerid = $vat; $define = "B"; $repairtype = $mtype; $repairerid = $mworker; $repairername = $worker['name']; foreach ($elevators as $idx => $elevator) { $sql_str = "INSERT INTO facility (contractno, define, facilityno, latitude, longitude, customerid, weight, numberofpassenger, numberofstop, numberoffloor, opentype, speed, repairtype, maintainance, facility_kind, address, repairerid, repairername, creater, create_at, area, takecertificatedate, licensedate) VALUES (:contractno, :define, :facilityno, :latitude, :longitude, :customerid, :weight, :numberofpassenger, :numberofstop, :numberoffloor, :opentype, :speed, :repairtype, :maintainance, :facility_kind, :address, :repairerid, :repairername, :creater, :create_at, :area, :takecertificatedate, :licensedate)"; $stmt = $conn->prepare($sql_str); $stmt->bindParam(':contractno', $contractno); $stmt->bindParam(':define', $define); $stmt->bindParam(':facilityno', $facilityno[$idx]); $stmt->bindParam(':latitude', $elevator['latitude']); $stmt->bindParam(':longitude', $elevator['longitude']); $stmt->bindParam(':customerid', $customerid); $stmt->bindParam(':weight', $elevator['weight']); $stmt->bindParam(':numberofpassenger', $elevator['persons']); $stmt->bindParam(':numberofstop', $elevator['stop']); $stmt->bindParam(':numberoffloor', $elevator['floors']); $stmt->bindParam(':opentype', $elevator['opendoor']); $stmt->bindParam(':speed', $elevator['speed']); $stmt->bindParam(':repairtype', $repairtype); $stmt->bindParam(':maintainance', $elevator['maintainance']); $stmt->bindParam(':facility_kind', $elevator['spec']); $stmt->bindParam(':address', $address); $stmt->bindParam(':repairerid', $repairerid); $stmt->bindParam(':repairername', $repairername); $stmt->bindParam(':creater', $creater); $stmt->bindParam(':create_at', $create_at); $stmt->bindParam(':area', $area); $stmt->bindParam(':takecertificatedate', $elevator['takecertificatedate']); $stmt->bindParam(':licensedate', $elevator['useful_date']); $result = $stmt->execute(); } //create schedule table $comboNo = new CreateComboNo($mcycle, $contract_begin_date, $contract_end_date); $comboArr = json_decode($comboNo->getComboNo(), true); foreach ($facilityno as $no) { foreach ($comboArr as $combo) { $sql_str = 'INSERT INTO schedule (contractno, facilityno, combono, repairerid, repairername, duedate, creater, create_at) VALUES (:contractno, :facilityno, :combono, :repairerid, :repairername, :duedate, :creater, :create_at)'; $stmt = $conn->prepare($sql_str); $stmt->bindParam(':contractno', $contractno); $stmt->bindParam(':facilityno', $no); $stmt->bindParam(':combono', $combo[0]); $stmt->bindParam(':repairerid', $repairerid); $stmt->bindParam(':repairername', $repairername); $stmt->bindParam(':duedate', $combo[1]); $stmt->bindParam(':creater', $creater); $stmt->bindParam(':create_at', $create_at); $result = $stmt->execute(); } } // create contract_b_signed_back table $contract_type = $mtype; $company = $customer; $customer_no = $vat; $salesperson = $salesman; $customer_phone = $phone; $customer_email = $email; $repairman = $mworker; $cycle = $mcycle; $contact_person = $partyA; $contact_address = $partyAaddress; $contact_phone = $partyAphone; $contract_email = $partyAemail; $elevators_number = $num; $bonus = 1000; $max_bonus = 2000; if (!empty($files)) { $englisharr = range('a', 'z'); $file = $_FILES['files']; $file_name = $file['name']; $file_type = $file['type']; $tmp_name = $file['tmp_name']; $file_size = $file['size']; $error = $file['error']; $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 = 'b' . $datetime; // 保養=>b + 日期時間 foreach ($newfiles as $key => $file) { $randNum = rand(1000, 9999); $randEnglish = $englisharr[rand(0, 25)]; $file_name = 'b' . (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', $user_id); $stmt->execute(); } $msg_result .= '第' . ($key + 1) . '個上傳檔案的結果:' . $msg . '
'; $src_name = $path . $file['name']; if (file_exists($src_name)) { //副檔名 $extname = pathinfo($src_name, PATHINFO_EXTENSION); //主檔名 $basename = basename($src_name, '.' . $extname); } } } else { $files = null; } $sql_str = "INSERT INTO contract_b_signed_back (contract_no, contract_type, company, customer_no, salesperson, contract_start_date, contract_end_date, total_price,payType, customer_phone, customer_email, repairman, cycle, contact_person, contact_address, contact_phone, contact_email, elevators_number, area, address, files_id, bonus, max_bonus, created_at, created_by) VALUES (:contract_no, :contract_type, :company, :customer_no, :salesperson, :contract_start_date, :contract_end_date, :total_price, :payType ,:customer_phone, :customer_email, :repairman, :cycle, :contact_person, :contact_address, :contact_phone, :contact_email, :elevators_number, :area, :address, :files_id, :bonus, :max_bonus, :created_at, :created_by)"; $stmt = $conn->prepare($sql_str); $stmt->bindParam(":contract_no", $contractno); $stmt->bindParam(":contract_type", $contract_type); $stmt->bindParam(":company", $company); $stmt->bindParam(":customer_no", $customer_no); $stmt->bindParam(":salesperson", $salesperson); $stmt->bindParam(":contract_start_date", $contract_begin_date); $stmt->bindParam(":contract_end_date", $contract_end_date); $stmt->bindParam(":total_price", $total_price); $stmt->bindParam(":customer_phone", $customer_phone); $stmt->bindParam(":customer_email", $customer_email); $stmt->bindParam(":repairman", $repairman); $stmt->bindParam(":cycle", $cycle); $stmt->bindParam(":contact_person", $contact_person); $stmt->bindParam(":contact_address", $contact_address); $stmt->bindParam(":contact_phone", $contact_phone); $stmt->bindParam(":contact_email", $contact_email); $stmt->bindParam(":elevators_number", $elevators_number); $stmt->bindParam(":area", $area); $stmt->bindParam(":address", $address); $stmt->bindParam(":files_id", $files_id); $stmt->bindParam(":bonus", $bonus); $stmt->bindParam(":max_bonus", $max_bonus); $stmt->bindParam(":created_at", $created_at); $stmt->bindParam(":created_by", $user_id); $stmt->bindParam(':payType', $payType); $stmt->execute(); // $date = date('Y-m-sH-s-i'); // $createTime = str_replace("-", '', $date); // $sql = "INSERT INTO comCustomer(BizPartnerId,PersonId,CreatorId,IsInUsed,InvoiceAddress,CreateTime)VALUES(:BizPartnerId,:PersonId,:CreatorId,1,:InvoiceAddress,:CreateTime)"; // $stmt = $connT8->prepare($sql); // $stmt->bindParam(':BizPartnerId', $contractno); // $stmt->bindParam(':PersonId', $salesman); // $stmt->bindParam(':CreatorId', $user_id); // $stmt->bindParam(':InvoiceAddress', $address); // $stmt->bindParam(':CreateTime', $createTime); // $stmt->execute(); T8insert($_POST, $facilityno); header('Content-Type: application/json'); $jsonData = json_encode($files); $conn->commit(); } catch (PDOException $e) { $conn->rollback(); http_response_code(404); echo $e->getMessage(); die('Error!:' . $e->getMessage()); } }; function T8insert($data, $facilityno) { require_once("../connt8.php"); // 引入 T8 API 連線 require_once("./T8salIncomeApply.php"); $contractno = !empty($data['contractno']) ? $data['contractno'] : null; // 合約號 $address = !empty($data['address']) ? $data['address'] : null; // 客戶地址 $customer = !empty($data['customer']) ? $data['customer'] : null; //企業名稱 $phone = !empty($data['phone']) ? $data['phone'] : null; //客戶電話 $vat = !empty($data['vat']) ? $data['vat'] : null; //統編 $email = !empty($data['email']) ? $data['email'] : null; $salesman = !empty($data['salesman']) ? $data['salesman'] : null; // 銷售人員 $partyA = !empty($data['partyA']) ? $data['partyA'] : null; //客戶名稱 / 業務聯絡人 $partyAaddress = !empty($data['partyAaddress']) ? $data['partyAaddress'] : null; // 業務聯絡人地址 $contract_begin_date = !empty($data['contract_begin_date']) ? $data['contract_begin_date'] : null; $contract_end_date = !empty($data['contract_end_date']) ? $data['contract_end_date'] : null; $num = !empty($data['num']) ? $data['num'] : null; // 電梯數量 $elevators = !empty($data['elevators']) ? json_decode($data['elevators'], true) : []; //電梯 $area = !empty($data['area']) ? $data['area'] : null; //縣市 Ex. A->台北 , B-> ..。 $payType = !empty($data['payType']) ? $data['payType'] : null; $user_id = !empty($_POST['user_id']) ? $_POST['user_id'] : null; $connT8->beginTransaction(); $sql = "SELECT * FROM comCustomer WHERE BizPartnerId = :BizPartnerId"; $stmt = $conn->prepare($sql); $stmt->bindParam(':BizPartnerId', $contractno); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); $date = date('Y-m-sH-s-i'); $date_1 = date('Y-m-s'); $createTime = str_replace("-", '', $date); $insertTime = str_replace('-', '', $date_1); $beginDate = str_replace("-", '', $contract_begin_date); $endDate = str_replace("-", '', $contract_end_date); if ($contractno == '') { echo 'Error'; exit; } if (empty($result)) { // 若 客戶資料為空,新增一筆到 comCustomer // 新增客戶資料 $sql = "INSERT INTO comBusinessPartner (BizPartnerId,BizPartnerName,BusinessAttr,CountryId,WorkTelNo,BizToDate,TaxNo,EnterpriseName,ContactAddress,EMail,CreatorId,CreateTime,BizPartnerTypeId) VALUES(:BizPartnerId,:BizPartnerName,1,'TW',:WorkTelNo,99999999,:TaxNo,:EnterpriseName,:ContactAddress,:EMail,:CreatorId,:CreateTime,'10')"; $stmt = $connT8->prepare($sql); $stmt->bindParam(':BizPartnerId', $contractno); $stmt->bindParam(':BizPartnerName', $partyA); $stmt->bindParam(':WorkTelNo', $phone); $stmt->bindParam(':TaxNo', $vat); $stmt->bindParam(':EnterpriseName', $customer); //企業名稱 $stmt->bindParam(':ContactAddress', $partyAaddress); $stmt->bindParam(':EMail', $email); $stmt->bindParam(':CreatorId', $user_id); $stmt->bindParam(':CreateTime', $createTime); $stmt->execute(); $sql = "INSERT INTO comCustomer (OrgId,BizPartnerTypeId,CurrId,BizPartnerId,PersonId,CreatorId,IsInUsed,InvoiceAddress,CreateTime) VALUES('1000','10','TWD',:BizPartnerId,:PersonId,:CreatorId,1,:InvoiceAddress,:CreateTime)"; $stmt = $connT8->prepare($sql); $stmt->bindParam(':BizPartnerId', $contractno); $stmt->bindParam(':PersonId', $salesman); $stmt->bindParam(':CreatorId', $user_id); $stmt->bindParam(':InvoiceAddress', $address); $stmt->bindParam(':CreateTime', $createTime); $stmt->execute(); } else { // // 若客戶資料不為空,更新該客戶資訊。 $sql = "UPDATE comCustomer SET PersonId=:PersonId, InvoiceAddress=:InvoiceAddress, LastOperatorId=:LastOperatorId, LastOperateTime=:LastOperateTime WHERE BizPartnerId=:BizPartnerId "; $stmt = $connT8->prepare($sql); $stmt->bindParam(':PersonId', $salesman); $stmt->bindParam(':InvoiceAddress', $address); $stmt->bindParam(':LastOperatorId', $user_id); $stmt->bindParam(':LastOperateTime', $createTime); $stmt->bindParam(':BizPartnerId', $contractno); $stmt->execute(); $sql = "UPDATE comBusinessPartner SET BizPartnerName=:BizPartnerName, WorkTelNo=:WorkTelNo, TaxNo=:TaxNo, EnterpriseName=:EnterpriseName, ContactAddress=:ContactAddress, EMail=:EMail, LastOperatorId=:LastOperatorId, LastOperateTime=:LastOperateTime WHERE BizPartnerId = :BizPartnerId "; $stmt = $connT8->prepare($sql); $stmt->bindParam(':BizPartnerName', $partyA); $stmt->bindParam(':WorkTelNo', $phone); $stmt->bindParam(':TaxNo', $vat); $stmt->bindParam(':EnterpriseName', $customer); $stmt->bindParam(':ContactAddress', $partyAaddress); $stmt->bindParam(':EMail', $email); $stmt->bindParam(':LastOperatorId', $user_id); $stmt->bindParam(':LastOperateTime', $createTime); $stmt->bindParam(':BizPartnerId', $contractno); $stmt->execute(); } // echo '
';
    // print_r($elevators['maintainance']);
    // echo '
'; foreach ($elevators as $index => $elevator) { $type[] = $elevator['maintainance']; if ($type[$index] == 'A') { $type = 'C3'; } else if ($type[$index] == 'B') { $type = 'C4'; } else if ($type[$index] == 'C') { $type = 'C5'; } } $sql = "SELECT * FROM comProject WHERE ProjectId = :ProjectId"; $stmt = $connT8->prepare($sql); $stmt->bindParam(':ProjectId', $contractno); $stmt->execute(); $resultProject = $stmt->fetchAll(PDO::FETCH_ASSOC); if (empty($resultProject)) { //新增於 comProject。合約 table $sql = "INSERT INTO comProject(ProjectId,ProjectName,TypeId,ValidityFromDate,ValidityToDate,CreateTime,CreatorId,IsInUsed) VALUES(:ProjectId,:ProjectName,:TypeId,:ValidityFromDate,:ValidityToDate,:CreateTime,:CreatorId,1)"; $stmt = $connT8->prepare($sql); $stmt->bindParam(':ProjectId', $contractno); $stmt->bindParam(':ProjectName', $customer); $stmt->bindParam(':TypeId', $type); $stmt->bindParam(':ValidityFromDate', $beginDate); $stmt->bindParam(':ValidityToDate', $endDate); $stmt->bindParam(':CreateTime', $createTime); $stmt->bindParam(':CreatorId', $user_id); $stmt->execute(); } // // 新增電梯數 foreach ($elevators as $index => $elevator) { // echo '
';
        // print_r($elevator);
        // echo '
'; $sql = "INSERT INTO comMaterial (FOrgid,MaterialId,MaterialTypeId,MaterialCategoryId,CreatorId,CreateTime) VALUES ('1000',:MaterialId,'10','E',:CreatorId,:CreateTime)"; $stmt = $connT8->prepare($sql); $stmt->bindParam(':MaterialId', $facilityno[$index]); $stmt->bindParam(':CreatorId', $user_id); $stmt->bindParam(':CreateTime', $createTime); $stmt->execute(); $sql = "INSERT INTO comMaterialGroup (MaterialTypeId,MaterialId,MaterialName,MaterialCategoryId,IsInUsed,UnitId,CreatorId,CreateTime) VALUES ('10',:MaterialId,:MaterialName,'E',1,'SET',:CreatorId,:CreateTime)"; $stmt = $connT8->prepare($sql); $stmt->bindParam(':MaterialId', $facilityno[$index]); $stmt->bindParam(':MaterialName', $customer); $stmt->bindParam(':CreatorId', $user_id); $stmt->bindParam(':CreateTime', $createTime); $stmt->execute(); $sql = "INSERT INTO comMaterialPurchases (Orgid,MaterialTypeId,MaterialId,CurrId,SUnitId,TaxId,CreatorId,CreateTime) VALUES ('1000','10',:MaterialId,'TWD','SET','ST005',:CreatorId,:CreateTime)"; $stmt = $connT8->prepare($sql); $stmt->bindParam(':MaterialId', $facilityno[$index]); $stmt->bindParam(':CreatorId', $user_id); $stmt->bindParam(':CreateTime', $createTime); $stmt->execute(); // 分期代碼計算 $IncomeId = [ 'A40006' => 1, //年繳 'A40007' => 4, //季繳 'A40003' => 12, //月繳 'A40004' => 6, //雙月繳 'A40005' => 2 //半年繳 ]; //分幾期 => 總共保養月 / 分期月 $SQuantity = $elevator['maintain_months'] / $IncomeId[$payType]; // 依每台電梯去加入各個的付款項 for ($i = 0; $i < $IncomeId[$payType]; $i++) { // 計算分期款的收款月份 $paymonth = $i * $SQuantity; $payday = date('Y-m-d', strtotime("$date_1 + $paymonth months")); $rows_data = [ "BillNo" => "$contractno", "IncomeId" => "$payType", "TaxId" => "ST005", "RowCode" => $i + 1, "RowNo" => $i + 1, "ItemType" => 1, "SPrice" => $elevator['sold_price'], "SQuantity" => $SQuantity, "ProjectId" => "$contractno", "FromSourceTag" => 0, "CU_MaterialId" => "$facilityno[$index]", "CU_EstPayDate" => "$payday" ]; $salIncomeApplyDetail_rows[] = $rows_data; } } $connT8->commit(); // $insertime = settype($insertTime, 'integer'); $salIncomeApplyMaster_rows = [ "BillNo" => "$contractno", "BillDate" => $insertTime, "OrgId" => "1000", "FOrgId" => "1000", "TypeId" => "RAS", "BpOrgId" => "1000", "BizPartnerId" => "$contractno", "CurrId" => "TWD", "CurrOAmount" => 1, "CurrLAmount" => 1, "PersonId" => "$salesman", "DtOrgId" => "1000", "DueToId" => "$contractno", "TaxId" => "ST005", "CompId" => "1001", "CreatorId" => "$user_id" ]; $salIncomeApplyMaster = [ 'name' => "salIncomeApplyMaster", 'rows' => [$salIncomeApplyMaster_rows] ]; $salIncomeApplyDetail = [ 'name' => "salIncomeApplyDetail", 'rows' => $salIncomeApplyDetail_rows ]; $API_body[] = $salIncomeApplyMaster; $API_body[] = $salIncomeApplyDetail; // 呼叫 API $result = T8API($API_body); if ($result['Status'] == 'Error') { http_response_code(404); die('Error!:' . $result['ErrorMsg']); } }