1093 lines
32 KiB
PHP
1093 lines
32 KiB
PHP
<?php
|
|
|
|
class Payment extends MY_Controller
|
|
{
|
|
var $db_onedev;
|
|
public function index()
|
|
{
|
|
echo "API";
|
|
}
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->db_onedev = $this->load->database("onedev", true);
|
|
}
|
|
|
|
/**
|
|
** FUNCTIONS FITUR KIRIM WA KWITANSI START HERE
|
|
*/
|
|
public function kirim_bukti_tx_via_wa()
|
|
{
|
|
try {
|
|
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$this->db_onedev->trans_begin();
|
|
$prm = $this->sys_input;
|
|
|
|
$url = isset($prm['urlX']) ? $prm['urlX'] : "";
|
|
|
|
$concat_PID = "&PID=";
|
|
|
|
$T_OrderHeaderID = isset($prm['T_OrderHeaderID']) ? $prm['T_OrderHeaderID'] : 0;
|
|
$M_PatientHp = isset($prm['M_PatientHp']) ? $prm['M_PatientHp'] : "";
|
|
$M_PatientID = isset($prm['M_PatientID']) ? $prm['M_PatientID'] : 0;
|
|
$T_OrderHeaderLabNumber = isset($prm['T_OrderHeaderLabNumber']) ? $prm['T_OrderHeaderLabNumber'] : "";
|
|
$urlPrint = "";
|
|
|
|
$sql_pid = "SELECT F_PaymentID, T_OrderHeaderDate
|
|
FROM f_payment
|
|
LEFT JOIN t_orderheader ON T_OrderHeaderID = F_PaymentT_OrderHeaderID
|
|
WHERE F_PaymentT_OrderHeaderID = ?
|
|
ORDER BY F_PaymentID DESC
|
|
LIMIT 1";
|
|
|
|
$qpid = $this->db_onedev->query($sql_pid, [$T_OrderHeaderID]);
|
|
if(!$qpid || !$qpid->row_array()){
|
|
$this->db_onedev->trans_rollback();
|
|
$this->sys_error_db("error select f_payment", $this->db_onedev);
|
|
exit;
|
|
}
|
|
|
|
$PID = $qpid->row_array()['F_PaymentID'];
|
|
$T_OrderHeaderDate = $qpid->row_array()['T_OrderHeaderDate'];
|
|
|
|
$hostname = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'];
|
|
$urlPrint .= $hostname . $url . $concat_PID . $PID;
|
|
|
|
// x_wa_outbox
|
|
$XWaOutboxSubject = "Kwitansi WA";
|
|
$XWaOutboxRecipientsNumber = $M_PatientHp;
|
|
$XWaOutboxRecipientsM_PatientID = $M_PatientID;
|
|
$XWaOutboxResultFileName = "kwitansi_".$T_OrderHeaderLabNumber.".pdf";
|
|
$XWaOutboxRefID = $T_OrderHeaderID;
|
|
|
|
$XWaOutboxBody = "";
|
|
|
|
$XWaOutboxLocalUrl= $urlPrint;
|
|
$XWaOutboxType = "KWITANSI";
|
|
|
|
// Kalau sudah pernah di kirim jangan di INSERT lagi
|
|
// Sebenarnya ketika XWaOutboxIsSent != '' sudah tidak bisa dikirim lagi dari FE
|
|
$check_sql = "SELECT XWaOutboxID FROM x_wa_outbox
|
|
WHERE XWaOutboxRefID = ?
|
|
OR XWaOutboxResultFileName = ?
|
|
OR XWaOutboxLocalUrl = ?
|
|
LIMIT 1";
|
|
|
|
$check_query = $this->db_onedev->query($check_sql, array(
|
|
$XWaOutboxRefID,
|
|
$XWaOutboxResultFileName,
|
|
$XWaOutboxLocalUrl
|
|
));
|
|
|
|
if ($check_query && $check_query->num_rows() > 0) {
|
|
$existing_record = $check_query->row();
|
|
$s_update = "UPDATE x_wa_outbox SET
|
|
XWaOutboxSubject = ?,
|
|
XWaOutboxRecipientsNumber = ?,
|
|
XWaOutboxRecipientsM_PatientID = ?,
|
|
XWaOutboxResultFileName = ?,
|
|
XWaOutboxResultDate = ?,
|
|
XWaOutboxBody = ?,
|
|
XWaOutboxLocalUrl = ?,
|
|
XWaOutboxType = ?,
|
|
XWaOutboxRefID = ?,
|
|
XWaOutboxLastUpdated = NOW(),
|
|
XWaOutboxIsSent = ?
|
|
WHERE XWaOutboxID = ?";
|
|
|
|
$qinsert = $this->db_onedev->query($s_update, array(
|
|
$XWaOutboxSubject,
|
|
$XWaOutboxRecipientsNumber,
|
|
$XWaOutboxRecipientsM_PatientID,
|
|
$XWaOutboxResultFileName,
|
|
$T_OrderHeaderDate,
|
|
$XWaOutboxBody,
|
|
$XWaOutboxLocalUrl,
|
|
$XWaOutboxType,
|
|
$XWaOutboxRefID,
|
|
'N', // XWaOutboxIsSent
|
|
$existing_record->XWaOutboxID
|
|
));
|
|
if(!$qinsert){
|
|
$this->db_onedev->trans_rollback();
|
|
$this->sys_error_db("error update wa outbox", $this->db_onedev);
|
|
exit;
|
|
}
|
|
} else {
|
|
$s_insert = "INSERT INTO x_wa_outbox(
|
|
XWaOutboxSubject,
|
|
XWaOutboxRecipientsNumber,
|
|
XWaOutboxRecipientsM_PatientID,
|
|
XWaOutboxResultFileName,
|
|
XWaOutboxResultDate,
|
|
XWaOutboxBody,
|
|
XWaOutboxLocalUrl,
|
|
XWaOutboxType,
|
|
XWaOutboxRefID,
|
|
XWaOutboxIsSent
|
|
) VALUES (?,?,?,?,?,?,?,?,?, 'N')";
|
|
|
|
$qinsert = $this->db_onedev->query($s_insert, [
|
|
$XWaOutboxSubject,
|
|
$XWaOutboxRecipientsNumber,
|
|
$XWaOutboxRecipientsM_PatientID,
|
|
$XWaOutboxResultFileName,
|
|
$T_OrderHeaderDate,
|
|
$XWaOutboxBody,
|
|
$XWaOutboxLocalUrl,
|
|
$XWaOutboxType,
|
|
$XWaOutboxRefID
|
|
]);
|
|
if(!$qinsert){
|
|
$this->db_onedev->trans_rollback();
|
|
$this->sys_error_db("error insert wa outbox", $this->db_onedev);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$this->db_onedev->trans_commit();
|
|
|
|
$result = array(
|
|
"message" => "Sukses Proses Insert Data",
|
|
"sql" => $this->db_onedev->last_query()
|
|
);
|
|
|
|
$this->sys_ok($result);
|
|
|
|
} catch (Exception $exc) {
|
|
$this->sys_error($exc->getMessage());
|
|
}
|
|
}
|
|
|
|
// * List Outbox yang mau dikirim
|
|
public function listOutbox(){
|
|
try {
|
|
$prm = $this->sys_input;
|
|
$status = $prm["statusOutbox"];
|
|
$startDate = $prm["startDate"];
|
|
$endDate = $prm["endDate"];
|
|
|
|
$query = "SELECT
|
|
T_OrderHeaderID as orderID,
|
|
T_OrderHeaderLabNumber as orderNumber,
|
|
DATE_FORMAT(T_OrderHeaderDate, '%d-%m-%Y') as orderDate,
|
|
T_OrderHeaderM_PatientID as patientID,
|
|
DATE_FORMAT(M_PatientDOB, '%d%m%Y') as patientDOB,
|
|
M_PatientDOB,
|
|
CONCAT(IF(ISNULL(M_TitleName),'',CONCAT(M_TitleName,'.')),
|
|
' ',
|
|
IFNULL(M_PatientPrefix,''),
|
|
' ',
|
|
M_PatientName,
|
|
' ',
|
|
IFNULL(M_PatientSuffix,'')) as patientName,
|
|
M_PatientHp as patientHp,
|
|
M_PatientHp as patientHpOld,
|
|
CorporateName,
|
|
XWaOutboxID as sendWaID,
|
|
XWaOutboxIsSent,
|
|
IFNULL(XWaOutboxRetry , 0) as XWaOutboxIsRetry,
|
|
XWaOutboxCdnUrl as fileUrl,
|
|
XWaOutboxLocalUrl as localUrl,
|
|
XWaOutboxResultFilename as fileName,
|
|
DATE_FORMAT(XWaOutboxSentDate, '%d-%m-%Y %H:%i') as sentDate,
|
|
XWaOutboxType as sentType
|
|
FROM t_orderheader
|
|
JOIN x_wa_outbox
|
|
ON T_OrderHeaderID = XWaOutboxRefID
|
|
AND XWaOutboxID IS NOT NULL
|
|
AND XWaOutboxIsSent = ?
|
|
AND XWaOutboxIsActive = 'Y'
|
|
AND XWaOutboxType = 'KWITANSI'
|
|
JOIN m_patient
|
|
ON T_OrderHeaderM_PatientID = M_PatientID
|
|
LEFT JOIN m_title
|
|
ON M_PatientM_TitleID = M_TitleID
|
|
JOIN corporate ON T_OrderHeaderCorporateID = CorporateID
|
|
WHERE T_OrderHeaderIsActive = 'Y'
|
|
AND DATE(T_OrderHeaderDate) BETWEEN ? AND ? ";
|
|
$query = $this->db_onedev->query($query, [$status, $startDate, $endDate]);
|
|
|
|
if (!$query) {
|
|
$message = json_encode($this->db_onedev->error(), JSON_PRETTY_PRINT);
|
|
throw new Exception("Error executing query: " . $message);
|
|
}
|
|
|
|
$result = $query->result_array();
|
|
$this->sys_ok($result);
|
|
|
|
} catch (Exception $e) {
|
|
$msg = $e->getMessage();
|
|
$this->sys_error($msg);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// ** Upload File Kwitansi dari Birt ke CDN Qontak
|
|
public function uploadFile(){
|
|
try {
|
|
$url = "https://service-chat.qontak.com/api/open/v1/file_uploader";
|
|
$fileName = $this->sys_input["fileName"];
|
|
$rpt_url_raw = $this->sys_input["rptUrl"];
|
|
$mimeType = $this->sys_input["mime"]; //application/pdf
|
|
|
|
|
|
// Breakdown rpt_url_raw ke scheme:https, host:devcpone, path:/birt/run, query:__report=...dst
|
|
$url_parts = parse_url($rpt_url_raw);
|
|
|
|
// Base url
|
|
$base_url = $url_parts['scheme'] . '://' . $url_parts['host'] . $url_parts['path'];
|
|
|
|
// Parse query ke array
|
|
$query_params = [];
|
|
if (isset($url_parts['query'])) {
|
|
parse_str($url_parts['query'], $query_params);
|
|
}
|
|
|
|
// Encode setiap query url dari array agar jadi url valid. Misal ada spasi atau escape character
|
|
$encoded_url = $base_url . '?' . http_build_query($query_params);
|
|
|
|
$fileContents = file_get_contents($encoded_url);
|
|
|
|
$this->db_onedev->trans_start();
|
|
|
|
// Jika file tidak ditemukan atau kosong
|
|
if ($fileContents === false || strlen($fileContents) === 0) {
|
|
// Return an error or handle it as needed
|
|
$resp = "Error: Gagal upload file ke CDN karena file local kosong atau tidak bisa diakses. Cek file di URL File Local: " . $rpt_url;
|
|
|
|
$sql = "UPDATE x_wa_outbox SET
|
|
XWaOutboxLastUpdated = NOW(),
|
|
XWaOutboxJsonQontak = ?
|
|
WHERE
|
|
XWaOutboxLocalUrl = ?
|
|
";
|
|
$query = $this->db_onedev->query($sql, [$resp, $rpt_url]);
|
|
if (!$query) {
|
|
$message = $this->db_onedev->error();
|
|
$message['qry'] = $this->db_onedev->last_query();
|
|
$this->sys_error([
|
|
"msg" => "Error change JSONQontak when upload file",
|
|
"error" => $message
|
|
]);
|
|
$this->db_onedev->trans_rollback();
|
|
exit;
|
|
}
|
|
$this->db_onedev->trans_complete();
|
|
$this->sys_error($resp);
|
|
exit;
|
|
}
|
|
|
|
$boundary = uniqid();
|
|
|
|
$body = "--$boundary\r\n" .
|
|
"Content-Disposition: form-data; name=\"file\"; filename=\"$fileName\"\r\n" .
|
|
"Content-Type: $mimeType\r\n\r\n" .
|
|
$fileContents . "\r\n" .
|
|
"--$boundary--\r\n";
|
|
|
|
// TODO: Need to be refactor to differentiate between Kwitansi dan Result WA
|
|
$query = "SELECT * FROM x_qontak_api
|
|
WHERE XQontakApiType = 'KWITANSI'
|
|
ORDER BY XQontakApiLastUpdated DESC LIMIT 1";
|
|
$configwa = $this->db_onedev->query($query)->result_array();
|
|
$token = $configwa[0]["XQontakApiToken"];
|
|
|
|
// Set cURL options
|
|
$curl = curl_init();
|
|
curl_setopt_array($curl, [
|
|
CURLOPT_URL => $url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_HTTPHEADER => [
|
|
"Authorization: Bearer {$token}",
|
|
"Content-Type: multipart/form-data; boundary=$boundary"
|
|
],
|
|
CURLOPT_POSTFIELDS => $body
|
|
]);
|
|
|
|
$response = curl_exec($curl);
|
|
$error = curl_error($curl);
|
|
|
|
curl_close($curl);
|
|
$respArray = json_decode($response, true);
|
|
|
|
if ($respArray['status'] == "success") {
|
|
// Check if decoding was successful and access the "url"
|
|
if (isset($respArray['data']['url'])) {
|
|
$url = $respArray['data']['url'];
|
|
|
|
$sql = "UPDATE x_wa_outbox SET
|
|
XWaOutboxCdnUrl = ?,
|
|
XWaOutboxLastUpdated = NOW()
|
|
WHERE
|
|
XWaOutboxID = ? ";
|
|
$query = $this->db_onedev->query($sql, [$url, $this->sys_input["XWaOutboxID"]]);
|
|
if (!$query) {
|
|
$message = json_encode($this->db_onedev->error());
|
|
throw new Exception("Error updating CDN URL: " . $message);
|
|
}
|
|
$this->sys_ok([
|
|
"msg" => "Berhasil upload file dan update CDN",
|
|
"url" => $url
|
|
]);
|
|
} else {
|
|
throw new Exception("URL not found in response.");
|
|
}
|
|
}
|
|
if ($error) {
|
|
$err = json_encode(["status" => "ERR", "message" => $error]);
|
|
throw new Exception("cURL Error: " . $err);
|
|
}
|
|
|
|
$this->db_onedev->trans_complete();
|
|
|
|
} catch (Exception $e) {
|
|
$msg = $e->getMessage();
|
|
$this->db_onedev->trans_rollback();
|
|
$this->sys_error($msg);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// * Send WA Msg Using Qontak
|
|
public function qontakSendMsg(){
|
|
try {
|
|
$url = "https://service-chat.qontak.com/api/open/v1/broadcasts/whatsapp/direct";
|
|
|
|
$query = "SELECT * FROM x_qontak_api
|
|
WHERE XQontakApiType = 'KWITANSI'
|
|
ORDER BY XQontakApiLastUpdated DESC LIMIT 1";
|
|
|
|
$configwa = $this->db_onedev->query($query)->result_array();
|
|
if (!$configwa) {
|
|
$err = json_encode($this->db_onedev->error());
|
|
throw new Exception("Error fetching Qontak API config: " . $err);
|
|
}
|
|
|
|
$token = $configwa[0]["XQontakApiToken"];
|
|
$wa_integration_id = $configwa[0]["XQontakApiWaIntegrationID"];
|
|
$template_id = $configwa[0]["XQontakApiTemplateID"];
|
|
|
|
$prm = $this->sys_input;
|
|
$orderID = $prm["orderID"];
|
|
$orderDate = $prm["orderDate"];
|
|
$patientName = $prm["patientName"];
|
|
$patientHp = $prm["patientHp"];
|
|
if (substr($patientHp, 0, 1) === "0") {
|
|
$patientHp = "62" . substr($patientHp, 1);
|
|
}
|
|
$corpName = $prm["corpName"];
|
|
$fileName = $prm["fileName"];
|
|
$statusOutbox = $prm["statusOutbox"];
|
|
$retryOutbox = $prm["retryOutbox"];
|
|
$outboxID = $prm["sendWaID"];
|
|
|
|
/* Ambil CDN Url */
|
|
$sql = "SELECT XWaOutboxCdnUrl as fileUrl FROM x_wa_outbox WHERE XWaOutboxID = ?";
|
|
|
|
$query = $this->db_onedev->query($sql, [$outboxID]);
|
|
if (!$query) {
|
|
$err = json_encode($this->db_onedev->error());
|
|
throw new Exception("Error fetching CDN URL: " . $err);
|
|
}
|
|
|
|
$sqlPayDate = "SELECT DATE_FORMAT(F_PaymentDate, '%d-%m-%Y') as F_PaymentDate FROM f_payment
|
|
WHERE F_PaymentT_OrderHeaderID = ?
|
|
AND F_PaymentIsActive = 'Y' ORDER BY F_PaymentID DESC LIMIT 1";
|
|
|
|
$queryPayDate = $this->db_onedev->query($sqlPayDate, [$orderID]);
|
|
if (!$queryPayDate) {
|
|
$err = json_encode($this->db_onedev->error());
|
|
throw new Exception("Error fetching payment date: " . $err);
|
|
}
|
|
|
|
$tglBayar = $queryPayDate->row()->F_PaymentDate;
|
|
|
|
$uploaded_url_doc = $query->row_array()['fileUrl'];
|
|
|
|
// Kirim WA
|
|
$param = [
|
|
"to_name" => $patientName,
|
|
"to_number" => $patientHp,
|
|
"message_template_id" => $template_id,
|
|
"channel_integration_id" => $wa_integration_id,
|
|
"language" => [
|
|
"code" => "id"
|
|
],
|
|
"parameters" => [
|
|
"header" => [
|
|
"format" => "DOCUMENT",
|
|
"params" => [
|
|
[
|
|
"key" => "url",
|
|
"value" => $uploaded_url_doc
|
|
],
|
|
[
|
|
"key" => "filename",
|
|
"value" => $fileName
|
|
]
|
|
]
|
|
],
|
|
"body" => [
|
|
[
|
|
"key" => 1,
|
|
"value" => "nama_pasien",
|
|
"value_text" => $patientName
|
|
],
|
|
[
|
|
"key" => 2,
|
|
"value" => "tanggal",
|
|
"value_text" => $tglBayar
|
|
]
|
|
]
|
|
]
|
|
];
|
|
$json_param = json_encode($param);
|
|
|
|
|
|
$curl = curl_init();
|
|
|
|
curl_setopt_array($curl, array(
|
|
CURLOPT_URL => $url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_ENCODING => '',
|
|
CURLOPT_MAXREDIRS => 10,
|
|
CURLOPT_TIMEOUT => 0,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
|
CURLOPT_CUSTOMREQUEST => 'POST',
|
|
CURLOPT_POSTFIELDS => $json_param,
|
|
CURLOPT_HTTPHEADER => array(
|
|
"Authorization: Bearer {$token}",
|
|
"Content-Type: application/json"
|
|
),
|
|
));
|
|
|
|
$response = curl_exec($curl);
|
|
$error = curl_error($curl);
|
|
curl_close($curl);
|
|
|
|
$respArray = json_decode($response, true);
|
|
|
|
if ($respArray['status'] == "success") {
|
|
$sql = "UPDATE x_wa_outbox SET
|
|
XWaOutboxIsSent = 'Y',
|
|
XWaOutboxRetry = 0,
|
|
XWaOutboxSentDate = NOW(),
|
|
XWaOutboxLastUpdated = NOW(),
|
|
XWaOutboxJsonQontak = ?
|
|
WHERE
|
|
XWaOutboxID = ?
|
|
";
|
|
$query = $this->db_onedev->query($sql, [json_encode($respArray), $outboxID]);
|
|
|
|
if (!$query) {
|
|
$message = $this->db_onedev->error();
|
|
$message['qry'] = $this->db_onedev->last_query();
|
|
$this->sys_error([
|
|
"msg" => "Error update outbox",
|
|
"error" => $message
|
|
]);
|
|
exit;
|
|
}
|
|
$this->sys_ok("Berhasil kirim wa dan update outbox");
|
|
exit;
|
|
} else {
|
|
$sql = "UPDATE x_wa_outbox SET
|
|
XWaOutboxIsSent = 'E',
|
|
XWaOutboxRetry = ?,
|
|
XWaOutboxSentDate = NOW(),
|
|
XWaOutboxLastUpdated = NOW(),
|
|
XWaOutboxJsonQontak = ?
|
|
WHERE
|
|
XWaOutboxID = ?
|
|
";
|
|
$query = $this->db_onedev->query($sql, [$retryOutbox, json_encode($respArray), $outboxID]);
|
|
if (!$query) {
|
|
$message = $this->db_onedev->error();
|
|
$message['qry'] = $this->db_onedev->last_query();
|
|
$this->sys_error([
|
|
"msg" => "Error update outbox",
|
|
"error" => $message
|
|
]);
|
|
exit;
|
|
}
|
|
$this->sys_error($respArray);
|
|
}
|
|
}catch (Exception $e) {
|
|
$msg = $e->getMessage();
|
|
// $this->db_onedev->trans_rollback(); // tidak perlu transaction karena hanya 1 update
|
|
$this->sys_error($msg);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function changeStatusOutbox(){
|
|
try {
|
|
$this->db_onedev->trans_start();
|
|
|
|
$prm = $this->sys_input;
|
|
|
|
$sql = "UPDATE x_wa_outbox
|
|
SET XWaOutboxIsSent = ?,
|
|
XWaOutboxRetry = ?,
|
|
XWaOutboxLastUpdated = NOW()
|
|
WHERE XWaOutboxID = ? ";
|
|
|
|
$query = $this->db_onedev->query($sql, [$prm["toStatus"], $prm["retry"], $prm["XWaOutboxID"]]);
|
|
|
|
if (!$query) {
|
|
$msg = $this->db_onedev->error();
|
|
throw new Exception($msg);
|
|
}
|
|
|
|
$this->db_onedev->trans_complete();
|
|
$this->sys_ok("Berhasil update status outbox");
|
|
} catch (Exceptions $e) {
|
|
$msg = $e->getMessage();
|
|
$this->db_onedev->trans_rollback();
|
|
$this->sys_error($msg);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
/**
|
|
** FUNCTIONS FITUR KIRIM WA KWITANSI END HERE
|
|
*/
|
|
function getlanguages()
|
|
{
|
|
//# cek token valid
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$query = "SELECT Nat_LangID as id,
|
|
Nat_LangCode as code,
|
|
Nat_LangName as name
|
|
FROM nat_lang WHERE Nat_LangIsActive = 'Y'";
|
|
$rows = $this->db_onedev->query($query)->result_array();
|
|
$this->sys_ok($rows);
|
|
exit;
|
|
}
|
|
|
|
function lookup_type()
|
|
{
|
|
//# cek token valid
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$query = "SELECT M_PaymentTypeID as id,
|
|
M_PaymentTypeCode as code,
|
|
'N' as chex,
|
|
M_PaymentTypeName as chexlabel,
|
|
'Jumlah' as leftlabel,
|
|
'' as selected_card,
|
|
'' as selected_edc,
|
|
'' as selected_account,
|
|
CASE
|
|
WHEN M_PaymentTypeCode = 'CASH' THEN 'Kembali'
|
|
WHEN M_PaymentTypeCode = 'DEBIT' THEN 'Nomor Kartu'
|
|
WHEN M_PaymentTypeCode = 'CREDIT' THEN 'Nomor Kartu'
|
|
WHEN M_PaymentTypeCode = 'TRANSFER' THEN 'No. Rekening'
|
|
ELSE 'Nomor Voucher'
|
|
END as rightlabel,
|
|
0 as leftvalue,
|
|
0 as rightvalue
|
|
FROM m_paymenttype WHERE M_PaymentTypeIsActive = 'Y'";
|
|
$rows = $this->db_onedev->query($query)->result_array();
|
|
foreach($rows as $k => $v){
|
|
$rows[$k]['selected_card'] = array('id'=>0,'name'=>'');
|
|
$rows[$k]['selected_edc'] = array('id'=>0,'name'=>'');
|
|
if($v['chex'] == 'N')
|
|
$rows[$k]['chex'] = false;
|
|
else
|
|
$rows[$k]['chex'] = true;
|
|
}
|
|
$result = array(
|
|
"total" => count($rows) ,
|
|
"records" => $rows,
|
|
);
|
|
$this->sys_ok($result);
|
|
exit;
|
|
}
|
|
|
|
function lookup_banks()
|
|
{
|
|
//# cek token valid
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$query = "SELECT Nat_BankID as id, Nat_BankCode as name
|
|
FROM nat_bank
|
|
WHERE
|
|
Nat_BankIsActive = 'Y'
|
|
ORDER BY Nat_BankCode DESC";
|
|
$rows = $this->db_onedev->query($query)->result_array();
|
|
|
|
$result = array(
|
|
"total" => count($rows) ,
|
|
"records" => $rows,
|
|
);
|
|
$this->sys_ok($result);
|
|
exit;
|
|
}
|
|
|
|
function lookup_accounts()
|
|
{
|
|
//# cek token valid
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$query = "SELECT M_BankAccountID as id, CONCAT(Nat_BankCode,' (',M_BankAccountNo,')') as name
|
|
FROM m_bank_account
|
|
JOIN nat_bank ON M_BankAccountNat_BankID = Nat_BankID
|
|
WHERE
|
|
M_BankAccountIsActive = 'Y'
|
|
ORDER BY Nat_BankCode DESC";
|
|
$rows = $this->db_onedev->query($query)->result_array();
|
|
|
|
$result = array(
|
|
"total" => count($rows) ,
|
|
"records" => $rows,
|
|
);
|
|
$this->sys_ok($result);
|
|
exit;
|
|
}
|
|
|
|
function searchcard(){
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$prm = $this->sys_input;
|
|
|
|
$max_rst = 12;
|
|
$tot_count =0;
|
|
|
|
$q = [
|
|
'search' => '%'
|
|
];
|
|
|
|
if ($prm['search'] != '')
|
|
{
|
|
$q['search'] = "%{$prm['search']}%";
|
|
}
|
|
|
|
// QUERY TOTAL
|
|
if($prm['search'] != ''){
|
|
$sql = "
|
|
SELECT count(*) as total
|
|
FROM nat_bank
|
|
WHERE
|
|
Nat_BankName like ?
|
|
AND Nat_BankIsActive = 'Y'
|
|
ORDER BY Nat_BankName DESC
|
|
";
|
|
}
|
|
else{
|
|
$sql = "
|
|
SELECT count(*) as total
|
|
FROM nat_bank
|
|
WHERE
|
|
Nat_BankIsActive = 'Y'
|
|
ORDER BY Nat_BankName DESC
|
|
";
|
|
}
|
|
$query = $this->db_onedev->query($sql,$q['search']);
|
|
//echo $query;
|
|
if ($query) {
|
|
$tot_count = $query->result_array()[0]["total"];
|
|
}
|
|
else {
|
|
$this->sys_error_db("m_city count",$this->db_onedev);
|
|
exit;
|
|
}
|
|
if($prm['search'] != ''){
|
|
$sql = "
|
|
SELECT Nat_BankID as id, Nat_BankName as name
|
|
FROM nat_bank
|
|
WHERE
|
|
Nat_BankName like ?
|
|
AND Nat_BankIsActive = 'Y'
|
|
ORDER BY Nat_BankName DESC
|
|
";
|
|
}
|
|
else{
|
|
$sql = "
|
|
SELECT Nat_BankID as id, Nat_BankName as name
|
|
FROM nat_bank
|
|
WHERE
|
|
Nat_BankIsActive = 'Y'
|
|
ORDER BY Nat_BankName DESC
|
|
";
|
|
}
|
|
|
|
$query = $this->db_onedev->query($sql, array($q['search']));
|
|
|
|
if ($query) {
|
|
$rows = $query->result_array();
|
|
//echo $this->db_onedev->last_query();
|
|
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows));
|
|
$this->sys_ok($result);
|
|
}
|
|
else {
|
|
$this->sys_error_db("m_city rows",$this->db_onedev);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
function pay()
|
|
{
|
|
//# cek token valid
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
//# ambil parameter input
|
|
$xuserid = $this->sys_user['M_UserID'];
|
|
$prm = $this->sys_input;
|
|
$orderid = $prm['orderid'];
|
|
$payments = $prm['payments'];
|
|
//$xnumber = $this->db_onedev->query("SELECT `fn_numbering`('PAY') as numberx")->row()->numberx;
|
|
$sql = "INSERT INTO f_payment(F_PaymentT_OrderHeaderID,F_PaymentDate,F_PaymentCreated,F_PaymentM_UserID) VALUES (?,CURDATE(),NOW(),?)";
|
|
$query = $this->db_onedev->query($sql,
|
|
array(
|
|
$orderid, $xuserid
|
|
)
|
|
);
|
|
|
|
if (!$query) {
|
|
$this->sys_error_db("f_payment insert");
|
|
exit;
|
|
}
|
|
$headerid = $this->db_onedev->insert_id();
|
|
|
|
foreach($payments as $k => $v){
|
|
if($v['chex']){
|
|
$actual = 0;
|
|
$change = 0;
|
|
$amount = $v['leftvalue'];
|
|
if($v['code'] == 'CASH'){
|
|
$actual = $v['leftvalue'];
|
|
$change = $v['rightvalue'];
|
|
if($actual > 0){
|
|
$amount = intval($v['leftvalue']) - intval($v['rightvalue']);
|
|
}
|
|
else{
|
|
$amount = $actual;
|
|
}
|
|
|
|
$sql = "INSERT INTO f_paymentdetail(
|
|
F_PaymentDetailF_PaymentID,
|
|
F_PaymentDetailM_PaymentTypeID,
|
|
F_PaymentDetailAmount,
|
|
F_PaymentDetailActual,
|
|
F_PaymentDetailChange,
|
|
F_PaymentDetailCreated,
|
|
F_PaymentDetailLastUpdated,
|
|
F_PaymentDetailUserID)
|
|
VALUES (
|
|
?,
|
|
?,
|
|
?,
|
|
?,
|
|
?,
|
|
now(),
|
|
now(),
|
|
?
|
|
)";
|
|
//echo $sql;
|
|
|
|
$query = $this->db_onedev->query($sql,
|
|
array(
|
|
$headerid,
|
|
$v['id'],
|
|
$amount,
|
|
$actual,
|
|
$change,
|
|
$xuserid
|
|
)
|
|
);
|
|
|
|
if (!$query) {
|
|
$this->sys_error_db("f_paymentdetail cash insert");
|
|
exit;
|
|
}
|
|
|
|
}
|
|
else{
|
|
//if(intval($v['leftvalue']) > 0){
|
|
$actual = 0;
|
|
$change = 0;
|
|
$amount = $v['leftvalue'];
|
|
$selected_card = 0;
|
|
$selected_edc = 0;
|
|
$selected_account = 0;
|
|
if($v['code'] == 'DEBIT' || $v['code'] == 'CREDIT' || $v['code'] == 'TRANSFER'){
|
|
$selected_card = $v['selected_card']['id'];
|
|
$selected_edc = $v['selected_edc']['id'];
|
|
if($v['code'] == 'TRANSFER')
|
|
$selected_edc = $v['selected_account']['id'];
|
|
}
|
|
$sql = "INSERT INTO f_paymentdetail(
|
|
F_PaymentDetailF_PaymentID,
|
|
F_PaymentDetailM_PaymentTypeID,
|
|
F_PaymentDetailAmount,
|
|
F_PaymentDetailActual,
|
|
F_PaymentDetailChange,
|
|
F_PaymentDetailCardNat_BankID,
|
|
F_PaymentDetailEDCNat_BankID,
|
|
F_PaymentDetailM_BankAccountID,
|
|
F_PaymentDetailCreated,
|
|
F_PaymentDetailLastUpdated,
|
|
F_PaymentDetailUserID)
|
|
VALUES (
|
|
?,
|
|
?,
|
|
?,
|
|
?,
|
|
?,
|
|
?,
|
|
?,
|
|
?,
|
|
now(),
|
|
now(),
|
|
?
|
|
)";
|
|
//echo $sql;
|
|
|
|
$query = $this->db_onedev->query($sql,
|
|
array(
|
|
$headerid,
|
|
$v['id'],
|
|
$amount,
|
|
$actual,
|
|
$change,
|
|
$selected_card,
|
|
0,
|
|
$selected_edc,
|
|
$xuserid
|
|
)
|
|
);
|
|
//echo $this->db_onedev->last_query();
|
|
if (!$query) {
|
|
$this->sys_error_db("f_paymentdetail non cash insert");
|
|
exit;
|
|
}
|
|
//}
|
|
}
|
|
}
|
|
}
|
|
|
|
$query = "SELECT M_PaymentTypeID as id,
|
|
M_PaymentTypeCode as code,
|
|
IF(M_PaymentTypeCode = 'CASH','Y','N') as chex,
|
|
M_PaymentTypeName as chexlabel,
|
|
'Jumlah' as leftlabel,
|
|
CASE
|
|
WHEN M_PaymentTypeCode = 'CASH' THEN 'Kembali'
|
|
WHEN M_PaymentTypeCode = 'DEBIT' THEN 'Nomor Kartu'
|
|
WHEN M_PaymentTypeCode = 'CREDIT' THEN 'Nomor Kartu'
|
|
WHEN M_PaymentTypeCode = 'TRANSFER' THEN 'Nomor Rekening'
|
|
ELSE 'Nomor Voucher'
|
|
END as rightlabel,
|
|
0 as leftvalue,
|
|
0 as rightvalue
|
|
FROM m_paymenttype WHERE M_PaymentTypeIsActive = 'Y'";
|
|
$rows = $this->db_onedev->query($query)->result_array();
|
|
|
|
foreach($rows as $k => $v){
|
|
if($v['chex'] == 'N')
|
|
$rows[$k]['chex'] = false;
|
|
else
|
|
$rows[$k]['chex'] = true;
|
|
}
|
|
$xdata = $this->db_onedev->query("SELECT F_PaymentID as idx, F_PaymentNumber as numberx FROM f_payment WHERE F_PaymentID = {$headerid}")->row();
|
|
|
|
$sql = "SELECT SUM(F_PaymentTotal) as total_payment,
|
|
T_OrderHeaderTotal as total
|
|
FROM f_payment
|
|
JOIN t_orderheader ON F_PaymentT_OrderHeaderID = T_OrderHeaderID
|
|
WHERE
|
|
F_PaymentT_OrderHeaderID = ? AND
|
|
F_PaymentIsActive = 'Y'";
|
|
$row_payment = $this->db_onedev->query($sql,array($orderid))->row_array();
|
|
|
|
$lunas = "N";
|
|
$paid = $row_payment['total_payment'];
|
|
$unpaid = intval($row_payment['total']) - intval($row_payment['total_payment']);
|
|
if(intval($row_payment['total_payment']) >= intval($row_payment['total']))
|
|
$lunas = "Y";
|
|
|
|
$last_payment_status = [];
|
|
$sql = "SELECT *
|
|
FROM last_statuspayment
|
|
WHERE
|
|
Last_StatusPaymentT_OrderHeaderID = ? AND Last_StatusPaymentIsActive = 'Y'";
|
|
$last_payment_status = $this->db_onedev->query($sql,array($orderid))->row_array();
|
|
if(count($last_payment_status) > 0){
|
|
$sql = "UPDATE last_statuspayment SET Last_StatusPaymentPaid = ?, Last_StatusPaymentUnpaid = ? Last_StatusPaymentIsLunas = ? WHERE Last_StatusPaymentID = ?";
|
|
$this->db_onedev->query($sql,array($paid,$unpaid,$lunas,$last_payment_status['Last_StatusPaymentID']));
|
|
}else{
|
|
$sql = "INSERT INTO last_statuspayment (
|
|
Last_StatusPaymentT_OrderHeaderID,
|
|
Last_StatusPaymentBillTotal,
|
|
Last_StatusPaymentPaid,
|
|
Last_StatusPaymentUnpaid,
|
|
Last_StatusPaymentIsLunas,
|
|
Last_StatusPaymentCreated,
|
|
Last_StatusPaymentUserID
|
|
)
|
|
VALUES(?,?,?,?,?,NOW(),?)";
|
|
$this->db_onedev->query($sql,array(
|
|
$orderid,
|
|
$row_payment['total'],
|
|
$paid,
|
|
$unpaid,
|
|
$lunas,
|
|
$xuserid
|
|
));
|
|
}
|
|
|
|
|
|
$result = array(
|
|
"total" => count($rows) ,
|
|
"records" => array('payments'=>$payments,'types'=>$rows,'data'=>$xdata)
|
|
);
|
|
$this->sys_ok($result);
|
|
exit;
|
|
}
|
|
|
|
function delete_note()
|
|
{
|
|
//# cek token valid
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
//# ambil parameter input
|
|
$xuserid = $this->sys_user['M_UserID'];
|
|
$prm = $this->sys_input;
|
|
$prmnota = $prm['nota'];
|
|
$catatan = $prm['catatan'];
|
|
$sql = "UPDATE f_payment SET F_PaymentIsActive = 'N', F_PaymentNote = '{$catatan}' WHERE F_PaymentID = {$prmnota['note_id']}";
|
|
//echo $sql;
|
|
$query = $this->db_onedev->query($sql);
|
|
if (!$query) {
|
|
$this->sys_error_db("f_payment delete");
|
|
exit;
|
|
}
|
|
|
|
$sql = "UPDATE f_paymentdetail SET F_PaymentDetailIsActive = 'N' WHERE F_PaymentDetailF_PaymentID = {$prmnota['note_id']}";
|
|
//echo $sql;
|
|
$query = $this->db_onedev->query($sql);
|
|
if (!$query) {
|
|
$this->sys_error_db("f_paymentdetail delete");
|
|
exit;
|
|
}
|
|
|
|
$result = array(
|
|
"total" => 1 ,
|
|
"records" => array('prm'=>$prm)
|
|
);
|
|
$this->sys_ok($result);
|
|
exit;
|
|
}
|
|
|
|
function getLocations()
|
|
{
|
|
$prm = $this->sys_input;
|
|
$station_location = [];
|
|
$locations = [];
|
|
$sql = "SELECT T_OrderDetailT_OrderHeaderID as order_id, T_SampleStationID as station_id, T_SampleStationName as station_name,
|
|
fn_get_location(T_SampleStationID,T_OrderDetailT_OrderHeaderID) as location_id, '' locations
|
|
FROM (
|
|
SELECT distinct T_OrderDetailT_OrderHeaderID,T_SampleStationID, T_SampleStationName
|
|
FROM t_orderdetail
|
|
JOIN t_test ON T_OrderDetailT_TestID = T_TestID
|
|
JOIN t_sampletype ON T_SampleTypeID = T_TestT_SampleTypeID
|
|
JOIN t_bahan ON T_SampleTypeT_BahanID = T_BahanID
|
|
JOIN t_samplestation ON T_BahanT_SampleStationID = T_SampleStationID
|
|
|
|
WHERE
|
|
T_OrderDetailT_OrderHeaderID = ? AND T_OrderDetailIsActive = 'Y'
|
|
|
|
) x";
|
|
$query = $this->db_onedev->query($sql,array($prm['order_id']));
|
|
//echo $this->db_onedev->last_query();
|
|
if ($query)
|
|
{
|
|
$datas = $query->result_array();
|
|
foreach ($datas as $key => $value) {
|
|
$sql = "SELECT M_LocationID as location_id, M_LocationName as location_name FROM m_location WHERE M_LocationT_SampleStationID = ? AND M_LocationIsActive = 'Y' ";
|
|
$query = $this->db_onedev->query($sql,array($value['station_id']));
|
|
if ($query)
|
|
{
|
|
$datas[$key]['locations'] = $query->result_array();
|
|
}
|
|
else
|
|
{
|
|
$datas[$key]['locations'] = [];
|
|
}
|
|
}
|
|
$this->sys_ok(["datas"=> $datas]);
|
|
|
|
}
|
|
else{
|
|
echo $this->db_onedev->last_query();
|
|
$this->sys_error_db("gagal ambil data", $this->db_onedev);
|
|
exit;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
function save_control()
|
|
{
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$prm = $this->sys_input;
|
|
$userid = $this->sys_user['M_UserID'];
|
|
if($prm['data'] && count($prm['data']) > 0){
|
|
foreach ($prm['data'] as $key => $value) {
|
|
$sql="INSERT INTO t_order_location (
|
|
T_OrderLocationT_OrderHeaderID,
|
|
T_OrderLocationM_LocationID,
|
|
T_OrderLocationT_SampleStationID,
|
|
T_OrderLocationCreated,
|
|
T_OrderLocationLastUpdated,
|
|
T_OrderLocationUserID
|
|
)
|
|
VALUES (?,?,?,NOW(),NOW(),?)
|
|
ON DUPLICATE KEY
|
|
UPDATE T_OrderLocationT_OrderHeaderID = ?,
|
|
T_OrderLocationM_LocationID = ?,
|
|
T_OrderLocationT_SampleStationID = ?,
|
|
T_OrderLocationLastUpdated = NOW(),
|
|
T_OrderLocationUserID = ?";
|
|
$query = $this->db_onedev->query($sql,array($value['order_id'],$value['location_id'],$value['station_id'],$userid,$value['order_id'],$value['location_id'],$value['station_id'],$userid));
|
|
}
|
|
$this->sys_ok(["datas"=> '']);
|
|
}
|
|
else{
|
|
$this->sys_error_db("data not valid", $this->db_onedev);
|
|
exit;
|
|
}
|
|
//echo $sql;
|
|
}
|
|
}
|