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() { 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(); if($data_get){ //API URL $url="http://".$data_get[0]['branch_ip']."/one-api/inventory/requestreceive/setupfromcenter/download_data"; $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 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']); $this->sys_ok($result); } } } } catch (Exception $exc) { $message = $exc->getMessage(); $this->sys_error($message); } } 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; } }