<?php

namespace backend\controllers;

use Yii;
use common\models\User;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\helpers\Upload ;
use common\models\Etablissement ;
use yii\data\ActiveDataProvider ;
use common\models\Facture ;

/**
 * UserController implements the CRUD actions for User model.
 */
class EtablissementAdminController extends BackendController
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
            'access' => [
	            'class' => AccessControl::className(),
	            'rules' => [
		            [
		            	'allow' => true,
		            	'roles' => ['@'],
		            	'matchCallback' => function ($rule, $action) {
                                    return Yii::$app->user->identity->status == USER::STATUS_ADMIN ;
		            	}
	            	]
	            ],
            ],
        ];
    }
    
    public function actionIndex() 
    {
        $datas_etablissements = new ActiveDataProvider([
            'query' => Etablissement::find()
                ->with('userEtablissement','user')
                ->orderBy('date_creation DESC'),
            'pagination' => [
                'pageSize' => 1000,
            ],
        ]);

        return $this->render('index', [
            'datas_etablissements' => $datas_etablissements,
        ]);
    }
    
    public function actionFacturer($id_etablissement) 
    {
        
        $etablissement = Etablissement::findOne($id_etablissement) ;
        
        if($etablissement)
        {
            $periode = date('Y-m', strtotime('-1 month')) ;
            
            $last_facture = Facture::getLastFacture() ;
            if(!$last_facture)
            {
                $reference = 'BAP000001' ;
            }
            else {
                $reference = str_replace('BAP','',$last_facture->reference) ;
                $reference ++ ; 
                $reference = 'BAP'.$reference ;
            }
            
            $facture = new Facture ;
            $facture->id_etablissement = $id_etablissement ;
            $facture->date = date('Y-m-d H:i:s') ;
            $facture->reference = $reference ;
            $facture->ca = $etablissement->getCA($periode) ;
            $facture->montant_ht = $etablissement->getMontantFacturer($periode) ;
            $facture->libelle = 'Facture '.date('m/Y', strtotime('-1 month')) ;
            $facture->texte = 'Utilisation de la plateforme <strong>La boîte à pain</strong> pour le mois : '.date('m/Y', strtotime('-1 month')).'<br />'
                    . 'Chiffre d\'affaire réalisé sur la plateforme : <strong>'.number_format($facture->ca,2).' €</strong> commissionné à <strong>1%</strong>.' ;
            $facture->paye = 0 ;
            $facture->periode = $periode ;
            $facture->save() ;
        }
        
        $this->redirect(['etablissement-admin/index']) ;
    }
    
    public function actionFacturation() 
    {
        $datas_factures = new ActiveDataProvider([
            'query' => Facture::find()
                ->with('etablissement')
                ->orderBy('reference DESC'),
            'pagination' => [
                'pageSize' => 1000,
            ],
        ]);

        return $this->render('facturation', [
            'datas_factures' => $datas_factures,
        ]);
    }
    
    protected function findModel($id)
    {
        if (($model = Etablissement::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
}