<?php
session_start();
set_time_limit(10000);
require('Connections/remote/courier.php');
$procedure = 'PULL_REQUISITION_ORDERS';
//CHECK THE LAST ID ON THE LOCAL SERVER.
list($last_update_id_info, $totalRows) = fetchDataOne($conn_local, "pull_logs", array('*'), array('process' => $procedure), ' ORDER BY id DESC LIMIT 1');
$last_update_id = $last_update_id_info['lastpullid'];

if ($last_update_id < 1) {
    $last_update_id = 0;
}

//FETCH LOCAL DATA
$i = 1;
$purchase_order_nos = array();
list($getLocalTransferOrders, $totalRowsLocal) = fetchDataMore($conn_remote, 'requisition_orders', array('*'), NULL, "WHERE order_status <> 'Not Saved' AND id>$last_update_id AND branch_id <> '" . $_SESSION['branch_id'] . "' AND (supplier='" . $_SESSION['branch_id'] . "' OR delivery_location='" . $_SESSION['branch_id'] . "') ORDER BY id ASC");
if ($totalRowsLocal > 0) {
    $querylist = "";
    foreach ($getLocalTransferOrders as $key => $getLocalTransferOrder) {
        $pullid = $getLocalTransferOrder['id'];
        $purchase_order_no = $getLocalTransferOrder['purchase_order_no'];
        $supplier = $getLocalTransferOrder['supplier'];
        $supplier_address = $getLocalTransferOrder['supplier_address'];
        $date_of_order = $getLocalTransferOrder['date_of_order'];
        $delivery_location = $getLocalTransferOrder['delivery_location'];
        $delivery_address = $getLocalTransferOrder['delivery_address'];
        $expected_date = $getLocalTransferOrder['expected_date'];
        $supplied_date = $getLocalTransferOrder['supplied_date'];
        $supplied_by = $getLocalTransferOrder['supplied_by'];
        $supplied_by_user_name = $getLocalTransferOrder['supplied_by_user_name'];
        $supplier_memo = $getLocalTransferOrder['memo'];
        $order_status_from = $getLocalTransferOrder['order_status_from'];
        $order_status = $getLocalTransferOrder['order_status'];
        $user_ipaddress = $getLocalTransferOrder['user_ipaddress'];
        $user_id = $getLocalTransferOrder['user_id'];
        $user_name = $getLocalTransferOrder['user_name'];
        $delivery_memo = $getLocalTransferOrder['delivery_memo'];
        $approved_by = $getLocalTransferOrder['approved_by'];
        $approved_by_from = $getLocalTransferOrder['approved_by_from'];
        $approved_by_user_name_from = $getLocalTransferOrder['approved_by_user_name_from'];
        $approved_date_from = $getLocalTransferOrder['approved_date_from'];
        $approved_by_user_name = $getLocalTransferOrder['approved_by_user_name'];
        $approved_date = $getLocalTransferOrder['approved_date'];
        $accepted_by = $getLocalTransferOrder['accepted_by'];
        $accepted_by_username = $getLocalTransferOrder['accepted_by_username'];
        $accepted_date = $getLocalTransferOrder['accepted_date'];
        $status_update_server_time = $getLocalTransferOrder['status_update_time'];

        $data = array(
            'branch_id' => $getLocalTransferOrder['branch_id'],
            'id_from_branch' => $getLocalTransferOrder['id_from_branch'],
            'purchase_order_no'=>$purchase_order_no,
            'supplier'=>$supplier,
            'supplier_address'=>$supplier_address,
            'date_of_order'=>$date_of_order,
            'delivery_location'=>$delivery_location,
            'delivery_address'=>$delivery_address,
            'expected_date'=>$expected_date,
            'supplied_date'=>$supplied_date,
            'supplied_by'=>$supplied_by,
            'supplied_by_user_name'=>$supplied_by_user_name,
            'memo'=>$supplier_memo,
            'order_status'=>$order_status,
            'order_status_from'=>$order_status_from,
            'user_ipaddress'=>$user_ipaddress,
            'user_id'=>$user_id,
            'user_name'=>$user_name,
            'delivery_memo'=>$delivery_memo,
            'approved_by'=>$approved_by,
            'approved_by_from'=>$approved_by_from,
            'approved_by_user_name'=>$approved_by_user_name,
            'approved_by_user_name_from'=>$approved_by_user_name_from,
            'approved_date'=>$approved_date,
            'approved_date_from'=>$approved_date_from,
            'accepted_by'=>$accepted_by,
            'accepted_by_username'=>$accepted_by_username,
            'accepted_date'=>$accepted_date,
            'status_update_time'=>  $status_update_server_time
        );

        $querylist = $querylist . InsertStatementSqlString('requisition_orders', $data);
        if (fmod($i, 100) == 0) {
            $query_rsodest = $querylist;
            mysqli_multi_query($conn_local, $query_rsodest) or die(mysqli_error($conn_local));
            $querylist = "";
        }
        $i++;

        $purchase_order_nos[] = $purchase_order_no;
    }

    $query_rsodest = $querylist;
    mysqli_multi_query($conn_local, $query_rsodest) or die(mysqli_error($conn_local));
    $querylist = "";


    $data = array(
        'process' => $procedure,
        'pulltime' => gmdate('Y-m-d h:i:s'),
        'lastpullid' => $pullid,
        'branch_id' => $_SESSION['branch_id'],
    );

    $conn_local = mysqli_connect($hostname_courier2, $username_courier2, $password_courier2,$database_courier2) or trigger_error(mysql_error(),E_USER_ERROR);
    $updatelist = InsertStatementSqlString('pull_logs', $data);
    mysqli_multi_query($conn_local, $updatelist . $updatelist2) or die(mysqli_error($conn_local));
    mysqli_close($conn_local);

    $ii = 1;
    if (count($purchase_order_nos) > 0) {
        $conn_remote = mysqli_connect($hostname_courier, $username_courier, $password_courier,$database_courier) or trigger_error(mysql_error(),E_USER_ERROR); 
        foreach ($purchase_order_nos as $purchase_order_no) {
            list($getLocalTransferOrdersdetails, $totalRowsLocaldetails) = fetchDataMore($conn_remote, 'requisition_order_details', array('*'), array('purchase_order_no' => $purchase_order_no));
            if (count($getLocalTransferOrdersdetails) > 0) {
                foreach ($getLocalTransferOrdersdetails as $key => $getLocalTransferOrdersdetail) {
                    $dataDetails = array(
                        'purchase_order_no' => $purchase_order_no,
                        'id_from_branch' => $getLocalTransferOrdersdetail['id_from_branch'],
                        'ins_item_code' => $getLocalTransferOrdersdetail['ins_item_code'],
                        'alternate_lookup' => $getLocalTransferOrdersdetail['alternate_lookup'],
                        'item_id' => $getLocalTransferOrdersdetail['item_id'],
                        'item' => $getLocalTransferOrdersdetail['item'],
                        'service' => $getLocalTransferOrdersdetail['service'],
                        'qty_expected' => $getLocalTransferOrdersdetail['qty_expected'],
                        'qty_received' => $getLocalTransferOrdersdetail['qty_received'],
                        'received_price' => $getLocalTransferOrdersdetail['received_price'],
                        'date_expected' => $getLocalTransferOrdersdetail['date_expected'],
                        'datetime_received' => $getLocalTransferOrdersdetail['datetime_received'],
                        'item_order_status' => $getLocalTransferOrdersdetail['item_order_status'],
                        'reason' => $getLocalTransferOrdersdetail['reason'],
                        'date_time' => $getLocalTransferOrdersdetail['date_time'],
                        'status_update_time' => $getLocalTransferOrdersdetail['status_update_time']
                    );

                    $querylist = $querylist . InsertStatementSqlString('requisition_order_details', $dataDetails);
                    if (fmod($ii, 100) == 0) {
                        $conn_local = mysqli_connect($hostname_courier2, $username_courier2, $password_courier2,$database_courier2) or trigger_error(mysql_error(),E_USER_ERROR); 
                        $query_rsodest = $querylist;
                        mysqli_multi_query($conn_local, $query_rsodest) or die(mysqli_error($conn_remote));
                        $querylist = "";
                        mysqli_close($conn_local);
                    }
                    $ii++;
                }
            }

            $query_rsodest = $querylist;
            $conn_local = mysqli_connect($hostname_courier2, $username_courier2, $password_courier2,$database_courier2) or trigger_error(mysql_error(),E_USER_ERROR);
            mysqli_multi_query($conn_local, $query_rsodest) or die(mysqli_error($conn_remote));
            $querylist = "";
            mysqli_close($conn_local);
        }
    }
    
     mysqli_close($conn_remote);
}


