diff --git a/wms/account-receivable-renovate-excel.php b/wms/account-receivable-renovate-excel.php index 77341d5a..36463a97 100644 --- a/wms/account-receivable-renovate-excel.php +++ b/wms/account-receivable-renovate-excel.php @@ -13,30 +13,51 @@ $Bill = json_decode(file_get_contents("php://input"), true); $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); // file_put_contents('account-receivable.txt', json_encode($Bill)); +if ($type == 'repair') { + $colomnHeader = [ + '合約號', '合約日期', '客戶編號', '客戶名稱', + '部門', '契約人員工號', '契約人員名稱', + '主管工號', '主管名稱', + '合約總金額', '應收金額', + '已開金額', '未開金額', + '已收金額', '催收金額', '催收次數' + ]; + $sheet->fromArray($colomnHeader, NULL, 'A1'); + $rowIndex = 2; + foreach ($Bill as $key => $value) { -$colomnHeader = [ - '合約號', '作番號', '部門編號', '部門名稱', '主管工號','主管名稱', '營業員工號', '營業員名稱', '客戶名稱', '抬頭', '統編', '通訊地址', - '收款階段名稱', '應收日期','是否應收', '應收金額', '已開金額','未開金額', '已收金額','催收金額', '催收次數' -]; -$sheet->fromArray($colomnHeader, NULL, 'A1'); -$rowIndex = 2; -foreach ($Bill as $key => $value) { - - $sheet->fromArray($value, NULL, 'A' . $rowIndex); - $column = [ - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R','S','T' + $sheet->fromArray($value, NULL, 'A' . $rowIndex); + $column = [ + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q' + ]; + foreach ($column as $col) { + $sheet->getColumnDimension($col)->setAutoSize(true); + } + $rowIndex++; + } + $writer = new Xlsx($spreadsheet); + $excelFileName = 'account-receivable-repair.xlsx'; +} else { + $colomnHeader = [ + '合約號', '作番號', '部門編號', '部門名稱', '主管工號', '主管名稱', '營業員工號', '營業員名稱', '客戶名稱', '抬頭', '統編', '通訊地址', + '收款階段名稱', '應收日期', '是否應收', '應收金額', '已開金額', '未開金額', '已收金額', '催收金額', '催收次數' ]; - foreach ($column as $col) { - $sheet->getColumnDimension($col)->setAutoSize(true); + $sheet->fromArray($colomnHeader, NULL, 'A1'); + $rowIndex = 2; + foreach ($Bill as $key => $value) { + + $sheet->fromArray($value, NULL, 'A' . $rowIndex); + $column = [ + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T' + ]; + foreach ($column as $col) { + $sheet->getColumnDimension($col)->setAutoSize(true); + } + $rowIndex++; } - $rowIndex++; + $writer = new Xlsx($spreadsheet); + $excelFileName = 'account-receivable-renovate.xlsx'; } -$writer = new Xlsx($spreadsheet); -$excelFileName = 'account-receivable-renovate.xlsx'; $writer->save($excelFileName); echo $excelFileName; -?> - \ No newline at end of file diff --git a/wms/account-receivable-renovate.xlsx b/wms/account-receivable-renovate.xlsx index 3c8c5dcf..0228fc23 100644 Binary files a/wms/account-receivable-renovate.xlsx and b/wms/account-receivable-renovate.xlsx differ diff --git a/wms/account-receivable-repair-index.php b/wms/account-receivable-repair-index.php index 49d50385..bd1d7acc 100644 --- a/wms/account-receivable-repair-index.php +++ b/wms/account-receivable-repair-index.php @@ -2,18 +2,11 @@ include "header.php"; -$sum_facility = 0; -// M1 -$sum_A40002 = 0; -// 設備 -$sum_A40009 = 0; -// 安裝 -$sum_A40010 = 0; + $sum_total_budget = 0; -$average_budget = 0; -$average_A40002 = 0; -$average_A40009 = 0; -$average_A40010 = 0; +$start_date = empty($_POST['date_start']) ? null : $_POST['date_start']; +$end_date = empty($_POST['date_end']) ? null : $_POST['date_end']; + /* 比較應收日期 @param $a array @@ -33,7 +26,6 @@ $find_manager = array(); foreach ($managers as $manager) { $find_manager[$manager['accountid']] = $manager; } - $follower = find_follow($user_id); $arrayData = array(); $receivable_array = []; @@ -58,6 +50,14 @@ Main.CurrentState LEFT JOIN comGroupPerson AS Person ON MainAll.PersonId= Person.PersonId LEFT JOIN comDepartment AS Dept ON MainAll.DeptId= Dept.DeptId WHERE MainAll.TypeId = 'SP' AND (MainAll.CurrentState=2 OR MainAll.CurrentState=4) "; +if (!is_null($start_date)) { + $start_date = (int)date('Ymd', strtotime($start_date)); + $sql_contractNumber .= " AND MainAll.BillDate >= $start_date "; +} +if (!is_null($end_date)) { + $end_date = (int)date('Ymd', strtotime($end_date)); + $sql_contractNumber .= " AND MainAll.BillDate <= $end_date "; +} if ((in_array($user_id, array('M0008', 'M0012'))) || (in_array(accountidToDepartId($user_id), array('220', '210', '501')))) { } else { $sql_contractNumber .= " AND (MainAll.PersonId = '$user_id'"; @@ -76,47 +76,61 @@ $query_contract_number = $conn->query($sql_contractNumber); if (is_iterable($query_contract_number)) { foreach ($query_contract_number as $contract_numbers) { array_push($contractNumbers, $contract_numbers['BillNo']); - $arrayData[$contract_numbers['BillNo']]['BillNo'] = $contract_numbers['BillNo']; - $arrayData[$contract_numbers['BillNo']]['BillDate'] = $contract_numbers['BillDate']; - $arrayData[$contract_numbers['BillNo']]['CustomerId'] = $contract_numbers['BizPartnerId']; - $arrayData[$contract_numbers['BillNo']]['CustomerAddress'] = $contract_numbers['ContactAddress']; - $arrayData[$contract_numbers['BillNo']]['OAmountWithTax'] = $contract_numbers['OAmountWithTax']; - $arrayData[$contract_numbers['BillNo']]['PersonId'] = $contract_numbers['PersonId']; - $arrayData[$contract_numbers['BillNo']]['DeptId'] = $contract_numbers['DeptId']; - $arrayData[$contract_numbers['BillNo']]['PersonName'] = $contract_numbers['PersonName']; - $arrayData[$contract_numbers['BillNo']]['DeptName'] = $contract_numbers['DeptName']; - $arrayData[$contract_numbers['BillNo']]['ShortName'] = $contract_numbers['ShortName']; - $arrayData[$contract_numbers['BillNo']]['ManagerId'] = $find_manager[$contract_numbers['PersonId']]['manager']; - $arrayData[$contract_numbers['BillNo']]['ManagerName'] = $find_manager[$contract_numbers['PersonId']]['manager_name']; + $arrayData[$contract_numbers['BillNo']]['BillNo'] = isset($contract_numbers['BillNo']) ? $contract_numbers['BillNo'] : '--'; + $arrayData[$contract_numbers['BillNo']]['BillDate'] = isset($contract_numbers['BillDate']) ? $contract_numbers['BillDate'] : '--'; + $arrayData[$contract_numbers['BillNo']]['CustomerId'] = isset($contract_numbers['BizPartnerId']) ? $contract_numbers['BizPartnerId'] : '--'; + $arrayData[$contract_numbers['BillNo']]['CustomerName'] = isset($contract_numbers['BizPartnerName']) ? $contract_numbers['BizPartnerName'] : '--'; + $arrayData[$contract_numbers['BillNo']]['CustomerAddress'] = isset($contract_numbers['ContactAddress']) ? $contract_numbers['ContactAddress'] : '--'; + $arrayData[$contract_numbers['BillNo']]['OAmountWithTax'] = isset($contract_numbers['OAmountWithTax']) ? $contract_numbers['OAmountWithTax'] : 0; + $arrayData[$contract_numbers['BillNo']]['PersonId'] = isset($contract_numbers['PersonId']) ? $contract_numbers['PersonId'] : '--'; + $arrayData[$contract_numbers['BillNo']]['DeptId'] = isset($contract_numbers['DeptId']) ? $contract_numbers['DeptId'] : '--'; + $arrayData[$contract_numbers['BillNo']]['PersonName'] = isset($contract_numbers['PersonName']) ? $contract_numbers['PersonName'] : '--'; + $arrayData[$contract_numbers['BillNo']]['DeptName'] = isset($contract_numbers['DeptName']) ? $contract_numbers['DeptName'] : '--'; + $arrayData[$contract_numbers['BillNo']]['ShortName'] = isset($contract_numbers['ShortName']) ? $contract_numbers['ShortName'] : '--'; + $arrayData[$contract_numbers['BillNo']]['ManagerId'] = !empty($find_manager[$contract_numbers['PersonId']]['manager']) ? $find_manager[$contract_numbers['PersonId']]['manager'] : '--'; + $arrayData[$contract_numbers['BillNo']]['ManagerName'] = !empty($find_manager[$contract_numbers['PersonId']]['manager_name']) ? $find_manager[$contract_numbers['PersonId']]['manager_name'] : '--'; + $arrayData[$contract_numbers['BillNo']]['invoice_budget'] = 0; + $arrayData[$contract_numbers['BillNo']]['received_budget'] = 0; + $arrayData[$contract_numbers['BillNo']]['receivable_budget'] = 0; + $sum_total_budget += $contract_numbers['OAmountWithTax']; } $str_numbers = "('" . implode("','", $contractNumbers) . "')"; } -// T8發票 (有專案的發票) -$sql_invoice = "SELECT Detail.BillNo AS InvoiceBillNo, Detail.MaterialId, Detail.LAmountWithTax, Main.InvoiceNo, CheckAll.FromSalSalesOrder AS BillNo FROM arSellInvoiceMaterial AS Detail -LEFT JOIN arSellInvoice AS Main ON Detail.BillNo=Main.BillNo -LEFT JOIN ( -SELECT CheckDetail.BillNo, CheckDetail.FromSalSalesOrder FROM arCheckBillDetail AS CheckDetail ) AS CheckAll ON CheckAll.BillNo= Detail.FromBillNo -WHERE Detail.ItemType=0"; +// T8發票 +$sql_invoice = "SELECT +Detail.BillNo AS InvoiceBillNo, +Detail.MaterialId, +Detail.LAmountWithTax, +Main.InvoiceNo, +CheckAll.FromSalSalesOrder AS BillNo + FROM arSellInvoiceMaterial AS Detail + LEFT JOIN arSellInvoice AS Main ON Detail.BillNo=Main.BillNo + LEFT JOIN ( + SELECT CheckDetail.BillNo, CheckDetail.FromSalSalesOrder, CheckDetail.RowNo + FROM arCheckBillDetail AS CheckDetail ) + AS CheckAll ON CheckAll.BillNo= Detail.FromBillNo AND Detail.FromRowCode = CheckAll.RowNo + WHERE Detail.ItemType=0 AND Main.InvoiceState!=2"; // 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"; +CheckDetail.OrderBillNo, +arWriteOffBillDetail.FromBillNo AS CheckBillNo, +arWriteOffBillDetail.FromRowCode AS CheckRowCode, +arWriteOffBillDetail.CurrStandOffOAmount +FROM arWriteOffBillDetail + LEFT JOIN + (SELECT + arCheckBillDetail.BillNo, + arCheckBillDetail.RowCode, + arCheckBillDetail.FromSalSalesOrder AS OrderBillNo, + salSalesOrder.TypeId + FROM arCheckBillDetail + LEFT JOIN salSalesOrder + ON salSalesOrder.BillNo =arCheckBillDetail.FromSalSalesOrder ) + AS CheckDetail + ON CheckDetail.BillNo=arWriteOffBillDetail.FromBillNo AND arWriteOffBillDetail.FromRowCode = CheckDetail.RowCode +WHERE CheckDetail.TypeId = 'SP'"; //T8 銷售訂單 階段收款計畫 @@ -134,6 +148,15 @@ $allPayStages = array(); foreach ($PayStage as $stage) { $allPayStages[$stage['BillNo']][$stage['PayStage']]['PlanPayAmt'] = $stage['PlanPayAmt']; $allPayStages[$stage['BillNo']][$stage['PayStage']]['PlanPayDate'] = $stage['PlanPayDate']; + $month = collect_month(strtotime($stage['PlanPayDate'])); + if (strtotime($stage['PlanPayDate']) > strtotime(date('Y-m-t', strtotime('now')))) { + $allPayStages[$stage['BillNo']][$stage['PayStage']]['receivable_ornot'] = '--'; + } else { + $allPayStages[$stage['BillNo']][$stage['PayStage']]['receivable_ornot'] = 'Y'; + } + $allPayStages[$stage['BillNo']][$stage['PayStage']]['month'] = $month; + $allPayStages[$stage['BillNo']][$stage['PayStage']]['received'] = 0; + $allPayStages[$stage['BillNo']][$stage['PayStage']]['invoice'] = 0; } foreach ($allPayStages as $paykey => $pay) { uasort($allPayStages[$paykey], 'comparePlanPayDate'); @@ -153,6 +176,9 @@ foreach ($allPayStages as $key => &$payStage) { foreach ($payStage as $key2 => $pstage) { if (isset($arrayData[$key])) { $arrayData[$key]['PayStage'][$pstage['sequenceNumber']] = $pstage; + if (isset($pstage['receivable_ornot']) && $pstage['receivable_ornot'] == 'Y') { + $arrayData[$key]['receivable_budget'] += $pstage['PlanPayAmt']; + } } } } @@ -160,11 +186,9 @@ foreach ($allPayStages as $key => &$payStage) { foreach ($invoice_data as $inv) { if (isset($arrayData[$inv['BillNo']])) { - if(isset($arrayData[$inv['BillNo']]['invoice']['total'])) { - $arrayData[$inv['BillNo']]['invoice']['total'] += $inv['LAmountWithTax']; - } else { - $arrayData[$inv['BillNo']]['invoice']['total'] = $inv['LAmountWithTax']; - } + + $arrayData[$inv['BillNo']]['invoice_budget'] += $inv['LAmountWithTax']; + if (isset($arrayData[$inv['BillNo']]['invoice'][$inv['InvoiceNo']])) { $arrayData[$inv['BillNo']]['invoice'][$inv['InvoiceNo']] += $inv['LAmountWithTax']; } else { @@ -172,453 +196,64 @@ foreach ($invoice_data as $inv) { } } } -foreach($arrayData as $key => &$value){ - echo $key."
"; - echo "
-------
"; - if(isset($value['invoice'])){ - foreach($value['invoice'] as $invkey => $invvalue){ - echo $invkey."
"; - echo $invvalue."
"; - } - } - echo "
---------------------------
"; -} - - -exit(); - - - - -// 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 "; -if (isset($str_numbers)) { - $sql_contract_budget .= "AND A.BillNo IN $str_numbers "; -} - -$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']; - $salesId = $cont['PersonId']; - - //['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][0] = $cont['DeptId']; - $arrayData[$BillNo][1] = $cont['DeptName']; - $arrayData[$BillNo][3] = $salesId; - $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"; +foreach ($received_array as $received) { + if (isset($arrayData[$received['OrderBillNo']]) && isset($received['CurrStandOffOAmount']) && $received['CurrStandOffOAmount'] > 0) { + $arrayData[$received['OrderBillNo']]['received_budget'] += $received['CurrStandOffOAmount']; } } -$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])) { - $salesid = $arrayData[$BillNo][3]; - if (isset($find_manager[$salesid])) { - $arrayData[$BillNo][2] = $find_manager[$salesid]['manager_name']; - $arrayData[$BillNo][4] = $find_manager[$salesid]['name']; - $arrayData[$BillNo][11] = $find_manager[$salesid]['manager']; - } - $arrayData[$BillNo]['total_budget'] += $cont['OAmountWithTax']; - $sum_total_budget += $cont['OAmountWithTax']; - $arrayData[$BillNo][$cont['MaterialId']] += $cont['OAmountWithTax']; - switch ($cont['MaterialId']) { - case "A40002": - $sum_A40002 += $cont['OAmountWithTax']; - break; - case "A40009": - $sum_A40009 += $cont['OAmountWithTax']; - break; - case "A40010": - $sum_A40010 += $cont['OAmountWithTax']; - break; - } - 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 { - $sum_facility += 1; - $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']; +foreach ($arrayData as $key => &$value) { + if (!isset($value['PayStage'])) { + $value['receivable_budget'] = $value['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; -} - - -//取作番大日程作番與時程 進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']]['A40002'] = (isset($contract_budget[$wip['contractno']]['A40002']) && !is_null($contract_budget[$wip['contractno']]['A40002'])) ? $contract_budget[$wip['contractno']]['A40002'] : 0; - $arrayData[$wip['contractno']]['A40009'] = (isset($contract_budget[$wip['contractno']]['A40009']) && !is_null($contract_budget[$wip['contractno']]['A40009'])) ? $contract_budget[$wip['contractno']]['A40009'] : 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']]['A40009'] = (isset($contract_budget[$wip['contractno']][$wip['facilityno']]['A40009']) && !is_null($contract_budget[$wip['contractno']][$wip['facilityno']]['A40009'])) ? $contract_budget[$wip['contractno']][$wip['facilityno']]['A40009'] : 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 (isset($value['facility']) && is_iterable($value['facility'])) { - foreach ($value['facility'] as $fakey => &$facility) { - if (isset($contract_budget[$value[10]])) { - $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)) - 1; - $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)) - 1; - $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)) - 1; - $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)) - 1; - $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)) - 1; - $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)) - 1; - $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)) - 1; - $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)) - 1; - $facility['PayStage'][$stage]['PlanPayDate'] = date("Ymd", strtotime("+ $haveNumber months", strtotime($wipwhole_status[$fakey]['real_contract_arrival_date']))); - } - break; - default: - break; - }; - default: - break; - } + $tmpReceived = $value['received_budget']; + $tmpInvoice = $value['invoice_budget']; + if (is_iterable($value['PayStage'])) { + for ($sequence = 1; $sequence <= count($value['PayStage']); $sequence++) { + if ($value['PayStage'][$sequence]['PlanPayAmt'] > $tmpInvoice) { + $value['PayStage'][$sequence]['invoice'] = $tmpInvoice; + $tmpInvoice = 0; + } else { + $value['PayStage'][$sequence]['invoice'] = $value['PayStage'][$sequence]['PlanPayAmt']; + $tmpInvoice -= $value['PayStage'][$sequence]['PlanPayAmt']; } - if (isset($allPayStages[$value[10]])) { - $allPayStages[$value[10]] = array_merge($allPayStages[$value[10]], $facility['PayStage']); + if ($value['PayStage'][$sequence]['PlanPayAmt'] > $tmpReceived) { + $value['PayStage'][$sequence]['received'] = $tmpReceived; + $tmpReceived = 0; } else { - $allPayStages[$value[10]] = $facility['PayStage']; + $value['PayStage'][$sequence]['received'] = $value['PayStage'][$sequence]['PlanPayAmt']; + $tmpReceived -= $value['PayStage'][$sequence]['PlanPayAmt']; } } } + $value['received_budget'] = $tmpReceived; } -} -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['FromSalSalesOrder']])) { - $arrayData[$invoice['FromSalSalesOrder']]['invoice_budget'] += intval($invoice['OAmountWithTax']); - $arrayData[$invoice['FromSalSalesOrder']]['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; - } - } + if ($value['receivable_budget'] > 0 && $value['received_budget'] < $value['receivable_budget']) { + $value['month'] = collect_month(strtotime($value['BillDate'])); + } else { + $value['month'] = 0; } } -// 0 部門id 1 部門名稱 2 經理名稱 3 營業員id 4 營業員名稱 5 客戶名稱 6 抬頭 7 統編 8 通訊地址 9 T8單據日期 10 合約號 -// 0 合約號 1 作番號 2 部門id 3 部門名稱 4 經理名稱 5 營業員id 6 營業員名稱 7 客戶名稱 8 抬頭 9 統編 10 通訊地址 -// 11 收款階段名稱 12 應收日期 13 收款金額 14 已開金額 15 已收金額 16 催收次數 $excel_aray = array(); -$today = strtotime(date('Ymt')); + foreach ($arrayData as $key => $value) { - if (isset($final_paystage[$key])) { - for ($sequence = 1; $sequence <= count($final_paystage[$key]); $sequence++) { - $value[4] = (isset($value[4]) && !empty($value[4])) ? $value[4] : '--'; - $value[5] = (isset($value[5]) && !empty($value[5])) ? $value[5] : '--'; - $value[6] = (isset($value[6]) && !empty($value[6])) ? $value[6] : '--'; - $value[7] = (isset($value[7]) && !empty($value[7])) ? $value[7] : '--'; - $value[8] = (isset($value[8]) && !empty($value[8])) ? $value[8] : '--'; - $value[11] = (isset($value[11]) && !empty($value[11])) ? $value[11] : '--'; - $value[12] = (isset($value[12]) && !empty($value[12])) ? date('Y/m/d', strtotime($value[12])) : '--'; - $thisPayStage = $final_paystage[$key][$sequence]; - $keyname = $key . "/" . $final_paystage[$key][$sequence]['facilityno'] . "/" . $value[5]; - $havetopay = ($today >= strtotime($thisPayStage['PlanPayDate'])) ? "V" : "--"; - switch ($havetopay) { - case "V": - $unInvoiceAmount = $thisPayStage['PayAmount'] - $thisPayStage['InvoiceAmount']; - $unReceivedAmount = $thisPayStage['PayAmount'] - $thisPayStage['receivedAmount']; - break; - default: - $unInvoiceAmount = 0; - $unReceivedAmount = 0; - break; - } - $excel_aray[$keyname] = [ - $value[10], $thisPayStage['facilityno'], $value[0], $value[1], $value[11], $value[2], $value[3], $value[4], $value[5], $value[6], $value[7], $value[8], - $thisPayStage['name'], $thisPayStage['PlanPayDate'], $havetopay, - number_format(round($thisPayStage['PayAmount'])), - number_format(round($thisPayStage['InvoiceAmount'])), number_format(round($unInvoiceAmount)), - number_format(round($thisPayStage['receivedAmount'])), number_format(round($unReceivedAmount)), - number_format(round($thisPayStage['collect_month'])) - - ]; - } - } + $uninvoice_budget = $value['OAmountWithTax'] - $value['invoice_budget']; + $unreceive_budget = $value['receivable_budget'] - $value['received_budget']; + + $excel_aray[$key] = [ + $value['BillNo'], date('Y-m-d', strtotime($value['BillDate'])), $value['CustomerId'], $value['CustomerName'], + $value['DeptName'], $value['PersonId'], $value['PersonName'], + $value['ManagerId'], $value['ManagerName'], + number_format(round($value['OAmountWithTax'])), number_format(round($value['receivable_budget'])), + number_format(round($value['invoice_budget'])), number_format(round($uninvoice_budget)), + number_format(round($value['received_budget'])), $unreceive_budget, $value['month'] + ]; } + $total_data = json_encode($excel_aray); -//計算總作番數、金額、A40002、A40009、A40010金額與平均 -$average_budget = round($sum_total_budget / $sum_facility); -$average_A40002 = round($sum_A40002 / $sum_facility); -$average_A40009 = round($sum_A40009 / $sum_facility); -$average_A40010 = round($sum_A40010 / $sum_facility); - -foreach ($arrayData as $key => $data) { - echo $key . "
"; - print_r($data); - echo "
-----------------------------
"; -} -exit(); + ?>