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.
 
 
 
 
 
 

357 lines
11 KiB

<?php
require_once dirname(__FILE__) . "/../mkt/database.php";
include "fun_global.php";
require dirname(__DIR__) . '/common/composer/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Fill;
// AJAX 顯示合約明細
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$form_name = $_POST['form_name'];
$type = $_POST['type'];
$mtype = $_POST['mtype'];
$year = $_POST['year'];
$month = $_POST['month'];
if (in_array($type, ['M1', 'MA']))
$type = null;
if ($form_name == 'getShipping')
echo getShipping($type, $mtype, $year, $month);
if ($form_name == 'getInstalling')
echo getInstalling($type, $mtype, $year, $month);
if ($form_name == 'getInstalling2')
echo getInstalling2($type, $mtype, $year, $month);
if ($form_name == 'getQCing')
echo getQCing($type, $mtype, $year, $month);
if ($form_name == 'getDeliverying')
echo getDeliverying($type, $mtype, $year, $month);
if ($form_name == 'getShipping_makeExcel') {
$data = getShipping_makeExcel($type, $mtype, $year, $month);
downloadExcel($data);
}
if ($form_name == 'getInstalling_makeExcel') {
$data = getInstalling_makeExcel($type, $mtype, $year, $month);
downloadExcel($data);
}
if ($form_name == 'getInstalling2_makeExcel') {
$data = getInstalling2_makeExcel($type, $mtype, $year, $month);
downloadExcel($data);
}
if ($form_name == 'getQCing_makeExcel') {
$data = getQCing_makeExcel($type, $mtype, $year, $month);
downloadExcel($data);
}
if ($form_name == 'getDeliverying_makeExcel') {
$data = getDeliverying_makeExcel($type, $mtype, $year, $month);
downloadExcel($data);
}
}
function fixDataNull($data)
{
foreach ($data as $key => $row) {
foreach ($row as $field => $value) {
if ($value === null) {
$data[$key][$field] = '';
}
}
}
return $data;
}
// 出貨台數
function getShipping($type, $mtype, $year, $month)
{
global $link;
$sql = "
SELECT
*
FROM wipwholestatus
WHERE status = '1'
AND (
real_arrival_date IS NOT NULL
AND real_arrival_date != ''
)
";
if (!empty($month)) {
$sql .= " AND real_arrival_date BETWEEN '$year-$month-01' AND '$year-$month-31'";
} else {
$sql .= " AND real_arrival_date BETWEEN '$year-01-01' AND '$year-12-31'";
}
$sql .= !empty($type) ? " AND contract_type = '$type'" : "";
$sql .= !empty($mtype) ? " AND renovate_type Like '%$mtype%'" : "";
$result = mysqli_query($link, $sql);
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
return json_encode(fixDataNull($data), JSON_UNESCAPED_UNICODE);
}
// 在裝台數
function getInstalling($type, $mtype, $year, $month)
{
global $link;
$sql = "
SELECT
*
FROM wipwholestatus
WHERE status = '1'
AND (
tryrun_end_date = ''
OR tryrun_end_date IS NULL
)
";
if (!empty($month)) {
$sql .= " AND real_arrival_date BETWEEN '$year-$month-01' AND '$year-$month-31'";
} else {
$sql .= " AND real_arrival_date BETWEEN '$year-01-01' AND '$year-12-31'";
}
$sql .= !empty($type) ? " AND contract_type = '$type'" : "";
$sql .= !empty($mtype) ? " AND renovate_type Like '%$mtype%'" : "";
$result = mysqli_query($link, $sql);
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
return json_encode(fixDataNull($data), JSON_UNESCAPED_UNICODE);
}
// 完工台數
function getInstalling2($type, $mtype, $year, $month)
{
global $link;
$sql = "
SELECT
*
FROM wipwholestatus
WHERE status = '1'
";
if (!empty($month)) {
$sql .= " AND tryrun_end_date BETWEEN '$year-$month-01' AND '$year-$month-31'";
} else {
$sql .= " AND tryrun_end_date BETWEEN '$year-01-01' AND '$year-12-31'";
}
$sql .= !empty($type) ? " AND contract_type = '$type'" : "";
$sql .= !empty($mtype) ? " AND renovate_type Like '%$mtype%'" : "";
$result = mysqli_query($link, $sql);
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
return json_encode(fixDataNull($data), JSON_UNESCAPED_UNICODE);
}
// QC台數
function getQCing($type, $mtype, $year, $month)
{
global $link;
$sql = "
SELECT
*
FROM wipwholestatus
WHERE status = '1'
";
if (!empty($month)) {
$sql .= " AND end_qc_date BETWEEN '$year-$month-01' AND '$year-$month-31'";
} else {
$sql .= " AND end_qc_date BETWEEN '$year-01-01' AND '$year-12-31'";
}
$sql .= !empty($type) ? " AND contract_type = '$type'" : "";
$sql .= !empty($mtype) ? " AND renovate_type Like '%$mtype%'" : "";
$result = mysqli_query($link, $sql);
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
return json_encode(fixDataNull($data), JSON_UNESCAPED_UNICODE);
}
// 移交台數
function getDeliverying($type, $mtype, $year, $month)
{
global $link;
$sql = "
SELECT
*
FROM wipwholestatus
WHERE status = '1'
";
if (!empty($month)) {
$sql .= " AND delivery_date BETWEEN '$year-$month-01' AND '$year-$month-31'";
} else {
$sql .= " AND delivery_date BETWEEN '$year-01-01' AND '$year-12-31'";
}
$sql .= !empty($type) ? " AND contract_type = '$type'" : "";
$sql .= !empty($mtype) ? " AND renovate_type Like '%$mtype%'" : "";
$result = mysqli_query($link, $sql);
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
return json_encode(fixDataNull($data), JSON_UNESCAPED_UNICODE);
}
// 出貨台數 產生excel
function getShipping_makeExcel($type, $mtype, $year, $month)
{
global $link;
$sql = "
SELECT
*
FROM wipwholestatus
WHERE status = '1'
AND (
real_arrival_date IS NOT NULL
AND real_arrival_date != ''
)
";
if (!empty($month)) {
$sql .= " AND real_arrival_date BETWEEN '$year-$month-01' AND '$year-$month-31'";
} else {
$sql .= " AND real_arrival_date BETWEEN '$year-01-01' AND '$year-12-31'";
}
$sql .= !empty($type) ? " AND contract_type = '$type'" : "";
$sql .= !empty($mtype) ? " AND renovate_type Like '%$mtype%'" : "";
$result = mysqli_query($link, $sql);
return mysqli_fetch_all($result, MYSQLI_ASSOC);
}
// 在裝台數 產生excel
function getInstalling_makeExcel($type, $mtype, $year, $month)
{
global $link;
$sql = "
SELECT
*
FROM wipwholestatus
WHERE status = '1'
AND (
tryrun_end_date = ''
OR tryrun_end_date IS NULL
)
";
if (!empty($month)) {
$sql .= " AND real_arrival_date BETWEEN '$year-$month-01' AND '$year-$month-31'";
} else {
$sql .= " AND real_arrival_date BETWEEN '$year-01-01' AND '$year-12-31'";
}
$sql .= !empty($type) ? " AND contract_type = '$type'" : "";
$sql .= !empty($mtype) ? " AND renovate_type Like '%$mtype%'" : "";
$result = mysqli_query($link, $sql);
return mysqli_fetch_all($result, MYSQLI_ASSOC);
}
// 完工台數 產生excel
function getInstalling2_makeExcel($type, $mtype, $year, $month)
{
global $link;
$sql = "
SELECT
*
FROM wipwholestatus
WHERE status = '1'
";
if (!empty($month)) {
$sql .= " AND tryrun_end_date BETWEEN '$year-$month-01' AND '$year-$month-31'";
} else {
$sql .= " AND tryrun_end_date BETWEEN '$year-01-01' AND '$year-12-31'";
}
$sql .= !empty($type) ? " AND contract_type = '$type'" : "";
$sql .= !empty($mtype) ? " AND renovate_type Like '%$mtype%'" : "";
$result = mysqli_query($link, $sql);
return mysqli_fetch_all($result, MYSQLI_ASSOC);
}
// QC台數 產生excel
function getQCing_makeExcel($type, $mtype, $year, $month)
{
global $link;
$sql = "
SELECT
*
FROM wipwholestatus
WHERE status = '1'
";
if (!empty($month)) {
$sql .= " AND end_qc_date BETWEEN '$year-$month-01' AND '$year-$month-31'";
} else {
$sql .= " AND end_qc_date BETWEEN '$year-01-01' AND '$year-12-31'";
}
$sql .= !empty($type) ? " AND contract_type = '$type'" : "";
$sql .= !empty($mtype) ? " AND renovate_type Like '%$mtype%'" : "";
$result = mysqli_query($link, $sql);
return mysqli_fetch_all($result, MYSQLI_ASSOC);
}
// 移交台數 產生excel
function getDeliverying_makeExcel($type, $mtype, $year, $month)
{
global $link;
// 建立一個新的 Spreadsheet 物件
$sql = "
SELECT
*
FROM wipwholestatus
WHERE status = '1'
";
if (!empty($month)) {
$sql .= " AND delivery_date BETWEEN '$year-$month-01' AND '$year-$month-31'";
} else {
$sql .= " AND delivery_date BETWEEN '$year-01-01' AND '$year-12-31'";
}
$sql .= !empty($type) ? " AND contract_type = '$type'" : "";
$sql .= !empty($mtype) ? " AND renovate_type Like '%$mtype%'" : "";
$result = mysqli_query($link, $sql);
return mysqli_fetch_all($result, MYSQLI_ASSOC);
}
function downloadExcel($data)
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$colArr = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
];
$colTitleArr = [
'合約號',
'作番號',
'客戶名稱',
'預計出貨日',
'試車完工日',
'QC合格日',
'官檢日',
'移交日',
];
$sheet->setTitle('出貨完工推移表');
for ($i = 0; $i < count($colArr); $i++)
$sheet->setCellValue($colArr[$i] . '1', $colTitleArr[$i]);
$i = 2;
foreach ($data as $row) {
$colContentArr = [
$row['contractno'],
$row['facilityno'],
$row['custom'],
$row['real_arrival_date'],
$row['tryrun_end_date'],
$row['end_qc_date'],
$row['official_check_date'],
$row['delivery_date']
];
for ($j = 0; $j < count($colContentArr); $j++)
$sheet->setCellValue($colArr[$j] . $i, $colContentArr[$j]);
$i++;
}
// 調整欄位大小
foreach ($sheet->getColumnIterator() as $column) {
$sheet->getColumnDimension($column->getColumnIndex())->setAutoSize(true);
}
$writer = new Xlsx($spreadsheet);
$file_path = dirname(__DIR__) . '/wms/excel/' . 'gary_test.xlsx';
try {
$writer->save($file_path);
// 回傳檔案路徑給 JavaScript
echo $file_path;
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
exit();
}