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.
 
 
 
 
 
 

133 lines
4.7 KiB

<?php
/**
* 產生安檢排程
* @url /schedule_check-import.php
* @method GET
* @return JSON
*
* respons json
* {
* "st" : "ok",
* "err" : ""
* }
**/
ob_start();
include "header.php";
ob_end_clean();
$rarr = array('st' => 'ok', 'err' => '');
try {
//require_once "database.php";
$data = [];
$tds = date("Y-m-d");
$tds_y = date("Y");
$base_date = date("Y-m-01", strtotime("-1 month")); // 今天算起前一個月起
$age_up = 15;
function get_facility_kind($fno="") {
switch ($fno) {
case (preg_match('/TX/', $fno) ? true : false):
case (preg_match('/MX/', $fno) ? true : false):
$kind = "MAE100";
break;
case (preg_match('/TW/', $fno) ? true : false):
$kind = "MAM200";
break;
case (preg_match('/TH/', $fno) ? true : false):
$kind = "MAH100";
break;
case (preg_match('/TZ/', $fno) ? true : false):
$kind = "MAZ100";
break;
default:
$kind = "";
}
return $kind;
}
function date_verify($date, $format = "Y-m-d") {
return date($format, strtotime($date)) == $date;
}
function get_check_date($ldate="", $month="") {
global $tds_y, $base_date;
$carr = [];
$lastday = substr($ldate, -2);
$cdate = date("Y-m-", strtotime(substr($ldate, 0, 7)." $month months")).$lastday;
if (!date_verify($cdate)) $cdate = date("Y-m-t", strtotime(substr($cdate, 0, 7)));
if ($cdate >= $base_date) $carr[] = $cdate;
$lastmd = substr($cdate, 4);
$lastyear = substr($cdate, 0, 4);
for ($i=$tds_y; $i<$lastyear; $i++) {
if ($i.$lastmd >= $base_date) $carr[] = $i.$lastmd;
}
return $carr;
}
// 許可證有效日在今天以後的
$db_query = "select f.contractno, f.facilityno, f.takecertificatedate, f.licensedate from facility f ";
$db_query .= "where f.licensedate >= '$tds'";
$res = mysqli_query($link, $db_query);
while ($row = mysqli_fetch_array($res)) {
$data[$row["contractno"]]["facilityno"][] = $row["facilityno"];
$data[$row["contractno"]]["cdate"] = $row["takecertificatedate"];
$data[$row["contractno"]]["ldate"] = $row["licensedate"];
$data[$row["contractno"]]["age"] = (strtotime($tds) - strtotime($row["takecertificatedate"]))/(86400*365); // 梯齡
}
mysqli_free_result($res);
//print_r($data);
foreach ($data as $contractno => $val) {
$check_date =[];
if ($val["age"] <= $age_up) {
$check_date = get_check_date($val["ldate"], "-2");
/*
$cdate = date("Y-m-", strtotime(substr($val["ldate"], 0, 7)." -2 months")).$lastday;
if (!date_verify($cdate)) $cdate = date("Y-m-t", strtotime(substr($cdate, 0, 7)));
$check_date[] = $cdate;
$lastmd = substr($cdate, 4);
$lastyear = substr($cdate, 0, 4) - 1;
for ($i=$lastyear; $i>=$tds_y; $i--) {
$check_date[] = $i.$lastmd;
}
*/
} else {
$check_date = array_merge(get_check_date($val["ldate"], "-7"), get_check_date($val["ldate"], "-1"));
/*
$cdate = date("Y-m-", strtotime(substr($val["ldate"], 0, 7)." -7 months")).$lastday;
if (!date_verify($cdate)) $cdate = date("Y-m-t", strtotime(substr($cdate, 0, 7)));
$check_date[] = $cdate;
$cdate = date("Y-m-", strtotime(substr($val["ldate"], 0, 7)." -1 months")).$lastday;
if (!date_verify($cdate)) $cdate = date("Y-m-t", strtotime(substr($cdate, 0, 7)));
$check_date[] = $cdate;
*/
}
sort($check_date);
foreach ($check_date as $cval) {
//if ($cval >= $base_date) {
foreach ($val["facilityno"] as $fno) {
$sql = "select count(*) from schedule_check where facilityno = '$fno' and check_date = '$cval'";
$res = mysqli_query($link, $sql);
$row = mysqli_fetch_row($res);
if ($row[0] == 0) {
$db_query = "insert into schedule_check (facilityno, check_date, creater, updater) values ";
$db_query .= "('$fno', '$cval', '$user_id', '$user_id')";
//echo $db_query."\n";
$result = mysqli_query($link, $db_query);
}
}
//}
}
}
}catch(\Exception $e) {
$rarr['st'] = 'err';
$rarr['err'] = $e->getMessage();
}
echo json_encode($rarr, JSON_UNESCAPED_UNICODE);
exit;
?>