234 lines
7.6 KiB
PHP
234 lines
7.6 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 *,
|
|
GROUP_CONCAT(DISTINCT M_PaymentTypeName separator ' , ') as paymenttypes_name,
|
|
SUM(F_BillPaymentDetailAmount) as note_amount
|
|
FROM
|
|
(SELECT F_BillPaymentF_BillID as note_order_id,
|
|
F_BillPaymentID as note_id,
|
|
F_BillPaymentDate as note_date,
|
|
F_BillPaymentNumber as note_number,
|
|
M_PaymentTypeName as M_PaymentTypeName,
|
|
F_BillPaymentDetailAmount,
|
|
M_UserFullName as note_user,
|
|
F_BillPaymentDetailIsActive as note_active,
|
|
'xxx' as tests,
|
|
'N' as show_detail,
|
|
IF(F_BillPaymentReturID IS NULL,'N','Y') as isretur,
|
|
'bill' as tipe
|
|
FROM f_bill_payment
|
|
JOIN f_bill_payment_detail ON F_BillPaymentDetailF_BillPaymentID = F_BillPaymentID
|
|
LEFT JOIN f_bill_payment_retur ON F_BillPaymentReturF_BillPaymentID = F_BillPaymentID
|
|
JOIN m_paymenttype ON F_BillPaymentM_PaymentTypeID = M_PaymentTypeID
|
|
LEFT JOIN m_user ON F_BillPaymentUserID = M_UserID
|
|
WHERE
|
|
F_BillPaymentF_BillID = {$orderid}
|
|
|
|
UNION
|
|
SELECT F_BillPaymentReturF_BillID as note_order_id,
|
|
F_BillPaymentReturID as note_id,
|
|
F_BillPaymentReturDate as note_date,
|
|
F_BillPaymentReturNumber as note_number,
|
|
M_PaymentTypeName,
|
|
F_BillPaymentReturDetailAmount as F_BillPaymentDetailAmount,
|
|
M_UserFullName as note_user,
|
|
F_BillPaymentReturDetailIsActive as note_active,
|
|
'xxx' as tests,
|
|
'N' as show_detail,
|
|
'Y' as isretur,
|
|
'retur' as tipe
|
|
FROM f_bill_payment_retur
|
|
JOIN f_bill_payment_retur_detail ON F_BillPaymentReturDetailF_BillPaymentReturID = F_BillPaymentReturID
|
|
JOIN m_paymenttype ON F_BillPaymentReturM_PaymentTypeID = M_PaymentTypeID
|
|
LEFT JOIN m_user ON F_BillPaymentReturUserID = M_UserID
|
|
WHERE
|
|
F_BillPaymentReturF_BillID = {$orderid}) a
|
|
GROUP BY note_number";
|
|
$query = $this->db_onedev->query($sql);
|
|
//echo $this->db_onedev->last_query();
|
|
if ($query) {
|
|
$rows = $query->result_array();
|
|
if($rows){
|
|
foreach($rows as $k => $v){
|
|
if($v['tipe'] == 'bill' && !empty($v['note_id'])){
|
|
$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 '' as numberingx,
|
|
F_BillID as tagihan_id,
|
|
T_OrderHeaderLabNumber as tagihan_number,
|
|
JSON_UNQUOTE(JSON_EXTRACT(fn_get_patient_atribute(T_OrderHeaderM_PatientID), '$.patient_fullname')) as pasien,
|
|
F_BillDetailTotal as tagihan_total,
|
|
F_BillDetailUnpaid as tagihan_tagihan,
|
|
0 as tagihan_bayar,
|
|
DATE_FORMAT(F_BillDueDate,'%d-%m-%Y') as tagihan_duedate,
|
|
F_BillDetailIsActive as tagihan_active,
|
|
'N' as show_detail,
|
|
F_BillDetailID,
|
|
F_BillDetailT_OrderHeaderID
|
|
FROM f_bill
|
|
JOIN f_bill_detail ON F_BillDetailF_BillID = F_BillID AND F_BillDetailIsActive = 'Y' AND F_BillDetailUnpaid > 0
|
|
JOIN t_orderheader ON F_BillDetailT_OrderHeaderID = T_OrderHeaderID
|
|
WHERE
|
|
F_BillID = {$orderid}
|
|
GROUP BY F_BillDetailID";
|
|
$query = $this->db_onedev->query($sql);
|
|
if ($query) {
|
|
$rows = $query->result_array();
|
|
$idx = 1;
|
|
if(count($rows) > 0){
|
|
foreach($rows as $key => $vx) {
|
|
$rows[$key]['numberingx'] = $idx;
|
|
$idx++;
|
|
}
|
|
}
|
|
return $rows;
|
|
|
|
} else {
|
|
$this->sys_error_db("get tagihans", $this->db_onedev);
|
|
exit;
|
|
}
|
|
}
|
|
public function add_tests($orderid){
|
|
$sql = "SELECT F_BillPaymentF_BillID as note_order_id,
|
|
F_BillPaymentID as note_id,
|
|
F_BillPaymentDate as note_date,
|
|
F_BillPaymentNumber as note_number,
|
|
GROUP_CONCAT(M_PaymentTypeName separator ' , ') as paymenttypes_name,
|
|
SUM(F_BillPaymentDetailAmount) as note_amount,
|
|
M_UserFullName as note_user,
|
|
F_BillPaymentDetailIsActive as note_active,
|
|
T_OrderHeaderLabNumber,
|
|
F_BillDetailTotal,
|
|
F_BillPaymentDetailAmount
|
|
FROM f_bill_payment
|
|
JOIN f_bill_payment_detail ON F_BillPaymentDetailF_BillPaymentID = F_BillPaymentID
|
|
LEFT JOIN f_bill ON F_BillPaymentDetailF_BillID = F_BillID
|
|
LEFT JOIN f_bill_detail ON F_BillPaymentDetailF_BillDetailID = F_BillDetailID
|
|
LEFT JOIN t_orderheader ON F_BillDetailT_OrderHeaderID = T_OrderHeaderID
|
|
JOIN m_paymenttype ON F_BillPaymentM_PaymentTypeID = M_PaymentTypeID
|
|
LEFT JOIN m_user ON F_BillPaymentDetailUserID = M_UserID
|
|
WHERE
|
|
F_BillPaymentID = {$orderid}
|
|
GROUP BY F_BillPaymentDetailID";
|
|
$query = $this->db_onedev->query($sql);
|
|
if ($query) {
|
|
$rows = $query->result_array();
|
|
if($rows){
|
|
}
|
|
return $rows;
|
|
|
|
} else {
|
|
$this->sys_error_db("get tests", $this->db_onedev);
|
|
exit;
|
|
}
|
|
|
|
}
|
|
public function search()
|
|
{
|
|
//# cek token valid
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$prm = $this->sys_input;
|
|
$corporate = $prm["corporate"];
|
|
$search = $prm["search"];
|
|
$status = $prm["status"];
|
|
$number_limit = 10;
|
|
$number_offset = ($prm['current_page'] - 1) * $number_limit ;
|
|
|
|
$where = "(F_BillIsActive = 'Y'AND F_BillTotal > 0 AND F_BillIsLunas = '{$status}' AND (F_BillNo LIKE '%{$search}%' OR F_BillRefNumber LIKE '%{$search}%' OR F_BillPaymentNumber LIKE '%{$search}%') AND CorporateName LIKE '%{$corporate}%')";
|
|
|
|
|
|
|
|
|
|
$sql = " SELECT count(*) as total
|
|
FROM f_bill
|
|
LEFT JOIN f_bill_payment ON F_BillID = F_BillPaymentF_BillID AND F_BillPaymentIsActive = 'Y'
|
|
LEFT JOIN corporate ON F_BillCorporateID = CorporateID
|
|
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("f_bill count", $this->db_onedev);
|
|
exit;
|
|
}
|
|
|
|
|
|
$sql = "SELECT f_bill.*, f_bill_payment.*,
|
|
Mgm_McuLabel,
|
|
CorporateName,
|
|
'' M_MouName,
|
|
IFNULL(F_BillTotal,0) as totalbill,
|
|
IFNULL(F_BillTotal - F_BillUnpaid,0) as paid,
|
|
IFNULL(F_BillUnpaid,0) as unpaid,
|
|
F_BillIsLunas as flaglunas,
|
|
GROUP_CONCAT(F_BillPaymentNumber SEPARATOR ', ') as F_BillPaymentNumber,
|
|
SUM(F_BillPaymentAmount) as F_BillPaymentAmount,
|
|
DATE_FORMAT(F_BillPaymentDate,'%d-%m-%Y') as F_BillPaymentDate,
|
|
IFNULL(F_BillRefNumber,'') as F_BillRefNumber,
|
|
'' as notes,
|
|
'' as tagihans,
|
|
'N' as isbillterpusat
|
|
FROM f_bill
|
|
LEFT JOIN f_bill_payment ON F_BillID = F_BillPaymentF_BillID AND F_BillPaymentIsActive = 'Y'
|
|
JOIN corporate ON F_BillCorporateID = CorporateID
|
|
join mgm_mcu on Mgm_McuID = F_BillMgm_McuID
|
|
WHERE
|
|
$where
|
|
GROUP BY F_BillID
|
|
ORDER BY F_BillID 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['F_BillID']);
|
|
$rows[$k]['tagihans'] = $this->add_tagihans($v['F_BillID']);
|
|
}
|
|
}
|
|
|
|
|
|
$result = array("total" => $tot_page, "records" => $rows, "sql"=> $this->db_onedev->last_query());
|
|
$this->sys_ok($result);
|
|
exit;
|
|
}
|
|
|
|
|
|
}
|