<?php

/**
 * this script will call a script at main server which will fill a data file containing every record uploaded online
 * then here we must insert them but important note that records to be downloaded for branch database must be empty 
 * so we must empty it before start restore..
 */
//
require_once 'common.php';
$__data_type = 'rstr';
require_once 'data__db_from_arr_functions.php';
//
if (!check_ping_ip())
    die('Error, server is offline');
//
if (empty($_GET['is_confirm']))
    die("You must empty database first [invoice, payments, deposit, cash drawer, pos customers, transfers, ..]<br/>
        After that call the 'branch__data_from_server.php' to download data like accounts, customers, uesrs,..<br/>
		Also what about if there is multi year id??<br/>
        if all is ok, and want to continue, <<<<a href='" . extend_debug_url('?is_confirm=1') . "'>Click Here</a>>>><br/>");
//
$branch_id = get_settings('default_branch_id');
if (empty($branch_id))
    die('Error, Undefined Branch ID!');
//
$depot_id = get_settings('default_depot');
if (empty($depot_id))
    die('Error, Undefined Default Depot ID!');
//
$server_data_link = extend_debug_url(SERVER_trusto_URL . "sync/server__restore_data_to_branch.php?branch_id={$branch_id}&depot_id={$depot_id}");
//
$result_arr = sync_get_contents($server_data_link);
$result_arr = json_decode($result_arr, true);
if (empty($result_arr['zip_file']))
    die('Error, No Zip File Path!');
//
$local_zip_file = __DIR__ . '/data_files/temp_dir/' . basename($result_arr['zip_file']);
server_ftp_get_file($result_arr['zip_file'], $local_zip_file);
server_ftp_close();
//remove the zip file from server, no need for it anymore
$result_msg = sync_get_contents($server_data_link . "&is_finish_zip=1&path=" . urlencode($result_arr['zip_file']));
//
fill_trusto_session();
//
$zip = new ZipArchive();
if ($zip->open($local_zip_file) === true) {
    $tmp_data_file = dirname($local_zip_file) . '/json_array_data.smc_srvr_rstr_data';    //$zip->getNameIndex(0);
    $zip->extractTo(dirname($local_zip_file));
    $zip->close();
    //
    $data = json_decode(file_get_contents($tmp_data_file), true);
    if (isset($data['branch_id']) && $data['branch_id'] == $branch_id) {
        //insert customers first since offline/online customer ids used in insert invoices below
        $tmp_cust_ids = insert_db_frm_arr__pos_customers($branch_id, $data['pos_customers']);
        //
        $tmp_inv_ids = insert_db_frm_arr__invoices($branch_id, $data['invoices'], $data['onlin_oflin_cstmr_ids']);
        //
        $tmp_ret_ids = insert_db_frm_arr__return_invoices($branch_id, $data['return']);
        //
        if (!empty($data['depot_id']))
            $tmp_trnsfr_ids = insert_db_frm_arr__transfers($branch_id, $data['depot_id'], $data['transfers']);
        //
        $tmp_pay_rec_ids = insert_db_frm_arr__payments_receipts($branch_id, $data['pay_rec'], $data['onlin_oflin_cstmr_ids']);
        //
        $tmp_dep_with_ids = insert_db_frm_arr__deposit_withdrawal($branch_id, $data['dep_with']);
        //
        $tmp_cash_drawer_ids = insert_db_frm_arr__cashdrawer($branch_id, $data['cash_drawer']);
    }
    //remove the zip & sql file no need for them anymore
    unlink($local_zip_file);
    unlink($tmp_data_file);
}
//
$msg = 'Data Restored from the server, received by the branch and inserted successfully..';
create_sync_log_file('brnch_rstr_frm_srvr', $msg, $branch_id);
die($msg);
//