[ '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', 'delivery_cost', ], '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', 'Менеджер'), 'delivery_cost' => 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() { return $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; } } } public function totalRecount() { $products = $this->products; $newTotal = 0; foreach ($products as $product) { if ($product->removed) continue; $newTotal += $product->count * $product->price; } $this->total = $newTotal; $this->save(); } }