142 lines
5.2 KiB
PHP
142 lines
5.2 KiB
PHP
<?php
|
|
class Setupfromcenter 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 mutasisetupbranch WHERE MutasiSetupBranchOldID = ? LIMIT 1";
|
|
$qry = $this->db_inventory->query([$v['MutasiSetupBranchID']]);
|
|
//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_setup = $v;
|
|
$arr_insert_setup['MutasiSetupBranchOldID'] = $v['MutasiSetupBranchID'];
|
|
unset($arr_insert_setup['branch_ip']);
|
|
unset($arr_insert_setup['MutasiSetupBranchID']);
|
|
$arr_insert_setup['MutasiSetupBranchIsSend'] = 'Y';
|
|
$arr_insert_setup['MutasiSetupBranchCreated'] = date("Y-m-d H:i:s");
|
|
$arr_insert_setup['MutasiSetupBranchLastUpdated'] = date("Y-m-d H:i:s");
|
|
|
|
$qry = $this->db_inventory->insert('mutasisetupbranch', $arr_insert_setup);
|
|
if(!$qry){
|
|
echo $this->db_inventory->last_query();
|
|
exit;
|
|
}
|
|
|
|
$last_id = $this->db_inventory->insert_id();
|
|
$sql = "SELECT * FROM mutasisetupbranch WHERE MutasiSetupBranchID = ?";
|
|
$qry = $this->db_inventory->query($sql, array($last_id));
|
|
if(!$qry){
|
|
echo $this->db_inventory->last_query();
|
|
exit;
|
|
}
|
|
|
|
$data_after_insert = $qry->row_array();
|
|
//print_r($data_after_insert);
|
|
$this->create_mutasi($data_after_insert);
|
|
|
|
//$arr_inserted = $v;
|
|
}
|
|
|
|
array_push($arr_inserted,$v['MutasiSetupBranchID']);
|
|
|
|
}
|
|
$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 create_mutasi($mutasi){
|
|
//print_r($mutasi);
|
|
$arr_insert_mutasi = $this->objToArray(json_decode($mutasi['MutasiSetupBranchJSON']));
|
|
//print_r($arr_insert_mutasi);
|
|
$details = $arr_insert_mutasi['details'];
|
|
$id = $arr_insert_mutasi['MutasiID'];
|
|
unset($arr_insert_mutasi['details']);
|
|
unset($arr_insert_mutasi['MutasiID']);
|
|
$arr_insert_mutasi['MutasiCreated'] = date("Y-m-d H:i:s");
|
|
$arr_insert_mutasi['MutasiLastUpdated'] = date("Y-m-d H:i:s");
|
|
$qry = $this->db_inventory->insert('mutasi', $arr_insert_mutasi);
|
|
//echo $this->db_inventory->last_query();
|
|
if(!$qry){
|
|
echo $this->db_inventory->last_query();
|
|
exit;
|
|
}
|
|
$last_id = $this->db_inventory->insert_id();
|
|
foreach($details as $key => $value){
|
|
$arr_insert_mutasi_details = $value;
|
|
unset($arr_insert_mutasi_details['MutasiDetailID']);
|
|
$arr_insert_mutasi_details['MutasiDetailCreated'] = date("Y-m-d H:i:s");
|
|
$arr_insert_mutasi_details['MutasiDetailLastUpdated'] = date("Y-m-d H:i:s");
|
|
$arr_insert_mutasi_details['MutasiDetailUserID'] = 220688;
|
|
$qry = $this->db_inventory->insert('mutasidetail', $arr_insert_mutasi_details);
|
|
//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;
|
|
}
|
|
|
|
}
|
|
|
|
|