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

204 lines
7.0 KiB
PHP

<?php
class Downloadfrombranchtobranch extends MY_Controller
{
var $db;
function __construct()
{
parent::__construct();
$this->db_inventory = $this->load->database("inventory", true);
$this->db_inventory_log = $this->load->database('inventory_log', true);
}
function download_data()
{
try {
header("Content-Type:application/json");
$datas = json_decode(file_get_contents('php://input'), true);
//print_r($datas);
if($datas){
$this->db_inventory->trans_start();
$this->db_inventory->trans_strict(FALSE);
$arr_inserted = array();
foreach ($datas as $k => $v) {
$sql ="SELECT * FROM mutasireceiverespon WHERE MutasiReceiveResponOldID = ? LIMIT 1";
$qry = $this->db_inventory->query([$v['MutasiReceiveResponID']]);
//echo $this->db_inventory->last_query();
if(!$qry){
// echo $this->db_inventory->last_query();
// exit;
$this->sys_error_db("select mutasireceiverespon error : ". $this->db_inventory->last_query());
exit;
}
//print_r($qry->row_array());
if(!$qry->row_array()){
$arr_insert_receive = $v;
unset($arr_insert_receive['branch_ip']);
unset($arr_insert_receive['MutasiReceiveResponID']);
$arr_insert_receive['MutasiReceiveResponIsSend'] = 'Y';
$arr_insert_receive['MutasiReceiveResponOldID'] = $v['MutasiReceiveResponID'];
$arr_insert_receive['MutasiReceiveResponSentTime'] = date("Y-m-d H:i:s");
$arr_insert_receive['MutasiReceiveResponLastUpdated'] = date("Y-m-d H:i:s");
$qry = $this->db_inventory->insert('mutasireceiverespon', $arr_insert_receive);
if(!$qry){
// echo $this->db_inventory->last_query();
// exit;
$this->sys_error_db("insert mutasireceiverespon error : ". $this->db_inventory->last_query());
exit;
}
$this->update_mutasi($v);
}
array_push($arr_inserted,$v['MutasiReceiveResponID']);
}
$this->db_inventory->trans_complete();
$result = array( "records" => $arr_inserted);
$this->sys_ok($result);
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function update_mutasi($data){
//print_r($mutasi);
$mutasi = $this->objToArray(json_decode($data['MutasiReceiveResponJSON']));
//print_r($arr_insert_mutasi);
$details = $mutasi['details'];
foreach($details as $key => $value){
$sql = "UPDATE mutasidetail SET MutasiDetailReceiveQty = ? WHERE MutasiDetailID = ? ";
$qry = $this->db_inventory->query($sql, array(
$value['MutasiDetailReceiveQty'],
$value['MutasiDetailID']
));
if(!$qry){
// echo $this->db_inventory->last_query();
// exit;
$this->sys_error_db("update mutasidetail error : ". $this->db_inventory->last_query());
exit;
}
}
$sql = "SELECT COUNT(*) as xcount
FROM mutasidetail
WHERE
MutasiDetailMutasiID = ? AND
MutasiDetailIsActive = 'Y' AND
MutasiDetailReceiveQty < MutasiDetailQty ";
$qry = $this->db_inventory->query($sql, array(
$mutasi['MutasiID']
));
if(!$qry){
// echo $this->db_inventory->last_query();
// exit;
$this->sys_error_db("select mutasidetail error : ". $this->db_inventory->last_query());
exit;
}
$count_not_done = $qry->row()->xcount;
$status = 'P';
if($count_not_done == 0){
$status = 'D';
}
$sql = "UPDATE mutasi SET MutasiStatus = ?
WHERE
MutasiID = ?";
$qry = $this->db_inventory->query($sql, array(
$status ,
$mutasi['MutasiID']
));
if(!$qry){
// echo $this->db_inventory->last_query();
// exit;
$this->sys_error_db("update mutasi error : ". $this->db_inventory->last_query());
exit;
}
$sql = "SELECT mutasi.*,'' as details
FROM mutasi
WHERE MutasiID = ?
";
$qry = $this->db_inventory->query($sql, array(
$mutasi['MutasiID']
));
if(!$qry){
// echo $this->db_inventory->last_query();
// exit;
$this->sys_error_db("select mutasi error : ". $this->db_inventory->last_query());
exit;
}
$mutasi_respon = $qry->row_array();
$sql = "SELECT mutasidetail.*
FROM mutasidetail
WHERE
MutasiDetailMutasiID = ? AND
MutasiDetailIsActive = 'Y'
";
$qry = $this->db_inventory->query($sql, array(
$mutasi['MutasiID']
));
if(!$qry){
// echo $this->db_inventory->last_query();
// exit;
$this->sys_error_db("select mutasi error : ". $this->db_inventory->last_query());
exit;
}
$mutasi_respon['details'] = $qry->result_array();
$arr_insert = array();
$arr_insert['MutasiResponBranchToBranchMutasiID'] = $mutasi_respon['MutasiID'];
$arr_insert['MutasiResponBranchToBranchFromCompanyAddressID'] = $mutasi_respon['MutasiFromCompanyAddressID'];
$arr_insert['MutasiResponBranchToBranchJSON'] = json_encode($mutasi_respon);
$arr_insert['MutasiResponBranchToBranchCreated'] = date("Y-m-d H:i:s");
$qry = $this->db_inventory->insert('mutasiresponbranchtobranch', $arr_insert);
if(!$qry){
// echo $this->db_inventory->last_query();
// exit;
$this->sys_error_db("insert mutasiresponbranchtobranch error : ". $this->db_inventory->last_query());
exit;
}
return true;
}
protected function objToArray($obj)
{
// Not an object or array
if (!is_object($obj) && !is_array($obj)) {
return $obj;
}
// Parse array
foreach ($obj as $key => $value) {
$arr[$key] = $this->objToArray($value);
}
// Return parsed array
return $arr;
}
}