<?php 
require_once dirname(__DIR__) . '/common.php';
//$from_address = 'MEG123456';
$customers_array = $invoices_array = array();
$customers_added = $customers_not_added = array();
$invoices_drop_off_added = $invoices_drop_off_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 = "5"');
$shipping_company_data = $db->query_fetch($shipping_company_query);
if(empty($shipping_company_data['token']))
    die(json_encode(array('error_type'=>'danger','error_message'=>'Unable to connect to the current api')));
if(empty($shipping_company_data['note']))
    die(json_encode(array('error_type'=>'danger','error_message'=>'Missing Data in note column')));
$shipping_from_address_data = json_decode($shipping_company_data['note'],true);
if(empty( $shipping_from_address_data['reference']))
    die(json_encode(array('error_type'=>'danger','error_message'=>'Missing From Address')));
$from_address = $shipping_from_address_data['reference'];
$api_version = !empty($shipping_company_data['api_version']) ? $shipping_company_data['api_version'] : 'v1';
$current_token = $shipping_company_data['token'];
$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_title as shipping_title,
                                             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 (1)
                                      ');
while ($invoice_row = $db->query_fetch($query_invoices)){
    if($invoice_row['Invoice_Currency'] != 84)
        $invoice_row['Collection'] = CurrencyFactory::returnExchangedValueBetweenTwoCurrencies($invoice_row['Invoice_Currency'],84,$invoice_row['Collection']); 
    $invoices_array[$invoice_row['Invoice_ID_Auto']] = $invoice_row;
    $customers_array[$invoice_row['Customer_ID']] = ['CityName'=>$invoice_row['customer_city_name'],
                                                     'CustomerName'=>$invoice_row['Customer_Name'],
                                                     'DistrictID'=>$invoice_row['customer_district'],
                                                     'Bldg'=>$invoice_row['customer_building'],
                                                     'Street'=>$invoice_row['customer_street'],
                                                     'AddressTitle'=>$invoice_row['shipping_title'],
                                                     'Phone'=>$invoice_row['Customer_Mobile'],
                                                     'NearbyLandmark'=>$invoice_row['Customer_Address'],
                                                     //                                                     'Latitude'=>$invoice_row['Customer_Lat'],
                                                     //                                                     'Longitude'=>$invoice_row['Customer_Lng'],
                                                     'Customer_ID'=>$invoice_row['Customer_ID'],
                                                     'Reference'=>'CUST'.$invoice_row['Customer_ID'],
                                                    ];
}
$address_link = SERVER_FASTPAX_URL.$api_version.'/address';
foreach($customers_array as $customer){
    $address_exist =send_post($address_link.'/get',$customer,$current_token);
    $response_address_exist= json_decode($address_exist);
    if(empty($response_address_exist->Data) && $response_address_exist->Status == 0){
        $address_add = send_post($address_link.'/create',$customer,$current_token);
        $response_address_add = json_decode($address_add);
        if($response_address_add->Status == 1){
            $customers_added[] = $customer['Customer_ID'];
        }else{
            if(!empty($response_address_add->Data->Errors))
                $error_message = implode("",$response_address_add->Data->Errors);
            else
                $error_message = $response_address_add->ErrorMessage;
            $customers_not_added[$customer['Customer_ID']] = $error_message.' (For Customer: '.$customer['CustomerName'].')';
        }
    }else{
        $address_edit = send_post($address_link.'/edit',$customer,$current_token);
        $response_address_edit = json_decode($address_edit);
        if($response_address_edit->Status == 1){
            $customers_added[] = $customer['Customer_ID'];
        }else{
            if(!empty($response_address_edit->Data->Errors))
                $error_message = implode("",$response_address_edit->Data->Errors);
            else
                $error_message = $response_address_edit->ErrorMessage;
            $customers_not_added[$customer['Customer_ID']] = $error_message.' (For Customer: '.$customer['CustomerName'].')';
        }
    } 
}
if(!empty($customers_not_added))
    die(json_encode(array('error_type'=>'danger','error_message'=>implode("<br>",$customers_not_added))));
