<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of xhr
 *
 * @author Ola
 */
class xhr extends mainController {

    private $filteredArray = array();
    private $status;

    public function __construct() {
        parent::__construct();
        $this->controller_permission = Session::getPermissionsObject();

        $this->load->model('admin');
    }

    public function landing() {
        
    }

    public function ajaxCustomer() {
        if (!isset($_GET["term"]))
            die();
        $cmd = isset($_GET["cmd"]) ? $_GET["cmd"] : "";
        $class = isset($_GET["ClassID"]) ? $_GET["ClassID"] : 1;

        $RET = array();
        if ($cmd == "") {
            $customer = new Customer();
            $Balance = new AccountingDashboard();
            $balance_flag = isset($_GET['with_balance']) ? 1 : 0;

            $ALL = $customer->view(null, ' AND c.Customer_Name like "%' . smc_escape($_GET["term"]) . '%" ', $balance_flag);
        }
        echo json_encode($ALL);
    }

    public function PricingClassCurrencySearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $is_supp = !empty($_GET['is_supp']) ? 1 : 0;
        $class_ids = !empty($_GET['class_ids']) ? $_GET['class_ids'] : '';
//        die($class_ids);
        $ALL = $this->_registry->product->setData($_GET)->pricingSearch($term, $page, $_GET['limit'], true, null, $is_supp, null, $class_ids);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function printTemplateSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->product->setData($_GET)->printTemplateSearch($term, $page, $_GET['limit'], true);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function ProductSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $cat = (isset($_GET['cat']) && $_GET['cat'] != '') ? $_GET['cat'] : 0;
        $ALL = $this->_registry->product->setData($_GET)->productSearch($term, $page, $_GET['limit'], $cat);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function searchVoidReason() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->product->setData($_GET)->searchVoidReason($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function productBarCodeSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->product->setData($_GET)->productBarCodeSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function recipeProductSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->product->setData($_GET)->recipeProductSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function get_product_expiry() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $_GET['limit'] = isset($_GET['limit']) ? $_GET['limit']  : 30;
        $depot = !empty($_GET['depot']) ? $_GET['depot'] : null;
        $ALL = $this->_registry->product->setData($_GET)->get_product_expiry($term, $page, $_GET['limit'],$depot);
        $data = isset($_GET['auto_com']) ? $ALL['data'] : array('data' => $ALL['data'], 'is_more' => $ALL['is_more']);
        echo json_encode($data);
    }

    public function get_product_lot() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $depot = !empty($_GET['depot']) ? $_GET['depot'] : null;
        $_GET['limit'] = isset($_GET['limit']) ? $_GET['limit']  : 30;
        $ALL = $this->_registry->product->setData($_GET)->get_product_lot($term, $page, $_GET['limit'],$depot);
        $data = isset($_GET['auto_com']) ? $ALL['data'] : array('data' => $ALL['data'], 'is_more' => $ALL['is_more']);
        echo json_encode($data);
    }

    public function get_product_serial() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $depot = !empty($_GET['depot']) ? $_GET['depot'] : null;
        $_GET['limit'] = isset($_GET['limit']) ? $_GET['limit']  : 30;
        $ALL = $this->_registry->product->setData($_GET)->get_product_serial($term, $page, $_GET['limit'],$depot);
        $data = isset($_GET['auto_com']) ? $ALL['data'] : array('data' => $ALL['data'], 'is_more' => $ALL['is_more']);
        echo json_encode($data);
    }

    public function jobSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->job->jobSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function DeliveryFeesSeacrh() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->Invoice->DeliveryFeesSeacrh($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function paygradeSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $job_id = (isset($_GET['job_id']) && $_GET['job_id'] != '') ? $_GET['job_id'] : 0;
        $ALL = $this->_registry->job->paygradeSearch($term, $page, $_GET['limit'], $job_id);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function TruckSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->vehicle->truckSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function availableCheckoutDatesSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->vehicle->setData($_GET)->availableCheckoutDatesSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function PricingClassSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $is_virtual = isset($_GET['is_virtual']) ? $_GET['is_virtual'] : null;
        $type = isset($_GET['type']) ? $_GET['type'] : 0;
        $supp_id = isset($_GET['supp_id']) ? $_GET['supp_id'] : null;
        $classes = isset($_GET['classes']) ? $_GET['classes'] : '';
        $ALL = $this->_registry->product->pricingSearch($term, $page, $_GET['limit'], false, $is_virtual, $type, $supp_id, $classes);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function cashDrawerSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->CommonACC->cashDrawerSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function customerSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->customer->setData($_GET)->customerSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function searchShippingAddress() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->customer->setData($_GET)->searchShippingAddress($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function searchShippingCompany() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->shipping->setData($_GET)->searchShippingCompany($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function poscustomerSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->customer->poscustomerSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function supplierSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->supplier->setData($_GET)->supplierSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function depotSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $branch_id = isset($_GET['branch_id']) ? $_GET['branch_id'] : null;
        $depots = isset($_GET['depots']) ? $_GET['depots'] : '';
        $ALL = $this->_registry->depot->depotSearch($term, $page, $_GET['limit'], $branch_id, $depots);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function CommonSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        //
        $cond = '';
        if ($_GET['table'] == 'tbl_common_currency')
            $cond.= (empty(smc_session()['system_currencies']) ? " and 1 = 2 " : " and Currency_ID in (" . implode(',', array_keys(smc_session()['system_currencies'])) . ") ");
        $ALL = $this->_registry->forms->CommonSearch($term, $page, $_GET['limit'], $_GET['col_name'], $_GET['col_id'], $_GET['table'], $cond);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }
    public function common_search() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        
        $ALL = $this->_registry->forms->common_search($term, $page, $_GET['limit'],$_GET['type']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

//    public function payAccSearch() {
//        $term = isset($_GET['term']) ? $_GET['term'] : null;
//        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
//        $ALL = $this->_registry->payment->payAccSearch($term, $page, $_GET['limit']);
//        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
//    }
    //    public function accSearch_5() {
    //        $term = isset($_GET['term']) ? $_GET['term'] : null;
    //        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
    //        $ALL = $this->_registry->contra->accSearch_5($term, $page, $_GET['limit']);
    //        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    //    }
    // check if no use
    public function ExpenseAccSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->jv->AccountsByTypeSearch($term, $page, $_GET['limit'], "Expense");
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    // check if no use
    public function AccruedExpSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->jv->AccountsByTypeSearch($term, $page, $_GET['limit'], "AccruedExpense");
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function AccountsByTypeSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->jv->AccountsByTypeSearch($term, $page, $_GET['limit'], "Payment");
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function allAccountsSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        if (isset($_GET['empty']))
            $ALL = array('data' => array(), 'is_more' => false);
        else
            $ALL = $this->_registry->jv->allAccountsSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    // check if no use
    public function prepaidExpSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->jv->AccountsByTypeSearch($term, $page, $_GET['limit'], "PrepaidExpense");
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    // check if no use
    public function IncAccSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->jv->AccountsByTypeSearch($term, $page, $_GET['limit'], "Income");
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    // check if no use
    public function PrepaidIncSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->jv->AccountsByTypeSearch($term, $page, $_GET['limit'], "PrepaidIncome");
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function uomSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->product->settingSearch($term, $page, $_GET['limit'], "uom");
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function productuomSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->product->setData($_GET)->productuomSearch($term, $page, $_GET['limit'], $_GET['pid']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function shelfdepotSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->depot->shelfdepotSearch($term, $page, $_GET['limit'], $_GET['pid']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function depotcatSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->depot->depotcatSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function catSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ignore_cat_id = isset($_GET['ignore_cat_id']) ? $_GET['ignore_cat_id'] : null;
        $ALL = $this->_registry->product->catSearch($term, $page, $_GET['limit'], $ignore_cat_id);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    // to be deleted after update of account code
    public function accSearch() {
        $type = $_GET['type'];
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->jv->AccountsByTypeSearch($term, $page, $_GET['limit'], $type);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function accIDSearch() {
        $type = empty($_GET['type']) ? '' : $_GET['type'];
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->jv->setData($_GET)->AccountIDByTypeSearch($term, $page, $_GET['limit'], $type);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function employeeSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $is_actual = isset($_GET['is_actual']) ? $_GET['is_actual'] : 0;
        $with_id = isset($_GET['with_id']) ? $_GET['with_id'] : 0;
        $customer_id = isset($_GET['customer_id']) ? $_GET['customer_id'] : null;
        $ALL = $this->_registry->employee->setData($_GET)->employeeSearch($term, $page, $_GET['limit'], $is_actual, $with_id, $customer_id);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function search_emp_by_pay_period() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $is_actual = isset($_GET['is_actual']) ? $_GET['is_actual'] : 0;
        $pay_period = isset($_GET['pay_period']) ? $_GET['pay_period'] : null;
        $ALL = $this->_registry->employee->search_emp_by_pay_period($term, $page, $_GET['limit'], $is_actual, $pay_period);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function getUsersWithCashAccounts() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->user->getUsersWithCashAccounts($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function searchEmployeeUsers() {
        $type = isset($_GET['type']) ? $_GET['type'] : null;
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->user->searchEmployeeUsers($term, $page, $_GET['limit'], $type);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function templateSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->production->templateSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function searchProductionEmpType() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->production->setData($_GET)->searchProductionEmpType($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function opt_temp_prod_search() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $template_id = isset($_GET['template_id']) ? $_GET['template_id'] : 0;
        $is_needed = isset($_GET['is_needed']) ? $_GET['is_needed'] : null;
        $ALL = $this->_registry->production->setData($_GET)->opt_temp_prod_search($term, $page, $_GET['limit'], $is_needed, $template_id);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function acc_subtype_search() {
        $type = isset($_GET['type']) ? $_GET['type'] : '';
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->CommonACC->acc_subtype_search($term, $page, $_GET['limit'], $type);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    //    public function ajaxSalesPurchaseAccounts() {
    //        if (!isset($_GET["term"]))
    //            die();
    //        $cmd = $_GET["cmd"];
    //        $RET = array();
    //        if ($cmd == 1) {
    //            $purchase = new Purchase();
    //            $ALL = $purchase->getPurchaseAccounts();
    //            for ($x = 0; $x < count($ALL); $x++) {
    //                if (strpos(strtolower($ALL[$x]["Account_Name"]), strtolower($_GET["term"])) !== false) {
    //                    array_push($RET, $ALL[$x]);
    //                }
    //            }
    //        } else {
    //            $invoice = new Invoice();
    //            $ALL = $invoice->getSalesAccounts();
    //            for ($x = 0; $x < count($ALL); $x++) {
    //                if (strpos(strtolower($ALL[$x]["Account_Name"]), strtolower($_GET["term"])) !== false) {
    //                    array_push($RET, $ALL[$x]);
    //                }
    //            }
    //        }
    //
    //        echo json_encode($RET);
    //    }

    public function ajaxVendor() {

        if (!isset($_GET["term"]))
            die();
        $cmd = isset($_GET["cmd"]) ? $_GET["cmd"] : "";
        $RET = array();
        if ($cmd == "") {
            $supplier = new Supplier();
            $Balance = new AccountingDashboard();

            $ALL = $supplier->setData($_GET)->viewSuppliersForAutoComplete(null, ' AND (s.Supplier_Name like "%' . smc_escape($_GET["term"]) . '%" or s.Supplier_Company like "%' . smc_escape($_GET["term"]) . '%") ');
            for ($x = 0; $x < count($ALL); $x++) {
                if (strpos(strtolower($ALL[$x]["Supplier_Name"]), strtolower($_GET["term"])) !== false || strpos(strtolower($ALL[$x]["Supplier_Company"]), strtolower($_GET["term"])) !== false) {
                    $bal = $Balance->returnBalance('Supplier', $ALL[$x]["Account_ID"]);
                    if (count($bal) == 0)
                        $val = 0;
                    else
                        $val = $bal[0]['Balance'];
                    $ALL[$x]["balance"] = $val;
                    array_push($RET, $ALL[$x]);
                    //			print_r($RET);
                    $val = 0;
                    $bal = array();
                }
            }
        }
        echo json_encode($RET);
    }

    public function getProducts() {
        //        die("here");
        $cmd = isset($_GET["cmd"]) ? $_GET["cmd"] : "";
        $class = isset($_GET["ClassID"]) ? $_GET["ClassID"] : smc_session()['pricing_class_id'];
        $depot = isset($_GET["depot_id"]) ? $_GET["depot_id"] : null;
        $customer_id = isset($_GET["customer_id"]) ? $_GET["customer_id"] : null;
        $cat_id = !empty($_GET["cat_id"]) ? $_GET["cat_id"] : null;
        $type = !empty($_GET["type"]) ? $_GET["type"] : null;
        $supp_class = !empty($_GET["supp_class"]) ? $_GET["supp_class"] : '';
        $is_sales = !empty($_GET["is_sales"]) ? $_GET["is_sales"] : 0;
        $is_purchase = !empty($_GET["is_purchase"]) ? $_GET["is_purchase"] : 0;
        $RET = $this->_registry->product->setData($_GET)->getProductsForCustomer($class, $cmd, $_GET['term'], $depot, $cat_id, $type, $customer_id, $supp_class, $is_sales, $is_purchase);
        echo json_encode($RET);
    }

    public function getProductsBarCode() {
        $RET = $this->_registry->product->setData($_GET)->productBarCodeSearch();
        echo json_encode($RET);
    }

    //    public function ajaxVehicle() {
    //        if (!isset($_GET["term"]))
    //            die();
    //        $RET = array();
    //        $ALL = $this->_registry->vehicle->getVehicleDriver();
    //        for ($x = 0; $x < count($ALL); $x++) {
    //            if (strpos(strtolower($ALL[$x]["Vehicle_Registration_Number"]), strtolower($_GET["term"])) !== false) {
    //                array_push($RET, $ALL[$x]);
    //            }
    //        }
    //        echo json_encode($RET);
    //    }

    public function ajaxEmployee() {
        if (!isset($_GET["term"]))
            die();
        $cmd = isset($_GET["cmd"]) ? $_GET["cmd"] : "";
        $hidden = array_key_exists("hidden", $_GET) ? true : false;


        $RET = array();
        if ($cmd == "") {
            $ALL = $this->_registry->employee->viewEmployee(null, $hidden);
            for ($x = 0; $x < count($ALL); $x++) {
                if (strpos(strtolower($ALL[$x]["Employee_Name"]), strtolower($_GET["term"])) !== false) {
                    array_push($RET, $ALL[$x]);
                }
            }
        }

        echo json_encode($RET);
    }

    public function ajaxAddEmployeeOverTime() {
        if (!isset($_GET["term"]))
            die();

        $RET = array();

        $hidden = array_key_exists("hidden", $_GET) ? true : false;
        $month = null;
        if (!empty($_GET['month']))
            $month = $_GET['month'];
        $finance = new Finance();
        $ALL = $finance->returnEmployeeInfoForOverTime($hidden, $month);
        for ($x = 0; $x < count($ALL); $x++) {
            if (strpos(strtolower($ALL[$x]["Employee_Name"]), strtolower($_GET["term"])) !== false) {
                array_push($RET, $ALL[$x]);
            }
        }


        echo json_encode($RET);
    }

    public function ajaxAssignPermissionsRequest() {

        if (isset($_GET['type']) && $_GET['type'] != '') {
            $type = $_GET['type'];
            if ($type == 'assign') {
                if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                    if ($_POST['Role_ID'] != '') {
                        $Ret = $this->_registry->factory->setData($_POST)->assignPermissions();
                        die(json_encode(empty($Ret) ? "OK" : "error"));
                    } else
                        die('Form Incomplete');
                }
            }else if ($type == 'fetch') {
                $role_id = $_GET['id'];
                $roleObj = new Role();
                $perms = $roleObj->getPermissionsByRoleID($role_id);
                die(json_encode($perms));
            }
        }
    }

    public function ajaxCOA() {
        $cmd = isset($_GET["cmd"]) ? $_GET["cmd"] : "";
        $id = isset($_GET["id"]) ? intval($_GET["id"]) : 0;
        $this->status = isset($_GET["status"]) ? intval($_GET["status"]) : -1;

        if ($cmd == "fetch") {
            $RET = $this->_registry->Organization->viewCOA($id, $this->status);
            for ($x = 0; $x < count($RET); $x++) {
                $RET2 = $this->_registry->Organization->viewCOA(intval($RET[$x]["Account_ID"]), $this->status);
                if (count($RET2) > 0)
                    $RET[$x]["isLazy"] = true;
                $RET[$x]["key"] = $RET[$x]["Account_ID"];
            }
            echo json_encode($RET);
        }else if ($cmd == "updatestatus") {
            $Data = isset($_POST["nodes"]) ? $_POST["nodes"] : die();
            $update = $this->_registry->Organization->updateStatusesCOAMain($Data);
            die(empty($update) ? "OK" : "error");
        } else if ($cmd == "addnewitem") {
            $remove = $this->_registry->Organization->setdata($_POST)->createChildernInCOA();
            die(empty($remove) ? "OK" : "error");
        } else if ($cmd == "removeaddeditem") {
            $RET2 = $this->_registry->Organization->RemoveItemWithChildrenMain($_POST["MyID"]);
            die(empty($RET2) ? "OK" : $RET2[0]);
        } else if ($cmd == "edititem") {
            $RET3 = $this->_registry->Organization->setdata($_POST)->editAccount();
            die(empty($RET3) ? "OK" : "error");
        } else if ($cmd == "search") {
            $tree = new Tree;
            $charts = $tree->getChartOFAccounts();
            $list = $tree->buildHierarchyArrary($charts);
            $this->filteredArray = array();
            self::FilterArray($list, 0);
            $this->filteredArray = self::BuildArrayFromKeyChain();
            $this->filteredArray = self::JoinItemsWithSameParents($this->filteredArray);
            echo json_encode($this->filteredArray);
        }
        //        else {
        //            $RET = $this->_registry->Organization->viewCOA(-1, 1);
        //        }
    }

    /*
      This function fetches every row from the table that includes the search term,
      and puts their IDs into arrays as chains, like this:
      [4,26,566]
     */

    public function FilterArray($L, $pid) {

        for ($x = 0; $x < count($L); $x++) {
            if (strpos(strtolower($L[$x]["Account_Name"]), strtolower($_GET["term"])) !== false || strpos(strtolower($L[$x]["Account_Arabic_Name"]), strtolower($_GET["term"])) !== false || strpos(strtolower($L[$x]["Account_Code"]), strtolower($_GET["term"])) !== false) {
                $ParentChain = explode("-", $pid);
                array_push($ParentChain, $L[$x]["Account_ID"]);
                array_push($this->filteredArray, $ParentChain);
            }
            if (isset($L[$x]["children"])) {
                if (count($L[$x]["children"]) > 0) {
                    self::FilterArray($L[$x]["children"], $pid . "-" . $L[$x]["Account_ID"]);
                }
            }
        }
    }

    /*
      This function Builds the array with items including their nested children
     */

    public function BuildArrayFromKeyChain() {

        $x2 = array();
        $Wanted = $_GET["id"];
        for ($x = 0; $x < count($this->filteredArray); $x++) {
            $CurChain = $this->filteredArray[$x];
            if ($Wanted == "all")
                $CurChain[1] = $Wanted;
            if ($CurChain[1] == $Wanted) {
                $CurItem = array();
                for ($y = count($CurChain) - 1; $y > 1; $y--) {
                    $CurPiece = $this->_registry->Organization->viewSingleCOA($CurChain[$y], $this->status);
                    $CurPiece[0]["key"] = $CurPiece[0]["Account_ID"];
                    if (count($CurItem) == 0) {
                        $CurItem = $CurPiece[0];
                    } else {
                        $ArrTemp = $CurItem;
                        $CurItem = $CurPiece[0];
                        $CurItem["children"] = array($ArrTemp);
                    }
                }
                if (count($CurItem) > 0)
                    array_push($x2, $CurItem);
            }
        }
        return $x2;
    }

    /*
      Join items with same parents
     */

    public function JoinItemsWithSameParents($L) {
        $L = self::JoinItemWithSameParents($L);
        for ($x = 0; $x < count($L); $x++) {
            if (isset($L[$x]["children"])) {
                //for($y=0;$y<count($L[$x]["children"]);$y++){
                $L[$x]["children"] = self::JoinItemsWithSameParents($L[$x]["children"]);
                //}
                $L[$x]["children"] = self::JoinItemWithSameParents($L[$x]["children"]);
            } else {
                //  $L[$x] = JoinItemWithSameParents($L[$x]);
            }
        }
        return $L;
    }

    public function JoinItemWithSameParents($L) {
        for ($x = 0; $x < count($L); $x++) {
            if (isset($L[$x])) {
                for ($y = $x + 1; $y < count($L); $y++) {
                    if (isset($L[$y]) && count($L[$x]) > 0) {
                        if ($L[$x]["Account_ID"] == $L[$y]["Account_ID"]) {
                            if (isset($L[$x]["children"]) && isset($L[$y]["children"][0])) {
                                array_push($L[$x]["children"], $L[$y]["children"][0]);
                                unset($L[$y]["children"][0]);
                                $L[$y]["DontShow"] = "yes";
                            }
                        }
                    }
                }
                if ($x > 0) {
                    if ($L[$x]["Account_ID"] == $L[$x - 1]["Account_ID"] && !isset($L[$x - 1]["children"])) {
                        $L[$x - 1]["DontShow"] = "yes";
                    }
                }
            }
        }
        return $L;
    }

    public function ajaxAccount() {
        if (!isset($_GET["term"]))
            die();
        $cmd = isset($_GET["cmd"]) ? $_GET["cmd"] : "";
        $RET = array();
        $ALL = $this->_registry->jv->setData($_GET)->viewAccountByCodeOrName($cmd, $_GET["term"]);
        //        for ($x = 0; $x < count($ALL); $x++) {
        //            if (strpos(strtolower($ALL[$x]["Account_Code"]), strtolower($_GET["term"])) !== false && $cmd == 1) {
        //
        //                array_push($RET, $ALL[$x]);
        //            } else if (strpos(strtolower($ALL[$x]["Account_Name"]), strtolower($_GET["term"])) !== false && $cmd == 2) {
        //                array_push($RET, $ALL[$x]);
        //            }
        //        }
        echo json_encode($ALL);
    }

    //    public function getCategory() {
    //
    //        $ID = isset($_GET["id"]) ? floatval($_GET["id"]) : 0;
    //        $Customer_ID = isset($_GET["Customerid"]) ? floatval($_GET["Customerid"]) : 0;
    //        $categories = $this->_registry->product->getProductsCat($ID);
    //        $Products = $this->_registry->product->getProductsForCustomerByClass($ID);
    //        if (count($Products) > 0) {
    //            if (!$categories)
    //                $categories = Array();
    //            for ($x = 0; $x < count($Products); $x++) {
    //                $CurrencyID = $Products[$x]['Currency_ID'];
    //                $CurrencyCode = $this->_registry->currency->getCurrencyCodeByID($CurrencyID);
    //                $CurrencySymbol = $this->_registry->currency->viewCurrencySymbol($CurrencyID);
    //                $Thumb = file_exists('uploads/products_photos/' . $Products[$x]['ID'] . '.jpg');
    //                /**
    //                 * @edited by: Assaad
    //                 * @added: producted quantity at the end of the array: $Products[$x][3])
    //                 * @reason: for valiation
    //                 *
    //                 * the array below holds the data for each product within categories as mentioned below:
    //                 * Product ID, Product Name, Product, Product Price, 0, Thumb dir,
    //                 * array[Currency ID, Currency Symbol], Product Code, Product Stock Quantity
    //                 */
    //                //                        $Products[$x]['Product_UOM'] = str_replace(",","/",$Products[$x]['Product_UOM']);
    //                //                        echo $Products[$x]['Product_UOM'];
    //                array_push($categories, Array($Products[$x]['ID'], $Products[$x]['Name'], $Products[$x]['Code'], "Product", $Products[$x]['Price'], "0", $Thumb, array($CurrencyID, $CurrencySymbol), $Products[$x]['Code'], $Products[$x]['Quantity'], $Products[$x]['VAT'], $Products[$x]['UOM_ID'], $Products[$x]['Product_UOM']));
    //            }
    //        }
    //        for ($x = 0; $x < count($categories); $x++) {
    //            $Cat = $categories[$x];
    //            if (count($Cat) == 2) {//check if cat
    //                $SubC = ((count($this->_registry->product->getProductsCat($Cat[0])) > 0) || (count($this->_registry->product->getProduct($Cat[0])) > 0));
    //                $Cat = array($Cat[0], $Cat[1], $SubC);
    //                $categories[$x] = $Cat;
    //            }
    //        }
    //        $J = json_encode($categories);
    //        echo "GotoCateg($ID," . ($J ? $J : "null") . ")";
    //    }
    //    public function ajaxInvoice() {
    //        $RET = $this->_registry->Invoice->setdata($_POST)->insertSalesInvoice(null, null, true);
    //
    //        if (!$RET)
    //            die("error");
    //
    //        ob_clean();
    //        $LastInvoiceID = $this->_registry->product->getLastSalesOrder();
    //        if (!empty($LastInvoiceID))
    //            $NewInvoice_ID = $LastInvoiceID + 1;
    //        else
    //            $NewInvoice_ID = 1;
    //        echo($NewInvoice_ID);
    //    }
    public function ajaxInvoice() {
        $RET = $this->_registry->Invoice->setdata($_POST)->insertSalesInvoice();
        die(json_encode(is_numeric($RET) ? $RET : 'error' ));
    }

    public function ajaxPOS() {
        $db = DBConnectorFactory::loadDriver();
        $return_arr = array();

        $query = $db->prepare_Stmt('SELECT Cat_ID, Cat_Name FROM tbl_product_cat 
            WHERE Cat_Name LIKE ?', ["%" . $_GET['term'] . "%"]);
        while ($row = $db->fetch($query)) {
            $row_array['id'] = $row['Cat_ID'];
            $row_array['value'] = $row['Cat_Name'];
            array_push($return_arr, $row_array);
        }
        $query = $db->prepare_Stmt('SELECT tbl_stock_products.*, tbl_product_cat.* FROM tbl_stock_products 
            LEFT JOIN tbl_product_cat ON tbl_stock_products.Category_ID = tbl_product_cat.Cat_ID 
            WHERE tbl_stock_products.Product_Name LIKE ? ', ["%" . $_GET['term'] . "%"]);
        while ($row = $db->fetch($query)) {
            $Thumb = file_exists(upload_path . 'products_photos/' . $row['Product_ID'] . '.jpg');
            $row_array['id'] = $row['Product_ID'];
            $row_array['value'] = $row['Product_Name'];
            $row_array['category'] = $row['Cat_ID'];
            $row_array['thumb'] = $Thumb;
            array_push($return_arr, $row_array);
        }
        echo json_encode($return_arr);
    }

    public function ajaxTransaction() {
        $only_cust = 0;
        if ((empty($_GET['is_actual']) && !$this->controller_permission->hasPermission('view_ledger_all_accounts')) || (!empty($_GET['is_actual']) && $_GET['is_actual'] == 1 && !$this->controller_permission->hasPermission('view_ledger_all_accounts_act'))) {
            $only_cust = 1;
        }

//        echo $only_cust.'===';
        $data = $this->_registry->jv->setData($_GET)->getAccountName($_GET['term'], $only_cust);
        echo json_encode($data);
    }

    public function ajaxLanguage() {
        if (!isset($_POST["Lang"]))
            die();
        $Lang = isset($_POST["Lang"]) ? $_POST["Lang"] : "";
        if ($Lang != "") {
            $user = new User;
            $insert = $user->setData($_POST)->setUserPreference();
        }
        if (empty($insert)) {
            smc_session()['lang_code'] = $_POST['Lang'];
            die('ok');
        } else {
            die('error');
        }
    }

    public function ajaxCurrentYear() {
        //        if (!isset($_POST["year"]))
        //            die();
        //        $year = isset($_POST["year"]) ? $_POST["year"] : "";
        //        if ($year != "") {
        //            $user = new User;
        //            $insert = $user->setData($_POST)->setUserPreference();
        //        }
        //        if (empty($insert)) {
        //            smc_session()['current_year_id'] = $_POST['year'];
        //            die('ok');
        //        } else {
        //            die('error');
        //        }

        if (!empty($_POST['year_id'])) {
//            smc_session()['current_year_id'] = $_POST['year_id'];
//            smc_session()['current_year'] = $_POST['year'];
//            smc_session()['start_date'] = $_POST['start_date'];
//            smc_session()['end_date'] = $_POST['end_date'];
//            smc_session()['year_closed'] = $_POST['is_closed'];
//            smc_session()['start_date_act'] = $_POST['start_date'];
//            smc_session()['end_date_act'] = $_POST['end_date'];
//            smc_session()['year_closed_act'] = $_POST['is_closed'];
//            smc_session()['current_year_id_actual'] = $_POST['year_id'];
//            smc_session()['current_year_actual'] = $_POST['year'];
            $db = DBConnectorFactory::loadDriver();
            $query = $db->query('SELECT * from tbl_common_current_year WHERE current_year_id= ?', array($_POST['year_id']));
            $session_year_data = $db->assoc($query);
            smc_session()['current_year_id'] = $session_year_data['current_year_id'];
            smc_session()['current_year'] = $session_year_data['current_year'];
            smc_session()['start_date'] = $session_year_data['start_date'];
            smc_session()['end_date'] = $session_year_data['end_date'];
            smc_session()['year_closed'] = $session_year_data['is_closed'];
            smc_session()['start_date_act'] = $session_year_data['start_date'];
            smc_session()['end_date_act'] = $session_year_data['end_date'];
            smc_session()['year_closed_act'] = $session_year_data['is_act_closed'];
            smc_session()['current_year_id_actual'] = $session_year_data['current_year_id'];
            smc_session()['current_year_actual'] = $session_year_data['current_year'];

            die("ok");
        }
    }

    public function ajaxProducts() {
        $ALL = $this->_registry->product->setData($_GET)->getProductsForAutoComplete($_GET["term"], $_GET['class']);
        die(json_encode($ALL));
    }

    public function cutomerReturn() {
        $ALL = $_GET["is_vendor"] ? $this->_registry->return->viewSupplierPurchases($_GET["term"], $_GET['is_actual']) : $this->_registry->return->viewCustomerInvoices($_GET["term"], $_GET['is_actual']);
        die(json_encode($ALL));
    }

    public function getBarcodebyUom() {
        $ALL = $this->_registry->product->getBarcodebyUom($_GET["uom_id"], $_GET["product_id"]);
        die(json_encode($ALL));
    }

    public function searchInvTruck() {
        $data = $this->_registry->Invoice->searchInvTruck($_GET['term']);
        die(json_encode($data));
    }

    public function search_shift_note() {
        $data = $this->_registry->employee->search_shift_note($_GET['term']);
        die(json_encode($data));
    }

    public function search_location_address() {
        $data = $this->_registry->employee->search_location_address($_GET['term']);
        die(json_encode($data));
    }

    public function ministry_report_search() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->employee->ministry_report_search($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    //    public function getProduct() {
    //
    //        $ID = isset($_GET["id"]) ? floatval($_GET["id"]) : 0;
    //
    //        $Customer_ID = isset($_GET["Customerid"]) ? floatval($_GET["Customerid"]) : 0;
    //        $Products = $this->_registry->product->getProductsForCustomers($ID);
    //
    //
    //        $categories = $this->_registry->product->getProductsCat($Products[0]['Category_ID']);
    //        if (count($Products) > 0) {
    //            if (!$categories)
    //                $categories = Array();
    //            for ($x = 0; $x < count($Products); $x++) {
    //                $CurrencyID = $Products[$x]['Currency_ID'];
    //                $CurrencyCode = $this->_registry->currency->getCurrencyCodeByID($CurrencyID);
    //                $CurrencySymbol = $this->_registry->currency->viewCurrencySymbol($CurrencyID);
    //                $Thumb = file_exists('uploads/products_photos/' . $Products[$x]['ID'] . '.jpg');
    //                /**
    //                 * @edited by: Assaad
    //                 * @added: producted quantity at the end of the array: $Products[$x][3])
    //                 * @reason: for valiation
    //                 *
    //                 * the array below holds the data for each product within categories as mentioned below:
    //                 * Product ID, Product Name, Product, Product Price, 0, Thumb dir,
    //                 * array[Currency ID, Currency Symbol], Product Code, Product Stock Quantity
    //                 */
    //                //                        $Products[$x]['Product_UOM'] = str_replace(",","/",$Products[$x]['Product_UOM']);
    //                //                        echo $Products[$x]['Product_UOM'];
    //                array_push($categories, Array($Products[$x]['ID'], $Products[$x]['Name'], $Products[$x]['Code'], "Product", $Products[$x]['Price'], "0", $Thumb, array($CurrencyID, $CurrencySymbol), $Products[$x]['Code'], $Products[$x]['Quantity'], $Products[$x]['VAT'], $Products[$x]['UOM_ID'], $Products[$x]['Product_UOM']));
    //            }
    //        }
    //        for ($x = 0; $x < count($categories); $x++) {
    //            $Cat = $categories[$x];
    //            if (count($Cat) == 2) {//check if cat
    //                $SubC = ((count($this->_registry->product->getProductsCat($Cat[0])) > 0) || (count($this->_registry->product->getProduct($Cat[0])) > 0));
    //                $Cat = array($Cat[0], $Cat[1], $SubC);
    //                $categories[$x] = $Cat;
    //            }
    //        }
    //        $J = json_encode($categories);
    //        echo "GotoCateg($ID," . ($J ? $J : "null") . ")";
    //    }

    public function GroupAccountSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->CommonACC->GroupAccountSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function getOrganizations() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->crmClass->setData($_GET)->organizationSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function getContacts() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $organization_id = isset($_GET['organization_id']) ? $_GET['organization_id'] : null;
        $ALL = $this->_registry->crmClass->contactsSearch($term, $page, $_GET['limit'], $organization_id);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function getDealsContact() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $organization_id = isset($_GET['organization_id']) ? $_GET['organization_id'] : null;
        $contact_id = isset($_GET['contact_id']) ? $_GET['contact_id'] : null;
        $ALL = $this->_registry->crmClass->getDealsContact($term, $page, $_GET['limit'], $organization_id, $contact_id);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function getElement() {
        $data = CommonFunctions::CommonView($_GET['table'], [$_GET['rowEntry']], [$_GET['rowId']]);
        echo json_encode($data);
    }

    public function getAllActivities() {
        $data = CommonFunctions::CommonView('all_activities');
        echo json_encode($data);
    }

    public function searchshift() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->worktime->searchshift($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function employeeDependantSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->employee->employeeDependantSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function employeeDependantForChildSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $is_actual = isset($_GET['is_actual']) ? $_GET['is_actual'] : 0;
        $ALL = $this->_registry->employee->employeeDependantForChildSearch($term, $page, $_GET['limit'], $is_actual);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function searchSchoolType() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->employee->searchSchoolType($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function medicalPlaceSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->employee->medicalPlaceSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

//    public function ViewDocCategoryBySearch() {
//        $term = isset($_GET['term']) ? $_GET['term'] : null;
//        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
//        $ALL = $this->_registry->doc->ViewDocCategory($term, $page, $_GET['limit']);
//        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
//    }

    public function emergencySearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->employee->emergencySearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function employeeForUserSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $is_actual = isset($_GET['is_actual']) ? $_GET['is_actual'] : 0;
        $ALL = $this->_registry->employee->employeeForUserSearch($term, $page, $_GET['limit'], $is_actual);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function getRelatedRoles() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->role->setData($_GET)->getRelatedRoles($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function getAllRoles() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->role->setData($_GET)->getAllRoles($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function getphonenumber() {
        $caller_id = $this->_registry->customer->viewLastCaller();
        die(json_encode($caller_id));
    }

    public function MachinePartSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->product->setData($_GET)->MachinePartSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function customerCouponSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->station->setData($_GET)->customerCouponSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function MachineLineSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->product->setData($_GET)->MachineLineSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function MainMachineSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->product->setData($_GET)->MainMachineSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function ScheduleSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->product->setData($_GET)->ScheduleSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function PartSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $is_product = !empty($_GET['is_product']) ? 1 : 0;
        $ALL = $this->_registry->product->setData($_GET)->PartSearch($term, $page, $_GET['limit'], $is_product);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function PropertySearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->product->setData($_GET)->PropertySearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function PropertyLineSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->product->setData($_GET)->PropertyLineSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function groupSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->user->groupSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function search_report_by_role() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->CommonACC->search_report_by_role($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function CurrencySearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->CommonACC->CurrencySearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function searchCity() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->Organization->setData($_GET)->searchCity($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function searchFastpaxCities() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->Organization->setData($_GET)->searchFastpaxCities($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function update_acc_node() {
        $update = $this->_registry->Organization->setData($_POST)->update_acc_node();
        die(empty($update) ? 'OK' : $update[0]);
    }

    public function searchInsuranceCompany() {
        $data = $this->_registry->vehicle->searchInsuranceCompany($_GET['term']);
        die(json_encode($data));
    }

    public function companyJobOrderSearch() {
        $ALL = $this->_registry->jobOrder->setData($_GET)->companyJobOrderSearch();
        echo json_encode($ALL);
    }

    public function payForScheduleBillSearch() {
        $ALL = $this->_registry->payment->setData($_GET)->payForScheduleBillSearch();
        echo json_encode($ALL);
    }

    public function autocompleteJoborderOptionSearch() {
        $ALL = $this->_registry->jobOrder->setData($_GET)->autocompleteJoborderOptionSearch();
        echo json_encode($ALL);
    }

    public function search_dynamic_blocks() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->CommonACC->search_dynamic_blocks($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function customerAddressSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->customer->setData($_GET)->customerAddressSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function contraSearch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->crmClass->setData($_GET)->contraSearch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function search_additional_barcodes() {
        $data = $this->_registry->product->search_additional_barcodes($_GET['product_id'], $_GET['term']);
        die(json_encode($data));
    }

    public function search_orders() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->purchase->search_orders($term, $page, $_GET['limit'], $_GET['is_act'], $_GET['is_sales']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function general_group_Search() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->common_inventory->setData($_GET)->general_group_Search($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function general_class_Search() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->common_inventory->setData($_GET)->general_class_Search($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function searchCustomerBranch() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->customer->setData($_GET)->searchCustomerBranch($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function searchCustomerUsers() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $is_app = isset($_GET['is_app']) ? $_GET['is_app'] : null;
        $ALL = $this->_registry->customer->setData($_GET)->searchCustomerUsers($term, $page, $_GET['limit'], $is_app);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function searchReservations() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->customer->setData($_GET)->searchReservations($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function invoice_type_search() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->Invoice->setData($_GET)->invoice_type_search($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

    public function searchSubscriptionBlocks() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->subscription->setData($_GET)->searchSubscriptionBlocks($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }
    
    public function notification_type_search() {
        $term = isset($_GET['term']) ? $_GET['term'] : null;
        $page = isset($_GET['page']) ? $_GET['page'] - 1 : 0;
        $ALL = $this->_registry->user->setData($_GET)->notification_type_search($term, $page, $_GET['limit']);
        echo json_encode(array('data' => $ALL['data'], 'is_more' => $ALL['is_more']));
    }

}
