888 lines
32 KiB
PHP
888 lines
32 KiB
PHP
<?php
|
|
|
|
class Fix_future extends MY_Controller
|
|
{
|
|
function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->PJ_DOCTOR_ID = 0;
|
|
$this->SENDER_DOCTOR_ID = 0;
|
|
$this->SENDER_ADDRESS_ID = 0;
|
|
$this->ONLINE_USER_ID = 1500;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
echo "Fixing snapshot ss_piutang_test";
|
|
}
|
|
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]];
|
|
}
|
|
|
|
function now()
|
|
{
|
|
return Date("Y-m-d H:i:s");
|
|
}
|
|
|
|
public function fix($futureNo)
|
|
{
|
|
$sql = "select FutureOrderID,
|
|
FutureOrderNumber, FutureOrderDateBooking, FutureOrderIsActive,
|
|
JSON_EXTRACT( fn_get_patient_atribute(FutureOrderM_PatientID) , '$.patient_fullname' )
|
|
PatientName,
|
|
JSON_EXTRACT( fn_get_patient_atribute(FutureOrderM_PatientID) , '$.M_PatientHP' )
|
|
PatientHP,
|
|
ifnull(T_OrderHeaderLabNumber,'Pending') LabNumber ,
|
|
if(FutureOrderDateBooking > date(now()), 'Y', 'N') inFuture,
|
|
if(FutureMapT_OrderHeaderID is not null, 'Y', 'N') isMapped
|
|
from future_order
|
|
left join future_map on FutureOrderID = FutureMapFutureOrderID
|
|
left join t_orderheader on FutureMapT_OrderHeaderID = T_OrderHeaderID
|
|
where FutureOrderNumber = ?
|
|
";
|
|
list($status, $row) = $this->get_one_row($sql, [$futureNo]);
|
|
if ($status === false) {
|
|
echo "ERR get Future Order: $row";
|
|
exit();
|
|
}
|
|
$this->print_table([$row], array_keys($row));
|
|
if ($row["inFuture"] == "Y") {
|
|
echo "<h4> Booking masa depan </h4>";
|
|
exit();
|
|
}
|
|
if ($row["FutureOrderIsActive"] == "N") {
|
|
echo "<h4> Booking dihapus </h4>";
|
|
exit();
|
|
}
|
|
|
|
if ($row["isMapped"] == "Y") {
|
|
echo "<h4> Sudah Tercreate ORDER {$row["LabNumber"]} </h4>";
|
|
exit();
|
|
}
|
|
|
|
$url_fix =
|
|
"/one-api/fix/fix_future/do_fix/" .
|
|
$row["FutureOrderID"] .
|
|
"/" .
|
|
md5($row["FutureOrderNumber"]);
|
|
echo "<div>
|
|
<button onClick=\"document.location.href='{$url_fix}'\"
|
|
style='padding:5px; color:white; border: solid 0px; border-radius: 5px;
|
|
background-color:blue; margin-top:20px; display:block;' >Generate Order</button> </div>\n";
|
|
}
|
|
|
|
function do_fix($futureOrderID, $md5Future)
|
|
{
|
|
$sql = "select FutureOrderNumber,
|
|
if(FutureOrderDateBooking > date(now()) , 'Y' , 'N') inFuture,
|
|
if(FutureMapID is not null,'Y','N') isMapped,
|
|
FutureMapT_OrderHeaderID
|
|
from
|
|
future_order
|
|
left join future_map on FutureOrderID = FutureMapFutureOrderID and FutureMapIsActive ='Y'
|
|
where FutureOrderID = ?
|
|
and FutureOrderIsActive = 'Y' ";
|
|
list($status, $msg) = $this->get_one_row($sql, [$futureOrderID]);
|
|
if ($status === false) {
|
|
echo $msg;
|
|
exit();
|
|
}
|
|
$row = $msg;
|
|
$futureOrderNumber = $row["FutureOrderNumber"];
|
|
if (md5($row["FutureOrderNumber"]) != $md5Future) {
|
|
echo "Invalid Redirect Link";
|
|
exit();
|
|
}
|
|
if ($row["inFuture"] == "Y") {
|
|
echo "Booking Masa depan > hari ini";
|
|
exit();
|
|
}
|
|
if ($row["inMapped"] == "Y") {
|
|
echo "Booking sudah ada";
|
|
exit();
|
|
}
|
|
|
|
ob_start();
|
|
echo "<H5>Generate Order</H5>";
|
|
$this->generate_order($futureOrderID);
|
|
$result = ob_get_contents();
|
|
ob_end_clean();
|
|
echo "<div style='padding:10px; border-radius:10px; border:solid 1px #383838'>
|
|
<pre>$result</pre>
|
|
</div>";
|
|
|
|
$url_fix = "/one-ui/test/vuex/one-fo-cashier-new-payment-v4/";
|
|
|
|
echo "<div>
|
|
<button onClick=\"document.location.href='{$url_fix}'\"
|
|
style='padding:5px; color:white; border: solid 0px; border-radius: 5px;
|
|
background-color:blue; margin-top:20px; display:block;' >Kasir</button> </div>\n";
|
|
}
|
|
|
|
|
|
public function fix_debug($futureNo)
|
|
{
|
|
$sql = "select FutureOrderID,
|
|
FutureOrderNumber, FutureOrderDateBooking, FutureOrderIsActive,
|
|
JSON_EXTRACT( fn_get_patient_atribute(FutureOrderM_PatientID) , '$.patient_fullname' )
|
|
PatientName,
|
|
JSON_EXTRACT( fn_get_patient_atribute(FutureOrderM_PatientID) , '$.M_PatientHP' )
|
|
PatientHP,
|
|
ifnull(T_OrderHeaderLabNumber,'Pending') LabNumber ,
|
|
if(FutureOrderDateBooking > date(now()), 'Y', 'N') inFuture,
|
|
if(FutureMapT_OrderHeaderID is not null, 'Y', 'N') isMapped
|
|
from future_order
|
|
left join future_map on FutureOrderID = FutureMapFutureOrderID
|
|
left join t_orderheader on FutureMapT_OrderHeaderID = T_OrderHeaderID
|
|
where FutureOrderNumber = ?
|
|
";
|
|
list($status, $row) = $this->get_one_row($sql, [$futureNo]);
|
|
if ($status === false) {
|
|
echo "ERR get Future Order: $row";
|
|
exit();
|
|
}
|
|
$this->print_table([$row], array_keys($row));
|
|
if ($row["inFuture"] == "Y") {
|
|
echo "<h4> Booking masa depan </h4>";
|
|
exit();
|
|
}
|
|
if ($row["FutureOrderIsActive"] == "N") {
|
|
echo "<h4> Booking dihapus </h4>";
|
|
exit();
|
|
}
|
|
|
|
if ($row["isMapped"] == "Y") {
|
|
echo "<h4> Sudah Tercreate ORDER {$row["LabNumber"]} </h4>";
|
|
exit();
|
|
}
|
|
|
|
$url_fix =
|
|
"/one-api/fix/fix_future/do_fix_debug/" .
|
|
$row["FutureOrderID"] .
|
|
"/" .
|
|
md5($row["FutureOrderNumber"]);
|
|
echo "<div>
|
|
<button onClick=\"document.location.href='{$url_fix}'\"
|
|
style='padding:5px; color:white; border: solid 0px; border-radius: 5px;
|
|
background-color:blue; margin-top:20px; display:block;' >Generate Order</button> </div>\n";
|
|
}
|
|
|
|
function do_fix_debug($futureOrderID, $md5Future)
|
|
{
|
|
$sql = "select FutureOrderNumber,
|
|
if(FutureOrderDateBooking > date(now()) , 'Y' , 'N') inFuture,
|
|
if(FutureMapID is not null,'Y','N') isMapped,
|
|
FutureMapT_OrderHeaderID
|
|
from
|
|
future_order
|
|
left join future_map on FutureOrderID = FutureMapFutureOrderID and FutureMapIsActive ='Y'
|
|
where FutureOrderID = ?
|
|
and FutureOrderIsActive = 'Y' ";
|
|
list($status, $msg) = $this->get_one_row($sql, [$futureOrderID]);
|
|
if ($status === false) {
|
|
echo $msg;
|
|
exit();
|
|
}
|
|
$row = $msg;
|
|
$futureOrderNumber = $row["FutureOrderNumber"];
|
|
if (md5($row["FutureOrderNumber"]) != $md5Future) {
|
|
echo "Invalid Redirect Link";
|
|
exit();
|
|
}
|
|
if ($row["inFuture"] == "Y") {
|
|
echo "Booking Masa depan > hari ini";
|
|
exit();
|
|
}
|
|
if ($row["inMapped"] == "Y") {
|
|
echo "Booking sudah ada";
|
|
exit();
|
|
}
|
|
|
|
ob_start();
|
|
echo "<H5>Generate Order</H5>";
|
|
$this->generate_order_debug($futureOrderID);
|
|
$result = ob_get_contents();
|
|
ob_end_clean();
|
|
echo "<div style='padding:10px; border-radius:10px; border:solid 1px #383838'>
|
|
<pre>$result</pre>
|
|
</div>";
|
|
|
|
$url_fix = "/one-ui/test/vuex/one-fo-cashier-new-payment-v4/";
|
|
|
|
echo "<div>
|
|
<button onClick=\"document.location.href='{$url_fix}'\"
|
|
style='padding:5px; color:white; border: solid 0px; border-radius: 5px;
|
|
background-color:blue; margin-top:20px; display:block;' >Kasir</button> </div>\n";
|
|
}
|
|
public function print_table_style()
|
|
{
|
|
echo "
|
|
<style>
|
|
th, td {
|
|
padding: 15px;
|
|
text-align: left;
|
|
}
|
|
tr:nth-child(even) {background-color: #f2f2f2;}
|
|
table {
|
|
border: solid 1px ;
|
|
min-width:600px;
|
|
}
|
|
</style>
|
|
";
|
|
}
|
|
|
|
public function print_table($rows, $keys)
|
|
{
|
|
$this->print_table_style();
|
|
echo "<table>";
|
|
echo "<tr>";
|
|
foreach ($keys as $k) {
|
|
echo "<td>$k</td>";
|
|
}
|
|
echo "</tr>\n";
|
|
foreach ($rows as $r) {
|
|
echo "<tr>";
|
|
foreach ($keys as $k) {
|
|
echo "<td>" . $r[$k] . "</td>";
|
|
}
|
|
echo "</tr>";
|
|
}
|
|
echo "</table>";
|
|
}
|
|
|
|
public function get_branch_default()
|
|
{
|
|
$sql =
|
|
"select * from m_branch where M_BranchIsDefault = 'Y' and M_BranchIsActive = 'Y'";
|
|
$qry = $this->db->query($sql);
|
|
if (!$qry) {
|
|
echo "{$this->now()} ERR : {$this->db->error()["message"]}\n";
|
|
exit();
|
|
}
|
|
$rows = $qry->result_array();
|
|
if (count($rows) == 0) {
|
|
echo "{$this->now()} ERR Get Default Branch: {$this->db->error()["message"]}\n";
|
|
exit();
|
|
}
|
|
$branchKelurahanID = $rows[0]["M_BranchM_KelurahanID"];
|
|
$this->SENDER_DOCTOR_ID = $rows[0]["M_BranchM_DoctorID"];
|
|
$this->SENDER_ADDRESS_ID = $rows[0]["M_BranchM_DoctorAddressID"];
|
|
|
|
$sql =
|
|
"select * from m_doctorpj where M_DoctorPjIsActive = 'Y' and M_DoctorPjIsDefaultPJ='Y'";
|
|
$qry = $this->db->query($sql);
|
|
if (!$qry) {
|
|
echo "{$this->now()} ERR : {$this->db->error()["message"]}\n";
|
|
exit();
|
|
}
|
|
$rows = $qry->result_array();
|
|
if (count($rows) == 0) {
|
|
echo "{$this->now()} ERR Get Default PJ: {$this->db->error()["message"]}\n";
|
|
exit();
|
|
}
|
|
$this->PJ_DOCTOR_ID = $rows[0]["M_DoctorPjM_DoctorID"];
|
|
|
|
return [$branchKelurahanID];
|
|
}
|
|
|
|
public function do_pelunasan(
|
|
$headerID,
|
|
$bankAccountID,
|
|
$paymentTypeID,
|
|
$paymentNote,
|
|
$amount
|
|
) {
|
|
$paymentTotal = $amount;
|
|
$paymentDate = date("Y-m-d H:i:s");
|
|
$arr = [
|
|
"F_PaymentT_OrderHeaderID" => $headerID,
|
|
"F_PaymentDate" => $paymentDate,
|
|
"F_PaymentNote" => $paymentNote,
|
|
"F_PaymentTotal" => $paymentTotal,
|
|
"F_PaymentM_UserID" => $this->ONLINE_USER_ID,
|
|
];
|
|
$qry = $this->db->insert("f_payment", $arr);
|
|
if (!$qry) {
|
|
echo "{$this->now()} ERR Get Total Order: {$this->db->error()["message"]} | {$this->db->last_query()}\n";
|
|
return false;
|
|
}
|
|
$paymentID = $this->db->insert_id();
|
|
$detailNote = "";
|
|
|
|
$arr = [
|
|
"F_PaymentDetailF_PaymentID" => $paymentID,
|
|
"F_PaymentDetailM_PaymentTypeID" => $paymentTypeID,
|
|
"F_PaymentDetailEDCNat_BankID" => 0,
|
|
"F_PaymentDetailCardNat_BankID" => 0,
|
|
"F_PaymentDetailM_BankAccountID" => $bankAccountID,
|
|
"F_PaymentDetailNote" => $paymentNote,
|
|
"F_PaymentDetailAmount" => $paymentTotal,
|
|
"F_PaymentDetailActual" => $paymentTotal,
|
|
"F_PaymentDetailChange" => 0,
|
|
];
|
|
$qry = $this->db->insert("f_paymentdetail", $arr);
|
|
if (!$qry) {
|
|
echo "{$this->now()} ERR Pelunasan: {$this->db->error()["message"]} | {$this->db->last_query()}\n";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function add_delivery_note($orderHeaderID, $delivery_note)
|
|
{
|
|
$rst_id = $orderHeaderID;
|
|
$db_msg = "";
|
|
foreach ($delivery_note as $k => $v) {
|
|
if ($v["noteplus"] != "") {
|
|
if (
|
|
$v["delivery_code"] == "EMAIL" ||
|
|
$v["delivery_code"] == "WHATSAPP" ||
|
|
$v["delivery_code"] == "TELEGRAM"
|
|
) {
|
|
$sql = "UPDATE t_orderdelivery SET T_OrderDeliveryDestination = '{$v["noteplus"]}'
|
|
WHERE
|
|
T_OrderDeliveryT_OrderHeaderID = {$rst_id} AND
|
|
T_OrderDeliveryM_DeliveryID = {$v["delivery_id"]} AND
|
|
T_OrderDeliveryM_DeliveryTypeID = {$v["delivery_type_id"]} AND
|
|
T_OrderDeliveryAddressID = {$v["address_id"]} AND
|
|
T_OrderDeliveryIsActive = 'Y'";
|
|
$qry = $this->db_smartone->query($sql);
|
|
if (!$qry) {
|
|
$db_msg .=
|
|
"ERR : Update T_orderDelivery " .
|
|
$this->db->error()["message"] .
|
|
" | " .
|
|
$$this->db->last_query();
|
|
$db_msg .= "\n";
|
|
}
|
|
} else {
|
|
$sql = "SELECT T_OrderDeliveryID as xdel_id
|
|
FROM t_orderdelivery
|
|
WHERE
|
|
T_OrderDeliveryT_OrderHeaderID = {$rst_id} AND
|
|
T_OrderDeliveryM_DeliveryID = {$v["delivery_id"]} AND
|
|
T_OrderDeliveryM_DeliveryTypeID = {$v["delivery_type_id"]} AND
|
|
T_OrderDeliveryAddressID = {$v["address_id"]} AND
|
|
T_OrderDeliveryIsActive = 'Y'";
|
|
//echo $sql;
|
|
$qry = $this->db_smartone->query($sql);
|
|
if (!$qry) {
|
|
$db_msg .=
|
|
"ERR : Get T_orderDelivery for XDel " .
|
|
$this->db->error()["message"] .
|
|
" | " .
|
|
$$this->db->last_query();
|
|
$db_msg .= "\n";
|
|
}
|
|
$xdel_id = $qry->row()->xdel_id;
|
|
|
|
$sql = "INSERT INTO t_orderdeliverynote(
|
|
T_OrderDeliveryNoteT_OrderDeliveryID,
|
|
T_OrderDeliveryNoteValue,
|
|
T_OrderDeliveryNoteCreated,
|
|
T_OrderDeliveryNoteUserID
|
|
)
|
|
VALUES(
|
|
{$xdel_id},
|
|
'{$v["noteplus"]}',
|
|
NOW(),
|
|
{$this->sys_user["M_UserID"]}
|
|
)";
|
|
$qry = $this->db_smartone->query($sql);
|
|
if (!$qry) {
|
|
$db_msg .=
|
|
"ERR : Update T_orderDelivery " .
|
|
$this->db->error()["message"] .
|
|
" | " .
|
|
$$this->db->last_query();
|
|
$db_msg .= "\n";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return $db_msg;
|
|
}
|
|
|
|
public function generate_order_debug($futureOrderID)
|
|
{
|
|
$this->db->trans_begin();
|
|
$this->get_branch_default();
|
|
|
|
$sql = "select future_order.*,
|
|
if(FutureMapIsActive is null , 'N', 'Y') isMapped
|
|
from future_order
|
|
left join future_map
|
|
on FutureOrderID = FutureMapFutureOrderID
|
|
where FutureOrderID = ?
|
|
";
|
|
$qry = $this->db->query($sql, [$futureOrderID]);
|
|
if (!$qry) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err GetFutureOrder {$this->db->error()["message"]} | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
$rows = $qry->result_array();
|
|
if (count($rows) == 0) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err GetFutureOrder not found | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
if ($rows[0]["isMapped"] == "Y") {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err Order sudah di future_map | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
$futureOrder = $rows[0];
|
|
echo "{$this->now()} Create Order from {$futureOrder["FutureOrderNumber"]} \n";
|
|
$x_header = ($futureOrder["FutureOrderJSONHeader"]);
|
|
echo "$x_header\n";
|
|
$x_header = preg_replace('/[[:^print:]]/', '', $x_header);
|
|
$header = json_decode($x_header,true);
|
|
echo "HEADER : ". $x_header;
|
|
echo "JSON LAST ERR : " . json_last_error_msg() . "\n";
|
|
print_r($header);
|
|
echo "<br/><br/>";
|
|
$header["date"] = date("Y-m-d H:i:s");
|
|
echo "New Header: " ;
|
|
print_r($header);
|
|
$t_header = json_encode($header);
|
|
echo "XXX Header : $t_header | " . json_last_error_msg();
|
|
echo "<br/><br/>";
|
|
|
|
|
|
$x_delivery = $futureOrder["FutureOrderJSONDelivery"];
|
|
$x_delivery = str_replace("\n","",$x_delivery);
|
|
|
|
$delivery = json_decode($x_delivery, true);
|
|
echo "Raw Delivery :<br/> $x_delivery <br/>";
|
|
echo "Decode Delivery Json : " . json_last_error_msg() . "<br/>";
|
|
|
|
$delivery_note = json_decode(
|
|
$futureOrder["FutureOrderJSONDeliveryNote"],
|
|
true
|
|
);
|
|
$req = json_decode($futureOrder["FutureOrderJSONReq"], true);
|
|
$detail = json_decode($futureOrder["FutureOrderJSONDetail"], true);
|
|
$header_json = addslashes(
|
|
str_replace('\n', '\\\n', json_encode($header))
|
|
);
|
|
$detail_json = str_replace('\n', '\\\n', json_encode($detail));
|
|
$delivery_json = addslashes(
|
|
str_replace('\n', '\\\n', json_encode($delivery))
|
|
);
|
|
|
|
echo "<br/>";
|
|
echo "Delivery String:<br/> ";
|
|
echo $delivery;
|
|
echo "<br/>";
|
|
$req_json = json_encode($req);
|
|
echo "<br/>";
|
|
print_r($header_json);
|
|
echo "<br/>";
|
|
print_r($detail_json);
|
|
echo "<br/>";
|
|
echo "Delivery JSON:";
|
|
print_r($delivery_json);
|
|
|
|
$sql = "CALL sp_fo_register_save_online_debug(0, '{$header_json}', '{$delivery_json}',
|
|
'{$detail_json}', '{$req_json}', '3');";
|
|
$query = $this->db->query($sql);
|
|
if (!$query) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err Create Order : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
$rows = $query->result_array();
|
|
|
|
$this->db->trans_rollback();
|
|
$rst_data = json_decode($rows[0]["data"]);
|
|
$rst_id = $rst_data->id;
|
|
if ($rows[0]["status"] == "ERR") {
|
|
echo "ERR CALL sp_fo_register_save_online_debug| ";
|
|
echo "<br/>";
|
|
print_r($rows);
|
|
$this->db->trans_rollback();
|
|
return;
|
|
}
|
|
$this->db->trans_rollback();
|
|
echo "DEBUG ONLY";
|
|
return;
|
|
|
|
$this->clean_mysqli_connection($this->db->conn_id);
|
|
|
|
$orderHeaderID = $rst_id;
|
|
|
|
if ($orderHeaderID == 0) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err Generate Order | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
$orderHeaderNo = $rst->data->number;
|
|
|
|
$db_msg = $this->add_delivery_note($orderHeaderID, $delivery_note);
|
|
if ($db_msg != "") {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err Add Delivery Note $db_msg\n";
|
|
return;
|
|
}
|
|
|
|
//Doctor Alias
|
|
$sql = "UPDATE t_orderheaderaddon
|
|
SET T_OrderHeaderAddOnAliasDoctorName = '{$futureOrder["FutureOrderDoctorAlias"]}',
|
|
T_OrderHeaderAddOnAliasDoctorAddress = '{$futureOrder["FutureOrderDoctorAddress"]}',
|
|
T_OrderHeaderAddOnFoTimeStart = '{$futureOrder["FutureOrderFoTimeStart"]}',
|
|
T_OrderHeaderAddOnFoTimeEnd = '{$futureOrder["FutureOrderFoTimeEnd"]}'
|
|
WHERE
|
|
T_OrderHeaderAddOnT_OrderHeaderID = '{$orderHeaderID}'";
|
|
$query = $this->db->query($sql);
|
|
if (!$query) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err Create Order : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
|
|
// futtureMap
|
|
$arr = [
|
|
"FutureMapT_OrderHeaderID" => $orderHeaderID,
|
|
"FutureMapFutureOrderID" => $futureOrderID,
|
|
];
|
|
$qry = $this->db->insert("future_map", $arr);
|
|
if (!$qry) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err Future Map: {$this->db->error()["message"]} | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
//
|
|
// Down payment
|
|
//
|
|
$sql = "select group_concat(FutureDownpaymentNumber) as dpNo,
|
|
sum(FutureDownpaymentTotal) dpTotal
|
|
from future_downpayment
|
|
where FutureDownpaymentFutureOrderID = ?
|
|
and FutureDownpaymentIsActive='Y'";
|
|
|
|
$query = $this->db->query($sql, $futureOrderID);
|
|
if (!$query) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err Get DownPaymentNumber : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
$rows = $query->result_array();
|
|
$dpTotal = 0;
|
|
$dpNo = "";
|
|
if (count($rows) > 0) {
|
|
$dpTotal = $rows[0]["dpTotal"];
|
|
$dpNo = $rows[0]["dpNo"];
|
|
}
|
|
|
|
if ($dpTotal > 0) {
|
|
$bankAccountID = 0;
|
|
$paymentTypeID = 10;
|
|
$paymentNote = "DP : $dpNo";
|
|
$rst_payment = $this->do_pelunasan(
|
|
$orderHeaderID,
|
|
$bankAccountID,
|
|
$paymentTypeID,
|
|
$paymentNote,
|
|
$dpTotal
|
|
);
|
|
}
|
|
|
|
if ($this->db->trans_status === false) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} ERR : {$db_msg}\n";
|
|
} else {
|
|
$this->db->trans_commit();
|
|
echo "{$this->now()} $orderHeaderNo [Success]\n";
|
|
}
|
|
}
|
|
public function generate_order($futureOrderID)
|
|
{
|
|
$this->db->trans_begin();
|
|
$this->get_branch_default();
|
|
|
|
$sql = "select future_order.*,
|
|
if(FutureMapIsActive is null , 'N', 'Y') isMapped
|
|
from future_order
|
|
left join future_map
|
|
on FutureOrderID = FutureMapFutureOrderID
|
|
where FutureOrderID = ?
|
|
";
|
|
$qry = $this->db->query($sql, [$futureOrderID]);
|
|
if (!$qry) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err GetFutureOrder {$this->db->error()["message"]} | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
$rows = $qry->result_array();
|
|
if (count($rows) == 0) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err GetFutureOrder not found | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
if ($rows[0]["isMapped"] == "Y") {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err Order sudah di future_map | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
$futureOrder = $rows[0];
|
|
echo "{$this->now()} Create Order from {$futureOrder["FutureOrderNumber"]} \n";
|
|
|
|
$x_header = ($futureOrder["FutureOrderJSONHeader"]);
|
|
$x_header = preg_replace('/[[:^print:]]/', '', $x_header);
|
|
$header = json_decode($x_header,true);
|
|
$header["date"] = date("Y-m-d H:i:s");
|
|
|
|
$x_delivery = $futureOrder["FutureOrderJSONDelivery"];
|
|
$x_delivery = str_replace("\n","",$x_delivery);
|
|
|
|
$delivery = json_decode($x_delivery, true);
|
|
|
|
$delivery_note = json_decode(
|
|
$futureOrder["FutureOrderJSONDeliveryNote"],
|
|
true
|
|
);
|
|
$req = json_decode($futureOrder["FutureOrderJSONReq"], true);
|
|
$detail = json_decode($futureOrder["FutureOrderJSONDetail"], true);
|
|
$header_json = addslashes(
|
|
str_replace('\n', '\\\n', json_encode($header))
|
|
);
|
|
$detail_json = str_replace('\n', '\\\n', json_encode($detail));
|
|
$delivery_json = addslashes(
|
|
str_replace('\n', '\\\n', json_encode($delivery))
|
|
);
|
|
|
|
$req_json = json_encode($req);
|
|
$sql = "CALL sp_fo_register_save_online(0, '{$header_json}', '{$delivery_json}',
|
|
'{$detail_json}', '{$req_json}', '3');";
|
|
$query = $this->db->query($sql);
|
|
if (!$query) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err Create Order : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
$rst = $query->row();
|
|
$rst->data = json_decode($rst->data);
|
|
$rst_id = $rst->data->id;
|
|
|
|
$this->clean_mysqli_connection($this->db->conn_id);
|
|
|
|
$orderHeaderID = $rst_id;
|
|
|
|
if ($orderHeaderID == 0) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err Generate Order | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
$orderHeaderNo = $rst->data->number;
|
|
|
|
$db_msg = $this->add_delivery_note($orderHeaderID, $delivery_note);
|
|
if ($db_msg != "") {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err Add Delivery Note $db_msg\n";
|
|
return;
|
|
}
|
|
|
|
//Doctor Alias
|
|
$sql = "UPDATE t_orderheaderaddon
|
|
SET T_OrderHeaderAddOnAliasDoctorName = '{$futureOrder["FutureOrderDoctorAlias"]}',
|
|
T_OrderHeaderAddOnAliasDoctorAddress = '{$futureOrder["FutureOrderDoctorAddress"]}',
|
|
T_OrderHeaderAddOnFoTimeStart = '{$futureOrder["FutureOrderFoTimeStart"]}',
|
|
T_OrderHeaderAddOnFoTimeEnd = '{$futureOrder["FutureOrderFoTimeEnd"]}'
|
|
WHERE
|
|
T_OrderHeaderAddOnT_OrderHeaderID = '{$orderHeaderID}'";
|
|
$query = $this->db->query($sql);
|
|
if (!$query) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err Create Order : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
|
|
// futtureMap
|
|
$arr = [
|
|
"FutureMapT_OrderHeaderID" => $orderHeaderID,
|
|
"FutureMapFutureOrderID" => $futureOrderID,
|
|
];
|
|
$qry = $this->db->insert("future_map", $arr);
|
|
if (!$qry) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err Future Map: {$this->db->error()["message"]} | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
//
|
|
// Down payment
|
|
//
|
|
$sql = "select group_concat(FutureDownpaymentNumber) as dpNo,
|
|
sum(FutureDownpaymentTotal) dpTotal
|
|
from future_downpayment
|
|
where FutureDownpaymentFutureOrderID = ?
|
|
and FutureDownpaymentIsActive='Y'";
|
|
|
|
$query = $this->db->query($sql, $futureOrderID);
|
|
if (!$query) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} Err Get DownPaymentNumber : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
$rows = $query->result_array();
|
|
$dpTotal = 0;
|
|
$dpNo = "";
|
|
if (count($rows) > 0) {
|
|
$dpTotal = $rows[0]["dpTotal"];
|
|
$dpNo = $rows[0]["dpNo"];
|
|
}
|
|
|
|
if ($dpTotal > 0) {
|
|
$bankAccountID = 0;
|
|
$paymentTypeID = 10;
|
|
$paymentNote = "DP : $dpNo";
|
|
$rst_payment = $this->do_pelunasan(
|
|
$orderHeaderID,
|
|
$bankAccountID,
|
|
$paymentTypeID,
|
|
$paymentNote,
|
|
$dpTotal
|
|
);
|
|
}
|
|
|
|
if ($this->db->trans_status === false) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} ERR : {$db_msg}\n";
|
|
} else {
|
|
$this->db->trans_commit();
|
|
echo "{$this->now()} $orderHeaderNo [Success]\n";
|
|
}
|
|
}
|
|
|
|
function do_order_log($orderHeaderID, $delivery)
|
|
{
|
|
$rst_id = $$orderHeaderID;
|
|
|
|
$sql = "SELECT *
|
|
FROM t_orderheader
|
|
JOIN m_patient ON M_PatientID = T_OrderHeaderM_PatientID
|
|
WHERE
|
|
T_OrderHeaderID = {$rst_id}";
|
|
//echo $sql;
|
|
$qry = $this->db_smartone->query($sql);
|
|
if (!$qry) {
|
|
$this->db->trans_rollback();
|
|
echo "{$this->now()} ERR : {$db_msg}\n";
|
|
return false;
|
|
}
|
|
$x_header = $qry->row_array();
|
|
|
|
$sql = "SELECT *
|
|
FROM t_orderdetail
|
|
WHERE
|
|
T_OrderDetailT_OrderHeaderID = {$rst_id} AND
|
|
T_OrderDetailT_TestIsPrice = 'Y' AND
|
|
T_OrderDetailIsActive = 'Y'";
|
|
|
|
$x_details = $this->db_smartone->query($sql)->result_array();
|
|
$x_details = json_encode($x_details);
|
|
$x_deliveries = json_encode($delivery);
|
|
|
|
$sql = "SELECT *
|
|
FROM t_orderpromise
|
|
WHERE
|
|
T_OrderPromiseT_OrderHeaderID = {$rst_id} AND T_OrderPromiseIsActive = 'Y'";
|
|
$x_promises = $this->db_smartone->query($sql)->result_array();
|
|
$x_promises = json_encode($x_promises);
|
|
|
|
$sql = "INSERT INTO order_log(
|
|
OrderLogT_OrderHeaderID,
|
|
OrderLogM_PatientDOB,
|
|
OrderLogM_CompanyID,
|
|
OrderLogM_MouID,
|
|
OrderLogM_DoctorSenderID,
|
|
OrderLogM_DoctorSenderAddressID,
|
|
orderLogT_OrderHeaderAddOnAliasDoctorName,
|
|
orderLogT_OrderHeaderAddOnAliasDoctorAddress,
|
|
OrderLogAge,
|
|
OrderLogFoNote,
|
|
OrderLogSubtotal,
|
|
OrderLogTotal,
|
|
OrderLogUserID,
|
|
OrderLogDetails,
|
|
OrderLogDeliveries,
|
|
OrderLogPromises
|
|
)
|
|
VALUES(
|
|
{$rst_id},
|
|
'{$x_header["M_PatientDOB"]}',
|
|
{$x_header["T_OrderHeaderM_CompanyID"]},
|
|
{$x_header["T_OrderHeaderM_MouID"]},
|
|
{$x_header["T_OrderHeaderSenderM_DoctorID"]},
|
|
{$x_header["T_OrderHeaderSenderM_DoctorAddressID"]},
|
|
'{$hdr["alias_doctor"]}',
|
|
'{$hdr["alias_doctor_address"]}',
|
|
'{$x_header["T_OrderHeaderM_PatientAge"]}',
|
|
'{$x_header["T_OrderHeaderFoNote"]}',
|
|
'{$x_header["T_OrderHeaderSubTotal"]}',
|
|
'{$x_header["T_OrderHeaderTotal"]}',
|
|
{$this->sys_user["M_UserID"]},
|
|
'{$x_details}',
|
|
'{$x_deliveries}',
|
|
'{$x_promises}'
|
|
)";
|
|
$this->db->query($sql);
|
|
}
|
|
public function update_delivery($orderID, $deliveries)
|
|
{
|
|
$db_msg = "";
|
|
$sql = "update t_orderdelivery set T_OrderDeliveryIsActive = 'N'
|
|
where T_OrderDeliveryT_OrderHeaderID = ?";
|
|
$qry = $this->db->query($sql, [$orderID]);
|
|
if (!$qry) {
|
|
return "ERR Update Delivery | " .
|
|
$this->db->error()["message"] .
|
|
"|" .
|
|
$this->db->last_query();
|
|
}
|
|
foreach ($deliveries as $d) {
|
|
$arr = [
|
|
"T_OrderDeliveryT_OrderHeaderID" => $orderID,
|
|
"T_OrderDeliveryM_DeliveryID" =>
|
|
$d["T_OrderDeliveriesM_DeliveryID"],
|
|
"T_OrderDeliveryM_DeliveryTypeID" =>
|
|
$d["T_OrderDeliveriesM_DeliveryTypeID"],
|
|
"T_OrderDeliveryDestination" =>
|
|
$d["T_OrderDeliveriesDestination"],
|
|
];
|
|
$qry = $this->db->insert("t_orderdelivery", $arr);
|
|
if (!$qry) {
|
|
$db_msg .=
|
|
"ERR Update Delivery | " .
|
|
$this->db->error()["message"] .
|
|
"|" .
|
|
$this->db->last_query() .
|
|
"\n";
|
|
}
|
|
}
|
|
return $db_msg;
|
|
}
|
|
}
|