804 lines
40 KiB
PHP
804 lines
40 KiB
PHP
<?php
|
|
class Bill extends MY_Controller
|
|
{
|
|
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,
|
|
SupplierPaymentID as note_id,
|
|
SupplierPaymentDetailSupplierInvoiceDetailID as detail_id,
|
|
SupplierPaymentDate as note_date,
|
|
SupplierPaymentNumber as note_number,
|
|
GROUP_CONCAT(DISTINCT coaDescription separator ' , ') as paymenttypes_name,
|
|
SUM(SupplierPaymentDetailAmount) as note_amount,
|
|
n.M_UserUsername as note_user,
|
|
SupplierPaymentDetailIsActive as note_active,
|
|
'xxx' as tests,
|
|
'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_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_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_notes_installment($orderid)
|
|
{
|
|
$sql = "SELECT SupplierPaymentSupplierInstallmentID as note_order_id,
|
|
SupplierPaymentID as note_id,
|
|
SupplierInstallmentID as detail_id,
|
|
SupplierPaymentDate as note_date,
|
|
SupplierPaymentNumber as note_number,
|
|
GROUP_CONCAT(DISTINCT coaDescription separator ' , ') as paymenttypes_name,
|
|
SUM(SupplierInstallmentAmount) as note_amount,
|
|
n.M_UserUsername as note_user,
|
|
SupplierInstallmentIsActive 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_installment
|
|
ON SupplierInstallmentID = SupplierPaymentSupplierInstallmentID
|
|
AND SupplierInstallmentIsActive = '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 SupplierPaymentSupplierInstallmentID = {$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 Installment notes", $this->db_onedev);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function add_tagihans($orderid)
|
|
{
|
|
$sql_dp = "SELECT
|
|
Fa_ClassDownPaymentCoaID
|
|
FROM supplier_invoice_detail
|
|
JOIN m_item
|
|
ON M_ItemID = SupplierInvoiceDetailItemID
|
|
AND SupplierInvoiceDetailSupplierInvoiceID = ?
|
|
AND SupplierInvoiceDetailIsActive = 'Y'
|
|
AND SupplierInvoiceDetailIsVerified = 'Y'
|
|
JOIN fa_class
|
|
ON Fa_ClassID = M_ItemFa_ClassID
|
|
AND Fa_ClassIsActive = 'Y'";
|
|
$que_dp = $this->db_onedev->query($sql_dp, [$orderid]);
|
|
if (!$que_dp) {
|
|
$this->sys_error_db("failed to get coa id dp tagihans", $this->db_onedev);
|
|
exit;
|
|
}
|
|
$dataDP = $que_dp->result_array();
|
|
$excludeIds = array_column($dataDP, 'Fa_ClassDownPaymentCoaID');
|
|
$excludeIds[] = 563; // always excluded
|
|
$excludeIds = array_unique($excludeIds);
|
|
$excludeIds = array_values($excludeIds); // re-index
|
|
|
|
$notInPlaceholders = implode(',', array_fill(0, count($excludeIds), '?'));
|
|
|
|
$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 receive_order_po
|
|
ON ReceiveOrderPoID = SupplierInvoiceReceiveOrderPoID
|
|
JOIN receive_order_po_detail
|
|
ON ReceiveOrderPoDetailReceiveOrderPoID = ReceiveOrderPoID
|
|
JOIN purchase_order
|
|
ON ReceiveOrderPoDetailPurchaseOrderID = PurchaseOrderID
|
|
JOIN jurnal_addon
|
|
ON jurnalAddOnValue = SupplierInvoiceNumber
|
|
AND jurnalAddOnIsActive = 'Y'
|
|
JOIN jurnal_tx
|
|
ON jurnalTxJurnalID = jurnalAddOnJurnalID
|
|
AND jurnalTxIsActive = 'Y'
|
|
AND jurnalTxCredit <> 0
|
|
AND jurnalTxCoaID NOT IN ($notInPlaceholders)
|
|
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, array_merge($excludeIds, [$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'];
|
|
|
|
$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 ? ";
|
|
|
|
$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";
|
|
|
|
$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;
|
|
}
|
|
|
|
// ── 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 ?";
|
|
|
|
$data_params = array_merge($where_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) {
|
|
$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;
|
|
}
|
|
|
|
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];
|
|
|
|
// ── INSTALLMENT branch WHERE ────────────────────────────
|
|
$inst_where = " inst.SupplierInstallmentIsActive = 'Y'
|
|
AND rop.ReceiveOrderPoS_RegionalID = ? ";
|
|
|
|
$inst_params = [$regionalid];
|
|
|
|
// ── UNION base ───────────────────────────────────────────
|
|
$sql_base = "
|
|
SELECT
|
|
si.SupplierInvoiceID,
|
|
0 AS DownpaymentID,
|
|
0 AS InstallmentID,
|
|
0 AS parent_invoice_id,
|
|
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
|
|
0 AS SupplierInvoiceID,
|
|
dp.SupplierDownpaymentID AS DownpaymentID,
|
|
0 AS InstallmentID,
|
|
0 AS parent_invoice_id,
|
|
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
|
|
|
|
UNION ALL
|
|
|
|
SELECT
|
|
0 AS SupplierInvoiceID,
|
|
0 AS DownpaymentID,
|
|
inst.SupplierInstallmentID AS InstallmentID,
|
|
si_inst.SupplierInvoiceID AS parent_invoice_id,
|
|
CONCAT('INST-', si_inst.SupplierInvoiceNumber) AS SupplierInvoiceNumber,
|
|
inst.SupplierInstallmentDueDate AS SupplierInvoiceDraftPaymentDate,
|
|
inst.SupplierInstallmentDueDate AS filter_date,
|
|
inst.SupplierInstallmentIsLunas AS flaglunas,
|
|
si_inst.SupplierInvoiceSupplierInvoiceNumber AS SupplierInvoiceSupplierInvoiceNumber,
|
|
DATE_FORMAT(
|
|
IFNULL(si_inst.SupplierInvoiceSupplierInvoiceDate,''),'%d-%m-%Y'
|
|
) AS tanggalinvoice,
|
|
inst_sup.SupplierName AS SupplierName,
|
|
IFNULL(sp.SupplierPaymentID,0) AS SupplierPaymentID,
|
|
IFNULL(sp.SupplierPaymentIsConfirm,'N') AS SupplierPaymentIsConfirm,
|
|
IFNULL(sp.SupplierPaymentIsApproved,'N') AS SupplierPaymentIsApproved,
|
|
IFNULL(sp.SupplierPaymentIsVerif,'N') AS SupplierPaymentIsVerif,
|
|
IFNULL(sp.SupplierPaymentIsActive,'Y') AS SupplierPaymentIsActive,
|
|
IFNULL(sp.SupplierPaymentCashierNumber,'') AS SupplierPaymentCashierNumber,
|
|
DATE_FORMAT(
|
|
inst.SupplierInstallmentDueDate,'%d-%m-%Y'
|
|
) AS tanggalbayar,
|
|
inst.SupplierInstallmentAmount AS totalbill,
|
|
CASE WHEN inst.SupplierInstallmentStatus = 'Paid'
|
|
THEN inst.SupplierInstallmentAmount
|
|
ELSE 0 END AS paid,
|
|
CASE WHEN inst.SupplierInstallmentStatus = 'Paid'
|
|
THEN 0
|
|
ELSE inst.SupplierInstallmentAmount END AS unpaid,
|
|
'' AS SupplierPaymentNumber,
|
|
0 AS SupplierPaymentAmount,
|
|
'' AS SupplierPaymentDate,
|
|
'' AS notes,
|
|
'' AS tagihans,
|
|
0 AS xrounding,
|
|
'' AS chex,
|
|
'INSTALLMENT' AS type
|
|
FROM supplier_installment inst
|
|
LEFT JOIN supplier_payment sp
|
|
ON sp.SupplierPaymentSupplierInstallmentID = inst.SupplierInstallmentID
|
|
AND sp.SupplierPaymentIsActive = 'Y'
|
|
JOIN supplier_invoice si_inst
|
|
ON si_inst.SupplierInvoiceID = inst.SupplierInstallmentSupplierInvoiceID
|
|
JOIN receive_order_po rop
|
|
ON rop.ReceiveOrderPoID = si_inst.SupplierInvoiceReceiveOrderPoID
|
|
JOIN supplier inst_sup
|
|
ON inst_sup.SupplierID = inst.SupplierInstallmentSupplierID
|
|
WHERE $inst_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, $inst_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, $inst_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 = $v['DownpaymentID'];
|
|
|
|
$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;
|
|
}
|
|
|
|
// ── Installment branch ─────────────────────────
|
|
if ($v['type'] === 'INSTALLMENT') {
|
|
$rows[$k]['chex'] = false;
|
|
$inst_id = $v['InstallmentID'];
|
|
$parent_invoice_id = $v['parent_invoice_id'];
|
|
|
|
$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 SupplierPaymentSupplierInstallmentID = ?",
|
|
[$inst_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_installment($inst_id);
|
|
$rows[$k]['tagihans'] = $this->add_tagihans($parent_invoice_id);
|
|
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;
|
|
}
|
|
}
|