Files
BE_CPONE/application/controllers/mockup/masterdata/Voucher.php
2026-04-27 10:31:17 +07:00

363 lines
9.9 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
class Voucher extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "Patient API";
}
public function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("onedev", true);
}
public function add_notes($orderid){
}
public function search()
{
//# cek token valid
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$startdate = $prm['startdate'] . " 00:00:01";
$enddate = $prm['enddate'] . " 23:59:59";
$search = $prm["search"];
$number_limit = 10;
$number_offset = ($prm['current_page'] - 1) * $number_limit;
// ===============================
// BUILD WHERE DINAMIS
// ===============================
$where = "VoucherHeaderIsActive = 'Y'
AND VoucherHeaderStartDate BETWEEN ? AND ?";
$params = [$startdate, $enddate];
if (!empty($search)) {
$where .= " AND (VoucherHeaderName LIKE ? OR VoucherHeaderPrefix LIKE '%{$search}%')";
$params[] = "%{$search}%";
}
// ===============================
// COUNT TOTAL
// ===============================
$sql = "SELECT COUNT(*) as total
FROM voucher_header
WHERE $where";
$query = $this->db_onedev->query($sql, $params);
if (!$query) {
$this->sys_error_db("voucher_header count", $this->db_onedev);
exit;
}
$tot_count = $query->row()->total;
$tot_page = ceil($tot_count / $number_limit);
// ===============================
// GET DATA
// ===============================
$sql = "SELECT *,
CONCAT(
DATE_FORMAT(VoucherHeaderStartDate,'%d-%m-%Y'),
' s/d ',
DATE_FORMAT(VoucherHeaderEndDate,'%d-%m-%Y')
) as periode,
'N' isused
FROM voucher_header
WHERE $where
ORDER BY VoucherHeaderID DESC
LIMIT $number_limit OFFSET $number_offset";
$query = $this->db_onedev->query($sql, $params);
if (!$query) {
$this->sys_error_db("voucher_header list", $this->db_onedev);
exit;
}
$rows = $query->result_array();
foreach ($rows as $k => $v) {
$x = $this->db_onedev->query("select count(*) as tot
FROM voucher_detail
where VoucherDetailVoucherHeaderID = '{$v['VoucherHeaderID']}'
AND VoucherDetailIsUsed = 'Y'")->row();
if($x->tot == 0){
$isused = 'N';
}else{
$isused = 'Y';
}
$rows[$k]['isused'] = $isused;
}
$result = [
"total" => $tot_page,
"records" => $rows
];
$this->sys_ok($result);
exit;
}
public function searchdetail()
{
//# cek token valid
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$search = isset($prm["search"]) ? $prm["search"] : '';
$ID = isset($prm["ID"]) ? (int) $prm["ID"] : 0;
$status = isset($prm["status"]) ? $prm["status"] : ''; // '' = semua, 'N' = belum dipakai, 'Y' = sudah dipakai
$number_limit = 10;
$number_offset = ($prm['current_page'] - 1) * $number_limit;
// ===============================
// BUILD WHERE DINAMIS
// ===============================
$where = "VoucherDetailIsActive = 'Y'";
if (!empty($search)) {
$where .= " AND (VoucherDetailNumber LIKE '%{$search}%' OR IFNULL(T_OrderHeaderLabNumber,'') LIKE '%{$search}%')";
}
if ($status === 'Y') {
$where .= " AND VoucherDetailIsUsed = 'Y'";
} elseif ($status === 'N') {
$where .= " AND VoucherDetailIsUsed = 'N'";
}
// ===============================
// COUNT TOTAL
// ===============================
$sql = "SELECT COUNT(*) as total
FROM voucher_detail
LEFT JOIN t_orderheader ON T_OrderHeaderID = VoucherDetailT_OrderHeaderID
WHERE $where
AND ($ID = 0 OR($ID > 0 AND VoucherDetailVoucherHeaderID = $ID))";
$query = $this->db_onedev->query($sql);
if (!$query) {
$this->sys_error_db("voucher_detail count", $this->db_onedev);
exit;
}
$tot_count = $query->row()->total;
$tot_page = ceil($tot_count / $number_limit);
// ===============================
// GET DATA
// ===============================
$sql = "SELECT VoucherDetailID,
VoucherDetailT_OrderHeaderID,
VoucherDetailVoucherHeaderID,
VoucherDetailNumber,
VoucherDetailIsUsed,
IFNULL(T_OrderHeaderLabNumber,'') T_OrderHeaderLabNumber,
IF(VoucherDetailIsUsed = 'Y', 'Digunakan','Belum Digunakan') statusvoucher,
T_OrderHeaderDate
FROM voucher_detail
LEFT JOIN t_orderheader ON T_OrderHeaderID = VoucherDetailT_OrderHeaderID
WHERE $where
AND ($ID = 0 OR($ID > 0 AND VoucherDetailVoucherHeaderID = $ID))
ORDER BY VoucherDetailNumber DESC
LIMIT $number_limit OFFSET $number_offset";
$query = $this->db_onedev->query($sql);
if (!$query) {
$this->sys_error_db("voucher_header list", $this->db_onedev);
exit;
}
$rows = $query->result_array();
foreach ($rows as $k => $v) {
$x = $this->db_onedev->query("select count(*) as tot
FROM voucher_detail
where VoucherDetailVoucherHeaderID = '{$v['VoucherHeaderID']}'
AND VoucherDetailIsUsed = 'Y'")->row();
if($x->tot == 0){
$isused = 'N';
}else{
$isused = 'Y';
}
$rows[$k]['isused'] = $isused;
}
$result = [
"total" => $tot_page,
"records" => $rows
];
$this->sys_ok($result);
exit;
}
public function save()
{
//# ambil parameter input
$prm = $this->sys_input;
$puserid = $prm['pUserID'];
$sql = "CALL sp_fo_send_to_cashier({$puserid})";
$query = $this->db_onedev->query($sql);
if ($query) {
$result = array();
$this->sys_ok($result);
}
else {
$this->sys_error_db("payment save rows", $this->db_onedev);
exit;
}
}
public function savetutup()
{
//# ambil parameter input
$prm = $this->sys_input;
$ppaymentkasirid = $prm['pPaymentKasirID'];
$puserid = $prm['pUserID'];
$sql = "CALL sp_fo_received_cashier('{$ppaymentkasirid}','{$puserid}')";
//$sql = "CALL sp_fo_send_to_cashier({$puserid})";
$query = $this->db_onedev->query($sql);
if ($query) {
$result = array();
$this->sys_ok($result);
}
else {
$this->sys_error_db("payment savetutup rows", $this->db_onedev);
exit;
}
}
public function saveData()
{
$this->db->trans_begin();
try {
$prm = $this->sys_input;
$userid = $this->sys_user["M_UserID"];
$VoucherHeaderID = (int)$prm['xid'];
$VoucherHeaderName = $prm['VoucherHeaderName'];
$VoucherHeaderPrefix = $prm['VoucherHeaderPrefix'];
$VoucherHeaderAmount = $prm['VoucherHeaderAmount'];
$VoucherHeaderQty = (int)$prm['VoucherHeaderQty'];
$VoucherHeaderNote = $prm['VoucherHeaderNote'];
$VoucherHeaderStartDate = $prm['VoucherHeaderStartDate'];
$VoucherHeaderEndDate = $prm['VoucherHeaderEndDate'];
// ======================================
// 1⃣ JIKA INSERT BARU
// ======================================
if ($VoucherHeaderID == 0) {
$header_data = [
'VoucherHeaderName' => $VoucherHeaderName,
'VoucherHeaderPrefix' => $VoucherHeaderPrefix,
'VoucherHeaderAmount' => $VoucherHeaderAmount,
'VoucherHeaderQty' => $VoucherHeaderQty,
'VoucherHeaderNote' => $VoucherHeaderNote,
'VoucherHeaderStartDate' => $VoucherHeaderStartDate,
'VoucherHeaderEndDate' => $VoucherHeaderEndDate,
'VoucherHeaderIsActive' => 'Y',
'VoucherHeaderCreated' => date('Y-m-d H:i:s'),
'VoucherHeaderUserID' => $userid
];
$this->db->insert('voucher_header', $header_data);
$VoucherHeaderID = $this->db->insert_id();
}
// ======================================
// 2⃣ JIKA UPDATE
// ======================================
else {
// update header saja (TIDAK ubah IsActive)
$this->db->where('VoucherHeaderID', $VoucherHeaderID);
$this->db->update('voucher_header', [
'VoucherHeaderName' => $VoucherHeaderName,
'VoucherHeaderPrefix' => $VoucherHeaderPrefix,
'VoucherHeaderAmount' => $VoucherHeaderAmount,
'VoucherHeaderQty' => $VoucherHeaderQty,
'VoucherHeaderNote' => $VoucherHeaderNote,
'VoucherHeaderStartDate' => $VoucherHeaderStartDate,
'VoucherHeaderEndDate' => $VoucherHeaderEndDate,
'VoucherHeaderUserID' => $userid
]);
// nonaktifkan detail lama
$this->db->where('VoucherDetailVoucherHeaderID', $VoucherHeaderID);
$this->db->update('voucher_detail', [
'VoucherDetailIsActive' => 'N'
]);
}
// ======================================
// 3⃣ GENERATE DETAIL BARU
// ======================================
$insert_detail = [];
for ($i = 1; $i <= $VoucherHeaderQty; $i++) {
$insert_detail[] = [
'VoucherDetailVoucherHeaderID' => $VoucherHeaderID,
'VoucherDetailNumber' =>
$VoucherHeaderPrefix . str_pad($i, 4, '0', STR_PAD_LEFT),
'VoucherDetailIsActive' => 'Y',
'VoucherDetailUserID' => $userid
];
}
$this->db->insert_batch('voucher_detail', $insert_detail);
if ($this->db->trans_status() === FALSE) {
throw new Exception("Transaction gagal");
}
$this->db->trans_commit();
echo json_encode([
"status" => "OK",
"VoucherHeaderID" => $VoucherHeaderID,
"total_generated" => $VoucherHeaderQty
]);
exit;
} catch (Exception $e) {
$this->db->trans_rollback();
echo json_encode([
"status" => "ERROR",
"message" => $e->getMessage()
]);
exit;
}
}
}