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

159 lines
6.1 KiB
PHP

<?php
class Uploadresponstatusv1test 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 requeststatusrespon.*, M_BranchIPAddress as branch_ip
FROM `requeststatusrespon`
JOIN `companyaddress` ON RequestStatusResponCompanyAddressID = CompanyAddressID
JOIN `company` ON CompanyAddressCompanyID = CompanyID
JOIN $this->db_onex.`m_branch` ON CompanyM_BranchID = M_BranchID
WHERE
RequestStatusResponIsUploaded = 'N'";
//echo $sql;
$qry = $this->db_inventory->query($sql);
if (!$qry) {
$this->sys_error_db("select requeststatusrespon error", $this->db_inventory->last_query());
exit;
}
$data_get = $qry->result_array();
// print_r($data_get);
// exit;
$resp = array();
if ($data_get) {
foreach ($data_get as $key => $val) {
//API URL
$url = "http://" . $val['branch_ip'] . "/one-api/inventory/requestreceive/downloadresponstatusv1/download_data";
$data = array($val);
// print_r($val['RequestStatusResponID']);
// exit;
$ch = curl_init($url);
# Setup request to send json via POST.
$payload = json_encode($data);
// print_r($payload);
$payloadZ = gzcompress($payload);
// print_r($payloadZ);
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);
// print_r($result);
curl_close($ch);
# 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($array_rst);
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 requeststatusrespon SET
RequestStatusResponIsUploaded = 'Y',
RequestStatusResponSentTime = NOW()
WHERE RequestStatusResponID = ?";
$qry = $this->db_inventory->query($sql, [$value]);
if (!$qry) {
$this->sys_error_db("update requeststatusrespon RequestStatusResponIsUploaded = Y error", $this->db_inventory->last_query());
exit;
}
}
// $result = array("records" => $data_respon['records']);
// $this->sys_ok($result);
$arr_rsp = array(
'Branch_IP' => $val['branch_ip'],
'ID' => $val['RequestStatusResponID'],
'Response' => $array_rst
);
$resp[] = $arr_rsp;
}
} else {
$arr_rsp = array(
'Branch_IP' => $val['branch_ip'],
'ID' => $val['RequestStatusResponID'],
'Response' => $array_rst
);
$resp[] = $arr_rsp;
// $this->sys_error($array_rst);
// exit;
}
}
$this->sys_ok($resp);
} 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;
}
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));
}
}