Files
BE_CPONE/application/controllers/mockup/masterdata/Price.php
2026-04-27 10:31:17 +07:00

448 lines
14 KiB
PHP

<?php
class Price extends MY_Controller
{
var $db_smartone;
public function index()
{
echo "Price API";
}
public function __construct()
{
parent::__construct();
$this->db_smartone = $this->load->database("onedev", true);
}
public function search_company()
{
$prm = $this->sys_input;
$max_rst = 12;
// QUERY TOTAL
$sql = "select distinct count(*) total
from m_company
join m_mou on M_MouM_CompanyID = M_CompanyID and M_MouIsActive = 'Y'
and M_MouStartDate <= date(now()) and M_MouEndDate >= date(now())
where M_CompanyIsActive = 'Y' and M_CompanyName LIKE ?
order by M_CompanyName ASC";
$query = $this->db_smartone->query($sql, ['%'.$prm['search'].'%']);
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("price company count", $this->db_smartone);
exit;
}
$sql = "select distinct M_CompanyID, M_CompanyName
from m_company
join m_mou on M_MouM_CompanyID = M_CompanyID and M_MouIsActive = 'Y'
and M_MouStartDate <= date(now()) and M_MouEndDate >= date(now())
where M_CompanyIsActive = 'Y' and M_CompanyName LIKE ?
order by M_CompanyName ASC
limit 0, {$max_rst}";
$query = $this->db_smartone->query($sql, ['%'.$prm['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 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 ?
and date(now()) >= M_MouStartDate
and date(now()) <= M_MouEndDate
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, M_MouIsVerified
from m_mou
where M_MouIsActive = 'Y'
and M_MouM_CompanyID = ?
and M_MouName LIKE ?
and date(now()) >= M_MouStartDate
and date(now()) <= M_MouEndDate
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_price()
{
$prm = $this->sys_input;
$max_rst = 10;
$search = '%' . $prm["search"] . '%';
$mou = $prm["mou_id"];
$page = $prm['page'];
if ($page == null)
$page = 1;
$offset = ($page - 1) * $max_rst;
// 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 = ceil($query->result_array()[0]["total"]/$max_rst);
}
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,
M_MouIsVerified
from t_price
join t_test on t_pricet_testid = t_testid
join m_mou on t_pricem_mouid = m_mouid
where T_PriceIsActive = 'Y'
and T_PriceM_MouID = ?
and T_TestName LIKE ?
order by T_TestName ASC
limit {$offset}, {$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_page" => ceil($tot_count/$max_rst), "cur_page" => $page, "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_price()
{
$prm = $this->sys_input;
$id = $prm["id"];
$sql = "update t_price
set t_priceisactive = 'N'
where T_PriceIsActive = 'Y'
and T_PriceID = ?";
$query = $this->db_smartone->query($sql, [$id]);
if ($query)
{
$sql = "update t_priceaddon
set t_priceaddonisactive = 'N'
where T_PriceaddonIsActive = 'Y'
and t_priceaddont_priceid = ?";
$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("price mou rows", $this->db_smartone);
exit;
}
}
public function search_px()
{
$prm = $this->sys_input;
$max_rst = 50;
$search = '%' . $prm["search"] . '%';
// QUERY TOTAL
$sql = "select count(*) total
from t_test
where T_TestIsActive = 'Y'
and T_TestName LIKE ?
and ((T_TestIsPrice = 'Y') OR (T_TestIsPrice = 'N' AND LENGTH(T_TestSasCode) = 8))
order by T_TestName ASC";
$query = $this->db_smartone->query($sql, [$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
from t_test
where T_TestIsActive = 'Y'
and T_TestName LIKE ?
and ((T_TestIsPrice = 'Y') OR (T_TestIsPrice = 'N' AND LENGTH(T_TestSasCode) = 8))
order by T_TestSasCode ASC
limit 0, {$max_rst}";
$query = $this->db_smartone->query($sql, [$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("px rows", $this->db_smartone);
exit;
}
}
public function save_px()
{
$prm = $this->sys_input;
$max_rst = 50;
$data = $prm["data"];
$data_others = $prm["data_others"];
// QUERY
// $sql = "INSERT INTO t_addon(T_AddonName) values('{$name}')";
$query = $this->db_smartone->query("CALL sp_master_price_save('{$data}', '{$data_others}', '0')");
if ($query) {
$result = $query->row();
$this->sys_ok($result);
exit;
}
else {
$this->sys_error_db("addon new", $this->db_smartone);
exit;
}
}
public function search_addon()
{
$prm = $this->sys_input;
$max_rst = 50;
$search = '%' . $prm["search"] . '%';
// QUERY TOTAL
$sql = "select count(*) total
from t_addon
where T_AddonIsActive = 'Y'
and T_AddonName LIKE ?
order by T_AddonName ASC";
$query = $this->db_smartone->query($sql, [$search]);
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("addon count", $this->db_smartone);
exit;
}
$sql = "select T_AddonID, T_AddonName
from t_addon
where T_AddonIsActive = 'Y'
and T_AddonName LIKE ?
order by T_AddonName ASC
limit 0, {$max_rst}";
$query = $this->db_smartone->query($sql, [$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("addon rows", $this->db_smartone);
exit;
}
}
public function save_addon()
{
$prm = $this->sys_input;
$max_rst = 50;
$name = $prm["name"];
// QUERY
// $sql = "INSERT INTO t_addon(T_AddonName) values('{$name}')";
$query = $this->db_smartone->set('T_AddonName', $name)
->insert('t_addon');
if ($query) {
$result = array("inserted_id" => $this->db_smartone->insert_id(), "q" => $this->db_smartone->last_query());
$this->sys_ok($result);
exit;
}
else {
$this->sys_error_db("addon new", $this->db_smartone);
exit;
}
}
public function search_company_2()
{
$prm = $this->sys_input;
$search = '%'.$prm['search'].'%';
$max_rst = 25;
$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) ), ']'), '[]') as mou
from m_company
JOIN m_mou on M_MouM_CompanyID = M_CompanyID and M_MouIsActive = '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" => 1, "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 copy_price()
{
$prm = $this->sys_input;
$sql = "CALL sp_master_price_copy(?, ?, ?, 'N')";
$query = $this->db_smartone->query($sql, [$prm['source_id'], $prm['target_id'], $prm['disc']]);
if ($query)
{
$rows = $query->row;
$this->sys_ok($rows->status);
}
else {
$this->sys_error_db("price copy rows", $this->db_smartone);
exit;
}
}
public function search_single_company()
{
$prm = $this->sys_input;
$sql = "SELECT M_CompanyID, M_CompanyName, CONCAT('[', GROUP_CONCAT(JSON_OBJECT('M_MouID', M_MouID, 'M_MouName', M_MouName, 'M_MouStartDate', M_MouStartDate, 'M_MouEndDate', M_MouEndDate, 'M_MouIsVerified', M_MouIsVerified) SEPARATOR ','), ']') mou
from m_company
join m_mou on M_MouM_CompanyID = M_CompanyID and M_MouIsActive = 'Y'
and M_MouStartDate <= date(now()) and M_MouEndDate >= date(now()) AND M_MouID = ?
where M_CompanyIsActive = 'Y'
GROUP BY M_CompanyID
order by M_CompanyName ASC";
$query = $this->db_smartone->query($sql, [$prm['id']]);
if ($query)
{
$rows = $query->result_array();
$rows[0]['mou'] = json_decode($rows[0]['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;
}
}
}
?>