274 lines
9.5 KiB
PHP
274 lines
9.5 KiB
PHP
<?php
|
|
class Downloadfrombranchtocenter1 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 get_param_z()
|
|
{
|
|
$body_z = file_get_contents("php://input");
|
|
$body = gzuncompress($body_z);
|
|
return json_decode($body, true);
|
|
}
|
|
|
|
function reply($resp)
|
|
{
|
|
echo json_encode($resp);
|
|
}
|
|
|
|
function reply_gz($resp)
|
|
{
|
|
//echo json_encode($resp);
|
|
echo gzcompress(json_encode($resp));
|
|
}
|
|
|
|
function download_data()
|
|
{
|
|
try {
|
|
header("Content-Type:application/json");
|
|
// $datas = json_decode(file_get_contents('php://input'), true);
|
|
|
|
$datas = $this->get_param_z();
|
|
|
|
//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 mutasiresponbranchtobranch WHERE MutasiResponBranchToBranchOldID = ? LIMIT 1";
|
|
$qry = $this->db_inventory->query([$v['MutasiResponBranchToBranchID']]);
|
|
//echo $this->db_inventory->last_query();
|
|
if(!$qry){
|
|
echo $this->db_inventory->last_query();
|
|
exit;
|
|
}
|
|
//print_r($qry->row_array());
|
|
if(!$qry->row_array()){
|
|
|
|
$arr_insert_mutation = $v;
|
|
unset($arr_insert_mutation['branch_ip']);
|
|
unset($arr_insert_mutation['mutasi_center_id']);
|
|
unset($arr_insert_mutation['MutasiResponBranchToBranchID']);
|
|
$arr_insert_mutation['MutasiResponBranchToBranchIsSend'] = 'Y';
|
|
$arr_insert_mutation['MutasiResponBranchToBranchOldID'] = $v['MutasiResponBranchToBranchID'];
|
|
$arr_insert_mutation['MutasiResponBranchToBranchLastUpdated'] = date("Y-m-d H:i:s");
|
|
|
|
// $qry = $this->db_inventory->insert('mutasiresponbranchtobranch', $arr_insert_mutation);
|
|
|
|
$sql = $this->db_inventory->insert_string('mutasiresponbranchtobranch', $arr_insert_mutation) . ' ON DUPLICATE KEY UPDATE MutasiResponBranchToBranchOldID = MutasiResponBranchToBranchOldID';
|
|
$qry = $this->db_inventory->query($sql);
|
|
if(!$qry){
|
|
echo $this->db_inventory->last_query();
|
|
exit;
|
|
}
|
|
|
|
$this->update_data($v);
|
|
}
|
|
|
|
array_push($arr_inserted,$v['MutasiResponBranchToBranchID']);
|
|
|
|
}
|
|
$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_data($data){
|
|
//print_r($mutasi);
|
|
$mutasi_center_id = $data['mutasi_center_id'];
|
|
$mutasi = $this->objToArray(json_decode($data['MutasiResponBranchToBranchJSON']));
|
|
//print_r($arr_insert_mutasi);
|
|
$details = $mutasi['details'];
|
|
|
|
foreach($details as $key => $value){
|
|
$sql = "UPDATE mutasidetail SET
|
|
MutasiDetailReceiveQty = ?
|
|
WHERE
|
|
MutasiDetailMutasiRequestReceiveID = ? AND
|
|
MutasiDetailMutasiID = ? AND
|
|
MutasiDetailItemID = ? AND
|
|
MutasiDetailItemUnitID = ?
|
|
";
|
|
|
|
$qry = $this->db_inventory->query($sql, array(
|
|
$value['MutasiDetailReceiveQty'],
|
|
$value['MutasiDetailMutasiRequestReceiveID'],
|
|
$mutasi_center_id,
|
|
$value['MutasiDetailItemID'],
|
|
$value['MutasiDetailItemUnitID']
|
|
));
|
|
//echo $this->db_inventory->last_query();
|
|
if(!$qry){
|
|
echo $this->db_inventory->last_query();
|
|
exit;
|
|
}
|
|
|
|
$sql = "UPDATE mutasirequestreceive SET
|
|
MutasiRequestReceiveReceiveQty = ?
|
|
WHERE
|
|
MutasiRequestReceiveID = ?
|
|
";
|
|
|
|
$qry = $this->db_inventory->query($sql, array(
|
|
$value['MutasiDetailReceiveQty'],
|
|
$value['MutasiDetailMutasiRequestReceiveID']
|
|
));
|
|
//echo $this->db_inventory->last_query();
|
|
if(!$qry){
|
|
echo $this->db_inventory->last_query();
|
|
exit;
|
|
}
|
|
|
|
$sql = "SELECT * FROM mutasirequestreceive WHERE MutasiRequestReceiveID = ?";
|
|
$qry = $this->db_inventory->query($sql, array(
|
|
$value['MutasiDetailMutasiRequestReceiveID']
|
|
));
|
|
|
|
//echo $this->db_inventory->last_query();
|
|
if(!$qry){
|
|
echo $this->db_inventory->last_query();
|
|
exit;
|
|
}
|
|
|
|
$data_mutasi_received = $qry->row_array();
|
|
$lastest_status = 'P';
|
|
if(intval($data_mutasi_received['MutasiRequestReceiveReceiveQty']) >= intval($data_mutasi_received['MutasiRequestReceiveRequestQty']))
|
|
$lastest_status = 'D';
|
|
|
|
$sql = "UPDATE mutasirequestreceive SET
|
|
MutasiRequestReceiveStatus = ?
|
|
WHERE
|
|
MutasiRequestReceiveID = ?
|
|
";
|
|
|
|
$qry = $this->db_inventory->query($sql, array(
|
|
$lastest_status,
|
|
$value['MutasiDetailMutasiRequestReceiveID']
|
|
));
|
|
//echo $this->db_inventory->last_query();
|
|
if(!$qry){
|
|
echo $this->db_inventory->last_query();
|
|
exit;
|
|
}
|
|
|
|
|
|
$sql = "SELECT ItemID, ItemName, ItemUnitID, ItemUnitName,
|
|
? as qty_request,
|
|
? as qty_receive
|
|
FROM item
|
|
JOIN itemunitmap ON ItemUnitMapItemID = ItemID
|
|
JOIN itemunit ON ItemUnitMapItemUnitID = ItemUnitID AND ItemUnitID = ?
|
|
WHERE
|
|
ItemID = ?";
|
|
$qry = $this->db_inventory->query($sql, array(
|
|
$value['MutasiDetailQty'],
|
|
$value['MutasiDetailReceiveQty'],
|
|
$value['MutasiDetailItemUnitID'],
|
|
$value['MutasiDetailItemID'],
|
|
));
|
|
//echo $this->db_inventory->last_query();
|
|
if(!$qry){
|
|
echo $this->db_inventory->last_query();
|
|
exit;
|
|
}
|
|
|
|
$data_json = $qry->row_array();
|
|
|
|
$sql = "INSERT INTO mutasirequestreceive_status (
|
|
MutasiRequestReceiveStatusMutasiRequestReceiveID,
|
|
MutasiRequestReceiveStatusStatus,
|
|
MutasiRequestReceiveStatusJSON,
|
|
MutasiRequestReceiveStatusCreated,
|
|
MutasiRequestReceiveStatusUserID
|
|
)
|
|
VALUES(?,?,?,NOW(),10000)";
|
|
$qry = $this->db_inventory->query($sql, array(
|
|
$value['MutasiDetailMutasiRequestReceiveID'],
|
|
'P',
|
|
json_encode($data_json)
|
|
));
|
|
//echo $this->db_inventory->last_query();
|
|
if(!$qry){
|
|
echo $this->db_inventory->last_query();
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$status = 'P';
|
|
$sql = "SELECT count(*) as xcount
|
|
FROM mutasidetail
|
|
WHERE
|
|
MutasiDetailMutasiID = ? AND
|
|
MutasiDetailReceiveQty < MutasiDetailQty AND
|
|
MutasiDetailIsActive = 'Y'";
|
|
|
|
$qry = $this->db_inventory->query($sql, array(
|
|
$mutasi_center_id
|
|
));
|
|
//echo $this->db_inventory->last_query();
|
|
if(!$qry){
|
|
echo $this->db_inventory->last_query();
|
|
exit;
|
|
}
|
|
|
|
$count_not_done = $qry->row()->xcount;
|
|
if($count_not_done == 0)
|
|
$status = 'D';
|
|
|
|
$sql = "UPDATE mutasi SET MutasiStatus = ?
|
|
WHERE
|
|
MutasiID = ?";
|
|
$qry = $this->db_inventory->query($sql, array(
|
|
$status,
|
|
$mutasi_center_id
|
|
));
|
|
//echo $this->db_inventory->last_query();
|
|
if(!$qry){
|
|
echo $this->db_inventory->last_query();
|
|
exit;
|
|
}
|
|
|
|
$sql = "UPDATE mutasisetupbranch SET MutasiSetupBranchIsCreated = 'Y' WHERE MutasiSetupBranchID = ? ";
|
|
$qry = $this->db_inventory->query($sql, array($id));
|
|
if(!$qry){
|
|
echo $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;
|
|
}
|
|
|
|
}
|