<?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\data\ArrayDataProvider;
use yii\web\Response;
use app\base\Converter;
use app\models\Ostvisitnew;
use app\models\Linkidlinkage;
use app\models\Ostmonthreport;
use app\models\Repdateost;
use app\models\Ostvisitrep;
use app\models\Repperiodost;

/**
 * OstmonthreportController implements the CRUD actions for Ostmonthreport model.
 */
class OstmonthreportController 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 Ostmonthreport();
		
		if ($model->load(Yii::$app->request->post()) || $model->load(Yii::$app->request->get())) {
            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);
				
				$Rmodel = Repdateost::find()->one();
			     if($Rmodel === null)
				 $Rmodel = new Repdateost();			  		
			
			     if($frmdate  != "") {
					$Rmodel->fromdate = $frmdate ;						
				}	
				
				if($todate  != "") {
					$Rmodel->todate = $todate ;						
				}				
				$Rmodel->save();
			 
				Yii::$app->db->createCommand('TRUNCATE cms_ostrepdateperiod')->execute();
				$Pmodel = new Repperiodost();
				
				$totaldays = date("d", $todate);
                $title = "Monthly Distribution Chart for ".date("F - Y", $todate);
								
				 for($i = 1; $i <= $totaldays; $i++) {
                    if($i < 10)
                        $i = "0".$i;
                    $day = $i."-".$model->month;
								
					$Pmodel = new Repperiodost();
					$period = Converter::toUnixTimeformat($day);
					$Pmodel->period = $period ;		
				    $Pmodel->save();					
                }
				
				$Dmodel = Repperiodost::find();
				$DmodelCount = $Dmodel->count();
				
				if($DmodelCount ==31)
                   $qry = "SELECT ost_visit_part_id,ost_visit_q1,link_idlinkage_q1,						day1,day2,day3,day4,day5,day6,day7,day8,day9,day10,day11,day12,day13,day14,day15,day16,day17,day18,day19,day20,day21,day22,day23,day24,day25,day26,day27,day28,day29,day30,day31,total_taken,total_days_missed FROM v_ostvisit_report_1 Order By ost_visit_part_id ";	
             	else if($DmodelCount ==29)
                   $qry = "SELECT ost_visit_part_id,ost_visit_q1,link_idlinkage_q1,				 		 day1,day2,day3,day4,day5,day6,day7,day8,day9,day10,day11,day12,day13,day14,day15,day16,day17,day18,day19,day20,day21,day22,day23,day24,day25,day26,day27,day28,day29,total_taken,total_days_missed FROM v_ostvisit_report_3 Order By ost_visit_part_id ";
                else if($DmodelCount ==28)
                   $qry = "SELECT ost_visit_part_id,ost_visit_q1,link_idlinkage_q1,				 		day1,day2,day3,day4,day5,day6,day7,day8,day9,day10,day11,day12,day13,day14,day15,day16,day17,day18,day19,day20,day21,day22,day23,day24,day25,day26,day27,day28,total_taken,total_days_missed FROM v_ostvisit_report_4 Order By ost_visit_part_id ";	
                else
				   $qry = "SELECT ost_visit_part_id,ost_visit_q1,link_idlinkage_q1,						day1,day2,day3,day4,day5,day6,day7,day8,day9,day10,day11,day12,day13,day14,day15,day16,day17,day18,day19,day20,day21,day22,day23,day24,day25,day26,day27,day28,day29,day30,total_taken,total_days_missed FROM v_ostvisit_report_2 Order By ost_visit_part_id ";	
					
                $QModel = Yii::$app->db->createCommand($qry)->queryAll();              
                $nmodel = [];
				$services=[];
                foreach ($QModel as $key => $value) {
                    foreach ($services as $skey => $svalue) {
                        $nservices[$svalue] = '-';
                    }                   
					
                    $xservices = [];
                    $index = 0;
                    foreach ($value as $key1 => $value1) {
                        $xservices[] = $value1;                        
                    }
                    
                    $nmodel[] = $xservices;
                   
                }

                $dataProvider = new ArrayDataProvider([
                    'allModels' => $nmodel,
                    'pagination' => false
                ]);
                
                if($DmodelCount ==31)				
                   return $this->render('index', ['model' => $model, 'dataProvider' => $dataProvider, 'title' => $title]);
			    else if($DmodelCount ==29)
					return $this->render('index3', ['model' => $model, 'dataProvider' => $dataProvider, 'title' => $title]);
			    else if($DmodelCount ==28)
					return $this->render('index4', ['model' => $model, 'dataProvider' => $dataProvider, 'title' => $title]);
			    else 
					return $this->render('index2', ['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);
    }
}
