[ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['post'], ], ], 'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return User::getCurrentStatus() == USER::STATUS_ADMIN; } ] ], ], ]; } /** * Liste les producteurs. * * @return mixed */ public function actionIndex() { $dataProviderProducer = new ActiveDataProvider([ 'query' => Producer::find() ->with('userProducer', 'user') ->orderBy('active DESC, free_price DESC'), 'pagination' => [ 'pageSize' => 1000, ], ]); $producersArray = Producer::find()->where('active = 1')->all(); $sumFreePrice = 0 ; foreach($producersArray as $producer) { $sumFreePrice += $producer->free_price ; } return $this->render('index', [ 'dataProviderProducer' => $dataProviderProducer, 'sumFreePrice' => $sumFreePrice ]); } /** * Crée un producteur. * * @return mixed */ public function actionCreate() { $model = new Producer(); if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->getSession()->setFlash('success', 'Producteur créé.'); return $this->redirect(['index']); } else { return $this->render('create', [ 'model' => $model, ]); } } /** * Génère la facture mensuelle d'un producteur. * * @param integer $idProducer */ public function actionBill($idProducer) { $producer = Producer::findOne($idProducer); if ($producer) { $period = date('Y-m', strtotime('-1 month')); $last_invoice = Invoice::getLastInvoice() ; if (!$last_invoice) { $reference = 'BAP000001'; } else { $reference = str_replace('BAP', '', $last_invoice->reference); $reference ++; $reference = 'BAP' . $reference; } $invoice = new Invoice; $invoice->id_producer = $idProducer; $invoice->date = date('Y-m-d H:i:s'); $invoice->reference = $reference; $invoice->turnover = $producer->getTurnover($period); $invoice->amount_ht = $producer->getFreePrice() ; $invoice->wording = 'Facture ' . date('m/Y', strtotime('-1 month')); $invoice->text = 'Utilisation de la plateforme distrib pour le mois : ' . date('m/Y', strtotime('-1 month')) . '
' . 'Chiffre d\'affaire réalisé sur la plateforme : ' . number_format($facture->ca, 2) . ' € commissionné à 1%.'; $invoice->paid = 0; $invoice->period = $period; $invoice->save(); } $this->redirect(['producer-admin/index']); } /** * Liste les factures des producteurs. * * @return mxied */ public function actionBilling() { $dataProviderInvoice = new ActiveDataProvider([ 'query' => Invoice::find() ->with('producer') ->orderBy('reference DESC'), 'pagination' => [ 'pageSize' => 1000, ], ]); return $this->render('billing', [ 'dataProviderInvoice' => $dataProviderInvoice, ]); } public function actionProducerInstallTaxUpdatePrices($idProducer) { // product $productsArray = Product::searchAll([ 'id_producer' => $idProducer ]) ; $connection = Yii::$app->getDb(); foreach($productsArray as $product) { $product->price = round($product->price / (1 + $product->taxRate->value), 2) ; $product->save() ; $command = $connection->createCommand(" UPDATE `product_order` SET price = ROUND(price / (1 + :tax_value), 2), id_tax_rate = :id_tax_rate WHERE id_product = :id_product", [ ':id_product' => $product->id, ':tax_value' => $product->taxRate->value, ':id_tax_rate' => $product->taxRate->id, ]); $result = $command->query(); } echo 'ok' ; } /** * Recherche un établissement. * * @param integer $id * @return Etablissement * @throws NotFoundHttpException */ protected function findModel($id) { if (($model = Producer::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } }