Files
BE_IBL/application/controllers/mockup/masterdata/Packet.php
2026-04-15 15:24:12 +07:00

614 lines
20 KiB
PHP
Executable File

<?php
class Packet extends MY_Controller
{
var $db_smartone;
public function index()
{
echo "Packet API";
}
public function __construct()
{
parent::__construct();
$this->db_smartone = $this->load->database("onedev", true);
}
public function search_packet($pxs = false)
{
$prm = $this->sys_input;
$max_rst = 12;
$mou = $prm['mou'];
$search = '%'. $prm['search'] . '%';
$type = $prm['type'];
$qtype = ($type == null ? "<> ''" : "= '{$type}'");
// QUERY TOTAL
if ($pxs)
{
$sql = "select count(distinct T_PacketID) total
from t_packet
join t_packetdetail on t_packetdetailt_packetid = t_packetid
and t_packetdetailisactive = 'Y'
join t_test on t_packetdetailt_testid = t_testid
where T_PacketIsActive = 'Y'
and ((T_PacketM_MouID = ? AND ? <> 0) OR ? = 0)
and T_PacketName LIKE ?
and T_PacketType {$qtype}
order by T_PacketName ASC";
}
else
{
$sql = "select count(*) total
from t_packet
where T_PacketIsActive = 'Y'
and ((T_PacketM_MouID = ? AND ? <> 0) OR ? = 0)
and T_PacketName LIKE ?
and T_PacketType {$qtype}
order by T_PacketName ASC";
}
$query = $this->db_smartone->query($sql, [$mou, $mou, $mou, $search]);
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("Packet count", $this->db_smartone);
exit;
}
if ($pxs)
{
$sql = "select T_PacketID, T_PacketName, T_PacketType, T_PacketPrice, T_PacketOriginalPrice,
GROUP_CONCAT(T_TestName SEPARATOR ', ') pxs, T_PacketIsNota
from t_packet
join t_packetdetail on t_packetdetailt_packetid = t_packetid
and t_packetdetailisactive = 'Y'
join t_test on t_packetdetailt_testid = t_testid
where T_PacketIsActive = 'Y'
and ((T_PacketM_MouID = ? AND ? <> 0) OR ? = 0)
and T_PacketName LIKE ?
and T_PacketType {$qtype}
group by T_PacketID
order by T_PacketName ASC
limit 0, {$max_rst}";
}
else
{
$sql = "select T_PacketID, T_PacketName, T_PacketType, T_PacketPrice, T_PacketOriginalPrice, T_PacketIsNota
from t_packet
where T_PacketIsActive = 'Y'
and ((T_PacketM_MouID = ? AND ? <> 0) OR ? = 0)
and T_PacketName LIKE ?
and T_PacketType {$qtype}
order by T_PacketName ASC
limit 0, {$max_rst}";
}
$query = $this->db_smartone->query($sql, [$mou, $mou, $mou, $search]);
if ($query)
{
$rows = $query->result_array();
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows), "q" => $this->db_smartone->last_query(), "type"=>$type);
$this->sys_ok($result);
}
else {
$this->sys_error_db("Packet rows", $this->db_smartone);
exit;
}
}
public function search_company()
{
$prm = $this->sys_input;
$search = '%'.$prm['search'].'%';
$max_rst = 12;
// QUERY TOTAL
$sql = "select count(DISTINCT M_CompanyID) total
from m_company
where M_CompanyIsActive = 'Y'
and M_CompanyName LIKE ?
order by M_CompanyName ASC";
$query = $this->db_smartone->query($sql, [$search]);
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("price company count", $this->db_smartone);
exit;
}
$sql = "select M_CompanyID, M_CompanyName,
IFNULL( concat('[', group_concat( json_object('M_MouID', M_MouID, 'M_MouName', M_MouName, 'M_MouStartDate', M_MouStartDate, 'M_MouEndDate', M_MouEndDate, 'M_MouIsReleased', M_MouIsReleased, 'M_MouIsVerified', M_MouIsVerified) ), ']'), '[]') as mou
from m_company
left join m_mou on M_MouM_CompanyID = M_CompanyID and M_MouIsActive = 'Y'
-- AND M_MouIsReleased = 'Y'
and M_MouStartDate <= date(now()) and M_MouEndDate >= date(now())
where M_CompanyIsActive = 'Y'
and M_CompanyName LIKE ?
group by m_companyid
order by M_CompanyName ASC
limit 0, {$max_rst}";
$query = $this->db_smartone->query($sql, [$search]);
if ($query)
{
$rows = $query->result_array();
foreach ($rows as $k => $v)
$rows[$k]['mou'] = json_decode($v['mou']);
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows), "q" => $this->db_smartone->last_query());
$this->sys_ok($result);
}
else {
$this->sys_error_db("price company rows", $this->db_smartone);
exit;
}
}
public function search_mou()
{
$prm = $this->sys_input;
$max_rst = 12;
$search = '%' . $prm["search"] . '%';
$company = $prm["company_id"];
// QUERY TOTAL
$sql = "select count(*) total
from m_mou
where M_MouIsActive = 'Y'
and M_MouM_CompanyID = ?
and M_MouName LIKE ?
order by M_MouName ASC";
$query = $this->db_smartone->query($sql, [$company, $search]);
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("price mou count", $this->db_smartone);
exit;
}
$sql = "select M_MouID, M_MouName, M_MouStartDate, M_MouEndDate
from m_mou
where M_MouIsActive = 'Y'
and M_MouM_CompanyID = ?
and M_MouName LIKE ?
order by M_MouName ASC
limit 0, {$max_rst}";
$query = $this->db_smartone->query($sql, [$company, $search]);
if ($query)
{
$rows = $query->result_array();
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows), "q" => $this->db_smartone->last_query());
$this->sys_ok($result);
}
else {
$this->sys_error_db("price mou rows", $this->db_smartone);
exit;
}
}
// public function search_packet()
// {
// $prm = $this->sys_input;
// $max_rst = 12;
// $search = '%' . $prm["search"] . '%';
// $mou = $prm["mou_id"];
// // QUERY TOTAL
// $sql = "select count(*) total
// from t_price
// join t_test on t_pricet_testid = t_testid
// where T_PriceIsActive = 'Y'
// and T_PriceM_MouID = ?
// and T_TestName LIKE ?
// order by T_TestName ASC";
// $query = $this->db_smartone->query($sql, [$mou, $search]);
// if ($query) {
// $tot_count = $query->result_array()[0]["total"];
// }
// else {
// $this->sys_error_db("price mou count", $this->db_smartone);
// exit;
// }
// $sql = "select T_PriceID, T_TestID, T_TestCode, T_TestName, T_PriceIsCito,
// T_PriceM_CompanyID,
// T_PriceM_MouID,
// T_PricePriority,
// T_PriceAmount,
// T_PriceDisc,
// T_PriceDiscRp,
// T_PriceSubTotal,
// T_PriceOther,
// T_PriceTotal,
// JSON_OBJECT('T_TestID', T_TestID, 'T_TestCode', T_TestCode, 'T_TestName', T_TestName) px
// from t_price
// join t_test on t_pricet_testid = t_testid
// where T_PriceIsActive = 'Y'
// and T_PriceM_MouID = ?
// and T_TestName LIKE ?
// order by T_TestName ASC
// limit 0, {$max_rst}";
// $query = $this->db_smartone->query($sql, [$mou, $search]);
// if ($query)
// {
// $rows = $query->result_array();
// foreach ($rows as $k => $v)
// {
// $rows[$k]['px'] = json_decode($v['px']);
// $sql = "select t_addonid id, t_addonname name, t_priceaddonamount price
// from t_priceaddon
// join t_addon on t_priceaddont_addonid = t_addonid
// where t_priceaddont_priceid = ?
// and t_priceaddonisactive = 'Y'";
// $query = $this->db_smartone->query($sql, [$v['T_PriceID']]);
// $rows2 = [];
// if ($query)
// {
// $rows2 = $query->result_array();
// }
// $rows[$k]['others'] = $rows2;
// }
// $result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows), "q" => $this->db_smartone->last_query());
// $this->sys_ok($result);
// }
// else {
// $this->sys_error_db("price mou rows", $this->db_smartone);
// exit;
// }
// }
public function del_packet()
{
$prm = $this->sys_input;
$id = $prm["id"];
$sql = "update t_packet
set t_packetisactive = 'N'
where T_PacketIsActive = 'Y'
and T_PacketID = ?";
$query = $this->db_smartone->query($sql, [$id]);
if ($query)
{
$sql = "update t_packetdetail
set t_packetdetailisactive = 'N'
where T_PacketdetailIsActive = 'Y'
and t_packetdetailt_packetid = ?";
$query = $this->db_smartone->query($sql, [$id]);
$result = array("query"=>$this->db_smartone->last_query(), "id"=>$id);
$this->sys_ok($result);
}
else {
$this->sys_error_db("packet delete rows", $this->db_smartone);
exit;
}
}
public function search_px()
{
$prm = $this->sys_input;
$max_rst = 50;
$search = '%' . $prm["search"] . '%';
$packet = $prm['packet_id'];
// QUERY TOTAL
$sql = "select count(*) total
from t_packetdetail
join t_test on T_PacketDetailT_TestID = T_TestID
where T_PacketDetailIsActive = 'Y'
and T_PacketDetailT_PacketID = ?
and T_TestName LIKE ?
order by T_TestName ASC";
$query = $this->db_smartone->query($sql, [$packet, $search]);
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("px count", $this->db_smartone);
exit;
}
$sql = "select T_PacketDetailID, T_TestID, T_TestCode, T_TestName, T_PacketDetailPrice, T_TestNat_TestID,
IFNULL(Helper_NatTestsJson, '[]') nat_tests
from t_packetdetail
join t_test on T_PacketDetailT_TestID = T_TestID
left join helper_nattests on helper_nattestst_testid = t_testid
and helper_natteststype = 'PX'
and helper_nattestsisactive = 'Y'
where T_PacketDetailIsActive = 'Y'
and T_PacketDetailT_PacketID = ?
and T_TestName LIKE ?
order by T_TestSasCode ASC
limit 0, {$max_rst}";
$query = $this->db_smartone->query($sql, [$packet, $search]);
if ($query)
{
$rows = $query->result_array();
foreach ($rows as $k => $v)
$rows[$k]['nat_tests'] = json_decode($v['nat_tests']);
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows), "q" => $this->db_smartone->last_query());
$this->sys_ok($result);
}
else {
$this->sys_error_db("px rows", $this->db_smartone);
exit;
}
}
public function save()
{
$prm = $this->sys_input;
$data = (array) json_decode($prm["data"]);
// QUERY
$sql = "INSERT INTO t_packet(T_PacketM_CompanyID,
T_PacketM_MouID,
T_PacketType,
T_PacketName,
T_PacketSequence,
T_PacketSasCode,
T_PacketIsNota)
values(?, ?, ?, ?, '', '', ?)";
$query = $this->db_smartone->query($sql, [$data['packet_company'], $data['packet_mou'], $data['packet_type'], $data['packet_name'], $data['packet_is_nota']]);
if ($query) {
$result = $this->db_smartone->insert_id();
$this->sys_ok($result);
exit;
}
else {
$this->sys_error_db("addon new", $this->db_smartone);
exit;
}
}
public function save_edit()
{
$prm = $this->sys_input;
$data = (array) json_decode($prm["data"]);
// QUERY
$sql = "UPDATE t_packet SET T_PacketName = ?, T_PacketIsNota = ? WHERE T_PacketID = ?";
$query = $this->db_smartone->query($sql, [$data['packet_name'], $data['packet_is_nota'], $data['packet_id']]);
if ($query) {
$result = $data['packet_id'];
// UPDATE HELPER
$sql = "sp_helper_nattests_create_packet(?)";
$query = $this->db_smartone->query($sql, [$result]);
$this->sys_ok($result);
exit;
}
else {
$this->sys_error_db("edit", $this->db_smartone);
exit;
}
}
public function save_px()
{
$prm = $this->sys_input;
$json_test = $prm['json_px'];
$packet_id = $prm['packet_id'];
$packet_price = $prm['packet_price'];
// QUERY
$sql = "CALL sp_master_packet_px_save(?, ?, ?)";
$query = $this->db_smartone->query($sql, [$packet_id, $packet_price, $json_test])
->row();
if ($query->status == 'OK') {
$result = $query->data;
$this->sys_ok($result);
exit;
}
else {
$this->sys_error_db("Save Px : " . $query->message, $this->db_smartone);
exit;
}
}
public function search_test()
{
$prm = $this->sys_input;
$max_rst = 5000;
$search = '%' . $prm["search"] . '%';
$mou = $prm['mou_id'];
$packet = $prm['packet_id'];
$exclude = $prm['exclude'];
$include = $prm['include'];
$q_exc = $exclude == "" ? "" : "and T_TestID NOT IN ({$exclude})";
$q_inc = $include == "" ? "" : "or (T_TestID IN ({$include}))";
// Packet Type
$pck = $this->db_smartone->select('T_PacketType', false)
->where('T_PacketID', $packet_id)
->get('t_packet')->row();
if ($pck->T_PacketType == 'PN')
{
$moud = $this->db_smartone->select('M_MouID')
->where('M_MouIsDefault', 'Y')
->where('M_MouIsActive', 'Y')
->limit(1)
->get('m_mou')->row();
$mou = $moud->M_MouID;
}
// QUERY TOTAL
$sql = "select count(distinct t_testid) total
from t_test
join t_price on t_pricet_testid = t_testid
and t_pricem_mouid = ? and t_priceisactive = 'Y'
JOIN (
SELECT MAX(CONCAT(T_PriceT_TestID, '-', T_PricePriority)) m
FROM t_price
WHERE T_PriceIsActive = 'Y' AND T_PriceM_MouID = ? AND T_PriceIsCito = 'N'
GROUP BY T_PriceT_TestID, T_PriceIsCito, T_PriceM_MouID
ORDER BY t_pricet_testid
) b ON t_pricet_testid = SUBSTRING_INDEX(m, '-', 1) AND t_pricepriority = SUBSTRING_INDEX(m, '-', -1)
left join t_packetdetail on t_packetdetailisactive = 'Y'
and t_packetdetailt_packetid = ?
and t_packetdetailt_testid = t_testid
where T_TestIsActive = 'Y'
and T_TestName LIKE ?
and (t_packetdetailid is null {$q_inc})
{$q_exc}
order by T_TestName ASC";
$query = $this->db_smartone->query($sql, [$mou, $mou, $packet, $search]);
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("px count", $this->db_smartone);
exit;
}
$sql = "select T_TestID, T_TestCode, T_TestName, T_PriceTotal T_PriceAmount, T_TestNat_TestID,
IFNULL(Helper_NatTestsJson, '[]') nat_tests
from t_test
join t_price on t_pricet_testid = t_testid
and t_pricem_mouid = ? and t_priceisactive = 'Y'
JOIN (
SELECT MAX(CONCAT(T_PriceT_TestID, '-', T_PricePriority)) m
FROM t_price
WHERE T_PriceIsActive = 'Y' AND T_PriceM_MouID = ? AND T_PriceIsCito = 'N'
GROUP BY T_PriceT_TestID, T_PriceIsCito, T_PriceM_MouID
ORDER BY t_pricet_testid
) b ON t_pricet_testid = SUBSTRING_INDEX(m, '-', 1) AND t_pricepriority = SUBSTRING_INDEX(m, '-', -1)
left join t_packetdetail on t_packetdetailisactive = 'Y'
and t_packetdetailt_packetid = ?
and t_packetdetailt_testid = t_testid
left join helper_nattests on helper_nattestst_testid = t_testid
and helper_natteststype = 'PX'
and helper_nattestsisactive = 'Y'
where T_TestIsActive = 'Y'
and T_TestName LIKE ?
and (t_packetdetailid is null {$q_inc})
{$q_exc}
order by T_TestName ASC
limit 0, {$max_rst}";
$query = $this->db_smartone->query($sql, [$mou, $mou, $packet, $search]);
if ($query)
{
$rows = $query->result_array();
foreach ($rows as $k => $v)
$rows[$k]['nat_tests'] = json_decode($v['nat_tests']);
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows), "q" => $this->db_smartone->last_query());
$this->sys_ok($result);
}
else {
$this->sys_error_db("px rows", $this->db_smartone);
exit;
}
}
public function search_mou_default()
{
$prm = $this->sys_input;
$search = '%' . $prm["search"] . '%';
$packet = $prm['packet_id'];
$sql = "select JSON_OBJECT('M_CompanyID', M_CompanyID, 'M_CompanyName', M_CompanyName) company,
JSON_OBJECT('M_MouID', M_MouID, 'M_MouName', M_MouName) mou
from m_mou
join m_company on M_MouM_CompanyID = M_CompanyID and M_CompanyIsDefault = 'Y'
where M_MouIsDefault = 'Y'
and M_MouIsActive = 'Y'
limit 1";
$query = $this->db_smartone->query($sql, [$packet, $search]);
if ($query)
{
$rows = $query->row();
$result = array("company" => json_decode($rows->company), "mou" => json_decode($rows->mou));
$this->sys_ok($result);
}
else {
$this->sys_error_db("MOU Default", $this->db_smartone);
exit;
}
}
public function save_copy() { return $this->packet_copy(); }
public function packet_copy()
{
$prm = $this->sys_input;
$source_id = $prm['source_id'];
$target_id = $prm['target_id'];
// QUERY
$sql = "CALL sp_master_packet_copy(?, ?)";
$query = $this->db_smartone->query($sql, [$source_id, $target_id])
->row();
if ($query->status == 'OK') {
$result = $query->data;
$this->sys_ok(json_decode($result));
exit;
}
else {
$this->sys_error_db("Packet Copy : " . $query->message, $this->db_smartone);
exit;
}
}
}
?>