969 lines
32 KiB
PHP
969 lines
32 KiB
PHP
<?php
|
||
class Titip_pelunasan extends MY_Controller
|
||
{
|
||
function __construct()
|
||
{
|
||
parent::__construct();
|
||
$this->PAYMENT_TYPE_ID = 20;
|
||
$this->USER_ID = 1500;
|
||
}
|
||
public function r_fix_rk()
|
||
{
|
||
$this->db->trans_begin();
|
||
$param = $this->get_param();
|
||
$param["Payment_RkIsActive"] = "Y";
|
||
$param["Payment_RkM_UserID"] = $this->USER_ID;
|
||
$resp = $this->insert_or_update(
|
||
"payment_rk",
|
||
$param,
|
||
[
|
||
"Payment_RkM_BranchID",
|
||
"Payment_RkM_BranchCode",
|
||
"Payment_RkF_BillPaymentPusatID",
|
||
"Payment_RkF_PaymentNumber",
|
||
"Payment_RkIsActive",
|
||
],
|
||
"Payment_RkID"
|
||
);
|
||
if ($resp["status"] == "ERR") {
|
||
$this->reply_gz([
|
||
"status" => "ERR",
|
||
"message" => $resp["message"],
|
||
]);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
|
||
$this->reply_gz([
|
||
"status" => "OK",
|
||
"message" => print_r($resp, true),
|
||
]);
|
||
$this->db->trans_commit();
|
||
}
|
||
public function fix_payment($billPusatID)
|
||
{
|
||
$sql = "select distinct
|
||
F_BillTitipLunasDetailT_OrderHeaderID,
|
||
F_PaymentDetailID, F_PaymentDetailAmount, F_BillTitipLunasDetailAmount
|
||
from f_bill_titip_lunas
|
||
join f_bill on F_BillTitipLunasF_BillID = F_BillID
|
||
and F_BillTitipLunasIsActive='Y'
|
||
and F_BillIsActive = 'Y'
|
||
and F_BillTitipLunasF_BillPaymentPusatID = ?
|
||
join f_bill_titip_lunas_detail on F_BillTitipLunasDetailF_BillTitipLunasID = F_BillTitipLunasID
|
||
and F_BillTitipLunasDetailIsActive = 'Y'
|
||
join f_payment on F_PaymentT_OrderHeaderID = F_BillTitipLunasDetailT_OrderHeaderID
|
||
and F_PaymentIsActive = 'Y'
|
||
join f_paymentdetail on F_PaymentID = F_PaymentDetailF_PaymentID
|
||
and F_PaymentDetailIsActive = 'Y' ";
|
||
$qry = $this->db->query($sql, [$billPusatID]);
|
||
if (!$qry) {
|
||
$this->log(
|
||
"\tError Get Payment from RK : " .
|
||
$this->db->error()["message"] .
|
||
"|" .
|
||
$this->db->last_query()
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
$rows = $qry->result_array();
|
||
$sql = "update f_paymentdetail set F_PaymentDetailAmount=?
|
||
where F_PaymentDetailID = ?";
|
||
foreach ($rows as $r) {
|
||
if (
|
||
$r["F_PaymentDetailAmount"] !=
|
||
$r["F_BillTitipLunasDetailAmount"]
|
||
) {
|
||
$fPaymentDetailID = $r["F_PaymentDetailID"];
|
||
$fBillTitipLunasDetailAmount =
|
||
$r["F_BillTitipLunasDetailAmount"];
|
||
$this->db->query($sql, [
|
||
$fBillTitipLunasDetailAmount,
|
||
$fPaymentDetailID,
|
||
]);
|
||
//echo $this->db->last_query() . "<br/>";
|
||
}
|
||
}
|
||
}
|
||
public function fix_rk($billPusatID)
|
||
{
|
||
$this->fix_payment($billPusatID);
|
||
$this->log("Starting Fix RK Bill Pusat ID $billPusatID.");
|
||
$this->db->trans_begin();
|
||
list($regionalID, $branchCode, $branchID) = $this->get_branch();
|
||
$sql = "select F_BillTitipLunasM_BranchID Payment_RkM_BranchID,
|
||
F_BillTitipLunasM_BranchCode Payment_RkM_BranchCode,
|
||
F_PaymentID Payment_RkF_PaymentID, F_PaymentNumber Payment_RkF_PaymentNumber,
|
||
F_PaymentTotal Payment_RkAmount, F_PaymentDate Payment_RkF_PaymentDate,
|
||
F_PaymentM_UserID Payment_RkM_UserID, F_BillTitipLunasF_BillPaymentPusatID Payment_RkF_BillPaymentPusatID
|
||
from f_bill_titip_lunas
|
||
join f_bill_titip_lunas_detail on F_BillTitipLunasID = F_BillTitipLunasDetailF_BillTitipLunasID
|
||
and F_BillTitipLunasF_BillPaymentPusatID = ?
|
||
join f_payment on f_paymentT_OrderHeaderID = F_BillTitipLunasDetailT_OrderHeaderID";
|
||
$qry = $this->db->query($sql, [$billPusatID]);
|
||
if (!$qry) {
|
||
$this->log(
|
||
"\tError Insert f_bill_payment : " .
|
||
$this->db->error()["message"] .
|
||
"|" .
|
||
$this->db->last_query()
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
$rows = $qry->result_array();
|
||
$r_resp = [];
|
||
foreach ($rows as $r) {
|
||
$resp = $this->insert_or_update(
|
||
"payment_rk",
|
||
$r,
|
||
["Payment_RkM_BranchID", "Payment_RkF_PaymentNumber"],
|
||
"Payment_RkID"
|
||
);
|
||
$param = [
|
||
"Payment_RkF_PaymentID" => $r["Payment_RkF_PaymentID"],
|
||
"Payment_RkF_PaymentNumber" => $r["Payment_RkF_PaymentNumber"],
|
||
"Payment_RkF_PaymentDate" => $r["Payment_RkF_PaymentDate"],
|
||
"Payment_RkM_BranchID" => $branchID,
|
||
"Payment_RkM_BranchCode" => $branchCode,
|
||
"Payment_RkF_BillPaymentPusatID" => $billPusatID,
|
||
"Payment_RkAmount" => $r["Payment_RkAmount"],
|
||
];
|
||
$originIpAddress = $this->get_branch_ip($r["Payment_RkM_BranchID"]);
|
||
if ($originIpAddress == "") {
|
||
$this->log(
|
||
"\tError get Ip Address : " .
|
||
$r["F_BillTitipLunasM_BranchCode"]
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
$rk_url = "http://$originIpAddress/one-api/keu/titip_pelunasan/r_fix_rk";
|
||
$jparam = json_encode($param);
|
||
$resp = $this->post($rk_url, $jparam);
|
||
if ($resp["status"] != "OK") {
|
||
$this->log("\tError Post RK to $rk_url : " . $resp["message"]);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
$r_resp[] = $resp;
|
||
}
|
||
$this->log("Done Fix RK Bill Pusat ID $billPusatID.");
|
||
$this->db->trans_commit();
|
||
}
|
||
|
||
public function real()
|
||
{
|
||
$this->log("Starting Pelunasan Real.");
|
||
$this->db->trans_begin();
|
||
list($regionalID, $branchCode, $branchID) = $this->get_branch();
|
||
//isConfirm R => sudah real payment
|
||
$sql = "select
|
||
distinct
|
||
F_BillTitipLunasID,
|
||
F_BillTitipLunasF_BillID, F_BillTitipLunasDate, F_BillTitipLunasF_BillID,
|
||
F_BillTitipLunasF_BillPaymentPusatID,
|
||
F_BillTitipLunasNumber,F_BillNo, F_BillTitipLunasAmount,
|
||
F_BillTitipLunasM_PaymentTypeID, F_BillTitipLunasEDCNat_BankID, F_BillTitipLunasCardNat_BankID,
|
||
if(F_BillPaymentPusatM_BankAccountID > 0, F_BillPaymentPusatM_BankAccountID, F_BillTitipLunasM_BankAccountID) F_BillTitipLunasM_BankAccountID ,
|
||
F_BillTitipLunasVoucherNumber,
|
||
F_BillTitipLunasM_BranchID, F_BillTitipLunasM_BranchCode
|
||
from f_bill_titip_lunas
|
||
join f_bill on F_BillTitipLunasF_BillID = F_BillID
|
||
and F_BillTitipLunasIsConfirm = 'N'
|
||
and F_BillTitipLunasIsActive='Y'
|
||
and F_BillIsActive = 'Y'
|
||
join f_bill_titip_lunas_detail on F_BillTitipLunasDetailF_BillTitipLunasID = F_BillTitipLunasID
|
||
and F_BillTitipLunasDetailIsActive = 'Y'
|
||
left join f_bill_payment_pusat on F_BillTitipLunasF_BillPaymentPusatID = F_BillPaymentPusatID
|
||
and F_BillPaymentPusatIsActive = 'Y' ";
|
||
$resp = $this->get_rows($sql);
|
||
if (!$resp[0]) {
|
||
$this->log("\tError : " . $resp[1]);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
$rows = $resp[1];
|
||
$arr_titip_number = [];
|
||
$arr_payment_number = [];
|
||
|
||
$sql_detail = "select
|
||
F_BillTitipLunasDetailF_BillDetailID,
|
||
F_BillTitipLunasDetailT_OrderHeaderID,
|
||
F_BillTitipLunasM_BranchID, F_BillTitipLunasM_BranchCode,
|
||
F_BillTitipLunasDetailAmount
|
||
from f_bill_titip_lunas
|
||
join f_bill on F_BillTitipLunasF_BillID = F_BillID
|
||
and F_BillTitipLunasIsConfirm = 'N'
|
||
and F_BillTitipLunasIsActive='Y'
|
||
and F_BillIsActive = 'Y'
|
||
and F_BillTitipLunasID = ?
|
||
join f_bill_titip_lunas_detail on F_BillTitipLunasDetailF_BillTitipLunasID = F_BillTitipLunasID
|
||
and F_BillTitipLunasDetailIsActive = 'Y'";
|
||
|
||
foreach ($rows as $r) {
|
||
$fBillTitipLunasID = $r["F_BillTitipLunasID"];
|
||
$fBillID = $r["F_BillTitipLunasF_BillID"];
|
||
$fBilltTitipLunasAmount = $r["F_BillTitipLunasAmount"];
|
||
$fBillNumber = $r["F_BillTitipLunasNumber"];
|
||
$fBillAmount = $r["F_BillTitipLunasAmount"];
|
||
$fBillTitipLunasM_BranchID = $r["F_BillTitipLunasM_BranchID"];
|
||
$fBillPaymentPusatID = $r["F_BillTitipLunasF_BillPaymentPusatID"];
|
||
|
||
if ($fBillTitipLunasM_BranchID == $branchID) {
|
||
$isLocalPayment = true;
|
||
} else {
|
||
$isLocalPayment = false;
|
||
}
|
||
|
||
$sql = "INSERT INTO f_bill_payment(F_BillPaymentF_BillID, F_BillPaymentDate, F_BillPaymentAmount,
|
||
F_BillPaymentM_PaymentTypeID, F_BillPaymentEDCNat_BankID,
|
||
F_BillPaymentCardNat_BankID, F_BillPaymentM_BankAccountID, F_BillPaymentVoucherNumber,
|
||
F_BillPaymentCreated, F_BillPaymentUserID)
|
||
VALUES (?, CURDATE(), ?, ?, ?, ?, ?, ?, NOW(), ? )";
|
||
|
||
if ($isLocalPayment) {
|
||
$qry = $this->db->query($sql, [
|
||
$fBillID,
|
||
$fBilltTitipLunasAmount,
|
||
$r["F_BillTitipLunasM_PaymentTypeID"],
|
||
$r["F_BillTitipLunasEDCNat_BankID"],
|
||
$r["F_BillTitipLunasCardNat_BankID"],
|
||
$r["F_BillTitipLunasM_BankAccountID"],
|
||
$r["F_BillTitipLunasVoucherNumber"],
|
||
$this->USER_ID,
|
||
]);
|
||
} else {
|
||
$qry = $this->db->query($sql, [
|
||
$fBillID,
|
||
$fBilltTitipLunasAmount,
|
||
$this->PAYMENT_TYPE_ID,
|
||
0,
|
||
0,
|
||
0,
|
||
"",
|
||
$this->USER_ID,
|
||
]);
|
||
}
|
||
if (!$qry) {
|
||
$this->log(
|
||
"\tError Insert f_bill_payment : " .
|
||
$this->db->error()["message"] .
|
||
"|" .
|
||
$this->db->last_query()
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
$fBillPaymentID = $this->db->insert_id();
|
||
$qry = $this->db->query($sql_detail, [$fBillTitipLunasID]);
|
||
|
||
// update f_bill
|
||
$sql = "UPDATE f_bill SET
|
||
F_BillUnpaid = F_BillUnpaid - ?, F_BillIsLunas = IF(F_BillUnpaid = 0,'Y','N')
|
||
WHERE F_BillID = ?";
|
||
$qry = $this->db->query($sql, [$fBillAmount, $fBillID]);
|
||
if (!$qry) {
|
||
$this->log(
|
||
"\tError Update f_bill : " . $this->db->error()["message"]
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
|
||
//get detail
|
||
$qry = $this->db->query($sql_detail, [$fBillTitipLunasID]);
|
||
if (!$qry) {
|
||
$this->log(
|
||
"\tError get f_bill_titip_lunas_detail : " .
|
||
$this->db->error()["message"]
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
$rows_detail = $qry->result_array();
|
||
foreach ($rows_detail as $rd) {
|
||
$orderHeaderID = $rd["F_BillTitipLunasDetailT_OrderHeaderID"];
|
||
$fBillDetailID = $rd["F_BillTitipLunasDetailF_BillDetailID"];
|
||
$fBillDetailAmount = $rd["F_BillTitipLunasDetailAmount"];
|
||
//3 INSERT f_bill_payment_detail
|
||
$sql = "INSERT INTO f_bill_payment_detail( F_BillPaymentDetailF_BillPaymentID,
|
||
F_BillPaymentDetailF_BillID, F_BillPaymentDetailF_BillDetailID, F_BillPaymentDetailAmount,
|
||
F_BillPaymentDetailUserID, F_BillPaymentDetailCreated, F_BillPaymentDetailLastUpdated)
|
||
VALUES( ?, ?, ?, ?, ?, now(), now())";
|
||
|
||
$qry = $this->db->query($sql, [
|
||
$fBillPaymentID,
|
||
$fBillID,
|
||
$fBillDetailID,
|
||
$fBillDetailAmount,
|
||
$this->USER_ID,
|
||
]);
|
||
if (!$qry) {
|
||
$this->log(
|
||
"\tError insert f_bill_payment_detail : " .
|
||
$this->db->error()["message"]
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
$fBillPaymentDetailID = $this->db->insert_id();
|
||
//4 update f_bill_detail
|
||
$sql = "UPDATE f_bill_detail SET
|
||
F_BillDetailUnpaid = F_BillDetailUnpaid - ?
|
||
WHERE F_BillDetailID = ?";
|
||
$qry = $this->db->query($sql, [
|
||
$fBillDetailAmount,
|
||
$fBillDetailID,
|
||
]);
|
||
if (!$qry) {
|
||
$this->log(
|
||
"\tError update f_bill_detail : " .
|
||
$this->db->error()["message"]
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
|
||
//5 INSERT f_payment
|
||
$sql = "INSERT INTO f_payment (F_PaymentT_OrderHeaderID, F_PaymentDate,
|
||
F_PaymentTotal, F_PaymentCreated, F_PaymentLastUpdated, F_PaymentM_UserID)
|
||
VALUES( ?, now(), ?, now(), now(), ?)";
|
||
$qry = $this->db->query($sql, [
|
||
$orderHeaderID,
|
||
$fBillDetailAmount,
|
||
$this->USER_ID,
|
||
]);
|
||
if (!$qry) {
|
||
$this->log(
|
||
"\tError insert f_payment : " .
|
||
$this->db->error()["message"]
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
$fPaymentID = $this->db->insert_id();
|
||
|
||
//6 INSERT f_paymentdetail
|
||
$sql = "INSERT INTO f_paymentdetail
|
||
(F_PaymentDetailF_PaymentID, F_PaymentDetailM_PaymentTypeID, F_PaymentDetailAmount,
|
||
F_PaymentDetailActual, F_PaymentDetailChange, F_PaymentDetailEDCNat_BankID, F_PaymentDetailCardNat_BankID,
|
||
F_PaymentDetailM_BankAccountID, F_PaymentDetailCreated, F_PaymentDetailLastUpdated, F_PaymentDetailUserID)
|
||
VALUES( ?, ?, ?, ?, 0, ?, ?, ?, now(), now(), ?)";
|
||
if ($isLocalPayment) {
|
||
$qry = $this->db->query($sql, [
|
||
$fPaymentID,
|
||
$r["F_BillTitipLunasM_PaymentTypeID"],
|
||
$fBillDetailAmount,
|
||
$fBillDetailAmount,
|
||
$r["F_BillTitipLunasEDCNat_BankID"],
|
||
$r["F_BillTitipLunasCardNat_BankID"],
|
||
$r["F_BillTitipLunasM_BankAccountID"],
|
||
$this->USER_ID,
|
||
]);
|
||
} else {
|
||
$qry = $this->db->query($sql, [
|
||
$fPaymentID,
|
||
$this->PAYMENT_TYPE_ID,
|
||
$fBillDetailAmount,
|
||
$fBillDetailAmount,
|
||
0,
|
||
0,
|
||
0,
|
||
$this->USER_ID,
|
||
]);
|
||
}
|
||
if (!$qry) {
|
||
$this->log(
|
||
"\tError insert f_paymentdetail: " .
|
||
$this->db->error()["message"]
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
//7. update f_bill_payment_detail
|
||
$sql = "UPDATE f_bill_payment_detail SET
|
||
F_BillPaymentDetailF_PaymentID = ?
|
||
WHERE F_BillPaymentDetailID = ?";
|
||
$qry = $this->db->query($sql, [
|
||
$fPaymentID,
|
||
$fBillPaymentDetailID,
|
||
]);
|
||
if (!$qry) {
|
||
$this->log(
|
||
"\tError update f_bill_payment_detal : " .
|
||
$this->db->error()["message"]
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
|
||
$sql = "select F_PaymentID,F_PaymentNumber,F_PaymentDate, F_PaymentTotal
|
||
from f_payment
|
||
where F_PaymentID = ?";
|
||
$resp = $this->get_one_row($sql, [$fPaymentID]);
|
||
if (!$resp[0]) {
|
||
$this->log("\tError get f_payment : " . $resp[1]);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
$rpayment = $resp[1];
|
||
if (!$isLocalPayment) {
|
||
//Create`RK Payment
|
||
// - Piutang
|
||
// + Hutang
|
||
$sql = "insert into payment_rk(Payment_RkF_PaymentID,Payment_RkF_PaymentNumber, Payment_RkF_PaymentDate,
|
||
Payment_RkM_BranchID, Payment_RkM_BranchCode, Payment_RkAmount, Payment_RkM_UserID, Payment_RkF_BillPaymentPusatID )
|
||
value (?,?,?, ?,?,?,?,?)";
|
||
$qry = $this->db->query($sql, [
|
||
$rpayment["F_PaymentID"],
|
||
$rpayment["F_PaymentNumber"],
|
||
$rpayment["F_PaymentDate"],
|
||
$r["F_BillTitipLunasM_BranchID"],
|
||
$r["F_BillTitipLunasM_BranchCode"],
|
||
-1 * $rpayment["F_PaymentTotal"],
|
||
$this->USER_ID,
|
||
$fBillPaymentPusatID,
|
||
]);
|
||
if (!$qry) {
|
||
$this->log(
|
||
"\tError insert payment_rk local : " .
|
||
$this->db->error()["message"]
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
// create post rk to cabang asal
|
||
$param = [
|
||
"Payment_RkF_PaymentID" => $rpayment["F_PaymentID"],
|
||
"Payment_RkF_PaymentNumber" =>
|
||
$rpayment["F_PaymentNumber"],
|
||
"Payment_RkF_PaymentDate" => $rpayment["F_PaymentDate"],
|
||
"Payment_RkM_BranchID" => $branchID,
|
||
"Payment_RkM_BranchCode" => $branchCode,
|
||
"Payment_RkF_BillPaymentPusatID" => $fBillPaymentPusatID,
|
||
"Payment_RkAmount" => $rpayment["F_PaymentTotal"],
|
||
];
|
||
$originIpAddress = $this->get_branch_ip(
|
||
$r["F_BillTitipLunasM_BranchID"]
|
||
);
|
||
if ($originIpAddress == "") {
|
||
$this->log(
|
||
"\tError get Ip Address : " .
|
||
$r["F_BillTitipLunasM_BranchCode"]
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
$rk_url = "http://$originIpAddress/one-api/keu/titip_pelunasan/r_rk";
|
||
$jparam = json_encode($param);
|
||
$resp = $this->post($rk_url, $jparam);
|
||
if ($resp["status"] != "OK") {
|
||
$this->log(
|
||
"\tError Post RK to $rk_url : " . $resp["message"]
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
}
|
||
$sql =
|
||
"update f_bill_titip_lunas set F_BillTitipLunasIsConfirm='R' where F_BillTitipLunasID=?";
|
||
$qry = $this->db->query($sql, [$fBillTitipLunasID]);
|
||
if (!$qry) {
|
||
$this->log(
|
||
"\tError update f_bill_titip_lunas Confirm: " .
|
||
$this->db->error()["message"]
|
||
);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
$arr_titip_number[] = $fBillNumber;
|
||
$arr_payment_number[] = $rpayment["F_PaymentNumber"];
|
||
}
|
||
}
|
||
|
||
$this->db->trans_commit();
|
||
$this->log(
|
||
"\tSukses Payment No : " . implode(", ", $arr_payment_number)
|
||
);
|
||
$this->log("End Pelunasan Real.");
|
||
}
|
||
function r_rk()
|
||
{
|
||
$this->db->trans_begin();
|
||
$param = $this->get_param();
|
||
$param["Payment_RkIsActive"] = "Y";
|
||
$param["Payment_RkM_UserID"] = $this->USER_ID;
|
||
$resp = $this->insert_or_update(
|
||
"payment_rk",
|
||
$param,
|
||
[
|
||
"Payment_RkM_BranchID",
|
||
"Payment_RkM_BranchCode",
|
||
"Payment_RkF_BillPaymentPusatID",
|
||
"Payment_RkF_PaymentNumber",
|
||
"Payment_RkIsActive",
|
||
],
|
||
"Payment_RkID"
|
||
);
|
||
if ($resp["status"] == "ERR") {
|
||
$this->reply_gz([
|
||
"status" => "ERR",
|
||
"message" => $resp["message"],
|
||
]);
|
||
$this->db->trans_rollback();
|
||
exit();
|
||
}
|
||
|
||
$this->reply_gz([
|
||
"status" => "OK",
|
||
"message" => print_r($resp, true),
|
||
]);
|
||
$this->db->trans_commit();
|
||
}
|
||
function r_rk_old()
|
||
{
|
||
$param = $this->get_param();
|
||
$sql = "insert into payment_rk(Payment_RkF_PaymentID,Payment_RkF_PaymentNumber, Payment_RkF_PaymentDate,
|
||
Payment_RkM_BranchID, Payment_RkM_BranchCode, Payment_RkAmount , Payment_RkM_UserID, Payment_RkF_BillPaymentPusatID)
|
||
value (?,?,?, ?,?,?, ?,?)";
|
||
$qry = $this->db->query($sql, [
|
||
$param["Payment_RkF_PaymentID"],
|
||
$param["Payment_RkF_PaymentNumber"],
|
||
$param["Payment_RkF_PaymentDate"],
|
||
$param["Payment_RkM_BranchID"],
|
||
$param["Payment_RkM_BranchCode"],
|
||
$param["Payment_RkAmount"],
|
||
$this->USER_ID,
|
||
$param["Payment_RkF_BillPaymentPusatID"],
|
||
]);
|
||
if (!$qry) {
|
||
$this->reply_gz([
|
||
"status" => "ERR",
|
||
"message" => "Error : " . $this->db->error()["message"],
|
||
]);
|
||
} else {
|
||
$this->reply_gz(["status" => "OK", "message" => ""]);
|
||
}
|
||
}
|
||
function get_branch_ip($branchID)
|
||
{
|
||
$sql =
|
||
"select * from m_branch where M_BranchID = ? and M_BranchIsActive ='Y'";
|
||
$resp = $this->get_one_row($sql, [$branchID]);
|
||
if ($resp[0]) {
|
||
return $resp[1]["M_BranchIPAddress"];
|
||
} else {
|
||
$this->log("\tError Get Ip Address : " . $resp[1]);
|
||
}
|
||
return "";
|
||
}
|
||
public function index()
|
||
{
|
||
$this->log("Starting Pelunasan Titipan.");
|
||
list($regionalID, $branchCode, $branchID) = $this->get_branch();
|
||
if (!$regionalID) {
|
||
$this->log("Error No Default Branch");
|
||
exit();
|
||
}
|
||
$urls = $this->url_branches($regionalID, $branchCode);
|
||
foreach ($urls as $u) {
|
||
$url = $u["url"];
|
||
$name = $u["name"];
|
||
$this->log("Check Pelunasan from $name : $url");
|
||
$resp = $this->get($url);
|
||
if ($resp["status"] == "ERR") {
|
||
$this->log("\t Error : " . $resp["message"]);
|
||
continue;
|
||
}
|
||
$billCounter = 0;
|
||
$billCounterDetail = 0;
|
||
foreach ($resp["data"] as $d) {
|
||
$this->log(
|
||
"\t Titip Pelunasan : " .
|
||
$d["F_BillPaymentPusatNumber"] .
|
||
" | Bill ID : " .
|
||
$d["F_BillDetailF_BillID"]
|
||
);
|
||
$data = [
|
||
"F_BillTitipLunasDate" => $d["F_BillPaymentPusatDate"],
|
||
"F_BillTitipLunasF_BillID" => $d["F_BillDetailF_BillID"],
|
||
"F_BillTitipLunasF_BillPaymentPusatID" =>
|
||
$d["F_BillPaymentPusatID"],
|
||
"F_BillTitipLunasM_PaymentTypeID" =>
|
||
$d["F_BillPaymentPusatM_PaymentTypeID"],
|
||
"F_BillTitipLunasAmount" => $d["F_BillTitipLunasAmount"],
|
||
"F_BillTitipLunasIsActive" =>
|
||
$d["F_BillPaymentPusatIsActive"],
|
||
"F_BillTitipLunasStaffName" => "Admin",
|
||
"F_BillTitipLunasUserID" => 3,
|
||
"F_BillTitipLunasNumber" => $d["F_BillPaymentPusatNumber"],
|
||
"F_BillTitipLunasM_BranchID" => $d["M_BranchID"],
|
||
"F_BillTitipLunasM_BranchCode" => $d["M_BranchCode"],
|
||
];
|
||
$resp = $this->insert_or_update(
|
||
"f_bill_titip_lunas",
|
||
$data,
|
||
["F_BillTitipLunasNumber", "F_BillTitipLunasF_BillID"],
|
||
"F_BillTitipLUnasID"
|
||
);
|
||
if ($resp["status"] == "ERR") {
|
||
$this->log("Error : " . $resp["message"]);
|
||
continue;
|
||
}
|
||
$billCounter++;
|
||
$insertID = $resp["insertID"];
|
||
foreach ($d["details"] as $det) {
|
||
$data = [
|
||
"F_BillTitipLunasDetailF_BillTitipLunasID" => $insertID,
|
||
"F_BillTitipLunasDetailT_OrderHeaderID" =>
|
||
$det["F_BillDetailT_OrderHeaderID"],
|
||
"F_BillTitipLunasDetailFullPatientName" =>
|
||
$det["FullPatientName"],
|
||
"F_BillTitipLunasDetailF_BillDetailID" =>
|
||
$det["F_BillDetailID"],
|
||
"F_BillTitipLunasDetailT_OrderHeaderLabNumber" =>
|
||
$det["T_OrderHeaderLabNumber"],
|
||
"F_BillTitipLunasDetailT_OrderHeaderLabNumberExt" =>
|
||
$det["T_OrderHeaderLabNumberExt"],
|
||
"F_BillTitipLunasDetailAmount" =>
|
||
$det["Log_PaymentTitipDetailAmount"],
|
||
"F_BillTitipLunasDetailUserID" => 3,
|
||
];
|
||
$resp = $this->insert_or_update(
|
||
"f_bill_titip_lunas_detail",
|
||
$data,
|
||
[
|
||
"F_BillTitipLunasDetailT_OrderHeaderID",
|
||
"F_BillTitipLunasDetailF_BillTitipLunasID",
|
||
],
|
||
"F_BillTitipLunasDetailID"
|
||
);
|
||
if ($resp["status"] == "ERR") {
|
||
$this->log("Error : " . $resp["message"]);
|
||
break;
|
||
}
|
||
$billCounterDetail++;
|
||
}
|
||
}
|
||
$this->log(
|
||
"\tDone. Bill : $billCounter | Detail : $billCounterDetail\n"
|
||
);
|
||
}
|
||
}
|
||
function log($msg)
|
||
{
|
||
$sdate = date("Y-m-d H:i:s");
|
||
echo "$sdate $msg\n";
|
||
}
|
||
function insert_or_update($table, $dt, $keys, $keyID)
|
||
{
|
||
$s_where = "";
|
||
$param = [];
|
||
foreach ($keys as $k) {
|
||
if ($s_where != "") {
|
||
$s_where .= " and ";
|
||
}
|
||
$s_where .= " $k = ?";
|
||
$param[] = $dt[$k];
|
||
}
|
||
$sql = "select $keyID
|
||
from $table
|
||
where $s_where ";
|
||
$qry = $this->db->query($sql, $param);
|
||
if (!$qry) {
|
||
return [
|
||
"status" => "ERR",
|
||
"message" =>
|
||
$this->db->error()["message"] .
|
||
"|" .
|
||
$this->db->last_query(),
|
||
];
|
||
}
|
||
$rows = $qry->result_array();
|
||
$status = "Insert";
|
||
if (count($rows) > 0) {
|
||
$lastInsertID = $rows[0][$keyID];
|
||
foreach ($keys as $k) {
|
||
$this->db->where($k, $dt[$k]);
|
||
}
|
||
$qry = $this->db->update($table, $dt);
|
||
if (!$qry) {
|
||
return [
|
||
"status" => "ERR",
|
||
"message" =>
|
||
"ERR Update : " .
|
||
$this->db->error()["message"] .
|
||
"|" .
|
||
$this->db->last_query(),
|
||
];
|
||
}
|
||
$status = "Update";
|
||
} else {
|
||
$qry = $this->db->insert($table, $dt);
|
||
if (!$qry) {
|
||
return [
|
||
"status" => "ERR",
|
||
"message" =>
|
||
"ERR Insert : " .
|
||
$this->db->error()["message"] .
|
||
"|" .
|
||
$this->db->last_query(),
|
||
];
|
||
}
|
||
$lastInsertID = $this->db->insert_id();
|
||
}
|
||
return [
|
||
"status" => "OK",
|
||
"message" => $status,
|
||
"insertID" => $lastInsertID,
|
||
];
|
||
}
|
||
function r_lunas($inp_branchCode, $debug = 0)
|
||
{
|
||
$flag_debug = false;
|
||
if ($debug != 0) {
|
||
$flag_debug = true;
|
||
}
|
||
list($regionalID, $branchCode, $branchID) = $this->get_branch();
|
||
// pelunasan 3 hari ke belakang
|
||
$sql = "select distinct
|
||
F_BillPaymentPusatID, F_BillDetailF_BillID,
|
||
F_BillPaymentPusatDate, F_BillPaymentPusatNumber, F_BillPaymentPusatNote,
|
||
F_BillPaymentPusatAmount, F_BillPaymentPusatIsActive, F_BillPaymentPusatM_PaymentTypeID ,
|
||
F_BillIssuePusatID, $branchID M_BranchID, '$branchCode' M_BranchCode
|
||
from f_bill_payment_pusat
|
||
join f_bill_issue_pusat on F_BillPaymentPusatF_BillIssuePusatID = F_BillIssuePusatID
|
||
and F_BillIssuePusatIsActive = 'Y'
|
||
and F_BillPaymentPusatIsActive = 'Y'
|
||
and F_BillPaymentPusatLastUpdated + interval 3 day > now()
|
||
join f_bill_issue_pusat_detail on F_BillIssuePusatDetailF_BillIssuePusatID = F_BillIssuePusatID
|
||
and F_BillIssuePusatDetailIsActive = 'Y'
|
||
join f_bill_titip_detail on F_BillDetailF_BillID = F_BillIssuePusatDetailF_BillID
|
||
and f_bill_titip_detail.M_BranchID = F_BillIssuePusatDetailM_BranchID
|
||
and f_bill_titip_detail.M_BranchCode = ?
|
||
and F_BillDetailIsActive = 'Y'
|
||
";
|
||
$resp = $this->get_rows($sql, [$inp_branchCode]);
|
||
if (!$resp[0]) {
|
||
$this->reply_gz(
|
||
["status" => "ERR", "message" => $resp[1]],
|
||
$flag_debug
|
||
);
|
||
exit();
|
||
}
|
||
$rows = $resp[1];
|
||
$pusat_ids = "-1";
|
||
foreach ($rows as $idx => $r) {
|
||
$pusat_ids .= "," . $r["F_BillIssuePusatID"];
|
||
unset($rows[$idx]["F_BillIssuePusatID"]);
|
||
}
|
||
$bill_issue = $rows;
|
||
$sql = "select distinct
|
||
F_BillPaymentPusatDetailF_BillPaymentPusatID,
|
||
F_BillDetailT_OrderHeaderID, T_OrderHeaderDate,FullPatientName,
|
||
T_OrderHeaderLabNumber, T_OrderHeaderLabNumberExt,
|
||
Log_PaymentTitipDetailAmount,
|
||
F_BillPaymentPusatDetailAmount,
|
||
F_BillDetailTotal, F_BillDetailUnpaid,
|
||
F_BillDetailIsLunas,
|
||
F_BillDetailID, F_BillDetailF_BillID
|
||
from
|
||
f_bill_issue_pusat_detail
|
||
join f_bill_payment_pusat_detail on F_BillPaymentPusatDetailF_BillIssuePusatDetailID = F_BillIssuePusatDetailID
|
||
and F_BillIssuePusatDetailF_BillIssuePusatID in ($pusat_ids)
|
||
and F_BillPaymentPusatDetailIsActive = 'Y'
|
||
and F_BillIssuePusatDetailIsActive = 'Y'
|
||
join log_payment_titip_detail on Log_PaymentTitipDetailF_BillPaymentPusatDetailID = F_BillPaymentPusatDetailID
|
||
and Log_PaymentTitipDetailIsActive = 'Y'
|
||
join f_bill_titip_detail on F_BillDetailID = Log_PaymentTitipDetailF_BillDetailID
|
||
and M_BranchCode = ?
|
||
and F_BillIssuePusatDetailM_BranchID = Log_PaymentTitipDetailM_BranchID
|
||
and F_BillDetailUnpaid < F_BillDetailTotal
|
||
and F_BillDetailIsActive = 'Y'
|
||
";
|
||
$resp = $this->get_rows($sql, [$inp_branchCode]);
|
||
if (!$resp[0]) {
|
||
$this->reply_gz(
|
||
["status" => "ERR", "message" => $resp[1]],
|
||
$flag_debug
|
||
);
|
||
exit();
|
||
}
|
||
$rows = $resp[1];
|
||
foreach ($bill_issue as $idx => $bill) {
|
||
$billPusatID = $bill["F_BillPaymentPusatID"];
|
||
$billID = $bill["F_BillDetailF_BillID"];
|
||
$details = array_filter($rows, function ($r) use (
|
||
$billPusatID,
|
||
$billID
|
||
) {
|
||
if (
|
||
$r["F_BillPaymentPusatDetailF_BillPaymentPusatID"] ==
|
||
$billPusatID &&
|
||
$r["F_BillDetailF_BillID"] == $billID
|
||
) {
|
||
return true;
|
||
}
|
||
return false;
|
||
});
|
||
$total_per_cabang = 0;
|
||
foreach ($details as $xd) {
|
||
$total_per_cabang += $xd["Log_PaymentTitipDetailAmount"];
|
||
}
|
||
$bill_issue[$idx]["details"] = $details;
|
||
$bill_issue[$idx]["F_BillTitipLunasAmount"] = $total_per_cabang;
|
||
if (count($details) == 0) {
|
||
unset($bill_issue[$idx]);
|
||
}
|
||
}
|
||
$this->reply_gz(["status" => "OK", "data" => $bill_issue], $flag_debug);
|
||
}
|
||
|
||
function reply_gz($param, $debug = false)
|
||
{
|
||
if ($debug) {
|
||
echo json_encode($param);
|
||
} else {
|
||
echo gzcompress(json_encode($param), 9);
|
||
}
|
||
}
|
||
function get_param()
|
||
{
|
||
$zdata = file_get_contents("php://input");
|
||
$data = gzuncompress($zdata);
|
||
$param = json_decode($data, true);
|
||
if (json_last_error() != JSON_ERROR_NONE) {
|
||
echo json_encode([
|
||
"status" => "ERR",
|
||
"message" => "Json Error : " . json_last_error_msg(),
|
||
"raw" => $data,
|
||
]);
|
||
exit();
|
||
}
|
||
return $param;
|
||
}
|
||
function get_branch()
|
||
{
|
||
$sql = "select M_BranchS_RegionalID,M_BranchCode,M_BranchID
|
||
from m_branch where M_BranchIsActive = 'Y' and M_BranchIsDefault = 'Y'";
|
||
$qry = $this->db->query($sql);
|
||
if (!$qry) {
|
||
return [false, $this->db->error()["message"]];
|
||
}
|
||
$rows = $qry->result_array();
|
||
if (count($rows) == 0) {
|
||
return ["ERR", "No Default Branch"];
|
||
}
|
||
return [
|
||
$rows[0]["M_BranchS_RegionalID"],
|
||
$rows[0]["M_BranchCode"],
|
||
$rows[0]["M_BranchID"],
|
||
];
|
||
}
|
||
function url_branches($regionalID, $branchCode)
|
||
{
|
||
$sql = "select M_BranchCode,M_BranchIpAddress,M_BranchName
|
||
from m_branch where M_BranchS_RegionalID = ? and M_BranchIsActive = 'Y' ";
|
||
$qry = $this->db->query($sql, [$regionalID]);
|
||
|
||
if (!$qry) {
|
||
return [false, $this->db->error()["message"]];
|
||
}
|
||
$rows = $qry->result_array();
|
||
$url = [];
|
||
foreach ($rows as $r) {
|
||
$url[] = [
|
||
"url" =>
|
||
"http://" .
|
||
$r["M_BranchIpAddress"] .
|
||
"/one-api/keu/titip_pelunasan/r_lunas/" .
|
||
$branchCode,
|
||
"name" => $r["M_BranchName"],
|
||
];
|
||
}
|
||
return $url;
|
||
}
|
||
|
||
public function post($url, $data)
|
||
{
|
||
$ch = curl_init($url);
|
||
$zdata = gzcompress($data, 9);
|
||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, $zdata);
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
|
||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||
"Content-Type: application/octet",
|
||
"Content-Length: " . strlen($zdata),
|
||
]);
|
||
$zresult = curl_exec($ch);
|
||
if (curl_error($ch) != "") {
|
||
echo json_encode([
|
||
"status" => "ERR",
|
||
"message" => "Http Error : " . curl_error($ch),
|
||
]);
|
||
curl_close($ch);
|
||
exit();
|
||
}
|
||
curl_close($ch);
|
||
$result = gzuncompress($zresult);
|
||
$jresult = json_decode($result, true);
|
||
if (json_last_error() != 0) {
|
||
return [
|
||
"status" => "ERR",
|
||
"message" => "Invalid JSON : " . $result,
|
||
];
|
||
}
|
||
return $jresult;
|
||
}
|
||
function get($url, $timeout = 60, $c_timeout = 5)
|
||
{
|
||
$ch = curl_init($url);
|
||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $c_timeout);
|
||
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
$zresult = curl_exec($ch);
|
||
$err_msg = curl_error($ch);
|
||
if ($err_msg != "") {
|
||
return ["status" => "ERR", "message" => $err_msg];
|
||
}
|
||
$result = gzuncompress($zresult);
|
||
$jresult = json_decode($result, true);
|
||
if (json_last_error() != 0) {
|
||
return [
|
||
"status" => "ERR",
|
||
"message" => "Invalid JSON : " . $result,
|
||
];
|
||
}
|
||
return $jresult;
|
||
}
|
||
function get_rows($sql, $prm = false)
|
||
{
|
||
if ($prm) {
|
||
$qry = $this->db->query($sql, $prm);
|
||
} else {
|
||
$qry = $this->db->query($sql);
|
||
}
|
||
if (!$qry) {
|
||
$msg = "Error Query : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
|
||
return [false, $msg];
|
||
}
|
||
$rows = $qry->result_array();
|
||
return [true, $rows];
|
||
}
|
||
function get_one_row($sql, $prm)
|
||
{
|
||
list($status, $msg) = $this->get_rows($sql, $prm);
|
||
if ($status !== true) {
|
||
return [$status, $msg];
|
||
}
|
||
if (count($msg) == 0) {
|
||
return [false, "Record not found. | " . $msg];
|
||
}
|
||
return [true, $msg[0]];
|
||
}
|
||
}
|