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

493 lines
15 KiB
PHP

<?php
class Unit extends MY_Controller
{
public function index()
{
echo "UNIT API";
}
public function __construct()
{
parent::__construct();
}
public function lookup()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$search = $prm['search'];
$number_limit = 10;
$number_offset = ($prm['current_page'] - 1) * $number_limit ;
$sql = "select COUNT(*) as total
from nat_unit
where
( Nat_UnitName LIKE CONCAT('%','{$search}','%')
)AND
Nat_UnitIsActive = 'Y'";
$sql_param = array($search);
$query = $this->db_onedev->query($sql);
$tot_count = 0;
$tot_page = 0;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
$tot_page = ceil($tot_count/$number_limit);
} else {
$this->sys_error_db("nat_unit count", $this->db_onedev);
exit;
}
$sql = "select Nat_UnitID as id,
Nat_UnitCode as code,
Nat_UnitName as name,
Nat_UnitName as description,
'N' as show_detail,
'' as unitlangs
from nat_unit
where
( Nat_UnitName LIKE CONCAT('%','{$search}','%')
)AND
Nat_UnitIsActive = 'Y'
order by Nat_UnitCode
limit $number_limit offset $number_offset";
$sql_param = array($search);
$query = $this->db->query($sql);
//echo $this->db->last_query();
if ($query) {
$rows = $query->result_array();
if($rows){
if($v['show_detail'] === 'Y'){
foreach($rows as $k => $v){
$rows[$k]['unitlangs'] = $this->add_unitlang($v['id']);
}
}
}
} else {
$this->sys_error_db("nat_unit select");
exit;
}
$result = array ("total" => $tot_page, "total_filter"=>count($rows),"records" => $rows);
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function addnewunit()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$name_unit = $prm['name'];
$code = $prm['code'];
//cek duplicate
$sql = "select count(*) as tot
from nat_unit
where Nat_UnitIsActive = 'Y'
and ( Nat_UnitCode = ? or Nat_UnitName = ? )";
$qry = $this->db->query($sql, array($code,$name_unit));
if ($qry) {
$rows = $qry->result_array();
if (count($rows) > 0 && $rows[0]["tot"] > 0 ) {
$this->sys_error_db("Duplicate Kode or Name");
exit;
}
}
$sqlunit = "insert into nat_unit(
Nat_UnitCode,
Nat_UnitName,
Nat_UnitCreated,
Nat_UnitLastUpdated
)
values( ?, ?, now(), now())";
$queryunit = $this->db->query($sqlunit,
array(
$code,
$name_unit
)
);
//echo $query;
if (!$queryunit) {
$this->sys_error_db("nat_unit insert");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function editunit()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$id_unit = $prm['id'];
$name_unit = $prm['name'];
$code = $prm['code'];
//cek duplicate
$sql = "select count(*) as tot
from nat_unit
where Nat_UnitIsActive = 'Y'
and ( Nat_UnitCode = ? or Nat_UnitName = ? )
and Nat_UnitID <> ?";
$qry = $this->db->query($sql, array($code,$name_unit,$id_unit));
if ($qry) {
$rows = $qry->result_array();
if (count($rows) > 0 && $rows[0]["tot"] > 0 ) {
$this->sys_error_db("Duplicate Kode or Name");
exit;
}
}
$sqlunit = "update nat_unit SET
Nat_UnitCode = ?,
Nat_UnitName = ?,
Nat_UnitLastUpdated = now()
where
Nat_UnitID = ?
";
$queryunit = $this->db->query($sqlunit,
array($code,
$name_unit,
$id_unit
)
);
//echo $query;
if (!$queryunit) {
$this->sys_error_db("nat_unit update");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => $id_unit));
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function deleteunit()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$sql = "update nat_unit SET
Nat_UnitIsActive = 'N',
Nat_UnitLastUpdated = now()
WHERE
Nat_UnitID = ?
";
$query = $this->db->query($sql,
array(
$prm['id']
)
);
// echo $query;
if (!$query) {
$this->sys_error_db("nat_unit delete");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function lookupunitlang(){
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$id = $prm['id'];
$sql = "select Nat_UnitLangID as id,
Nat_UnitLangNat_UnitID as unitid,
Nat_UnitName as unitname,
Nat_UnitLangName as name,
Nat_UnitLangConversion as konversi,
Nat_UnitLangIsSI as issi,
Nat_LangName as langname,
IF(Nat_UnitLangIsSI = 'Y','Sebagai SI','Tidak Sebagai SI') as si,
'xxx' as action,
'Y' as show_detail
from nat_lang
left join nat_unitlang ON Nat_LangID = Nat_UnitLangNat_LangID AND Nat_UnitLangNat_UnitID = {$id}
left join nat_unit ON Nat_UnitLangNat_UnitID = Nat_UnitID
where
Nat_LangIsActive = 'Y'";
//echo $sql;
$rows = $this->db->query($sql)->result();
$result = array ("total" => count($rows), "records" => $rows);
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function lookupunitlangx()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$search = $prm['search'];
$all = $prm['all'];
$id = $prm['id'];
$number_limit = 10;
$number_offset = ($prm['current_page'] - 1) * $number_limit ;
$sql = "select COUNT(*) as total
from nat_unit
where
( Nat_UnitName LIKE CONCAT('%','{$search}','%')
)AND
Nat_UnitIsActive = 'Y'";
$sql_param = array($search);
$query = $this->db_onedev->query($sql);
$tot_count = 0;
$tot_page = 0;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
$tot_page = ceil($tot_count/$number_limit);
} else {
$this->sys_error_db("nat_unit count", $this->db_onedev);
exit;
}
$sql = "select Nat_UnitID as id,
Nat_UnitName as name,
Nat_UnitCode as code,
Nat_UnitName as description,
IF(Nat_UnitID = '{$id}','Y','N') as show_detail,
'' as unitlangs
from nat_unit
where
( Nat_UnitName LIKE CONCAT('%','{$search}','%')
)AND
Nat_UnitIsActive = 'Y'
limit $number_limit offset $number_offset";
$sql_param = array($search);
$query = $this->db->query($sql);
//echo $this->db->last_query();
if ($query) {
$rows = $query->result_array();
if($rows){
foreach($rows as $k => $v){
$rows[$k]['unitlangs'] = $this->add_unitlang($id);
}
}
} else {
$this->sys_error_db("nat_unit select");
exit;
}
$result = array ("total" => $tot_page, "total_filter"=>count($rows),"records" => $rows);
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function add_unitlang($unitid){
$query ="select IFNULL(Nat_UnitLangID,0) as id,
IFNULL(Nat_UnitLangNat_UnitID,$unitid) as unitid,
IFNULL(Nat_UnitLangName,'') as name,
IFNULL(Nat_UnitLangConversion, '') as konversi,
IFNULL(Nat_UnitLangSiName, '') as siname,
IFNULL(Nat_UnitLangNat_LangID, Nat_LangID) as langid,
IFNULL(Nat_LangName,'') as langname,
'xxx' as action,
'Y' as show_detail,
Nat_LangID as idx,
Nat_LangID
from nat_lang
left join nat_unitlang ON Nat_LangID = Nat_UnitLangNat_LangID AND Nat_UnitLangNat_UnitID = {$unitid}
left join nat_unit ON Nat_UnitLangNat_UnitID = Nat_UnitID
where
Nat_LangIsActive = 'Y'";
// echo $query;
$rows = $this->db->query($query)->result_array();
if(!$rows)
$rows = array();
return $rows;
}
public function lookupunitlanghidex()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$search = $prm['search'];
$all = $prm['all'];
$id = $prm['id'];
$number_limit = 10;
$number_offset = ($prm['current_page'] - 1) * $number_limit ;
$sql = "select COUNT(*) as total
from nat_unit
where
( Nat_UnitName LIKE CONCAT('%','{$search}','%')
)AND
Nat_UnitIsActive = 'Y'";
$sql_param = array($search);
$query = $this->db_onedev->query($sql);
$tot_count = 0;
$tot_page = 0;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
$tot_page = ceil($tot_count/$number_limit);
} else {
$this->sys_error_db("nat_unit count", $this->db_onedev);
exit;
}
$sql = "select Nat_UnitID as id,
Nat_UnitName as name,
Nat_UnitCode as code,
Nat_UnitName as description,
'N' as show_detail,
'' as unitlangs
from nat_unit
where
( Nat_UnitName LIKE CONCAT('%','{$search}','%')
)AND
Nat_UnitIsActive = 'Y'
limit $number_limit offset $number_offset";
$sql_param = array($search);
$query = $this->db->query($sql);
//echo $this->db->last_query();
if ($query) {
$rows = $query->result_array();
if($rows){
foreach($rows as $k => $v){
$rows[$k]['unitlangs'] = $this->add_unitlang($id);
}
}
} else {
$this->sys_error_db("nat_unit select");
exit;
}
$result = array ("total" => $tot_page, "total_filter"=>count($rows),"records" => $rows);
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function saveunitlang(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$unit_id = $prm['unitid'];
$userid = $this->sys_user["M_UserID"];
foreach($prm['unitlangs'] as $k=>$v){
if($v['id'] == 0 || $v['id'] == '0'){
$query = "INSERT INTO nat_unitlang (
Nat_UnitLangNat_UnitID,
Nat_UnitLangNat_LangID,
Nat_UnitLangName,
Nat_UnitLangSiName,
Nat_UnitLangConversion,
Nat_UnitLangUserID,
Nat_UnitLangCreated,
Nat_UnitLangLastUpdated
)
VALUE(
?,?,?,?,?,?,now(),now()
)";
$insert_new_unitlang = $this->db->query($query,array(
$unit_id,
$v['langid'],
$v['name'],
$v['siname'],
$v['konversi'],
$userid
));
} else {
$query = "UPDATE nat_unitlang SET
Nat_UnitLangNat_UnitID = ?,
Nat_UnitLangNat_LangID = ?,
Nat_UnitLangName = ?,
Nat_UnitLangSiName = ?,
Nat_UnitLangConversion = ?,
Nat_UnitLangUserID = ?
WHERE
Nat_UnitLangID = ?";
$update_unitlang = $this->db->query($query,array($unit_id,$v['langid'],$v['name'],$v['siname'],$v['konversi'],$userid,$v['id']));
}
}
$result = array(
"total" => 1 ,
"records" => array('status'=>'OK')
);
$this->sys_ok($result);
exit;
}
}