= '$start_y-$start_m-01'
AND DATE_FORMAT(real_contract_arrival_date, '%Y-%m-%d') <= '$end_y-$end_m-31'
) AS this_order
WHERE 1 = 1
GROUP BY this_order.contract_type,
this_order.sub_address,
this_order.date_year,
this_order.date_month
";
$result = mysqli_query($link, $sql);
$data = [];
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
function findData($data, $searchData)
{
foreach ($data as $item) {
if (
$item['contract_type'] == $searchData['contract_type']
&& $item['sub_address'] == $searchData['sub_address']
&& $item['date_year'] == $searchData['date_year']
&& $item['date_month'] == $searchData['date_month']
) {
return $item['facility_count'];
}
}
return 0;
}
function findDataByCity($data, $searchData)
{
$count = 0;
$start_y = $searchData['start_y'];
$start_m = $searchData['start_m'];
$end_y = $searchData['end_y'];
$end_m = $searchData['end_m'];
for ($i = $start_y; $i <= $end_y; $i++) {
$this_start_m = $i == $start_y ? $start_m : 1;
$this_end_m = $i == $start_y ? 12 : $end_m;
for ($j = $this_start_m; $j <= $this_end_m; $j++) {
foreach ($data as $item) {
if (
$item['contract_type'] == $searchData['contract_type']
&& $item['sub_address'] == $searchData['sub_address']
&& $item['date_year'] == $i
&& $item['date_month'] == str_pad($j, 2, '0', STR_PAD_LEFT)
) {
$count += $item['facility_count'];
}
}
}
}
return $count;
}
function findDataByYearMonth($data, $searchData)
{
$count = 0;
foreach ($data as $item) {
if (
$item['contract_type'] == $searchData['contract_type']
&& $item['date_year'] == $searchData['date_year']
&& $item['date_month'] == $searchData['date_month']
) {
$count += $item['facility_count'];
}
}
return $count;
}
function findAllDataByYearMonth($data, $searchData)
{
$count = 0;
$start_y = $searchData['start_y'];
$start_m = $searchData['start_m'];
$end_y = $searchData['end_y'];
$end_m = $searchData['end_m'];
for ($i = $start_y; $i <= $end_y; $i++) {
$this_start_m = $i == $start_y ? $start_m : 1;
$this_end_m = $i == $start_y ? 12 : $end_m;
for ($j = $this_start_m; $j <= $this_end_m; $j++) {
foreach ($data as $item) {
if (
$item['contract_type'] == $searchData['contract_type']
&& $item['date_year'] == $i
&& $item['date_month'] == str_pad($j, 2, '0', STR_PAD_LEFT)
) {
$count += $item['facility_count'];
}
}
}
}
return $count;
}
$sql = "
SELECT
new_order.facility_kind,
COUNT(new_order.facility_kind) AS facility_count
FROM(
SELECT
w.facility_kind
FROM wipwholestatus AS w
WHERE 1=1
AND status = '1'
AND contract_type = 'A'
AND real_contract_arrival_date >= '$start_y-$start_m-1'
AND real_contract_arrival_date <= '$end_y-$end_m-31'
) AS new_order
WHERE 1 = 1
GROUP BY new_order.facility_kind
";
$result = mysqli_query($link, $sql);
$data2 = [];
while ($row = $result->fetch_assoc()) {
$data2[] = $row;
}
$sql = "
SELECT
new_order.facility_kind,
COUNT(new_order.facility_kind) AS facility_count
FROM(
SELECT
w.facility_kind
FROM wipwholestatus AS w
WHERE 1=1
AND status = '1'
AND contract_type = 'A'
) AS new_order
WHERE 1 = 1
GROUP BY new_order.facility_kind
";
$result = mysqli_query($link, $sql);
$data4 = [];
while ($row = $result->fetch_assoc()) {
$data4[] = $row;
}
function findFacilityTypeAllCount($data2)
{
$count = 0;
foreach ($data2 as $row) {
$count += $row['facility_count'];
}
return $count;
}
function getFacilityNamebyType($facility_kind)
{
$arr = array(
'MAE100' => '小機房',
'MAM200' => '無機房',
'MAH100' => '小電梯',
'MAQ100' => '強驅梯',
'MAF100' => '貨梯',
'MAP100' => '平台梯',
'' => '未分類'
);
return isset($arr[$facility_kind]) ? $arr[$facility_kind] : "";
}
$sql = "
SELECT
w.renovate_type
FROM wipwholestatus AS w
WHERE 1=1
AND status = '1'
AND real_contract_arrival_date >= '$start_y-$start_m-1'
AND real_contract_arrival_date <= '$end_y-$end_m-31'
AND contract_type = 'B'
";
$result = mysqli_query($link, $sql);
$data3 = [];
while ($row = $result->fetch_assoc()) {
$data3[] = $row;
}
$sql = "
SELECT
w.renovate_type
FROM wipwholestatus AS w
WHERE 1=1
AND status = '1'
AND contract_type = 'B'
";
$result = mysqli_query($link, $sql);
$data5 = [];
while ($row = $result->fetch_assoc()) {
$data5[] = $row;
}
// 分開計算
// $M1 = 0;
// $M2 = 0;
// $M3 = 0;
// $M4 = 0;
// $MA = 0;
// $M_count = 0;
// foreach ($data3 as $row) {
// $renovate_type_arr = json_decode($row['renovate_type']);
// foreach ($renovate_type_arr as $row2) {
// if ($row2 == 'M1')
// $M1++;
// if ($row2 == 'M2')
// $M2++;
// if ($row2 == 'M3')
// $M3++;
// if ($row2 == 'M4')
// $M4++;
// if ($row2 == 'MA')
// $MA++;
// }
// }
// $M_count = $M1 + $M2 + $M3 + $M4 + $MA;
$M_count = count($data3);
$m_type_arr = [];
$m_type_arr[''] = 0;
foreach ($data3 as $row) {
$m_type_arr[implode("+", json_decode($row['renovate_type']))] = 0;
}
foreach ($data3 as $row) {
$m_type_arr[implode("+", json_decode($row['renovate_type']))]++;
}
$M_count2 = count($data5);
$m_type_arr2 = [];
$m_type_arr2[''] = 0;
foreach ($data5 as $row) {
$m_type_arr2[implode("+", json_decode($row['renovate_type']))] = 0;
}
foreach ($data5 as $row) {
$m_type_arr2[implode("+", json_decode($row['renovate_type']))]++;
}
//數字轉英文(0=>A、1=>B、26=>AA...以此類推)
function num2alpha($n)
{
for ($r = ""; $n >= 0; $n = intval($n / 26) - 1)
$r = chr($n % 26 + 0x41) . $r;
return $r;
}
//英文轉數字(A=>0、B=>1、AA=>26...以此類推)
function alpha2num($a)
{
$l = strlen($a);
$n = 0;
for ($i = 0; $i < $l; $i++)
$n = $n * 26 + ord($a[$i]) - 0x40;
return $n - 1;
}
if ($_SERVER["REQUEST_METHOD"] == 'POST') {
$excel_output = !empty($_POST['excel_output']) ? $_POST['excel_output'] : '';
if ($excel_output == 'excel_output') {
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('出貨總表');
$title_col_num = 3;
for ($i = $start_y; $i <= $end_y; $i++) {
$this_start_m = $i == $start_y ? $start_m : 1;
$this_end_m = $i == $start_y ? 12 : $end_m;
for ($j = $this_start_m; $j <= $this_end_m; $j++) {
$title_col_num += 2;
if ($i == $end_y && $j == ($end_m + 1)) {
break;
}
}
}
$sheet->mergeCells('A1:' . num2alpha($title_col_num) . '1');
$sheet->setCellValue('A1', '出貨地區預定明細');
$title_col_num = 0;
$sheet->setCellValue(num2alpha($title_col_num) . '2', '');
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '2', '');
$title_col_num++;
for ($i = $start_y; $i <= $end_y; $i++) {
$this_start_m = $i == $start_y ? $start_m : 1;
$this_end_m = $i == $start_y ? 12 : $end_m;
for ($j = $this_start_m; $j <= $this_end_m; $j++) {
if ($i == $end_y && $j == ($end_m + 1))
break;
$sheet->mergeCells(num2alpha($title_col_num) . '2:' . num2alpha($title_col_num + 1) . '2');
$sheet->setCellValue(num2alpha($title_col_num) . '2', $i . '年' . $j . '月');
$title_col_num += 2;
}
}
$sheet->setCellValue(num2alpha($title_col_num) . '2', '累計台數');
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '2', '累計台數');
$title_col_num++;
$title_col_num = 0;
$sheet->setCellValue(num2alpha($title_col_num) . '3', '區域');
$title_col_num = 2;
for ($i = $start_y; $i <= $end_y; $i++) {
$this_start_m = $i == $start_y ? $start_m : 1;
$this_end_m = $i == $start_y ? 12 : $end_m;
for ($j = $this_start_m; $j <= $this_end_m; $j++) {
if ($i == $end_y && $j == ($end_m + 1)) {
break;
}
$sheet->setCellValue(num2alpha($title_col_num) . '3', '新梯');
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '3', '汰改');
$title_col_num++;
}
}
$sheet->setCellValue(num2alpha($title_col_num) . '3', '新梯');
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '3', '汰改');
$title_col_num++;
$area_arr = [
[4, 6, '台北'],
[7, 9, '桃竹苗'],
[10, 13, '中部'],
[14, 17, '南部'],
[18, 20, '宜花東'],
[21, 21, '未分類']
];
foreach ($area_arr as $row) {
$title_col_num = 0;
$sheet->mergeCells(num2alpha($title_col_num) . $row[0] . ':' . num2alpha($title_col_num) . $row[1]);
$sheet->setCellValue(num2alpha($title_col_num) . $row[0], $row[2]);
}
$area_arr = [
[4, '台北'],
[5, '新北'],
[6, '基隆'],
[7, '桃園'],
[8, '新竹'],
[9, '苗栗'],
[10, '台中'],
[11, '南投'],
[12, '彰化'],
[13, '雲林'],
[14, '嘉義'],
[15, '台南'],
[16, '高雄'],
[17, '屏東'],
[18, '宜蘭'],
[19, '花蓮'],
[20, '台東'],
[21, '']
];
foreach ($area_arr as $row) {
$title_col_num = 0;
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . $row[0], $row[1]);
for ($i = $start_y; $i <= $end_y; $i++) {
$this_start_m = $i == $start_y ? $start_m : 1;
$this_end_m = $i == $start_y ? 12 : $end_m;
for ($j = $this_start_m; $j <= $this_end_m; $j++) {
if ($i == $end_y && $j == ($end_m + 1)) {
break;
}
$tmp_arr = array(
'date_year' => $i,
'date_month' => str_pad($j, 2, '0', STR_PAD_LEFT),
'sub_address' => $row[1],
'contract_type' => 'A',
);
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . $row[0], findData($data, $tmp_arr));
$tmp_arr['contract_type'] = 'B';
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . $row[0], findData($data, $tmp_arr));
}
}
$tmp_arr = array(
'start_y' => $start_y,
'end_y' => $end_y,
'start_m' => $start_m,
'end_m' => $end_m,
'sub_address' => $row[1],
'contract_type' => 'A',
);
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . $row[0], findDataByCity($data, $tmp_arr));
$tmp_arr['contract_type'] = 'B';
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . $row[0], findDataByCity($data, $tmp_arr));
}
$title_col_num = 0;
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '22', "合計");
for ($i = $start_y; $i <= $end_y; $i++) {
$this_start_m = $i == $start_y ? $start_m : 1;
$this_end_m = $i == $start_y ? 12 : $end_m;
for ($j = $this_start_m; $j <= $this_end_m; $j++) {
if ($i == $end_y && $j == ($end_m + 1))
break;
$tmp_arr = array(
'date_year' => $i,
'date_month' => str_pad($j, 2, '0', STR_PAD_LEFT),
'contract_type' => 'A',
);
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '22', findDataByYearMonth($data, $tmp_arr));
$tmp_arr['contract_type'] = 'B';
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '22', findDataByYearMonth($data, $tmp_arr));
}
}
$tmp_arr = array(
'start_y' => $start_y,
'end_y' => $end_y,
'start_m' => $start_m,
'end_m' => $end_m,
'contract_type' => 'A',
);
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '22', findAllDataByYearMonth($data, $tmp_arr));
$tmp_arr['contract_type'] = 'B';
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '22', findAllDataByYearMonth($data, $tmp_arr));
$sheet->mergeCells('A25:I25');
$sheet->setCellValue('A25', '受定規格分析');
$title_col_num = 0;
$sheet->setCellValue(num2alpha($title_col_num) . '26', "代碼");
foreach ($data2 as $row) {
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '26', $row['facility_kind']);
}
$title_col_num = 0;
$sheet->setCellValue(num2alpha($title_col_num) . '27', "機種");
foreach ($data2 as $row) {
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '27', getFacilityNamebyType($row['facility_kind']));
}
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '27', "合計");
$title_col_num = 0;
$sheet->setCellValue(num2alpha($title_col_num) . '28', "數量");
foreach ($data2 as $row) {
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '28', $row['facility_count']);
}
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '28', findFacilityTypeAllCount($data2));
$title_col_num = 0;
$sheet->setCellValue(num2alpha($title_col_num) . '29', "百分比");
foreach ($data2 as $row) {
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '29', (round($row['facility_count'] / findFacilityTypeAllCount($data2), 4) * 100) . "%");
}
$title_col_num++;
$sheet->setCellValue(num2alpha($title_col_num) . '29', "100%");
$sheet->mergeCells('A32:G32');
$sheet->setCellValue('G25', '舊改受訂規格分析');
$title_col_num = 0;
$sheet->setCellValue(num2alpha($title_col_num++) . '33', "類型");
foreach ($m_type_arr as $key => $val) {
$title_tmp = empty($key) ? '未分類' : $key;
$sheet->setCellValue(num2alpha($title_col_num++) . '33', $title_tmp);
}
$sheet->setCellValue(num2alpha($title_col_num++) . '33', "合計");
$title_col_num = 0;
$sheet->setCellValue(num2alpha($title_col_num++) . '34', "數量");
foreach ($m_type_arr as $key => $val) {
$sheet->setCellValue(num2alpha($title_col_num++) . '34', $val);
}
$sheet->setCellValue(num2alpha($title_col_num++) . '34', $M_count);
$title_col_num = 0;
$sheet->setCellValue(num2alpha($title_col_num++) . '35', "百分比");
foreach ($m_type_arr as $key => $val) {
$sheet->setCellValue(num2alpha($title_col_num++) . '35', (round($val / $M_count, 4) * 100) . "%");
}
$sheet->setCellValue(num2alpha($title_col_num++) . '35', "100%");
$writer = new Xlsx($spreadsheet);
$file_path = dirname(__DIR__) . '/wms/excel/' . 'estimated_shipping_date_report.xlsx';
try {
$writer->save($file_path);
// 回傳檔案路徑給 JavaScript
echo $file_path;
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
exit();
}
}
?>
出貨地區預定明細
受定規格分析(時間區間) |
代碼 |
" . $row['facility_kind'] . "";
}
?>
|
機種 |
" . getFacilityNamebyType($row['facility_kind']) . "";
}
?>
合計 |
數量 |
" . $row['facility_count'] . "";
}
?>
|
百分比 |
" . (round($row['facility_count'] / findFacilityTypeAllCount($data2), 4) * 100) . "%" . "";
}
?>
|
舊改受訂規格分析(時間區間) |
類型 |
$val) {
$title_tmp = empty($key) ? '未分類' : $key;
echo "" . $title_tmp . " | ";
}
?>
合計 |
數量 |
$val) {
echo "" . $val . " | ";
}
?>
|
百分比 |
$val) {
echo "" . round($val / $M_count, 4) * 100 . "%" . " | ";
}
?>
100% |
受定規格分析(全部) |
代碼 |
" . $row['facility_kind'] . "";
}
?>
|
機種 |
" . getFacilityNamebyType($row['facility_kind']) . "";
}
?>
合計 |
數量 |
" . $row['facility_count'] . "";
}
?>
|
百分比 |
" . (round($row['facility_count'] / findFacilityTypeAllCount($data4), 4) * 100) . "%" . "";
}
?>
|
舊改受訂規格分析(全部) |
類型 |
$val) {
$title_tmp = empty($key) ? '未分類' : $key;
echo "" . $title_tmp . " | ";
}
?>
合計 |
數量 |
$val) {
echo "" . $val . " | ";
}
?>
|
百分比 |
$val) {
echo "" . round($val / $M_count2, 4) * 100 . "%" . " | ";
}
?>
100% |