<?php
require_once dirname(__DIR__) . '/../common.php';

$soapClient = new SoapClient(SITE_LINK.'shipping_api/aramex/aramex/tracking.wsdl');
$shipping_company_query = $db->query_execute('select * from tbl_shipping_company where api_folder = "aramex" ');
$shipping_company_data = $db->query_fetch($shipping_company_query);
$shipping_account_data = json_decode($shipping_company_data['note'],true);
$error_message = '';

$invoices_to_be_updated = array();
$invoice_ids_array =  array('44885373330');
//$invoice_ids_array =  array('44885306126','31117754566','44885373330');
echo '<pre>';
$params = array(
    'ClientInfo'  			=> array(
        'AccountCountryCode'=> 'LB',
        'AccountEntity'		=> $shipping_account_data['AccountEntity'],
        'AccountNumber'		=> $shipping_account_data['AccountNumber'],
        'AccountPin'		=> $shipping_account_data['AccountPin'],
        'UserName'			=> $shipping_company_data['user_name'],
        'Password'		 	=> $shipping_company_data['password'],
        'Version'		 	=> $shipping_company_data['api_version']
    ),
    'Transaction' 			=> array(
        'Reference1'			=> '001',
    ),
    'Shipments'				=>$invoice_ids_array
);
try {
    $auth_call = $soapClient->TrackShipments($params);
    $tmp = (array)$auth_call->TrackingResults;
    if(!$auth_call->HasErrors){
        if(!empty($tmp)){
            if(count($invoice_ids_array) == 1){
                $last_track_result = $auth_call->TrackingResults->KeyValueOfstringArrayOfTrackingResultmFAkxlpY->Value->TrackingResult[0];
                $track_result = set_track_data($last_track_result);
                $invoices_to_be_updated[] =  $track_result;
            }else{
                $shippments_data = $auth_call->TrackingResults->KeyValueOfstringArrayOfTrackingResultmFAkxlpY;
                foreach($shippments_data as $current_shippment){
                    $last_track_result = $current_shippment->Value->TrackingResult[0];
                    $track_result = set_track_data($last_track_result);
                    $invoices_to_be_updated[] =  $track_result;
                }
            }
        }
    }else{
        if(!empty($auth_call->Notifications->Notification->Message)){
            $error_message = $auth_call->Notifications->Notification->Message;
        }else{
            if(!empty($tmp)){
                foreach($auth_call->Notifications->Notification as $current_errors){
                    $error_message .= ($error_message == '' ? '' : ',').$current_errors->Message;
                }
            }
        }
    }
    print_r($auth_call);
} catch (SoapFault $fault) {
    die('Error : ' . $fault->faultstring);
}
echo $error_message.'<br>';
print_r($invoices_to_be_updated);
?>