<?php

include __DIR__ . '/api_constant.php';
require_once dirname(__DIR__) . '/autoload.smart.php';
require_once LIBS_PATH . 'Common/db_standalone_class.php';
$db = new db_standalone(null, DB_HOST, DB_USER, DB_PASS, DB_NAME);

function send_post($post_url, $post_arr, $token = null) {
    $postdata = '';
    if (!empty($post_arr))
        $postdata = http_build_query($post_arr);
    //
    $header_arr = array(
        'Content-type: application/x-www-form-urlencoded',
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng',
        'Accept-Language: en-US,en;q=0.9,ar;q=0.8',
        'Cache-Control: max-age=0',
        'Host: ' . parse_url($post_url, PHP_URL_HOST),
        'Origin: ' . parse_url($post_url, PHP_URL_HOST),
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
    );
    if (!is_null($token))
        $header_arr[] = "Authorization: Bearer " . $token;
    //some times in some local server when try file get contents on large data or time it return failed for that if curl is activated we use it first
    if (extension_loaded('curl')) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $post_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if (!empty($postdata)) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        }
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header_arr);
        $contents = curl_exec($ch);
        curl_close($ch);
    } else { //ini_get('allow_url_fopen')
        $opts = array('http' =>
                      array(
                          'method' => (empty($postdata) ? 'GET' : 'POST'),
                          'header' => implode("\r\n", $header_arr),
                          'content' => $postdata
                      )
                     );
        $context = stream_context_create($opts);
        $contents = file_get_contents($post_url, false, $context);
    }
    return $contents;
}

function set_track_data($last_track_result) {
    $other_response = 'Description: ' . $last_track_result->UpdateDescription . ', DateTime: ' . $last_track_result->UpdateDateTime . ', Location: ' . $last_track_result->UpdateLocation . (!empty($last_track_result->Comments) ? (', Comments: ' . $last_track_result->Comments) : '');
    $current_status = 0;
    if ($last_track_result->UpdateCode == 'SH014' || $last_track_result->UpdateCode == 'SH047' || $last_track_result->UpdateCode == 'SH160')
        $current_status = 2;
    if ($last_track_result->UpdateCode == 'SH003')
        $current_status = 3;
    if ($last_track_result->UpdateCode == 'SH005')
        $current_status = 6;
    if ($last_track_result->UpdateCode == 'SH069')
        $current_status = 4;
    if ($last_track_result->UpdateCode == 'SH008' ||  $last_track_result->UpdateCode == 'SH294')
        $current_status = 10;
    if ($last_track_result->UpdateCode == 'SH164')
        $current_status = 12;
    if ($last_track_result->UpdateCode == 'SH295')
        $current_status = 9;
    if ($last_track_result->UpdateCode == 'SH583')
        $current_status = 11;
    if ($last_track_result->UpdateCode == 'SH154')
        $current_status = 13;
    if ($last_track_result->UpdateCode == 'SH033')
        $current_status = 14;
    if ($last_track_result->UpdateCode == 'SH249')
        $current_status = 15;
    $data = ['other_response' => $other_response,
             'current_status' => $current_status];
    return $data;
}

function getTotalItems($invoice_id, $db) {
    $query = $db->query_execute('select sum(Item_Quantity_Current) as total_item_qty from tbl_invoice_products_details 
                where Invoice_ID_Auto = "' . $invoice_id . '" group by Invoice_ID_Auto');
    $row = $db->query_fetch($query);
    return $row['total_item_qty'];
}
