<?php

namespace app\controllers;

use Yii;
use app\models\Voucherref;
use app\models\Artinitiationincentmaster;
use app\models\ArtinitiationincentmasterSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

/**
 * ArtinitiationincentmasterController implements the CRUD actions for Artinitiationincentmaster model.
 */
class ArtinitiationincentmasterController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['index', 'create', 'update', 'view', 'delete', 'generate', 'pdf'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],                   
                ],
            ],
        ];
    }

    /**
     * Lists all Artinitiationincentmaster models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ArtinitiationincentmasterSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        if(Yii::$app->request->isAjax)
            $this->layout = "base";

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Artinitiationincentmaster model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Artinitiationincentmaster model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Artinitiationincentmaster();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->art_init_inc_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Artinitiationincentmaster model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->art_init_inc_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Artinitiationincentmaster model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['/'.Yii::$app->controller->id]);
    }

    /**
     * Finds the Artinitiationincentmaster model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Artinitiationincentmaster the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Artinitiationincentmaster::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
/*
    public function actionGenerate($id)
    {
        if($id != "")
            $model = $this->findModel($id);
        else
            $model = new Artinitiationincentmaster();

        if ($model->load(Yii::$app->request->post()) || Yii::$app->request->isPost) {
            $model = Artinitiationincentmaster::find()->where(["art_init_inc_part_id" => $model->art_init_inc_part_id])->one();
            $VModel = Voucherref::generateVoucherno($model->art_init_inc_survey, $model->art_init_inc_part_id, Voucherref::INCENTIVE);
            $model->art_init_inc_q1 = 1;
            $model->art_init_inc_q3 = strtotime(date('Y/m/d'));
            $model->art_init_inc_q4 = $VModel->vchr_ref_loc."-".$VModel->vchr_ref_type."-".$VModel->vchr_ref_code;
        }

        if (Yii::$app->request->isPost && $model->save()) {
            $VModel->save();
            return $this->redirect(['/'.Yii::$app->controller->id]);
        }
    }
*/

public function actionGenerate()
    {
        $model = new Artinitiationincentmaster;
        $model->scenario = 'generate';

        if ($model->load(Yii::$app->request->post())) {
            $flag = false;
            $art_init_inc_id = explode(',', substr(Yii::$app->request->post()['Artinitiationincentmaster']['art_init_inc_id'], 0, -1));
            $VModel = Voucherref::generateVoucherno($model->art_init_inc_survey, $model->art_init_inc_part_id, Voucherref::INCENTIVE);
            foreach ($art_init_inc_id as $key => $value) {
                $model = Artinitiationincentmaster::find()->where(["art_init_inc_id" => $value])->andWhere(["art_init_inc_q1" => 0])->one();
                if($model !== null) {
                    $model->art_init_inc_q1 = 1;
                    $model->art_init_inc_q3 = strtotime(date('Y/m/d'));
                    $model->art_init_inc_q4 = $VModel->vchr_ref_loc."-".$VModel->vchr_ref_type."-".$VModel->vchr_ref_code;
                    if($model->save())
                        $flag = true;
                }
            }
            if($flag)
                $VModel->save();
            return $this->redirect(['/'.Yii::$app->controller->id]);
        }
        return $this->render('generate', ['model' => $model]);
    }
	
    public function actionPdf($id)
    {
        $model = $this->findModel($id);
        $pmodel = ["pid" => $model->art_init_inc_part_id, "vno" => $model->art_init_inc_q4, "vdt" => $model->art_init_inc_q3, "amt" => $model->art_init_inc_q2, "summary" => "ART Initiation Incentive"];
        return $content = $this->renderPartial('/layouts/print', ['model' => $pmodel]);

        $pdf = Yii::$app->pdf; // or new Pdf();
        $mpdf = $pdf->api; // fetches mpdf api
        $mpdf->WriteHtml($content); // call mpdf write html
        echo $mpdf->Output('filename.pdf', 'I'); // call the mpdf api output as needed
    }
}
