Order.php 9.28 KB
<?php
    namespace artweb\artbox\ecommerce\models;
    
    use artweb\artbox\models\Customer;
    use common\behaviors\DefaultLabelBehavior;
    use common\models\User;
    use Yii;
    use yii\behaviors\TimestampBehavior;
    use yii\db\ActiveRecord;
    
    /**
     * Class Order
     *
     * @todo    Write docs and refactor
     * @package artweb\artbox\ecommerce\models
     * @property integer $created_at
     * @property integer $updated_at
     * @property integer $deleted_at
     * @property integer $deadline
     * @property boolean $wasted
     * @property integer $reason
     * @property string  $check
     * @property string  $sms
     * @property int     $id
     * @property integer $edit_id
     * @property integer $edit_time
     * @property integer $manager_id
     * @property int     $user_id
     * @property string  $name
     * @property string  $phone
     * @property string  $phone2
     * @property string  $email
     * @property string  $adress
     * @property string  $body
     * @property double  $total
     * @property string  $date_time
     * @property string  $date_dedline
     * @property string  $reserve
     * @property string  $status
     * @property string  $comment
     * @property int     $label
     * @property int     $pay
     * @property int     $numbercard
     * @property int     $delivery
     * @property string  $declaration
     * @property string  $stock
     * @property string  $consignment
     * @property string  $payment
     * @property string  $insurance
     * @property double  $amount_imposed
     * @property string  $shipping_by
     * @property string  $city
     * @property string  $deliveryString
     */
    class Order extends ActiveRecord
    {
        
        const SHIPPING_BY = [
            1 => [
                'label' => 'Отправитель',
            ],
            2 => [
                'label' => 'Получатель',
            ],
        ];
        
        const REASONS = [
            1  => 'Нет товара',
            2  => 'Нет оплаты',
            3  => 'Передумал',
            4  => ' - Купил в другом месте',
            5  => ' - Не подошли условия доставки',
            6  => ' - Не подошел срок доставки',
            7  => ' - Нет денег',
            8  => ' - Купит позже',
            9  => 'Купил в другом месте',
            10 => 'Подьедет в маг.',
            11 => 'Дубль заказа.',
            12 => 'Другое',
            13 => 'Брак',
            14 => 'Отказался от Самовывоза',
            15 => 'Не приехал за Самовывозом',
            16 => 'Отменил заказ',
            17 => 'Не берет трубку',
        ];
        
        public static function tableName()
        {
            return 'order';
        }
        
        public function behaviors()
        {
            return [
                [
                    'class' => TimestampBehavior::className(),
                ],
                [
                    'class' => DefaultLabelBehavior::className(),
                ],
            ];
        }
        
        public function rules()
        {
            return [
                [
                    [ 'pay' ],
                    'boolean',
                ],
                [
                    [
                        'shipping_by',
                        'created_at',
                        'updated_at',
                        'deleted_at',
                        'payment',
                        'reason',
                        'label',
                        'manager_id',
                        'edit_time',
                        'edit_id',
                    ],
                    'integer',
                ],
                [
                    [ 'total' ],
                    'double',
                ],
                [
                    [
                        'phone',
                    ],
                    'required',
                ],
                [
                    [ 'comment' ],
                    'string',
                ],
                [
                    [ 'email' ],
                    'email',
                ],
                [
                    [
                        'phone',
                        'phone2',
                    ],
                    'match',
                    'pattern' => '/^\+38\(\d{3}\)\d{3}-\d{2}-\d{2}$/',
                ],
                [
                    [
                        'deadline',
                        'name',
                        'numbercard',
                        'body',
                        'declaration',
                        'stock',
                        'consignment',
                        'insurance',
                        'amount_imposed',
                        'city',
                        'adress',
                        'status',
                        'check',
                        'sms',
                    ],
                    'string',
                    'max' => 255,
                ],
            ];
        }
        
        public function afterFind()
        {
            parent::afterFind();
            $this->deadline = !empty( $this->deadline ) ? date('d.m.Y', $this->deadline) : '';
            
        }
        
        public function beforeSave($insert)
        {
            if (parent::beforeSave($insert)) {
                
                $this->convertDate();
                return true;
            }
            return false;
            
        }
        
        protected function convertDate()
        {
            if (!empty( $this->deadline )) {
                $date = new \DateTime($this->deadline);
                $date->format("d.m.Y");
                $this->deadline = $date->getTimestamp();
                
            }
            
        }
        
        public function attributeLabels()
        {
            return [
                'name'        => Yii::t('app', 'order_name'),
                'phone'       => Yii::t('app', 'order_phone'),
                'email'       => Yii::t('app', 'order_email'),
                'comment'     => Yii::t('app', 'order_comment'),
                'created_at'  => Yii::t('app', 'Дата добавления'),
                'updated_at'  => Yii::t('app', 'Дата обновления'),
                'deleted_at'  => Yii::t('app', 'Дата удаления'),
                'deadline'    => Yii::t('app', 'Дедлайн'),
                'reason'      => Yii::t('app', 'Причина'),
                'check'       => Yii::t('app', 'Чек'),
                'sms'         => Yii::t('app', 'СМС'),
                'consignment' => Yii::t('app', 'Номер накладной'),
                'manager_id'  => Yii::t('app', 'Менеджер'),
            ];
        }
        
        public function getUser()
        {
            return $this->hasOne(Customer::className(), [ 'id' => 'user_id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getProducts()
        {
            return $this->hasMany(OrderProduct::className(), [ 'order_id' => 'id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getOrderDelivery()
        {
            return $this->hasOne(Delivery::className(), [ 'id' => 'delivery' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getOrderLabel()
        {
            return $this->hasOne(Label::className(), [ 'id' => 'label' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getOrderPayment()
        {
            return $this->hasOne(OrderPayment::className(), [ 'id' => 'payment' ]);
        }
        
        /**
         * @return string
         */
        public function getDeliveryString()
        {
            if (!empty( $this->orderDelivery )) {
                if (!empty( $this->orderDelivery->parent )) {
                    return $this->orderDelivery->parent->lang->title . ': ' . $this->orderDelivery->lang->title;
                } else {
                    return $this->orderDelivery->lang->title;
                }
            } else {
                return '';
            }
        }
        
        /**
         * If deadline is fucked up returns true,
         * if deadline is not setted return false, like everything is ok
         *
         * @return bool
         */
        public function getWasted()
        {
            if (empty( $this->deadline )) {
                return false;
            } else {
                return time() > strtotime($this->deadline);
            }
        }
        
        /**
         *
         */
        public function getManager()
        {
            $this->hasOne(User::className(), [ 'id' => 'manager_id' ]);
        }
        
        public function isBlocked()
        {
            if ($this->edit_id === 0) {
                return false;
            } else {
                if ($this->edit_time + 7200 > time()) {
                    return true;
                } else {
                    return false;
                }
            }
        }
    }