<?php
class ReportsController extends Controller
{
    public function init() {
        parent::init();
        $this->initAjaxCsrfToken();
    }
    // this function will work to post csrf token.
    protected function initAjaxCsrfToken() {
        Yii::app()->clientScript->registerScript('AjaxCsrfToken', ' $.ajaxSetup({
             data: {"' . Yii::app()->request->csrfToken . '": "' . Yii::app()->request->csrfToken . '"},
             cache:false
        });', CClientScript::POS_HEAD);
    } 
    public function accessRules()
    {
        return array(
            array(
                'allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('login', 'error', 'logout'),
                'users' => array('*'),
            ),
            array(
                'allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array(
                    'ConsolidatedSupplyConfirmation','Consolidatedsupplyconfirmationhtml','Consolidatedsupplyconfirmationexcel','Index','ConsolidatedOrderReport','monthwisereportexcel',
                    'SalesTransaction','SalesTransactionExcel','SalesReport','SalesReportQtyExcel','SalesReportAmountExcel'
                ),
                'expression' => '$user->isMember()',
            ),
            array(
                'deny', // deny all users
                'users' => array('*'),
            ),
        );
    }
    public function actionIndex() {
        $this->render('index');
    }
    

    public function filters()
    {
        return array('accessControl'); // perform access control for CRUD operations
    }
    public function actionConsolidatedSupplyConfirmation()
    {
        $_GET  = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
        if(count($_GET) > 0){
            $this->render('filter_consolidated_supply_confirmation', array());
        }else{
            throw new CHttpException(404, 'Page not found.');
        }
    }
    public function actionConsolidatedsupplyconfirmationhtml() {
        $model = new SupplyConfirmation();
        $from_year = $_POST['from_year'];
        $from_month = $_POST['from_month'];
        $to_year = $_POST['to_year'];
        $to_month = $_POST['to_month'];
        $to_date = $to_year."-".$to_month;
        $from_date = $from_year."-".$from_month;
        $dateTimestamp_to = strtotime($to_date);
        $dateTimestamp_from = strtotime($from_date);
        // Compare the timestamp date 
        if ($dateTimestamp_to >= $dateTimestamp_from){
            Yii::app()->clientScript->scriptMap = array('*.js' => false, '*.min.js' => false, '*.css' => false);
            $this->render('html/print_consolidated_supply', array('model' => $model, 'from_year' => $from_year, 'from_month' => $from_month, 'to_year' => $to_year, 'to_month' => $to_month), false, true);
        }else{
            echo '<div style="color: red;text-align: center;font-size: 16px;font-weight: 600;">From Date Should be Less Than To Date</div>';
        }
    }

   public function actionConsolidatedsupplyconfirmationexcel() {
        $from_year = Yii::app()->request->getParam('from_year');
        $from_month = Yii::app()->request->getParam('from_month');
        $to_year = Yii::app()->request->getParam('to_year');
        $to_month = Yii::app()->request->getParam('to_month');
        $filename = 'consolidatedsupplyconfirmation_report' . date('dmYhis') . '.xls';
        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
        $this->renderpartial('excel/print_consolidatedsupplyconfirmation_excel', array(
            'from_year' => $from_year,
            'from_month' => $from_month,
            'to_year' => $to_year,
            'to_month' => $to_month), false, true);
        Yii::app()->end();
    } 
    public function actionConsolidatedOrderReport()
    {
        $_GET  = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
        if(count($_GET) > 0){
            $this->render('filter_datewise_consolidated_report', array());
        }else{
            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionMonthwiseReportExcel() {
        $filename = 'distributor_wise_order_report' . date('dmYhis') . '.xls';
        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
        $this->renderpartial('excel/print_monthwisereport_excel', $_GET, false, true);
        Yii::app()->end();
    } 
    public function actionSalesTransaction(){
        $_GET  = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
        if(count($_GET) > 0){
            $this->render('filter_sales_dump', array());
        }else{
            throw new CHttpException(404, 'Page not found.');
        }
    }
    public function actionSalesTransactionExcel() {
        $filename = 'sales_transaction_dump_report_' . date('dmYhis') . '.xls';
        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
        $this->renderpartial('excel/print_sales_transaction_dump_excel', $_GET, false, true);
        Yii::app()->end();
    }
    public function actionSalesReport(){
        $_GET  = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
        if(count($_GET) > 0){
            $this->render('filter_sales_report', array());
        }else{
            throw new CHttpException(404, 'Page not found.');
        }
    }
    public function actionSalesReportQtyExcel() {
        $from_year = $_REQUEST['from_year'];
        $from_month = $_REQUEST['from_month'];
        $to_year = $_REQUEST['to_year'];
        $to_month = $_REQUEST['to_month'];
        $to_date = $to_year."-".$to_month."-01";
        $from_date = $from_year."-".$from_month."-01";
        $start = new DateTime($from_date);
        $end = new DateTime($to_date);
        // Compare the timestamp date 
        if ($end >= $start){
            $filename = 'sales_report_' . date('dmYhis') . '.xls';
            header("Content-type: application/vnd.ms-excel");
            header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
            $this->renderpartial('excel/print_sales_report_excel', $_GET, false, true);
            Yii::app()->end();
        }else{
            echo '<div style="color: red;text-align: center;font-size: 16px;font-weight: 600;">From Date Should be Less Than To Date</div>';
        }
    }
    public function actionSalesReportAmountExcel() {
        $from_year = $_REQUEST['from_year'];
        $from_month = $_REQUEST['from_month'];
        $to_year = $_REQUEST['to_year'];
        $to_month = $_REQUEST['to_month'];
        $to_date = $to_year."-".$to_month."-01";
        $from_date = $from_year."-".$from_month."-01";
        $start = new DateTime($from_date);
        $end = new DateTime($to_date);
        // Compare the timestamp date 
        if ($end >= $start){
            $filename = 'sales_report_' . date('dmYhis') . '.xls';
            header("Content-type: application/vnd.ms-excel");
            header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
            $this->renderpartial('excel/print_sales_report_amount_excel', $_GET, false, true);
            Yii::app()->end();
        }else{
            echo '<div style="color: red;text-align: center;font-size: 16px;font-weight: 600;">From Date Should be Less Than To Date</div>';
        }
    }

}

