|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
-
-
-
- namespace common\models;
-
- use Yii;
- use common\models\Order;
- use common\models\Distribution;
-
-
- class Distribution extends \yii\db\ActiveRecord
- {
-
-
- public static function tableName()
- {
- return 'distribution';
- }
-
- public function getProducer()
- {
- return $this->hasOne(Producer::className(), ['id' => 'id_producer']);
- }
-
-
-
- public function rules()
- {
- return [
- [['date'], 'required'],
- [['date'], 'safe'],
- [['active'], 'integer']
- ];
- }
-
-
-
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'date' => 'Date',
- 'active' => 'Actif',
- ];
- }
-
-
-
-
- public function getOrder()
- {
- return $this->hasMany(Order::className(), ['id_distribution' => 'id']);
- }
-
- public function getProductDistribution()
- {
- return $this->hasMany(ProductDistribution::className(), ['id_distribution' => 'id']);
- }
-
-
-
- public static function defaultOptionsSearch() {
- return [
- 'with' => [],
- 'join_with' => [],
- 'orderby' => 'date ASC',
- 'attribute_id_producer' => 'distribution.id_producer'
- ] ;
- }
-
-
-
- public function isActiveProduct($idProduit)
- {
- if ($idProduit &&
- isset($this->productDistribution) &&
- count($this->productDistribution) > 0)
- {
-
- foreach ($this->productDistribution as $productDistribution) {
- if ($productDistribution['id_product'] == $idProduct &&
- $productDistribution['active'])
- {
- return true;
- }
- }
- }
-
- return false;
- }
-
-
-
- public static function initDistribution($date)
- {
- $distribution = null ;
- if ($date != '') {
- $distribution = Distribution::searchOne(['date' => $date]) ;
- if (!$distribution) {
- $distribution = new Distribution;
- $distribution->date = $date;
- $distribution->delivery = 1;
- $distribution->id_producer = Producer::getId();
- $distribution->save();
- }
- }
-
-
- if ($distribution) {
-
- $countPointSaleDistribution = PointSaleDistribution::searchCount([
- 'id_distribution' => $distribution->id
- ]) ;
-
- if (!$countPointSaleDistribution) {
- PointSaleDistribution::setAll($distribution->id, true);
- }
- }
-
-
- $products = Product::searchAll();
-
- if ($distribution) {
- $dayDistribution = date('N', strtotime($distribution->date));
- $productsDistribution = ProductDistribution::searchAll([
- 'id_distribution' => $distribution->id
- ]) ;
-
- if (!count($productsDistribution)) {
- foreach ($products as $product) {
- $productDistribution = new ProductDistribution();
- $productDistribution->id_distribution = $distribution->id;
- $productDistribution->id_product = $product->id;
-
- $productDistribution->actif = 0;
- if ($product->actif && $dayDistribution == 1 && $product->monday) {
- $productDistribution->active = 1;
- }
- if ($product->actif && $dayDistribution == 2 && $product->tuesday) {
- $productDistribution->active = 1;
- }
- if ($product->actif && $dayDistribution == 3 && $product->wednesday) {
- $productDistribution->active = 1;
- }
- if ($product->actif && $dayDistribution == 4 && $product->thursday) {
- $productDistribution->active = 1;
- }
- if ($product->actif && $dayDistribution == 5 && $product->friday) {
- $productDistribution->active = 1;
- }
- if ($product->actif && $dayDistribution == 6 && $product->saterday) {
- $productDistribution->active = 1;
- }
- if ($product->actif && $dayDistribution == 7 && $product->sunday) {
- $productDistribution->active = 1;
- }
-
- $productDistribution->quantity_max = $product->quantity_max;
-
- $productDistribution->save();
- }
- }
- }
-
- return $distribution ;
- }
-
- }
|