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, M_UserUsername as note_user, SupplierPaymentDetailIsActive as note_active, 'xxx' as tests, 'N' as show_detail, SupplierPaymentNote as keterangan, SupplierPaymentCoaID, coaID, coaDescription, SupplierPaymentIsConfirm FROM supplier_payment JOIN supplier_payment_detail ON SupplierPaymentDetailSupplierPaymentID = SupplierPaymentID JOIN coa ON SupplierPaymentCoaID = coaID LEFT JOIN m_user ON SupplierPaymentUserID = 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, '' as pasien, CAST(IFNULL(SupplierInvoiceGrandTotal,0) AS UNSIGNED) as tagihan_total, CAST(IFNULL(SupplierInvoiceUnpaid,0) AS UNSIGNED) as tagihan_tagihan, 0 as tagihan_bayar, DATE_FORMAT(SupplierInvoiceDueDate,'%d-%m-%Y') as tagihan_duedate, SupplierInvoiceIsActive as tagihan_active, 'N' as show_detail, SupplierInvoiceDetailID, SupplierInvoiceDetailPurchaseOrderID FROM supplier_invoice JOIN supplier_invoice_detail ON SupplierInvoiceDetailSupplierInvoiceID = SupplierInvoiceID AND SupplierInvoiceDetailIsActive = 'Y' JOIN purchase_order ON SupplierInvoicePurchaseOrderID = PurchaseOrderID WHERE SupplierInvoiceID = {$orderid} GROUP BY SupplierInvoiceID"; $query = $this->db_onedev->query($sql); 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; } $prm = $this->sys_input; $supplier = $prm["supplier"]; $search = $prm["search"]; $status = $prm["status"]; $regionalid = $this->sys_user['S_RegionalID']; $number_limit = 10; $number_offset = ($prm['current_page'] - 1) * $number_limit ; $where = "(SupplierInvoiceIsActive = 'Y' AND SupplierInvoiceGrandTotal > 0 AND SupplierInvoiceIsLunas = '{$status}' AND (SupplierInvoiceNumber LIKE '%{$search}%' OR SupplierPaymentNumber LIKE '%{$search}%') AND SupplierName LIKE '%{$supplier}%') AND PurchaseOrderS_RegionalID = {$regionalid}"; $sql = " SELECT count(*) as total FROM supplier_invoice LEFT JOIN supplier_payment ON SupplierInvoiceID = SupplierPaymentSupplierInvoiceID AND SupplierPaymentIsActive = 'Y' LEFT JOIN supplier ON SupplierInvoiceSupplierID = SupplierID JOIN purchase_order ON PurchaseOrderID = SupplierInvoicePurchaseOrderID WHERE $where "; // echo $sql; $query = $this->db_onedev->query($sql, $sql_param); $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; } $sql = "SELECT supplier_invoice.*, supplier_payment.*, SupplierName, '' M_MouName, CAST(IFNULL(SupplierInvoiceGrandTotal,0) AS UNSIGNED) as totalbill, CAST(IFNULL(SupplierInvoiceGrandTotal,0) AS UNSIGNED) - CAST(IFNULL(SupplierInvoiceUnpaid,0) AS UNSIGNED) as paid, CAST(IFNULL(SupplierInvoiceUnpaid,0) AS UNSIGNED) as unpaid, SupplierInvoiceIsLunas as flaglunas, GROUP_CONCAT(SupplierPaymentNumber SEPARATOR ', ') as SupplierPaymentNumber, SUM(CAST(IFNULL(SupplierPaymentAmount,0) AS UNSIGNED)) as SupplierPaymentAmount, DATE_FORMAT(SupplierPaymentDate,'%d-%m-%Y') as SupplierPaymentDate, '' as SupplierInvoiceIssueRefNumber, '' as notes, '' as tagihans, 'N' as isbillterpusat FROM supplier_invoice LEFT JOIN supplier_payment ON SupplierInvoiceID = SupplierPaymentSupplierInvoiceID AND SupplierPaymentIsActive = 'Y' LEFT JOIN supplier ON SupplierInvoiceSupplierID = SupplierID JOIN purchase_order ON PurchaseOrderID = SupplierInvoicePurchaseOrderID 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]['notes'] = $this->add_notes($v['SupplierInvoiceID']); $rows[$k]['tagihans'] = $this->add_tagihans($v['SupplierInvoiceID']); } } $result = array("total" => $tot_page, "records" => $rows, "sql"=> $this->db_onedev->last_query()); $this->sys_ok($result); exit; } }