<?php
require_once dirname(__DIR__) . '/common.php';
$soapClient = new SoapClient(SITE_LINK.'shipping_api/aramex/shipping-services.wsdl');
//'C0849C3';
echo '<pre>';
//var_dump($soapClient);
print_r($soapClient->__getFunctions());
$invoices_array = array();
$shippment_added = $shippment_not_added = $invoices_pick_up_added = $invoices_pick_up_not_added = array();
$invoices_to_be_updated = array();
$shipping_company_query = $db->query_execute('select * from tbl_shipping_company where shipping_company_id = "'.$_POST['shipping_company_id'].'"');
$shipping_company_data = $db->query_fetch($shipping_company_query);
$shipping_account_data = json_decode($shipping_company_data['note'],true);

$query_invoices = $db->query_execute('select inv.Invoice_ID_Auto,
                                             concat_ws("",inv.invoice_prefix,inv.Invoice_ID) as invoice_nb,
                                             concat_ws("",inv.invoice_prefix,inv.Invoice_ID) as Reference,
                                             (inv.Invoice_Shipping+ inv.Invoice_Net_Total) as Collection,
                                             inv.Invoice_Currency,
                                             cust_cities.Name as customer_city_name,
                                             cust.Customer_ID,
                                             cust.Customer_Name,
                                             address.shipping_street as customer_street,
                                             address.shipping_building  as customer_building,
                                             address.shipping_address as Customer_Address,
                                             cust.Customer_Mobile,
                                             cust.Customer_Lat,
                                             cust.Customer_Lng,
                                             address.district_id as customer_district,
                                             shipping.pick_up_date,
                                             shipping.drop_off_date,
                                             shipping.shipping_status_id,
                                             shipping.api_shipping_id as DBNumber,
                                             shipping.api_other_response
                                      from tbl_invoices inv
                                      inner join tbl_customers cust on cust.Customer_ID = inv.Customer_ID
                                      left join tbl_customer_shipping_address address on address.shipping_address_id =  inv.address_id
                                      inner join tbl_all_cities cust_cities on cust_cities.ID = address.city_id
                                      left join tbl_invoice_shipping shipping on shipping.Invoice_ID_Auto = inv.Invoice_ID_Auto
                                      where inv.Invoice_ID_Auto in ('.implode(",",$_POST['invoices']).')
                                      and (shipping.api_shipping_id is null or  shipping.api_shipping_id <> 0)
                                      ');
while ($invoice_row = $db->query_fetch($query_invoices)){
    $invoices_array[$invoice_row['Invoice_ID_Auto']] = $invoice_row;
}
$client_info = array(
    'AccountCountryCode'	=>$shipping_account_data['AccountCountryCode'],
    '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'
);
foreach($invoices_array as $invoice){
    $error_message = '';
    try {
        $parameters = array();
        $parameters['ClientInfo'] = $client_info;
        $parameters['Transaction'] = $transaction;
        $auth_call = $soapClient->FetchCities($parameters);// fetch cities 
        //        if(!$auth_call->HasErrors){
        //            $shippment_added = $invoice['Invoice_ID_Auto'];
        //            $invoices_to_be_updated[$invoice['Invoice_ID_Auto']] = $invoice;
        //        }else{
        //            if(is_array($auth_call->Notifications->Notification)){
        //                foreach($auth_call->Notifications->Notification as $errors){
        //                    $error_message .= ($error_message == '' ? '' : ',').$errors->Message;
        //                }
        //            }else{
        //                $error_message =  $auth_call->Notifications->Notification->Message;
        //            }
        //            $shippment_not_added[$invoice['Invoice_ID_Auto']] = $error_message. '(For invoice: '.$invoice['invoice_nb'].')';
        //        }
        echo '<pre>';
        print_r($auth_call);
    } catch (SoapFault $fault) {
        $shippment_not_added[$invoice['Invoice_ID_Auto']] =  $fault->faultstring. '(For invoice: '.$invoice['invoice_nb'].')';
    }
} 
if(empty($invoices_to_be_updated))
    die(json_encode(array('error_type'=>'danger','error_message'=>implode("<br>",$shippment_not_added))));
//foreach($invoices_to_be_updated as $invoices){
//    $updated_invoices =  $db->query_execute('Insert into tbl_invoice_shipping (Invoice_ID_Auto,pick_up_date,drop_off_date,shipping_status_id,api_shipping_id)
//    VALUES(?,?,?,2,?)',[&$invoices['Invoice_ID_Auto'],&$invoices['PickUpDate'],
//                        &$invoices['DropOffDate'],&$invoices['DBNumber']]);
//}
//$message = '';
//$message .= !empty($invoices_drop_off_not_added) ? ('Not Dropped'.'<br>'.(implode("<br>",$invoices_drop_off_not_added))) : '';
//$message .= !empty($invoices_pick_up_not_added) ? ('Not Picked'.'<br>'.(implode("<br>",$invoices_pick_up_not_added))) : '';
//die(json_encode(array('error_type'=>'success','error_message'=>CommonLanguage::getMessage('Submitted_Successfully').'<br>'.$message)));




