|
- <?php
-
-
-
- namespace common\models;
-
- use Yii;
- use common\helpers\GlobalParam;
- use common\models\Order;
- use common\components\ActiveRecordCommon;
-
-
- class Distribution extends ActiveRecordCommon
- {
-
-
- 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'] == $idProduit &&
- $productDistribution['active']) {
- return true;
- }
- }
- }
-
- return false;
- }
-
-
-
- public static function initDistribution($date, $idProducer = 0)
- {
- $distribution = null;
- if ($date != '') {
-
- $paramsDistribution = [
- 'date' => $date
- ];
- if ($idProducer) {
- $paramsDistribution['distribution.id_producer'] = (int)$idProducer;
- } else {
- $idProducer = GlobalParam::getCurrentProducerId();
- }
-
- $distribution = Distribution::searchOne($paramsDistribution);
-
- if (!$distribution) {
- $distribution = new Distribution;
- $distribution->date = $date;
- $distribution->delivery = 1;
- $distribution->id_producer = $idProducer;
- $distribution->save();
- }
- }
-
-
- if ($distribution) {
-
- $countPointSaleDistribution = PointSaleDistribution::searchCount([
- 'id_distribution' => $distribution->id
- ]);
-
- if (!$countPointSaleDistribution) {
- PointSaleDistribution::setAll($distribution->id, true);
- }
- }
-
-
- $products = Product::searchAll();
-
- if ($distribution) {
- $productsDistribution = ProductDistribution::searchAll([
- 'id_distribution' => $distribution->id
- ]);
- if (!count($productsDistribution)) {
- foreach ($products as $product) {
- $distribution->linkProduct($product);
- }
- }
- $distribution->linkProductGift();
- }
-
- return $distribution;
- }
-
-
-
- public static function getIncomingDistributions()
- {
- $distributionsArray = Distribution::find()
- ->where('date > \'' . date('Y-m-d') . '\'')
- ->andWhere([
- 'id_producer' => GlobalParam::getCurrentProducerId(),
- 'active' => 1
- ])
- ->orderBy('date ASC')
- ->all();
-
- return $distributionsArray;
- }
-
- public static function filterDistributionsByDateDelay($distributionsArray)
- {
- $producer = GlobalParam::getCurrentProducer() ;
- $dateToday = date('Y-m-d') ;
-
- foreach($distributionsArray as $keyDistribution => $distribution) {
-
- $dateDistribution = $distribution->date ;
- $dayDistribution = strtolower(date('l', strtotime($dateDistribution))) ;
- $fieldDayDistributionDelay = 'order_delay_'.$dayDistribution ;
- $fieldDayDistributionDeadline = 'order_deadline_'.$dayDistribution ;
-
- $delay = $producer->order_delay ;
- $deadline = $producer->order_deadline ;
-
- if($producer->$fieldDayDistributionDelay) {
- $delay = $producer->$fieldDayDistributionDelay ;
- }
-
- if($producer->$fieldDayDistributionDeadline) {
- $deadline = $producer->$fieldDayDistributionDeadline ;
- }
-
- $countDaysTodayDistribution = number_format((strtotime($dateDistribution) - strtotime($dateToday)) / (24 * 60 * 60) , 0);
-
- if(date('H') >= $deadline) {
- $countDaysTodayDistribution -- ;
- }
-
- if($countDaysTodayDistribution < $delay) {
- unset($distributionsArray[$keyDistribution]) ;
- }
- }
-
- $newDistributionsArray = [] ;
- foreach($distributionsArray as $distribution) {
- $newDistributionsArray[] = $distribution ;
- }
-
- return $newDistributionsArray ;
- }
-
-
-
- public static function linkProductIncomingDistributions($product)
- {
- $distributionsArray = self::getIncomingDistributions();
- foreach ($distributionsArray as $distribution) {
- $distribution->linkProduct($product);
- }
- }
-
-
-
- public function linkProduct($product)
- {
- $productDistribution = ProductDistribution::searchOne([
- 'id_distribution' => $this->id,
- 'id_product' => $product->id
- ]);
-
- if (!$productDistribution) {
- $productDistribution = new ProductDistribution();
- $productDistribution->id_distribution = $this->id;
- $productDistribution->id_product = $product->id;
- }
-
- $dayDistribution = date('N', strtotime($this->date));
- $daysArray = [
- 1 => 'monday',
- 2 => 'tuesday',
- 3 => 'wednesday',
- 4 => 'thursday',
- 5 => 'friday',
- 6 => 'saturday',
- 7 => 'sunday',
- ];
-
- $productDistribution->active = 0;
- $day = $daysArray[$dayDistribution];
- if ($product->active && $product->$day) {
- $productDistribution->active = 1;
- }
-
- $productDistribution->quantity_max = $product->quantity_max;
- $fieldQuantityMax = 'quantity_max_'.$day ;
- if(isset($product->$fieldQuantityMax) && $product->$fieldQuantityMax > 0) {
- $productDistribution->quantity_max = $product->$fieldQuantityMax ;
- }
- $productDistribution->save();
-
-
- $ordersArray = Order::searchAll([
- 'distribution.date' => $this->date,
- 'distribution.id_producer' => $this->id_producer
- ],
- [
- 'conditions' => 'date_delete IS NULL AND origin != "user"'
- ]);
- if($ordersArray) {
- foreach($ordersArray as $order) {
- foreach($order->productOrder as $productOrder) {
- if($productOrder->id_product == $product->id) {
- $productOrder->price = $product->price ;
- $productOrder->save() ;
- }
- }
- }
- }
-
- return $productDistribution;
- }
-
-
-
- public function linkProductGift()
- {
- $productGift = Product::getProductGift();
-
- if ($productGift) {
- $productDistribution = ProductDistribution::searchOne([
- 'id_distribution' => $this->id,
- 'id_product' => $productGift->id
- ]);
-
- if (!$productDistribution) {
- $productDistribution = new ProductDistribution();
- $productDistribution->id_distribution = $this->id;
- $productDistribution->id_product = $productGift->id;
- $productDistribution->active = 1;
- $productDistribution->save();
- }
- }
- }
-
-
-
- public static function linkPointSaleIncomingDistributions($pointSale)
- {
- $distributionsArray = self::getIncomingDistributions();
- foreach ($distributionsArray as $distribution) {
- $distribution->linkPointSale($pointSale);
- }
- }
-
-
-
- public function linkPointSale($pointSale)
- {
- $pointSaleDistribution = PointSaleDistribution::searchOne([
- 'id_distribution' => $this->id,
- 'id_point_sale' => $pointSale->id
- ]);
-
- if (!$pointSaleDistribution) {
- $pointSaleDistribution = new PointSaleDistribution();
- $pointSaleDistribution->id_distribution = $this->id;
- $pointSaleDistribution->id_point_sale = $pointSale->id;
- }
-
- $dayDistribution = date('N', strtotime($this->date));
- $daysArray = [
- 1 => 'monday',
- 2 => 'tuesday',
- 3 => 'wednesday',
- 4 => 'thursday',
- 5 => 'friday',
- 6 => 'saturday',
- 7 => 'sunday',
- ];
-
- $pointSaleDistribution->delivery = 0;
- $deliveryDay = 'delivery_' . $daysArray[$dayDistribution];
- if ($pointSale->$deliveryDay) {
- $pointSaleDistribution->delivery = 1;
- }
-
- $pointSaleDistribution->save();
- }
-
-
-
- public function active($active = true)
- {
- PointSaleDistribution::setAll($this->id, true);
- $this->active = (int)$active;
- $this->save();
-
- if ($active) {
-
- Subscription::addAll($this->date);
- }
- }
-
- }
|