<?php

class Dashboard extends mainController {

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

    public function landing() {

        $vars['pageName'] = 'Dashboard';
        $vars['title'] = ACCModel::getmsg('dashboard');
        $today = !empty($_GET['current_date']) ? $_GET['current_date'] : Date::getToday();
        $year = substr($today, 0, 4);
        $month = substr($today, 5, 2);
        $data = !empty($_GET['branch_id']) ? array('branch_id' => $_GET['branch_id']) : array();
        $vars['branch_data'] = !empty($_GET['branch_id']) ? CommonFunctions::CommonView('tbl_organization_branches', ['branch_id'], [$_GET['branch_id']]) : array();

        $vars['viewCash'] = $this->_registry->dashboard->setdata($data)->viewCashBankBalance(1);
        $vars['viewBank'] = $this->_registry->dashboard->setdata($data)->viewCashBankBalance(0);
        $vars['last_close_time'] = '';
        if (smc_session()['default_setting']['dashboard_daily_cards_type'] == 2) {
            $vars['last_close_time'] = Invoicepos::get_last_close_end_day(!empty($_GET['branch_id']) ? $_GET['branch_id'] : '');
        }
        $vars['daily_sales'] = $this->_registry->dashboard->setdata($data)->viewSalesInvoicesByDate($today);
        $vars['weekly_sales'] = $this->_registry->dashboard->setdata($data)->viewSalesInvoicesByDate($today, null, null, true);
        $vars['monthly_sales'] = $this->_registry->dashboard->setdata($data)->viewSalesInvoicesByDate(null, $year, $month);

        $vars['daily_purchases'] = $this->_registry->dashboard->setdata($data)->viewPurchaseInvoicesByDate($today);
        $vars['weekly_purchases'] = $this->_registry->dashboard->setdata($data)->viewPurchaseInvoicesByDate($today, null, null, true);
        $vars['monthly_purchases'] = $this->_registry->dashboard->setdata($data)->viewPurchaseInvoicesByDate(null, $year, $month);

        $vars['top_products'] = $this->_registry->dashboard->setdata($data)->viewTopSaledOrPurchasedProducts('tbl_invoice_products_details', 5, 'QTY');
        $vars['payables'] = $this->_registry->dashboard->setdata($data)->viewCustomersSuppliersBalance(1);
         $vars['receivables'] = $this->_registry->dashboard->setdata($data)->viewCustomersSuppliersBalance(0);

        $vars['unpaid'] = $this->_registry->dashboard->viewUnpaidInvoicesByNeededOrder('Invoice_Date', 5, $today);
        $CurrencyID = $this->_registry->currency->getDefaultCurrency();
        $vars['CurrencySymbol'] = $this->_registry->currency->viewCurrencySymbol($CurrencyID);
        $vars['arrName'] = postsModel::pie()[0];
        $vars['arrValue'] = postsModel::pie()[1];
        $revenue = $this->_registry->dashboard->setdata($data)->financialSettings();
        $vars['arrVal'] = $revenue[0];
        $vars['arrName1'] = $revenue[1];
        $vars['posts'] = $this->_registry->posts->get();
        $vars['total_inv_type'] = $this->_registry->invoicepos->setdata($data)->get_totals_all_invoice_type($today);
        $vars['user_cash'] = $this->_registry->invoicepos->setdata($data)->get_user_cashes(); // check with ola

        $vars['daily_expense'] = $this->_registry->dashboard->setdata($data)->get_total_expenses($today, $today);
        // First day of the month.
        $first_day = date('Y-m-01', strtotime($today));
        // Last day of the month.
        $last_day = date('Y-m-t', strtotime($today));
        $vars['monthly_expense'] = $this->_registry->expense->setdata($data)->get_total_expenses($first_day, $last_day);
        $this->load->view('Dashboard', $vars);
    }

    public function view() {
        echo '<pre>' . print_r(func_get_args()) . '</pre>';
    }

    public function DashboardAjax() {
        if (isset($_GET["cmd"])) {
            if ($_GET["cmd"] == "fetchtopsalesdata") {
                $items = $_GET["items"] == 1 ? 5 : ($_GET["items"] == 2 ? 10 : 15);
                $type = $_GET["type"] == 1;
                $sort = $_GET["sort"] == 1 ? "QTY" : "SUM";
                $top_products = $this->_registry->dashboard->viewTopSaledOrPurchasedProducts($type, $items, $sort);
                die(json_encode($top_products));
            } else if ($_GET["cmd"] == "fetchsalespurchase") {
                $data = $_GET["type"] == "Month1" ? $this->_registry->dashboard->viewPurchaseInvoicesByDate('Month') : ($_GET["type"] == "Month2" ? $this->_registry->dashboard->viewExpensesByDate('Month') : $this->_registry->dashboard->viewSalesInvoicesByDate($_GET["type"]));
                echo($data);
                die();
            }
        }
    }

}
