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

792 lines
25 KiB
PHP

<?php
class Branch extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "BRANCH API";
}
public function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("onedev", true);
}
function lookupbranchbyname(){
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$regional = $prm['regional'];
$branch = $prm['branch'];;
$all = $prm['all'];
$limit = '';
if($all == 'N'){
$limit = ' LIMIT 10';
}
$number_limit = 10;
$number_offset = ($prm['current_page'] - 1) * $number_limit ;
$sql = "select COUNT(*) as total
FROM(SELECT *
from m_branch
JOIN s_regional ON M_BranchS_RegionalID = S_RegionalID AND S_RegionalIsActive = 'Y'
WHERE
S_RegionalName LIKE CONCAT('%','{$regional}','%') AND
M_BranchName LIKE CONCAT('%','{$branch}','%') AND
M_BranchIsActive = 'Y' GROUP BY M_BranchID) a";
// $total = $this->db_onedev->query($sql,$sql_param)->row()->total;
$query = $this->db_onedev->query($sql);
//echo $this->db_onedev->last_query();
$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("m_branch count", $this->db_onedev);
exit;
}
$sql = "select M_BranchID as id,
M_BranchName as name,
M_BranchCode as code,
CONCAT(M_BranchAddress, ', Telp: ',IF(M_BranchPhone = '','-',M_BranchPhone),', Kota: ', M_CityName, ' ,Kecamatan: ', M_DistrictName, ' ,Kelurahan: ', M_KelurahanName) as address,
S_RegionalID,
S_RegionalName,
M_CityID,
M_CityName,
M_DistrictID,
M_DistrictName,
M_KelurahanID,
M_KelurahanName,
IF(M_BranchIsDefault = 'N','Tidak','Ya') as isdefault,
m_branch.*
from m_branch
JOIN s_regional ON M_BranchS_RegionalID = S_RegionalID AND S_RegionalIsActive = 'Y'
LEFT JOIN m_city ON M_BranchM_CityID = M_CityID
LEFT JOIN m_district ON M_BranchM_DistrictID = M_DistrictID
LEFT JOIN m_kelurahan ON M_BranchM_KelurahanID = M_KelurahanID
WHERE
S_RegionalName LIKE CONCAT('%','{$regional}','%') AND
M_BranchName LIKE CONCAT('%','{$branch}','%') AND
M_BranchIsActive = 'Y'
GROUP BY M_BranchID
ORDER BY S_RegionalName ASC, M_BranchName ASC
limit $number_limit offset $number_offset";
$sql_param = array($search);
$query = $this->db_onedev->query($sql);
//echo $this->db_onedev->last_query();
if ($query) {
$rows = $query->result_array();
} else {
$this->sys_error_db("m_branch 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 lookupregionalbyname()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$regional = $prm['regional'];
$branch = $prm['branch'];
$limit = '';
if($all == 'N'){
$limit = ' LIMIT 10';
}
$number_limit = 10;
$number_offset = ($prm['current_page'] - 1) * $number_limit ;
$sql = "select COUNT(*) as total
FROM(SELECT *
from s_regional
LEFT JOIN m_branch ON S_RegionalID = M_BranchS_RegionalID AND M_BranchIsActive = 'Y'
where
S_RegionalName LIKE CONCAT('%','{$regional}','%') AND
IFNULL(M_BranchName,'') LIKE CONCAT('%','{$branch}','%') AND
S_RegionalIsActive = 'Y'
GROUP BY S_RegionalID) a";
$sql_param = array($search);
// $total = $this->db_onedev->query($sql,$sql_param)->row()->total;
$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("s_regional count", $this->db_onedev);
exit;
}
$sql = "select S_RegionalID as id,
S_RegionalName as name,
S_RegionalName as namex,
s_regional.*
from s_regional
LEFT JOIN m_branch ON S_RegionalID = M_BranchS_RegionalID AND M_BranchIsActive = 'Y'
where
S_RegionalName LIKE CONCAT('%','{$regional}','%') AND
IFNULL(M_BranchName,'') LIKE CONCAT('%','{$branch}','%') AND
S_RegionalIsActive = 'Y'
GROUP BY S_RegionalID
ORDER BY S_RegionalName ASC
limit $number_limit offset $number_offset";
$sql_param = array($search);
$query = $this->db_onedev->query($sql);
//echo $this->db_onedev->last_query();
if ($query) {
$rows = $query->result_array();
} else {
$this->sys_error_db("s_regional 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 addnewregional()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$name = $prm['name'];
$sql = "insert into s_regional(
S_RegionalName,
S_RegionalCreated,
S_RegionalLastUpdated
)
values( ?, now(), now())";
$query = $this->db_onedev->query($sql,
array(
$name
)
);
//echo $query;
if (!$query) {
$this->sys_error_db("s_regional insert");
exit;
}
$last_id = $this->db_onedev->insert_id();
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
$last_id = $this->db_onedev->insert_id();
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function editregional()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$id = $prm['id'];
$name = $prm['name'];
$userid = $this->sys_user["M_UserID"];
$sqlcompany = "update s_regional SET
S_RegionalName = ?,
S_RegionalLastUpdated = now()
where
S_RegionalID = ?
";
$querycompany = $this->db_onedev->query($sqlcompany,
array(
$name,
$id
)
);
// echo $query;
if (!$querycompany) {
$this->sys_error_db("s_regional update");
exit;
}
$result = array ("total" => 1, "records" => array("xid" => $id));
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function addnewbranch()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$regionalid = $prm['regionalid'];
$code = $prm['code'];
$name = $prm['name'];
$address = $prm['address'];
$phone = $prm['phone'];
$cityid = $prm['cityid'];
$districtid = $prm['districtid'];
$kelurahanid = $prm['kelurahanid'];
$ipadrress = $prm['ipaddress'];
$basename = $prm['basename'];
$isdefault = $prm['isdefault'];
$userid = $this->sys_user["M_UserID"];
if($regionalid == 0 || $cityid == 0 || $districtid == 0 || $kelurahanid == 0){
$errors = array();
if($regionalid == 0){
array_push($errors,array('field'=>'regional','msg'=>'Regional dipilih dulu dong'));
}
if($cityid == 0){
array_push($errors,array('field'=>'city','msg'=>'Kota dipilih dulu dong'));
}
if($districtid == 0){
array_push($errors,array('field'=>'district','msg'=>'Kecamatan dipilih dulu dong'));
}
if($kelurahanid == 0){
array_push($errors,array('field'=>'kelurahan','msg'=>'Kelurahan dipilih dulu dong'));
}
$result = array ("total" => -1,"errors" => $errors, "records" => 0);
$this->sys_ok($result);
}else{
if($prm['xid'] == 0){
$query = "SELECT COUNT(*) as exist FROM m_branch WHERE M_BranchIsActive = 'Y' AND M_BranchCode = '{$code}'";
$exist_code = $this->db_onedev->query($query)->row()->exist;
if($exist_code == 0){
$sql = "insert into m_branch(
M_BranchS_RegionalID,
M_BranchCode,
M_BranchName,
M_BranchAddress,
M_BranchPhone,
M_BranchM_CityID,
M_BranchM_DistrictID,
M_BranchM_KelurahanID,
M_BranchIPAddress,
M_BranchBaseName,
M_BranchIsDefault,
M_BranchUserID,
M_BranchLastUpdated,
M_BranchCreated)
values(?,?,?,?,?,?,?,?,?,?,?,?,now(),now())";
$query = $this->db_onedev->query($sql,
array(
$regionalid,
$code,
$name,
$address,
$phone,
$cityid,
$districtid,
$kelurahanid,
$ipadrress,
$basename,
$isdefault,
$userid
)
);
if (!$query) {
$this->sys_error_db("m_branch insert",$this->db_onedev);
exit;
}
$last_id = $this->db_onedev->insert_id();
if ($isdefault == "Y" ) {
$sql = "update m_branch set M_BranchIsDefault= 'N'
where M_BranchID <> ? ";
$this->db_onedev->query($sql,array($last_id));
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
}else{
$errors = array();
if($exist_code != 0){
array_push($errors,array('field'=>'code','msg'=>'Kode sudah ada yang pakai dong'));
}
$result = array ("total" => -1,"errors" => $errors, "records" => 0);
$this->sys_ok($result);
}
}else{
$query = "SELECT COUNT(*) as exist FROM m_branch WHERE M_BranchIsActive = 'Y' AND M_BranchCode = '{$code}' AND M_BranchID <> {$prm['xid']}";
//echo $query;
$exist_code = $this->db_onedev->query($query)->row()->exist;
// echo $exist_code;
if($exist_code == 0){
$sql = "UPDATE m_branch SET M_BranchS_RegionalID = '{$regionalid}',
M_BranchCode = '{$code}',
M_BranchName = '{$name}',
M_BranchAddress = '{$address}',
M_BranchPhone = '{$phone}',
M_BranchM_CityID = '{$cityid}',
M_BranchM_DistrictID = '{$districtid}',
M_BranchM_KelurahanID = '{$kelurahanid}',
M_BranchIPAddress = '{$ipadrress}',
M_BranchBaseName = '{$basename}',
M_BranchIsDefault = '{$isdefault}',
M_BranchUserID = '{$userid}'
WHERE M_BranchID = '{$prm['xid']}'";
//echo $sql;
$query = $this->db_onedev->query($sql);
if ($isdefault == "Y" ) {
$sql = "update m_branch set M_BranchIsDefault= 'N'
where M_BranchID <> ? ";
$this->db_onedev->query($sql,array($prm['xid']));
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
}else{
$errors = array();
if($exist_code != 0){
array_push($errors,array('field'=>'code','msg'=>'Kode sudah ada yang pakai dong'));
}
$result = array ("total" => -1,"errors" => $errors, "records" => 0);
$this->sys_ok($result);
}
}
}
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
public function deleteregional()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$userid = $this->sys_user["M_UserID"];
$sql = "update s_regional SET
S_RegionalIsActive = 'N',
S_RegionalLastUpdated = now()
WHERE
S_RegionalID = ?
";
$query = $this->db_onedev->query($sql,
array(
$prm['id']
)
);
// echo $query;
if (!$query) {
$this->sys_error_db("s_regional delete");
exit;
}
$sql = "update m_branch SET
M_BranchIsActive = 'N',
M_BranchLastUpdated = now()
WHERE
M_BranchS_RegionalID = ?
";
$query = $this->db_onedev->query($sql,
array(
$prm['id']
)
);
// echo $query;
if (!$query) {
$this->sys_error_db("m_branch delete");
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 deletebranch()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$userid = $this->sys_user["M_UserID"];
$sql = "update m_branch SET
M_BranchIsActive = 'N',
M_BranchLastUpdated = now()
WHERE
M_BranchID = ?
";
$query = $this->db_onedev->query($sql,
array(
$prm['id']
)
);
// echo $query;
if (!$query) {
$this->sys_error_db("m_branch 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 searchregional(){
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
$sql = "SELECT count(*) as total
FROM s_regional
WHERE
S_RegionalName like ?
AND S_RegionalIsActive = 'Y'";
$query = $this->db_onedev->query($sql,$q['search']);
//echo $query;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("s_regional count",$this->db_onedev);
exit;
}
$sql = "
SELECT S_RegionalID, S_RegionalName
FROM s_regional
WHERE
S_RegionalName like ?
AND S_RegionalIsActive = 'Y'
ORDER BY S_RegionalName ASC
";
$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("s_regional rows",$this->db_onedev);
exit;
}
}
function searchregionalbyname(){
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
$sql = "SELECT count(*) as total
FROM s_regional
WHERE
S_RegionalName like ?
AND S_RegionalIsActive = 'Y'";
$query = $this->db_onedev->query($sql,$q['search']);
//echo $query;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("s_regional count",$this->db_onedev);
exit;
}
$sql = "
SELECT S_RegionalID, S_RegionalName
FROM s_regional
WHERE
S_RegionalName like ?
AND S_RegionalIsActive = 'Y'
ORDER BY S_RegionalName ASC
";
$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("s_regional rows",$this->db_onedev);
exit;
}
}
function searchcity(){
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
$sql = "SELECT count(*) as total
FROM m_city
WHERE
M_CityName like ?
AND M_CityIsActive = 'Y'";
$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;
}
$sql = "
SELECT *
FROM m_city
WHERE
M_CityName like ?
AND M_CityIsActive = 'Y'
ORDER BY M_CityName ASC
";
$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 searchdistrict(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$id = $prm['id'];
$max_rst = 12;
$tot_count =0;
$q = [
'search' => '%'
];
if ($prm['search'] != '')
{
$q['search'] = "%{$prm['search']}%";
}
// QUERY TOTAL
$sql = "SELECT count(*) as total
FROM m_district
WHERE
M_DistrictName like ?
AND M_DistrictM_CityID = '{$id}'
AND M_DistrictIsActive = 'Y'";
$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_district count",$this->db_onedev);
exit;
}
$sql = "
SELECT *
FROM m_district
WHERE
M_DistrictName like ?
AND M_DistrictM_CityID = '{$id}'
AND M_DistrictIsActive = 'Y'
ORDER BY M_DistrictName ASC
";
$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_district rows",$this->db_onedev);
exit;
}
}
function searchkelurahan(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$id = $prm['id'];
$max_rst = 12;
$tot_count =0;
$q = [
'search' => '%'
];
if ($prm['search'] != '')
{
$q['search'] = "%{$prm['search']}%";
}
// QUERY TOTAL
$sql = "SELECT count(*) as total
FROM m_kelurahan
WHERE
M_KelurahanName like ?
AND M_KelurahanM_DistrictID = '{$id}'
AND M_KelurahanIsActive = 'Y'";
$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_district count",$this->db_onedev);
exit;
}
$sql = "
SELECT *
FROM m_kelurahan
WHERE
M_KelurahanName like ?
AND M_KelurahanM_DistrictID = '{$id}'
AND M_KelurahanIsActive = 'Y'
ORDER BY M_KelurahanName ASC
";
$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_district rows",$this->db_onedev);
exit;
}
}
}