Files
sam-kd/output/lotnum_generator.php

73 lines
2.3 KiB
PHP
Raw Normal View History

<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
$pdo = db_connect();
$prodCode = isset($_POST['prodCode']) ? $_POST['prodCode'] : '';
$pairCode = isset($_POST['pairCode']) ? $_POST['pairCode'] : '';
$outworkplace = isset($_POST['outworkplace']) ? $_POST['outworkplace'] : '';
$user_name = isset($_POST['user_name']) ? $_POST['user_name'] : '';
$tablename = 'lot_sales'; // 테이블명
$lot_number = '';
$currentDate = date('ymd'); // 예: 240926
$filepath = $_SERVER['DOCUMENT_ROOT'] . '/output/lotnum.txt';
// 파일이 존재하지 않으면 새로 생성
if (!file_exists($filepath)) {
$lot_number = $currentDate . '-01';
} else {
// 파일에서 이전 로트번호 읽기
$lastLotNumber = file_get_contents($filepath);
list($date, $number) = explode('-', $lastLotNumber);
// 날짜가 같으면 번호를 증가, 다르면 01로 초기화
if ($date === $currentDate) {
$newNumber = str_pad((int)$number + 1, 2, '0', STR_PAD_LEFT);
$lot_number = $currentDate . '-' . $newNumber;
} else {
$lot_number = $currentDate . '-01';
}
}
// 로트번호 갱신
file_put_contents($filepath, $lot_number);
try {
$search_tag = $lot_number . ' ' .
$author . ' ' .
$workplacename . ' ' .
$remark . ' ' ;
$pdo->beginTransaction();
// SQL 쿼리 생성 (삽입)
$sql = "INSERT INTO {$DB}.{$tablename} (reg_date, lot_number, author, workplacename, remark, search_tag) ";
$sql .= "VALUES (?, ?, ?, ?, '', ?)";
$stmh = $pdo->prepare($sql);
$reg_date = date('Y-m-d'); // 현재 날짜
$search_tag = $lot_number . ' ' . $outworkplace . ' ' . $user_name; // 검색 태그 생성
// 변수 바인딩
$stmh->bindValue(1, $reg_date, PDO::PARAM_STR);
$stmh->bindValue(2, $lot_number, PDO::PARAM_STR);
$stmh->bindValue(3, $user_name, PDO::PARAM_STR);
$stmh->bindValue(4, $outworkplace, PDO::PARAM_STR);
$stmh->bindValue(5, $search_tag, PDO::PARAM_STR);
// 실행
$stmh->execute();
$pdo->commit();
// 결과 반환 (JSON 형식)
echo json_encode(['lotNum' => 'KD-' . $pairCode . '-' . $lot_number]);
} catch (PDOException $Exception) {
$pdo->rollBack();
echo json_encode(['error' => $Exception->getMessage()]);
}
?>