|
- <?php
-
-
-
- namespace backend\controllers;
-
- use common\models\User ;
- use common\helpers\GlobalParam ;
-
- class DocumentController extends BackendController
- {
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- ],
- ],
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return User::hasAccessBackend();
- }
- ]
- ],
- ],
- ];
- }
-
- public function actionCreate()
- {
- $class = $this->getClass();
- $model = new $class();
-
- if ($model->load(Yii::$app->request->post())) {
-
- $model->id_producer = GlobalParam::getCurrentProducerId() ;
- $model->reference = $model->generateReference() ;
-
- if($model->save()) {
- Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('create', $model));
- return $this->redirect(['index']);
- }
- else {
- Yii::$app->getSession()->setFlash('error', 'Un problème est survenu lors de la création du document.');
- }
- }
-
- return $this->render('/document/create', [
- 'title' => $this->getTitle('Ajouter'),
- 'typeDocument' => $this->getDocumentType(),
- 'model' => $model,
- ]);
- }
-
-
-
- public function actionUpdate($id)
- {
- $model = $this->findModel($id) ;
-
- $class = $this->getClass();
- $model = $class::searchOne([
- 'id' => $id
- ]) ;
-
- if ($model && $model->load(Yii::$app->request->post()) && $model->save()) {
-
- Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('update', $model));
- }
-
- return $this->render('/document/update', [
- 'title' => $this->getTitle('Modifier'),
- 'typeDocument' => $this->getDocumentType(),
- 'model' => $model,
- ]);
- }
-
- public function actionAjaxAddressUser($idUser)
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
-
- if($idUser > 0) {
- $user = User::findOne($idUser);
-
- if($user) {
- $address = $user['lastname'].' '.$user['name']."\n" ;
- $address .= $user['address'] ;
-
- return [
- 'return' => 'success',
- 'address' => $address
- ] ;
- }
- }
-
- return ['return' => 'error'] ;
- }
-
- public function actionAjaxInit($idDocument, $classDocument)
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
-
- if($idDocument > 0 && Document::isValidClass($classDocument)) {
-
- $document = $classDocument::searchOne([
- 'id' => $idDocument
- ]) ;
-
- if($document) {
- return [
- 'return' => 'success',
- 'address' => $document->address,
- 'idUser' => $document->user->id
- ] ;
- }
- }
-
- return ['return' => 'error'] ;
- }
-
- protected function getClass()
- {
- $class = get_class($this);
- $class = str_replace('Controller', '', $class) ;
- $class = str_replace('backend\controllers\\', '', $class) ;
- return $class ;
- }
-
- protected function getDocumentType()
- {
- $class = $this->getClass();
-
- if($class == 'Invoice') {
- $documentType = 'Facture' ;
- }
- elseif($class == 'DeliveryNote') {
- $documentType = 'Bon de livraison' ;
- }
- elseif($class == 'Quotation') {
- $documentType = 'Devis' ;
- }
-
- if(isset($documentType)) {
- return $documentType ;
- }
-
- return '' ;
- }
-
- public function getFlashMessage($type = 'create', $model)
- {
- $class = $this->getClass();
-
- $message = $this->getDocumentType() ;
- $message .= ' <strong>'.Html::encode($model->name).'</strong> ' ;
-
- if($type == 'create') {
- $message .= 'ajouté' ;
- }
- elseif($type == 'update') {
- $message .= 'modifié' ;
- }
-
- if($class == 'Invoice') {
- $message .= 'e' ;
- }
-
- return $message ;
- }
-
- protected function getTitle($prepend)
- {
- $class = $this->getClass();
-
- switch ($class) {
- case 'Invoice' :
- $title = $prepend . ' une facture';
- break;
- case 'DeliveryNote' :
- $title = $prepend . ' un bon de livraison';
- break;
- case 'Quotation' :
- $title = $prepend . ' un devis';
- break;
- }
-
- return $title;
- }
-
-
-
- protected function findModel($id)
- {
- $class = $this->getClass() ;
-
- if (($model = $class::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
-
- }
|