Files
BE_IBL/application/controllers/inventory/requestreceiveinternal/Setup.php
2026-04-15 15:23:57 +07:00

1198 lines
45 KiB
PHP

<?php
class Setup extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->db_inventory = $this->load->database("inventory", true);
$this->db_inventory_log = $this->load->database('inventory_log', true);
$this->db_onex = 'one_aditya';
}
function search()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$search = "%%";
if (isset($prm['search']) && trim($prm["search"]) != '') {
$search = trim($prm["search"]);
$search = '%' . $prm['search'] . '%';
}
$filter_from_company_address = '';
if (isset($prm['from_company_address']) && intval($prm['from_company_address']) > 0) {
$filter_from_company_address = ' AND MutasiFromCompanyAddressID = '.$prm['from_company_address'];
}
$filter_to_company_address = '';
if (isset($prm['to_company_address']) && intval($prm['to_company_address']) > 0) {
$filter_to_company_address = ' AND MutasiToCompanyAddressID = '.$prm['to_company_address'];
}
$filter_status = '';
if (isset($prm['status']) && $prm['status'] != 'A') {
$filter_status = " AND MutasiStatus = '{$prm['status']}'";
}
$order_by = "MutasiNumber";
if (isset($prm['order_by'])) {
$order_by = trim($prm["order_by"]);
}
$order_type = "asc";
if (isset($prm['order_type'])) {
$order_type = trim($prm["order_type"]);
}
$order = $order_by.' '.$order_type;
$perpage = 10;
$offset = ($prm['current_page'] - 1) * $perpage ;
$sql = "SELECT COUNT(*) as total FROM
(
SELECT MutasiID as id
FROM `mutasi`
JOIN `companyaddress` ON MutasiToCompanyAddressID = CompanyAddressID $filter_company_address
WHERE
MutasiIsActive = 'Y' AND MutasiType = 'T' AND (DATE(MutasiDate) BETWEEN ? AND ?) AND
( MutasiNumber like ? ) $filter_status
) x";
$qry = $this->db_inventory->query($sql,array($prm['start_date'],$prm['end_date'],$search));
$tot_count = 0;
$tot_page = 0;
if ($qry) {
$tot_count = $qry->row()->total;
$tot_page = ceil($tot_count/$perpage);
} else {
//echo $this->db_inventory->last_query();
$this->sys_error_db("purchase order count error", $this->db_inventory->last_query());
exit;
}
$rows = array();
$sql = "SELECT MutasiID as id,
MutasiStatus as status_pengiriman,
MutasiNumber as code,
DATE(MutasiDate) as mutation_date,
MutasiFromCompanyAddressID as from_company_address_id,
fromcompanyaddress.CompanyAddressLabel as from_company_address_name,
fromcompanyaddress.CompanyAddressDescription as from_company_address_description,
MutasiToCompanyAddressID as to_company_address_id,
tocompanyaddress.CompanyAddressLabel as to_company_address_name,
tocompanyaddress.CompanyAddressDescription as to_company_address_description,
tobranch.M_BranchName as to_branch_name,
frombranch.M_BranchName as from_branch_name,
IFNULL(ExpeditionName,'') as expedition_name,
IFNULL(ExpeditionID,'') as expedition_id,
MutasiDate as mutasi_date,
ExpeditionIsInternal as is_expedition_internal,
IF(ExpeditionIsInternal = 'Y',M_StaffID,0) as staff_id,
IF(ExpeditionIsInternal = 'Y',M_StaffName,'') as staff_name,
IF(ExpeditionIsInternal = 'Y',M_StaffHP,'') as staff_mobile,
IFNULL(MutasiExpeditionPIC,'') as courier_name,
IFNULL(MutasiExpeditionMobile,'') as courier_mobile,
MutasiNote as note,
MutasiIsConfirm as is_confirm,
IF(MutasiConfirmAt = NULL, '',DATE_FORMAT(MutasiConfirmAt,'%d-%m-%Y %H:%i')) as confirm_at,
IFNULL(usersent.M_UserUsername,'') as sent_by,
DATE_FORMAT(MutasiSentAt,'%d-%m-%Y %H:%i') as sent_at,
'' as details,
MutasiIsSent as is_sent
FROM `mutasi`
LEFT JOIN `expedition` ON MutasiExpeditionID = ExpeditionID
LEFT JOIN $this->db_onex.`m_staff` ON MutasiExpeditionStaffID = M_StaffID
LEFT JOIN $this->db_onex.`m_user` ON MutasiConfirmBy = M_UserID
JOIN `companyaddress` fromcompanyaddress ON MutasiFromCompanyAddressID = fromcompanyaddress.CompanyAddressID $filter_from_company_address
JOIN `company` fromcompany ON fromcompanyaddress.CompanyAddressCompanyID = fromcompany.CompanyID
JOIN `companyaddress` tocompanyaddress ON MutasiToCompanyAddressID = tocompanyaddress.CompanyAddressID $filter_to_company_address
JOIN `company` tocompany ON tocompanyaddress.CompanyAddressCompanyID = tocompany.CompanyID
JOIN $this->db_onex.`m_branch` frombranch ON fromcompany.CompanyM_BranchID = frombranch.M_BranchID
JOIN $this->db_onex.`m_branch` tobranch ON tocompany.CompanyM_BranchID = tobranch.M_BranchID
LEFT JOIN $this->db_onex.`m_user` usersent ON usersent.M_UserID = MutasiSentBy
WHERE
MutasiIsActive = 'Y' AND MutasiType = 'B' AND (DATE(MutasiDate) BETWEEN ? AND ?) AND
( MutasiNumber like ? ) $filter_status
ORDER BY $order_by $order_type
LIMIT ? OFFSET ?";
$qry = $this->db_inventory->query($sql, array($prm['start_date'],$prm['end_date'],$search, $perpage, $offset));
//echo $this->db_inventory->last_query();
if($qry){
$rows = $qry->result_array();
$result = array("total_page" => $tot_page, "records" => $rows);
$this->sys_ok($result);
exit;
}
else{
echo $this->db_inventory->last_query();
$this->sys_error_db("purchase order select error", $this->db_inventory->last_query());
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_details(){
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$sql = "SELECT MutasiRequestReceiveID as request_receive_id,
MutasiRequestReceiveMutasiRequestNumber as request_number,
CompanyName as company_name,
M_BranchName as branch_name,
ItemID as item_id,
ItemUnitID as unit_id,
MutasiRequestReceiveRequestQty-MutasiRequestReceiveReceiveQty as qty_request_rest,
MutasiRequestReceiveRequestQty-MutasiRequestReceiveReceiveQty as qty_request,
MutasiRequestReceiveRequestQty as qty_request_all,
MutasiRequestReceiveReceiveQty as qty_received_all,
ItemName as item_name,
ItemUnitName as unit_name,
CONCAT(ItemName,' : ',ItemUnitName) as itemunit_name,
IFNULL(MutasiDetailQty,0) as qty,
MutasiDetailReceiveQty as qty_received,
MutasiDetailID as id,
IFNULL(MutasiIsConfirm,'X') as is_confirm,
'' as details,
IF(MutasiDetailID IS NULL, 'N','Y') as selected
FROM mutasidetail
JOIN mutasirequestreceive ON MutasiDetailMutasiRequestReceiveID = MutasiRequestReceiveID
JOIN companyaddress ON MutasiRequestReceiveCompanyAddressID = CompanyAddressID
JOIN company ON CompanyAddressCompanyID = CompanyID
JOIN $this->db_onex.m_branch ON CompanyM_BranchID = M_BranchID
JOIN item ON MutasiDetailItemID = ItemID
JOIN itemunit ON MutasiDetailItemUnitID = ItemUnitID
JOIN mutasi ON MutasiDetailMutasiID = MutasiID
WHERE
MutasiDetailIsActive = 'Y' AND
MutasiDetailMutasiID = ?
GROUP BY MutasiDetailID
";
$query = $this->db_inventory->query($sql,array($prm['id']));
// echo $this->db_inventory->last_query();
if ($query) {
$rows = $query->result_array();
if($rows){
foreach ($rows as $key => $value) {
$rows[$key]['selected'] = false;
if($value['selected'] == 'Y'){
$rows[$key]['selected'] = true;
}
}
}
//echo $this->db_onedev->last_query();
$result = array( "records" => $rows);
$this->sys_ok($result);
}
else {
$this->sys_error_db("mutasidetail rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function search_to_company_address(){
try {
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']}%";
}
$sql = "
SELECT CompanyAddressID as id, CompanyAddressLabel as name, CompanyAddressDescription as description,
M_BranchID as branch_id, M_BranchName as branch_name
FROM companyaddress
JOIN company ON CompanyAddressCompanyID = CompanyID
JOIN $this->db_onex.m_branch ON CompanyM_BranchID = M_BranchID
WHERE
CompanyName like ?
AND CompanyAddressIsActive = 'Y'
ORDER BY CompanyName ASC
";
$query = $this->db_inventory->query($sql, array($q['search']));
if ($query) {
$rows = $query->result_array();
array_push($rows,array('id'=>0,'name'=>'Semua'));
//echo $this->db_onedev->last_query();
$result = array( "records" => $rows, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else {
$this->sys_error_db("companyaddress rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_from_company_address(){
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT CompanyAddressID as id, CompanyAddressLabel as name, CompanyAddressDescription as description,
M_BranchID as branch_id, M_BranchName as branch_name
FROM companyaddress
JOIN company ON CompanyAddressCompanyID = CompanyID
JOIN $this->db_onex.m_branch ON CompanyM_BranchID = M_BranchID
WHERE
CompanyAddressIsActive = 'Y'
ORDER BY CompanyName ASC
";
$query = $this->db_inventory->query($sql);
if ($query) {
$rows = $query->result_array();
array_push($rows,array('id'=>0,'name'=>'Semua'));
//echo $this->db_onedev->last_query();
$result = array( "records" => $rows, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else {
$this->sys_error_db("companyaddress rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_to_company_address(){
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT CompanyAddressID as id, CompanyAddressLabel as name, CompanyAddressDescription as description,
M_BranchID as branch_id, M_BranchName as branch_name
FROM companyaddress
JOIN company ON CompanyAddressCompanyID = CompanyID
JOIN $this->db_onex.m_branch ON CompanyM_BranchID = M_BranchID
WHERE
CompanyAddressIsActive = 'Y'
ORDER BY CompanyName ASC
";
$query = $this->db_inventory->query($sql);
if ($query) {
$rows = $query->result_array();
array_push($rows,array('id'=>0,'name'=>'Semua'));
//echo $this->db_onedev->last_query();
$result = array( "records" => $rows, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else {
$this->sys_error_db("companyaddress rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function saveAdd(){
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$userid = $this->sys_user['M_UserID'];
$prm = $this->sys_input;
$note = "";
if (isset($prm['note'])) {
$note = trim($prm['note']);
}
$this->db_inventory->trans_start();
$this->db_inventory->trans_strict(FALSE);
$numbering = '';
$sql = "SELECT `fn_numbering`('MPB') as numbering";
$qry = $this->db_inventory->query($sql);
if($qry){
$numbering = $qry->row()->numbering;
}
else{
$this->sys_error_db("get numbering error", $this->db_inventory->last_query());
exit;
}
$last_id = 0;
$now = new DateTime();
$sql = "INSERT INTO mutasi(
MutasiType,
MutasiStatus,
MutasiNumber,
MutasiDate,
MutasiFromCompanyAddressID,
MutasiToCompanyAddressID,
MutasiNote,
MutasiUserID,
MutasiCreated,
MutasiLastUpdated
)
VALUES(
'B','N',?,?,?,?,?,?,NOW(),NOW()
)";
$param_insert = array(
$numbering,
$prm['mutation_date'].' '.$now->format('H:i:s'),
$prm['from_company_address']['id'],
$prm['to_company_address']['id'],
$note,
$userid
);
$qry = $this->db_inventory->query($sql, $param_insert);
$last_id = 0;
if($qry){
$last_id = $this->db_inventory->insert_id();
}
else{
//echo $this->db_inventory->last_query();
$this->sys_error_db("save mutasi error", $this->db_inventory->last_query());
exit;
}
if(count($prm['details'])>0){
foreach ($prm['details'] as $key => $value) {
if($value['selected']){
$sql = "INSERT INTO mutasidetail(
MutasiDetailMutasiID,
MutasiDetailMutasiRequestReceiveID,
MutasiDetailItemID,
MutasiDetailItemUnitID,
MutasiDetailQty,
MutasiDetailUserID,
MutasiDetailCreated,
MutasiDetailLastUpdated
)
VALUES(
?,?,?,?,?,?,NOW(),NOW()
)";
$param_insert_detail = array(
$last_id,
$value['request_receive_id'],
$value['item_id'],
$value['unit_id'],
$value['qty'],
$userid
);
$qry = $this->db_inventory->query($sql,$param_insert_detail);
if(!$qry){
//echo $this->db_inventory->last_query();
$this->sys_error_db("save mutasidetail error", $this->db_inventory->last_query());
exit;
}
}
}
}
$id = $last_id;
$sql = "SELECT mutasi.*, '' as details
FROM mutasi
WHERE MutasiID = ? ";
$qry = $this->db_inventory->query($sql, array($id));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("select mutasi error", $this->db_inventory->last_query());
exit;
}
$row_after = $qry->row_array();
$sql = "SELECT *, '' as details
FROM `mutasidetail`
WHERE
MutasiDetailMutasiID = ? AND
MutasiDetailIsActive = 'Y'";
$qry = $this->db_inventory->query($sql, array($id));
if(!$qry){
$this->sys_error_db("select mutasidetail error", $this->db_inventory->last_query());
exit;
}
$row_after['details'] = $qry->result_array();
$data_log_after = $row_after;
$sql = "INSERT INTO mutasi_log (
MutasiLogMutasiID,
MutasiLogStatus,
MutasiLogJSONBefore,
MutasiLogJSONAfter,
MutasiLogUserID,
MutasiLogCreated
)
VALUES(
?,?,NULL,?,?,NOW()
)";
$qry = $this->db_inventory_log->query($sql, array($id,'ADD',json_encode($data_log_after),$userid));
if(!$qry){
$this->sys_error_db("insert log error", $this->db_inventory_log->last_query());
exit;
}
$this->db_inventory->trans_complete();
$result = array(
"message" => ''
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function edit(){
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$userid = $this->sys_user['M_UserID'];
$this->update($prm,$userid);
$result = array(
"message" => ''
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function update($prm,$userid){
$note = "";
if (isset($prm['note'])) {
$note = trim($prm["note"]);
}
$courier_name = '';
if (isset($prm['courier_name'])) {
$courier_name = trim($prm["courier_name"]);
}
$courier_mobile = '';
if (isset($prm['courier_mobile'])) {
$courier_mobile = trim($prm["courier_mobile"]);
}
$id = $prm['id'];
$sql = "SELECT mutasi.*, '' as details
FROM mutasi
WHERE MutasiID = ? ";
$qry = $this->db_inventory->query($sql, array($id));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("select mutasi error", $this->db_inventory->last_query());
exit;
}
$row_before = $qry->row_array();
$sql = "SELECT *, '' as details
FROM `mutasidetail`
WHERE
MutasiDetailMutasiID = ? AND
MutasiDetailIsActive = 'Y'";
$qry = $this->db_inventory->query($sql, array($id));
if(!$qry){
$this->sys_error_db("select mutasidetail error", $this->db_inventory->last_query());
exit;
}
$row_before['details'] = $qry->result_array();
$data_log_before = $row_before;
$this->db_inventory->trans_start();
$this->db_inventory->trans_strict(FALSE);
$last_id = $id;
$sql = "UPDATE mutasi SET
MutasiDate = ?,
MutasiExpeditionID = ?,
MutasiExpeditionStaffID = ?,
MutasiExpeditionPIC = ?,
MutasiExpeditionMobile = ?,
MutasiNote = ?,
MutasiLastUpdated = NOW(),
MutasiUserID = ?
WHERE
MutasiID = ?";
$param_update_header = array(
$prm['mutation_date'],
isset($prm['expedition'])?$prm['expedition']['id']:0,
isset($prm['courier_staf']) && intval($prm['courier_staf']['id']) > 0?$prm['courier_staf']['id']:0,
$courier_name,
$courier_mobile,
$note,
$userid,
$id
);
$qry = $this->db_inventory->query($sql, $param_update_header);
//echo $this->db_inventory->last_query();
if(!$qry){
//echo $this->db_inventory->last_query();
$this->sys_error_db("edit purchaseorder error", $this->db_inventory->last_query());
exit;
}
$sql = "UPDATE mutasidetail SET MutasiDetailIsActive = 'N' WHERE MutasiDetailMutasiID = ?";
$qry = $this->db_inventory->query($sql, array($id));
if(!$qry){
//echo $this->db_inventory->last_query();
$this->sys_error_db("update N detail error", $this->db_inventory->last_query());
exit;
}
if(count($prm['details'])>0){
foreach ($prm['details'] as $key => $value) {
if($value['selected']){
if(intval($value['id']) > 0){
$sql = "UPDATE mutasidetail SET
MutasiDetailQty = ?,
MutasiDetailUserID = ?,
MutasiDetailIsActive = 'Y',
MutasiDetailLastUpdated = NOW()
WHERE
MutasiDetailID = ?";
$param_edit_detail = array(
$value['qty'],
$userid,
$value['id']
);
$qry = $this->db_inventory->query($sql, $param_edit_detail);
//echo $this->db_inventory->last_query();
if(!$qry){
//echo $this->db_inventory->last_query();
$this->sys_error_db("edit mutasidetail error", $this->db_inventory->last_query());
exit;
}
}else{
$sql = "INSERT INTO mutasidetail(
MutasiDetailMutasiID,
MutasiDetailMutasiRequestReceiveID,
MutasiDetailItemID,
MutasiDetailItemUnitID,
MutasiDetailQty,
MutasiDetailUserID,
MutasiDetailCreated,
MutasiDetailLastUpdated
)
VALUES(
?,?,?,?,?,?,NOW(),NOW()
)";
$param_insert_detail = array(
$last_id,
$value['request_receive_id'],
$value['item_id'],
$value['unit_id'],
$value['qty'],
$userid
);
$qry = $this->db_inventory->query($sql,$param_insert_detail);
if(!$qry){
//echo $this->db_inventory->last_query();
$this->sys_error_db("save mutasidetail error", $this->db_inventory->last_query());
exit;
}
}
}
}
}
$sql = "SELECT mutasi.*, '' as details
FROM mutasi
WHERE MutasiID = ? ";
$qry = $this->db_inventory->query($sql, array($id));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("select mutasi error", $this->db_inventory->last_query());
exit;
}
$row_after = $qry->row_array();
$sql = "SELECT *, '' as details
FROM `mutasidetail`
WHERE
MutasiDetailMutasiID = ? AND
MutasiDetailIsActive = 'Y'";
$qry = $this->db_inventory->query($sql, array($id));
if(!$qry){
$this->sys_error_db("select mutasidetail error", $this->db_inventory->last_query());
exit;
}
$row_after['details'] = $qry->result_array();
$data_log_after = $row_after;
$sql = "INSERT INTO mutasi_log (
MutasiLogMutasiID,
MutasiLogStatus,
MutasiLogJSONBefore,
MutasiLogJSONAfter,
MutasiLogUserID,
MutasiLogCreated
)
VALUES(
?,?,?,?,?,NOW()
)";
$qry = $this->db_inventory_log->query($sql, array($id,'EDIT',json_encode($data_log_before),json_encode($data_log_after),$userid));
if(!$qry){
$this->sys_error_db("insert log error", $this->db_inventory_log->last_query());
exit;
}
$this->db_inventory->trans_complete();
return true;
}
function delete()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$userid = $this->sys_user['M_UserID'];
$param = $this->sys_input;
$id = "";
if (isset($param['id'])) {
$id = intval($param["id"]);
}
if ($id == "" || !$id) {
$error = array(
"message" => "id is mandatory",
);
$this->sys_error_db($error);
exit;
}
$this->db_inventory->trans_start();
$this->db_inventory->trans_strict(FALSE);
$sql = "UPDATE mutasi
SET
MutasiIsActive = 'N',
MutasiLastUpdated = now(),
MutasiUserID = ?
WHERE MutasiID = ?
";
$qry = $this->db_inventory->query($sql, [$userid,$id]);
if(!$qry){
$this->sys_error_db("delete mutasi error", $this->db_inventory->last_query());
exit;
}else{
$sql = "UPDATE mutasidetail
SET
MutasiDetailIsActive = 'N',
MutasiDetailLastUpdated = now(),
MutasiDetailUserID = ?
WHERE
MutasiDetailMutasiID = ?
";
$qry = $this->db_inventory->query($sql, [$userid,$id]);
if(!$qry){
$this->sys_error_db("delete mutasidetail error", $this->db_inventory->last_query());
exit;
}
}
$sql = "SELECT mutasi.*, '' as details
FROM mutasi
WHERE MutasiID = ? ";
$qry = $this->db_inventory->query($sql, array($id));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("select mutasi error", $this->db_inventory->last_query());
exit;
}
$row_after = $qry->row_array();
$data_log_after = $row_after;
$sql = "INSERT INTO mutasi_log (
MutasiLogMutasiID,
MutasiLogStatus,
MutasiLogJSONBefore,
MutasiLogJSONAfter,
MutasiLogUserID,
MutasiLogCreated
)
VALUES(
?,?,NULL,?,?,NOW()
)";
$qry = $this->db_inventory_log->query($sql, array($id,'DELETE',json_encode($data_log_after),$userid));
if(!$qry){
$this->sys_error_db("insert log error", $this->db_inventory_log->last_query());
exit;
}
$this->db_inventory->trans_complete();
$result = array(
"message" => ''
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function validateDate($date, $format = 'Y-m-d')
{
$d = DateTime::createFromFormat($format, $date);
// The Y ( 4 digits year ) returns TRUE for any integer with any number of digits so changing the comparison from == to === fixes the issue.
return $d && $d->format($format) === $date;
}
function confirm()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$userid = $this->sys_user['M_UserID'];
$param = $this->sys_input;
$this->update($param,$userid);
$id = "";
if (isset($param['id'])) {
$id = intval($param["id"]);
}
if ($id == "" || !$id) {
$error = array(
"message" => "id is mandatory",
);
$this->sys_error_db($error);
exit;
}
$this->db_inventory->trans_start();
$this->db_inventory->trans_strict(FALSE);
$sql = "UPDATE mutasi
SET MutasiIsSent = 'Y',
MutasiLastUpdated = NOW(),
MutasiSentAt = NOW(),
MutasiSentBy = ?,
MutasiUserID = ?
WHERE MutasiID = ?
";
$qry = $this->db_inventory->query($sql, [$userid,$userid,$id]);
if(!$qry){
$this->sys_error_db("confirm mutasi error", $this->db_inventory->last_query());
exit;
}
$sql = "SELECT mutasi.*, '' as details
FROM mutasi
LEFT JOIN expedition ON MutasiExpeditionID = ExpeditionID
LEFT JOIN $this->db_onex.m_staff ON MutasiExpeditionStaffID = M_StaffID
WHERE MutasiID = ? ";
$qry = $this->db_inventory->query($sql, array($id));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("select mutasi error", $this->db_inventory->last_query());
exit;
}
$row_after = $qry->row_array();
$sql = "SELECT mutasidetail.*
FROM mutasidetail
WHERE
MutasiDetailMutasiID = ? AND MutasiDetailIsActive = 'Y'";
$qry = $this->db_inventory->query($sql, [$id]);
if(!$qry){
$this->sys_error_db("select mutasidetail before get stock error", $this->db_inventory->last_query());
exit;
}
$row_after['details'] = $qry->result_array();
$data_log_after = $row_after;
$arr_insert_setup_branch = array(
'MutasiSetupBranchMutasiID' => $data_log_after['MutasiID'],
'MutasiSetupBranchToCompanyAddressID' => $data_log_after['MutasiFromCompanyAddressID'],
'MutasiSetupBranchJSON' => json_encode($data_log_after),
'MutasiSetupBranchSentTime' => date("Y-m-d H:i:s"),
'MutasiSetupBranchCreated' => date("Y-m-d H:i:s")
);
$qry = $this->db_inventory->insert('mutasisetupbranch', $arr_insert_setup_branch);
if(!$qry){
$this->sys_error_db("insert mutasisetupbranch error", $this->db_inventory_log->last_query());
exit;
}
$sql = "INSERT INTO mutasi_log (
MutasiLogMutasiID,
MutasiLogStatus,
MutasiLogJSONBefore,
MutasiLogJSONAfter,
MutasiLogUserID,
MutasiLogCreated
)
VALUES(
?,?,NULL,?,?,NOW()
)";
$qry = $this->db_inventory_log->query($sql, array($id,'SENT',json_encode($data_log_after),$userid));
if(!$qry){
$this->sys_error_db("insert log error", $this->db_inventory_log->last_query());
exit;
}
$this->db_inventory->trans_complete();
$result = array(
"message" => 'sent success'
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_data_company_default(){
try{
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT CompanyID as id,
CompanyName as name,
'' as address
FROM company
JOIN one_aditya.m_branch ON CompanyM_BranchID = M_BranchID AND M_BranchIsDefault = 'Y'
WHERE
CompanyIsActive = 'Y' LIMIT 1";
$query = $this->db_inventory->query($sql);
if ($query) {
$row = $query->row_array();
$sql = "SELECT CompanyAddressID as id,
CompanyAddressLabel as name,
CompanyAddressDescription as description
FROM companyaddress
WHERE
CompanyAddressCompanyID = ? AND CompanyAddressIsActive = 'Y'";
$query = $this->db_inventory->query($sql,array($row['id']));
$data_address = $query->result_array();
$row['address'] = $data_address;
$result = array("records" => $row);
$this->sys_ok($result);
}
else {
$this->sys_error_db("expedition rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_expedition(){
try{
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT ExpeditionID as id,
ExpeditionName as name,
ExpeditionIsInternal as is_internal
FROM expedition
WHERE
ExpeditionIsActive = 'Y'";
$query = $this->db_inventory->query($sql);
if ($query) {
$rows = $query->result_array();
$result = array("records" => $rows);
$this->sys_ok($result);
}
else {
$this->sys_error_db("expedition rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_expedition_staff(){
try{
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT M_StaffID as id,
M_StaffName as name
FROM expeditionstaff
JOIN $this->db_onex.m_staff ON ExpeditionStaffM_StaffID = M_StaffID AND M_StaffIsActive = 'Y'
WHERE
ExpeditionStaffIsActive = 'Y'";
$query = $this->db_inventory->query($sql);
if ($query) {
$rows = $query->result_array();
$result = array("records" => $rows);
$this->sys_ok($result);
}
else {
$this->sys_error_db("expedition rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_from_company_address_form(){
try{
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT CompanyAddressID as id,
CompanyAddressLabel as name,
CompanyAddressDescription as description,
CompanyName as company_name,
M_BranchID as branch_id, M_BranchName as branch_name
FROM companyaddress
JOIN company ON CompanyAddressCompanyID = CompanyID
JOIN $this->db_onex.m_branch ON CompanyM_BranchID = M_BranchID
WHERE
CompanyAddressIsActive = 'Y'
ORDER BY CompanyName ASC
";
$query = $this->db_inventory->query($sql);
if ($query) {
$rows = $query->result_array();
$result = array("records" => $rows);
$this->sys_ok($result);
}
else {
$this->sys_error_db("companyaddress rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_to_company_address_form(){
try{
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT CompanyAddressID as id,
CompanyAddressLabel as name,
CompanyAddressDescription as description,
CompanyName as company_name,
M_BranchID as branch_id, M_BranchName as branch_name
FROM companyaddress
JOIN mutasirequestreceive ON MutasiRequestReceiveCompanyAddressID = CompanyAddressID AND
MutasiRequestReceiveIsActive = 'Y' AND
( MutasiRequestReceiveStatus <> 'N' AND MutasiRequestReceiveStatus <> 'D' )
JOIN company ON CompanyAddressCompanyID = CompanyID
JOIN $this->db_onex.m_branch ON CompanyM_BranchID = M_BranchID
WHERE
CompanyAddressIsActive = 'Y'
GROUP BY CompanyAddressID";
$query = $this->db_inventory->query($sql);
if ($query) {
$rows = $query->result_array();
$result = array("records" => $rows);
$this->sys_ok($result);
}
else {
$this->sys_error_db("companyaddress rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_data_request(){
try{
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$userid = $this->sys_user['M_UserID'];
$prm = $this->sys_input;
$id = $prm['company_address_id'];
$sql = "SELECT MutasiRequestReceiveID as request_receive_id,
MutasiRequestReceiveMutasiRequestNumber as request_number,
CompanyName as company_name,
ItemID as item_id,
ItemUnitID as unit_id,
MutasiRequestReceiveRequestQty-MutasiRequestReceiveReceiveQty as qty_request,
ItemName as item_name,
ItemUnitName as unit_name,
CONCAT(ItemName,' : ',ItemUnitName) as itemunit_name,
IFNULL(SUM(StockQty),0) as qty_stock,
0 as qty,
IF(MutasiDetailID IS NULL,'N','Y') as selected
FROM mutasirequestreceive
LEFT JOIN mutasidetail ON MutasiDetailMutasiRequestReceiveID = MutasiRequestReceiveID AND
MutasiDetailItemID = MutasiRequestReceiveItemID AND MutasiDetailItemUnitID = MutasiRequestReceiveItemUnitID AND
MutasiDetailIsActive = 'Y'
JOIN companyaddress ON MutasiRequestReceiveCompanyAddressID = CompanyAddressID
JOIN company ON CompanyAddressCompanyID = CompanyID
JOIN item ON MutasiRequestReceiveItemID = ItemID
JOIN itemunit ON MutasiRequestReceiveItemUnitID = ItemUnitID
LEFT JOIN stock ON StockItemID = ItemID AND StockItemUnitID = ItemUnitID
LEFT JOIN warehouse ON StockWarehouseID = WarehouseID AND WarehouseIsOffice = 'Y'
WHERE
MutasiRequestReceiveStatus NOT IN('N','D','C') AND MutasiRequestReceiveIsActive = 'Y' AND
MutasiRequestReceiveCompanyAddressID = ? AND
( MutasiRequestReceiveRequestQty - MutasiRequestReceiveReceiveQty ) > 0
GROUP BY MutasiRequestReceiveID";
$query = $this->db_inventory->query($sql,array($id));
//echo $this->db_inventory->last_query();
if ($query) {
$rows = $query->result_array();
if(count($rows) > 0){
foreach ($rows as $key => $value) {
$rows[$key]['selected'] = false;
}
}
$result = array("records" => $rows);
$this->sys_ok($result);
}
else {
$this->sys_error_db("get data request rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_printout(){
try{
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT *
FROM r_report
WHERE
R_ReportCode = 'MPB' AND R_ReportIsActive = 'Y'";
$query = $this->db_inventory->query($sql);
if ($query) {
$rows = $query->row_array();
$result = array("records" => $rows);
$this->sys_ok($result);
}
else {
$this->sys_error_db("r_report rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
}