Files
2026-04-15 15:23:57 +07:00

123 lines
4.9 KiB
PHP

<?php
class Setuptobranchv1test 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);
$this->db_onex = 'one_aditya';
}
function index()
{
echo "dasd";
}
function upload_data()
{
// $this->db_inventory->trans_begin();
try {
$sql = "SELECT mutasisetupbranch.*, M_BranchIPAddress as branch_ip
FROM `mutasisetupbranch`
JOIN `companyaddress` ON MutasiSetupBranchToCompanyAddressID = CompanyAddressID
JOIN `company` ON CompanyAddressCompanyID = CompanyID
JOIN $this->db_onex.`m_branch` ON CompanyM_BranchID = M_BranchID
WHERE
MutasiSetupBranchIsSend = 'N'";
//echo $sql;
$qry = $this->db_inventory->query($sql);
if (!$qry) {
$this->sys_error_db("select mutasisetupbranch error", $this->db_inventory->last_query());
exit;
}
$data_get = $qry->result_array();
// print_r($data_get);
$resp = array();
if ($data_get) {
foreach ($data_get as $k => $val) {
//API URL
$url = "http://" . $val['branch_ip'] . "/one-api/inventory/requestreceive/setupfromcenterv1/download_data";
$data = array($val);
// echo ($url);
$ch = curl_init($url);
# Setup request to send json via POST.
$payload = json_encode($data);
$payloadZ = gzcompress($payload);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadZ);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . strlen($payloadZ)
)
);
# Return response instead of printing.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# Send request.
$result = curl_exec($ch);
curl_close($ch);
// print_r($result);
# Print response.
$array_rst = $this->objToArray(json_decode($result));
if (json_last_error() !== JSON_ERROR_NONE) {
$this->sys_error(json_last_error_msg() . " " . $result);
exit;
}
// $array_rst = $this->objToArray(gzuncompress(json_decode($result)));
// print_r(gzuncompress(json_decode($result)));
if ($array_rst['status'] == 'OK') {
$data_respon = $array_rst['data'];
//print_r($data_respon['records']);
if (count($data_respon['records']) > 0) {
foreach ($data_respon['records'] as $key => $value) {
$sql = "UPDATE mutasisetupbranch SET
MutasiSetupBranchIsSend = 'Y'
WHERE MutasiSetupBranchID = ?";
$qry = $this->db_inventory->query($sql, [$value]);
if (!$qry) {
$this->sys_error_db("update mutasisetupbranch MutasiSetupBranchIsSend = Y error", $this->db_inventory->last_query());
exit;
}
}
$result = array("records" => $data_respon['records']);
$resp[] = $result;
// $this->sys_ok($result);
}
} else {
// $this->sys_error($array_rst);
// exit;
$resp[] = $array_rst;
}
}
} else {
$result = array("message" => 'No Data');
$this->sys_ok($result);
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
// $this->db_inventory->trans_rollback();
}
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;
}
}