- <?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\Producer;
- use yii\data\ActiveDataProvider;
- use common\models\Invoice;
- use common\models\Product;
- use common\models\Order;
- use common\models\Subscription;
-
-
- class ProducerAdminController 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 User::getCurrentStatus() == USER::STATUS_ADMIN;
- }
- ]
- ],
- ],
- ];
- }
-
-
-
- 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
- ]);
- }
-
-
-
- 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,
- ]);
- }
- }
-
-
-
- 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 <strong>distrib</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>.';
- $invoice->paid = 0;
- $invoice->period = $period;
- $invoice->save();
- }
-
- $this->redirect(['producer-admin/index']);
- }
-
-
-
- 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)
- {
-
- $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' ;
- }
-
-
-
- protected function findModel($id) {
- if (($model = Producer::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
-
- }
|