<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;

use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\data\ActiveDataProvider;
use yii\db\Query;
use app\models\Drugdisprep;
use app\base\Converter;


/**
 * DrugdisprepController implements the CRUD actions for Drugdisprep model.
 */
class DrugdisprepController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['index',],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
	
	
 		 
    /**
     * Lists all Arvmaster models.
     * @return mixed
     */
    public function actionIndex()	
    {
		
        $model = new Drugdisprep();
		if ($model->load(Yii::$app->request->post()) || $model->load(Yii::$app->request->get())) {
            if (Yii::$app->request->isAjax) {
                Yii::$app->response->format = Response::FORMAT_JSON;
                return ActiveForm::validate($model);
            }
            
            if(!$model->validate()) {
                return $this->render('index', ['model' => $model]);
            } else {
				
				$frmdate = "01-".$model->month;
                $todate = self::last_day_of_the_month($frmdate);
                $frmdate = Converter::toUnixTimeformat($frmdate);
                if(date('m-Y') == date('m-Y', $frmdate))
                    $todate = strtotime(date('Y/m/d'));
                else
                    $todate = Converter::toUnixTimeformat($todate);
				
					
		    				
				$query = (new Query())
                ->select("sum(cms_ostvisit.ost_visit_q2) as sum_ost_visit_q2,sum(cms_ostvisit.ost_visit_q3) as sum_ost_visit_q3,sum(cms_ostvisit.ost_visit_q4) as sum_ost_visit_q4,sum(cms_ostvisit.ost_visit_q5) as sum_ost_visit_q5")
                ->from('cms_ostvisit')
				->where(["between", "record_date", $frmdate,$todate]);			
                 
               $title = "OST Drug Dispense Monthly Summary Report for ".date("F - Y", $todate);				 
		
                 $dataProvider = new ActiveDataProvider(['query' => $query,'pagination' => false]);
				
				 return $this->render('index', ['model' => $model, 'dataProvider' => $dataProvider,'title' => $title]);
			}
		}			
		return $this->render('index', ['model' => $model] );
        
       
    }
	
	function last_day_of_the_month($date = '')
    {
        $month  = date('m', strtotime($date));
        $year   = date('Y', strtotime($date));
        $result = strtotime("{$year}-{$month}-01");
        $result = strtotime('-1 second', strtotime('+1 month', $result));
        return Converter::toDisplay($result);
    }

}
