|
- <?php
-
-
-
- namespace backend\controllers;
-
- use common\helpers\GlobalParam;
- use domain\Document\Quotation\QuotationSearch;
- use domain\Order\Order\Order;
- use yii\base\UserException;
- use yii\filters\AccessControl;
- use yii\helpers\Html;
-
- class QuotationController extends DocumentController
- {
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::class,
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return $this->getUserModule()
- ->getAuthorizationChecker()
- ->isGrantedAsProducer($this->getUserCurrent());
- }
- ]
- ],
- ],
- ];
- }
-
-
-
- public function actionIndex()
- {
- $searchModel = new QuotationSearch();
- $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
-
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
-
- public function actionTransform($id)
- {
- $quotationModule = $this->getQuotationModule();
- $invoiceModule = $this-> getInvoiceModule();
- $quotation = $this->findModel($id);
- if ($quotationModule->isStatusValid($quotation)) {
-
- $invoice = $invoiceModule->instanciateInvoice();
- $invoiceModule->initTaxCalculationMethod($invoice);
- $invoice->id_producer = GlobalParam::getCurrentProducerId();
- $invoice->id_user = $quotation->id_user;
- $invoice->address = $quotation->address;
- $invoice->comment = $quotation->comment;
- $invoice->name = str_replace(['Devis', 'devis'], 'Facture', $quotation->name);
- $invoiceModule->saveCreate($invoice);
-
- Order::updateAll([
- 'order.id_invoice' => $invoice->id
- ], [
- 'order.id_quotation' => $id
- ]);
-
- $this->setFlash('success', 'Le devis <strong>' . Html::encode($quotation->name) . '</strong> a bien été transformé en facture.');
- return $this->redirect(['/' . $this->getControllerUrl() . '/index']);
- } else {
- throw new UserException('Vous ne pouvez pas transformer en facture un devis non validé.');
- }
- }
- }
|