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.
 
 
 
 
 
 

516 lines
19 KiB

<?php
// ini_set('display_errors', 'on');
// 汰改 前三碼 流水號 + 後兩碼 號機
// 新梯 流水號
// 新增 seq 的初始值
// INSERT INTO `sequence` (`seq_name`, `current_val`, `increment_val`, `remark`, `yyyymm`, `prefix`, `creator`, `create_at`) VALUES ('bf_vol_no', '0', '1', '保養-作番編號', '202310', '', 'M0117', '2023-10-26 11:28:11'), ('mf_vol_no', '0', '1', '新梯-作番編號', '202310', '', 'M0117', '2023-10-26 11:28:11'), ('tf_vol_no', '0', '1', '汰改-作番編號', '202310', '', 'M0117', '2023-10-26 11:28:11')
class CreateFacilityNo
{
/**
* 連接資料庫
*/
function connectionDB()
{
$envFile = __DIR__ . '/../../../.env';
if (file_exists($envFile)) {
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines !== false) {
foreach ($lines as $line) {
list($key, $value) = explode('=', $line, 2);
$key = trim($key);
$value = trim($value);
putenv("$key=$value");
}
}
}
date_default_timezone_set("Asia/Taipei");
$host = getenv('DB_HOST');
$dbport = getenv('DB_PORT');
$dbuser = getenv('DB_USERNAME');
$dbpassword = getenv('DB_PASSWORD');
$dbname = getenv('DB_DATABASE');
try {
$options = [
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
];
$pdo = new PDO('mysql:host=' . $host . ';port=' . $dbport . ';dbname=' . $dbname . '', $dbuser, $dbpassword, $options);
$pdo->exec('SET CHARACTER SET utf8mb4');
return $pdo;
} catch (PDOException $e) {
die("Something wrong: {$e->getMessage()}");
}
}
/**
* 結束資料庫連線
*/
function endConnectionDB($pdo)
{
unset($pdo);
}
/**
* 取得下一個新的作番
* @param string $facility_type : M:新梯 T:汰改 B:保養
* @param array $sale_type : M:内銷 E:外銷 T:他社维保 J:汰改 X:特殊部品
* @param array $make_type : X:小機房 W:無機房 H:家用梯 Z:雜物梯 F:扶梯 B:部品 Q:品保對策 T:研究開發 N:設備 W:出貨現場要求購買 J:營業問題對策 Y:已出貨作番營業進行規格訂正
* @param int $num : 號機
* @return array $new_facility_arr
*/
function getNextFacilityNo($facility_type, $sale_type, $make_type, $num = 1)
{
$this->checkYearAndResetAllSeq();
$Y = substr(date("Y"), 3, 1);
switch ($facility_type) {
case "M":
$next_seq = $this->getNextFacilitySeq("mf_vol_no") + 1;
$new_facility_arr = [];
for ($start_num = 1; $start_num <= $num; $start_num++) {
$facility_no_tmp = $Y . $sale_type . $make_type
. str_pad($next_seq, 5, "0", STR_PAD_LEFT);
array_push($new_facility_arr, $facility_no_tmp);
$next_seq++;
}
return $new_facility_arr;
case "T":
$next_seq = $this->getNextFacilitySeq("tf_vol_no");
$new_facility_arr = [];
for ($start_num = 1; $start_num <= $num; $start_num++) {
$facility_no_tmp = $Y . $sale_type . $make_type
. str_pad($next_seq + 1, 3, "0", STR_PAD_LEFT)
. str_pad($start_num, 2, "0", STR_PAD_LEFT);
array_push($new_facility_arr, $facility_no_tmp);
}
return $new_facility_arr;
case "B":
$next_seq = $this->getNextFacilitySeq("bf_vol_no");
$new_facility_arr = [];
for ($start_num = 1; $start_num <= $num; $start_num++) {
$facility_no_tmp = $Y . $sale_type[$start_num - 1] . $make_type[$start_num - 1]
. str_pad($next_seq + 1, 3, "0", STR_PAD_LEFT)
. str_pad($start_num, 2, "0", STR_PAD_LEFT);
array_push($new_facility_arr, $facility_no_tmp);
}
return $new_facility_arr;
default:
return "不存在的作番類型";
}
}
/**
* 建立新的作番
* @param string $facility_type : M:新梯 T:汰改 B:保養
* @param array $sale_type : M:内銷 E:外銷 T:他社维保 J:汰改 X:特殊部品
* @param array $make_type : X:小機房 W:無機房 H:家用梯 Z:雜物梯 F:扶梯 B:部品 Q:品保對策 T:研究開發 N:設備 W:出貨現場要求購買 J:營業問題對策 Y:已出貨作番營業進行規格訂正
* @param string $num : 號機 (非必填)
* @return string $new_facility_no
*/
function makeFacilityNo($facility_type, $sale_type, $make_type, $num = null)
{
$this->checkYearAndResetAllSeq();
$faclikity_details = array(
'facility_type' => $facility_type,
'sale_type' => $sale_type,
'make_type' => $make_type,
'num' => $num
);
switch ($facility_type) {
case "M":
return $this->makeNewMFacilityNo($faclikity_details);
break;
case "T":
return $this->makeNewTFacilityNo($faclikity_details);
break;
case "B":
return $this->makeNewBFacilityNo($faclikity_details);
break;
default:
return "不存在的作番類型";
}
}
/**
* 建立作番 -- 新梯
*/
function makeNewMFacilityNo($faclikity_details)
{
$Y = substr(date("Y"), 3, 1);
$sale_type = $faclikity_details['sale_type'];
$make_type = $faclikity_details['make_type'];
$num = $faclikity_details['num'];
$next_seq = $this->getNextFacilitySeq("mf_vol_no") + 1;
$new_facility_no_arr = [];
for ($i = 1; $i <= $num; $i++) {
$new_facility_no = $Y . $sale_type . $make_type
. str_pad($next_seq, 5, "0", STR_PAD_LEFT);
array_push($new_facility_no_arr, $new_facility_no);
$next_seq++;
}
foreach ($new_facility_no_arr as $new_facility_no) {
if ($this->getMakeNewMFacilityNoStatus($faclikity_details) !== "1") {
return $new_facility_no . ":" . $this->getMakeNewTFacilityNoStatus($faclikity_details);
}
// seq +1
$this->facilitySeqAddOne("M");
}
return $new_facility_no_arr;
}
/**
* 建立作番 -- 汰改
*/
function makeNewTFacilityNo($faclikity_details)
{
$Y = substr(date("Y"), 3, 1);
$sale_type = $faclikity_details['sale_type'];
$make_type = $faclikity_details['make_type'];
$num = $faclikity_details['num'];
$next_seq = $this->getNextFacilitySeq("tf_vol_no");
$new_facility_no_arr = [];
for ($i = 1; $i <= $num; $i++) {
$new_facility_no = $Y . $sale_type . $make_type
. str_pad($next_seq + 1, 3, "0", STR_PAD_LEFT)
. str_pad($i, 2, "0", STR_PAD_LEFT);
array_push($new_facility_no_arr, $new_facility_no);
}
foreach ($new_facility_no_arr as $new_facility_no) {
if ($this->getMakeNewTFacilityNoStatus($faclikity_details) !== "1") {
return $new_facility_no . ":" . $this->getMakeNewTFacilityNoStatus($faclikity_details);
}
// seq +1
$this->facilitySeqAddOne("T");
return $new_facility_no_arr;
}
}
/**
* 建立作番 -- 保養
*/
function makeNewBFacilityNo($faclikity_details)
{
$Y = substr(date("Y"), 3, 1);
$sale_type = $faclikity_details['sale_type'];
$make_type = $faclikity_details['make_type'];
$num = $faclikity_details['num'];
$next_seq = $this->getNextFacilitySeq("bf_vol_no");
$new_facility_no_arr = [];
for ($i = 1; $i <= $num; $i++) {
$new_facility_no = $Y . $sale_type[$i - 1] . $make_type[$i - 1]
. str_pad($next_seq + 1, 3, "0", STR_PAD_LEFT)
. str_pad($i, 2, "0", STR_PAD_LEFT);
array_push($new_facility_no_arr, $new_facility_no);
}
foreach ($new_facility_no_arr as $new_facility_no) {
if ($this->getMakeNewBFacilityNoStatus($faclikity_details) !== "1") {
return $new_facility_no . ":" . $this->getMakeNewBFacilityNoStatus($faclikity_details);
}
// seq +1
$this->facilitySeqAddOne("B");
return $new_facility_no_arr;
}
}
/**
* 檢查作番編列狀態 - 新梯
* @param array $faclikity_details
* @return string $status : 1:正確 else:error message
*/
function getMakeNewMFacilityNoStatus($faclikity_details)
{
$sale_type = $faclikity_details['sale_type'];
$make_type = $faclikity_details['make_type'];
if ($this->checkSaleTypeStatus($sale_type) == false)
return "銷售代號錯誤";
if ($this->checkMakeTypeStatus($make_type) == false)
return "製造編號類型錯誤";
return "1";
}
/**
* 檢查作番編列狀態 - 汰改
* @param array $faclikity_details
* @return string $status : 1:正確 else:error message
*/
function getMakeNewTFacilityNoStatus($faclikity_details)
{
$sale_type = $faclikity_details['sale_type'];
$make_type = $faclikity_details['make_type'];
if ($this->checkSaleTypeStatus($sale_type) == false)
return "銷售代號錯誤";
if ($this->checkMakeTypeStatus($make_type) == false)
return "製造編號類型錯誤";
return "1";
}
/**
* 檢查作番編列狀態 - 保養
* @param array $faclikity_details
* @return string $status : 1:正確 else:error message
*/
function getMakeNewBFacilityNoStatus($faclikity_details)
{
$sale_type = $faclikity_details['sale_type'];
$make_type = $faclikity_details['make_type'];
if ($this->checkSaleTypeStatus($sale_type) == false)
return "銷售代號錯誤";
if ($this->checkMakeTypeStatus($make_type) == false)
return "製造編號類型錯誤";
return "1";
}
/**
* seq 取號
* @param string $type M:新梯 T:汰改 B:保養
*/
function facilitySeqAddOne($type)
{
$type_arr = array(
"M" => "mf_vol_no",
"T" => "tf_vol_no",
"B" => "bf_vol_no",
);
if (!empty($type_arr[$type])) {
$pdo = $this->connectionDB();
$pdo->exec('SET CHARACTER SET utf8mb4');
$sth = $pdo->prepare('UPDATE sequence SET current_val = current_val + 1 WHERE `seq_name` = ?');
$sth->bindValue(1, $type_arr[$type]);
$sth->execute();
}
}
/**
* 修正 seq 取號
* @param string $type M:新梯 T:汰改 B:保養
*/
function facilityFixSeq($type)
{
$pdo = $this->connectionDB();
$pdo->exec('SET CHARACTER SET utf8mb4');
$type_arr = array(
"M" => "mf_vol_no",
"T" => "tf_vol_no",
"B" => "bf_vol_no",
);
$after_fix_seq = $this->getMaxSeq($type);
$sql = "
UPDATE sequence
SET current_val = ?
WHERE seq_name = ?
";
$sth = $pdo->prepare($sql);
$sth->bindValue(1, $after_fix_seq);
$sth->bindValue(2, $type_arr[$type]);
$sth->execute();
}
function getMaxSeq($type)
{
$pdo = $this->connectionDB();
$pdo->exec('SET CHARACTER SET utf8mb4');
$Y = substr(date("Y"), 3, 1);
$seq_num = $type == 'M' ? 5 : 3;
$sql = "
SELECT MAX(SUBSTR(f.facilityno,4,?))+1 AS seq
FROM facility AS f
WHERE 1=1
AND SUBSTR(f.facilityno,1,1) = ?
AND f.define = ?
ORDER BY SUBSTR(f.facilityno,4,3) ASC
";
$sth = $pdo->prepare($sql);
$sth->bindValue(1, $seq_num);
$sth->bindValue(2, $Y);
$sth->bindValue(3, $type);
$sth->execute();
$result = $sth->fetch();
return $result['seq'];
}
/**
* 檢查年月後 新梯及汰改seq歸零
*/
function checkYearAndResetAllSeq()
{
$pdo = $this->connectionDB();
$pdo->exec('SET CHARACTER SET utf8mb4');
$sth = $pdo->prepare('SELECT * FROM `sequence` WHERE `seq_name` = ?');
$sth->bindValue(1, 'mf_vol_no');
$sth->execute();
$result = $sth->fetch();
$yyyymm = $result['yyyymm'];
$dataY = substr($yyyymm, 0, 4);
$today_Y = date("Y");
$today_Ym = date("Ym");
if ($dataY != $today_Y) {
$sth = $pdo->prepare('UPDATE `sequence` SET `current_val` = ? , `yyyymm` = ? WHERE `seq_name` = ?');
$sth->bindValue(1, '0');
$sth->bindValue(2, $today_Ym);
$sth->bindValue(3, 'mf_vol_no');
$sth->execute();
}
}
/**
* 檢查作番在 facility table 中是否重複
* @param string|array $facility_no
* @return boolean $status : true:沒重複 false:重複
*/
function checkFacilityRepeatStatus($facility_no)
{
if (gettype($facility_no) == "string") {
$pdo = $this->connectionDB();
$pdo->exec('SET CHARACTER SET utf8mb4');
$sth = $pdo->prepare('SELECT * FROM `facility` WHERE `facilityno` = ?');
$sth->bindValue(1, $facility_no);
$sth->execute();
if ($sth->rowCount() == 0)
return true;
return false;
}
if (gettype($facility_no) == "array") {
$pdo = $this->connectionDB();
$pdo->exec('SET CHARACTER SET utf8mb4');
$status = true;
foreach ($facility_no as $row) {
$sth = $pdo->prepare('SELECT * FROM `facility` WHERE `facilityno` = ?');
$sth->bindValue(1, $row);
$sth->execute();
if ($sth->rowCount() !== 0)
$status = false;
}
return $status;
}
}
/**
* 檢查 $sale_type 是否有存在規則之中
* @param array $sale_type : M:内銷 E:外銷 T:他社维保 J:汰改 X:特殊部品
* @return boolean $status : true:合法代碼 false:非法代碼
*/
function checkSaleTypeStatus($sale_type)
{
foreach ($sale_type as $row)
if (!in_array($row, ['M', 'E', 'T', 'J', 'X']))
return false;
return true;
}
/**
* 檢查 $sale_type 是否有存在規則之中
* @param array $make_type : X:小機房 W:無機房 H:家用梯 Z:雜物梯 F:扶梯 B:部品 Q:品保對策 T:研究開發 N:設備 W:出貨現場要求購買 J:營業問題對策 Y:已出貨作番營業進行規格訂正
* @return boolean $status : true:合法代碼 false:非法代碼
*/
function checkMakeTypeStatus($make_type)
{
foreach ($make_type as $row)
if (!in_array($row, ['X', 'W', 'H', 'Z', 'F', 'B', 'Q', 'T', 'N', 'W', 'J', 'Y']))
return false;
return true;
}
/**
* 檢查 取得下個作番的 seq
* @param string $seq_name : 新梯:mf_vol_no 汰改:tf_vol_no 保養:bf_vol_no
* @return int $seq : 作番流水號
*/
function getNextFacilitySeq($seq_name)
{
$pdo = $this->connectionDB();
$pdo->exec('SET CHARACTER SET utf8mb4');
$sth = $pdo->prepare('SELECT * FROM `sequence` WHERE `seq_name` = ?');
$sth->bindValue(1, $seq_name);
$sth->execute();
$result = $sth->fetch();
return $result['current_val'];
}
/**
* 建立新的新梯作番
* @param string $sale_type : M:内銷 E:外銷 T:他社维保 J:汰改 X:特殊部品
* @param string $make_type : X:小機房 W:無機房 H:家用梯 Z:雜物梯 F:扶梯 B:部品 Q:品保對策 T:研究開發 N:設備 W:出貨現場要求購買 J:營業問題對策 Y:已出貨作番營業進行規格訂正
* @param int $seq_name : 幾個案場 0-99
* @return array $new_facilityno : 作番號
*/
function makeMFacilityNo($sale_type, $make_type, $num)
{
$new_facility_no = $this->getNextFacilityNo("M", $sale_type, $make_type, $num);
if ($this->checkFacilityRepeatStatus($new_facility_no) == false) {
// 如果作番號重複 使用此函數修正
$this->facilityFixSeq("M");
}
return $this->makeFacilityNo("M", $sale_type, $make_type, $num);
}
/**
* 建立新的汰改作番
* @param string $sale_type : M:内銷 E:外銷 T:他社维保 J:汰改 X:特殊部品
* @param string $make_type : X:小機房 W:無機房 H:家用梯 Z:雜物梯 F:扶梯 B:部品 Q:品保對策 T:研究開發 N:設備 W:出貨現場要求購買 J:營業問題對策 Y:已出貨作番營業進行規格訂正
* @param int $seq_name : 幾個案場 0-99
* @return array $new_facilityno : 作番號
*/
function makeTFacilityNo($sale_type, $make_type, $num)
{
$new_facility_no = $this->getNextFacilityNo("T", $sale_type, $make_type, $num);
if ($this->checkFacilityRepeatStatus($new_facility_no) == false) {
// 如果作番號重複 使用此函數修正
$this->facilityFixSeq("T");
}
return $this->makeFacilityNo("T", $sale_type, $make_type, $num);
}
/**
* 建立新的保養作番
* @param array $sale_type : M:内銷 E:外銷 T:他社维保 J:汰改 X:特殊部品
* @param array $make_type : X:小機房 W:無機房 H:家用梯 Z:雜物梯 F:扶梯 B:部品 Q:品保對策 T:研究開發 N:設備 W:出貨現場要求購買 J:營業問題對策 Y:已出貨作番營業進行規格訂正
* @param int $seq_name : 幾個案場 0-99
* @return array $new_facilityno : 作番號
*/
function makeBFacilityNo($sale_type, $make_type, $num=1)
{
if (count($sale_type) !== $num)
return "陣列數量不一致!";
if (count($make_type) !== $num)
return "陣列數量不一致!";
$new_facility_no = $this->getNextFacilityNo("B", $sale_type, $make_type, $num);
if ($this->checkFacilityRepeatStatus($new_facility_no) == false) {
// 如果作番號重複 使用此函數修正
$this->facilityFixSeq("B");
}
return $this->makeFacilityNo("B", $sale_type, $make_type, $num);
}
}
$cfn = new CreateFacilityNo;
// // 建立作番號 - 新梯
// print_r($cfn->makeMFacilityNo("M", "X", 5));
// echo "<br/><br/>";
// // 建立作番號 - 汰改
// print_r($cfn->makeTFacilityNo("M", "X", 1));
// echo "<br/><br/>";
// // 建立作番號 - 保養
// print_r($cfn->makeBFacilityNo(["M", "E"], ["X", "W"], 2));
// echo "<br/><br/>";