<?php

class main_pos extends mainController {

    public function __construct() {
        parent::__construct();
        $this->load->model('admin');
    }

    public function landing() {
        preg_match("/\/act\b/i", $_SERVER['REQUEST_URI'], $match_act);
        $vars['is_actual'] = count($match_act) > 0 ? 1 : 0;
        $vars['pageName'] = 'POS';
        $vars['error'] = false; //no error, stay on page; if true redirect to 404
        $vars['title'] = 'POS';
        $vars['bread_crumb_title'] = 'POS';
        $vars['bread_crumb_first_level'] = 'Sales Invoice';
        $vars['empty_collected_points'] = $this->_registry->Product->empty_collected_points();

        $vars['invoice_details'] = array();
        $url = $_SERVER['REQUEST_URI'];
        $result = explode('/', $url);
        $vars['destination'] = $result[count($result) - 1];
        if (ctype_digit($vars['destination'])) {
            $current_table = $this->_registry->restaurantpos->getMainTable($vars['destination']);
            $vars['destination'] = $current_table;
            $table_data = CommonFunctions::CommonView('tbl_table', ['table_id'], [$vars['destination']])[0];
            $vars['table_name'] = (!empty($table_data['table_alias_name']) && smc_session()['default_setting']['activate_table_alias'] == 1) ? $table_data['table_alias_name'] : $table_data['table_name'];
            $vars['invoice_details'] = $this->_registry->restaurantpos->getInvoiceByTable($vars['destination']);
//            echo '<pre>';
//            print_r($vars['invoice_details']);
        } else {
            $vars['destination'] = 0;
        }
        $vars['cash_drawer_logout'] = $this->_registry->CommonACC->getCashDrawerLogoutData();


        $vars['date'] = Date::getToday();
        $vars['local_currency_arr'] = array(smc_session()['local_currency'], smc_session()['local_currency_symbol'], smc_session()['local_currency_code'], smc_session()['local_currency_round']);

        $CurrencyRow = $this->_registry->currency->getCurrencyRow(smc_session()['pricing_class_currency']);
        $CurrencyID = $CurrencyRow['Currency_ID'];
        $CurrencyCode = $CurrencyRow['Currency_Code'];
        $CurrencySymbol = $CurrencyRow['Currency_Symbol'];
        $vars['PrimaryCurrency'] = array($CurrencyID, $CurrencySymbol, $CurrencyCode, $CurrencyRow['currency_round']);
        $CurrencyID2 = smc_session()['second_currency']['id'];
        $CurrencyCode2 = smc_session()['second_currency']['code'];
        $CurrencySymbol2 = smc_session()['second_currency']['symbol'];
        $vars['SecondCurrency'] = array($CurrencyID2, $CurrencySymbol2, $CurrencyCode2);
//        die(print_r($vars['SecondCurrency']));
        $vars['default_cat_id'] = 0;
        if ((!empty(smc_session()['user_accounts']['pos_cat_id']) && smc_session()['user_accounts']['pos_cat_id']))
            $vars['default_cat_id'] = smc_session()['user_accounts']['pos_cat_id'];
        elseif ($vars['destination'] == 0 && !empty(smc_session()['default_cat_delivery']))
            $vars['default_cat_id'] = smc_session()['default_cat_delivery'];
        elseif ($vars['destination'] != 0 && !empty(smc_session()['default_cat_table']))
            $vars['default_cat_id'] = smc_session()['default_cat_table'];
        $vars['categories'] = adminModel::getcategoriesPOS($vars['default_cat_id']);

        preg_match("/\/order\b/i", $_SERVER['REQUEST_URI'], $match_order);
        $vars['IsOrder'] = count($match_order) > 0 ? true : false;
//        echo"<pre>";
//        die(print_r($vars['categories']));
        $vars['Extable'] = $this->_registry->currency->getExchangeRateTable();
        $vars['all_uom'] = $this->_registry->material->getMaterialUOM();
        $vars['all_users'] = CommonFunctions::CommonView('tbl_common_users', ['Status_ID'], ['1']);
        $vars['kitchen_notes'] = CommonFunctions::CommonView('tbl_kitchen_note', ['type'], [0]);
        $vars['invoice_notes'] = CommonFunctions::CommonView('tbl_kitchen_note', ['type'], [1]);
        $vars['postponed_notes'] = CommonFunctions::CommonView('tbl_kitchen_note', ['type'], [2]);
        $vars['product_notes'] = CommonFunctions::CommonView('tbl_product_note');
        $vars['classes'] = $this->_registry->Product->getClasses();
        $vars['active_cat'] = $this->_registry->Product->getProductsCat(0, 1);

        $vars['cash_cat'] = $this->_registry->jv->getCashCategoriesByCurrency($CurrencyID);

        $today = Date::gettoday();
        $vars['year'] = date("Y", strtotime($today)); // get the year from today
        $vars['month'] = date("m", strtotime($today)); // get the month from today
        $vars['monthName'] = date("F", mktime(0, 0, 0, $vars['month'], 10)); // get Month Name
        $vars['money_tags'] = $this->_registry->jv->viewCashCategories();

        $vars['suppliers'] = $this->_registry->vendor->setData(array('Status_ID' => 1))->viewSuppliersForAutoComplete();
        $vars['CurList'] = CurrencyFactory::GetLIListCurrency();
        $vars['voidReasonList'] = $this->_registry->Product->getVoidInvoiceReason();
        $vars['Org_Type'] = 1; //Session::getOrgInvoiceType();
        $vars['functions_all'] = true;
        $vars['pos_fields_to_hide'] = smc_session()['default_setting']['pos_fields_to_hide'] != '' ? explode(",", smc_session()['default_setting']['pos_fields_to_hide']) : [];
//        echo"<pre>";
//        echo smc_session()['default_setting']['pos_fields_to_hide'].'==='.'<br>';
//        die(print_r($vars['pos_fields_to_hide']));


        //$vars['no_left_bar'] = true; // to show left bar


        $vars['users'] = CommonFunctions::CommonView('tbl_common_users');
        $vars['err_msg'] = '';
        if (empty(smc_session()['user_accounts']['sales_account_id']))
            $vars['err_msg'].=CommonLanguage::getMessage('fill_sale_acc') . '<br>';
        if (empty(smc_session()['user_accounts']['cash_account_id']))
            $vars['err_msg'].=CommonLanguage::getMessage('fill_cash_acc') . '<br>';
        if (empty(smc_session()['user_accounts']['other_account_id']))
            $vars['err_msg'].=CommonLanguage::getMessage('fill_other_acc') . '<br>';
        if (empty(smc_session()['user_accounts']['sales_ship_account_id']))
            $vars['err_msg'].=CommonLanguage::getMessage('fill_ship_acc') . '<br>';
        $vars['cash_customer_arr'] = Customer::get_cash_customer_acc();

//        $this->_registry->Invoicepos->retreive_exchange_by_id(31);
        $this->load->view('main_pos', $vars);
    }

}
