430 lines
17 KiB
PHP
430 lines
17 KiB
PHP
<?php
|
|
class Future_generate extends MY_Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->debug = false;
|
|
$this->PJ_DOCTOR_ID = 0;
|
|
$this->SENDER_DOCTOR_ID = 0;
|
|
$this->SENDER_ADDRESS_ID = 0;
|
|
$this->ONLINE_USER_ID=1500;
|
|
}
|
|
public function now()
|
|
{
|
|
return Date("Y-m-d H:i:s");
|
|
}
|
|
|
|
|
|
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 array($branchKelurahanID);
|
|
}
|
|
|
|
|
|
|
|
public function do_pelunasan(
|
|
$headerID,
|
|
$bankAccountID,
|
|
$paymentTypeID,
|
|
$paymentNote,
|
|
$amount
|
|
) {
|
|
$paymentTotal = $amount;
|
|
$paymentDate = date("Y-m-d H:i:s");
|
|
$arr = array(
|
|
"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 = array(
|
|
"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 daily() {
|
|
echo "{$this->now()} Get Daily FutureOrder Process.\n";
|
|
$sql = "select FutureOrderID, FutureOrderNumber
|
|
from future_order
|
|
left join future_map on FutureOrderID = FutureMapFutureOrderID
|
|
and FutureMapIsActive = 'Y'
|
|
where FutureOrderIsActive = 'Y'
|
|
and FutureOrderDateBooking = date(now())
|
|
and FutureMapID is null";
|
|
$qry = $this->db->query($sql);
|
|
if (!$qry) {
|
|
echo "{$this->now()} Err Get Daily FutureOrder {$this->db->error()['message']} | {$this->db->last_query()}\n";
|
|
return;
|
|
}
|
|
$rows = $qry->result_array();
|
|
foreach($rows as $r ) {
|
|
$futureOrderID = $r["FutureOrderID"];
|
|
$futureOrderNumber = $r["FutureOrderNumber"];
|
|
echo "{$this->now()} Processing {$futureOrderNumber}\n";
|
|
$this->generate_order($futureOrderID);
|
|
}
|
|
}
|
|
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";
|
|
|
|
$header = json_decode($futureOrder["FutureOrderJSONHeader"],true);
|
|
$delivery = json_decode($futureOrder["FutureOrderJSONDelivery"],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_v5('', '{$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 = array(
|
|
"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 ) {
|
|
echo "{$this->now()} ERR : No Down Payment\nDP NO. $dpNo \n Amount: $dpTotal\n";
|
|
$this->db->trans_rollback();
|
|
return;
|
|
}
|
|
$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, array($orderID));
|
|
if (!$qry) {
|
|
return "ERR Update Delivery | " . $this->db->error()["message"] . "|"
|
|
. $this->db->last_query();
|
|
}
|
|
foreach ($deliveries as $d) {
|
|
$arr = array(
|
|
"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;
|
|
}
|
|
|
|
}
|
|
// asumsi 1 order 1 janji hasil
|