0 ORDER BY InvoiceTime ASC"; // T8核銷 $sql_received = "SELECT a.BillNo, a.WriteOffBizPartnerId,a.PayWriteOffOAmount, b.FromBillDate AS ReceivedDate,b.CurrWOFeeOAmt AS Fee, c.OrderBillNo, c.checkBillNo, c.InvoiceNo, c.BillDate AS CheckBillDate FROM arWriteOffBill AS a LEFT JOIN arWriteOffBillRec AS b ON a.BillNo=b.BillNo LEFT JOIN (SELECT temp1.*,arWriteOffBillDetail.* FROM arWriteOffBillDetail LEFT JOIN (SELECT arCheckBill.BillNo AS checkBillNo, arCheckBill.BillDate,arCheckBillInvInfo.InvoiceNo FROM arCheckBill LEFT JOIN arCheckBillInvInfo ON arCheckBill.InvoiceBillNo=arCheckBillInvInfo.InvoiceBillNo) AS temp1 ON temp1.checkBillNo = arWriteOffBillDetail.FromBillNo) AS c ON a.BillNo=c.BillNo WHERE DATALENGTH(c.OrderBillNo) >0"; //ORDER BY contractno // 期初收款資訊 $sql_opening = " SELECT * FROM account_received "; // 設定權限:看到自己的/下屬的,資訊處跟財會處可以看全部的 if ((in_array($user_id, array('M0060', 'M0175'))) || (in_array(accountidToDepartId($user_id), array('220', '210')))) { } else { $sql_contract .= "AND (s.PersonId = '$user_id'"; // $sql_received .= " AND (PersonId = '$user_id'"; $sql_opening .= " OR person_id = '$user_id'"; $sql_contractNumber .= " AND (PersonId = '$user_id'"; $sql_contract_budget .= " AND (PersonId = '$user_id'"; if (count($follower) > 0) { $column_str = "('$user_id'" . ",'"; $column_str .= implode("','", $follower); $column_str .= "')"; $sql_contractNumber .= " OR PersonId IN $column_str)"; // $sql_contract .= " OR s.PersonId IN $column_str)"; // $sql_received .= " OR PersonId IN $column_str)"; $sql_opening .= " OR person_id IN $column_str)"; // $sql_contract_budget .= " OR PersonId IN $column_str )ORDER BY BillNo, CU_MaterialId"; } else { $sql_contract .= ")"; // $sql_received .= ")"; $sql_opening .= ")"; $sql_contractNumber .= ")"; // $sql_contract_budget .= ")"; }; } $contractNumbers = array(); $query_contract_number = $conn->query($sql_contractNumber); foreach ($query_contract_number as $contract_numbers) { array_push($contractNumbers, $contract_numbers['BillNo']); $str_numbers = "('" . implode("','", $contractNumbers) . "')"; } //作番大日程 $sql = "SELECT tmp2.*, d.name AS depart_name FROM( SELECT a.manager, a2.name as manager_name, a.name, a.department_id, w.salesid, w.contractno, w.facilityno, w.estimated_shipping_date, w.real_contract_arrival_date, w.actual_tofactory_date, w.real_arrival_date, w.install_end_date, w.tryrun_end_date, w.official_check_date, w.delivery_date from wipwholestatus AS w left join account AS a ON w.salesid = a.accountid left join account AS a2 ON a2.accountid = a.manager where w.status = '1' AND w.contract_type='B' )AS tmp2 left join ( SELECT DISTINCT department_id, name FROM department ) AS d ON d.department_id = tmp2.department_id"; //T8 銷售訂單 階段收款計畫 $sql_contract = "SELECT a.BillNo,s.BillDate, a.RowNo, a.PayStage, a.PlanPayAmt, a.PlanPayDate,s.BizPartnerId,c.BizPartnerName, s.PersonId, c.EnterpriseName, c.TaxNo, c.ContactAddress FROM salOrderStagePay AS a LEFT JOIN salSalesOrder AS s on a.BillNo = s.BillNo LEFT JOIN comBusinessPartner AS c ON s.BizPartnerId=c.BizPartnerId WHERE s.BillNo IN $str_numbers "; // T8 銷售訂單 作番金額 $sql_contract_budget = "SELECT A.BillNo,A.MaterialId, A.OAmountWithTax,A.CU_MaterialId FROM salSalesOrderDetail AS A LEFT JOIN salSalesOrder AS B ON A.BillNo=B.BillNo WHERE B.ModeId='T' AND A.ItemType=1 AND A.BillNo IN $str_numbers "; $contract = $conn->query($sql_contract); $contract_data = array(); // 將T8合約收款階段資料依據款別分類放進arraData裡 foreach ($contract as $cont) { $BillNo = $cont['BillNo']; $BillDate = $cont['BillDate']; $PayStage = $cont['PayStage']; $PlanPayDate = $cont['PlanPayDate']; $PayAmount = $cont['PlanPayAmt']; $partnerName = $cont['BizPartnerName']; $EnterpriseName = $cont['EnterpriseName']; $TaxNo = $cont['TaxNo']; $ContactAddress = $cont['ContactAddress']; $RowNo = $cont['RowNo']; //['type'] A:比照新梯、B:純分期、C:其他 //['sign'] 0 款項名稱 1 簽約金額 2 簽約日期 3 應收金額 min 最早應收月份 max 最晚應收月份 ['second'] 0 二次款名 1 二次款金額 2 收款日期 ['arrive'] 0 貨抵工地款名稱 1 貨抵工地款金額 2 貨抵工地收款日期 ['install'] 0 安裝款名 1 安裝金額 2 安裝收款日期 //['tryrun'] 0 試車款名 1試車金額 2 試車收款日期 ['check'] 0 官檢款名 1 官檢金額 2 官檢收款日期 ['delivery'] 0 交車款名 1 交車金額 2 交車收款日期 ['final'] 0 尾款名 1 尾款金額 2 尾款收款日期 //['facility'] [作番號][款別]0 款項名稱 1 合約金額 2 應收日期 3 應收金額 4 已收金額 5 催收金額 6 催收次數 if (!(isset($arrayData[$BillNo]))) { // 0 部門id 1 部門名稱 2 經理名稱 3 營業員id 4 營業員名稱 5 客戶名稱 6 抬頭 7 統編 8 通訊地址 9 T8單據日期 10 合約號 $arrayData[$BillNo] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; $arrayData[$BillNo][5] = $partnerName; $arrayData[$BillNo][6] = $EnterpriseName; $arrayData[$BillNo][7] = $TaxNo; $arrayData[$BillNo][8] = $ContactAddress; $arrayData[$BillNo][9] = $BillDate; $arrayData[$BillNo][10] = $BillNo; $arrayData[$BillNo]['total_budget'] = 0; $arrayData[$BillNo]['invoice'] = array(); $arrayData[$BillNo]['received_budget'] = 0; $arrayData[$BillNo]['invoice_budget'] = 0; $arrayData[$BillNo]['A40002'] = $arrayData[$BillNo]['A40009'] = $arrayData[$BillNo]['A40010'] = 0; $arrayData[$BillNo]['total_facility_num'] = 0; } $arrayData[$BillNo]['PayStage'][$RowNo]['type'] = 0; $arrayData[$BillNo]['PayStage'][$RowNo]['name'] = $PayStage; $arrayData[$BillNo]['PayStage'][$RowNo]['PlanPayDate'] = $PlanPayDate; $arrayData[$BillNo]['PayStage'][$RowNo]['PayAmount'] = $PayAmount; // 判斷type $wipStage = ['簽約', '訂金', '貨抵工地', '完工', '試車', '安裝', '驗收', '完工', '交車', '尾款', '合約']; foreach ($wipStage as $wipstage) { if (strpos($PayStage, $wipstage) !== false) { $arrayData[$BillNo]['PayStage'][$RowNo]['type'] = "A"; } } $haveNumber = (int) filter_var(str_replace('-', '', $PayStage), FILTER_SANITIZE_NUMBER_INT); if ($haveNumber > 0 && $arrayData[$BillNo]['PayStage'][$RowNo]['type'] === 0) { $arrayData[$BillNo]['PayStage'][$RowNo]['type'] = "B"; } elseif ($haveNumber > 0 && $arrayData[$BillNo]['PayStage'][$RowNo]['type'] == 'A') { $arrayData[$BillNo]['PayStage'][$RowNo]['type'] = "C"; } } $received_array = $conn->query($sql_received); $opening_data = mysqli_query($link, $sql_opening); $invoice_data = $conn->query($sql_invoice); $contract_budget_data = $conn->query($sql_contract_budget); // 將T8銷售訂單明細 每個作番金額存入arrayData $contract_budget = array(); foreach ($contract_budget_data as $cont) { $BillNo = $cont['BillNo']; if (isset($arrayData[$BillNo])) { $arrayData[$BillNo]['total_budget'] += $cont['OAmountWithTax']; $arrayData[$BillNo][$cont['MaterialId']] += $cont['OAmountWithTax']; if (isset($arrayData[$BillNo]['facility'][$cont['CU_MaterialId']])) { $arrayData[$BillNo]['facility'][$cont['CU_MaterialId']]['total_budget'] += $cont['OAmountWithTax']; $arrayData[$BillNo]['facility'][$cont['CU_MaterialId']][$cont['MaterialId']] += $cont['OAmountWithTax']; } else { $arrayData[$BillNo]['total_facility_num'] += 1; $arrayData[$BillNo]['facility'][$cont['CU_MaterialId']]['total_budget'] = $cont['OAmountWithTax']; $arrayData[$BillNo]['facility'][$cont['CU_MaterialId']]['A40002'] = $arrayData[$BillNo]['facility'][$cont['CU_MaterialId']]['A40009'] = $arrayData[$BillNo]['facility'][$cont['CU_MaterialId']]['A40010'] = 0; $arrayData[$BillNo]['facility'][$cont['CU_MaterialId']][$cont['MaterialId']] = $cont['OAmountWithTax']; } } if (isset($contract_budget[$cont['BillNo']])) { $contract_budget[$cont['BillNo']]['total'] += $cont['OAmountWithTax']; } else { $contract_budget[$cont['BillNo']]['total'] = $cont['OAmountWithTax']; } if (isset($contract_budget[$cont['BillNo']][$cont['MaterialId']])) { $contract_budget[$cont['BillNo']][$cont['MaterialId']] += $cont['OAmountWithTax']; } else { $contract_budget[$cont['BillNo']][$cont['MaterialId']] = $cont['OAmountWithTax']; } if (isset($contract_budget[$cont['BillNo']]['facility'][$cont['CU_MaterialId']])) { $contract_budget[$cont['BillNo']]['facility'][$cont['CU_MaterialId']]['total'] += $cont['OAmountWithTax']; $contract_budget[$cont['BillNo']]['facility'][$cont['CU_MaterialId']][$cont['MaterialId']] += $cont['OAmountWithTax']; } else { $contract_budget[$cont['BillNo']]['facility'][$cont['CU_MaterialId']]['total'] = $cont['OAmountWithTax']; $contract_budget[$cont['BillNo']]['facility'][$cont['CU_MaterialId']]['A40002'] = 0; $contract_budget[$cont['BillNo']]['facility'][$cont['CU_MaterialId']]['A40009'] = 0; $contract_budget[$cont['BillNo']]['facility'][$cont['CU_MaterialId']]['A40010'] = 0; $contract_budget[$cont['BillNo']]['facility'][$cont['CU_MaterialId']][$cont['MaterialId']] = $cont['OAmountWithTax']; }; } /*計算作番佔合約的金額比例 @param $facility_no string => 作番號 @param $contract_budget_billno array => $contract_budget[合約號] return float */ function get_contract_ratio($facility_no, $contract_budget_billno) { $ratio = 0; if (isset($contract_budget_billno['facility'][$facility_no])) { if (empty($contract_budget_billno['total'])) { } else { $ratio += $contract_budget_billno['facility'][$facility_no]['total'] / $contract_budget_billno['total']; } } return $ratio; } /* 比較應收日期 @param $a array @param $b array return int */ function comparePlanPayDate($a, $b) { return strtotime($a['PlanPayDate']) - strtotime($b['PlanPayDate']); } //取作番大日程作番與時程 進arrayData // real_contract_arrival_date 預計出貨日 // real_arrival_date 實際出貨日 $today = strtotime(date('Ymd')); $wipwhole_array = mysqli_query($link, $sql); $wipwhole_status = array(); foreach ($wipwhole_array as $wip) { //[合約號]['facility'] [作番號][款別]0 款項名稱 1 合約金額 2 應收日期 3 應收金額 4 已收金額 5 催收金額 6 催收次數 if (isset($arrayData[$wip['contractno']])) { // 整理合約資料 $arrayData[$wip['contractno']][0] = $wip['department_id'];; $arrayData[$wip['contractno']][1] = $wip['depart_name']; $arrayData[$wip['contractno']][2] = $wip['manager_name']; $arrayData[$wip['contractno']][3] = $wip['salesid']; $arrayData[$wip['contractno']][4] = $wip['name']; $arrayData[$wip['contractno']][10] = $wip['contractno']; $arrayData[$wip['contractno']]['A40001'] = (isset($contract_budget[$wip['contractno']]['A40001']) && !is_null($contract_budget[$wip['contractno']]['A40001'])) ? $contract_budget[$wip['contractno']]['A40001'] : 0; $arrayData[$wip['contractno']]['A40008'] = (isset($contract_budget[$wip['contractno']]['A40008']) && !is_null($contract_budget[$wip['contractno']]['A40008'])) ? $contract_budget[$wip['contractno']]['A40008'] : 0; // [合約號]['facility'][作番號]['no'] $arrayData[$wip['contractno']]['facility'][$wip['facilityno']]['no'] = $wip['facilityno']; $arrayData[$wip['contractno']]['facility'][$wip['facilityno']]['status'] = ""; $arrayData[$wip['contractno']]['facility'][$wip['facilityno']]['total_budget'] = 0; $arrayData[$wip['contractno']]['facility'][$wip['facilityno']]['receivable_budget'] = 0; $arrayData[$wip['contractno']]['facility'][$wip['facilityno']]['collect_budget'] = 0; $arrayData[$wip['contractno']]['facility'][$wip['facilityno']]['A40002'] = (isset($contract_budget[$wip['contractno']][$wip['facilityno']]['A40002']) && !is_null($contract_budget[$wip['contractno']][$wip['facilityno']]['A40002'])) ? $contract_budget[$wip['contractno']][$wip['facilityno']]['A40002'] : 0; $arrayData[$wip['contractno']]['facility'][$wip['facilityno']]['A40008'] = (isset($contract_budget[$wip['contractno']][$wip['facilityno']]['A40008']) && !is_null($contract_budget[$wip['contractno']][$wip['facilityno']]['A40008'])) ? $contract_budget[$wip['contractno']][$wip['facilityno']]['A40008'] : 0; $arrayData[$wip['contractno']]['facility'][$wip['facilityno']]['A40010'] = (isset($contract_budget[$wip['contractno']][$wip['facilityno']]['A40010']) && !is_null($contract_budget[$wip['contractno']][$wip['facilityno']]['A40010'])) ? $contract_budget[$wip['contractno']][$wip['facilityno']]['A40010'] : 0; } $wipwhole_status[$wip['facilityno']] = $wip; } // 整理arrayData[合約]['PayStage']進 (新增) arrayData[合約]['facility'][作番號][PayStage] $sign_array = ['訂金', '簽約', '合約']; // 將所有facility的paystage放入一個array以便計算順序 $allPayStages = []; foreach ($arrayData as &$value) { if (is_iterable($value['facility'])) { foreach ($value['facility'] as $fakey => &$facility) { $ratio = get_contract_ratio($fakey, $contract_budget[$value[10]]); for ($stage = 1; $stage < (count($value['PayStage']) + 1); $stage++) { $facility['PayStage'][$stage] = $value['PayStage'][$stage]; $facility['PayStage'][$stage]['facilityno'] = $fakey; //算每個作番的金額 $facility['PayStage'][$stage]['PayAmount'] = $value['PayStage'][$stage]['PayAmount'] * $ratio; //算每個作番每個收款階段的收款時間 switch ($facility['PayStage'][$stage]['type']) { case "A": switch ($facility['PayStage'][$stage]['name']) { case (stristr($facility['PayStage'][$stage]['name'], '試車')): if (isset($wipwhole_status[$fakey]['tryrun_end_date']) && $wipwhole_status[$fakey]['tryrun_end_date'] !== NULL && !empty($wipwhole_status[$fakey]['tryrun_end_date'])) { $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime($wipwhole_status[$fakey]['tryrun_end_date'])); } break; case (stristr($facility['PayStage'][$stage]['name'], '完工')): if (isset($wipwhole_status[$fakey]['delivery_date']) && $wipwhole_status[$fakey]['delivery_date'] !== NULL && !empty($wipwhole_status[$fakey]['delivery_date'])) { $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime($wipwhole_status[$fakey]['delivery_date'])); } break; case (stristr($facility['PayStage'][$stage]['name'], '尾款')): if (isset($wipwhole_status[$fakey]['delivery_date']) && $wipwhole_status[$fakey]['delivery_date'] !== NULL && !empty($wipwhole_status[$fakey]['delivery_date'])) { $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime($wipwhole_status[$fakey]['delivery_date'])); } break; case (stristr($facility['PayStage'][$stage]['name'], '交車')): if (isset($wipwhole_status[$fakey]['delivery_date']) && $wipwhole_status[$fakey]['delivery_date'] !== NULL && !empty($wipwhole_status[$fakey]['delivery_date'])) { $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime($wipwhole_status[$fakey]['delivery_date'])); } break; case (stristr($facility['PayStage'][$stage]['name'], '驗收')): if (isset($wipwhole_status[$fakey]['delivery_date']) && $wipwhole_status[$fakey]['delivery_date'] !== NULL && !empty($wipwhole_status[$fakey]['delivery_date'])) { $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime($wipwhole_status[$fakey]['delivery_date'])); } break; case (stristr($facility['PayStage'][$stage]['name'], '安裝')): if (isset($wipwhole_status[$fakey]['install_end_date']) && $wipwhole_status[$fakey]['install_end_date'] !== NULL && !empty($wipwhole_status[$fakey]['install_end_date'])) { $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime($wipwhole_status[$fakey]['install_end_date'])); } break; case (stristr($facility['PayStage'][$stage]['name'], '貨抵工地')): if (isset($wipwhole_status[$fakey]['real_arrival_date']) && $wipwhole_status[$fakey]['real_arrival_date'] !== NULL && !empty($wipwhole_status[$fakey]['real_arrival_date'])) { $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime($wipwhole_status[$fakey]['real_arrival_date'])); } elseif (isset($wipwhole_status[$fakey]['real_contract_arrival_date']) && $wipwhole_status[$fakey]['real_contract_arrival_date'] !== NULL && !empty($wipwhole_status[$fakey]['real_contract_arrival_date'])) { $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime($wipwhole_status[$fakey]['real_contract_arrival_date'])); } break; default: break; }; case "B": break; case "C": switch ($facility['PayStage'][$stage]['name']) { case (stristr($facility['PayStage'][$stage]['name'], '試車')): if (isset($wipwhole_status[$fakey]['tryrun_end_date']) && $wipwhole_status[$fakey]['tryrun_end_date'] !== NULL && !empty($wipwhole_status[$fakey]['tryrun_end_date'])) { $haveNumber = (int) filter_var(str_replace('-', '', $facility['PayStage'][$stage]['name']), FILTER_SANITIZE_NUMBER_INT); $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime("+ $haveNumber months", strtotime($wipwhole_status[$fakey]['tryrun_end_date']))); } break; case (stristr($facility['PayStage'][$stage]['name'], '完工')): if (isset($wipwhole_status[$fakey]['delivery_date']) && $wipwhole_status[$fakey]['delivery_date'] !== NULL && !empty($wipwhole_status[$fakey]['delivery_date'])) { $haveNumber = (int) filter_var(str_replace('-', '', $facility['PayStage'][$stage]['name']), FILTER_SANITIZE_NUMBER_INT); $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime("+ $haveNumber months", strtotime($wipwhole_status[$fakey]['delivery_date']))); } break; case (stristr($facility['PayStage'][$stage]['name'], '尾款')): if (isset($wipwhole_status[$fakey]['delivery_date']) && $wipwhole_status[$fakey]['delivery_date'] !== NULL && !empty($wipwhole_status[$fakey]['delivery_date'])) { $haveNumber = (int) filter_var(str_replace('-', '', $facility['PayStage'][$stage]['name']), FILTER_SANITIZE_NUMBER_INT); $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime("+ $haveNumber months", strtotime($wipwhole_status[$fakey]['delivery_date']))); } break; case (stristr($facility['PayStage'][$stage]['name'], '交車')): if (isset($wipwhole_status[$fakey]['delivery_date']) && $wipwhole_status[$fakey]['delivery_date'] !== NULL && !empty($wipwhole_status[$fakey]['delivery_date'])) { $haveNumber = (int) filter_var(str_replace('-', '', $facility['PayStage'][$stage]['name']), FILTER_SANITIZE_NUMBER_INT); $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime("+ $haveNumber months", strtotime($wipwhole_status[$fakey]['delivery_date']))); } break; case (stristr($facility['PayStage'][$stage]['name'], '驗收')): if (isset($wipwhole_status[$fakey]['delivery_date']) && $wipwhole_status[$fakey]['delivery_date'] !== NULL && !empty($wipwhole_status[$fakey]['delivery_date'])) { $haveNumber = (int) filter_var(str_replace('-', '', $facility['PayStage'][$stage]['name']), FILTER_SANITIZE_NUMBER_INT); $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime("+ $haveNumber months", strtotime($wipwhole_status[$fakey]['delivery_date']))); } break; case (stristr($facility['PayStage'][$stage]['name'], '安裝')): if (isset($wipwhole_status[$fakey]['install_end_date']) && $wipwhole_status[$fakey]['install_end_date'] !== NULL && !empty($wipwhole_status[$fakey]['install_end_date'])) { $haveNumber = (int) filter_var(str_replace('-', '', $facility['PayStage'][$stage]['name']), FILTER_SANITIZE_NUMBER_INT); $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime("+ $haveNumber months", strtotime($wipwhole_status[$fakey]['install_end_date']))); } break; case (stristr($facility['PayStage'][$stage]['name'], '貨抵工地')): if (isset($wipwhole_status[$fakey]['real_arrival_date']) && $wipwhole_status[$fakey]['real_arrival_date'] !== NULL && !empty($wipwhole_status[$fakey]['real_arrival_date'])) { $haveNumber = (int) filter_var(str_replace('-', '', $facility['PayStage'][$stage]['name']), FILTER_SANITIZE_NUMBER_INT); $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime("+ $haveNumber months", strtotime($wipwhole_status[$fakey]['real_arrival_date']))); } elseif (isset($wipwhole_status[$fakey]['real_contract_arrival_date']) && $wipwhole_status[$fakey]['real_contract_arrival_date'] !== NULL && !empty($wipwhole_status[$fakey]['real_contract_arrival_date'])) { $haveNumber = (int) filter_var(str_replace('-', '', $facility['PayStage'][$stage]['name']), FILTER_SANITIZE_NUMBER_INT); $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime("+ $haveNumber months", strtotime($wipwhole_status[$fakey]['real_contract_arrival_date']))); } break; default: break; }; default: break; } } if (isset($allPayStages[$value[10]])) { $allPayStages[$value[10]] = array_merge($allPayStages[$value[10]], $facility['PayStage']); } else { $allPayStages[$value[10]] = $facility['PayStage']; } } } } foreach ($allPayStages as $paykey => $pay) { uasort($allPayStages[$paykey], 'comparePlanPayDate'); } foreach ($allPayStages as &$payStage) { $sequenceNumber = 1; foreach ($payStage as $tk => &$pstage) { $pstage['sequenceNumber'] = $sequenceNumber; $sequenceNumber += 1; } } // 整理好的收款階段 $final_paystage = array(); foreach ($allPayStages as $key => &$payStage) { if (is_iterable($payStage)) { foreach ($payStage as $key2 => $pstage) { $final_paystage[$key][$pstage['sequenceNumber']] = $pstage; } } } // [invoice][發票號碼][0 發票日期, 1 發票金額, 2 發票狀態, 3 收款日期, 4 收款金額, 5 手續費, 6 核銷單號] foreach ($invoice_data as $invoice) { if (isset($arrayData[$invoice['ProjectId']])) { $arrayData[$invoice['ProjectId']]['invoice_budget'] += intval($invoice['OAmountWithTax']); $arrayData[$invoice['ProjectId']]['invoice'][$invoice['InvoiceNo']] = [date("Ymd", strtotime($invoice['InvoiceTime'])), $invoice['OAmountWithTax'], $invoice['InvoiceState']]; } } // 把核銷單的內容補到發票資料中 foreach ($received_array as $received) { $BillNo = $received['OrderBillNo']; if (isset($arrayData[$BillNo])) { if (isset($arrayData[$BillNo]['invoice'][$received['InvoiceNo']])) { $arrayData[$BillNo]['received_budget'] += $received['PayWriteOffOAmount']; array_push($arrayData[$BillNo]['invoice'][$received['InvoiceNo']], date($received['ReceivedDate']), $received['PayWriteOffOAmount'], $received['Fee'], $received['BillNo']); } } } // $final_paystage ['type'] ['PlanPayDate'] ['PayAmount'] ['facilityno'] ['sequenceNumber'] ['name'] ['InvoiceAmount'] ['receivedAmount'] foreach($final_paystage as $key => &$payStage){ if(isset($arrayData[$key]['invoice_budget']) && isset($arrayData[$key]['received_budget'])){ $tmpInvoice = $arrayData[$key]['invoice_budget']; $tmpReceived = $arrayData[$key]['received_budget']; for($sequence = 1; $sequence <= count($payStage); $sequence++){ $collect_month = collect_month(strtotime($payStage[$sequence]['PlanPayDate'])); if($tmpInvoice >$payStage[$sequence]['PayAmount']){ $payStage[$sequence]['InvoiceAmount'] = $payStage[$sequence]['PayAmount']; $tmpInvoice -= $payStage[$sequence]['PayAmount']; }else{ $payStage[$sequence]['InvoiceAmount'] = $tmpInvoice; $tmpInvoice = 0; } if($tmpReceived >$payStage[$sequence]['PayAmount']){ $payStage[$sequence]['receivedAmount'] = $payStage[$sequence]['PayAmount']; $tmpReceived -= $payStage[$sequence]['PayAmount']; }else{ $payStage[$sequence]['receivedAmount'] = $tmpReceived; $tmpReceived = 0; } if($collect_month>0){ // $arrayData[$key]['facility'][$payStage[$sequence]['facilityno']]['receivable_budget']+=$payStage[$sequence]['PayAmount']; } if($collect_month>0 && $payStage[$sequence]['PayAmount']>$payStage[$sequence]['receivedAmount']){ $payStage[$sequence]['collect_month'] = $collect_month; }else{ $payStage[$sequence]['collect_month'] = 0; } } } } // foreach($final_paystage as $key => $payStage){ // echo $key."
"; // for($sequence = 1; $sequence <= count($payStage); $sequence++){ // print_r($payStage[$sequence]); // echo "
"; // } // echo "---------------------
"; // } foreach ($arrayData as &$value) { // if (is_iterable($value['facility'])) { // foreach ($value['facility'] as $fakey => &$facility) { // // foreach ($facility['PayStage'] as $index => &$payStage) { // // // $payStage['sequenceNumber'] = $allPayStages[$value[10]][$index]['sequenceNumber']; // // echo $index." => "; // // print_r($payStage); // // echo "
"; // // } // echo $fakey." => "; // print_r($facility); // echo "
"; // } // } print_r($value); echo "---------------------
"; } exit(); //========================================================================================================================================================= //產生excel的array $excel_aray = array(); $boga_array = array(); $exclude_boga_array = array(); $facility_array = array(); $facility_boga_array = array(); $facility_exclude_boga_array = array(); foreach ($arrayData as $key => $value) { $sum_A40001 += isset($value['A40001']) ? $value['A40001'] : 0; $sum_A40008 += isset($value['A40008']) ? $value['A40008'] : 0; $sum_total_budget += $value['total_budget']; $sum_facility += $value['total_facility_num']; $value['sign'][3] = (isset($value['sign'][3])) ? $value['sign'][3] : 0; $value['second'][3] = (isset($value['second'][3])) ? $value['second'][3] : 0; $value['arrive'][3] = (isset($value['arrive'][3])) ? $value['arrive'][3] : 0; $value['install'][3] = (isset($value['install'][3])) ? $value['install'][3] : 0; $value['tryrun'][3] = (isset($value['tryrun'][3])) ? $value['tryrun'][3] : 0; $value['check'][3] = (isset($value['check'][3])) ? $value['check'][3] : 0; $value['delivery'][3] = (isset($value['delivery'][3])) ? $value['delivery'][3] : 0; $value['final'][3] = (isset($value['final'][3])) ? $value['final'][3] : 0; $value[9] = (isset($value[9])) ? Date('Ymd', strtotime($value[9])) : 0; $contractstage = array('sign', 'second', 'arrive', 'install', 'tryrun', 'check', 'delivery', 'final'); foreach ($contractstage as $i) { $value[$i]['max'] = (empty($value[$i]['max']) || is_null($value[$i]['max'])) ? 0 : $value[$i]['max']; $value[$i]['min'] = (empty($value[$i]['min']) || is_null($value[$i]['min'])) ? 0 : $value[$i]['min']; $value[$i][4] = (isset($value[$i][4])) ? $value[$i][4] : 0; $value[$i][5] = (isset($value[$i][5])) ? $value[$i][5] : 0; $value['A40001'] = (isset($value['A40001'])) ? $value['A40001'] : 0; $value['A40008'] = (isset($value['A40008'])) ? $value['A40008'] : 0; }; if (isset($value['sign'][0]) && $value['sign'][0] == "寶佳出貨前30天") { $boga_array[$key] = [ $value[9], $key, $value[1], $value[2], $value[4], $value[5], $value[7], $value[8], $value[6], number_format(round($value['sign'][1])), number_format(round($value['sign'][3])), number_format(round($value['sign'][6])), number_format(round($value['sign'][7])), number_format(round($value['sign'][4])), number_format(round($value['sign'][5])), $value['sign']['max'], $value['sign']['min'], number_format(round($value['second'][1])), number_format(round($value['second'][3])), number_format(round($value['second'][6])), number_format(round($value['second'][7])), number_format(round($value['second'][4])), number_format(round($value['second'][5])), $value['second']['max'], $value['second']['min'], number_format(round($value['arrive'][1])), number_format(round($value['arrive'][3])), number_format(round($value['arrive'][6])), number_format(round($value['arrive'][7])), number_format(round($value['arrive'][4])), number_format(round($value['arrive'][5])), $value['arrive']['max'], $value['arrive']['min'], number_format(round($value['install'][1])), number_format(round($value['install'][3])), number_format(round($value['install'][6])), number_format(round($value['install'][7])), number_format(round($value['install'][4])), number_format(round($value['install'][5])), $value['install']['max'], $value['install']['min'], number_format(round($value['tryrun'][1])), number_format(round($value['tryrun'][3])), number_format(round($value['tryrun'][6])), number_format(round($value['tryrun'][7])), number_format(round($value['tryrun'][4])), number_format(round($value['tryrun'][5])), $value['tryrun']['max'], $value['tryrun']['min'], number_format(round($value['check'][1])), number_format(round($value['check'][3])), number_format(round($value['check'][6])), number_format(round($value['check'][7])), number_format(round($value['check'][4])), number_format(round($value['check'][5])), $value['check']['max'], $value['check']['min'], number_format(round($value['delivery'][1])), number_format(round($value['delivery'][3])), number_format(round($value['delivery'][6])), number_format(round($value['delivery'][7])), number_format(round($value['delivery'][4])), number_format(round($value['delivery'][5])), $value['delivery']['max'], $value['delivery']['min'], number_format(round($value['final'][1])), number_format(round($value['final'][3])), number_format(round($value['final'][6])), number_format(round($value['final'][7])), number_format(round($value['final'][4])), number_format(round($value['final'][5])), $value['final']['max'], $value['final']['min'], number_format(round($value['A40001'])), number_format(round($value['A40008'])), number_format(round($value['total_budget'])), number_format(round($value['receivable_budget'])), number_format(round($value['invoice_budget'])), number_format(round($value['received_budget'])), $value['total_facility_num'], str_replace('
', '; ', $value['facilities']) ]; } else { $exclude_boga_array[$key] = [ $value[9], $key, $value[1], $value[2], $value[4], $value[5], $value[7], $value[8], $value[6], number_format(round($value['sign'][1])), number_format(round($value['sign'][3])), number_format(round($value['sign'][6])), number_format(round($value['sign'][7])), number_format(round($value['sign'][4])), number_format(round($value['sign'][5])), $value['sign']['max'], $value['sign']['min'], number_format(round($value['second'][1])), number_format(round($value['second'][3])), number_format(round($value['second'][6])), number_format(round($value['second'][7])), number_format(round($value['second'][4])), number_format(round($value['second'][5])), $value['second']['max'], $value['second']['min'], number_format(round($value['arrive'][1])), number_format(round($value['arrive'][3])), number_format(round($value['arrive'][6])), number_format(round($value['arrive'][7])), number_format(round($value['arrive'][4])), number_format(round($value['arrive'][5])), $value['arrive']['max'], $value['arrive']['min'], number_format(round($value['install'][1])), number_format(round($value['install'][3])), number_format(round($value['install'][6])), number_format(round($value['install'][7])), number_format(round($value['install'][4])), number_format(round($value['install'][5])), $value['install']['max'], $value['install']['min'], number_format(round($value['tryrun'][1])), number_format(round($value['tryrun'][3])), number_format(round($value['tryrun'][6])), number_format(round($value['tryrun'][7])), number_format(round($value['tryrun'][4])), number_format(round($value['tryrun'][5])), $value['tryrun']['max'], $value['tryrun']['min'], number_format(round($value['check'][1])), number_format(round($value['check'][3])), number_format(round($value['check'][6])), number_format(round($value['check'][7])), number_format(round($value['check'][4])), number_format(round($value['check'][5])), $value['check']['max'], $value['check']['min'], number_format(round($value['delivery'][1])), number_format(round($value['delivery'][3])), number_format(round($value['delivery'][6])), number_format(round($value['delivery'][7])), number_format(round($value['delivery'][4])), number_format(round($value['delivery'][5])), $value['delivery']['max'], $value['delivery']['min'], number_format(round($value['final'][1])), number_format(round($value['final'][3])), number_format(round($value['final'][6])), number_format(round($value['final'][7])), number_format(round($value['final'][4])), number_format(round($value['final'][5])), $value['final']['max'], $value['final']['min'], number_format(round($value['A40001'])), number_format(round($value['A40008'])), number_format(round($value['total_budget'])), number_format(round($value['receivable_budget'])), number_format(round($value['invoice_budget'])), number_format(round($value['received_budget'])), $value['total_facility_num'], str_replace('
', '; ', $value['facilities']) ]; } $excel_aray[$key] = [ $value[9], $key, $value[1], $value[2], $value[4], $value[5], $value[7], $value[8], $value[6], number_format(round($value['sign'][1])), number_format(round($value['sign'][3])), number_format(round($value['sign'][6])), number_format(round($value['sign'][7])), number_format(round($value['sign'][4])), number_format(round($value['sign'][5])), $value['sign']['max'], $value['sign']['min'], number_format(round($value['second'][1])), number_format(round($value['second'][3])), number_format(round($value['second'][6])), number_format(round($value['second'][7])), number_format(round($value['second'][4])), number_format(round($value['second'][5])), $value['second']['max'], $value['second']['min'], number_format(round($value['arrive'][1])), number_format(round($value['arrive'][3])), number_format(round($value['arrive'][6])), number_format(round($value['arrive'][7])), number_format(round($value['arrive'][4])), number_format(round($value['arrive'][5])), $value['arrive']['max'], $value['arrive']['min'], number_format(round($value['install'][1])), number_format(round($value['install'][3])), number_format(round($value['install'][6])), number_format(round($value['install'][7])), number_format(round($value['install'][4])), number_format(round($value['install'][5])), $value['install']['max'], $value['install']['min'], number_format(round($value['tryrun'][1])), number_format(round($value['tryrun'][3])), number_format(round($value['tryrun'][6])), number_format(round($value['tryrun'][7])), number_format(round($value['tryrun'][4])), number_format(round($value['tryrun'][5])), $value['tryrun']['max'], $value['tryrun']['min'], number_format(round($value['check'][1])), number_format(round($value['check'][3])), number_format(round($value['check'][6])), number_format(round($value['check'][7])), number_format(round($value['check'][4])), number_format(round($value['check'][5])), $value['check']['max'], $value['check']['min'], number_format(round($value['delivery'][1])), number_format(round($value['delivery'][3])), number_format(round($value['delivery'][6])), number_format(round($value['delivery'][7])), number_format(round($value['delivery'][4])), number_format(round($value['delivery'][5])), $value['delivery']['max'], $value['delivery']['min'], number_format(round($value['final'][1])), number_format(round($value['final'][3])), number_format(round($value['final'][6])), number_format(round($value['final'][7])), number_format(round($value['final'][4])), number_format(round($value['final'][5])), $value['final']['max'], $value['final']['min'], number_format(round($value['A40001'])), number_format(round($value['A40008'])), number_format(round($value['total_budget'])), number_format(round($value['receivable_budget'])), number_format(round($value['invoice_budget'])), number_format(round($value['received_budget'])), $value['total_facility_num'], str_replace('
', '; ', $value['facilities']) ]; //['sign'] 0 款項名稱 1 簽約金額 2 簽約日期 3 應收金額 min 最早應收月份 max 最晚應收月份 //[合約號]['facility'] [作番號][款別]0 款項名稱 1 合約金額 2 應收日期 3 應收金額 4 已收金額 5 催收金額 6 催收次數 if (isset($value['facility'])) { foreach ($value['facility'] as $valkey => $val) { foreach ($contractstage as $stage) { $val[$stage][0] = (!(isset($val[$stage][0])) || is_null($val[$stage][0])) ? "-" : $val[$stage][0]; $val[$stage][1] = (!(isset($val[$stage][1])) || is_null($val[$stage][1])) ? 0 : $val[$stage][1]; $val[$stage][2] = (!(isset($val[$stage][2])) || is_null($val[$stage][2])) ? "-" : $val[$stage][2]; $val[$stage][3] = (!(isset($val[$stage][3])) || is_null($val[$stage][3])) ? 0 : $val[$stage][3]; $val[$stage][4] = (!(isset($val[$stage][4])) || is_null($val[$stage][4])) ? 0 : $val[$stage][4]; $val[$stage][5] = (!(isset($val[$stage][5])) || is_null($val[$stage][5])) ? 0 : $val[$stage][5]; $val[$stage][6] = (!(isset($val[$stage][6])) || is_null($val[$stage][6])) ? 0 : $val[$stage][6]; } $facility_array[$valkey] = [ $value[9], $valkey, $key, $value[1], $value[2], $value[4], $value[5], $value[7], $value[8], $value[6], $val['sign'][0], number_format(round($val['sign'][1])), $val['sign'][2], number_format(round($val['sign'][3])), number_format(round($val['sign'][4])), number_format(round($val['sign'][5])), number_format(round($val['sign'][6])), $val['second'][0], number_format(round($val['second'][1])), $val['second'][2], number_format(round($val['second'][3])), number_format(round($val['second'][4])), number_format(round($val['second'][5])), number_format(round($val['second'][6])), $val['arrive'][0], number_format(round($val['arrive'][1])), $val['arrive'][2], number_format(round($val['arrive'][3])), number_format(round($val['arrive'][4])), number_format(round($val['arrive'][5])), number_format(round($val['arrive'][6])), $val['install'][0], number_format(round($val['install'][1])), $val['install'][2], number_format(round($val['install'][3])), number_format(round($val['install'][4])), number_format(round($val['install'][5])), number_format(round($val['install'][6])), $val['tryrun'][0], number_format(round($val['tryrun'][1])), $val['tryrun'][2], number_format(round($val['tryrun'][3])), number_format(round($val['tryrun'][4])), number_format(round($val['tryrun'][5])), number_format(round($val['tryrun'][6])), $val['check'][0], number_format(round($val['check'][1])), $val['check'][2], number_format(round($val['check'][3])), number_format(round($val['check'][4])), number_format(round($val['check'][5])), number_format(round($val['check'][6])), $val['delivery'][0], number_format(round($val['delivery'][1])), $val['delivery'][2], number_format(round($val['delivery'][3])), number_format(round($val['delivery'][4])), number_format(round($val['delivery'][5])), number_format(round($val['delivery'][6])), $val['final'][0], number_format(round($val['final'][1])), $val['final'][2], number_format(round($val['final'][3])), number_format(round($val['final'][4])), number_format(round($val['final'][5])), number_format(round($val['final'][6])), str_replace('
', '; ', $val['status']), number_format(round($val['A40001'])), number_format(round($val['A40008'])), number_format(round($val['total_budget'])), number_format(round($val['receivable_budget'])) ]; if ($value['sign'][0] == "寶佳出貨前30天") { $facility_boga_array[$valkey] = [ $value[9], $valkey, $key, $value[1], $value[2], $value[4], $value[5], $value[7], $value[8], $value[6], $val['sign'][0], number_format(round($val['sign'][1])), $val['sign'][2], number_format(round($val['sign'][3])), number_format(round($val['sign'][4])), number_format(round($val['sign'][5])), number_format(round($val['sign'][6])), $val['second'][0], number_format(round($val['second'][1])), $val['second'][2], number_format(round($val['second'][3])), number_format(round($val['second'][4])), number_format(round($val['second'][5])), number_format(round($val['second'][6])), $val['arrive'][0], number_format(round($val['arrive'][1])), $val['arrive'][2], number_format(round($val['arrive'][3])), number_format(round($val['arrive'][4])), number_format(round($val['arrive'][5])), number_format(round($val['arrive'][6])), $val['install'][0], number_format(round($val['install'][1])), $val['install'][2], number_format(round($val['install'][3])), number_format(round($val['install'][4])), number_format(round($val['install'][5])), number_format(round($val['install'][6])), $val['tryrun'][0], number_format(round($val['tryrun'][1])), $val['tryrun'][2], number_format(round($val['tryrun'][3])), number_format(round($val['tryrun'][4])), number_format(round($val['tryrun'][5])), number_format(round($val['tryrun'][6])), $val['check'][0], number_format(round($val['check'][1])), $val['check'][2], number_format(round($val['check'][3])), number_format(round($val['check'][4])), number_format(round($val['check'][5])), number_format(round($val['check'][6])), $val['delivery'][0], number_format(round($val['delivery'][1])), $val['delivery'][2], number_format(round($val['delivery'][3])), number_format(round($val['delivery'][4])), number_format(round($val['delivery'][5])), number_format(round($val['delivery'][6])), $val['final'][0], number_format(round($val['final'][1])), $val['final'][2], number_format(round($val['final'][3])), number_format(round($val['final'][4])), number_format(round($val['final'][5])), number_format(round($val['final'][6])), str_replace('
', '; ', $val['status']), number_format(round($val['A40001'])), number_format(round($val['A40008'])), number_format(round($val['total_budget'])), number_format(round($val['receivable_budget'])) ]; } else { $facility_exclude_boga_array[$valkey] = [ $value[9], $valkey, $key, $value[1], $value[2], $value[4], $value[5], $value[7], $value[8], $value[6], $val['sign'][0], number_format(round($val['sign'][1])), $val['sign'][2], number_format(round($val['sign'][3])), number_format(round($val['sign'][4])), number_format(round($val['sign'][5])), number_format(round($val['sign'][6])), $val['second'][0], number_format(round($val['second'][1])), $val['second'][2], number_format(round($val['second'][3])), number_format(round($val['second'][4])), number_format(round($val['second'][5])), number_format(round($val['second'][6])), $val['arrive'][0], number_format(round($val['arrive'][1])), $val['arrive'][2], number_format(round($val['arrive'][3])), number_format(round($val['arrive'][4])), number_format(round($val['arrive'][5])), number_format(round($val['arrive'][6])), $val['install'][0], number_format(round($val['install'][1])), $val['install'][2], number_format(round($val['install'][3])), number_format(round($val['install'][4])), number_format(round($val['install'][5])), number_format(round($val['install'][6])), $val['tryrun'][0], number_format(round($val['tryrun'][1])), $val['tryrun'][2], number_format(round($val['tryrun'][3])), number_format(round($val['tryrun'][4])), number_format(round($val['tryrun'][5])), number_format(round($val['tryrun'][6])), $val['check'][0], number_format(round($val['check'][1])), $val['check'][2], number_format(round($val['check'][3])), number_format(round($val['check'][4])), number_format(round($val['check'][5])), number_format(round($val['check'][6])), $val['delivery'][0], number_format(round($val['delivery'][1])), $val['delivery'][2], number_format(round($val['delivery'][3])), number_format(round($val['delivery'][4])), number_format(round($val['delivery'][5])), number_format(round($val['delivery'][6])), $val['final'][0], number_format(round($val['final'][1])), $val['final'][2], number_format(round($val['final'][3])), number_format(round($val['final'][4])), number_format(round($val['final'][5])), number_format(round($val['final'][6])), str_replace('
', '; ', $val['status']), number_format(round($val['A40001'])), number_format(round($val['A40008'])), number_format(round($val['total_budget'])), number_format(round($val['receivable_budget'])) ]; } } } } $average_A40001 = round($sum_A40001 / $sum_facility, 2); $average_A40008 = round($sum_A40008 / $sum_facility, 2); $average_budget = round($sum_total_budget / $sum_facility, 2); $total_data = json_encode($excel_aray); $boga_data = json_encode($boga_array); $exclude_boga_data = json_encode($exclude_boga_array); $facility_data = json_encode($facility_array); $facility_boga_data = json_encode($facility_boga_array); $facility_exclude_boga_data = json_encode($facility_exclude_boga_array); ?>

合約 (新梯)統整資訊

$value) { ?>
合約號 部門 經理 營業員 客戶名稱 簽約款 應收簽約金額 簽約最小催收次數 簽約最大催收次數 二次款 應收二次金額 二次最小催收次數 二次最大催收次數 貨抵工地款 應收貨抵工地金額 貨抵工地最小催收次數 貨抵工地最大催收次數 安裝款 應收安裝金額 安裝最小催收次數 安裝最大催收次數 試車款 應收試車金額 試車最小催收次數 試車最大催收次數 官檢款 應收官檢金額 官檢最小催收次數 官檢最大催收次數 移交款 應收移交金額 移交最小催收次數 移交最大催收次數 尾款 應收尾款金額 尾款最小催收次數 尾款最大催收次數 合約總金額 目前應收 已開發票金額 已收金額 收款狀態