<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Reports extends Auth_Controller {

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

	function stock()
    {
        $this->mPageTitle = lang('stock');
        $this->repairer->checkPermissions();
        $data['error'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
        $this->data['stock'] = $this->reports_model->getStockValue();
        $this->data['totals'] = $this->reports_model->getStockTotals();
        $this->render('reports/stock_chart');

    }

	public function finance($month = NULL, $year = NULL)
    {
        $this->repairer->checkPermissions();
        $this->mPageTitle = lang('revenue_chart');

        $this->data['currency'] = $this->mSettings->currency;
        $this->data['settings'] = $this->mSettings;
        if (isset($month) && isset($year)) {
            $this->data['list'] = $this->reports_model->list_earnings($month, $year);
        } else {
            $this->data['list'] = $this->reports_model->list_earnings(date('m'), date('Y'));
        }
        $this->render('reports/finance');
    }

    function quantity_alerts($warehouse_id = NULL)
    {
        $this->repairer->checkPermissions();
        $this->mPageTitle = lang('quantity_alerts');

        $this->render('reports/quantity_alerts');
    }
    function getQuantityAlerts($warehouse_id = NULL, $pdf = NULL, $xls = NULL)
    {
        $this->repairer->checkPermissions('quantity_alerts');

        $this->load->library('datatables');
        
        $this->datatables
            ->select('code, name, quantity, alert_quantity')
            ->from('inventory')
            ->where('alert_quantity > quantity', NULL)
            ->where('alert_quantity >', 0);
        echo $this->datatables->generate();
    }

}
