You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

546 lines
24 KiB

<?php
include "header.php";
$follower = find_follow($user_id);
$arrayData = [];
$receivable_array = [];
$received_array = [];
//4 簽約 5 二次款 6 貨抵工地 7 安裝 8 試車 9官檢 10 交車
//arrive_num 貨抵工地 tryrun_num 試車安裝完畢 official_num 官檢完畢 delivery_num 移交
$sql_received = "SELECT * from account_received";
$sql_contract = "SELECT a.BillNo, 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.ModeId = 'M' AND (s.CurrentState=2 OR s.CurrentState=4) ";
if (!(in_array(accountidToDepartId($user_id), array('220', '210')))) {
$sql_contract .= "AND (s.PersonId = '$user_id'";
$sql_received .= " WHERE person_id = '$user_id'";
if (count($follower) > 0) {
$column_str = "('$user_id'" . ",'";
$column_str .= implode("','", $follower);
$column_str .= "')";
$sql_contract .= " OR s.PersonId IN $column_str)";
$sql_received .= " OR person_id IN $column_str)";
} else {
$sql_contract .= ")";
};
}
$sign60 = array('簽訂後60天', '簽約60日', '簽約後60天', '簽訂後60日內', '訂金支付後60天');
$sign90 = array('簽約後90天', '簽約後90日', '簽訂後90天');
$sign120 = array('簽約後120天', '簽約後120日', '簽訂後120天');
$contract = $conn->query($sql_contract);
// 取收款資料 account_received
$received_data = mysqli_query($link, $sql_received);
foreach ($received_data as $received) {
if ($received['BillNo'] == 'SO20230801001') {
$BillNo = 'M230947';
} elseif ($received['BillNo'] == 'SO20230801002') {
$BillNo = 'M231067';
} else {
$BillNo = $received['BillNo'];
};
if (isset($received_array[$BillNo])) {
$received_array[$BillNo][0] += $received['invoice_amount'];
$received_array[$BillNo][1] += $received['received_amount'];
$received['status'] = (intval($received['status']) < 4) ? intval($received['status']) : 0;
if ($received_array[$BillNo][2] < intval($received['status'])) {
$received_array[$BillNo][2] = intval($received['status']);
}
} else {
$received_array[$BillNo][0] = $received['invoice_amount'];
$received_array[$BillNo][1] = $received['received_amount'];
$received_array[$BillNo][2] = (intval($received['status']) < 4) ? intval($received['status']) : 0;
}
}
// 將T8合約收款階段資料依據款別分類放進arraData裡
foreach ($contract as $cont) {
// print_r($cont);
// echo "<br>";
if ($cont['BillNo'] == 'SO20230801001') {
$BillNo = 'M230947';
} elseif ($cont['BillNo'] == 'SO20230801002') {
$BillNo = 'M231067';
} else {
$BillNo = $cont['BillNo'];
};
$PayStage = $cont['PayStage'];
$PlanPayDate = $cont['PlanPayDate'];
$PayAmount = $cont['PlanPayAmt'];
$partnerName = $cont['BizPartnerName'];
$EnterpriseName = $cont['EnterpriseName'];
$TaxNo = $cont['TaxNo'];
$ContactAddress = $cont['ContactAddress'];
//['sign'] 0 款項名稱 1 簽約金額 2 簽約日期 ['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 尾款收款日期
if (!(isset($arrayData[$BillNo]))) {
// 0 部門id 1 部門名稱 2 經理名稱 3 營業員id 4 營業員名稱 5 客戶名稱 6 抬頭 7 統編 8 通訊地址
$arrayData[$BillNo] = [0, 0, 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;
}
if (stristr($PayStage, '二次款') || stristr($PayStage, '出貨前') || stristr($PayStage, '簽約後') || stristr($PayStage, '簽訂後') || stristr($PayStage, '建照核發時') || stristr($PayStage, '簽約60')) {
$arrayData[$BillNo]['second'][0] = $PayStage;
$arrayData[$BillNo]['second'][1] = $PayAmount;
$arrayData[$BillNo]['second'][2] = $PlanPayDate;
if (isset($arrayData[$BillNo]['sign'][2]) && !is_null($arrayData[$BillNo]['sign'][2])) {
$signtime = strtotime(strval($arrayData[$BillNo]['sign'][2]));
if (in_array($PayStage, $sign60)) {
$secondtime = $signtime - (60 * 86400);
$secondtime = date('Ymd', $secondtime);
$arrayData[$BillNo]['second'][2] = intval($secondtime);
} elseif (in_array($PayStage, $sign90)) {
$secondtime = $signtime - (90 * 86400);
$secondtime = date('Ymd', $secondtime);
$arrayData[$BillNo]['second'][2] = intval($secondtime);
} elseif (in_array($PayStage, $sign120)) {
$secondtime = $signtime - (120 * 86400);
$secondtime = date('Ymd', $secondtime);
$arrayData[$BillNo]['second'][2] = intval($secondtime);
} elseif (stristr($PayStage, '簽訂後30天')) {
$secondtime = $signtime - (30 * 86400);
$secondtime = date('Ymd', $secondtime);
$arrayData[$BillNo]['second'][2] = intval($secondtime);
}
}
} elseif ($PayStage == '簽約' || stristr($PayStage, '簽定') || stristr($PayStage, '簽訂') || stristr($PayStage, '訂金') || stristr($PayStage, '工地動工')) {
$arrayData[$BillNo]['sign'][0] = $PayStage;
$arrayData[$BillNo]['sign'][1] = $PayAmount;
$arrayData[$BillNo]['sign'][2] = $PlanPayDate;
} elseif (stristr($PayStage, '試車') || stristr($PayStage, '驗收完成')) {
$arrayData[$BillNo]['tryrun'][0] = $PayStage;
$arrayData[$BillNo]['tryrun'][1] = $PayAmount;
$arrayData[$BillNo]['tryrun'][2] = $PlanPayDate;
} elseif (stristr($PayStage, '安裝') || stristr($PayStage, '貨抵工地且完工')) {
$arrayData[$BillNo]['install'][0] = $PayStage;
$arrayData[$BillNo]['install'][1] = $PayAmount;
$arrayData[$BillNo]['install'][2] = $PlanPayDate;
} elseif (stristr($PayStage, '貨抵工地') || stristr($PayStage, '貨底工地')) {
$arrayData[$BillNo]['arrive'][0] = $PayStage;
$arrayData[$BillNo]['arrive'][1] = $PayAmount;
$arrayData[$BillNo]['arrive'][2] = $PlanPayDate;
} elseif (stristr($PayStage, '得合格函後6個月') || stristr($PayStage, '核可函取得後6個月') || stristr($PayStage, '交車')) {
$arrayData[$BillNo]['delivery'][0] = $PayStage;
$arrayData[$BillNo]['delivery'][1] = $PayAmount;
$arrayData[$BillNo]['delivery'][2] = $PlanPayDate;
} elseif (stristr($PayStage, '合格') || stristr($PayStage, '驗收') || stristr($PayStage, '許可') || stristr($PayStage, '核可')) {
$arrayData[$BillNo]['check'][0] = $PayStage;
$arrayData[$BillNo]['check'][1] = $PayAmount;
$arrayData[$BillNo]['check'][2] = $PlanPayDate;
} else {
$arrayData[$BillNo]['other'][1] = $PayAmount;
$arrayData[$BillNo]['other'][2] = $PlanPayDate;
}
}
$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.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='A'
)AS tmp2
left join (
SELECT DISTINCT
department_id,
name
FROM department
) AS d
ON d.department_id = tmp2.department_id";
$wipwhole_array = mysqli_query($link, $sql);
foreach ($wipwhole_array as $wip) {
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'];
if (isset($arrayData[$wip['contractno']]['second']) && stristr($arrayData[$wip['contractno']]['second'][0], '出貨前30天')) {
$estimate_delivery_time = strtotime(strval($arrayData[$wip['contractno']]['second'][2]));
$secondtime = $estimate_delivery_time - (30 * 86400);
$secondtime = date('Ymd', $secondtime);
$arrayData[$wip['contractno']]['second'][2] = intval($secondtime);
} elseif (isset($arrayData[$wip['contractno']]['second']) && stristr($arrayData[$wip['contractno']]['second'][0], '出貨前90天')) {
$estimate_delivery_time = strtotime(strval($arrayData[$wip['contractno']]['second'][2]));
$secondtime = $estimate_delivery_time - (90 * 86400);
$secondtime = date('Ymd', $secondtime);
$arrayData[$wip['contractno']]['second'][2] = intval($secondtime);
}
$facitities = "";
$total_facility_num = 0;
$arrive_num = 0;
$tryrun_num = 0;
$delivery_num = 0;
$official_num = 0;
if ($wip['delivery_date'] != NULL) {
$facitities .= $wip['facilityno'] . " (" . $wip['delivery_date'] . "已移交) <br>";
$total_facility_num += 1;
$arrive_num += 1;
$tryrun_num += 1;
$official_num += 1;
$delivery_num += 1;
} elseif ($wip['official_check_date'] != NULL) {
$facitities .= $wip['facilityno'] . " (" . $wip['official_check_date'] . "官檢完畢) <br>";
$total_facility_num += 1;
$arrive_num += 1;
$tryrun_num += 1;
$official_num += 1;
} elseif ($wip['tryrun_end_date'] != NULL) {
$facitities .= $wip['facilityno'] . " (" . $wip['tryrun_end_date'] . "試車完畢) <br>";
$total_facility_num += 1;
$arrive_num += 1;
$tryrun_num += 1;
} elseif ($wip['install_end_date'] != NULL) {
$facitities .= $wip['facilityno'] . " (" . $wip['install_end_date'] . "安裝完畢) <br>";
$total_facility_num += 1;
$arrive_num += 1;
} elseif ($wip['real_arrival_date'] != NULL) {
$facitities .= $wip['facilityno'] . " (" . $wip['real_arrival_date'] . "貨抵工地) <br>";
$total_facility_num += 1;
$arrive_num += 1;
} elseif ($wip['actual_tofactory_date'] != NULL) {
$facitities .= $wip['facilityno'] . " (" . $wip['actual_tofactory_date'] . "實際到觀音日) <br>";
$total_facility_num += 1;
} elseif ($wip['estimated_shipping_date'] != NULL) {
$facitities .= $wip['facilityno'] . " (" . $wip['estimated_shipping_date'] . "預計到港) <br>";
$total_facility_num += 1;
} else {
$facitities .= $wip['facilityno'] . " (預計到港日待確認) <br>";
$total_facility_num += 1;
}
if ($total_facility_num > 0) {
$receivable_budget= $arrayData[$wip['contractno']]['sign'][1];
// $receivable_budget = $value[4] + ($value[5] + $value[6]) * ($arrive_num / $total_facility_num) +
// ($value[7] + $value[8]) * ($tryrun_num / $total_facility_num) + ($value[9]) * ($official_num / $total_facility_num)
// + ($value[10]) * ($delivery_num / $total_facility_num);
$uninvoice_budget = $receivable_budget;
$unreceived_budget = $receivable_budget;
$received_invoice = 0;
$received_budget = 0;
if (isset($received_array[$key][0])) {
$uninvoice_budget = $receivable_budget - $received_array[$key][0];
$received_invoice = $received_array[$key][0];
} else {
$uninvoice_budget = $receivable_budget;
}
if (isset($received_array[$key][1])) {
$unreceived_budget = $receivable_budget - $received_array[$key][1];
$received_budget = $received_array[$key][1];
} else {
$unreceived_budget = $receivable_budget;
}
if (isset($received_array[$key][2])) {
$status = $received_array[$key][2];
} else {
$status = 0;
}
}
}
}
exit();
foreach ($arrayData as $key => $value) {
echo $key . "<br>";
print_r($value);
echo "<br><br>";
}
//D23070086
//Array ( [0] => 312 [1] => 桃竹營銷部 [2] => 張莉凱 [3] => M0074 [4] => 章又仁 [5] => 永融營造(大園學八街) [6] => 永融營造工程有限公司 [7] => 13010305 [8] => 桃園市平鎮區環南路184號7樓之2 [9] => 0 [10] => 0 [11] => 0 [12] => 0 [sign] => Array ( [0] => 簽約 [1] => 210000.000000 [2] => 20230825 ) [arrive] => Array ( [0] => 貨抵工地 [1] => 525000.000000 [2] => 20231231 ) [tryrun] => Array ( [0] => 安裝試車 [1] => 210000.000000 [2] => 20240131 ) [delivery] => Array ( [0] => 交車 [1] => 105000.000000 [2] => 20240301 ) )
exit();
foreach ($arrayData as $key => $value) {
if (array_key_exists($key, $arrayData) && isset($arrayData[$key])) {
$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.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.contractno='" . $key . "'" . "
)AS tmp2
left join (
SELECT DISTINCT
department_id,
name
FROM department
) AS d
ON d.department_id = tmp2.department_id";
$facitities = "";
$total_facility_num = 0;
$arrive_num = 0;
$tryrun_num = 0;
$delivery_num = 0;
$official_num = 0;
$value[16] = '';
$value[17] = '';
$wipwhole_array = mysqli_query($link, $sql);
foreach ($wipwhole_array as $wip) {
$value[0] = $wip['depart_name'];
$value[1] = $wip['manager_name'];
$value[2] = $wip['name'];
$value[16] = is_null($wip['department_id']) ? '' : $wip['department_id'];
$value[17] = is_null($wip['salesid']) ? '' : $wip['salesid'];
if ($wip['delivery_date'] != NULL) {
$facitities .= $wip['facilityno'] . " (" . $wip['delivery_date'] . "已移交) <br>";
$total_facility_num += 1;
$arrive_num += 1;
$tryrun_num += 1;
$official_num += 1;
$delivery_num += 1;
} elseif ($wip['official_check_date'] != NULL) {
$facitities .= $wip['facilityno'] . " (" . $wip['official_check_date'] . "官檢完畢) <br>";
$total_facility_num += 1;
$arrive_num += 1;
$tryrun_num += 1;
$official_num += 1;
} elseif ($wip['tryrun_end_date'] != NULL) {
$facitities .= $wip['facilityno'] . " (" . $wip['tryrun_end_date'] . "試車完畢) <br>";
$total_facility_num += 1;
$arrive_num += 1;
$tryrun_num += 1;
} elseif ($wip['install_end_date'] != NULL) {
$facitities .= $wip['facilityno'] . " (" . $wip['install_end_date'] . "安裝完畢) <br>";
$total_facility_num += 1;
$arrive_num += 1;
} elseif ($wip['real_arrival_date'] != NULL) {
$facitities .= $wip['facilityno'] . " (" . $wip['real_arrival_date'] . "貨抵工地) <br>";
$total_facility_num += 1;
$arrive_num += 1;
} elseif ($wip['actual_tofactory_date'] != NULL) {
$facitities .= $wip['facilityno'] . " (" . $wip['actual_tofactory_date'] . "實際到觀音日) <br>";
$total_facility_num += 1;
} elseif ($wip['estimated_shipping_date'] != NULL) {
$facitities .= $wip['facilityno'] . " (" . $wip['estimated_shipping_date'] . "預計到港) <br>";
$total_facility_num += 1;
} else {
$facitities .= $wip['facilityno'] . " (預計到港日待確認) <br>";
$total_facility_num += 1;
}
}
$receivable_budget = 0;
//這是錢 4 簽約 5 二次款 6 貨抵工地 7 安裝 8 試車 9官檢 10 交車
//這是數量 arrive_num 貨抵工地 tryrun_num 試車安裝完畢 official_num 官檢完畢 delivery_num 移交
if ($total_facility_num > 0) {
$receivable_budget = $value[4] + ($value[5] + $value[6]) * ($arrive_num / $total_facility_num) +
($value[7] + $value[8]) * ($tryrun_num / $total_facility_num) + ($value[9]) * ($official_num / $total_facility_num)
+ ($value[10]) * ($delivery_num / $total_facility_num);
$uninvoice_budget = $receivable_budget;
$unreceived_budget = $receivable_budget;
$received_invoice = 0;
$received_budget = 0;
if (isset($received_array[$key][0])) {
$uninvoice_budget = $receivable_budget - $received_array[$key][0];
$received_invoice = $received_array[$key][0];
} else {
$uninvoice_budget = $receivable_budget;
}
if (isset($received_array[$key][1])) {
$unreceived_budget = $receivable_budget - $received_array[$key][1];
$received_budget = $received_array[$key][1];
} else {
$unreceived_budget = $receivable_budget;
}
if (isset($received_array[$key][2])) {
$status = $received_array[$key][2];
} else {
$status = 0;
}
}
if (isset($key)) {
$receivable_array[$key] = [
$key, $value[0], $value[1], $value[2], $value[3], $value[4], $value[5], $value[6], $value[7], $value[8], $value[9],
$value[10], $receivable_budget, $facitities, $value[13], $value[14], $value[15], $value[16], $value[17], $received_invoice, $received_budget,
$uninvoice_budget, $unreceived_budget, $status
];
}
}
}
$data = json_encode($receivable_array);
//0 合約號; 1 部門; 2 經理; 3 營業員; 4 客戶名稱; 5 簽約款; 6 二次款; 7 貨底工地款; 8 安裝款; 9 式車款; 10 官檢款;
//11 交車款; 12 目前應收; 13 作番狀態; 14 抬頭; 15 統一編號; 16 聯絡地址; 17 部門id; 18 營業員id; 19 已開發票金額; 20 已收金額;
//21 未開發票金額; 22 未收金額; 23 狀態
?>
<style>
table {
table-layout: fixed;
width: 100%;
}
td {
word-wrap: break-word;
}
img {
width: 125px;
}
.width_style_1 {
width: 125px;
}
table {
width: 100%;
}
#table_index_filter {
float: right;
}
#table_index_paginate {
float: right;
}
label {
display: inline-flex;
margin-bottom: .5rem;
margin-top: .5rem;
}
</style>
<div style="width:98%;margin:1%">
<table id="table_index" class="table table-striped table-bordered" style="width:100%;">
<thead>
<tr>
<th style="width: 120px;">合約號</th>
<th style="width: 120px;">部門</th>
<th style="width: 80px;">經理</th>
<th style="width: 80px;">營業員</th>
<th>客戶名稱</th>
<!-- <th style="width: 100px;">簽約款</th>
<th style="width: 100px;">二次款</th>
<th style="width: 100px;">貨抵工地款</th>
<th style="width: 100px;">安裝款</th>
<th style="width: 100px;">試車款</th>
<th style="width: 100px;">官檢款</th>
<th style="width: 100px;">交車款</th> -->
<th style="width: 100px;">目前應收</th>
<th style="width: 150px;">未開發票金額</th>
<th style="width: 100px;">未收金額</th>
<th>作番狀態</th>
<th style="width: 100px;">收款狀態</th>
<th style="width: 100px;">操作</th>
</tr>
</thead>
<tbody>
<?php
foreach ($receivable_array as $contract) {
?>
<tr>
<th><?= $contract[0] ?></th>
<td><?= $contract[1] ?></td>
<td><?= $contract[2] ?></td>
<td><?= $contract[3] ?></td>
<td><?= $contract[4] ?></td>
<!-- <td><?= round($contract[5]) ?></td>
<td><?= round($contract[6]) ?></td>
<td><?= round($contract[7]) ?></td>
<td><?= round($contract[8]) ?></td>
<td><?= round($contract[9]) ?></td>
<td><?= round($contract[10]) ?></td>
<td><?= round($contract[11]) ?></td> -->
<td style="text-align: end;"><?= number_format(round($contract[12])) ?></td>
<td style="text-align: end;"><?= number_format(round($contract[21])) ?></td>
<td style="text-align: end;"><?= number_format(round($contract[22])) ?></td>
<td style="text-align: start;"><?= $contract[13] ?></td>
<td style="text-align: start;"><?php
if (in_array($contract[23], array(0, 4, 5))) {
echo "正常";
} elseif ($contract[23] == 1) {
echo "<span style='color:blue'>催收</span>";
} else {
echo "<span style='color:red'>法務件</span>";
}
?></td>
<td><button type="button" onclick="sendData('<?= $contract[0] ?>')" class="btn btn-primary btn-sm"><span class=" glyphicon glyphicon-search"></span></button>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<script>
// function sendData(BillNo) {
// console.log(BillNo);
// var BillData = <?= $data ?>;
// var Bill = BillData[BillNo];
// var form = document.createElement("form");
// form.method = 'POST';
// form.action = "account-receivable-excel.php?<?= $token_link ?>";
// var input = document.createElement("input");
// input.type = "hidden";
// input.name = "Bill";
// input.value = JSON.stringify(Bill);
// form.appendChild(input);
// document.body.appendChild(form);
// form.submit();
// }
function sendData(BillNo) {
var BillData = <?= $data ?>;
var Bill = BillData;
var form = document.createElement("form");
form.method = 'POST';
form.action = "account-receivable-excel.php?<?= $token_link ?>";
var input = document.createElement("input");
input.type = "hidden";
input.name = "Bill";
input.value = JSON.stringify(Bill);
form.appendChild(input);
document.body.appendChild(form);
form.submit();
}
</script>