From 64f434cd780710dd156b5ca4927e8dee7999b93f Mon Sep 17 00:00:00 2001 From: sasadib Date: Mon, 20 Jul 2026 13:13:59 +0700 Subject: [PATCH] feature: po pay downpayment --- .../controllers/map/InventarisCoaMapping.php | 445 ++++-- .../mockup/purchase/faktur/Fakturv4.php | 131 +- .../purchase/order/PurchaseOrderAset.php | 181 ++- .../receive-item-po/ReceiveItemPOAsset.php | 15 +- .../receive-item-po/ReceiveItemPoV2.php | 478 +++--- .../supplierpaymentapprovedv3/Billv2.php | 279 ++-- .../mockup/supplierpaymentcashierv5/Bill.php | 790 ++++++--- .../supplierpaymentcashierv5/PaymentV2.php | 172 +- .../mockup/supplierpaymentv4/Bill.php | 429 ++++- .../mockup/supplierpaymentv4/Payment.php | 1407 +++++++++-------- 10 files changed, 2842 insertions(+), 1485 deletions(-) diff --git a/application/controllers/map/InventarisCoaMapping.php b/application/controllers/map/InventarisCoaMapping.php index 4879b72..f764c52 100644 --- a/application/controllers/map/InventarisCoaMapping.php +++ b/application/controllers/map/InventarisCoaMapping.php @@ -1,8 +1,10 @@ isLogin) { + throw new Exception('Invalid token'); + } + + $para = $this->sys_input; + $keyword = "%" . $para['keyword'] . "%"; + + $sql = "SELECT + coaID, + coaAccountNo, + coaDescription + FROM coa + WHERE coaIsInput = 'Y' + AND ( + coaDescription LIKE ? + OR coaAccountNo LIKE ? + ) + AND coaIsActive = 'Y' + LIMIT 15"; + $que = $this->db->query($sql, [$keyword, $keyword]); + if (!$que) { + throw new Exception('failed to query data inventaris gol', 1); + } + $data = $que->result_array(); + + $this->sys_ok($data); + } catch (Exception $e) { + $msg = '[Error] ' . $e->getMessage(); + $code = $e->getCode(); + if ($code == 0) { + $this->sys_error($msg); + } else { + $this->sys_error_db($msg); + } + exit; + } + } + + public function getListInventarisGol() + { + try { + if (!$this->isLogin) { + throw new Exception('Invalid token'); + } + + $sql = "SELECT + M_InventarisGolID, + M_InventarisGolCode, + M_InventarisGolName + FROM m_inventaris_gol gol + WHERE gol.M_InventarisGolIsActive = 'Y' + AND NOT EXISTS ( + SELECT 1 + FROM m_inventaris_coa_mapping m + WHERE m.M_InventarisCoaMappingM_InventarisGolID = gol.M_InventarisGolID + AND m.M_InventarisCoaMappingIsActive = 'Y' + );"; + $que = $this->db->query($sql); + if (!$que) { + throw new Exception('failed to query data inventaris gol', 1); + } + $data = $que->result_array(); + + $output = [ + 'records' => $data, + 'total' => count($data) + ]; + + $this->sys_ok($output); + } catch (Exception $e) { + $msg = '[Error] ' . $e->getMessage(); + $code = $e->getCode(); + if ($code == 0) { + $this->sys_error($msg); + } else { + $this->sys_error_db($msg); + } + exit; + } + } + + public function getListInventorygolMapping() + { + try { + if (!$this->isLogin) { + throw new Exception('invalid token'); + } + + $para = $this->sys_input; + $keyword = "%" . $para['keyword'] . "%"; + + $limit = 10; + $offset = 0; + if ($para['currpage'] > 0) { + $offset = ($para['currpage'] - 1) * $limit; + } + + $sql = "SELECT + M_InventarisGolID, + M_InventarisGolCode, + M_InventarisGolName, + M_InventarisCoaMappingID, + M_InventarisCoaMappingCoaInventarisID, + coaInv.coaAccountNo AS CoaInventarisAccountNo, + coaInv.coaDescription AS CoaInventarisDescription, + M_InventarisCoaMappingCoaHutangID, + coaHtg.coaAccountNo AS CoaHutangAccountNo, + coaHtg.coaDescription AS CoaHutangDescription, + M_InventarisCoaMappingCoaBebanPenyusutanID, + coaBbn.coaAccountNo AS CoaBebanPenyusutanAccountNo, + coaBbn.coaDescription AS CoaBebanPenyusutanDescription, + M_InventarisCoaMappingCoaAkumulasiPenyusutanID, + coaAkm.coaAccountNo AS CoaAkumulasiPenyusutanAccountNo, + coaAkm.coaDescription AS CoaAkumulasiPenyusutanDescription, + M_InventarisCoaMappingCoaLabaPelepasanID, + coaLab.coaAccountNo AS CoaLabaPelepasanAccountNo, + coaLab.coaDescription AS CoaLabaPelepasanDescription, + M_InventarisCoaMappingCoaRugiPelepasanID, + coaRug.coaAccountNo AS CoaRugiPelepasanAccountNo, + coaRug.coaDescription AS CoaRugiPelepasanDescription + FROM m_inventaris_gol + JOIN m_inventaris_coa_mapping + ON M_InventarisCoaMappingM_InventarisGolID = M_InventarisGolID + AND M_InventarisCoaMappingIsActive = 'Y' + AND M_InventarisGolIsActive = 'Y' + AND M_InventarisGolName LIKE ? + LEFT JOIN coa AS coaInv + ON M_InventarisCoaMappingCoaInventarisID = coaInv.coaID + LEFT JOIN coa AS coaHtg + ON M_InventarisCoaMappingCoaHutangID = coaHtg.coaID + LEFT JOIN coa AS coaBbn + ON M_InventarisCoaMappingCoaBebanPenyusutanID = coaBbn.coaID + LEFT JOIN coa AS coaAkm + ON M_InventarisCoaMappingCoaAkumulasiPenyusutanID = coaAkm.coaID + LEFT JOIN coa AS coaLab + ON M_InventarisCoaMappingCoaLabaPelepasanID = coaLab.coaID + LEFT JOIN coa AS coaRug + ON M_InventarisCoaMappingCoaRugiPelepasanID = coaRug.coaID + WHERE M_InventarisGolIsActive = 'Y'"; + + $sql_data = $sql . " ORDER BY M_InventarisGolID LIMIT ? OFFSET ? "; + $que = $this->db->query($sql_data, [ + $keyword, + $limit, + $offset + ]); + if (!$que) { + throw new Exception('failed to query data mapping coa inventaris golongan', 1); + } + + $sql_total = "SELECT COUNT(*) AS total FROM ($sql) AS x"; + $que_total = $this->db->query($sql_total, [$keyword]); + if (!$que_total) { + throw new Exception('failed to get total rows data', 1); + } + + $this->sys_ok([ + "records" => $que->result_array(), + "total" => $que_total->row_array()['total'] + ]); + } catch (Exception $e) { + $msg = '[Error] ' . $e->getMessage(); + $code = $e->getCode(); + if ($code == 0) { + $this->sys_error($msg); + } else { + $this->sys_error_db($msg); + } + exit; + } + } + + public function getInvCoaMappingDetail() + { + try { + if (!$this->isLogin) { + $this->sys_error("invalid token"); + exit; + } + + $para = $this->sys_input; + + $sql = "SELECT + M_InventarisCoaMappingID, + M_InventarisCoaMappingM_InventarisGolID, + M_InventarisCoaMappingCoaInventarisID, + M_InventarisCoaMappingCoaHutangID, + M_InventarisCoaMappingCoaPembelianID, + M_InventarisCoaMappingCoaBebanPenyusutanID, + M_InventarisCoaMappingCoaAkumulasiPenyusutanID, + M_InventarisCoaMappingCoaLabaPelepasanID, + M_InventarisCoaMappingCoaRugiPelepasanID, + M_InventarisCoaMappingCreatedUserID, + M_InventarisCoaMappingCreated, + M_InventarisCoaMappingLastUpdated + FROM m_inventaris_coa_mapping + WHERE M_InventarisCoaMappingIsActive = 'Y' + AND M_InventarisCoaMappingID = ?"; + $query = $this->db->query($sql, [$para['M_InventarisCoaMappingID']]); + if (!$query) { + $this->sys_error_db("[Error] get data m_inventaris_coa_mapping"); + exit; + } + $data = $query->row_array(); + + $this->sys_ok($data); + } catch (Exception $exc) { + $msg = $exc->getMessage(); + $this->sys_error($msg); + } + } + ## MUTATIONS ## - public function createInvCoaMapping() { + public function createInvCoaMapping() + { try { if (!$this->isLogin) { $this->sys_error("invalid token"); @@ -64,48 +284,8 @@ class InventarisCoaMapping extends MY_Controller { } } - ## QUERY ## - public function getInvCoaMapping() { - try { - if (!$this->isLogin) { - $this->sys_error("invalid token"); - exit; - } - - $para = $this->sys_input; - - $sql = "SELECT - M_InventarisCoaMappingID, - M_InventarisCoaMappingM_InventarisGolID, - M_InventarisCoaMappingCoaInventarisID, - M_InventarisCoaMappingCoaHutangID, - M_InventarisCoaMappingCoaPembelianID, - M_InventarisCoaMappingCoaBebanPenyusutanID, - M_InventarisCoaMappingCoaAkumulasiPenyusutanID, - M_InventarisCoaMappingCoaLabaPelepasanID, - M_InventarisCoaMappingCoaRugiPelepasanID, - M_InventarisCoaMappingCreatedUserID, - M_InventarisCoaMappingCreated, - M_InventarisCoaMappingLastUpdated - FROM m_inventaris_coa_mapping - WHERE M_InventarisCoaMappingIsActive = 'Y' - AND M_InventarisCoaMappingID = ?"; - $query = $this->db->query($sql, [$para['M_InventarisCoaMappingID']]); - if (!$query) { - $this->sys_error_db("[Error] get data m_inventaris_coa_mapping"); - exit; - } - $data = $query->row_array(); - - $this->sys_ok($data); - } catch (Exception $exc) { - $msg = $exc->getMessage(); - $this->sys_error($msg); - } - } - - ## MUTATIONS ## - public function editInvCoaMapping() { + public function editInvCoaMapping() + { try { if (!$this->isLogin) { $this->sys_error("invalid token"); @@ -154,7 +334,8 @@ class InventarisCoaMapping extends MY_Controller { } } - public function deleteInvCoaMapping() { + public function deleteInvCoaMapping() + { try { if (!$this->isLogin) { $this->sys_error("invalid token"); @@ -185,8 +366,129 @@ class InventarisCoaMapping extends MY_Controller { } } + + ## QUERY ITEM ## + public function getListItemInventaris() + { + try { + if (!$this->isLogin) { + throw new Exception('Invalid token'); + } + + $para = $this->sys_input; + + $sql = "SELECT + M_ItemID, + M_ItemCode, + M_ItemDesc, + M_ItemM_InventarisGolID AS itemGolID, + IFNULL(M_InventarisItemCoaMappingID, 0) AS itemCoaMapID, + M_InventarisItemCoaMappingCoaInventarisID, + coaInv.coaAccountNo AS CoaInventarisAccountNo, + coaInv.coaDescription AS CoaInventarisDescription, + M_InventarisItemCoaMappingCoaHutangID, + coaHtg.coaAccountNo AS CoaHutangAccountNo, + coaHtg.coaDescription AS CoaHutangDescription, + M_InventarisItemCoaMappingCoaBebanPenyusutanID, + coaBbn.coaAccountNo AS CoaBebanPenyusutanAccountNo, + coaBbn.coaDescription AS CoaBebanPenyusutanDescription, + M_InventarisItemCoaMappingCoaAkumulasiPenyusutanID, + coaAkm.coaAccountNo AS CoaAkumulasiPenyusutanAccountNo, + coaAkm.coaDescription AS CoaAkumulasiPenyusutanDescription, + M_InventarisItemCoaMappingCoaLabaPelepasanID, + coaLab.coaAccountNo AS CoaLabaPelepasanAccountNo, + coaLab.coaDescription AS CoaLabaPelepasanDescription, + M_InventarisItemCoaMappingCoaRugiPelepasanID, + coaRug.coaAccountNo AS CoaRugiPelepasanAccountNo, + coaRug.coaDescription AS CoaRugiPelepasanDescription + FROM m_item + LEFT JOIN m_inventaris_item_coa_mapping + ON M_ItemID = M_InventarisItemCoaMappingM_ItemID + AND M_InventarisItemCoaMappingIsActive = 'Y' + LEFT JOIN coa AS coaInv + ON M_InventarisItemCoaMappingCoaInventarisID = coaInv.coaID + LEFT JOIN coa AS coaHtg + ON M_InventarisItemCoaMappingCoaHutangID = coaHtg.coaID + LEFT JOIN coa AS coaBbn + ON M_InventarisItemCoaMappingCoaBebanPenyusutanID = coaBbn.coaID + LEFT JOIN coa AS coaAkm + ON M_InventarisItemCoaMappingCoaAkumulasiPenyusutanID = coaAkm.coaID + LEFT JOIN coa AS coaLab + ON M_InventarisItemCoaMappingCoaLabaPelepasanID = coaLab.coaID + LEFT JOIN coa AS coaRug + ON M_InventarisItemCoaMappingCoaRugiPelepasanID = coaRug.coaID + WHERE M_ItemItem_CategoryID = 2 + AND M_ItemM_InventarisGolID = ? + AND M_ItemIsActive = 'Y'"; + $que = $this->db->query($sql, [ + $para['golID'] + ]); + if (!$que) { + throw new Exception('failed to query data inventaris gol', 1); + } + $data = $que->result_array(); + + $output = [ + 'records' => $data, + 'total' => count($data) + ]; + + $this->sys_ok($output); + } catch (Exception $e) { + $msg = '[Error] ' . $e->getMessage(); + $code = $e->getCode(); + if ($code == 0) { + $this->sys_error($msg); + } else { + $this->sys_error_db($msg); + } + exit; + } + } + + public function getInvItemCoaMapping() + { + try { + if (!$this->isLogin) { + $this->sys_error("invalid token"); + exit; + } + + $para = $this->sys_input; + + $sql = "SELECT + M_InventarisItemCoaMappingID, + M_InventarisItemCoaMappingM_ItemID, + M_InventarisItemCoaMappingCoaInventarisID, + M_InventarisItemCoaMappingCoaHutangID, + M_InventarisItemCoaMappingCoaPembelianID, + M_InventarisItemCoaMappingCoaBebanPenyusutanID, + M_InventarisItemCoaMappingCoaAkumulasiPenyusutanID, + M_InventarisItemCoaMappingCoaLabaPelepasanID, + M_InventarisItemCoaMappingCoaRugiPelepasanID, + M_InventarisItemCoaMappingCreatedUserID, + M_InventarisItemCoaMappingCreated, + M_InventarisItemCoaMappingLastUpdated + FROM m_inventaris_item_coa_mapping + WHERE M_InventarisItemCoaMappingIsActive = 'Y' + AND M_InventarisItemCoaMappingID = ?"; + $query = $this->db->query($sql, [$para['M_InventarisItemCoaMappingID']]); + if (!$query) { + $this->sys_error_db("[Error] get data m_inventaris_item_coa_mapping"); + exit; + } + $data = $query->row_array(); + + $this->sys_ok($data); + } catch (Exception $exc) { + $msg = $exc->getMessage(); + $this->sys_error($msg); + } + } + ## MUTATIONS ITEM ## - public function createInvItemCoaMapping() { + public function createInvItemCoaMapping() + { try { if (!$this->isLogin) { $this->sys_error("invalid token"); @@ -231,55 +533,15 @@ class InventarisCoaMapping extends MY_Controller { $insertID = $this->db->insert_id(); $this->db->trans_commit(); - $this->sys_ok($insertID); + $this->sys_ok("[Success] insert coa item"); } catch (Exception $exc) { $msg = $exc->getMessage(); $this->sys_error($msg); } } - ## QUERY ITEM ## - public function getInvItemCoaMapping() { - try { - if (!$this->isLogin) { - $this->sys_error("invalid token"); - exit; - } - - $para = $this->sys_input; - - $sql = "SELECT - M_InventarisItemCoaMappingID, - M_InventarisItemCoaMappingM_ItemID, - M_InventarisItemCoaMappingCoaInventarisID, - M_InventarisItemCoaMappingCoaHutangID, - M_InventarisItemCoaMappingCoaPembelianID, - M_InventarisItemCoaMappingCoaBebanPenyusutanID, - M_InventarisItemCoaMappingCoaAkumulasiPenyusutanID, - M_InventarisItemCoaMappingCoaLabaPelepasanID, - M_InventarisItemCoaMappingCoaRugiPelepasanID, - M_InventarisItemCoaMappingCreatedUserID, - M_InventarisItemCoaMappingCreated, - M_InventarisItemCoaMappingLastUpdated - FROM m_inventaris_item_coa_mapping - WHERE M_InventarisItemCoaMappingIsActive = 'Y' - AND M_InventarisItemCoaMappingID = ?"; - $query = $this->db->query($sql, [$para['M_InventarisItemCoaMappingID']]); - if (!$query) { - $this->sys_error_db("[Error] get data m_inventaris_item_coa_mapping"); - exit; - } - $data = $query->row_array(); - - $this->sys_ok($data); - } catch (Exception $exc) { - $msg = $exc->getMessage(); - $this->sys_error($msg); - } - } - - ## MUTATIONS ITEM ## - public function editInvItemCoaMapping() { + public function editInvItemCoaMapping() + { try { if (!$this->isLogin) { $this->sys_error("invalid token"); @@ -328,7 +590,8 @@ class InventarisCoaMapping extends MY_Controller { } } - public function deleteInvItemCoaMapping() { + public function deleteInvItemCoaMapping() + { try { if (!$this->isLogin) { $this->sys_error("invalid token"); diff --git a/application/controllers/mockup/purchase/faktur/Fakturv4.php b/application/controllers/mockup/purchase/faktur/Fakturv4.php index 3c15c3b..6437caa 100644 --- a/application/controllers/mockup/purchase/faktur/Fakturv4.php +++ b/application/controllers/mockup/purchase/faktur/Fakturv4.php @@ -496,6 +496,7 @@ class Fakturv4 extends MY_Controller ELSE '' END as WarehouseName, ReceiveOrderPoID, + ReceiveOrderPoTypePurchase, PurchaseOrderItemCategoryID FROM supplier_invoice JOIN supplier_invoice_detail ON SupplierInvoiceID = SupplierInvoiceDetailSupplierInvoiceID @@ -588,12 +589,11 @@ class Fakturv4 extends MY_Controller 'P' ) AS DiscountType, SupplierInvoiceReceiveOrderPoID, - ReceiveOrderPoNumber + ReceiveOrderPoNumber, + ReceiveOrderPoTypePurchase AS typePurchase FROM supplier_invoice JOIN supplier ON SupplierInvoiceSupplierID = SupplierID JOIN receive_order_po ON ReceiveOrderPoID = SupplierInvoiceReceiveOrderPoID - -- JOIN purchase_order ON SupplierInvoicePurchaseOrderID = PurchaseOrderID - -- AND PurchaseOrderIsActive = 'Y' WHERE SupplierInvoiceIsActive = 'Y' AND SupplierInvoiceID = ?"; $que = $this->db->query($sql, $para['SInvoiceID']); @@ -601,6 +601,7 @@ class Fakturv4 extends MY_Controller $this->sys_error_db('[Error] get detail data invoice'); exit; } + $data = $que->row_array(); $sqldet = "SELECT SupplierInvoiceDetailID, @@ -636,7 +637,30 @@ class Fakturv4 extends MY_Controller exit; } - $data = $que->result_array()[0]; + if (isset($data['typePurchase']) && $data['typePurchase'] == 'aset') { + $sql_dp = "SELECT + IFNULL(SupplierDownpaymentAmount, 0) AS dp_amount + FROM supplier_invoice + JOIN receive_order_po + ON ReceiveOrderPoID = SupplierInvoiceReceiveOrderPoID + JOIN purchase_order_asset_contract + ON PurchaseOrderAssetContractReceiveOrderPoID = ReceiveOrderPoID + AND PurchaseOrderAssetContractIsActive = 'Y' + JOIN supplier_downpayment + ON SupplierDownpaymentPurchasOrderID = PurchaseOrderAssetContractPurchaseOrderID + AND SupplierDownpaymentIsActive = 'Y' + WHERE SupplierInvoiceID = ?"; + $que_dp = $this->db->query($sql_dp, [ + $para['SInvoiceID'] + ]); + if (!$que_dp) { + $this->sys_error_db("[Error] get info contract asset"); + exit; + } + $dp_amount = $que_dp->row_array()['dp_amount']; + $data['dp_amount'] = $dp_amount; + } + $data['detail'] = $quedet->result_array(); $result = $data; @@ -1317,7 +1341,8 @@ class Fakturv4 extends MY_Controller supplier_invoice_detail.*, ReceiveOrderPoNumber, ReceiveOrderPoM_BranchCode, - ReceiveOrderPoS_RegionalID + ReceiveOrderPoS_RegionalID, + ReceiveOrderPoTypePurchase AS typePurchase FROM supplier_invoice JOIN supplier_invoice_detail ON SupplierInvoiceDetailSupplierInvoiceID = SupplierInvoiceID AND SupplierInvoiceDetailIsActive = 'Y' @@ -1497,52 +1522,58 @@ class Fakturv4 extends MY_Controller /* Insert jurnal tx for down payment asset */ // ---------------------------------------------------------------------------------------- - $sql_dpasset = "SELECT - PurchaseOrderAssetContractID, - PurchaseOrderAssetContractName, - PurchaseOrderAssetContractInstallmentDownPayment, - coaID, - coaDescription - FROM purchase_order_asset_contract - JOIN purchase_order ON PurchaseOrderID = PurchaseOrderAssetContractPurchaseOrderID - JOIN purchase_order_detail ON PurchaseOrderID = PurchaseOrderDetailPurchaseOrderID - AND PurchaseOrderDetailIsActive = 'Y' - JOIN m_item ON M_ItemID = PurchaseOrderDetailItemID - AND M_ItemItem_CategoryID = 3 - JOIN fa_class ON Fa_ClassID = M_ItemFa_ClassID - AND Fa_ClassIsActive = 'Y' - JOIN coa ON coaID = Fa_ClassDownPaymentCoaID - AND coaIsActive = 'Y' - WHERE PurchaseOrderAssetContractReceiveOrderPoID = ? - AND PurchaseOrderAssetContractIsActive = 'Y' - LIMIT 1"; - $que_dpasset = $this->db->query($sql_dpasset, [ - $invoice[0]['SupplierInvoiceReceiveOrderPoID'] - ]); - if (!$que_dpasset) { - $this->db->trans_rollback(); - $this->sys_error_db("[Error] query dp asset not error"); - exit; - } - $dpasset_coa = $que_dpasset->row_array(); - if (!empty($dpasset_coa)) { - $totalDP = round($dpasset_coa['PurchaseOrderAssetContractInstallmentDownPayment'], 2); - if ($totalDP > 0) { - $insert_dp = $this->InsertJurnalTx( - $jurnalID, - $dpasset_coa['coaID'], - $dpasset_coa['coaDescription'], - $user['M_UserID'], - 0, - $totalDP - ); - if (!$insert_dp['status']) { - $this->db->trans_rollback(); - $this->sys_error_db($insert_dp['msg']); - exit; - } + if (isset($invoice[0]['typePurchase']) && $invoice[0]['typePurchase'] == 'aset') { + $sql_dpasset = "SELECT + PurchaseOrderAssetContractID, + PurchaseOrderAssetContractName, + SupplierDownpaymentAmount, + coaID, + coaDescription + FROM purchase_order_asset_contract + JOIN purchase_order ON PurchaseOrderID = PurchaseOrderAssetContractPurchaseOrderID + JOIN supplier_downpayment + ON SupplierDownpaymentPurchasOrderID = PurchaseOrderID + AND SupplierDownpaymentIsActive = 'Y' + JOIN purchase_order_detail + ON PurchaseOrderID = PurchaseOrderDetailPurchaseOrderID + AND PurchaseOrderDetailIsActive = 'Y' + JOIN m_item ON M_ItemID = PurchaseOrderDetailItemID + AND M_ItemItem_CategoryID = 3 + JOIN fa_class ON Fa_ClassID = M_ItemFa_ClassID + AND Fa_ClassIsActive = 'Y' + JOIN coa ON coaID = Fa_ClassDownPaymentCoaID + AND coaIsActive = 'Y' + WHERE PurchaseOrderAssetContractReceiveOrderPoID = ? + AND PurchaseOrderAssetContractIsActive = 'Y' + LIMIT 1"; + $que_dpasset = $this->db->query($sql_dpasset, [ + $invoice[0]['SupplierInvoiceReceiveOrderPoID'] + ]); + if (!$que_dpasset) { + $this->db->trans_rollback(); + $this->sys_error_db("[Error] query dp asset not error"); + exit; + } + $dpasset_coa = $que_dpasset->row_array(); + if (!empty($dpasset_coa)) { + $totalDP = round($dpasset_coa['SupplierDownpaymentAmount'], 2); + if ($totalDP > 0) { + $insert_dp = $this->InsertJurnalTx( + $jurnalID, + $dpasset_coa['coaID'], + $dpasset_coa['coaDescription'], + $user['M_UserID'], + 0, + $totalDP + ); + if (!$insert_dp['status']) { + $this->db->trans_rollback(); + $this->sys_error_db($insert_dp['msg']); + exit; + } - $invoice = $this->CalcProrateDownPaymentAsset($invoice, $totalDP); + $invoice = $this->CalcProrateDownPaymentAsset($invoice, $totalDP); + } } } diff --git a/application/controllers/mockup/purchase/order/PurchaseOrderAset.php b/application/controllers/mockup/purchase/order/PurchaseOrderAset.php index fbc3b71..eb5cc6f 100644 --- a/application/controllers/mockup/purchase/order/PurchaseOrderAset.php +++ b/application/controllers/mockup/purchase/order/PurchaseOrderAset.php @@ -228,11 +228,15 @@ class PurchaseOrderAset extends MY_Controller { PurchaseOrderAssetContractEndDate AS contractEnd, IFNULL(attach.AttachmentCount, 0) AS AttachmentCount, SupplierID, - SupplierName + SupplierName, + SupplierDownpaymentID FROM purchase_order JOIN supplier ON SupplierID = PurchaseOrderSupplierID JOIN purchase_order_asset_contract ON PurchaseOrderAssetContractPurchaseOrderID = PurchaseOrderID + LEFT JOIN supplier_downpayment + ON SupplierDownpaymentPurchasOrderID = PurchaseOrderID + AND SupplierDownpaymentIsActive = 'Y' LEFT JOIN ( SELECT ContractAssetAttachmentPurchaseOrderID, @@ -398,55 +402,59 @@ class PurchaseOrderAset extends MY_Controller { # get data from purchase order # $sql_poasset = "SELECT - PurchaseOrderID, - PurchaseOrderDate AS podate, - PurchaseOrderNote AS catatan, - PurchaseOrderNumber, - PurchaseOrderStatus, - PurchaseOrderRefNumber AS reference, - PurchaseOrderGrandTotal AS grandtotal, - CASE - WHEN PurchaseOrderTaxPpnType = 'P' THEN 'percent' - ELSE 'nominal' - END AS typepajak, - CASE - WHEN PurchaseOrderTaxPpnType = 'P' THEN PurchaseOrderTaxPercentPpn - ELSE PurchaseOrderTaxAmountPpn - END AS valuepajak, - CASE - WHEN PurchaseOrderDiscountPercent > 0.00 THEN 'percent' - ELSE 'nominal' - END AS typediskon, - CASE - WHEN PurchaseOrderDiscountPercent > 0.00 THEN PurchaseOrderDiscountPercent - ELSE PurchaseOrderDiscountAmount - END AS valuediskon, - PurchaseOrderSubTotal AS subtotal, - PurchaseOrderGrandTotal AS grandtotal, - SupplierID AS supplierID, - SupplierName, - WarehouseID AS gudangID, - WarehouseM_BranchID AS branchID, - PurchaseOrderAssetContractID, - PurchaseOrderAssetContractTAssetAttachmentID AS assetAttachmentID, - PurchaseOrderAssetContractName AS contractName, - PurchaseOrderAssetContractDate AS contractDate, - PurchaseOrderAssetContractStartDate AS contractStart, - PurchaseOrderAssetContractEndDate AS contractEnd, - PurchaseOrderAssetContractDuration AS contractDuration, - PurchaseOrderAssetContractInstallmentNumber AS installmentNumber, - PurchaseOrderAssetContractInstallmentDate AS installmentDate, - PurchaseOrderAssetContractInstallmentPayAmount AS installmentPayAmount, - PurchaseOrderAssetContractInstallmentDownPaymentType AS installmentDownPaymentType, - PurchaseOrderAssetContractInstallmentDownPayment AS installmentDownPayment, - PurchaseOrderApprovedManagerUserID AS verifiedby - FROM purchase_order - JOIN supplier ON SupplierID = PurchaseOrderSupplierID - JOIN warehouse ON WarehouseID = PurchaseOrderWarehouseID - JOIN purchase_order_asset_contract - ON PurchaseOrderAssetContractPurchaseOrderID = PurchaseOrderID - WHERE PurchaseOrderIsActive = 'Y' - AND PurchaseOrderID = ?"; + PurchaseOrderID, + PurchaseOrderDate AS podate, + PurchaseOrderNote AS catatan, + PurchaseOrderNumber, + PurchaseOrderStatus, + PurchaseOrderRefNumber AS reference, + PurchaseOrderGrandTotal AS grandtotal, + CASE + WHEN PurchaseOrderTaxPpnType = 'P' THEN 'percent' + ELSE 'nominal' + END AS typepajak, + CASE + WHEN PurchaseOrderTaxPpnType = 'P' THEN PurchaseOrderTaxPercentPpn + ELSE PurchaseOrderTaxAmountPpn + END AS valuepajak, + CASE + WHEN PurchaseOrderDiscountPercent > 0.00 THEN 'percent' + ELSE 'nominal' + END AS typediskon, + CASE + WHEN PurchaseOrderDiscountPercent > 0.00 THEN PurchaseOrderDiscountPercent + ELSE PurchaseOrderDiscountAmount + END AS valuediskon, + PurchaseOrderSubTotal AS subtotal, + PurchaseOrderGrandTotal AS grandtotal, + SupplierID AS supplierID, + SupplierName, + WarehouseID AS gudangID, + WarehouseM_BranchID AS branchID, + PurchaseOrderAssetContractID, + PurchaseOrderAssetContractTAssetAttachmentID AS assetAttachmentID, + PurchaseOrderAssetContractName AS contractName, + PurchaseOrderAssetContractDate AS contractDate, + PurchaseOrderAssetContractStartDate AS contractStart, + PurchaseOrderAssetContractEndDate AS contractEnd, + PurchaseOrderAssetContractDuration AS contractDuration, + PurchaseOrderAssetContractInstallmentNumber AS installmentNumber, + PurchaseOrderAssetContractInstallmentDate AS installmentDate, + PurchaseOrderAssetContractInstallmentPayAmount AS installmentPayAmount, + PurchaseOrderAssetContractInstallmentDownPaymentType AS installmentDownPaymentType, + PurchaseOrderAssetContractInstallmentDownPayment AS installmentDownPayment, + PurchaseOrderApprovedManagerUserID AS verifiedby, + SupplierDownpaymentID + FROM purchase_order + JOIN supplier ON SupplierID = PurchaseOrderSupplierID + JOIN warehouse ON WarehouseID = PurchaseOrderWarehouseID + JOIN purchase_order_asset_contract + ON PurchaseOrderAssetContractPurchaseOrderID = PurchaseOrderID + LEFT JOIN supplier_downpayment + ON SupplierDownpaymentPurchasOrderID = PurchaseOrderID + AND SupplierDownpaymentIsActive = 'Y' + WHERE PurchaseOrderIsActive = 'Y' + AND PurchaseOrderID = ?"; $que_poasset = $this->db->query($sql_poasset, [$para['poID']]); if (!$que_poasset) { $this->sys_error_db("[Error] failed get data from purchase order"); @@ -592,6 +600,7 @@ class PurchaseOrderAset extends MY_Controller { $summary_subtotal = floatval($para['summary']['subtotal']); $summary_diskon = floatval($para['summary']['diskon']); $summary_pajak = floatval($para['summary']['pajak']); + $summary_downpayment = floatval($para['summary']['downpayment']); $summary_total = floatval($para['summary']['total']); $sql_po = "INSERT INTO purchase_order ( @@ -762,6 +771,33 @@ class PurchaseOrderAset extends MY_Controller { exit; } + # INSERT into table supplier_downpayment # + $sql_dp = "INSERT INTO supplier_downpayment ( + SupplierDownpaymentPurchasOrderID, + SupplierDownpaymentSupplierID, + SupplierDownpaymentAmount, + SupplierDownpaymentDate, + SupplierDownpaymentDueDate, + SupplierDownpaymentStatus, + SupplierDownpaymentCreatedUserID, + SupplierDownpaymentLastUpdatedUserID + ) VALUES (?,?,?,?,?,?,?,?)"; + $que_dp = $this->db->query($sql_dp, [ + $PurchaseOrderID, + $para['supplierID'], + $summary_downpayment ?: 0.00, + $para['contractStart'], + $para['contractStart'], + 'Draft', + $user['M_UserID'], + $user['M_UserID'] + ]); + if (!$que_dp) { + $this->db->trans_rollback(); + $this->sys_error_db("[Error] failed insert supplier downpayment"); + exit; + } + $this->insertLog( $PurchaseOrderID, 'CREATE', $para, '', [], $user['M_UserID'], 'create purchase order asset' @@ -803,6 +839,7 @@ class PurchaseOrderAset extends MY_Controller { $summary_subtotal = floatval($para['summary']['subtotal']); $summary_diskon = floatval($para['summary']['diskon']); $summary_pajak = floatval($para['summary']['pajak']); + $summary_downpayment = floatval($para['summary']['downpayment']); $summary_total = floatval($para['summary']['total']); # update po header # @@ -871,6 +908,29 @@ class PurchaseOrderAset extends MY_Controller { exit; } + ## UPDATE existing downpayment ## + $sql = "UPDATE supplier_downpayment SET + SupplierDownpaymentAmount = ?, + SupplierDownpaymentDate = ?, + SupplierDownpaymentDueDate = ?, + SupplierDownpaymentStatus = ?, + SupplierDownpaymentLastUpdatedUserID = ? + WHERE SupplierDownpaymentID = ? + AND SupplierDownpaymentIsActive = 'Y'"; + $que = $this->db->query($sql, [ + $summary_downpayment ?: 0.00, + $para['contractStart'], + $para['contractStart'], + 'Draft', + $user['M_UserID'], + $para['SupplierDownpaymentID'] + ]); + if (!$que) { + $this->db->trans_rollback(); + $this->sys_error_db("[Error] failed update supplier downpayment"); + exit; + } + # update status old po summary isActive to 'N' # $sql_active = "UPDATE purchase_order_summary SET PurchaseOrderSummaryIsActive = 'N' @@ -956,7 +1016,7 @@ class PurchaseOrderAset extends MY_Controller { PurchaseOrderDetailQty, PurchaseOrderDetailPrice, PurchaseOrderDetailTotal, - PurchaseOrderWarehouseID, + PurchaseOrderDetailWarehouseID, PurchaseOrderDetailUserID, PurchaseOrderDetailCreatedUserID, PurchaseOrderDetailCreated @@ -1097,6 +1157,21 @@ class PurchaseOrderAset extends MY_Controller { exit; } + /* soft delete supplier_downpayment */ + $sql_deldp = "UPDATE supplier_downpayment SET + SupplierDownpaymentIsActive = 'N', + SupplierDownpaymentLastUpdatedUserID = ? + WHERE SupplierDownpaymentID = ? + AND SupplierDownpaymentIsActive = 'Y'"; + $que_deldp = $this->db->query($sql_deldp, [ + $user['M_UserID'], $para['SupplierDownpaymentID'] + ]); + if (!$que_deldp) { + $this->db->trans_rollback(); + $this->sys_error_db("[Error] failed soft delete supplier downpayment"); + exit; + } + /* soft delete purchase order */ $sql_delorder = "UPDATE purchase_order SET PurchaseOrderIsActive = 'N', @@ -1298,8 +1373,4 @@ class PurchaseOrderAset extends MY_Controller { exit; } } - - private function generateDownPaymentPI() { - - } } diff --git a/application/controllers/mockup/receive-item-po/ReceiveItemPOAsset.php b/application/controllers/mockup/receive-item-po/ReceiveItemPOAsset.php index ffd63de..4affc1e 100644 --- a/application/controllers/mockup/receive-item-po/ReceiveItemPOAsset.php +++ b/application/controllers/mockup/receive-item-po/ReceiveItemPOAsset.php @@ -693,8 +693,9 @@ class ReceiveItemPOAsset extends MY_Controller ReceiveOrderPoNote, ReceiveOrderPoRefNumber, ReceiveOrderPoDONumber, + ReceiveOrderPoTypePurchase, ReceiveOrderPoCreatedUserID - ) VALUE (?,?,?,?,?,?,?,?,?,?,?,?)"; + ) VALUE (?,?,?,?,?,?,?,?,?,?,?,?,?)"; $queInsRO = $this->db->query($sqlInsRO, [ $numGR, $param['supplier'], @@ -707,6 +708,7 @@ class ReceiveItemPOAsset extends MY_Controller $param['catatan'], $param['reference'], $param['reference'], + 'aset', $user['M_UserID'] ]); if (!$queInsRO) { @@ -1465,12 +1467,15 @@ class ReceiveItemPOAsset extends MY_Controller PurchaseOrderID, PurchaseOrderTaxPpnType AS tax_type, PurchaseOrderTaxPercentPpn AS tax_percent, - PurchaseOrderTaxAmountPpn AS tax_amount + PurchaseOrderTaxAmountPpn AS tax_amount, + IFNULL(SupplierDownpaymentAmount, 0) AS downpayment_amount FROM receive_order_po JOIN receive_order_po_detail ON ReceiveOrderPoDetailReceiveOrderPoID = ReceiveOrderPoID AND ReceiveOrderPoDetailIsActive = 'Y' AND ReceiveOrderPoIsActive = 'Y' JOIN purchase_order ON ReceiveOrderPoDetailPurchaseOrderID = PurchaseOrderID + LEFT JOIN supplier_downpayment + ON SupplierDownpaymentPurchasOrderID = PurchaseOrderID WHERE ReceiveOrderPoID = ?"; $que_dataro = $this->db->query($sql_dataro, [$roID]); if (!$que_dataro) { @@ -1510,6 +1515,8 @@ class ReceiveItemPOAsset extends MY_Controller $donumber = $data_ro['ReceiveOrderPoDONumber']; } + $addedtax = round(($grni + $data_ro['tax_amount']), 2); + $grandtotal = round(($addedtax - $data_ro['downpayment_amount']), 2); $sql_insert_pi = "INSERT INTO supplier_invoice ( SupplierInvoiceNumber, SupplierInvoiceReceiveOrderPoID, @@ -1542,8 +1549,8 @@ class ReceiveItemPOAsset extends MY_Controller $diskonPO, $data_ro['tax_percent'], $data_ro['tax_amount'], - $grni, - $grni, + $grandtotal, + $grandtotal, $data_ro['ReceiveOrderPoNote'], $user['M_UserID'], $user['M_UserID'] diff --git a/application/controllers/mockup/receive-item-po/ReceiveItemPoV2.php b/application/controllers/mockup/receive-item-po/ReceiveItemPoV2.php index 4fd47b7..d6ee9fe 100644 --- a/application/controllers/mockup/receive-item-po/ReceiveItemPoV2.php +++ b/application/controllers/mockup/receive-item-po/ReceiveItemPoV2.php @@ -23,7 +23,7 @@ class ReceiveItemPoV2 extends MY_Controller { $payload = $this->sys_input; $search = $payload['search'] . '%'; - $sql = "SELECT + $sql = "SELECT SupplierID, SupplierCode, SupplierName @@ -56,7 +56,7 @@ class ReceiveItemPoV2 extends MY_Controller { $user = $this->sys_user; $const_params = [ - $user['M_BranchID'] ?? 0, + $user['M_BranchID'] ?? 0, $user['S_RegionalID'], $payload['SupplierID'], $payload['search'] . '%' @@ -69,22 +69,22 @@ class ReceiveItemPoV2 extends MY_Controller { $placeholders = implode(", ", array_fill(0, count($localPO_deleted), '?')); $list_includeID = $payload['localPO_deleted']; } - + $params = array_merge($list_includeID, $const_params); - $sql = "SELECT + $sql = "SELECT GROUP_CONCAT(DISTINCT PurchaseOrderDetailWarehouseID) AS arrWarehouse, - purchase_order.* - FROM purchase_order + purchase_order.* + FROM purchase_order JOIN purchase_order_detail ON PurchaseOrderID = PurchaseOrderDetailPurchaseOrderID AND PurchaseOrderDetailIsActive = 'Y' - AND + AND ((SELECT IFNULL(SUM(PoItemReceiveQty - PoItemReceiveQtyReceived), 0) - FROM po_item_receive + FROM po_item_receive WHERE PoItemReceivePurchaseOrderID = PurchaseOrderID) > 0) OR (SELECT PoItemReceiveID - FROM po_item_receive + FROM po_item_receive WHERE PoItemReceivePurchaseOrderID = PurchaseOrderID AND PoItemReceiveID IN ({$placeholders})) JOIN warehouse ON PurchaseOrderDetailWarehouseID = WarehouseID @@ -95,7 +95,7 @@ class ReceiveItemPoV2 extends MY_Controller { AND PurchaseOrderItemCategoryID = 1 AND PurchaseOrderNumber LIKE ? AND PurchaseOrderIsActive = 'Y' - GROUP BY PurchaseOrderID + GROUP BY PurchaseOrderID ORDER BY PurchaseOrderID DESC LIMIT 50"; $que = $this->db->query($sql, $params); if (!$que) { @@ -120,12 +120,12 @@ class ReceiveItemPoV2 extends MY_Controller { $user = $this->sys_user; $whtype = ($user['M_UserLocationFlag'] == 'R') ? 'R' : 'B'; - $sql = "SELECT + $sql = "SELECT WarehouseID, WarehouseCode, CASE - WHEN WarehouseType = 'B' THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', M_BranchName) - WHEN WarehouseType = 'R' THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', S_RegionalName) + WHEN WarehouseType = 'B' THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', M_BranchName) + WHEN WarehouseType = 'R' THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', S_RegionalName) ELSE '' END WarehouseName, WarehouseType, @@ -134,12 +134,12 @@ class ReceiveItemPoV2 extends MY_Controller { WarehouseM_BranchID, S_RegionalID, S_RegionalName - FROM warehouse + FROM warehouse JOIN s_regional ON WarehouseS_RegionalID = S_RegionalID AND S_RegionalIsActive = 'Y' - LEFT JOIN m_branch ON WarehouseM_BranchID = M_BranchID AND M_BranchIsActive = 'Y' - WHERE WarehouseS_RegionalID = ? + LEFT JOIN m_branch ON WarehouseM_BranchID = M_BranchID AND M_BranchIsActive = 'Y' + WHERE WarehouseS_RegionalID = ? AND WarehouseIsActive = 'Y' AND WarehouseIsTransit = 'N' - AND WarehouseType = ? AND WarehouseM_BranchID = ? + AND WarehouseType = ? AND WarehouseM_BranchID = ? ORDER BY WarehouseName ASC"; $que = $this->db->query($sql, [ $user['S_RegionalID'], $whtype, $user['M_BranchID'] @@ -154,7 +154,7 @@ class ReceiveItemPoV2 extends MY_Controller { $message = $exc->getMessage(); $this->sys_error($message); } - } + } function getItem() { try { @@ -169,7 +169,7 @@ class ReceiveItemPoV2 extends MY_Controller { $POID = $param['POID']; $warehouseID = $param['WarehouseID']; - $sql = "SELECT + $sql = "SELECT CONCAT(PoItemReceiveID,',','-') as keyID, PoItemReceiveID, PurchaseOrderNumber, @@ -181,10 +181,10 @@ class ReceiveItemPoV2 extends MY_Controller { WarehouseID, WarehouseCode, CASE - WHEN WarehouseType = 'B' - THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', M_BranchName) - WHEN WarehouseType = 'R' - THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', S_RegionalName) + WHEN WarehouseType = 'B' + THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', M_BranchName) + WHEN WarehouseType = 'R' + THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', S_RegionalName) ELSE '' END WarehouseName, M_ItemID, @@ -199,18 +199,18 @@ class ReceiveItemPoV2 extends MY_Controller { PoItemReceiveQty - PurchaseOrderSummaryQtyReceived AS qtyRest, PoItemReceivePrice, 'N' as alreadySave - FROM po_item_receive - JOIN purchase_order ON PoItemReceivePurchaseOrderID = PurchaseOrderID + FROM po_item_receive + JOIN purchase_order ON PoItemReceivePurchaseOrderID = PurchaseOrderID AND PurchaseOrderIsActive = 'Y' AND PoItemReceiveIsActive = 'Y' -- AND (PoItemReceiveQty - PoItemReceiveQtyReceived) > 0 - JOIN purchase_order_summary ON PurchaseOrderSummaryID = PoItemReceivePurchaseOrderSummaryID - JOIN warehouse ON PoItemReceiveWarehouseID = WarehouseID + JOIN purchase_order_summary ON PurchaseOrderSummaryID = PoItemReceivePurchaseOrderSummaryID + JOIN warehouse ON PoItemReceiveWarehouseID = WarehouseID AND WarehouseIsActive = 'Y' - JOIN m_item ON PoItemReceiveItemID = M_ItemID - JOIN itemunit ON PoItemReceiveItemUnitID= ItemUnitID - JOIN s_regional ON WarehouseS_RegionalID = S_RegionalID - LEFT JOIN m_branch ON WarehouseM_BranchID = M_BranchID + JOIN m_item ON PoItemReceiveItemID = M_ItemID + JOIN itemunit ON PoItemReceiveItemUnitID= ItemUnitID + JOIN s_regional ON WarehouseS_RegionalID = S_RegionalID + LEFT JOIN m_branch ON WarehouseM_BranchID = M_BranchID WHERE PurchaseOrderID = ? AND WarehouseID = ?"; $que = $this->db->query($sql, [$POID, $warehouseID]); if (!$que) { @@ -221,7 +221,7 @@ class ReceiveItemPoV2 extends MY_Controller { foreach ($data as $key => $value) { $data[$key]['batchlist'] = []; } - + $this->sys_ok($data); } catch (Exception $exc) { $message = $exc->getMessage(); @@ -239,8 +239,8 @@ class ReceiveItemPoV2 extends MY_Controller { $param = $this->sys_input; $ROID = $param['ROID']; - $sql = "SELECT - ReceiveOrderPoDetailID, + $sql = "SELECT + ReceiveOrderPoDetailID, CONCAT(ReceiveOrderPoDetailPoItemReceiveID,',',ReceiveOrderPoDetailID) AS keyID, ReceiveOrderPoDetailPoItemReceiveID AS PoItemReceiveID, PurchaseOrderNumber, @@ -252,10 +252,10 @@ class ReceiveItemPoV2 extends MY_Controller { WarehouseID, WarehouseCode, CASE - WHEN WarehouseType = 'B' - THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', M_BranchName) - WHEN WarehouseType = 'R' - THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', S_RegionalName) + WHEN WarehouseType = 'B' + THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', M_BranchName) + WHEN WarehouseType = 'R' + THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', S_RegionalName) ELSE '' END WarehouseName, M_ItemID, @@ -275,17 +275,17 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoDetailBatchNo AS batchNo, ReceiveOrderPoDetailEd AS itemEd, ReceiveOrderPoDetailBatchList - FROM receive_order_po_detail - JOIN receive_order_po ON ReceiveOrderPoDetailReceiveOrderPoID = ReceiveOrderPoID - JOIN purchase_order ON ReceiveOrderPoDetailPurchaseOrderID = PurchaseOrderID + FROM receive_order_po_detail + JOIN receive_order_po ON ReceiveOrderPoDetailReceiveOrderPoID = ReceiveOrderPoID + JOIN purchase_order ON ReceiveOrderPoDetailPurchaseOrderID = PurchaseOrderID JOIN po_item_receive ON ReceiveOrderPoDetailPoItemReceiveID = PoItemReceiveID - JOIN purchase_order_summary ON PurchaseOrderSummaryID = PoItemReceivePurchaseOrderSummaryID - JOIN warehouse ON ReceiveOrderPoWarehouseID = WarehouseID - JOIN m_item ON ReceiveOrderPoItemID = M_ItemID - JOIN itemunit ON ReceiveOrderPoItemUnitID = ItemUnitID - JOIN s_regional ON WarehouseS_RegionalID = S_RegionalID - LEFT JOIN m_branch ON WarehouseM_BranchID = M_BranchID - WHERE ReceiveOrderPoDetailIsActive = 'Y' + JOIN purchase_order_summary ON PurchaseOrderSummaryID = PoItemReceivePurchaseOrderSummaryID + JOIN warehouse ON ReceiveOrderPoWarehouseID = WarehouseID + JOIN m_item ON ReceiveOrderPoItemID = M_ItemID + JOIN itemunit ON ReceiveOrderPoItemUnitID = ItemUnitID + JOIN s_regional ON WarehouseS_RegionalID = S_RegionalID + LEFT JOIN m_branch ON WarehouseM_BranchID = M_BranchID + WHERE ReceiveOrderPoDetailIsActive = 'Y' AND ReceiveOrderPoDetailReceiveOrderPoID = ? ORDER BY ReceiveOrderPoDetailID ASC"; $que = $this->db->query($sql, [$ROID]); @@ -334,8 +334,8 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoInspeksiCatatan AS catatan, ReceiveOrderPoInspeksiStaffPenerimaID AS pemeriksa, ReceiveOrderPoInspeksiPengirim AS pengirim - FROM receive_order_po_inspeksi - WHERE ReceiveOrderPoInspeksiIsActive = 'Y' + FROM receive_order_po_inspeksi + WHERE ReceiveOrderPoInspeksiIsActive = 'Y' AND ReceiveOrderPoInspeksiReceiveOrderPoDetailID IN ({$placeholders})"; $queInspeksi = $this->db->query($sqlInspeksi, $detailID); if (!$queInspeksi) { @@ -394,7 +394,7 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoNumber, ReceiveOrderPoID, ReceiveOrderPoSupplierID, - ReceiveOrderPoIDate, + ReceiveOrderPoIDate, ReceiveOrderShippingCostAmount, ReceiveOrderPoNote, ReceiveOrderPoRefNumber, @@ -404,31 +404,31 @@ class ReceiveItemPoV2 extends MY_Controller { WarehouseID, WarehouseCode, CASE - WHEN WarehouseType = 'B' - THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', M_BranchName) - WHEN WarehouseType = 'R' - THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', S_RegionalName) + WHEN WarehouseType = 'B' + THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', M_BranchName) + WHEN WarehouseType = 'R' + THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', S_RegionalName) ELSE '' END WarehouseName, ReceiveOrderPoConfirmed, DATE_FORMAT(ReceiveOrderPoConfirmedDate, '%d-%m-%Y %H:%i') AS ReceiveOrderPoConfirmedDate, M_UserUsername AS ReceiveOrderPoConfirmedUserName - FROM receive_order_po - JOIN warehouse ON ReceiveOrderPoWarehouseID = WarehouseID - JOIN supplier ON ReceiveOrderPoSupplierID = SupplierID - JOIN s_regional ON WarehouseS_RegionalID = S_RegionalID - AND S_RegionalIsActive = 'Y' - LEFT JOIN m_branch ON WarehouseM_BranchID = M_BranchID + FROM receive_order_po + JOIN warehouse ON ReceiveOrderPoWarehouseID = WarehouseID + JOIN supplier ON ReceiveOrderPoSupplierID = SupplierID + JOIN s_regional ON WarehouseS_RegionalID = S_RegionalID + AND S_RegionalIsActive = 'Y' + LEFT JOIN m_branch ON WarehouseM_BranchID = M_BranchID AND M_BranchIsActive = 'Y' LEFT JOIN m_user ON ReceiveOrderPoConfirmedUserID = M_UserID WHERE ReceiveOrderPoIsActive = 'Y' AND (ReceiveOrderPoNumber LIKE ? OR ReceiveOrderPoDONumber LIKE ?) AND ReceiveOrderPoIDate BETWEEN DATE(?) AND DATE(?) - AND ReceiveOrderPoS_RegionalID = ? - AND ReceiveOrderPoM_BranchCode LIKE ? - AND ReceiveOrderPoID NOT IN ( - SELECT DISTINCT ReceiveOrderPoDetailReceiveOrderPoID - FROM receive_order_po_detail + AND ReceiveOrderPoS_RegionalID = ? + AND ReceiveOrderPoM_BranchCode LIKE ? + AND ReceiveOrderPoID IN ( + SELECT DISTINCT ReceiveOrderPoDetailReceiveOrderPoID + FROM receive_order_po_detail JOIN purchase_order ON ReceiveOrderPoDetailPurchaseOrderID = PurchaseOrderID WHERE PurchaseOrderItemCategoryID = 1 AND ReceiveOrderPoDetailIsActive = 'Y' @@ -445,7 +445,7 @@ class ReceiveItemPoV2 extends MY_Controller { $sqldata = $sql . " LIMIT ? OFFSET ?"; $quedata = $this->db->query($sqldata, [ - $search, $search, $startdate, $enddate, + $search, $search, $startdate, $enddate, $regID, $brnCD, $limit, $page ]); if (!$quedata) { @@ -474,7 +474,7 @@ class ReceiveItemPoV2 extends MY_Controller { $param = $this->sys_input; $RODetailID = $param['RODetailID']; - $sql = "SELECT + $sql = "SELECT ReceiveOrderPoInspeksiID, ReceiveOrderPoInspeksiReceiveOrderPoDetailID, ReceiveOrderPoInspeksiQtyPesan, @@ -490,8 +490,8 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoInspeksiCatatan, ReceiveOrderPoInspeksiStaffPenerima, ReceiveOrderPoInspeksiStaffPengirim - FROM receive_order_po_inspeksi - WHERE ReceiveOrderPoInspeksiIsActive = 'Y' + FROM receive_order_po_inspeksi + WHERE ReceiveOrderPoInspeksiIsActive = 'Y' AND ReceiveOrderPoInspeksiReceiveOrderPoDetailID = ?"; $que = $this->db->query($sql, [$RODetailID]); if (!$que) { @@ -516,7 +516,7 @@ class ReceiveItemPoV2 extends MY_Controller { $user = $this->sys_user; - $sql = "SELECT + $sql = "SELECT M_UserID, M_UserUsername FROM m_user @@ -541,7 +541,7 @@ class ReceiveItemPoV2 extends MY_Controller { $areaid = ($user['loginLevel'] == 'regional') ? $user['S_RegionalID'] : $user['M_BranchID']; $areatype = ($user['loginLevel'] == 'regional') ? 'R' : 'B'; - $sqlusrdivisi = "SELECT M_UserDivisionDivisionID FROM m_userdivision + $sqlusrdivisi = "SELECT M_UserDivisionDivisionID FROM m_userdivision WHERE M_UserDivisionM_UserID = ? AND M_UserDivisionIsActive = 'Y'"; $queusrdivisi = $this->db->query($sqlusrdivisi, [$user['M_UserID']]); if (!$queusrdivisi) { @@ -549,7 +549,7 @@ class ReceiveItemPoV2 extends MY_Controller { $this->sys_error_db("[Error] get user divisi"); exit; } - + $usrdivisiID = $queusrdivisi->row_array()['M_UserDivisionDivisionID']; if (!isset($usrdivisiID)) { $this->db->trans_rollback(); @@ -577,8 +577,8 @@ class ReceiveItemPoV2 extends MY_Controller { private function getPOItemReceived($id) { $sql = "SELECT * FROM po_item_receive - JOIN warehouse ON PoItemReceiveWarehouseID= WarehouseID - JOIN itemunit ON PoItemReceiveItemUnitID = ItemUnitID + JOIN warehouse ON PoItemReceiveWarehouseID= WarehouseID + JOIN itemunit ON PoItemReceiveItemUnitID = ItemUnitID WHERE PoItemReceiveID = ?"; $que = $this->db->query($sql, [$id]); if (!$que) { @@ -599,7 +599,7 @@ class ReceiveItemPoV2 extends MY_Controller { } $ROheader = $que->row_array(); - $sqldetail = "SELECT * FROM receive_order_po_detail + $sqldetail = "SELECT * FROM receive_order_po_detail WHERE ReceiveOrderPoDetailReceiveOrderPoID = ? AND ReceiveOrderPoDetailIsActive = 'Y'"; $quedetail = $this->db->query($sqldetail, [$id]); if (!$quedetail) { @@ -644,9 +644,9 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoCreatedUserID ) VALUE (?,?,?,?,?,?,?,?,?,?,?,?)"; $queInsRO = $this->db->query($sqlInsRO, [ - $numGR, $param['supplierID'], $param['date'], $param['warehouseID'], - $user['M_BranchCode'], $user['S_RegionalID'], $param['shippingCost'], - $param['shippingIsPaid'], $param['note'], $param['refNumber'], + $numGR, $param['supplierID'], $param['date'], $param['warehouseID'], + $user['M_BranchCode'], $user['S_RegionalID'], $param['shippingCost'], + $param['shippingIsPaid'], $param['note'], $param['refNumber'], $param['DONumber'], $user['M_UserID'] ]); if (!$queInsRO) { @@ -719,7 +719,7 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoDetailBatchList ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; $queInsRODetail = $this->db->query($sqlInsRODetail, [ - $roID, $obj['PoItemReceivePurchaseOrderID'], + $roID, $obj['PoItemReceivePurchaseOrderID'], $obj['PoItemReceivePurchaseOrderSummaryID'], $obj['PoItemReceivePurchaseOrderDetailID'], $obj['PoItemReceiveID'], $obj['qty'], $obj['PoItemReceivePrice'], @@ -759,9 +759,9 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoInspeksiUserID ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; $queInspeksi = $this->db->query($sqlInspeksi, [ - $RODetailID, $inspeksi['qty_po'], $inspeksi['qty_ro'], $inspeksi['price_po'], - $inspeksi['price_ro'], $inspeksi['date_po'], $inspeksi['date_ro'], - $inspeksi['expire_date'], $inspeksi['condt_kemasan'], $inspeksi['catatan_kemasan'], + $RODetailID, $inspeksi['qty_po'], $inspeksi['qty_ro'], $inspeksi['price_po'], + $inspeksi['price_ro'], $inspeksi['date_po'], $inspeksi['date_ro'], + $inspeksi['expire_date'], $inspeksi['condt_kemasan'], $inspeksi['catatan_kemasan'], $inspeksi['condt_pengiriman'], $inspeksi['catatan_kirim'], $inspeksi['summary'], $inspeksi['catatan'], $inspeksi['pemeriksa'], $inspeksi['pengirim'], $user['M_UserID'] ]); @@ -774,7 +774,7 @@ class ReceiveItemPoV2 extends MY_Controller { # po item received && log item change # $jsonBefore = $this->getPOItemReceived($obj['PoItemReceiveID']); $sqlupd = "UPDATE po_item_receive SET - PoItemReceiveQtyReceived = ?, + PoItemReceiveQtyReceived = ?, PoItemReceiveUpdatedUserID = ? WHERE PoItemReceiveID = ?"; $queupd = $this->db->query($sqlupd, [ @@ -790,7 +790,7 @@ class ReceiveItemPoV2 extends MY_Controller { $jsonAfter = $this->getPOItemReceived($obj['PoItemReceiveID']); $desc = "Diterima {$obj['M_ItemDesc']} sejumlah {$obj['qty']} {$obj['ItemUnitName']} di RO nomor {$numGR} *RO belum di konfirmasi"; $this->insertLogPoItemReceive( - $obj['PoItemReceiveID'], 'UPDATE', $roID, + $obj['PoItemReceiveID'], 'UPDATE', $roID, $jsonBefore, $jsonAfter, $user['M_UserID'], $desc ); } @@ -806,7 +806,7 @@ class ReceiveItemPoV2 extends MY_Controller { $this->sys_error($message); } } - + function saveEditRO() { try { if (!$this->isLogin) { @@ -832,8 +832,8 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoDONumber = ? WHERE ReceiveOrderPoID = ?"; $que_ro = $this->db->query($sql_ro, [ - $param['numGR'], $param['supplierID'], $param['date'], $param['warehouseID'], - $user['M_BranchCode'], $user['S_RegionalID'], $param['shippingCost'], $param['shippingIsPaid'], + $param['numGR'], $param['supplierID'], $param['date'], $param['warehouseID'], + $user['M_BranchCode'], $user['S_RegionalID'], $param['shippingCost'], $param['shippingIsPaid'], $param['note'], $param['refNumber'], $param['DONumber'], $param['ReceiveOrderID'] ]); if (!$que_ro) { @@ -843,9 +843,9 @@ class ReceiveItemPoV2 extends MY_Controller { } # CHECK IF DETAIL ALREADY IN TABLE RECEIVE # - $sql_check_ro = "SELECT ReceiveOrderPoDetailID, ReceiveOrderPoDetailPoItemReceiveID - FROM receive_order_po_detail - WHERE ReceiveOrderPoDetailReceiveOrderPoID = ? + $sql_check_ro = "SELECT ReceiveOrderPoDetailID, ReceiveOrderPoDetailPoItemReceiveID + FROM receive_order_po_detail + WHERE ReceiveOrderPoDetailReceiveOrderPoID = ? AND ReceiveOrderPoDetailIsActive = 'Y'"; $que_check_ro = $this->db->query($sql_check_ro, [$param['ReceiveOrderID']]); if (!$que_check_ro) { @@ -860,7 +860,7 @@ class ReceiveItemPoV2 extends MY_Controller { $update = []; $insert = []; $delete = []; - + foreach ($param['detail'] as $key => $obj) { if (!isset($obj['ReceiveOrderPoDetailID']) ) { $insert[] = $obj; @@ -901,15 +901,15 @@ class ReceiveItemPoV2 extends MY_Controller { } # UPDATE po item receive # - $sql_up_po_receive = "UPDATE po_item_receive + $sql_up_po_receive = "UPDATE po_item_receive JOIN receive_order_po_detail ON ReceiveOrderPoDetailPoItemReceiveID = PoItemReceiveID AND ReceiveOrderPoDetailID = ? - SET - PoItemReceiveQtyReceived = (PoItemReceiveQtyReceived - ReceiveOrderPoDetailQty) + ?, + SET + PoItemReceiveQtyReceived = (PoItemReceiveQtyReceived - ReceiveOrderPoDetailQty) + ?, PoItemReceiveUpdatedUserID = ? WHERE PoItemReceiveID = ?"; $que_up_po_receive = $this->db->query($sql_up_po_receive, [ - $obj['ReceiveOrderPoDetailID'], $obj['qty'], + $obj['ReceiveOrderPoDetailID'], $obj['qty'], $user['M_UserID'], $obj['PoItemReceiveID'] ]); if (!$que_up_po_receive) { @@ -934,7 +934,7 @@ class ReceiveItemPoV2 extends MY_Controller { } } - $sql_update_detail = "UPDATE receive_order_po_detail SET + $sql_update_detail = "UPDATE receive_order_po_detail SET ReceiveOrderPoDetailReceiveOrderPoID = ?, ReceiveOrderPoDetailPurchaseOrderID = ?, ReceiveOrderPoDetailPurchaseOrderSummaryID = ?, @@ -950,12 +950,12 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoDetailBatchList = ? WHERE ReceiveOrderPoDetailID = ?"; $que_update_detail = $this->db->query($sql_update_detail, [ - $param['ReceiveOrderID'], $obj['PoItemReceivePurchaseOrderID'], - $obj['PoItemReceivePurchaseOrderSummaryID'], - $obj['PoItemReceivePurchaseOrderDetailID'], - $obj['PoItemReceiveID'], $obj['qty'], $obj['PoItemReceivePrice'], - (doubleval($obj['PoItemReceivePrice']) * intval($obj['qty'])), - $obj['M_ItemID'], $obj['ItemUnitID'], $obj['batchNo'], + $param['ReceiveOrderID'], $obj['PoItemReceivePurchaseOrderID'], + $obj['PoItemReceivePurchaseOrderSummaryID'], + $obj['PoItemReceivePurchaseOrderDetailID'], + $obj['PoItemReceiveID'], $obj['qty'], $obj['PoItemReceivePrice'], + (doubleval($obj['PoItemReceivePrice']) * intval($obj['qty'])), + $obj['M_ItemID'], $obj['ItemUnitID'], $obj['batchNo'], $obj['itemEd'], $jsonBatch, $obj['ReceiveOrderPoDetailID'] ]); if (!$que_update_detail) { @@ -966,7 +966,7 @@ class ReceiveItemPoV2 extends MY_Controller { # update form inspeksi item # $inspeksi = $obj['inspeksi_item']; - $sql_inspeksi = "UPDATE receive_order_po_inspeksi SET + $sql_inspeksi = "UPDATE receive_order_po_inspeksi SET ReceiveOrderPoInspeksiQtyPesan = ?, ReceiveOrderPoInspeksiQtyActual = ?, ReceiveOrderPoInspeksiPricePesan = ?, @@ -984,11 +984,11 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoInspeksiPengirim = ? WHERE ReceiveOrderPoInspeksiID = ?"; $que_inspeksi = $this->db->query($sql_inspeksi, [ - $inspeksi['qty_po'], $inspeksi['qty_ro'], $inspeksi['price_po'], - $inspeksi['price_ro'], $inspeksi['date_po'], $inspeksi['date_ro'], - $inspeksi['expire_date'], $inspeksi['condt_kemasan'], - $inspeksi['catatan_kemasan'], $inspeksi['condt_pengiriman'], - $inspeksi['catatan_kirim'], $inspeksi['summary'], $inspeksi['catatan'], + $inspeksi['qty_po'], $inspeksi['qty_ro'], $inspeksi['price_po'], + $inspeksi['price_ro'], $inspeksi['date_po'], $inspeksi['date_ro'], + $inspeksi['expire_date'], $inspeksi['condt_kemasan'], + $inspeksi['catatan_kemasan'], $inspeksi['condt_pengiriman'], + $inspeksi['catatan_kirim'], $inspeksi['summary'], $inspeksi['catatan'], $inspeksi['pemeriksa'], $inspeksi['pemeriksa'], $inspeksi['ReceiveOrderPoInspeksiID'] ]); if (!$que_inspeksi) { @@ -1022,8 +1022,8 @@ class ReceiveItemPoV2 extends MY_Controller { } # UPDATE po item receive # - $sql_up_po_receive = "UPDATE po_item_receive SET - PoItemReceiveQtyReceived = ?, + $sql_up_po_receive = "UPDATE po_item_receive SET + PoItemReceiveQtyReceived = ?, PoItemReceiveUpdatedUserID = ? WHERE PoItemReceiveID = ?"; $que_up_po_receive = $this->db->query($sql_up_po_receive, [ @@ -1069,10 +1069,10 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoDetailBatchList ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; $que_insert_rodetail = $this->db->query($sql_insert_rodetail, [ - $param['ReceiveOrderID'], $obj['PoItemReceivePurchaseOrderID'], + $param['ReceiveOrderID'], $obj['PoItemReceivePurchaseOrderID'], $obj['PoItemReceivePurchaseOrderSummaryID'], $obj['PoItemReceivePurchaseOrderDetailID'], - $obj['PoItemReceiveID'], $obj['qty'], $obj['PoItemReceivePrice'], + $obj['PoItemReceiveID'], $obj['qty'], $obj['PoItemReceivePrice'], (doubleval($obj['PoItemReceivePrice']) * intval($obj['qty'])), $user['M_UserID'], $obj['M_ItemID'], $obj['ItemUnitID'], $obj['batchNo'], $obj['itemEd'], $jsonBatch @@ -1106,11 +1106,11 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoInspeksiUserID ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; $que_insert_inspeksi = $this->db->query($sql_insert_inspeksi, [ - $ro_detail_ID, $inspekitem['qty_po'], $inspekitem['qty_ro'], - $inspekitem['price_po'], $inspekitem['price_ro'], $inspekitem['date_po'], - $inspekitem['date_ro'], $inspekitem['expire_date'], $inspekitem['condt_kemasan'], - $inspekitem['catatan_kemasan'], $inspekitem['condt_pengiriman'], - $inspekitem['catatan_kirim'], $inspekitem['summary'], $inspekitem['catatan'], + $ro_detail_ID, $inspekitem['qty_po'], $inspekitem['qty_ro'], + $inspekitem['price_po'], $inspekitem['price_ro'], $inspekitem['date_po'], + $inspekitem['date_ro'], $inspekitem['expire_date'], $inspekitem['condt_kemasan'], + $inspekitem['catatan_kemasan'], $inspekitem['condt_pengiriman'], + $inspekitem['catatan_kirim'], $inspekitem['summary'], $inspekitem['catatan'], $inspekitem['pemeriksa'], $inspekitem['pengirim'], $user['M_UserID'] ]); if (!$que_insert_inspeksi) { @@ -1127,13 +1127,13 @@ class ReceiveItemPoV2 extends MY_Controller { $deleted_poid = array_column($delete , 'ReceiveOrderPoDetailPoItemReceiveID'); $placeholders_po = implode(', ', array_fill(0, count($deleted_poid), '?')); - + # UPDATE po item receive # $del_param = array_merge(array($user['M_UserID']), $deleted_poid, $deleted_id); - $sql_up_po_receive = "UPDATE po_item_receive + $sql_up_po_receive = "UPDATE po_item_receive JOIN receive_order_po_detail ON ReceiveOrderPoDetailPoItemReceiveID = PoItemReceiveID - SET PoItemReceiveQtyReceived = (PoItemReceiveQtyReceived - ReceiveOrderPoDetailQty), + SET PoItemReceiveQtyReceived = (PoItemReceiveQtyReceived - ReceiveOrderPoDetailQty), PoItemReceiveUpdatedUserID = ? WHERE PoItemReceiveID IN ($placeholders) AND ReceiveOrderPoDetailID IN ($placeholders_po)"; @@ -1143,11 +1143,11 @@ class ReceiveItemPoV2 extends MY_Controller { $this->sys_error_db("[Error] update receive po item"); exit; } - + # UPDATE receive order po detail # $upd_param = array_merge(array($user['M_UserID']), $deleted_id); - $sql_update_detail = "UPDATE receive_order_po_detail SET - ReceiveOrderPoDetailIsActive = 'N', + $sql_update_detail = "UPDATE receive_order_po_detail SET + ReceiveOrderPoDetailIsActive = 'N', ReceiveOrderPoDetailDeleted = NOW(), ReceiveOrderPoDetailDeletedUserID = ? WHERE ReceiveOrderPoDetailID IN ($placeholders)"; @@ -1168,7 +1168,7 @@ class ReceiveItemPoV2 extends MY_Controller { $placeholders_inspeksi = implode(', ', array_fill(0, count($deleted_inspeksi), '?')); $delinspeksi_param = array_merge($user['M_UserID'], $deleted_inspeksi); - $sql_inspeksi_status = "UPDATE receive_order_po_inspeksi SET + $sql_inspeksi_status = "UPDATE receive_order_po_inspeksi SET ReceiveOrderPoInspeksiIsActive = 'N', ReceiveOrderPoInspeksiUserID = ?, ReceiveOrderPoInspeksiDeleted = NOW() @@ -1203,22 +1203,22 @@ class ReceiveItemPoV2 extends MY_Controller { # GET DATA RO # $json_ro_before = $this->getDataROPO($ROID); - $sql_ro = "SELECT + $sql_ro = "SELECT receive_order_po.*, DATE_FORMAT(ReceiveOrderPoIDate, '%d-%m-%Y') AS formatedDate, CASE - WHEN WarehouseType = 'B' - THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', M_BranchName) - WHEN WarehouseType = 'R' - THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', S_RegionalName) + WHEN WarehouseType = 'B' + THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', M_BranchName) + WHEN WarehouseType = 'R' + THEN CONCAT(WarehouseCode,' ', WarehouseName, ' - ', S_RegionalName) ELSE '' END WarehouseName, WarehouseID FROM receive_order_po - JOIN warehouse ON ReceiveOrderPoWarehouseID = WarehouseID - JOIN s_regional ON WarehouseS_RegionalID = S_RegionalID + JOIN warehouse ON ReceiveOrderPoWarehouseID = WarehouseID + JOIN s_regional ON WarehouseS_RegionalID = S_RegionalID AND S_RegionalIsActive = 'Y' - LEFT JOIN m_branch ON WarehouseM_BranchID = M_BranchID + LEFT JOIN m_branch ON WarehouseM_BranchID = M_BranchID AND M_BranchIsActive = 'Y' WHERE ReceiveOrderPoID = ?"; $que_ro = $this->db->query($sql_ro, [$ROID]); @@ -1234,14 +1234,14 @@ class ReceiveItemPoV2 extends MY_Controller { exit; } - $sql_detail_ro = "SELECT + $sql_detail_ro = "SELECT receive_order_po_detail.*, - PurchaseOrderID, + PurchaseOrderID, PurchaseOrderNumber, PurchaseOrderTaxPercentPph, PurchaseOrderTaxPercentPpn, PurchaseOrderSummaryDiscountAmount as DiscountPerItem, - PurchaseOrderSummaryQty as QtyAllPO, + PurchaseOrderSummaryQty as QtyAllPO, PurchaseOrderSummaryTotal as TotalAllPO, PurchaseOrderDiscountPercent AS DiscPOPercent, PurchaseOrderDiscountAmount AS DiscPORupiah @@ -1249,7 +1249,7 @@ class ReceiveItemPoV2 extends MY_Controller { JOIN purchase_order ON ReceiveOrderPoDetailPurchaseOrderID = PurchaseOrderID JOIN purchase_order_summary ON ReceiveOrderPoDetailPurchaseOrderSummaryID = PurchaseOrderSummaryID AND PurchaseOrderSummaryIsActive = 'Y' - WHERE ReceiveOrderPoDetailReceiveOrderPoID = ? + WHERE ReceiveOrderPoDetailReceiveOrderPoID = ? AND ReceiveOrderPoDetailIsActive='Y' GROUP BY ReceiveOrderPoDetailID"; $que_detail_ro = $this->db->query($sql_detail_ro, [$ROID]); @@ -1261,7 +1261,7 @@ class ReceiveItemPoV2 extends MY_Controller { $data_detail_ro = $que_detail_ro->result_array(); # UPDATE STATUS RO # - $sql_update_ro = "UPDATE receive_order_po SET + $sql_update_ro = "UPDATE receive_order_po SET ReceiveOrderPoConfirmed = 'Y', ReceiveOrderPoConfirmedDate = NOW(), ReceiveOrderPoConfirmedUserID = ? @@ -1284,7 +1284,7 @@ class ReceiveItemPoV2 extends MY_Controller { $jurnal_number = $que_jrnl_num->row_array()['numbering']; $sql_periode = "SELECT periodeID FROM periode - WHERE DATE(NOW()) BETWEEN periodeStartDate AND periodeEndDate + WHERE DATE(NOW()) BETWEEN periodeStartDate AND periodeEndDate AND periodeIsActive= 'Y' AND periodeIsClosed = 'N'"; $que_periode = $this->db->query($sql_periode, []); if (!$que_periode) { @@ -1333,8 +1333,8 @@ class ReceiveItemPoV2 extends MY_Controller { jurnalM_UserID ) VALUES (?,?,?,?,?,?,?,NOW(),?,?)"; $que_head_jurnal = $this->db->query($sql_head_jurnal, [ - $user['M_BranchCompanyID'], $user['S_RegionalID'], $user['M_BranchCode'], - $jurnal_periode, $jurnal_number, $jurnal_title, $jurnal_desc, $jurnal_type, $user['M_UserID'] + $user['M_BranchCompanyID'], $user['S_RegionalID'], $user['M_BranchCode'], + $jurnal_periode, $jurnal_number, $jurnal_title, $jurnal_desc, $jurnal_type, $user['M_UserID'] ]); if (!$que_head_jurnal) { $this->sys_error_db("[Error] insert header jurnal", $this->db); @@ -1350,8 +1350,8 @@ class ReceiveItemPoV2 extends MY_Controller { foreach ($data_detail_ro as $key => $detail) { # UPDATE PO Summary # - $sql_posummary = "UPDATE purchase_order_summary SET - PurchaseOrderSummaryQtyReceived = PurchaseOrderSummaryQtyReceived + ? + $sql_posummary = "UPDATE purchase_order_summary SET + PurchaseOrderSummaryQtyReceived = PurchaseOrderSummaryQtyReceived + ? WHERE PurchaseOrderSummaryIsActive = 'Y' AND PurchaseOrderSummaryID = ?"; $que_posummary = $this->db->query($sql_posummary, [ @@ -1388,8 +1388,8 @@ class ReceiveItemPoV2 extends MY_Controller { exit; } - $sql_item_group = "SELECT * FROM map_nat_subgroup - WHERE MapNatSub_NatGroupID = ? + $sql_item_group = "SELECT * FROM map_nat_subgroup + WHERE MapNatSub_NatGroupID = ? AND MapNatSub_NatSubGroupID = ? AND MapNatSub_IsActive = 'Y'"; $que_item_group = $this->db->query($sql_item_group, [ @@ -1402,8 +1402,8 @@ class ReceiveItemPoV2 extends MY_Controller { exit; } $item_group = $que_item_group->row_array(); - if (empty($item_group['MapNatSub_PersediaanCoaID']) || - empty($item_group['MapNatSub_PersediaanCoaAccountNo']) || + if (empty($item_group['MapNatSub_PersediaanCoaID']) || + empty($item_group['MapNatSub_PersediaanCoaAccountNo']) || empty($item_group['MapNatSub_PersediaanCoaDescription']) ) { $this->db->trans_rollback(); @@ -1424,18 +1424,18 @@ class ReceiveItemPoV2 extends MY_Controller { jurnalTxM_UserID ) VALUES (?,?,?,?,?,?)"; $que_tx_jurnal = $this->db->query($sql_tx_jurnal, [ - $jurnal_ID, $item_group['MapNatSub_PersediaanCoaID'], + $jurnal_ID, $item_group['MapNatSub_PersediaanCoaID'], $item_group['MapNatSub_PersediaanCoaDescription'], $debet, 0, $user['M_UserID'] ]); if (!$que_tx_jurnal) { $this->db->trans_rollback(); - $this->sys_error_db("[Error] failed insert jurnal tx {$item_cek['M_ItemDesc']} + $this->sys_error_db("[Error] failed insert jurnal tx {$item_cek['M_ItemDesc']} PO {$detail['PurchaseOrderNumber']}"); exit; } $juranl_tx_ID = $this->db->insert_id(); - $each_desc[] = "- Add detail Jurnal debet {$item_cek['M_ItemDesc']} + $each_desc[] = "- Add detail Jurnal debet {$item_cek['M_ItemDesc']} {$item_group['MapNatSub_PersediaanCoaDescription']} sejumlah {$this->formatRupiah($debet)}"; # INSERT JURNAL ADDON # @@ -1452,17 +1452,17 @@ class ReceiveItemPoV2 extends MY_Controller { ]); if (!$que_addon_jurnal) { $this->db->trans_rollback(); - $this->sys_error_db("[Error] failed insert jurnal addon {$item_cek['M_ItemDesc']} + $this->sys_error_db("[Error] failed insert jurnal addon {$item_cek['M_ItemDesc']} PO {$detail['PurchaseOrderNumber']}"); exit; } # INSERT JURNAL TX KREDIT DISKON # - $sql_po = "SELECT + $sql_po = "SELECT SUM(subquery.PurchaseOrderSummaryQty) as totalAllQtyPO, SUM(subquery.PurchaseOrderSummaryTotal) as totalAllPricePO FROM ( - SELECT + SELECT PurchaseOrderSummaryQty, PurchaseOrderSummaryTotal FROM receive_order_po_detail @@ -1509,12 +1509,12 @@ class ReceiveItemPoV2 extends MY_Controller { continue; } $que_tx_jurnal = $this->db->query($sql_tx_jurnal, [ - $jurnal_ID, $coa_diskon['coaID'], $coa_diskon['coaDescription'], + $jurnal_ID, $coa_diskon['coaID'], $coa_diskon['coaDescription'], 0, $value, $user['M_UserID'] ]); if (!$que_tx_jurnal) { $this->db->trans_rollback(); - $this->sys_error_db("[Error] failed insert jurnal tx diskon + $this->sys_error_db("[Error] failed insert jurnal tx diskon item {$item_cek['M_ItemDesc']} PO {$detail['PurchaseOrderNumber']}"); exit; } @@ -1524,7 +1524,7 @@ class ReceiveItemPoV2 extends MY_Controller { ]); if (!$que_addon_jurnal) { $this->db->trans_rollback(); - $this->sys_error_db("[Error] failed insert jurnal addon diskon + $this->sys_error_db("[Error] failed insert jurnal addon diskon item {$item_cek['M_ItemDesc']} PO {$detail['PurchaseOrderNumber']}"); exit; } @@ -1539,7 +1539,7 @@ class ReceiveItemPoV2 extends MY_Controller { ]); if (!$que_save_diskon) { $this->db->trans_rollback(); - $this->sys_error_db("[Error] save diskon prorata + $this->sys_error_db("[Error] save diskon prorata item {$item_cek['M_ItemDesc']} PO {$detail['PurchaseOrderNumber']}"); exit; } @@ -1548,9 +1548,9 @@ class ReceiveItemPoV2 extends MY_Controller { $this->sys_error_db("[Error] BE Jurnal tipe selain Persediaan belum di implementasi"); exit; } - + # HANDLE STOCK UPDATE # - // decode batchlist item diterima + // decode batchlist item diterima $json_batch_item = $detail['ReceiveOrderPoDetailBatchList']; $batch_list = []; if (!empty($json_batch_item)) { @@ -1566,11 +1566,11 @@ class ReceiveItemPoV2 extends MY_Controller { // update stock based on batch foreach ($batch_list as $key => $batch) { - $sql_cek_stock = "SELECT * FROM stock - WHERE StockItemID = ? AND StockItemUnitID = ? + $sql_cek_stock = "SELECT * FROM stock + WHERE StockItemID = ? AND StockItemUnitID = ? AND StockWarehouseID = ? AND StockBatchNo = ?"; $que_cek_stock = $this->db->query($sql_cek_stock, [ - $detail['ReceiveOrderPoItemID'], $detail['ReceiveOrderPoItemUnitID'], + $detail['ReceiveOrderPoItemID'], $detail['ReceiveOrderPoItemUnitID'], $data_ro['WarehouseID'], $batch['batchno'] ]); if (!$que_cek_stock) { @@ -1585,21 +1585,21 @@ class ReceiveItemPoV2 extends MY_Controller { $stock_qty_end = 0; if (!empty($stock_exist)) { - // calc new prive average + // calc new prive average $stock_qty_end = $stock_qty_ori + intval($batch['qty']); $all_price = ($stock_qty_ori * $stock_price) + (intval($batch['qty']) * doubleval($detail['ReceiveOrderPoDetailPrice'])); $avg_prive = round($all_price / $stock_qty_end, 2); - $sql_update_stock = "UPDATE stock SET + $sql_update_stock = "UPDATE stock SET StockItemPrice = ?, StockQty = ?, StockBatchNo = ?, - StockED = ?, + StockED = ?, StockLastUpdated = NOW(), StockUserID = ? WHERE StockID = ?"; $que_update_stock = $this->db->query($sql_update_stock, [ - $avg_prive, $stock_qty_end, $batch['batchno'], + $avg_prive, $stock_qty_end, $batch['batchno'], $batch['expiredate'], $user['M_UserID'], $stock_ID ]); if (!$que_update_stock) { @@ -1623,8 +1623,8 @@ class ReceiveItemPoV2 extends MY_Controller { StockUserID ) VALUES (0,0,?,fn_numbering('SN'),?,?,?,?,?,?,NOW(),?)"; $que_insert_stock = $this->db->query($sql_insert_stock, [ - $data_ro['WarehouseID'], $batch['batchno'], $detail['ReceiveOrderPoItemID'], - $detail['ReceiveOrderPoItemUnitID'], $batch['expiredate'], + $data_ro['WarehouseID'], $batch['batchno'], $detail['ReceiveOrderPoItemID'], + $detail['ReceiveOrderPoItemUnitID'], $batch['expiredate'], $detail['ReceiveOrderPoDetailPrice'], $batch['qty'], $user['M_UserID'] ]); if (!$que_insert_stock) { @@ -1655,8 +1655,8 @@ class ReceiveItemPoV2 extends MY_Controller { StockCardUserID ) VALUES (?, NOW(), ?, ?, ?, ?, ?, 'PRV', ?, ?, 0, ?, ?)"; $que_stock_card = $this->db->query($sql_stock_card, [ - $data_ro['WarehouseID'], $detail['ReceiveOrderPoItemID'], $detail['ReceiveOrderPoItemUnitID'], - $batch['batchno'], $batch['expiredate'], $stock_ID, $stock_qty_ori, $batch['qty'], + $data_ro['WarehouseID'], $detail['ReceiveOrderPoItemID'], $detail['ReceiveOrderPoItemUnitID'], + $batch['batchno'], $batch['expiredate'], $stock_ID, $stock_qty_ori, $batch['qty'], $stock_qty_end, $user['M_UserID'] ]); if (!$que_stock_card) { @@ -1689,8 +1689,8 @@ class ReceiveItemPoV2 extends MY_Controller { StockLogUserID ) VALUES (?, NOW(), ?, ?, ?, ?, ?, ?, 'PRV', ?, ?)"; $que_stock_log = $this->db->query($sql_stock_log, [ - $data_ro['WarehouseID'], $detail['ReceiveOrderPoItemID'], $detail['ReceiveOrderPoItemUnitID'], - $stock_latest['StockStockNumber'], $stock_latest['StockBatchNo'], $stock_latest['StockED'], + $data_ro['WarehouseID'], $detail['ReceiveOrderPoItemID'], $detail['ReceiveOrderPoItemUnitID'], + $stock_latest['StockStockNumber'], $stock_latest['StockBatchNo'], $stock_latest['StockED'], $stock_ID, $batch['qty'], $user['M_UserID'] ]); if (!$que_stock_log) { @@ -1711,7 +1711,7 @@ class ReceiveItemPoV2 extends MY_Controller { jurnalTxDebit, jurnalTxCredit, jurnalTxM_UserID - ) SELECT ?, coaID, coaDescription, ?, 0, ? + ) SELECT ?, coaID, coaDescription, ?, 0, ? FROM coa WHERE coaAccountNo = '5320700001' LIMIT 1"; $que_tx_ship = $this->db->query($sql_tx_ship, [$jurnal_ID, $ship_cost, $user['M_UserID']]); if (!$que_tx_ship) { @@ -1730,7 +1730,7 @@ class ReceiveItemPoV2 extends MY_Controller { jurnalTxDebit, jurnalTxCredit, jurnalTxM_UserID - ) SELECT ?, coaID, coaDescription, 0, ?, ? + ) SELECT ?, coaID, coaDescription, 0, ?, ? FROM coa WHERE coaAccountNo = '2110100030' LIMIT 1"; $que_grni = $this->db->query($sql_grni, [$jurnal_ID, $total_grni, $user['M_UserID']]); if (!$que_grni) { @@ -1765,7 +1765,7 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoShippingStatus, ReceiveOrderPoShippingIsActive, ReceiveOrderPoShippingCreatedUserID - ) SELECT + ) SELECT PurchaseOrderID, {$ROID}, PurchaseOrderShippingCost, @@ -1774,11 +1774,11 @@ class ReceiveItemPoV2 extends MY_Controller { {$user['M_UserID']} FROM purchase_order WHERE PurchaseOrderID IN ( - SELECT DISTINCT ReceiveOrderPoDetailPurchaseOrderID + SELECT DISTINCT ReceiveOrderPoDetailPurchaseOrderID FROM receive_order_po JOIN receive_order_po_detail ON ReceiveOrderPoDetailReceiveOrderPoID = ReceiveOrderPoID WHERE ReceiveOrderPoID = {$user['M_UserID']} - ) + ) AND PurchaseOrderShippingCost IS NOT NULL AND PurchaseOrderShippingCost > 0"; $que_rec_ship = $this->db->query($sql_rec_ship, []); @@ -1789,10 +1789,10 @@ class ReceiveItemPoV2 extends MY_Controller { } # LOG # - $desc = "Konfirmasi penerimaan barang {$data_ro['ReceiveOrderPoNumber']} Tanggal {$data_ro['formatedDate']} + $desc = "Konfirmasi penerimaan barang {$data_ro['ReceiveOrderPoNumber']} Tanggal {$data_ro['formatedDate']} di {$data_ro['WarehouseName']} yang meliputi beberapa item dari PO {$str_po}"; $log_desc = $desc . "\n" . implode("\n", $each_desc); - + $json_ro_after = $this->getDataROPO($ROID); $this->insertLogReceiveOrderPo($ROID, 'CONFIRM', $json_ro_before, $json_ro_after, $user['M_UserID'], $log_desc); $this->insertUserActivty($ROID, 'RO', 'CONFIRM', $user['M_UserID'], $log_desc); @@ -1825,7 +1825,7 @@ class ReceiveItemPoV2 extends MY_Controller { $json_ro_before = $this->getDataROPO($ROID); # update status header RO # - $sql = "UPDATE receive_order_po SET + $sql = "UPDATE receive_order_po SET ReceiveOrderPoIsActive = 'N', ReceiveOrderPoDeleted = NOW(), ReceiveOrderPoDeletedUserID = ? @@ -1837,8 +1837,8 @@ class ReceiveItemPoV2 extends MY_Controller { exit; } - $sql_detail = "SELECT * FROM receive_order_po_detail - WHERE ReceiveOrderPoDetailReceiveOrderPoID = ? + $sql_detail = "SELECT * FROM receive_order_po_detail + WHERE ReceiveOrderPoDetailReceiveOrderPoID = ? AND ReceiveOrderPoDetailIsActive = 'Y'"; $que_detail = $this->db->query($sql_detail, [$ROID]); if (!$que_detail) { @@ -1851,10 +1851,10 @@ class ReceiveItemPoV2 extends MY_Controller { # update status detail RO # foreach ($details as $key => $detail) { # update receive detail po # - $sql_update_detail = "UPDATE receive_order_po_detail SET - ReceiveOrderPoDetailIsActive = 'N', - ReceiveOrderPoDetailDeletedUserID = ?, - ReceiveOrderPoDetailDeleted = NOW() + $sql_update_detail = "UPDATE receive_order_po_detail SET + ReceiveOrderPoDetailIsActive = 'N', + ReceiveOrderPoDetailDeletedUserID = ?, + ReceiveOrderPoDetailDeleted = NOW() WHERE ReceiveOrderPoDetailID = ?"; $que_update_detail = $this->db->query($sql_update_detail, [ $user['M_UserID'], @@ -1867,7 +1867,7 @@ class ReceiveItemPoV2 extends MY_Controller { } # update status form inspeksi # - $sql_inspeksi = "UPDATE receive_order_po_inspeksi SET + $sql_inspeksi = "UPDATE receive_order_po_inspeksi SET ReceiveOrderPoInspeksiIsActive = 'N' WHERE ReceiveOrderPoInspeksiReceiveOrderPoDetailID = ? "; $que_inspeksi = $this->db->query($sql_inspeksi, [$detail['ReceiveOrderPoDetailID']]); @@ -1879,15 +1879,15 @@ class ReceiveItemPoV2 extends MY_Controller { # update po item receive # $json_po_before = $this->getPOItemReceived($detail['ReceiveOrderPoDetailPoItemReceiveID']); - $sql_upd_po_received = "UPDATE po_item_receive - JOIN receive_order_po_detail ON PoItemReceiveID = ReceiveOrderPoDetailPoItemReceiveID - AND ReceiveOrderPoDetailReceiveOrderPoID = ? - AND ReceiveOrderPoDetailID = ? - SET PoItemReceiveQtyReceived = PoItemReceiveQtyReceived - ReceiveOrderPoDetailQty, - PoItemReceiveUpdatedUserID = ? + $sql_upd_po_received = "UPDATE po_item_receive + JOIN receive_order_po_detail ON PoItemReceiveID = ReceiveOrderPoDetailPoItemReceiveID + AND ReceiveOrderPoDetailReceiveOrderPoID = ? + AND ReceiveOrderPoDetailID = ? + SET PoItemReceiveQtyReceived = PoItemReceiveQtyReceived - ReceiveOrderPoDetailQty, + PoItemReceiveUpdatedUserID = ? WHERE PoItemReceiveID = ?"; $que_upd_po_received = $this->db->query($sql_upd_po_received, [ - $ROID, $detail['ReceiveOrderPoDetailID'], $user['M_UserID'], + $ROID, $detail['ReceiveOrderPoDetailID'], $user['M_UserID'], $detail['ReceiveOrderPoDetailPoItemReceiveID'] ]); if (!$que_upd_po_received) { @@ -1896,21 +1896,21 @@ class ReceiveItemPoV2 extends MY_Controller { exit; } $json_po_after = $this->getPOItemReceived($detail['ReceiveOrderPoDetailPoItemReceiveID']); - $descItem = "Dihapus {$detail['M_ItemDesc']} sejumlah {$detail['ReceiveOrderPoDetailQty']} - {$detail['ItemUnitName']} di RO nomor {$json_ro_before['ReceiveOrderPoNumber']} + $descItem = "Dihapus {$detail['M_ItemDesc']} sejumlah {$detail['ReceiveOrderPoDetailQty']} + {$detail['ItemUnitName']} di RO nomor {$json_ro_before['ReceiveOrderPoNumber']} *RO belum di konfirmasi"; # log # $this->insertLogPoItemReceive( $detail['ReceiveOrderPoDetailPoItemReceiveID'], - 'UPDATE', $ROID, $json_po_before, $json_po_after, + 'UPDATE', $ROID, $json_po_before, $json_po_after, $user['M_UserID'], $descItem ); } $json_ro_after = $this->getDataROPO($ROID); $this->insertLogReceiveOrderPo( - $ROID, 'DELETE', $json_ro_before, + $ROID, 'DELETE', $json_ro_before, $json_ro_after, $user['M_UserID'], "Update data RO ID: {$ROID}" ); @@ -1959,10 +1959,10 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoInspeksiUserID ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; $que_insert = $this->db->query($sql_insert, [ - $ROdetailID, $inspeksi['qty_po'], $inspeksi['qty_ro'], - $inspeksi['price_po'], $inspeksi['price_ro'], $inspeksi['date_po'], - $inspeksi['date_ro'], $inspeksi['expire_date'], $inspeksi['condt_kemasan'], - $inspeksi['condt_pengiriman'], $inspeksi['summary'], $inspeksi['catatan'], + $ROdetailID, $inspeksi['qty_po'], $inspeksi['qty_ro'], + $inspeksi['price_po'], $inspeksi['price_ro'], $inspeksi['date_po'], + $inspeksi['date_ro'], $inspeksi['expire_date'], $inspeksi['condt_kemasan'], + $inspeksi['condt_pengiriman'], $inspeksi['summary'], $inspeksi['catatan'], $inspeksi['pemeriksa'], $inspeksi['pengirim'], $user['M_UserID'] ]); if (!$que_insert) { @@ -2015,10 +2015,10 @@ class ReceiveItemPoV2 extends MY_Controller { $data_detail = []; if (strtolower($data_po['PurchaseOrderWarehouseType']) == 'single') { - $sql_POdetail = "SELECT + $sql_POdetail = "SELECT IFNULL(pir.PoItemReceiveID, 0) as receivedID, - IFNULL(pir.PoItemReceiveQtyReceived, 0) as qtyReceived, - pos.PurchaseOrderSummaryID as orderSummaryID, + IFNULL(pir.PoItemReceiveQtyReceived, 0) as qtyReceived, + pos.PurchaseOrderSummaryID as orderSummaryID, pos.PurchaseOrderSummaryItemID as itemID, pos.PurchaseOrderSummaryItemUnitID as itemUnitID, pos.PurchaseOrderSummaryQty as qty, @@ -2027,15 +2027,15 @@ class ReceiveItemPoV2 extends MY_Controller { ItemUnitName as itemUnitName, PurchaseOrderDetailID FROM purchase_order_summary pos - JOIN purchase_order_detail ON PurchaseOrderDetailPurchaseOrderID = pos.PurchaseOrderSummaryPurchaseOrderID - AND PurchaseOrderSummaryItemID = PurchaseOrderDetailItemID - AND PurchaseOrderSummaryItemUnitID = PurchaseOrderDetailItemUnitID - JOIN m_item ON pos.PurchaseOrderSummaryItemID = M_ItemID - JOIN itemunit ON pos.PurchaseOrderSummaryItemUnitID = ItemUnitID - LEFT JOIN po_item_receive pir ON pos.PurchaseOrderSummaryPurchaseOrderID = pir.PoItemReceivePurchaseOrderID - AND pos.PurchaseOrderSummaryItemID = pir.PoItemReceiveItemID - WHERE pos.PurchaseOrderSummaryPurchaseOrderID = ? - AND pos.PurchaseOrderSummaryIsActive = 'Y' + JOIN purchase_order_detail ON PurchaseOrderDetailPurchaseOrderID = pos.PurchaseOrderSummaryPurchaseOrderID + AND PurchaseOrderSummaryItemID = PurchaseOrderDetailItemID + AND PurchaseOrderSummaryItemUnitID = PurchaseOrderDetailItemUnitID + JOIN m_item ON pos.PurchaseOrderSummaryItemID = M_ItemID + JOIN itemunit ON pos.PurchaseOrderSummaryItemUnitID = ItemUnitID + LEFT JOIN po_item_receive pir ON pos.PurchaseOrderSummaryPurchaseOrderID = pir.PoItemReceivePurchaseOrderID + AND pos.PurchaseOrderSummaryItemID = pir.PoItemReceiveItemID + WHERE pos.PurchaseOrderSummaryPurchaseOrderID = ? + AND pos.PurchaseOrderSummaryIsActive = 'Y' AND IFNULL(pir.PoItemReceiveQtyReceived, 0) = 0"; $que_POdetail = $this->db->query($sql_POdetail, [$POID]); if (!$que_POdetail) { @@ -2044,7 +2044,7 @@ class ReceiveItemPoV2 extends MY_Controller { exit; } $data_detail = $que_POdetail->result_array(); - + foreach ($data_detail as $key => $obj) { if (intval($obj['PoItemReceiveID']) == 0) { $sql_ins = "INSERT INTO po_item_receive ( @@ -2062,8 +2062,8 @@ class ReceiveItemPoV2 extends MY_Controller { PoItemReceivePrice ) VALUES (?,?,?,?,?,?,?,0,'Y',NOW(),?,?)"; $que_ins = $this->db->query($sql_ins, [ - $POID, $obj['orderSummaryID'], $obj['PurchaseOrderDetailID'], - $data_po['PurchaseOrderWarehouseID'], $obj['itemID'], $obj['itemUnitID'], + $POID, $obj['orderSummaryID'], $obj['PurchaseOrderDetailID'], + $data_po['PurchaseOrderWarehouseID'], $obj['itemID'], $obj['itemUnitID'], $obj['qty'], $userid, $obj['price'] ]); if (!$que_ins) { @@ -2105,7 +2105,7 @@ class ReceiveItemPoV2 extends MY_Controller { } if (strtolower($data_po['PurchaseOrderWarehouseType']) == 'multiple') { - $sql_det = "SELECT + $sql_det = "SELECT IFNULL(PoItemReceiveID, 0) as cekID, IFNULL(PoItemReceiveQtyReceived, 0) as qtyCek, pod.PurchaseOrderDetailPurchaseSummaryID as summaryID, @@ -2117,8 +2117,8 @@ class ReceiveItemPoV2 extends MY_Controller { pos.PurchaseOrderSummaryItemUnitID as itemUnitID, ItemUnitName as itemUnitName, ConvertQuantityToPurchasedItemUnit( - pod.PurchaseOrderDetailItemID, - pod.PurchaseOrderDetailItemUnitID, + pod.PurchaseOrderDetailItemID, + pod.PurchaseOrderDetailItemUnitID, pod.PurchaseOrderDetailQty ) as poQty, pos.PurchaseOrderSummaryPrice as price @@ -2159,7 +2159,7 @@ class ReceiveItemPoV2 extends MY_Controller { PoItemReceivePrice ) VALUES (?,?,?,?,?,?,?,0,'Y',NOW(),?,?)"; $que_ins = $this->db->query($sql_ins, [ - $POID, $obj['summaryID'], $obj['poDetailID'], $obj['warehouseID'], + $POID, $obj['summaryID'], $obj['poDetailID'], $obj['warehouseID'], $obj['itemID'], $obj['itemUnitID'], $obj['poQty'], $userid, $obj['price'] ]); if (!$que_ins) { @@ -2175,7 +2175,7 @@ class ReceiveItemPoV2 extends MY_Controller { } else { $jsonBefore = $this->getPOItemReceived($obj['cekID']); - $sql_upd = "UPDATE po_item_receive SET + $sql_upd = "UPDATE po_item_receive SET PoItemReceiveQty = ?, PoItemReceiveIsActive = 'Y', PoItemReceiveUpdate = NOW(), @@ -2185,7 +2185,7 @@ class ReceiveItemPoV2 extends MY_Controller { PoItemReceivePrice = ? WHERE PoItemReceiveID = ?"; $que_upd = $this->db->query($sql_upd, [ - $obj['poQty'], $userid, $obj['warehouseID'], + $obj['poQty'], $userid, $obj['warehouseID'], $obj['itemUnitID'], $obj['price'], $obj['cekID'] ]); if (!$que_upd) { @@ -2213,7 +2213,7 @@ class ReceiveItemPoV2 extends MY_Controller { } private function generatePurchaseInvoice($user, $ROID, $grni, $discpo, $shipping) { - + # generate nomor purchase invoice # $numPI = $this->generateNoLogistik($user, 'PI'); @@ -2227,14 +2227,14 @@ class ReceiveItemPoV2 extends MY_Controller { } $data_ro = $que_data_ro->row_array(); - $sql_detail_ro = "SELECT + $sql_detail_ro = "SELECT receive_order_po_detail.*, M_ItemDesc, PurchaseOrderSummaryDiscountType AS DiscountType, PurchaseOrderSummaryDiscountRupiah AS DiscountRupiah, PurchaseOrderSummaryDiscountPercent AS DiscountPercent, PurchaseOrderSummaryDiscountAmount as DiscountPerItem - FROM receive_order_po_detail + FROM receive_order_po_detail JOIN m_item ON M_ItemID = ReceiveOrderPoItemID AND M_ItemIsActive = 'Y' JOIN purchase_order_summary ON ReceiveOrderPoDetailPurchaseOrderSummaryID = PurchaseOrderSummaryID WHERE ReceiveOrderPoDetailIsActive = 'Y' @@ -2279,8 +2279,8 @@ class ReceiveItemPoV2 extends MY_Controller { ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW(),?,?)"; $que_insert_PI = $this->db->query($sql_insert_PI, [ $numPI, $data_ro['ReceiveOrderPoID'], $data_ro['ReceiveOrderPoIDate'], $due_date, - $data_ro['ReceiveOrderPoSupplierID'], $data_ro['ReceiveOrderPoRefNumber'], $data_ro['ReceiveOrderPoDONumber'], - $subtotal, 0.00, $discpo, 0.00, 0.00, 0.00, 0.00, $shipping, $grni, $grni, + $data_ro['ReceiveOrderPoSupplierID'], $data_ro['ReceiveOrderPoRefNumber'], $data_ro['ReceiveOrderPoDONumber'], + $subtotal, 0.00, $discpo, 0.00, 0.00, 0.00, 0.00, $shipping, $grni, $grni, $data_ro['ReceiveOrderPoNote'], $user['M_UserID'], $user['M_UserID'] ]); if (!$que_insert_PI) { @@ -2311,17 +2311,17 @@ class ReceiveItemPoV2 extends MY_Controller { SupplierInvoiceDetailUnpaid, SupplierInvoiceDetailCreatedUserID ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; - + foreach ($detail_ro as $key => $item) { $itemprice = doubleval($item['ReceiveOrderPoDetailPrice']) - doubleval($item['DiscountPerItem']); $total_price = round($itemprice * intval($item['ReceiveOrderPoDetailQty']), 2); $que_insert_itemPI = $this->db->query($sql_insert_detailPI, [ - $Pinvoice_ID, $item['ReceiveOrderPoDetailPurchaseOrderID'], - $item['ReceiveOrderPoDetailPurchaseOrderSummaryID'], $item['ReceiveOrderPoDetailReceiveOrderPoID'], - $item['ReceiveOrderPoDetailID'], $item['ReceiveOrderPoItemID'], $item['ReceiveOrderPoItemUnitID'], - $item['M_ItemDesc'], $item['ReceiveOrderPoDetailQty'], $item['ReceiveOrderPoDetailPrice'], - $item['DiscountPercent'], $item['DiscountRupiah'], $item['DiscountType'], $item['ReceiveOrderPoDetailDiskonPoProrata'], + $Pinvoice_ID, $item['ReceiveOrderPoDetailPurchaseOrderID'], + $item['ReceiveOrderPoDetailPurchaseOrderSummaryID'], $item['ReceiveOrderPoDetailReceiveOrderPoID'], + $item['ReceiveOrderPoDetailID'], $item['ReceiveOrderPoItemID'], $item['ReceiveOrderPoItemUnitID'], + $item['M_ItemDesc'], $item['ReceiveOrderPoDetailQty'], $item['ReceiveOrderPoDetailPrice'], + $item['DiscountPercent'], $item['DiscountRupiah'], $item['DiscountType'], $item['ReceiveOrderPoDetailDiskonPoProrata'], $item['DiscountPerItem'], $total_price, $total_price, $user['M_UserID'] ]); if (!$que_insert_itemPI) { @@ -2346,9 +2346,9 @@ class ReceiveItemPoV2 extends MY_Controller { ReceiveOrderPoLogCreated ) VALUES (?, ?, ?, ?, ?, ?, NOW())"; $que = $this->db->query($sql, [ - $id, $type, $desc, - !empty($dataBefore) ? json_encode($dataBefore) : '', - !empty($dataAfter) ? json_encode($dataAfter) : '', + $id, $type, $desc, + !empty($dataBefore) ? json_encode($dataBefore) : '', + !empty($dataAfter) ? json_encode($dataAfter) : '', $userID ]); if (!$que) { @@ -2370,7 +2370,7 @@ class ReceiveItemPoV2 extends MY_Controller { PoItemReceiveLogCreated ) VALUES (?, ?, ?, ?, ?, ?, ?, NOW())"; $que = $this->db->query($sql, [ - $id, $roID, $type, $desc, + $id, $roID, $type, $desc, !empty($dataBefore) ? json_encode($dataBefore) : '', !empty($dataAfter) ? json_encode($dataAfter) : '', $userID @@ -2427,4 +2427,4 @@ class ReceiveItemPoV2 extends MY_Controller { $this->sys_error($message); } } -} \ No newline at end of file +} diff --git a/application/controllers/mockup/supplierpaymentapprovedv3/Billv2.php b/application/controllers/mockup/supplierpaymentapprovedv3/Billv2.php index c8003d9..cbb9658 100644 --- a/application/controllers/mockup/supplierpaymentapprovedv3/Billv2.php +++ b/application/controllers/mockup/supplierpaymentapprovedv3/Billv2.php @@ -28,29 +28,66 @@ class Billv2 extends MY_Controller { $offset = ($params['currentpage'] - 1) * $limit; } - $sql_base = "SELECT - SupplierPaymentID, - SupplierPaymentDate, - SupplierPaymentNumber, - SupplierPaymentAmount, - SupplierPaymentStatus, - SupplierPaymentIsVerif, - SupplierPaymentIsApproved, - SupplierInvoiceID, - SupplierInvoiceNumber, - SupplierInvoiceDraftPaymentDate, - SupplierCode, - SupplierName - FROM supplier_payment - JOIN supplier_invoice ON SupplierInvoiceID = SupplierPaymentSupplierInvoiceID - AND SupplierPaymentNumber LIKE ? - AND (SupplierPaymentDate BETWEEN DATE(?) AND DATE(?)) - AND (SupplierPaymentStatus = ? OR ? = 'All') - JOIN supplier ON SupplierID = SupplierInvoiceSupplierID - WHERE SupplierPaymentIsActive = 'Y' - ORDER BY SupplierPaymentID DESC"; + // ── UNION base — invoice branch + downpayment branch ──── + $sql_base = " + SELECT + sp.SupplierPaymentID, + sp.SupplierPaymentDate, + sp.SupplierPaymentNumber, + sp.SupplierPaymentAmount, + sp.SupplierPaymentStatus, + sp.SupplierPaymentIsVerif, + sp.SupplierPaymentIsApproved, + sp.SupplierPaymentIsActive, + si.SupplierInvoiceID, + si.SupplierInvoiceNumber, + si.SupplierInvoiceDraftPaymentDate, + sup.SupplierCode, + sup.SupplierName, + 'INVOICE' AS type + FROM supplier_payment sp + JOIN supplier_invoice si + ON si.SupplierInvoiceID = sp.SupplierPaymentSupplierInvoiceID + JOIN supplier sup + ON sup.SupplierID = si.SupplierInvoiceSupplierID + WHERE sp.SupplierPaymentSupplierInvoiceID > 0 + + UNION ALL + + SELECT + sp.SupplierPaymentID, + sp.SupplierPaymentDate, + sp.SupplierPaymentNumber, + sp.SupplierPaymentAmount, + sp.SupplierPaymentStatus, + sp.SupplierPaymentIsVerif, + sp.SupplierPaymentIsApproved, + sp.SupplierPaymentIsActive, + dp.SupplierDownpaymentID * -1 AS SupplierInvoiceID, + CONCAT('DP-', po.PurchaseOrderNumber) AS SupplierInvoiceNumber, + dp.SupplierDownpaymentDueDate AS SupplierInvoiceDraftPaymentDate, + dp_sup.SupplierCode AS SupplierCode, + dp_sup.SupplierName AS SupplierName, + 'DP' AS type + FROM supplier_payment sp + JOIN supplier_downpayment dp + ON dp.SupplierDownpaymentID = sp.SupplierPaymentSupplierDownpaymentID + JOIN supplier dp_sup + ON dp_sup.SupplierID = dp.SupplierDownpaymentSupplierID + JOIN purchase_order po + ON po.PurchaseOrderID = dp.SupplierDownpaymentPurchasOrderID + WHERE sp.SupplierPaymentSupplierDownpaymentID IS NOT NULL"; + + // ── Outer: common filters + ordering + pagination ─────── + $sql_data = " + SELECT * FROM ($sql_base) AS combined + WHERE SupplierPaymentIsActive = 'Y' + AND SupplierPaymentNumber LIKE ? + AND (SupplierPaymentDate BETWEEN DATE(?) AND DATE(?)) + AND (SupplierPaymentStatus = ? OR ? = 'All') + ORDER BY SupplierPaymentID DESC + LIMIT ? OFFSET ?"; - $sql_data = $sql_base . " LIMIT ? OFFSET ? "; $que_data = $this->db->query($sql_data, [ $keyword, $params['startdate'], $params['enddate'], $params['status'], $params['status'], $limit, $offset @@ -59,7 +96,14 @@ class Billv2 extends MY_Controller { throw new Exception("[Error] failed get data supplier payment", 2); } - $sql_total = "SELECT COUNT(*) AS total FROM ($sql_base) AS x"; + // ── COUNT — wrap UNION in outer filter ────────────────── + $sql_total = " + SELECT COUNT(*) AS total FROM ($sql_base) AS combined + WHERE SupplierPaymentIsActive = 'Y' + AND SupplierPaymentNumber LIKE ? + AND (SupplierPaymentDate BETWEEN DATE(?) AND DATE(?)) + AND (SupplierPaymentStatus = ? OR ? = 'All')"; + $que_total = $this->db->query($sql_total, [ $keyword, $params['startdate'], $params['enddate'], $params['status'], $params['status'] @@ -70,9 +114,9 @@ class Billv2 extends MY_Controller { $output = [ "records" => $que_data->result_array(), - "total" =>$que_total->row_array()['total'] + "total" => $que_total->row_array()['total'] ]; - + $this->sys_ok($output); exit; } catch (Exception $exc) { @@ -96,70 +140,123 @@ class Billv2 extends MY_Controller { $para = $this->sys_input; - $sql = "SELECT - SupplierInvoiceID, - SupplierInvoiceRefNumber, - SupplierInvoiceDeliveryOrderNumber, - SupplierInvoiceSupplierInvoiceNumber, - SupplierInvoiceSupplierInvoiceDate, - SupplierInvoiceSubTotal, - SupplierInvoiceTaxPercentPph, - SupplierInvoiceTaxPercentPpn, - SupplierInvoiceTaxAmountPpn, - SupplierInvoiceDiscountAmount, - SupplierInvoiceDiscountPercent, - SupplierInvoiceShippingCost, - SupplierInvoiceGrandTotal, - SupplierInvoiceAdjustmentAmount, - SupplierInvoiceAdjustmentNote, - SupplierInvoiceNote, - IF (SupplierInvoiceDiscountAmount > 0, 'R', 'P') AS DiscountType - FROM supplier_payment - JOIN supplier_invoice ON SupplierPaymentSupplierInvoiceID = SupplierInvoiceID - AND SupplierPaymentID = ? - AND SupplierPaymentIsActive = 'Y'"; - $que = $this->db->query($sql, [$para['paymentID']]); - if (!$que) { - throw new Exception("[Error] failed get row data", 2); - } - $data = $que->row_array(); + // ── Detect payment type ────────────────────────────────── + $sql_type = "SELECT + SupplierPaymentSupplierInvoiceID, + SupplierPaymentSupplierDownpaymentID + FROM supplier_payment + WHERE SupplierPaymentID = ? AND SupplierPaymentIsActive = 'Y'"; - $sql_detail = "SELECT - SupplierInvoiceDetailID, - SupplierInvoiceDetailSupplierInvoiceID, - SupplierInvoiceDetailPurchaseOrderID, - SupplierInvoiceDetailPurchaseOrderSummaryID, - SupplierInvoiceDetailReceiveOrderPoID, - SupplierInvoiceDetailReceiveOrderPoDetailID, - SupplierInvoiceDetailItemID, - SupplierInvoiceDetailItemUnitID, - SupplierInvoiceDetailDescription, - SupplierInvoiceDetailQty, - SupplierInvoiceDetailPrice, - SupplierInvoiceDetailDiscountPercent, - SupplierInvoiceDetailDiscountDiscountRupiah, - SupplierInvoiceDetailDiscountDiscountType, - SupplierInvoiceDetailDiscountAmount, - (SupplierInvoiceDetailPrice - SupplierInvoiceDetailDiscountAmount) AS DiscountedPrice, - SupplierInvoiceDetailDiscountPoProrata, - SupplierInvoiceDetailTotal, - M_ItemCode, - M_ItemDesc - FROM supplier_payment_detail - JOIN supplier_invoice_detail ON SupplierInvoiceDetailIsActive = 'Y' - AND SupplierPaymentDetailSupplierPaymentID = ? - AND SupplierInvoiceDetailSupplierInvoiceID = ? - JOIN m_item ON M_ItemID = SupplierInvoiceDetailItemID - AND M_ItemIsActive = 'Y' - GROUP BY SupplierInvoiceDetailID"; - $que_detail = $this->db->query($sql_detail, [ - $para['paymentID'], $data['SupplierInvoiceID'] - ]); - if (!$que_detail) { - throw new Exception("[Error] failed to get item payments", 2); + $que_type = $this->db->query($sql_type, [$para['paymentID']]); + if (!$que_type) { + throw new Exception("[Error] failed get payment header", 2); + } + $payment = $que_type->row_array(); + if (!$payment) { + throw new Exception("[Error] payment not found", 2); } - $data['detail'] = $que_detail->result_array(); + // ── INVOICE branch ────────────────────────────────────── + if ($payment['SupplierPaymentSupplierInvoiceID'] > 0) { + $sql = "SELECT + si.SupplierInvoiceSubTotal, + si.SupplierInvoiceShippingCost, + si.SupplierInvoiceDiscountPercent, + si.SupplierInvoiceDiscountAmount, + si.SupplierInvoiceTaxPercentPpn, + si.SupplierInvoiceTaxAmountPpn, + si.SupplierInvoiceGrandTotal, + si.SupplierInvoiceID, + 'INVOICE' AS type + FROM supplier_payment sp + JOIN supplier_invoice si + ON si.SupplierInvoiceID = sp.SupplierPaymentSupplierInvoiceID + WHERE sp.SupplierPaymentID = ? AND sp.SupplierPaymentIsActive = 'Y'"; + + $que = $this->db->query($sql, [$para['paymentID']]); + if (!$que) { + throw new Exception("[Error] failed get row data", 2); + } + $data = $que->row_array(); + + $sql_detail = "SELECT + M_ItemDesc, + SupplierInvoiceDetailQty, + SupplierInvoiceDetailPrice, + SupplierInvoiceDetailDiscountAmount, + (SupplierInvoiceDetailPrice - SupplierInvoiceDetailDiscountAmount) AS DiscountedPrice, + SupplierInvoiceDetailTotal + FROM supplier_payment_detail + JOIN supplier_invoice_detail + ON SupplierInvoiceDetailIsActive = 'Y' + AND SupplierPaymentDetailSupplierPaymentID = ? + AND SupplierInvoiceDetailSupplierInvoiceID = ? + JOIN m_item + ON M_ItemID = SupplierInvoiceDetailItemID AND M_ItemIsActive = 'Y' + GROUP BY SupplierInvoiceDetailID"; + + $que_detail = $this->db->query($sql_detail, [ + $para['paymentID'], $data['SupplierInvoiceID'] + ]); + if (!$que_detail) { + throw new Exception("[Error] failed to get item payments", 2); + } + + unset($data['SupplierInvoiceID']); + $data['detail'] = $que_detail->result_array(); + + // ── DOWNPAYMENT branch ────────────────────────────────── + } else { + $sql = "SELECT + dp.SupplierDownpaymentPurchasOrderID, + dp.SupplierDownpaymentAmount AS SupplierInvoiceSubTotal, + 0 AS SupplierInvoiceShippingCost, + 0 AS SupplierInvoiceDiscountPercent, + 0 AS SupplierInvoiceDiscountAmount, + 0 AS SupplierInvoiceTaxPercentPpn, + 0 AS SupplierInvoiceTaxAmountPpn, + dp.SupplierDownpaymentAmount AS SupplierInvoiceGrandTotal, + 'DP' AS type + FROM supplier_payment sp + JOIN supplier_downpayment dp + ON dp.SupplierDownpaymentID = sp.SupplierPaymentSupplierDownpaymentID + WHERE sp.SupplierPaymentID = ? AND sp.SupplierPaymentIsActive = 'Y'"; + + $que = $this->db->query($sql, [$para['paymentID']]); + if (!$que) { + throw new Exception("[Error] failed get DP row data", 2); + } + $data = $que->row_array(); + + $sql_detail = "SELECT + CONCAT('DP-', M_ItemDesc) AS M_ItemDesc, + PurchaseOrderDetailQty AS SupplierInvoiceDetailQty, + PurchaseOrderDetailPrice AS SupplierInvoiceDetailPrice, + PurchaseOrderSummaryDiscountAmount AS SupplierInvoiceDetailDiscountAmount, + (PurchaseOrderDetailPrice - PurchaseOrderSummaryDiscountAmount) AS DiscountedPrice, + PurchaseOrderSummaryTotal AS SupplierInvoiceDetailTotal + FROM supplier_downpayment + JOIN purchase_order + ON SupplierDownpaymentPurchasOrderID = PurchaseOrderID + JOIN purchase_order_detail + ON PurchaseOrderDetailPurchaseOrderID = PurchaseOrderID + AND PurchaseOrderDetailIsActive = 'Y' + JOIN purchase_order_summary + ON PurchaseOrderSummaryID = PurchaseOrderDetailPurchaseSummaryID + AND PurchaseOrderSummaryIsActive = 'Y' + JOIN m_item + ON M_ItemID = PurchaseOrderDetailItemID + WHERE SupplierDownpaymentPurchasOrderID = ?"; + + $que_detail = $this->db->query($sql_detail, [ + $data['SupplierDownpaymentPurchasOrderID'] + ]); + if (!$que_detail) { + throw new Exception('failed to get dp detail', 2); + } + + $data['detail'] = $que_detail->result_array(); + } $this->sys_ok($data); } catch (Exception $exc) { @@ -214,12 +311,12 @@ class Billv2 extends MY_Controller { $user = $this->sys_user; if ($para['userlevel'] == '1') { - $sql = "UPDATE supplier_payment SET + $sql = "UPDATE supplier_payment SET SupplierPaymentIsVerif = 'Y', SupplierPaymentStatus = 'Verified', SupplierPaymentVerifUserID = ?, SupplierPaymentVerifDate = NOW() - WHERE SupplierPaymentID = ? + WHERE SupplierPaymentID = ? AND SupplierPaymentIsActive = 'Y'"; $query = $this->db->query($sql, [ $user['M_UserID'], $para['paymentID'] @@ -231,12 +328,12 @@ class Billv2 extends MY_Controller { } if ($para['userlevel'] == '2') { - $sql = "UPDATE supplier_payment SET + $sql = "UPDATE supplier_payment SET SupplierPaymentIsApproved = 'Y', SupplierPaymentStatus = 'Approved', SupplierPaymentApprovedUserID = ?, SupplierPaymentApprovedDate = NOW() - WHERE SupplierPaymentID = ? + WHERE SupplierPaymentID = ? AND SupplierPaymentIsActive = 'Y'"; $query = $this->db->query($sql, [ $user['M_UserID'], $para['paymentID'] @@ -261,4 +358,4 @@ class Billv2 extends MY_Controller { exit; } } -} \ No newline at end of file +} diff --git a/application/controllers/mockup/supplierpaymentcashierv5/Bill.php b/application/controllers/mockup/supplierpaymentcashierv5/Bill.php index c0b2ad2..2cb05dc 100644 --- a/application/controllers/mockup/supplierpaymentcashierv5/Bill.php +++ b/application/controllers/mockup/supplierpaymentcashierv5/Bill.php @@ -1,19 +1,20 @@ db_onedev = $this->load->database("onedev", true); - } + var $db_onedev; + public function index() + { + echo "Bill API"; + } + public function __construct() + { + parent::__construct(); + $this->db_onedev = $this->load->database("onedev", true); + } - public function add_notes($orderid){ - $sql = " SELECT SupplierPaymentSupplierInvoiceID as note_order_id, + public function add_notes($orderid) + { + $sql = "SELECT SupplierPaymentSupplierInvoiceID as note_order_id, SupplierPaymentID as note_id, SupplierPaymentDetailSupplierInvoiceDetailID as detail_id, SupplierPaymentDate as note_date, @@ -33,223 +34,590 @@ class Bill extends MY_Controller SupplierPaymentIsApproved, CONCAT('Approved by : ',a.M_UserUsername, ' ',DATE_FORMAT(SupplierPaymentApprovedDate,'%d-%m-%Y %H:%i')) as d_approved, CONCAT('Verified by : ',b.M_UserUsername, ' ',DATE_FORMAT(SupplierPaymentVerifDate,'%d-%m-%Y %H:%i')) as d_verif - FROM supplier_payment - JOIN supplier_payment_detail ON SupplierPaymentDetailSupplierPaymentID = SupplierPaymentID AND SupplierPaymentDetailIsActive = 'Y' - LEFT JOIN coa ON SupplierPaymentCoaID = coaID - LEFT JOIN m_user n ON SupplierPaymentUserID = n.M_UserID - LEFT JOIN m_user c ON SupplierPaymentConfirmUserID = c.M_UserID - LEFT JOIN m_user a ON SupplierPaymentApprovedUserID = a.M_UserID - LEFT JOIN m_user b ON SupplierPaymentVerifUserID = b.M_UserID - WHERE - SupplierPaymentSupplierInvoiceID = {$orderid} - AND - SupplierPaymentIsActive = 'Y' - GROUP BY SupplierPaymentID"; - $query = $this->db_onedev->query($sql); - if ($query) { - $rows = $query->result_array(); - if($rows){ - foreach($rows as $k => $v){ - $rows[$k]['tests'] = $this->add_tests($v['note_id']); - } - } - return $rows; - - } else { - $this->sys_error_db("get notes", $this->db_onedev); - exit; - } - } - public function add_tagihans($orderid){ - $sql = "SELECT SupplierInvoiceID as tagihan_id, - PurchaseOrderNumber as tagihan_number, - jurnalTxDescription as pasien, - jurnalTxCredit as tagihan_total, - IF(SupplierPaymentDetailID IS NULL , jurnalTxCredit, jurnalTxCredit - SUM(SupplierPaymentDetailAmount)) as tagihan_tagihan, - 0 as tagihan_bayar, - DATE_FORMAT(SupplierInvoiceDueDate,'%d-%m-%Y') as tagihan_duedate, - SupplierInvoiceIsActive as tagihan_active, - 'N' as show_detail, - jurnalTxID SupplierInvoiceDetailID, - PurchaseOrderID SupplierInvoiceDetailPurchaseOrderID - FROM supplier_invoice - JOIN purchase_order ON SupplierInvoicePurchaseOrderID = PurchaseOrderID - JOIN jurnal_addon ON jurnalAddOnValue = SupplierInvoiceNumber - JOIN jurnal_tx ON jurnalTxJurnalID = jurnalAddOnJurnalID AND jurnalTxCredit <> 0 AND jurnalTxCoaID <> 563 - LEFT JOIN supplier_payment ON SupplierPaymentSupplierInvoiceID = SupplierInvoiceID AND SupplierInvoiceIsActive = 'Y' - LEFT JOIN supplier_payment_detail ON SupplierPaymentDetailSupplierPaymentID = SupplierPaymentID AND SupplierPaymentDetailSupplierInvoiceDetailID = jurnalTxID AND SupplierPaymentDetailIsActive = 'Y' - WHERE SupplierInvoiceID = ? - GROUP BY jurnalTxID"; - $query = $this->db_onedev->query($sql, [$orderid]); - if ($query) { - $rows = $query->result_array(); - return $rows; - - } else { - $this->sys_error_db("get notes", $this->db_onedev); - exit; - } -} - public function add_tests($orderid){ - $sql = " SELECT SupplierPaymentSupplierInvoiceID as note_order_id, - SupplierPaymentID as note_id, - SupplierPaymentDate as note_date, - SupplierPaymentNumber as note_number, - GROUP_CONCAT(coaDescription separator ' , ') as paymenttypes_name, - SUM(SupplierPaymentDetailAmount) as note_amount, - M_UserUsername as note_user, - SupplierPaymentDetailIsActive as note_active, - PurchaseOrderNumber, - SupplierInvoiceDetailTotal, - SupplierPaymentDetailAmount - FROM supplier_payment - JOIN supplier_payment_detail ON SupplierPaymentDetailSupplierPaymentID = SupplierPaymentID - LEFT JOIN supplier_invoice_detail ON SupplierPaymentDetailSupplierInvoiceDetailID = SupplierInvoiceDetailID - LEFT JOIN purchase_order ON SupplierInvoiceDetailPurchaseOrderID = PurchaseOrderID - JOIN coa ON SupplierPaymentCoaID = coaID - LEFT JOIN m_user ON SupplierPaymentDetailUserID = M_UserID - WHERE - SupplierPaymentID = {$orderid} - GROUP BY SupplierPaymentDetailID"; - $query = $this->db_onedev->query($sql); - if ($query) { - $rows = $query->result_array(); - if($rows){ - } - return $rows; - - } else { - $this->sys_error_db("get notes", $this->db_onedev); - exit; - } -} - public function search() - { - //# cek token valid - if (! $this->isLogin) { - $this->sys_error("Invalid Token"); - exit; + FROM supplier_payment + JOIN supplier_payment_detail ON SupplierPaymentDetailSupplierPaymentID = SupplierPaymentID AND SupplierPaymentDetailIsActive = 'Y' + LEFT JOIN coa + ON SupplierPaymentCoaID = coaID + LEFT JOIN m_user n + ON SupplierPaymentUserID = n.M_UserID + LEFT JOIN m_user c + ON SupplierPaymentConfirmUserID = c.M_UserID + LEFT JOIN m_user a + ON SupplierPaymentApprovedUserID = a.M_UserID + LEFT JOIN m_user b + ON SupplierPaymentVerifUserID = b.M_UserID + WHERE SupplierPaymentSupplierInvoiceID = {$orderid} + AND SupplierPaymentIsActive = 'Y' + GROUP BY SupplierPaymentID"; + $query = $this->db_onedev->query($sql); + if ($query) { + $rows = $query->result_array(); + if ($rows) { + foreach ($rows as $k => $v) { + $rows[$k]['tests'] = $this->add_tests($v['note_id']); + } + } + return $rows; + } else { + $this->sys_error_db("get notes", $this->db_onedev); + exit; } - $prm = $this->sys_input; - $supplier = $prm["supplier"]; - $search = $prm["search"]; - $status = $prm["status"]; - $startdate = $prm["startdate"]; - $enddate = $prm["enddate"]; - $regionalid = $this->sys_user['S_RegionalID']; + } - $number_limit = 10; - $number_offset = ($prm['current_page'] - 1) * $number_limit ; + public function add_notes_downpayment($orderid) + { + $sql = "SELECT SupplierPaymentSupplierDownpaymentID AS note_order_id, + SupplierPaymentID AS note_id, + SupplierDownpaymentID AS detail_id, + SupplierPaymentDate AS note_date, + SupplierPaymentNumber AS note_number, + GROUP_CONCAT(DISTINCT coaDescription separator ' , ') AS paymenttypes_name, + SUM(SupplierDownpaymentAmount) AS note_amount, + n.M_UserUsername AS note_user, + SupplierDownpaymentIsActive AS note_active, + 'N' AS show_detail, + SupplierPaymentNote AS keterangan, + SupplierPaymentCoaID, + coaID, + coaDescription, + SupplierPaymentIsConfirm, + CONCAT( + 'Confirmed by : ',c.M_UserUsername, ' ', + DATE_FORMAT(SupplierPaymentConfirmDate, '%d-%m-%Y %H:%i') + ) AS d_confirm, + SupplierPaymentIsApproved, + CONCAT( + 'Approved by : ',a.M_UserUsername, ' ', + DATE_FORMAT(SupplierPaymentApprovedDate,'%d-%m-%Y %H:%i') + ) AS d_approved, + CONCAT( + 'Verified by : ',b.M_UserUsername, ' ', + DATE_FORMAT(SupplierPaymentVerifDate,'%d-%m-%Y %H:%i') + ) AS d_verif + FROM supplier_payment + JOIN supplier_downpayment + ON SupplierDownpaymentID = SupplierPaymentSupplierDownpaymentID + AND SupplierDownpaymentIsActive = 'Y' + LEFT JOIN coa + ON SupplierPaymentCoaID = coaID + LEFT JOIN m_user n + ON SupplierPaymentUserID = n.M_UserID + LEFT JOIN m_user c + ON SupplierPaymentConfirmUserID = c.M_UserID + LEFT JOIN m_user a + ON SupplierPaymentApprovedUserID = a.M_UserID + LEFT JOIN m_user b + ON SupplierPaymentVerifUserID = b.M_UserID + WHERE SupplierPaymentSupplierDownpaymentID = {$orderid} + AND SupplierPaymentIsActive = 'Y' + GROUP BY SupplierPaymentID"; + $query = $this->db_onedev->query($sql); + if ($query) { + $rows = $query->result_array(); + return $rows ?: []; + } else { + $this->sys_error_db("get DP notes", $this->db_onedev); + exit; + } + } - $where = "SupplierInvoiceIsActive = 'Y' - AND SupplierPaymentIsApproved = 'Y' - AND SupplierPaymentIsVerif = 'Y' - AND SupplierPaymentIsConfirm = '{$status}' - AND (SupplierInvoiceNumber LIKE '%{$search}%' OR SupplierInvoiceSupplierInvoiceNumber LIKE '%{$search}%') - AND SupplierName LIKE '%{$supplier}%' - AND ReceiveOrderPoS_RegionalID = {$regionalid} - AND SupplierInvoiceDraftPaymentDate BETWEEN '{$startdate}' AND '{$enddate}'"; + public function add_tagihans($orderid) + { + $sql = "SELECT SupplierInvoiceID as tagihan_id, + PurchaseOrderNumber as tagihan_number, + jurnalTxDescription as pasien, + jurnalTxCredit as tagihan_total, + IF(SupplierPaymentDetailID IS NULL , jurnalTxCredit, jurnalTxCredit - SUM(SupplierPaymentDetailAmount)) as tagihan_tagihan, + 0 as tagihan_bayar, + DATE_FORMAT(SupplierInvoiceDueDate,'%d-%m-%Y') as tagihan_duedate, + SupplierInvoiceIsActive as tagihan_active, + 'N' as show_detail, + jurnalTxID SupplierInvoiceDetailID, + PurchaseOrderID SupplierInvoiceDetailPurchaseOrderID + FROM supplier_invoice + JOIN purchase_order + ON SupplierInvoicePurchaseOrderID = PurchaseOrderID + JOIN jurnal_addon + ON jurnalAddOnValue = SupplierInvoiceNumber + JOIN jurnal_tx + ON jurnalTxJurnalID = jurnalAddOnJurnalID + AND jurnalTxCredit <> 0 + AND jurnalTxCoaID <> 563 + LEFT JOIN supplier_payment + ON SupplierPaymentSupplierInvoiceID = SupplierInvoiceID + AND SupplierInvoiceIsActive = 'Y' + LEFT JOIN supplier_payment_detail + ON SupplierPaymentDetailSupplierPaymentID = SupplierPaymentID + AND SupplierPaymentDetailSupplierInvoiceDetailID = jurnalTxID + AND SupplierPaymentDetailIsActive = 'Y' + WHERE SupplierInvoiceID = ? + GROUP BY jurnalTxID"; + $query = $this->db_onedev->query($sql, [$orderid]); + if ($query) { + $rows = $query->result_array(); + return $rows; + } else { + $this->sys_error_db("get notes", $this->db_onedev); + exit; + } + } + public function add_tests($orderid) + { + $sql = "SELECT + SupplierPaymentSupplierInvoiceID as note_order_id, + SupplierPaymentID as note_id, + SupplierPaymentDate as note_date, + SupplierPaymentNumber as note_number, + GROUP_CONCAT(coaDescription separator ' , ') as paymenttypes_name, + SUM(SupplierPaymentDetailAmount) as note_amount, + M_UserUsername as note_user, + SupplierPaymentDetailIsActive as note_active, + PurchaseOrderNumber, + SupplierInvoiceDetailTotal, + SupplierPaymentDetailAmount + FROM supplier_payment + JOIN supplier_payment_detail + ON SupplierPaymentDetailSupplierPaymentID = SupplierPaymentID + LEFT JOIN supplier_invoice_detail + ON SupplierPaymentDetailSupplierInvoiceDetailID = SupplierInvoiceDetailID + LEFT JOIN purchase_order + ON SupplierInvoiceDetailPurchaseOrderID = PurchaseOrderID + JOIN coa + ON SupplierPaymentCoaID = coaID + LEFT JOIN m_user + ON SupplierPaymentDetailUserID = M_UserID + WHERE SupplierPaymentID = {$orderid} + GROUP BY SupplierPaymentDetailID"; + $query = $this->db_onedev->query($sql); + if ($query) { + $rows = $query->result_array(); + if ($rows) { + } + return $rows; + } else { + $this->sys_error_db("get notes", $this->db_onedev); + exit; + } + } + public function search_old() + { + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $prm = $this->sys_input; + $supplier = $prm["supplier"]; + $search = $prm["search"]; + $status = $prm["status"]; + $startdate = $prm["startdate"]; + $enddate = $prm["enddate"]; + $regionalid = $this->sys_user['S_RegionalID']; - $sql = " SELECT count(*) as total - FROM supplier_invoice - JOIN jurnal_addon ON jurnalAddOnValue = SupplierInvoiceNumber - LEFT JOIN supplier_payment ON SupplierInvoiceID = SupplierPaymentSupplierInvoiceID AND SupplierPaymentIsActive = 'Y' - LEFT JOIN supplier ON SupplierInvoiceSupplierID = SupplierID - JOIN receive_order_po ON SupplierInvoiceReceiveOrderPoID = ReceiveOrderPoID - WHERE - $where - "; - // echo $sql; - $query = $this->db_onedev->query($sql, $sql_param); + $number_limit = 10; + $number_offset = ((int)$prm['current_page'] - 1) * $number_limit; + // ── WHERE ──────────────────────────────────────────────── + $where = " si.SupplierInvoiceIsActive = 'Y' + AND sp.SupplierPaymentIsApproved = 'Y' + AND sp.SupplierPaymentIsVerif = 'Y' + AND sp.SupplierPaymentIsConfirm = ? + AND (si.SupplierInvoiceNumber LIKE ? + OR si.SupplierInvoiceSupplierInvoiceNumber LIKE ?) + AND sup.SupplierName LIKE ? + AND rop.ReceiveOrderPoS_RegionalID = ? + AND si.SupplierInvoiceDraftPaymentDate BETWEEN ? AND ? "; - $tot_count = 0; - $tot_page = 0; - if ($query) { - $tot_count = $query->result_array()[0]["total"]; - $tot_page = ceil($tot_count/$number_limit); - } else { - $this->sys_error_db("supplier_invoice count", $this->db_onedev); - exit; - } + $where_params = [ + $status, + '%' . $search . '%', + '%' . $search . '%', + '%' . $supplier . '%', + $regionalid, + $startdate, + $enddate, + ]; + // ── COUNT ──────────────────────────────────────────────── + $sql_count = " + SELECT count(*) as total + FROM supplier_invoice si + JOIN jurnal_addon ja ON ja.jurnalAddOnValue = si.SupplierInvoiceNumber + JOIN receive_order_po rop ON rop.ReceiveOrderPoID = si.SupplierInvoiceReceiveOrderPoID + LEFT JOIN supplier_payment sp + ON sp.SupplierPaymentSupplierInvoiceID = si.SupplierInvoiceID + AND sp.SupplierPaymentIsActive = 'Y' + LEFT JOIN supplier sup ON sup.SupplierID = si.SupplierInvoiceSupplierID + WHERE $where"; - $sql = "SELECT supplier_invoice.*, - SupplierName, - '' M_MouName, - 0 as totalbill, - 0 as paid, - 0 as unpaid, - SupplierInvoiceIsLunas as flaglunas, - '' as SupplierPaymentNumber, - 0 as SupplierPaymentAmount, - '' as SupplierPaymentDate, - '' as SupplierInvoiceIssueRefNumber, - '' as notes, - '' as tagihans, - 'N' as isbillterpusat, - DATE_FORMAT(SupplierInvoiceDraftPaymentDate,'%d-%m-%Y') as tanggalbayar, - DATE_FORMAT(SupplierInvoiceDraftPaymentDate,'%d%m%Y') as tanggalbayartext, - IF(SupplierPaymentID IS NULL,'N','Y') as status_invoice, - SupplierPaymentID, - SupplierPaymentIsConfirm, - DATE_FORMAT(IFNULL(SupplierInvoiceSupplierInvoiceDate,''),'%d-%m-%Y') as tanggalinvoice, - 0 xrounding, - '' chex, - IFNULL(SupplierPaymentCashierNumber,'') SupplierPaymentCashierNumber + $query = $this->db_onedev->query($sql_count, $where_params); + $tot_count = 0; + $tot_page = 0; + if ($query) { + $tot_count = $query->result_array()[0]["total"]; + $tot_page = ceil($tot_count / $number_limit); + } else { + $this->sys_error_db("supplier_invoice count", $this->db_onedev); + exit; + } - FROM supplier_invoice - LEFT JOIN supplier ON SupplierInvoiceSupplierID = SupplierID - JOIN jurnal_addon ON jurnalAddOnValue = SupplierInvoiceNumber - JOIN receive_order_po ON SupplierInvoiceReceiveOrderPoID = ReceiveOrderPoID - LEFT JOIN supplier_payment ON SupplierPaymentSupplierInvoiceID = SupplierInvoiceID AND SupplierPaymentIsActive = 'Y' - WHERE - $where - GROUP BY SupplierInvoiceID - ORDER BY SupplierInvoiceID ASC - limit $number_limit offset $number_offset"; - //echo $sql; - $query = $this->db_onedev->query($sql, $sql_param); - $rows = $query->result_array(); - if($rows){ - foreach($rows as $k => $v){ - $rows[$k]['chex'] = false; - $s_payment = $this->db_onedev->query("SELECT GROUP_CONCAT(SupplierPaymentNumber SEPARATOR ', ') as SupplierPaymentNumber, - SUM(IFNULL(SupplierPaymentAmount,0)) as SupplierPaymentAmount, - GROUP_CONCAT(DATE_FORMAT(SupplierPaymentDate,'%d-%m-%Y') SEPARATOR ', ') as SupplierPaymentDate - FROM supplier_payment - WHERE SupplierPaymentIsActive = 'Y' AND SupplierPaymentSupplierInvoiceID = {$v['SupplierInvoiceID']}")->row(); + // ── DATA — only columns the frontend actually reads ────── + $sql_data = " + SELECT + si.SupplierInvoiceID, + si.SupplierInvoiceNumber, + si.SupplierInvoiceDraftPaymentDate, + si.SupplierInvoiceIsLunas AS flaglunas, + si.SupplierInvoiceSupplierInvoiceNumber, + DATE_FORMAT( + IFNULL(si.SupplierInvoiceSupplierInvoiceDate,''),'%d-%m-%Y' + ) AS tanggalinvoice, + sup.SupplierName, + sp.SupplierPaymentID, + sp.SupplierPaymentIsConfirm, + IFNULL(sp.SupplierPaymentCashierNumber,'') AS SupplierPaymentCashierNumber, + DATE_FORMAT( + si.SupplierInvoiceDraftPaymentDate,'%d-%m-%Y' + ) AS tanggalbayar, + 0 AS totalbill, + 0 AS paid, + 0 AS unpaid, + '' AS SupplierPaymentNumber, + 0 AS SupplierPaymentAmount, + '' AS SupplierPaymentDate, + '' AS notes, + '' AS tagihans, + 0 AS xrounding, + '' AS chex + FROM supplier_invoice si + JOIN jurnal_addon ja ON ja.jurnalAddOnValue = si.SupplierInvoiceNumber + JOIN receive_order_po rop ON rop.ReceiveOrderPoID = si.SupplierInvoiceReceiveOrderPoID + LEFT JOIN supplier_payment sp + ON sp.SupplierPaymentSupplierInvoiceID = si.SupplierInvoiceID + AND sp.SupplierPaymentIsActive = 'Y' + LEFT JOIN supplier sup ON sup.SupplierID = si.SupplierInvoiceSupplierID + WHERE $where + GROUP BY si.SupplierInvoiceID + ORDER BY si.SupplierInvoiceID ASC + LIMIT ? OFFSET ?"; - $s_jurnal = $this->db_onedev->query("SELECT SUM(jurnalTxCredit) totalbill - FROM supplier_invoice - JOIN jurnal_addon ON jurnalAddOnValue = SupplierInvoiceNumber - JOIN jurnal_tx ON jurnalTxJurnalID = jurnalAddOnJurnalID AND jurnalTxCredit <> 0 AND jurnalTxCoaID <> 563 - WHERE SupplierInvoiceID = {$v['SupplierInvoiceID']} - GROUP BY SupplierInvoiceID")->row(); - $amount = $s_payment->SupplierPaymentAmount ? $s_payment->SupplierPaymentAmount : "0.00"; - $unpaid = (float)$s_jurnal->totalbill - (float)$amount; - $rows[$k]['SupplierPaymentNumber'] = $s_payment->SupplierPaymentNumber; - $rows[$k]['SupplierPaymentAmount'] = $amount; - $rows[$k]['SupplierPaymentDate'] = $s_payment->SupplierPaymentDate; - $rows[$k]['paid'] = $amount; - $rows[$k]['totalbill'] = $s_jurnal->totalbill ? $s_jurnal->totalbill : "0.00"; - $rows[$k]['unpaid'] = number_format($unpaid, 2, '.', ''); + $data_params = array_merge($where_params, [$number_limit, $number_offset]); + $query = $this->db_onedev->query($sql_data, $data_params); + $rows = $query->result_array(); - $rows[$k]['notes'] = $this->add_notes($v['SupplierInvoiceID']); - $rows[$k]['tagihans'] = $this->add_tagihans($v['SupplierInvoiceID']); + // ── ENRICH ─────────────────────────────────────────────── + if ($rows) { + foreach ($rows as $k => $v) { + $rows[$k]['chex'] = false; + $inv_id = $v['SupplierInvoiceID']; - } - } + $s_payment = $this->db_onedev->query(" + SELECT + GROUP_CONCAT(SupplierPaymentNumber SEPARATOR ', ') as SupplierPaymentNumber, + SUM(IFNULL(SupplierPaymentAmount,0)) as SupplierPaymentAmount, + GROUP_CONCAT( + DATE_FORMAT(SupplierPaymentDate,'%d-%m-%Y') + SEPARATOR ', ' + ) as SupplierPaymentDate + FROM supplier_payment + WHERE SupplierPaymentIsActive = 'Y' + AND SupplierPaymentSupplierInvoiceID = ?", + [$inv_id] + )->row(); + $s_jurnal = $this->db_onedev->query(" + SELECT SUM(jurnalTxCredit) totalbill + FROM supplier_invoice si + JOIN jurnal_addon ja ON ja.jurnalAddOnValue = si.SupplierInvoiceNumber + JOIN jurnal_tx jt + ON jt.jurnalTxJurnalID = ja.jurnalAddOnJurnalID + AND jt.jurnalTxCredit <> 0 + AND jt.jurnalTxCoaID <> 563 + WHERE si.SupplierInvoiceID = ? + GROUP BY si.SupplierInvoiceID", + [$inv_id] + )->row(); - $result = array("total" => $tot_page, "records" => $rows, "sql"=> $this->db_onedev->last_query()); - $this->sys_ok($result); - exit; - } + $amount = $s_payment->SupplierPaymentAmount + ? $s_payment->SupplierPaymentAmount + : "0.00"; + $unpaid = (float)$s_jurnal->totalbill - (float)$amount; + $rows[$k]['SupplierPaymentNumber'] = $s_payment->SupplierPaymentNumber; + $rows[$k]['SupplierPaymentAmount'] = $amount; + $rows[$k]['SupplierPaymentDate'] = $s_payment->SupplierPaymentDate; + $rows[$k]['paid'] = $amount; + $rows[$k]['totalbill'] = $s_jurnal->totalbill + ? $s_jurnal->totalbill + : "0.00"; + $rows[$k]['unpaid'] = number_format($unpaid, 2, '.', ''); + $rows[$k]['notes'] = $this->add_notes($inv_id); + $rows[$k]['tagihans'] = $this->add_tagihans($inv_id); + } + } + + $result = array("total" => $tot_page, "records" => $rows); + $this->sys_ok($result); + exit; + } + + public function search() + { + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + + $prm = $this->sys_input; + $supplier = $prm["supplier"]; + $search = $prm["search"]; + $status = $prm["status"]; + $startdate = $prm["startdate"]; + $enddate = $prm["enddate"]; + $regionalid = $this->sys_user['S_RegionalID']; + + $number_limit = 10; + $number_offset = ((int)$prm['current_page'] - 1) * $number_limit; + + // ── INVOICE branch WHERE ───────────────────────────────── + $inv_where = " si.SupplierInvoiceIsActive = 'Y' + AND rop.ReceiveOrderPoS_RegionalID = ? "; + + $inv_params = [$regionalid]; + + // ── DOWNPAYMENT branch WHERE ───────────────────────────── + $dp_where = " dp.SupplierDownpaymentIsActive = 'Y' + AND po.PurchaseOrderS_RegionalID = ? "; + + $dp_params = [$regionalid]; + + // ── UNION base ─────────────────────────────────────────── + $sql_base = " + SELECT + si.SupplierInvoiceID, + si.SupplierInvoiceNumber, + si.SupplierInvoiceDraftPaymentDate, + si.SupplierInvoiceDraftPaymentDate AS filter_date, + si.SupplierInvoiceIsLunas AS flaglunas, + si.SupplierInvoiceSupplierInvoiceNumber, + DATE_FORMAT( + IFNULL(si.SupplierInvoiceSupplierInvoiceDate,''),'%d-%m-%Y' + ) AS tanggalinvoice, + sup.SupplierName, + sp.SupplierPaymentID, + sp.SupplierPaymentIsConfirm, + sp.SupplierPaymentIsApproved, + sp.SupplierPaymentIsVerif, + sp.SupplierPaymentIsActive, + IFNULL(sp.SupplierPaymentCashierNumber,'') AS SupplierPaymentCashierNumber, + DATE_FORMAT( + si.SupplierInvoiceDraftPaymentDate,'%d-%m-%Y' + ) AS tanggalbayar, + 0 AS totalbill, + 0 AS paid, + 0 AS unpaid, + '' AS SupplierPaymentNumber, + 0 AS SupplierPaymentAmount, + '' AS SupplierPaymentDate, + '' AS notes, + '' AS tagihans, + 0 AS xrounding, + '' AS chex, + 'INVOICE' AS type + FROM supplier_invoice si + JOIN jurnal_addon ja ON ja.jurnalAddOnValue = si.SupplierInvoiceNumber + JOIN receive_order_po rop ON rop.ReceiveOrderPoID = si.SupplierInvoiceReceiveOrderPoID + LEFT JOIN supplier_payment sp + ON sp.SupplierPaymentSupplierInvoiceID = si.SupplierInvoiceID + AND sp.SupplierPaymentIsActive = 'Y' + LEFT JOIN supplier sup ON sup.SupplierID = si.SupplierInvoiceSupplierID + WHERE $inv_where + GROUP BY si.SupplierInvoiceID + + UNION ALL + + SELECT + dp.SupplierDownpaymentID * -1 AS SupplierInvoiceID, + CONCAT('DP-', po.PurchaseOrderNumber) AS SupplierInvoiceNumber, + dp.SupplierDownpaymentDueDate AS SupplierInvoiceDraftPaymentDate, + dp.SupplierDownpaymentDueDate AS filter_date, + dp.SupplierDownpaymentIsLunas AS flaglunas, + '' AS SupplierInvoiceSupplierInvoiceNumber, + DATE_FORMAT(dp.SupplierDownpaymentDate,'%d-%m-%Y') AS tanggalinvoice, + dp_sup.SupplierName AS SupplierName, + sp.SupplierPaymentID, + sp.SupplierPaymentIsConfirm, + sp.SupplierPaymentIsApproved, + sp.SupplierPaymentIsVerif, + sp.SupplierPaymentIsActive, + IFNULL(sp.SupplierPaymentCashierNumber,'') AS SupplierPaymentCashierNumber, + DATE_FORMAT( + dp.SupplierDownpaymentDueDate,'%d-%m-%Y' + ) AS tanggalbayar, + dp.SupplierDownpaymentAmount AS totalbill, + CASE WHEN dp.SupplierDownpaymentStatus = 'Paid' + THEN dp.SupplierDownpaymentAmount + ELSE 0 END AS paid, + CASE WHEN dp.SupplierDownpaymentStatus = 'Paid' + THEN 0 + ELSE dp.SupplierDownpaymentAmount END AS unpaid, + '' AS SupplierPaymentNumber, + 0 AS SupplierPaymentAmount, + '' AS SupplierPaymentDate, + '' AS notes, + '' AS tagihans, + 0 AS xrounding, + '' AS chex, + 'DP' AS type + FROM supplier_payment sp + JOIN supplier_downpayment dp + ON dp.SupplierDownpaymentID = sp.SupplierPaymentSupplierDownpaymentID + JOIN supplier dp_sup + ON dp_sup.SupplierID = dp.SupplierDownpaymentSupplierID + JOIN purchase_order po + ON po.PurchaseOrderID = dp.SupplierDownpaymentPurchasOrderID + WHERE $dp_where"; + + // ── Outer common filters ───────────────────────────────── + $outer_where = " SupplierPaymentIsActive = 'Y' + AND SupplierPaymentIsApproved = 'Y' + AND SupplierPaymentIsVerif = 'Y' + AND SupplierPaymentIsConfirm = ? + AND (SupplierInvoiceNumber LIKE ? OR SupplierInvoiceSupplierInvoiceNumber LIKE ?) + AND SupplierName LIKE ? + AND filter_date BETWEEN ? AND ? "; + + $outer_params = [ + $status, + '%' . $search . '%', + '%' . $search . '%', + '%' . $supplier . '%', + $startdate, + $enddate, + ]; + + // ── COUNT ──────────────────────────────────────────────── + $sql_count = " + SELECT COUNT(*) AS total + FROM ($sql_base) AS combined + WHERE $outer_where"; + + $count_params = array_merge($inv_params, $dp_params, $outer_params); + $query = $this->db_onedev->query($sql_count, $count_params); + + $tot_count = 0; + $tot_page = 0; + if ($query) { + $tot_count = $query->result_array()[0]["total"]; + $tot_page = ceil($tot_count / $number_limit); + } else { + $this->sys_error_db("supplier payment count", $this->db_onedev); + exit; + } + + // ── DATA ───────────────────────────────────────────────── + $sql_data = " + SELECT * FROM ($sql_base) AS combined + WHERE $outer_where + ORDER BY SupplierInvoiceID ASC + LIMIT ? OFFSET ?"; + + $data_params = array_merge($inv_params, $dp_params, $outer_params, [$number_limit, $number_offset]); + $query = $this->db_onedev->query($sql_data, $data_params); + $rows = $query->result_array(); + + // ── ENRICH ─────────────────────────────────────────────── + if ($rows) { + foreach ($rows as $k => $v) { + + // ── Downpayment branch ────────────────────────── + if ($v['type'] === 'DP') { + $rows[$k]['chex'] = false; + $dp_id = abs($v['SupplierInvoiceID']); + + $s_payment = $this->db_onedev->query(" + SELECT + GROUP_CONCAT(SupplierPaymentNumber SEPARATOR ', ') as SupplierPaymentNumber, + SUM(IFNULL(SupplierPaymentAmount,0)) as SupplierPaymentAmount, + GROUP_CONCAT( + DATE_FORMAT(SupplierPaymentDate,'%d-%m-%Y') + SEPARATOR ', ' + ) as SupplierPaymentDate + FROM supplier_payment + WHERE SupplierPaymentIsActive = 'Y' + AND SupplierPaymentSupplierDownpaymentID = ?", + [$dp_id] + )->row(); + + $amount = $s_payment->SupplierPaymentAmount + ? $s_payment->SupplierPaymentAmount + : "0.00"; + + $rows[$k]['SupplierPaymentNumber'] = $s_payment->SupplierPaymentNumber; + $rows[$k]['SupplierPaymentAmount'] = $amount; + $rows[$k]['SupplierPaymentDate'] = $s_payment->SupplierPaymentDate; + $rows[$k]['paid'] = $amount; + $rows[$k]['notes'] = $this->add_notes_downpayment($dp_id); + $rows[$k]['tagihans'] = []; + continue; + } + + // ── Invoice branch ────────────────────────────── + $rows[$k]['chex'] = false; + $inv_id = $v['SupplierInvoiceID']; + + $s_payment = $this->db_onedev->query(" + SELECT + GROUP_CONCAT(SupplierPaymentNumber SEPARATOR ', ') as SupplierPaymentNumber, + SUM(IFNULL(SupplierPaymentAmount,0)) as SupplierPaymentAmount, + GROUP_CONCAT( + DATE_FORMAT(SupplierPaymentDate,'%d-%m-%Y') + SEPARATOR ', ' + ) as SupplierPaymentDate + FROM supplier_payment + WHERE SupplierPaymentIsActive = 'Y' + AND SupplierPaymentSupplierInvoiceID = ?", + [$inv_id] + )->row(); + + $s_jurnal = $this->db_onedev->query(" + SELECT SUM(jurnalTxCredit) totalbill + FROM supplier_invoice si + JOIN jurnal_addon ja ON ja.jurnalAddOnValue = si.SupplierInvoiceNumber + JOIN jurnal_tx jt + ON jt.jurnalTxJurnalID = ja.jurnalAddOnJurnalID + AND jt.jurnalTxCredit <> 0 + AND jt.jurnalTxCoaID <> 563 + WHERE si.SupplierInvoiceID = ? + GROUP BY si.SupplierInvoiceID", + [$inv_id] + )->row(); + + $amount = $s_payment->SupplierPaymentAmount + ? $s_payment->SupplierPaymentAmount + : "0.00"; + $unpaid = (float)$s_jurnal->totalbill - (float)$amount; + + $rows[$k]['SupplierPaymentNumber'] = $s_payment->SupplierPaymentNumber; + $rows[$k]['SupplierPaymentAmount'] = $amount; + $rows[$k]['SupplierPaymentDate'] = $s_payment->SupplierPaymentDate; + $rows[$k]['paid'] = $amount; + $rows[$k]['totalbill'] = $s_jurnal->totalbill + ? $s_jurnal->totalbill + : "0.00"; + $rows[$k]['unpaid'] = number_format($unpaid, 2, '.', ''); + + $rows[$k]['notes'] = $this->add_notes($inv_id); + $rows[$k]['tagihans'] = $this->add_tagihans($inv_id); + } + } + + $result = array("total" => $tot_page, "records" => $rows); + $this->sys_ok($result); + exit; + } } diff --git a/application/controllers/mockup/supplierpaymentcashierv5/PaymentV2.php b/application/controllers/mockup/supplierpaymentcashierv5/PaymentV2.php index bc55a10..a5ea7d5 100644 --- a/application/controllers/mockup/supplierpaymentcashierv5/PaymentV2.php +++ b/application/controllers/mockup/supplierpaymentcashierv5/PaymentV2.php @@ -70,14 +70,28 @@ class PaymentV2 extends MY_Controller } # UPDATE status lunas supplier invoice # - $sql_suppinvoice = "UPDATE supplier_invoice - SET SupplierInvoiceIsLunas = 'Y' - WHERE SupplierInvoiceID = ?"; - $que_suppinvoice = $this->db->query($sql_suppinvoice, [$param['SupplierInvoiceID']]); - if (!$que_suppinvoice) { - $this->db->trans_rollback(); - $this->sys_error_db("[Error] update status lunas invoice"); - exit; + if ($param['type'] == 'DP') { + $sql_updatedp = "UPDATE supplier_downpayment + SET SupplierDownpaymentIsLunas = 'Y' + WHERE SupplierDownpaymentID = ?"; + $que_updatedp = $this->db->query($sql_updatedp, [ + abs($param['SupplierInvoiceID']) + ]); + if (!$que_updatedp) { + $this->db->trans_rollback(); + $this->sys_error_db("[Error] update status lunas downpayment"); + exit; + } + } else { + $sql_suppinvoice = "UPDATE supplier_invoice + SET SupplierInvoiceIsLunas = 'Y' + WHERE SupplierInvoiceID = ?"; + $que_suppinvoice = $this->db->query($sql_suppinvoice, [$param['SupplierInvoiceID']]); + if (!$que_suppinvoice) { + $this->db->trans_rollback(); + $this->sys_error_db("[Error] update status lunas invoice"); + exit; + } } # GET Latest data supplier payment # @@ -89,16 +103,33 @@ class PaymentV2 extends MY_Controller exit; } $suppayment_header = $que_suppayment->row_array(); + $suppayment_detail = []; - $sql_suppaymentdetail = "SELECT * FROM supplier_payment_detail + if ($param['type'] == 'DP') { + $sql_suppaymentdetail = "SELECT supplier_downpayment.* + FROM supplier_payment + JOIN supplier_downpayment + ON SupplierPaymentSupplierDownpaymentID = SupplierDownpaymentID + AND SupplierDownpaymentIsActive = 'Y' + WHERE SupplierPaymentID = ?"; + $que_suppaymentdetail = $this->db->query($sql_suppaymentdetail, [$param['orderid']]); + if (!$que_suppaymentdetail) { + $this->db->trans_rollback(); + $this->sys_error_db("[Error] get data latest supplier downpayment"); + exit; + } + $suppayment_detail = $que_suppaymentdetail->result_array(); + } else { + $sql_suppaymentdetail = "SELECT * FROM supplier_payment_detail WHERE SupplierPaymentDetailSupplierPaymentID = ?"; - $que_suppaymentdetail = $this->db->query($sql_suppaymentdetail, [$param['orderid']]); - if (!$que_suppaymentdetail) { - $this->db->trans_rollback(); - $this->sys_error_db("[Error] get data latest supplier payment detail"); - exit; + $que_suppaymentdetail = $this->db->query($sql_suppaymentdetail, [$param['orderid']]); + if (!$que_suppaymentdetail) { + $this->db->trans_rollback(); + $this->sys_error_db("[Error] get data latest supplier payment detail"); + exit; + } + $suppayment_detail = $que_suppaymentdetail->result_array(); } - $suppayment_detail = $que_suppaymentdetail->result_array(); $data_log = [ "header" => $suppayment_header, @@ -121,38 +152,85 @@ class PaymentV2 extends MY_Controller # INSERT JURNAL # $detail_transac = []; - # GET data hutang # - $sql_datahutang = "SELECT - SupplierPaymentNumber AS addonvalue, - SupplierPaymentDetailID, - jurnalTxCoaID AS coaID, - jurnalTxDescription AS coaDescription, - SupplierPaymentDetailAmount, - SupplierInvoiceDetailItemID - FROM supplier_payment - JOIN supplier_payment_detail ON SupplierPaymentDetailSupplierPaymentID = SupplierPaymentID - JOIN supplier_invoice_detail ON SupplierInvoiceDetailSupplierInvoiceID = SupplierPaymentSupplierInvoiceID - AND SupplierInvoiceDetailIsActive = 'Y' - JOIN jurnal_tx ON SupplierPaymentDetailSupplierInvoiceDetailID = jurnalTxID - WHERE SupplierPaymentID = ? - AND SupplierPaymentDetailIsActive = 'Y'"; - $que_datahutang = $this->db->query($sql_datahutang, [$param['orderid']]); - if (!$que_datahutang) { - $this->db->trans_rollback(); - $this->sys_error_db("[Error] get data hutang item"); - exit; - } - $data_hutang = $que_datahutang->result_array(); - foreach ($data_hutang as $key => $debt) { - $detail_transac[] = [ - "coaID" => $debt['coaID'], - "coaDescription" => $debt['coaDescription'], - "debit" => $debt['SupplierPaymentDetailAmount'], - "credit" => 0, - "addoncode" => "JFA", - "addonvalue" => $debt['addonvalue'], - "addonitemid" => $debt['SupplierInvoiceDetailItemID'] - ]; + if ($param['type'] == "DP") { + $sql_coaDP = "SELECT + coaID, + coaDescription, + SupplierDownpaymentAmount, + M_ItemID, + SupplierPaymentNumber, + M_ItemID + FROM supplier_payment + JOIN supplier_downpayment + ON SupplierDownpaymentID = SupplierPaymentSupplierDownpaymentID + AND SupplierDownpaymentIsActive = 'Y' + JOIN purchase_order_detail + ON SupplierDownpaymentPurchasOrderID = PurchaseOrderDetailPurchaseOrderID + AND PurchaseOrderDetailIsActive = 'Y' + JOIN m_item + ON M_ItemID = PurchaseOrderDetailItemID + AND M_ItemIsActive = 'Y' + JOIN fa_class + ON Fa_ClassID = M_ItemFa_ClassID + AND M_ItemIsActive = 'Y' + JOIN coa + ON coaID = Fa_ClassDownPaymentCoaID + AND coaIsActive = 'Y' + WHERE SupplierPaymentID = ?"; + $que_coaDP = $this->db->query($sql_coaDP, [ + $param['orderid'] + ]); + if (!$que_coaDP) { + $this->db->trans_rollback(); + $this->sys_error_db("[Error] get data hutang item"); + exit; + } + $data_dp = $que_coaDP->result_array(); + foreach ($data_dp as $key => $dp) { + $detail_transac[] = [ + "coaID" => $dp['coaID'], + "coaDescription" => $dp['coaDescription'], + "debit" => $dp['SupplierDownpaymentAmount'], + "credit" => 0, + "addoncode" => "DP-JFA", + "addonvalue" => $dp['SupplierPaymentNumber'], + "addonitemid" => $dp['M_ItemID'] + ]; + } + } else { + # GET data hutang # + $sql_datahutang = "SELECT + SupplierPaymentNumber AS addonvalue, + SupplierPaymentDetailID, + jurnalTxCoaID AS coaID, + jurnalTxDescription AS coaDescription, + SupplierPaymentDetailAmount, + SupplierInvoiceDetailItemID + FROM supplier_payment + JOIN supplier_payment_detail ON SupplierPaymentDetailSupplierPaymentID = SupplierPaymentID + JOIN supplier_invoice_detail ON SupplierInvoiceDetailSupplierInvoiceID = SupplierPaymentSupplierInvoiceID + AND SupplierInvoiceDetailIsActive = 'Y' + JOIN jurnal_tx ON SupplierPaymentDetailSupplierInvoiceDetailID = jurnalTxID + WHERE SupplierPaymentID = ? + AND SupplierPaymentDetailIsActive = 'Y'"; + $que_datahutang = $this->db->query($sql_datahutang, [$param['orderid']]); + if (!$que_datahutang) { + $this->db->trans_rollback(); + $this->sys_error_db("[Error] get data hutang item"); + exit; + } + $data_hutang = $que_datahutang->result_array(); + foreach ($data_hutang as $key => $debt) { + $detail_transac[] = [ + "coaID" => $debt['coaID'], + "coaDescription" => $debt['coaDescription'], + "debit" => $debt['SupplierPaymentDetailAmount'], + "credit" => 0, + "addoncode" => "JFA", + "addonvalue" => $debt['addonvalue'], + "addonitemid" => $debt['SupplierInvoiceDetailItemID'] + ]; + } } # GET data bayar # diff --git a/application/controllers/mockup/supplierpaymentv4/Bill.php b/application/controllers/mockup/supplierpaymentv4/Bill.php index f6db786..48905a8 100644 --- a/application/controllers/mockup/supplierpaymentv4/Bill.php +++ b/application/controllers/mockup/supplierpaymentv4/Bill.php @@ -62,6 +62,58 @@ class Bill extends MY_Controller } } + public function add_notes_downpayment($orderid) + { + $sql = "SELECT SupplierPaymentSupplierDownpaymentID AS note_order_id, + SupplierPaymentID AS note_id, + SupplierDownpaymentID AS detail_id, + SupplierPaymentDate AS note_date, + SupplierPaymentNumber AS note_number, + GROUP_CONCAT(DISTINCT coaDescription separator ' , ') AS paymenttypes_name, + SUM(SupplierDownpaymentAmount) AS note_amount, + n.M_UserUsername AS note_user, + SupplierDownpaymentIsActive AS note_active, + 'N' AS show_detail, + SupplierPaymentNote AS keterangan, + SupplierPaymentCoaID, + coaID, + coaDescription, + SupplierPaymentIsConfirm, + CONCAT( + 'Confirmed by : ',c.M_UserUsername, ' ', + DATE_FORMAT(SupplierPaymentConfirmDate, '%d-%m-%Y %H:%i') + ) AS d_confirm, + SupplierPaymentIsApproved, + CONCAT( + 'Approved by : ',a.M_UserUsername, ' ', + DATE_FORMAT(SupplierPaymentApprovedDate,'%d-%m-%Y %H:%i') + ) AS d_approved, + CONCAT( + 'Verified by : ',b.M_UserUsername, ' ', + DATE_FORMAT(SupplierPaymentVerifDate,'%d-%m-%Y %H:%i') + ) AS d_verif + FROM supplier_payment + JOIN supplier_downpayment + ON SupplierDownpaymentID = SupplierPaymentSupplierDownpaymentID + AND SupplierDownpaymentIsActive = 'Y' + LEFT JOIN coa ON SupplierPaymentCoaID = coaID + LEFT JOIN m_user n ON SupplierPaymentUserID = n.M_UserID + LEFT JOIN m_user c ON SupplierPaymentConfirmUserID = c.M_UserID + LEFT JOIN m_user a ON SupplierPaymentApprovedUserID = a.M_UserID + LEFT JOIN m_user b ON SupplierPaymentVerifUserID = b.M_UserID + WHERE SupplierPaymentSupplierDownpaymentID = {$orderid} + AND SupplierPaymentIsActive = 'Y' + GROUP BY SupplierPaymentID"; + $query = $this->db_onedev->query($sql); + if ($query) { + $rows = $query->result_array(); + return $rows ?: []; + } else { + $this->sys_error_db("get DP notes", $this->db_onedev); + exit; + } + } + public function add_tagihans($orderid) { $sql = "SELECT SupplierInvoiceID as tagihan_id, @@ -132,13 +184,14 @@ class Bill extends MY_Controller } } - public function search() + public function search_old() { //# cek token valid if (! $this->isLogin) { $this->sys_error("Invalid Token"); exit; } + $prm = $this->sys_input; $supplier = $prm["supplier"]; $search = $prm["search"]; @@ -151,25 +204,25 @@ class Bill extends MY_Controller $number_offset = ($prm['current_page'] - 1) * $number_limit; $where = "SupplierInvoiceIsActive = 'Y' - AND SupplierInvoiceStatus = 'Approved' - AND SupplierInvoiceGrandTotal > 0 - AND IF(SupplierPaymentID IS NULL,'N','Y') = '{$status}' - AND (SupplierInvoiceNumber LIKE '%{$search}%' AND SupplierName LIKE '%{$supplier}%') - AND ReceiveOrderPoS_RegionalID = {$regionalid} - AND SupplierInvoiceDraftPaymentDate BETWEEN '{$startdate}' AND '{$enddate}'"; + AND SupplierInvoiceStatus = 'Approved' + AND SupplierInvoiceGrandTotal > 0 + AND IF(SupplierPaymentID IS NULL,'N','Y') = '{$status}' + AND (SupplierInvoiceNumber LIKE '%{$search}%' AND SupplierName LIKE '%{$supplier}%') + AND ReceiveOrderPoS_RegionalID = {$regionalid} + AND SupplierInvoiceDraftPaymentDate BETWEEN '{$startdate}' AND '{$enddate}'"; $sql = " SELECT count(*) as total - FROM supplier_invoice - JOIN jurnal_addon ON jurnalAddOnValue = SupplierInvoiceNumber - LEFT JOIN supplier_payment ON SupplierInvoiceID = SupplierPaymentSupplierInvoiceID AND SupplierPaymentIsActive = 'Y' - LEFT JOIN supplier ON SupplierInvoiceSupplierID = SupplierID - JOIN receive_order_po ON SupplierInvoiceReceiveOrderPoID = ReceiveOrderPoID - WHERE - $where - "; + FROM supplier_invoice + JOIN jurnal_addon ON jurnalAddOnValue = SupplierInvoiceNumber + LEFT JOIN supplier_payment ON SupplierInvoiceID = SupplierPaymentSupplierInvoiceID AND SupplierPaymentIsActive = 'Y' + LEFT JOIN supplier ON SupplierInvoiceSupplierID = SupplierID + JOIN receive_order_po ON SupplierInvoiceReceiveOrderPoID = ReceiveOrderPoID + WHERE + $where + "; // echo $sql; $query = $this->db_onedev->query($sql, $sql_param); @@ -186,55 +239,55 @@ class Bill extends MY_Controller $sql = "SELECT supplier_invoice.*, - SupplierName, - '' M_MouName, - 0 as totalbill, - 0 as paid, - 0 as unpaid, - SupplierInvoiceIsLunas as flaglunas, - 0 as SupplierPaymentID, - '' as SupplierPaymentNumber, - 0 as SupplierPaymentAmount, - '' as SupplierPaymentDate, - '' as SupplierInvoiceIssueRefNumber, - '' as notes, - '' as tagihans, - 'N' as isbillterpusat, - DATE_FORMAT(SupplierInvoiceDraftPaymentDate,'%d-%m-%Y') as tanggalbayar, - DATE_FORMAT(SupplierInvoiceDraftPaymentDate,'%d%m%Y') as tanggalbayartext, - IF(SupplierPaymentID IS NULL,'N','Y') as status_invoice, - IFNULL(SupplierPaymentIsApproved,'N') as SupplierPaymentIsApproved, - IFNULL(SupplierPaymentIsVerif,'N') as SupplierPaymentIsVerif, - IFNULL(SupplierPaymentCashierNumber,'') SupplierPaymentCashierNumber + SupplierName, + '' M_MouName, + 0 as totalbill, + 0 as paid, + 0 as unpaid, + SupplierInvoiceIsLunas as flaglunas, + 0 as SupplierPaymentID, + '' as SupplierPaymentNumber, + 0 as SupplierPaymentAmount, + '' as SupplierPaymentDate, + '' as SupplierInvoiceIssueRefNumber, + '' as notes, + '' as tagihans, + 'N' as isbillterpusat, + DATE_FORMAT(SupplierInvoiceDraftPaymentDate,'%d-%m-%Y') as tanggalbayar, + DATE_FORMAT(SupplierInvoiceDraftPaymentDate,'%d%m%Y') as tanggalbayartext, + IF(SupplierPaymentID IS NULL,'N','Y') as status_invoice, + IFNULL(SupplierPaymentIsApproved,'N') as SupplierPaymentIsApproved, + IFNULL(SupplierPaymentIsVerif,'N') as SupplierPaymentIsVerif, + IFNULL(SupplierPaymentCashierNumber,'') SupplierPaymentCashierNumber - FROM supplier_invoice - LEFT JOIN supplier ON SupplierInvoiceSupplierID = SupplierID - JOIN jurnal_addon ON jurnalAddOnValue = SupplierInvoiceNumber - JOIN receive_order_po ON SupplierInvoiceReceiveOrderPoID = ReceiveOrderPoID - LEFT JOIN supplier_payment ON SupplierPaymentSupplierInvoiceID = SupplierInvoiceID AND SupplierPaymentIsActive = 'Y' - WHERE - $where - GROUP BY SupplierInvoiceID - ORDER BY SupplierInvoiceID ASC - limit $number_limit offset $number_offset"; + FROM supplier_invoice + LEFT JOIN supplier ON SupplierInvoiceSupplierID = SupplierID + JOIN jurnal_addon ON jurnalAddOnValue = SupplierInvoiceNumber + JOIN receive_order_po ON SupplierInvoiceReceiveOrderPoID = ReceiveOrderPoID + LEFT JOIN supplier_payment ON SupplierPaymentSupplierInvoiceID = SupplierInvoiceID AND SupplierPaymentIsActive = 'Y' + WHERE + $where + GROUP BY SupplierInvoiceID + ORDER BY SupplierInvoiceID ASC + limit $number_limit offset $number_offset"; //echo $sql; $query = $this->db_onedev->query($sql, $sql_param); $rows = $query->result_array(); if ($rows) { foreach ($rows as $k => $v) { $s_payment = $this->db_onedev->query("SELECT GROUP_CONCAT(SupplierPaymentNumber SEPARATOR ', ') as SupplierPaymentNumber, - SUM(IFNULL(SupplierPaymentAmount,0)) as SupplierPaymentAmount, - IFNULL(SupplierPaymentID,0) SupplierPaymentID, - GROUP_CONCAT(DATE_FORMAT(SupplierPaymentDate,'%d-%m-%Y') SEPARATOR ', ') as SupplierPaymentDate - FROM supplier_payment - WHERE SupplierPaymentIsActive = 'Y' AND SupplierPaymentSupplierInvoiceID = {$v['SupplierInvoiceID']}")->row(); + SUM(IFNULL(SupplierPaymentAmount,0)) as SupplierPaymentAmount, + IFNULL(SupplierPaymentID,0) SupplierPaymentID, + GROUP_CONCAT(DATE_FORMAT(SupplierPaymentDate,'%d-%m-%Y') SEPARATOR ', ') as SupplierPaymentDate + FROM supplier_payment + WHERE SupplierPaymentIsActive = 'Y' AND SupplierPaymentSupplierInvoiceID = {$v['SupplierInvoiceID']}")->row(); $s_jurnal = $this->db_onedev->query("SELECT SUM(jurnalTxCredit) totalbill - FROM supplier_invoice - JOIN jurnal_addon ON jurnalAddOnValue = SupplierInvoiceNumber - JOIN jurnal_tx ON jurnalTxJurnalID = jurnalAddOnJurnalID AND jurnalTxCredit <> 0 AND jurnalTxCoaID <> 563 - WHERE SupplierInvoiceID = {$v['SupplierInvoiceID']} - GROUP BY SupplierInvoiceID")->row(); + FROM supplier_invoice + JOIN jurnal_addon ON jurnalAddOnValue = SupplierInvoiceNumber + JOIN jurnal_tx ON jurnalTxJurnalID = jurnalAddOnJurnalID AND jurnalTxCredit <> 0 AND jurnalTxCoaID <> 563 + WHERE SupplierInvoiceID = {$v['SupplierInvoiceID']} + GROUP BY SupplierInvoiceID")->row(); $amount = $s_payment->SupplierPaymentAmount ? $s_payment->SupplierPaymentAmount : "0.00"; $unpaid = (float)$s_jurnal->totalbill - (float)$amount; $rows[$k]['SupplierPaymentID'] = $s_payment->SupplierPaymentID ? $s_payment->SupplierPaymentID : '0'; @@ -255,4 +308,270 @@ class Bill extends MY_Controller $this->sys_ok($result); exit; } + + /** + * search — unified invoice + downpayment list: + * - Only columns the frontend actually reads (no wildcard, no dead aliases) + * - UNION ALL merges supplier_invoice and supplier_downpayment + * - DP rows enriched via SupplierPaymentSupplierDownpaymentID FK + * - All queries use PDO parameterised placeholders + */ + public function search() + { + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + + $prm = $this->sys_input; + $supplier = $prm["supplier"]; + $search = $prm["search"]; + $status = $prm["status"]; + $startdate = $prm["startdate"]; + $enddate = $prm["enddate"]; + $regionalid = $this->sys_user['S_RegionalID']; + + $number_limit = 10; + $number_offset = ((int)$prm['current_page'] - 1) * $number_limit; + + // ── INVOICE WHERE ────────────────────────────────────────── + $inv_where = " SupplierInvoiceIsActive = 'Y' + AND SupplierInvoiceStatus = 'Approved' + AND SupplierInvoiceGrandTotal > 0 + AND SupplierInvoiceIsInstallment = 'N' + AND IF(SupplierPaymentID IS NULL,'N','Y') = ? + AND SupplierInvoiceNumber LIKE ? + AND SupplierName LIKE ? + AND ReceiveOrderPoS_RegionalID = ? + AND SupplierInvoiceDraftPaymentDate BETWEEN ? AND ? "; + + $inv_params = [ + $status, + '%' . $search . '%', + '%' . $supplier . '%', + $regionalid, + $startdate, + $enddate, + ]; + + // ── DOWNPAYMENT WHERE ────────────────────────────────────── + $dp_where = " SupplierDownpaymentIsActive = 'Y' + AND IF(SupplierDownpaymentStatus = 'Paid','Y','N') = ? + AND PurchaseOrderNumber LIKE ? + AND SupplierName LIKE ? + AND PurchaseOrderS_RegionalID = ? + AND SupplierDownpaymentDueDate BETWEEN ? AND ? "; + + $dp_params = [ + $status, + '%' . $search . '%', + '%' . $supplier . '%', + $regionalid, + $startdate, + $enddate, + ]; + + // ── COUNT query — UNION of both sources ──────────────────── + $sql_count = "SELECT SUM(cnt) as total FROM ( + SELECT count(*) as cnt + FROM supplier_invoice + JOIN jurnal_addon ON jurnalAddOnValue = SupplierInvoiceNumber + LEFT JOIN supplier_payment + ON SupplierInvoiceID = SupplierPaymentSupplierInvoiceID + AND SupplierPaymentIsActive = 'Y' + LEFT JOIN supplier ON SupplierInvoiceSupplierID = SupplierID + JOIN receive_order_po ON SupplierInvoiceReceiveOrderPoID = ReceiveOrderPoID + WHERE $inv_where + + UNION ALL + + SELECT count(*) as cnt + FROM supplier_downpayment + JOIN purchase_order ON PurchaseOrderID = SupplierDownpaymentPurchasOrderID + JOIN supplier ON SupplierID = SupplierDownpaymentSupplierID + WHERE $dp_where + ) AS combined"; + + $count_params = array_merge($inv_params, $dp_params); + $query = $this->db_onedev->query($sql_count, $count_params); + + $tot_count = 0; + $tot_page = 0; + if ($query) { + $tot_count = $query->result_array()[0]["total"]; + $tot_page = ceil($tot_count / $number_limit); + } else { + $this->sys_error_db("payment instructions count", $this->db_onedev); + exit; + } + + // ── DATA query — UNION with identical columns ────────────── + $sql_data = " + SELECT * FROM ( + SELECT + SupplierInvoiceID, + SupplierInvoiceNumber, + SupplierInvoiceDraftPaymentDate, + SupplierInvoiceIsLunas as flaglunas, + SupplierName, + DATE_FORMAT(SupplierInvoiceDraftPaymentDate,'%d-%m-%Y') as tanggalbayar, + IFNULL(SupplierPaymentIsApproved,'N') as SupplierPaymentIsApproved, + IFNULL(SupplierPaymentCashierNumber,'') as SupplierPaymentCashierNumber, + 0 as totalbill, + 0 as paid, + 0 as unpaid, + 0 as SupplierPaymentID, + '' as SupplierPaymentNumber, + 0 as SupplierPaymentAmount, + '' as SupplierPaymentDate, + '' as notes, + '' as tagihans, + 'INVOICE' as type + FROM supplier_invoice + LEFT JOIN supplier ON SupplierInvoiceSupplierID = SupplierID + JOIN jurnal_addon ON jurnalAddOnValue = SupplierInvoiceNumber + JOIN receive_order_po ON SupplierInvoiceReceiveOrderPoID = ReceiveOrderPoID + LEFT JOIN supplier_payment + ON SupplierPaymentSupplierInvoiceID = SupplierInvoiceID + AND SupplierPaymentIsActive = 'Y' + WHERE $inv_where + GROUP BY SupplierInvoiceID + + UNION ALL + + SELECT + SupplierDownpaymentID * -1 as SupplierInvoiceID, + CONCAT('DP-', PurchaseOrderNumber) as SupplierInvoiceNumber, + SupplierDownpaymentDueDate as SupplierInvoiceDraftPaymentDate, + SupplierDownpaymentIsLunas as flaglunas, + SupplierName, + DATE_FORMAT(SupplierDownpaymentDueDate,'%d-%m-%Y') as tanggalbayar, + 'N' as SupplierPaymentIsApproved, + '' as SupplierPaymentCashierNumber, + SupplierDownpaymentAmount as totalbill, + CASE WHEN SupplierDownpaymentStatus = 'Paid' + THEN SupplierDownpaymentAmount + ELSE 0 END as paid, + CASE WHEN SupplierDownpaymentStatus = 'Paid' + THEN 0 + ELSE SupplierDownpaymentAmount END as unpaid, + 0 as SupplierPaymentID, + '' as SupplierPaymentNumber, + 0 as SupplierPaymentAmount, + '' as SupplierPaymentDate, + '' as notes, + '' as tagihans, + 'DP' as type + FROM supplier_downpayment + JOIN purchase_order ON PurchaseOrderID = SupplierDownpaymentPurchasOrderID + JOIN supplier ON SupplierID = SupplierDownpaymentSupplierID + WHERE $dp_where + ) AS combined + ORDER BY SupplierInvoiceID ASC + LIMIT ? OFFSET ?"; + + $data_params = array_merge($inv_params, $dp_params, [$number_limit, $number_offset]); + $query = $this->db_onedev->query($sql_data, $data_params); + $rows = $query->result_array(); + + // ── ENRICH — per-row sub-queries ─────────────────────────── + if ($rows) { + foreach ($rows as $k => $v) { + + // ── Downpayment branch ────────────────────────── + if ($v['type'] === 'DP') { + $dp_id = abs($v['SupplierInvoiceID']); + + $s_payment = $this->db_onedev->query(" + SELECT + GROUP_CONCAT(SupplierPaymentNumber SEPARATOR ', ') as SupplierPaymentNumber, + SUM(IFNULL(SupplierPaymentAmount,0)) as SupplierPaymentAmount, + IFNULL(SupplierPaymentID,0) as SupplierPaymentID, + GROUP_CONCAT( + DATE_FORMAT(SupplierPaymentDate,'%d-%m-%Y') + SEPARATOR ', ' + ) as SupplierPaymentDate + FROM supplier_payment + WHERE SupplierPaymentIsActive = 'Y' + AND SupplierPaymentSupplierDownpaymentID = ?", + [$dp_id] + )->row(); + + $amount = $s_payment->SupplierPaymentAmount + ? $s_payment->SupplierPaymentAmount + : "0.00"; + $rows[$k]['SupplierPaymentID'] = $s_payment->SupplierPaymentID + ? $s_payment->SupplierPaymentID + : '0'; + $rows[$k]['SupplierPaymentNumber'] = $s_payment->SupplierPaymentNumber + ? $s_payment->SupplierPaymentNumber + : ''; + $rows[$k]['SupplierPaymentAmount'] = $amount; + $rows[$k]['SupplierPaymentDate'] = $s_payment->SupplierPaymentDate; + $rows[$k]['paid'] = $amount; + $rows[$k]['notes'] = $this->add_notes_downpayment($dp_id); + $rows[$k]['tagihans'] = []; + // totalbill, unpaid already correct from UNION CASE + continue; + } + + // ── Invoice branch ─────────────────────────────── + $inv_id = $v['SupplierInvoiceID']; + + $s_payment = $this->db_onedev->query(" + SELECT + GROUP_CONCAT(SupplierPaymentNumber SEPARATOR ', ') as SupplierPaymentNumber, + SUM(IFNULL(SupplierPaymentAmount,0)) as SupplierPaymentAmount, + IFNULL(SupplierPaymentID,0) as SupplierPaymentID, + GROUP_CONCAT( + DATE_FORMAT(SupplierPaymentDate,'%d-%m-%Y') + SEPARATOR ', ' + ) as SupplierPaymentDate + FROM supplier_payment + WHERE SupplierPaymentIsActive = 'Y' + AND SupplierPaymentSupplierInvoiceID = ?", + [$inv_id] + )->row(); + + $s_jurnal = $this->db_onedev->query(" + SELECT SUM(jurnalTxCredit) totalbill + FROM supplier_invoice + JOIN jurnal_addon ON jurnalAddOnValue = SupplierInvoiceNumber + JOIN jurnal_tx + ON jurnalTxJurnalID = jurnalAddOnJurnalID + AND jurnalTxCredit <> 0 + AND jurnalTxCoaID <> 563 + WHERE SupplierInvoiceID = ? + GROUP BY SupplierInvoiceID", + [$inv_id] + )->row(); + + $amount = $s_payment->SupplierPaymentAmount + ? $s_payment->SupplierPaymentAmount + : "0.00"; + $unpaid = (float)$s_jurnal->totalbill - (float)$amount; + + $rows[$k]['SupplierPaymentID'] = $s_payment->SupplierPaymentID + ? $s_payment->SupplierPaymentID + : '0'; + $rows[$k]['SupplierPaymentNumber'] = $s_payment->SupplierPaymentNumber + ? $s_payment->SupplierPaymentNumber + : ''; + $rows[$k]['SupplierPaymentAmount'] = $amount; + $rows[$k]['SupplierPaymentDate'] = $s_payment->SupplierPaymentDate; + $rows[$k]['paid'] = $amount; + $rows[$k]['totalbill'] = $s_jurnal->totalbill + ? $s_jurnal->totalbill + : "0.00"; + $rows[$k]['unpaid'] = number_format($unpaid, 2, '.', ''); + + $rows[$k]['notes'] = $this->add_notes($inv_id); + $rows[$k]['tagihans'] = $this->add_tagihans($inv_id); + } + } + + $result = array("total" => $tot_page, "records" => $rows); + $this->sys_ok($result); + exit; + } } diff --git a/application/controllers/mockup/supplierpaymentv4/Payment.php b/application/controllers/mockup/supplierpaymentv4/Payment.php index d9512dc..d406719 100644 --- a/application/controllers/mockup/supplierpaymentv4/Payment.php +++ b/application/controllers/mockup/supplierpaymentv4/Payment.php @@ -2,26 +2,26 @@ class Payment extends MY_Controller { - var $db_smartone; - public function index() - { - echo "API"; - } - - public function __construct() - { - parent::__construct(); - $this->db_onedev = $this->load->database("onedev", true); - } - - function lookup_type() - { - //# cek token valid + var $db_smartone; + public function index() + { + echo "API"; + } + + public function __construct() + { + parent::__construct(); + $this->db_onedev = $this->load->database("onedev", true); + } + + function lookup_type() + { + //# cek token valid if (! $this->isLogin) { - $this->sys_error("Invalid Token"); - exit; + $this->sys_error("Invalid Token"); + exit; } - $query = "SELECT coaID as id, + $query = "SELECT coaID as id, coaCode as code, 'N' as chex, coaDescription as chexlabel, @@ -39,73 +39,79 @@ class Payment extends MY_Controller 0 as leftvalue, 0 as rightvalue FROM m_paymenttype WHERE coaIsActive = 'Y'"; - $rows = $this->db_onedev->query($query)->result_array(); - foreach($rows as $k => $v){ - $rows[$k]['selected_card'] = array('id'=>0,'name'=>''); - $rows[$k]['selected_edc'] = array('id'=>0,'name'=>''); - $rows[$k]['selected_account'] = array('id'=>0,'name'=>''); - if($v['chex'] == 'N') - $rows[$k]['chex'] = false; - else - $rows[$k]['chex'] = true; - } - $result = array( - "total" => count($rows) , - "records" => $rows, - ); - $this->sys_ok($result); - exit; - } - function selectpaymenttypeold(){ - - try { - //# cek token valid - if (! $this->isLogin) { - $this->sys_error("Invalid Token"); - exit; - } - $rows = []; - $query ="SELECT * FROM m_paymenttype - WHERE + $que = $this->db_onedev->query($query); + if (!$que) { + $this->sys_error_db("failed to query tipe"); + exit; + } + $rows = $que->result_array(); + foreach ($rows as $k => $v) { + $rows[$k]['selected_card'] = array('id' => 0, 'name' => ''); + $rows[$k]['selected_edc'] = array('id' => 0, 'name' => ''); + $rows[$k]['selected_account'] = array('id' => 0, 'name' => ''); + if ($v['chex'] == 'N') + $rows[$k]['chex'] = false; + else + $rows[$k]['chex'] = true; + } + $result = array( + "total" => count($rows), + "records" => $rows, + ); + $this->sys_ok($result); + exit; + } + + function selectpaymenttypeold() + { + + try { + //# cek token valid + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $rows = []; + $query = "SELECT * FROM m_paymenttype + WHERE coaIsActive = 'Y'"; - //echo $query; - $rows['paymenttypes'] = $this->db_onedev->query($query)->result_array(); - - - $result = array( - "total" => count($rows) , - "records" => $rows, - ); - $this->sys_ok($result); - - - } catch(Exception $exc) { - $message = $exc->getMessage(); - $this->sys_error($message); - } - - } - function selectpaymenttype(){ - - try { - //# cek token valid - if (! $this->isLogin) { - $this->sys_error("Invalid Token"); - exit; - } - $rows = []; - $regionalid = $this->sys_user['S_RegionalID']; - $prm = $this->sys_input; - $search = $prm["search"]; - $query ="SELECT coaID, -coaAccountNo, + //echo $query; + $rows['paymenttypes'] = $this->db_onedev->query($query)->result_array(); + + + $result = array( + "total" => count($rows), + "records" => $rows, + ); + $this->sys_ok($result); + } catch (Exception $exc) { + $message = $exc->getMessage(); + $this->sys_error($message); + } + } + + function selectpaymenttype() + { + + try { + //# cek token valid + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $rows = []; + $regionalid = $this->sys_user['S_RegionalID']; + $prm = $this->sys_input; + $search = $prm["search"]; + $query = "SELECT coaID, +coaAccountNo, coaDescription, coaSubDescription -FROM coa +FROM coa JOIN s_regional ON S_RegionalID = $regionalid JOIN m_branch ON M_BranchS_RegionalID = S_RegionalID JOIN map_bank_coa ON MapBank_CoaID = coaID AND MapBank_BranchCode = M_BranchCode -WHERE +WHERE coaIsActive = 'Y' AND coaIsInput = 'Y' AND coaAccountNo LIKE '111%' AND @@ -113,168 +119,181 @@ coaAccountNo LIKE '11102%' AND coaDescription LIKE '%{$search}%' UNION -SELECT coaID, -coaAccountNo, +SELECT coaID, +coaAccountNo, coaDescription, coaSubDescription -FROM coa -WHERE +FROM coa +WHERE coaIsActive = 'Y' AND coaIsInput = 'Y' AND coaAccountNo LIKE '111%' AND coaAccountNo NOT LIKE '11102%' AND coaDescription LIKE '%{$search}%' ORDER BY coaAccountNo ASC"; - //echo $query; - $rows['paymenttypes'] = $this->db_onedev->query($query)->result_array(); - - - $result = array( - "total" => count($rows) , - "records" => $rows, - ); - $this->sys_ok($result); - - - } catch(Exception $exc) { - $message = $exc->getMessage(); - $this->sys_error($message); - } - - } - function selectbank(){ - - try { - //# cek token valid - if (! $this->isLogin) { - $this->sys_error("Invalid Token"); - exit; - } - $rows = []; - $query =" SELECT * + //echo $query; + $rows['paymenttypes'] = $this->db_onedev->query($query)->result_array(); + + + $result = array( + "total" => count($rows), + "records" => $rows, + ); + $this->sys_ok($result); + } catch (Exception $exc) { + $message = $exc->getMessage(); + $this->sys_error($message); + } + } + + function selectbank() + { + + try { + //# cek token valid + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $rows = []; + $query = " SELECT * FROM nat_bank WHERE Nat_BankIsActive = 'Y' ORDER BY Nat_BankCode DESC "; - //echo $query; - $rows['banks'] = $this->db_onedev->query($query)->result_array(); - - - $result = array( - "total" => count($rows) , - "records" => $rows, - ); - $this->sys_ok($result); - - - } catch(Exception $exc) { - $message = $exc->getMessage(); - $this->sys_error($message); - } - - } - function selectaccount(){ - - try { - //# cek token valid - if (! $this->isLogin) { - $this->sys_error("Invalid Token"); - exit; - } - $rows = []; - $query =" SELECT M_BankAccountID as M_BankAccountID, CONCAT(Nat_BankCode,' (',M_BankAccountNo,')') as M_BankAccountName + //echo $query; + $que = $this->db_onedev->query($query); + if (!$que) { + $this->sys_error_db("failed"); + exit; + } + $rows['banks'] = $que->result_array(); + + + $result = array( + "total" => count($rows), + "records" => $rows, + ); + $this->sys_ok($result); + } catch (Exception $exc) { + $message = $exc->getMessage(); + $this->sys_error($message); + } + } + + function selectaccount() + { + + try { + //# cek token valid + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $rows = []; + $query = " SELECT M_BankAccountID as M_BankAccountID, CONCAT(Nat_BankCode,' (',M_BankAccountNo,')') as M_BankAccountName FROM m_bank_account JOIN nat_bank ON M_BankAccountNat_BankID = Nat_BankID WHERE M_BankAccountIsActive = 'Y' ORDER BY Nat_BankCode DESC"; - //echo $query; - $rows['accounts'] = $this->db_onedev->query($query)->result_array(); - - - $result = array( - "total" => count($rows) , - "records" => $rows, - ); - $this->sys_ok($result); - - - } catch(Exception $exc) { - $message = $exc->getMessage(); - $this->sys_error($message); - } - - } - function lookup_banks() - { - //# cek token valid - if (! $this->isLogin) { - $this->sys_error("Invalid Token"); - exit; + //echo $query; + $que = $this->db_onedev->query($query); + if (!$que) { + $this->sys_error_db("failed"); + exit; + } + $rows['accounts'] = $que->result_array(); + + + $result = array( + "total" => count($rows), + "records" => $rows, + ); + $this->sys_ok($result); + } catch (Exception $exc) { + $message = $exc->getMessage(); + $this->sys_error($message); } - $query = "SELECT Nat_BankID as id, Nat_BankCode as name + } + + function lookup_banks() + { + //# cek token valid + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $query = "SELECT Nat_BankID as id, Nat_BankCode as name FROM nat_bank WHERE Nat_BankIsActive = 'Y' ORDER BY Nat_BankCode DESC"; - $rows = $this->db_onedev->query($query)->result_array(); - - $result = array( - "total" => count($rows) , - "records" => $rows, - ); - $this->sys_ok($result); - exit; - } - - function lookup_accounts() - { - //# cek token valid - if (! $this->isLogin) { - $this->sys_error("Invalid Token"); - exit; + $que = $this->db_onedev->query($query); + if (!$que) { + $this->sys_error_db("failed to query tipe"); + exit; } - $query = "SELECT M_BankAccountID as id, CONCAT(Nat_BankCode,' (',M_BankAccountNo,')') as name + $rows = $que->result_array(); + + $result = array( + "total" => count($rows), + "records" => $rows, + ); + $this->sys_ok($result); + exit; + } + + function lookup_accounts() + { + //# cek token valid + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $query = "SELECT M_BankAccountID as id, CONCAT(Nat_BankCode,' (',M_BankAccountNo,')') as name FROM m_bank_account JOIN nat_bank ON M_BankAccountNat_BankID = Nat_BankID WHERE M_BankAccountIsActive = 'Y' ORDER BY Nat_BankCode DESC"; - $rows = $this->db_onedev->query($query)->result_array(); - - $result = array( - "total" => count($rows) , - "records" => $rows, - ); - $this->sys_ok($result); - exit; - } - - - - function searchcard(){ - if (! $this->isLogin) { - $this->sys_error("Invalid Token"); - exit; - } - $prm = $this->sys_input; + $que = $this->db_onedev->query($query); + if (!$que) { + $this->sys_error_db("failed to query tipe"); + exit; + } + $rows = $que->result_array(); - $max_rst = 12; - $tot_count =0; + $result = array( + "total" => count($rows), + "records" => $rows, + ); + $this->sys_ok($result); + } - $q = [ - 'search' => '%' - ]; + function searchcard() + { + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $prm = $this->sys_input; - if ($prm['search'] != '') - { - $q['search'] = "%{$prm['search']}%"; - } + $max_rst = 12; + $tot_count = 0; - // QUERY TOTAL - if($prm['search'] != ''){ - $sql = " + $q = [ + 'search' => '%' + ]; + + if ($prm['search'] != '') { + $q['search'] = "%{$prm['search']}%"; + } + + // QUERY TOTAL + if ($prm['search'] != '') { + $sql = " SELECT count(*) as total FROM nat_bank WHERE @@ -282,27 +301,25 @@ ORDER BY coaAccountNo ASC"; AND Nat_BankIsActive = 'Y' ORDER BY Nat_BankName DESC "; - } - else{ - $sql = " + } else { + $sql = " SELECT count(*) as total FROM nat_bank WHERE Nat_BankIsActive = 'Y' ORDER BY Nat_BankName DESC "; - } - $query = $this->db_onedev->query($sql,$q['search']); - //echo $query; - if ($query) { - $tot_count = $query->result_array()[0]["total"]; - } - else { - $this->sys_error_db("m_city count",$this->db_onedev); - exit; - } - if($prm['search'] != ''){ - $sql = " + } + $query = $this->db_onedev->query($sql, $q['search']); + //echo $query; + if ($query) { + $tot_count = $query->result_array()[0]["total"]; + } else { + $this->sys_error_db("m_city count", $this->db_onedev); + exit; + } + if ($prm['search'] != '') { + $sql = " SELECT Nat_BankID as id, Nat_BankName as name FROM nat_bank WHERE @@ -310,114 +327,110 @@ ORDER BY coaAccountNo ASC"; AND Nat_BankIsActive = 'Y' ORDER BY Nat_BankName DESC "; - } - else{ - $sql = " + } else { + $sql = " SELECT Nat_BankID as id, Nat_BankName as name FROM nat_bank WHERE Nat_BankIsActive = 'Y' ORDER BY Nat_BankName DESC "; - } - - $query = $this->db_onedev->query($sql, array($q['search'])); + } - if ($query) { - $rows = $query->result_array(); - //echo $this->db_onedev->last_query(); - $result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows)); - $this->sys_ok($result); - } - else { - $this->sys_error_db("m_city rows",$this->db_onedev); - exit; - } - } + $query = $this->db_onedev->query($sql, array($q['search'])); - - function pay() - { - //# cek token valid + if ($query) { + $rows = $query->result_array(); + //echo $this->db_onedev->last_query(); + $result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows)); + $this->sys_ok($result); + } else { + $this->sys_error_db("m_city rows", $this->db_onedev); + exit; + } + } + + function pay() + { + //# cek token valid if (! $this->isLogin) { - $this->sys_error("Invalid Token"); - exit; + $this->sys_error("Invalid Token"); + exit; } //# ambil parameter input - $xuserid = $this->sys_user['M_UserID']; - $prm = $this->sys_input; - $orderid = $prm['orderid']; - $payments = $prm['payments']; - //$xnumber = $this->db_onedev->query("SELECT `fn_numbering`('PAY') as numberx")->row()->numberx; - $sql = "INSERT INTO supplier_payment - (SupplierPaymentSupplierInvoiceID,SupplierPaymentDate,SupplierPaymentCreated,SupplierPaymentUserID) + $xuserid = $this->sys_user['M_UserID']; + $prm = $this->sys_input; + $orderid = $prm['orderid']; + $payments = $prm['payments']; + //$xnumber = $this->db_onedev->query("SELECT `fn_numbering`('PAY') as numberx")->row()->numberx; + $sql = "INSERT INTO supplier_payment + (SupplierPaymentSupplierInvoiceID,SupplierPaymentDate,SupplierPaymentCreated,SupplierPaymentUserID) VALUES (?,CURDATE(),NOW(),?)"; - $query = $this->db_onedev->query($sql, - array( - $orderid, $xuserid - ) - ); + $query = $this->db_onedev->query( + $sql, + array( + $orderid, + $xuserid + ) + ); - if (!$query) { - $this->sys_error_db("supplier_payment insert"); - exit; - } - $headerid = $this->db_onedev->insert_id(); - //echo $headerid; - - foreach($payments as $k => $v){ - if($v['chex']){ - $actual = 0; - $change = 0; - $amount = $v['leftvalue']; - if($v['code'] == 'CASH'){ - $actual = $v['leftvalue']; - $change = $v['rightvalue']; - if($actual > 0){ - $amount = intval($v['leftvalue']) - intval($v['rightvalue']); - } - else{ - $amount = $actual; - } - - $sql = "CALL `sp_bill_payment_add_cash`(".$orderid.",".$amount.",".$amount.",".$headerid.",".$v['id'].",".$xuserid.")"; - $query = $this->db_onedev->query($sql); - - if (!$query) { - $this->sys_error_db("supplier_payment_detail cash insert"); - exit; - } - - } - else{ - if(intval($v['leftvalue']) > 0){ - $actual = 0; - $change = 0; - $amount = $v['leftvalue']; - $selected_card = 0; - $selected_edc = 0; - $selected_account = 0; - if($v['code'] == 'DEBIT' || $v['code'] == 'CREDIT' || $v['code'] == 'TRANSFER'){ - $selected_card = $v['selected_card']['id']; - $selected_edc = $v['selected_edc']['id']; - $selected_account = $v['selected_account']['id']; - } - $sql = "CALL `sp_bill_payment_add_noncash`(".$orderid.",".$amount.",".$amount.",".$headerid.",".$v['id'].",".$xuserid.",".$selected_card.",".$selected_edc.",".$selected_account.")"; - //echo $sql; - - $query = $this->db_onedev->query($sql); - //echo $this->db_onedev->last_query(); - if (!$query) { - $this->sys_error_db("supplier_payment_detail non cash insert"); - exit; - } - } - } - } - } - - $query = "SELECT coaID as id, + if (!$query) { + $this->sys_error_db("supplier_payment insert"); + exit; + } + $headerid = $this->db_onedev->insert_id(); + //echo $headerid; + + foreach ($payments as $k => $v) { + if ($v['chex']) { + $actual = 0; + $change = 0; + $amount = $v['leftvalue']; + if ($v['code'] == 'CASH') { + $actual = $v['leftvalue']; + $change = $v['rightvalue']; + if ($actual > 0) { + $amount = intval($v['leftvalue']) - intval($v['rightvalue']); + } else { + $amount = $actual; + } + + $sql = "CALL `sp_bill_payment_add_cash`(" . $orderid . "," . $amount . "," . $amount . "," . $headerid . "," . $v['id'] . "," . $xuserid . ")"; + $query = $this->db_onedev->query($sql); + + if (!$query) { + $this->sys_error_db("supplier_payment_detail cash insert"); + exit; + } + } else { + if (intval($v['leftvalue']) > 0) { + $actual = 0; + $change = 0; + $amount = $v['leftvalue']; + $selected_card = 0; + $selected_edc = 0; + $selected_account = 0; + if ($v['code'] == 'DEBIT' || $v['code'] == 'CREDIT' || $v['code'] == 'TRANSFER') { + $selected_card = $v['selected_card']['id']; + $selected_edc = $v['selected_edc']['id']; + $selected_account = $v['selected_account']['id']; + } + $sql = "CALL `sp_bill_payment_add_noncash`(" . $orderid . "," . $amount . "," . $amount . "," . $headerid . "," . $v['id'] . "," . $xuserid . "," . $selected_card . "," . $selected_edc . "," . $selected_account . ")"; + //echo $sql; + + $query = $this->db_onedev->query($sql); + //echo $this->db_onedev->last_query(); + if (!$query) { + $this->sys_error_db("supplier_payment_detail non cash insert"); + exit; + } + } + } + } + } + + $query = "SELECT coaID as id, coaCode as code, IF(coaCode = 'CASH','Y','N') as chex, coaDescription as chexlabel, @@ -432,97 +445,191 @@ ORDER BY coaAccountNo ASC"; 0 as leftvalue, 0 as rightvalue FROM m_paymenttype WHERE coaIsActive = 'Y'"; - $rows = $this->db_onedev->query($query)->result_array(); - - foreach($rows as $k => $v){ - if($v['chex'] == 'N') - $rows[$k]['chex'] = false; - else - $rows[$k]['chex'] = true; - } - $xdata = $this->db_onedev->query("SELECT SupplierPaymentID as idx, SupplierPaymentNumber as numberx FROM supplier_payment WHERE SupplierPaymentID = {$headerid}")->row(); - $result = array( - "total" => count($rows) , - "records" => array('types'=>$rows,'data'=>$xdata) - ); - $this->sys_ok($result); - exit; - } - function paymanual() - { - //# cek token valid - if (! $this->isLogin) { - $this->sys_error("Invalid Token"); + $rows = $this->db_onedev->query($query)->result_array(); + + foreach ($rows as $k => $v) { + if ($v['chex'] == 'N') + $rows[$k]['chex'] = false; + else + $rows[$k]['chex'] = true; + } + $xdata = $this->db_onedev->query("SELECT SupplierPaymentID as idx, SupplierPaymentNumber as numberx FROM supplier_payment WHERE SupplierPaymentID = {$headerid}")->row(); + $result = array( + "total" => count($rows), + "records" => array('types' => $rows, 'data' => $xdata) + ); + $this->sys_ok($result); exit; + } + + function payDownpayment() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + + $xuserid = $this->sys_user['M_UserID']; + $prm = $this->sys_input; + $supllierDPID = abs($prm['orderid']); // SupplierDownpaymentID sent as orderid + + $this->db_onedev->trans_begin(); + + $xnumber = $this->db_onedev->query( + "SELECT `fn_numbering`('PINV') as numberx" + )->row()->numberx; + + // Insert payment header (SupplierInvoiceID = 0, linked via Downpayment FK) + $sql = "INSERT INTO supplier_payment ( + SupplierPaymentSupplierInvoiceID, + SupplierPaymentSupplierDownpaymentID, + SupplierPaymentNumber, + SupplierPaymentDate, + SupplierPaymentAmount, + SupplierPaymentCoaID, + SupplierPaymentNote, + SupplierPaymentCreated, + SupplierPaymentUserID + ) VALUES (0, ?, ?, CURDATE(), ?, ?, ?, NOW(), ?)"; + $que = $this->db_onedev->query($sql, [ + $supllierDPID, + $xnumber, + $prm['amount'], + $prm['paymenttype'], + $prm['keterangan'], + $xuserid + ]); + if (!$que) { + $this->db_onedev->trans_rollback(); + $this->sys_error_db("supplier_payment insert for DP"); + exit; + } + $headerid = $this->db_onedev->insert_id(); + + // Mark downpayment as paid + $sql_dp = "UPDATE supplier_downpayment SET + SupplierDownpaymentDueDate = ?, + SupplierDownpaymentStatus = 'Paid', + SupplierDownpaymentDate = CURDATE(), + SupplierDownpaymentLastUpdatedUserID = ? + WHERE SupplierDownpaymentID = ? + AND SupplierDownpaymentIsActive = 'Y' + AND SupplierDownpaymentStatus != 'Paid'"; + $que_dp = $this->db_onedev->query($sql_dp, [ + $prm['tanggalbayar'], + $xuserid, + $supllierDPID + ]); + if (!$que_dp) { + $this->db_onedev->trans_rollback(); + $this->sys_error_db("supplier_downpayment update status"); + exit; + } + + // Audit + $sql_audit = "SELECT * FROM supplier_payment + JOIN m_user ON M_UserID = SupplierPaymentUserID + WHERE SupplierPaymentID = ?"; + $que_audit = $this->db_onedev->query($sql_audit, [$headerid]); + $row = $que_audit->row_array(); + $data = array("header" => $row, "details" => []); + + $message = "Nomor Pembayaran DP: " . $row["SupplierPaymentNumber"] + . " berhasil dibuat oleh " . $row["M_UserUsername"]; + $this->insert_act_log( + "PF", + "NEW", + $message, + $headerid, + $this->safeJsonEncode($data), + $xuserid + ); + + $this->db_onedev->trans_commit(); + + $xdata = $this->db_onedev->query( + "SELECT SupplierPaymentID as idx, SupplierPaymentNumber as numberx + FROM supplier_payment WHERE SupplierPaymentID = ?", + [$headerid] + )->row(); + + $result = array( + "total" => 1, + "records" => array('data' => $xdata) + ); + $this->sys_ok($result); + exit; + } + + function paymanual() + { + //# cek token valid + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; } //# ambil parameter input - $xuserid = $this->sys_user['M_UserID']; - $prm = $this->sys_input; - $orderid = $prm['orderid']; - $amount = $prm['amount']; - $paymenttype = $prm['paymenttype']; - $tanggalbayar = date('Y-m-d', strtotime($prm['tanggalbayar'])); + $xuserid = $this->sys_user['M_UserID']; + $prm = $this->sys_input; + $orderid = $prm['orderid']; + $amount = $prm['amount']; + $paymenttype = $prm['paymenttype']; + $tanggalbayar = date('Y-m-d', strtotime($prm['tanggalbayar'])); - $totalbill = $prm['totalbill']; - $paid = $prm['paid']; - - $keterangan = $prm['keterangan']; - $bills = $prm['bills']; - $xnumber = $this->db_onedev->query("SELECT `fn_numbering`('PINV') as numberx")->row()->numberx; - $sql = "INSERT INTO supplier_payment - (SupplierPaymentSupplierInvoiceID, - SupplierPaymentNumber, - SupplierPaymentDate, - SupplierPaymentAmount, - SupplierPaymentCoaID, - SupplierPaymentNote, - SupplierPaymentCreated, - SupplierPaymentUserID) - VALUES (?, - ?, - CURDATE(), - ?, - ?, - ?, - NOW(), - ?)"; - $query = $this->db_onedev->query($sql, - array( - $orderid, - $xnumber, - $amount, - $paymenttype, - $keterangan, - $xuserid - ) - ); - $headerid = $this->db_onedev->insert_id(); - if (!$query) { - $this->sys_error_db("supplier_payment insert"); - exit; - } else{ - $sqlbill = "UPDATE supplier_invoice SET + $totalbill = $prm['totalbill']; + $paid = $prm['paid']; + + $keterangan = $prm['keterangan']; + $bills = $prm['bills']; + $xnumber = $this->db_onedev->query("SELECT `fn_numbering`('PINV') as numberx")->row()->numberx; + $sql = "INSERT INTO supplier_payment + (SupplierPaymentSupplierInvoiceID, + SupplierPaymentNumber, + SupplierPaymentDate, + SupplierPaymentAmount, + SupplierPaymentCoaID, + SupplierPaymentNote, + SupplierPaymentCreated, + SupplierPaymentUserID) + VALUES (?,?,CURDATE(),?,?,?,NOW(),?)"; + $query = $this->db_onedev->query( + $sql, + array( + $orderid, + $xnumber, + $amount, + $paymenttype, + $keterangan, + $xuserid + ) + ); + $headerid = $this->db_onedev->insert_id(); + if (!$query) { + $this->sys_error_db("supplier_payment insert"); + exit; + } else { + $sqlbill = "UPDATE supplier_invoice SET SupplierInvoiceDraftPaymentDate = '{$tanggalbayar}' WHERE SupplierInvoiceID = $orderid"; - $querybill = $this->db_onedev->query($sqlbill); - - //echo $this->db_onedev->last_query(); + $querybill = $this->db_onedev->query($sqlbill); - } - - //echo $headerid; - - foreach($bills as $k => $v){ - if($v['tagihan_bayar'] > 0){ - $SupplierInvoiceDetailID = $v['SupplierInvoiceDetailID']; - $tagihan_bayar = $v['tagihan_bayar']; - $SupplierInvoiceDetailPurchaseOrderID = $v['SupplierInvoiceDetailPurchaseOrderID']; - $sql = "INSERT INTO supplier_payment_detail( - SupplierPaymentDetailSupplierPaymentID, - SupplierPaymentDetailSupplierInvoiceDetailID, - SupplierPaymentDetailAmount, - SupplierPaymentDetailUserID, + //echo $this->db_onedev->last_query(); + + } + + //echo $headerid; + + foreach ($bills as $k => $v) { + if ($v['tagihan_bayar'] > 0) { + $SupplierInvoiceDetailID = $v['SupplierInvoiceDetailID']; + $tagihan_bayar = $v['tagihan_bayar']; + $SupplierInvoiceDetailPurchaseOrderID = $v['SupplierInvoiceDetailPurchaseOrderID']; + $sql = "INSERT INTO supplier_payment_detail( + SupplierPaymentDetailSupplierPaymentID, + SupplierPaymentDetailSupplierInvoiceDetailID, + SupplierPaymentDetailAmount, + SupplierPaymentDetailUserID, SupplierPaymentDetailCreated, SupplierPaymentDetailLastUpdated) VALUES( @@ -531,46 +638,46 @@ ORDER BY coaAccountNo ASC"; $tagihan_bayar, $xuserid, now(), - now())"; - $query = $this->db_onedev->query($sql); - $billpaymentdetailid = $this->db_onedev->insert_id(); - if (!$query) { - $this->sys_error_db("supplier_payment_detail cash insert"); - exit; - }else{ - $sqlbilldetail = "UPDATE supplier_invoice_detail SET + now())"; + $query = $this->db_onedev->query($sql); + $billpaymentdetailid = $this->db_onedev->insert_id(); + if (!$query) { + $this->sys_error_db("supplier_payment_detail cash insert"); + exit; + } else { + $sqlbilldetail = "UPDATE supplier_invoice_detail SET SupplierInvoiceDetailUnpaid = SupplierInvoiceDetailUnpaid - $tagihan_bayar WHERE SupplierInvoiceDetailID = $SupplierInvoiceDetailID"; - $querybilldetail = $this->db_onedev->query($sqlbilldetail); + $querybilldetail = $this->db_onedev->query($sqlbilldetail); - /* $sqlpayment = "INSERT INTO f_payment + /* $sqlpayment = "INSERT INTO f_payment (F_PaymentPurchaseOrderID, F_PaymentDate, F_PaymentTotal, F_PaymentCreated, F_PaymentLastUpdated, - F_PaymentM_UserID) + F_PaymentM_UserID) VALUES( $SupplierInvoiceDetailPurchaseOrderID, now(), $tagihan_bayar, now(), now(), - $xuserid)"; + $xuserid)"; $querypayment = $this->db_onedev->query($sqlpayment); $paymentid = $this->db_onedev->insert_id(); $sqlpaymentdetail = "INSERT INTO f_paymentdetail (F_PaymentDetailF_PaymentID, - F_PaymentDetailcoaID, + F_PaymentDetailcoaID, F_PaymentDetailAmount, F_PaymentDetailActual, - F_PaymentDetailChange, + F_PaymentDetailChange, F_PaymentDetailEDCNat_BankID, - F_PaymentDetailCardNat_BankID, + F_PaymentDetailCardNat_BankID, F_PaymentDetailM_BankAccountID, - F_PaymentDetailCreated, - F_PaymentDetailLastUpdated, - F_PaymentDetailUserID) + F_PaymentDetailCreated, + F_PaymentDetailLastUpdated, + F_PaymentDetailUserID) VALUES( $paymentid, $paymenttype, @@ -582,275 +689,284 @@ ORDER BY coaAccountNo ASC"; $account, now(), now(), - $xuserid)"; - //echo $sqlpaymentdetail; + $xuserid)"; + //echo $sqlpaymentdetail; $querypaymentdetail = $this->db_onedev->query($sqlpaymentdetail); - $sqleditbillpaymentdetail = "UPDATE supplier_payment_detail SET + $sqleditbillpaymentdetail = "UPDATE supplier_payment_detail SET SupplierPaymentDetailF_PaymentID = $paymentid WHERE SupplierPaymentDetailID = $billpaymentdetailid"; $queryeditbillpaymentdetail = $this->db_onedev->query($sqleditbillpaymentdetail); */ - } - - - } - } - $sql = "SELECT * FROM supplier_payment + } + } + } + + $sql = "SELECT * FROM supplier_payment JOIN m_user ON M_UserID = SupplierPaymentUserID WHERE SupplierPaymentID = ?"; - $query = $this->db_onedev->query($sql, [$headerid]); - $row = $query->row_array(); + $query = $this->db_onedev->query($sql, [$headerid]); + $row = $query->row_array(); - $sql = "SELECT * FROM supplier_payment_detail + $sql = "SELECT * FROM supplier_payment_detail WHERE SupplierPaymentDetailSupplierPaymentID = ?"; - $query = $this->db_onedev->query($sql, [$headerid]); - $rows = $query->row_array(); + $query = $this->db_onedev->query($sql, [$headerid]); + $rows = $query->row_array(); - $data = array("header" => $row, - "details" => $rows); - $message = "Nomor Pembayaran Faktur: " . $row["SupplierPaymentNumber"] ." berhasil dibuat oleh " . $row["M_UserUsername"]; - $this->insert_act_log("PF", "NEW", $message, $headerid, $this->safeJsonEncode($data), $xuserid); - - $xdata = $this->db_onedev->query("SELECT SupplierPaymentID as idx, SupplierPaymentNumber as numberx FROM supplier_payment WHERE SupplierPaymentID = {$headerid}")->row(); - $result = array( - "total" => count($rows) , - "records" => array('data'=>$xdata) - ); - $this->sys_ok($result); - exit; - } - function editpaymanual() - { - //# cek token valid - if (! $this->isLogin) { - $this->sys_error("Invalid Token"); + $data = array( + "header" => $row, + "details" => $rows + ); + $message = "Nomor Pembayaran Faktur: " . $row["SupplierPaymentNumber"] . " berhasil dibuat oleh " . $row["M_UserUsername"]; + $this->insert_act_log("PF", "NEW", $message, $headerid, $this->safeJsonEncode($data), $xuserid); + + $xdata = $this->db_onedev->query("SELECT SupplierPaymentID as idx, SupplierPaymentNumber as numberx FROM supplier_payment WHERE SupplierPaymentID = {$headerid}")->row(); + $result = array( + "total" => count($rows), + "records" => array('data' => $xdata) + ); + $this->sys_ok($result); exit; + } + + function editpaymanual() + { + //# cek token valid + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; } //# ambil parameter input - $xuserid = $this->sys_user['M_UserID']; - $prm = $this->sys_input; - $orderid = $prm['orderid']; - $headerid = $prm['headerid']; - $tanggalbayar = date('Y-m-d', strtotime($prm['tanggalbayar'])); - $sqlbill = "UPDATE supplier_invoice SET + $xuserid = $this->sys_user['M_UserID']; + $prm = $this->sys_input; + $orderid = $prm['orderid']; + $headerid = $prm['headerid']; + $tanggalbayar = date('Y-m-d', strtotime($prm['tanggalbayar'])); + $sqlbill = "UPDATE supplier_invoice SET SupplierInvoiceDraftPaymentDate = '{$tanggalbayar}' WHERE SupplierInvoiceID = $orderid"; - $querybill = $this->db_onedev->query($sqlbill); - - //echo $this->db_onedev->last_query(); + $querybill = $this->db_onedev->query($sqlbill); - - - $sql = "SELECT * FROM supplier_payment + //echo $this->db_onedev->last_query(); + + + + $sql = "SELECT * FROM supplier_payment JOIN supplier_invoice ON SupplierInvoiceID = SupplierPaymentSupplierInvoiceID JOIN m_user ON M_UserID = SupplierPaymentUserID WHERE SupplierPaymentID = ?"; - $query = $this->db_onedev->query($sql, [$headerid]); - $row = $query->row_array(); + $query = $this->db_onedev->query($sql, [$headerid]); + $row = $query->row_array(); - $sql = "SELECT * FROM supplier_payment_detail + $sql = "SELECT * FROM supplier_payment_detail WHERE SupplierPaymentDetailSupplierPaymentID = ?"; - $query = $this->db_onedev->query($sql, [$headerid]); - $rows = $query->row_array(); + $query = $this->db_onedev->query($sql, [$headerid]); + $rows = $query->row_array(); - $data = array("header" => $row, - "details" => $rows); - $message = "Nomor Pembayaran Faktur: " . $row["SupplierPaymentNumber"] ." berhasil diubah oleh " . $row["M_UserUsername"]; - $this->insert_act_log("PF", "NEW", $message, $headerid, $this->safeJsonEncode($data), $xuserid); - - $xdata = $this->db_onedev->query("SELECT SupplierPaymentID as idx, SupplierPaymentNumber as numberx FROM supplier_payment WHERE SupplierPaymentID = {$headerid}")->row(); - $result = array( - "total" => count($rows) , - "records" => array('data'=>$xdata) - ); - $this->sys_ok($result); - exit; - } - function delete_note() - { - //# cek token valid - if (! $this->isLogin) { - $this->sys_error("Invalid Token"); + $data = array( + "header" => $row, + "details" => $rows + ); + $message = "Nomor Pembayaran Faktur: " . $row["SupplierPaymentNumber"] . " berhasil diubah oleh " . $row["M_UserUsername"]; + $this->insert_act_log("PF", "NEW", $message, $headerid, $this->safeJsonEncode($data), $xuserid); + + $xdata = $this->db_onedev->query("SELECT SupplierPaymentID as idx, SupplierPaymentNumber as numberx FROM supplier_payment WHERE SupplierPaymentID = {$headerid}")->row(); + $result = array( + "total" => count($rows), + "records" => array('data' => $xdata) + ); + $this->sys_ok($result); exit; + } + + function delete_note() + { + //# cek token valid + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; } //# ambil parameter input - $xuserid = $this->sys_user['M_UserID']; - $prm = $this->sys_input; - $prmnota = $prm['nota']; + $xuserid = $this->sys_user['M_UserID']; + $prm = $this->sys_input; + $prmnota = $prm['nota']; - $headerid = $prmnota['note_id']; - $sql = "SELECT * FROM supplier_payment + $headerid = $prmnota['note_id']; + $sql = "SELECT * FROM supplier_payment JOIN m_user ON M_UserID = SupplierPaymentUserID WHERE SupplierPaymentID = ?"; - $query = $this->db_onedev->query($sql, [$headerid]); - $row = $query->row_array(); + $query = $this->db_onedev->query($sql, [$headerid]); + $row = $query->row_array(); - $sql = "SELECT * FROM supplier_payment_detail + $sql = "SELECT * FROM supplier_payment_detail WHERE SupplierPaymentDetailSupplierPaymentID = ?"; - $query = $this->db_onedev->query($sql, [$headerid]); - $rows = $query->row_array(); + $query = $this->db_onedev->query($sql, [$headerid]); + $rows = $query->row_array(); - $data = array("header" => $row, - "details" => $rows); + $data = array( + "header" => $row, + "details" => $rows + ); - $sql = "UPDATE supplier_payment + $sql = "UPDATE supplier_payment SET SupplierPaymentIsActive = 'N' WHERE SupplierPaymentID = {$prmnota['note_id']}"; - //echo $sql; - $query = $this->db_onedev->query($sql); - if (!$query) { - $this->sys_error_db("supplier_payment delete"); - exit; - } - - $sql = "UPDATE supplier_payment_detail - SET SupplierPaymentDetailIsActive = 'N' - WHERE SupplierPaymentDetailSupplierPaymentID = {$prmnota['note_id']}"; - //echo $sql; - $query = $this->db_onedev->query($sql); - if (!$query) { - $this->sys_error_db("supplier_payment_detail delete"); - exit; - } + //echo $sql; + $query = $this->db_onedev->query($sql); + if (!$query) { + $this->sys_error_db("supplier_payment delete"); + exit; + } - $sql = "UPDATE supplier_invoice + $sql = "UPDATE supplier_payment_detail + SET SupplierPaymentDetailIsActive = 'N' + WHERE SupplierPaymentDetailSupplierPaymentID = {$prmnota['note_id']}"; + //echo $sql; + $query = $this->db_onedev->query($sql); + if (!$query) { + $this->sys_error_db("supplier_payment_detail delete"); + exit; + } + + $sql = "UPDATE supplier_invoice SET SupplierInvoiceUnpaid = SupplierInvoiceUnpaid + CAST({$prmnota['note_amount']} AS UNSIGNED) WHERE SupplierInvoiceID = {$prmnota['note_order_id']}"; - //echo $sql; - $query = $this->db_onedev->query($sql); - if (!$query) { - $this->sys_error_db("supplier_invoice delete"); - exit; - } + //echo $sql; + $query = $this->db_onedev->query($sql); + if (!$query) { + $this->sys_error_db("supplier_invoice delete"); + exit; + } - $sql = "UPDATE supplier_invoice_detail + $sql = "UPDATE supplier_invoice_detail SET SupplierInvoiceDetailUnpaid = SupplierInvoiceDetailUnpaid + CAST({$prmnota['note_amount']} AS UNSIGNED) WHERE SupplierInvoiceDetailID = {$prmnota['detail_id']}"; - //echo $sql; - $query = $this->db_onedev->query($sql); - if (!$query) { - $this->sys_error_db("supplier_invoice_detail delete"); - exit; - } - - $message = "Nomor Pembayaran Faktur: " . $row["SupplierPaymentNumber"] ." telah dihapus oleh " . $row["M_UserUsername"]; - $this->insert_act_log("PF", "DELETE", $message, $headerid, $this->safeJsonEncode($data), $xuserid); - $result = array( - "total" => 1 , - "records" => array('prm'=>$prm) - ); - $this->sys_ok($result); - exit; - } - function edit_note() - { - //# cek token valid - if (! $this->isLogin) { - $this->sys_error("Invalid Token"); + //echo $sql; + $query = $this->db_onedev->query($sql); + if (!$query) { + $this->sys_error_db("supplier_invoice_detail delete"); + exit; + } + + $message = "Nomor Pembayaran Faktur: " . $row["SupplierPaymentNumber"] . " telah dihapus oleh " . $row["M_UserUsername"]; + $this->insert_act_log("PF", "DELETE", $message, $headerid, $this->safeJsonEncode($data), $xuserid); + $result = array( + "total" => 1, + "records" => array('prm' => $prm) + ); + $this->sys_ok($result); exit; + } + + function edit_note() + { + //# cek token valid + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; } //# ambil parameter input - $xuserid = $this->sys_user['M_UserID']; - $prm = $this->sys_input; - $id = $prm['id']; - $inv_id = $prm['inv_id']; - $detail_id = $prm['detail_id']; - $paymenttype = $prm['paymenttype']; - $amount_old = $prm['amount_old']; - $amount_new = $prm['amount_new']; - $keterangan = $prm['keterangan']; - - $datas_log = []; - $messages_log = []; + $xuserid = $this->sys_user['M_UserID']; + $prm = $this->sys_input; + $id = $prm['id']; + $inv_id = $prm['inv_id']; + $detail_id = $prm['detail_id']; + $paymenttype = $prm['paymenttype']; + $amount_old = $prm['amount_old']; + $amount_new = $prm['amount_new']; + $keterangan = $prm['keterangan']; - $sql = "SELECT * - FROM supplier_payment + $datas_log = []; + $messages_log = []; + + $sql = "SELECT * + FROM supplier_payment WHERE SupplierPaymentID = ?"; - $query = $this->db_onedev->query($sql, [$id]); - if (!$query) { - $this->db_onedev->trans_rollback(); - $this->sys_error_db("supplier payment", $this->db_onedev); - exit; - } - $row = $query->row_array(); + $query = $this->db_onedev->query($sql, [$id]); + if (!$query) { + $this->db_onedev->trans_rollback(); + $this->sys_error_db("supplier payment", $this->db_onedev); + exit; + } + $row = $query->row_array(); - if($row["SupplierPaymentAmount"]!= $amount_new) { - $messages_log[] = "Perubahan pembayaran : " . $row["SupplierPaymentAmount"] . " menjadi " . $amount_new; - } - if($row["SupplierPaymentNote"]!= $keterangan) { - $messages_log[] = "Perubahan keterangan : " . $row["SupplierPaymentNote"] . " menjadi " . $keterangan; - } - if($row["SupplierPaymentCoaID"]!= $paymenttype) { - $messages_log[] = "Perubahan tipe pembayaran id : " . $row["SupplierPaymentCoaID"] . " menjadi " . $paymenttype; - } + if ($row["SupplierPaymentAmount"] != $amount_new) { + $messages_log[] = "Perubahan pembayaran : " . $row["SupplierPaymentAmount"] . " menjadi " . $amount_new; + } + if ($row["SupplierPaymentNote"] != $keterangan) { + $messages_log[] = "Perubahan keterangan : " . $row["SupplierPaymentNote"] . " menjadi " . $keterangan; + } + if ($row["SupplierPaymentCoaID"] != $paymenttype) { + $messages_log[] = "Perubahan tipe pembayaran id : " . $row["SupplierPaymentCoaID"] . " menjadi " . $paymenttype; + } - $datas_log['header'] = $row; + $datas_log['header'] = $row; - $sql = "UPDATE supplier_payment SET - SupplierPaymentAmount = {$amount_new}, + $sql = "UPDATE supplier_payment SET + SupplierPaymentAmount = {$amount_new}, SupplierPaymentNote = '{$keterangan}', SupplierPaymentCoaID = {$paymenttype}, SupplierPaymentUserID = {$xuserid} WHERE SupplierPaymentID = {$id}"; - //echo $sql; - $query = $this->db_onedev->query($sql); - if (!$query) { - $this->sys_error_db("supplier_payment edit"); - exit; - } - - $sql = "UPDATE supplier_payment_detail + //echo $sql; + $query = $this->db_onedev->query($sql); + if (!$query) { + $this->sys_error_db("supplier_payment edit"); + exit; + } + + $sql = "UPDATE supplier_payment_detail SET SupplierPaymentDetailAmount = {$amount_new}, SupplierPaymentDetailUserID = {$xuserid} WHERE SupplierPaymentDetailSupplierPaymentID = {$id}"; - //echo $sql; - $query = $this->db_onedev->query($sql); - if (!$query) { - $this->sys_error_db("supplier_payment_detail edit"); - exit; - } - - $sql = "UPDATE supplier_invoice - SET SupplierInvoiceUnpaid = (SupplierInvoiceUnpaid + $amount_old) - {$amount_new} - WHERE SupplierInvoiceID = {$inv_id}"; - //echo $sql; - $query = $this->db_onedev->query($sql); - if (!$query) { - $this->sys_error_db("supplier_invoice edit"); - exit; - } + //echo $sql; + $query = $this->db_onedev->query($sql); + if (!$query) { + $this->sys_error_db("supplier_payment_detail edit"); + exit; + } - $sql = "UPDATE supplier_invoice_detail - SET SupplierInvoiceDetailUnpaid = (SupplierInvoiceDetailUnpaid + $amount_old) - {$amount_new} + $sql = "UPDATE supplier_invoice + SET SupplierInvoiceUnpaid = (SupplierInvoiceUnpaid + $amount_old) - {$amount_new} + WHERE SupplierInvoiceID = {$inv_id}"; + //echo $sql; + $query = $this->db_onedev->query($sql); + if (!$query) { + $this->sys_error_db("supplier_invoice edit"); + exit; + } + + $sql = "UPDATE supplier_invoice_detail + SET SupplierInvoiceDetailUnpaid = (SupplierInvoiceDetailUnpaid + $amount_old) - {$amount_new} WHERE SupplierInvoiceDetailID = {$detail_id}"; - //echo $sql; - $query = $this->db_onedev->query($sql); - if (!$query) { - $this->sys_error_db("supplier_invoice_detail edit"); - exit; - } - if(count($messages_log) > 0) { - $message = "Perubahan Pembayaran Faktur: " . $row["SupplierPaymentNumber"] . "\n"; - $message .= implode("\n", $messages_log); - }else{ - $message = "Pembayaran Faktur: " . $row["SupplierPaymentNumber"] . " tanpa perubahan"; - } - - $datas_log = $this->convertNumericValuesToStrings($datas_log); - $this->insert_act_log("PF", "EDIT", $message, $id, $this->safeJsonEncode($datas_log), $xuserid); - $result = array( - "total" => 1 , - "records" => array('prm'=>$prm) - ); - $this->sys_ok($result); - exit; - } - function insert_act_log($code, $status, $description, $refId, $data, $userId) + //echo $sql; + $query = $this->db_onedev->query($sql); + if (!$query) { + $this->sys_error_db("supplier_invoice_detail edit"); + exit; + } + if (count($messages_log) > 0) { + $message = "Perubahan Pembayaran Faktur: " . $row["SupplierPaymentNumber"] . "\n"; + $message .= implode("\n", $messages_log); + } else { + $message = "Pembayaran Faktur: " . $row["SupplierPaymentNumber"] . " tanpa perubahan"; + } + + $datas_log = $this->convertNumericValuesToStrings($datas_log); + $this->insert_act_log("PF", "EDIT", $message, $id, $this->safeJsonEncode($datas_log), $xuserid); + $result = array( + "total" => 1, + "records" => array('prm' => $prm) + ); + $this->sys_ok($result); + exit; + } + + function insert_act_log($code, $status, $description, $refId, $data, $userId) { $sql = "INSERT INTO user_activity( UserActivityCode, @@ -859,7 +975,7 @@ ORDER BY coaAccountNo ASC"; UserActivityRefID, UserActivityData, UserActivityUserID, - UserActivityCreated) + UserActivityCreated) VALUES (?,?,?,?,?,?,?)"; $query = $this->db_onedev->query($sql, [$code, $status, $description, $refId, $data, $userId, date("Y-m-d H:i:s")]); if (!$query) { @@ -867,21 +983,23 @@ ORDER BY coaAccountNo ASC"; exit; } } - private function safeJsonEncode($data) { + + private function safeJsonEncode($data) + { // Coba encode data ke JSON $jsonData = json_encode($data); - + // Cek apakah terjadi error saat encode if (json_last_error() !== JSON_ERROR_NONE) { $errorMsg = json_last_error_msg(); error_log("JSON encode error: " . $errorMsg); - + // Lakukan sanitasi dan perbaikan data $fixedData = $this->fixJsonEncodeIssues($data, $errorMsg); - + // Coba encode lagi setelah diperbaiki $jsonData = json_encode($fixedData); - + // Jika masih error, log dan kembalikan objek kosong if (json_last_error() !== JSON_ERROR_NONE) { error_log("Failed to fix JSON encode issues: " . json_last_error_msg()); @@ -889,15 +1007,16 @@ ORDER BY coaAccountNo ASC"; return '{}'; } } - + return $jsonData; } // Fungsi untuk memperbaiki masalah encoding JSON - private function fixJsonEncodeIssues($data, $errorMsg) { + private function fixJsonEncodeIssues($data, $errorMsg) + { // Buat salinan data untuk dimodifikasi $fixedData = $data; - + // Tangani berbagai jenis error if (strpos($errorMsg, 'Malformed UTF-8') !== false) { // Perbaiki masalah karakter UTF-8 @@ -908,16 +1027,17 @@ ORDER BY coaAccountNo ASC"; } else { // Konversi semua nilai numerik menjadi string untuk menghindari masalah presisi $fixedData = $this->convertNumericValuesToStrings($fixedData); - + // Perbaiki masalah referensi recursif $fixedData = $this->fixRecursiveReferences($fixedData); } - + return $fixedData; } // Perbaiki masalah karakter UTF-8 - private function fixUTF8Issues($data) { + private function fixUTF8Issues($data) + { if (is_string($data)) { return mb_convert_encoding($data, 'UTF-8', 'UTF-8'); } else if (is_array($data)) { @@ -929,7 +1049,8 @@ ORDER BY coaAccountNo ASC"; } // Perbaiki masalah nilai Infinity atau NaN - private function fixInfNanIssues($data) { + private function fixInfNanIssues($data) + { if (is_array($data)) { foreach ($data as $key => $value) { if (is_float($value) && (is_nan($value) || is_infinite($value))) { @@ -943,12 +1064,13 @@ ORDER BY coaAccountNo ASC"; } // Perbaiki masalah referensi recursif - private function fixRecursiveReferences($data, $depth = 0) { + private function fixRecursiveReferences($data, $depth = 0) + { // Batasi kedalaman rekursi untuk menghindari infinite loop if ($depth > 50) { return "[MAX_DEPTH_REACHED]"; } - + if (is_array($data)) { $result = []; foreach ($data as $key => $value) { @@ -960,12 +1082,13 @@ ORDER BY coaAccountNo ASC"; } return $result; } - + return $data; } // Cari dan konversi numerik ke string secara rekursif - private function convertNumericValuesToStrings($data) { + private function convertNumericValuesToStrings($data) + { if (is_array($data)) { foreach ($data as $key => $value) { if (is_array($value)) {