feature: po pay downpayment
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user