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

107 lines
4.0 KiB
PHP

<?php
class Uploadtocenter 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 mutasirequestrespon.*, M_BranchIPAddress as branch_ip,
M_BranchID
FROM `mutasirequestrespon`
JOIN `companyaddress` ON MutasiRequestResponToCompanyAdddressID = CompanyAddressID
JOIN `company` ON CompanyAddressCompanyID = CompanyID
JOIN $this->db_onex.`m_branch` ON CompanyM_BranchID = M_BranchID
WHERE
MutasiRequestResponIsUploaded = 'N'";
//echo $sql;
$qry = $this->db_inventory->query($sql);
if (!$qry) {
$this->sys_error_db("select mutasitransitdetail error", $this->db_inventory->last_query());
exit;
}
$data_get = $qry->result_array();
if ($data_get) {
//API URL
$url = "http://" . $data_get[0]['branch_ip'] . "/one-api/inventory/receivemutasi/downloadfrombranch/download_data";
//echo $url;
$data = $qry->result_array();
$ch = curl_init($url);
# Setup request to send json via POST.
$payload = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . strlen($payload)
)
);
# Return response instead of printing.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# Send request.
$result = curl_exec($ch);
curl_close($ch);
# Print response.
$array_rst = $this->objToArray(json_decode($result));
//print_r($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 mutasirequestrespon SET
MutasiRequestResponIsUploaded = 'Y'
WHERE MutasiRequestResponID = ?";
$qry = $this->db_inventory->query($sql, [$value]);
if (!$qry) {
$this->sys_error_db("update mutasirequestrespon MutasiRequestResponIsUploaded = Y error", $this->db_inventory->last_query());
exit;
}
}
$result = array("records" => $data_respon['records']);
$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;
}
}