80 lines
2.2 KiB
PHP
80 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
class InventoryManager
|
|
{
|
|
protected $api_url;
|
|
protected $api_ocp_key;
|
|
protected $api_auth_prefix;
|
|
protected $api_auth_token;
|
|
|
|
public function __construct($api_url, $api_ocp_key, $api_auth_prefix, $api_auth_token)
|
|
{
|
|
$this->api_url = $api_url;
|
|
$this->api_ocp_key = $api_ocp_key;
|
|
$this->api_auth_prefix = $api_auth_prefix;
|
|
$this->api_auth_token = $api_auth_token;
|
|
}
|
|
|
|
public function getBranchesInventory($nearest_branch_codes, $skus)
|
|
{
|
|
// api call to check inventory
|
|
// pass the list of branch codes and the skus
|
|
$data = [
|
|
"BranchCodes" => $nearest_branch_codes,
|
|
"Skus" => $skus,
|
|
];
|
|
|
|
$json_data = json_encode($data);
|
|
// error_log('JSON ' . $json_data);
|
|
|
|
// initializes a new cURL session
|
|
$curl = curl_init($this->api_url);
|
|
|
|
// Set the CURLOPT_RETURNTRANSFER option to true
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
// Set the CURLOPT_POST option to true for POST request
|
|
curl_setopt($curl, CURLOPT_POST, true);
|
|
|
|
// Set the request data as JSON using json_encode function
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data);
|
|
|
|
// set timeout
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 300);
|
|
|
|
$authorization = $this->api_auth_prefix . ' ' . $this->api_auth_token;
|
|
$ocp = $this->api_ocp_key;
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, [
|
|
'Authorization: ' . $authorization,
|
|
'Content-Type: application/json',
|
|
'Ocp-Apim-Subscription-Key: ' . $ocp,
|
|
]);
|
|
|
|
$response = curl_exec($curl);
|
|
|
|
// close cURL session
|
|
curl_close($curl);
|
|
|
|
// response is array of these
|
|
// {
|
|
// "Id": 37,
|
|
// "BranchCode": "WestAve",
|
|
// "BranchName": "West ave. Branch",
|
|
// "BranchRole": 0,
|
|
// "Name": "3SMF / GOLD ",
|
|
// "SapCode": "WMGD31EL-CPNM0-L",
|
|
// "Quantity": 38
|
|
// }
|
|
|
|
// check if the response is empty
|
|
$results = [];
|
|
if (!empty($response))
|
|
$results = json_decode($response, true);
|
|
|
|
return $results;
|
|
}
|
|
|
|
}
|