Blame view

models/Order.php 7.26 KB
8a7e6ecf   Yarik   Namespaces
1
2
3
4
5
  <?php
      namespace artweb\artbox\ecommerce\models;
      
      use artweb\artbox\models\Customer;
      use Yii;
2b29877e   Alexey Boroda   -Added time colum...
6
      use yii\behaviors\TimestampBehavior;
8a7e6ecf   Yarik   Namespaces
7
      use yii\db\ActiveRecord;
8a7e6ecf   Yarik   Namespaces
8
9
10
11
12
13
      
      /**
       * Class Order
       *
       * @todo    Write docs and refactor
       * @package artweb\artbox\ecommerce\models
2b29877e   Alexey Boroda   -Added time colum...
14
15
16
       * @property integer $created_at
       * @property integer $updated_at
       * @property integer $deleted_at
ccfc5763   Alexey Boroda   -Order in process 3
17
       * @property integer $deadline
ce75bc1d   Alexey Boroda   -Grid view half w...
18
       * @property boolean $wasted
ccfc5763   Alexey Boroda   -Order in process 3
19
20
21
       * @property integer $reason
       * @property string  $check
       * @property string  $sms
2b29877e   Alexey Boroda   -Added time colum...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
       * @property int     $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
25b21043   Administrator   add create_item t...
48
       * @property string  $deliveryString
8a7e6ecf   Yarik   Namespaces
49
50
51
       */
      class Order extends ActiveRecord
      {
bb962a6d   Alexey Boroda   -Order in process
52
53
54
55
56
57
58
59
60
          
          const SHIPPING_BY = [
              1 => [
                  'label' => 'Отправитель',
              ],
              2 => [
                  'label' => 'Получатель',
              ],
          ];
8a7e6ecf   Yarik   Namespaces
61
62
63
64
65
66
          
          public static function tableName()
          {
              return 'order';
          }
          
2b29877e   Alexey Boroda   -Added time colum...
67
68
69
70
71
72
73
74
75
          public function behaviors()
          {
              return [
                  [
                      'class' => TimestampBehavior::className(),
                  ],
              ];
          }
          
8a7e6ecf   Yarik   Namespaces
76
77
78
79
          public function rules()
          {
              return [
                  [
ccfc5763   Alexey Boroda   -Order in process 3
80
                      [ 'pay' ],
bb962a6d   Alexey Boroda   -Order in process
81
82
83
                      'boolean',
                  ],
                  [
8a7e6ecf   Yarik   Namespaces
84
                      [
2b29877e   Alexey Boroda   -Added time colum...
85
86
87
                          'created_at',
                          'updated_at',
                          'deleted_at',
ccfc5763   Alexey Boroda   -Order in process 3
88
89
90
                          'payment',
                          'reason',
                          'label',
2b29877e   Alexey Boroda   -Added time colum...
91
92
93
94
95
                      ],
                      'integer',
                  ],
                  [
                      [
8a7e6ecf   Yarik   Namespaces
96
97
98
99
100
101
                          'phone',
                      ],
                      'required',
                  ],
                  [
                      [ 'comment' ],
bb962a6d   Alexey Boroda   -Order in process
102
                      'string',
8a7e6ecf   Yarik   Namespaces
103
104
105
106
107
108
                  ],
                  [
                      [ 'email' ],
                      'email',
                  ],
                  [
bb962a6d   Alexey Boroda   -Order in process
109
110
111
112
                      [
                          'phone',
                          'phone2',
                      ],
8a7e6ecf   Yarik   Namespaces
113
114
                      'match',
                      'pattern' => '/^\+38\(\d{3}\)\d{3}-\d{2}-\d{2}$/',
8a7e6ecf   Yarik   Namespaces
115
116
117
                  ],
                  [
                      [
ccfc5763   Alexey Boroda   -Order in process 3
118
                          'deadline',
8a7e6ecf   Yarik   Namespaces
119
                          'name',
8a7e6ecf   Yarik   Namespaces
120
121
122
123
124
                          'numbercard',
                          'body',
                          'declaration',
                          'stock',
                          'consignment',
8a7e6ecf   Yarik   Namespaces
125
126
127
128
129
130
131
                          'insurance',
                          'amount_imposed',
                          'shipping_by',
                          'city',
                          'adress',
                          'total',
                          'status',
ccfc5763   Alexey Boroda   -Order in process 3
132
133
                          'check',
                          'sms',
8a7e6ecf   Yarik   Namespaces
134
135
136
137
                      ],
                      'string',
                      'max' => 255,
                  ],
8a7e6ecf   Yarik   Namespaces
138
139
140
              ];
          }
          
ccfc5763   Alexey Boroda   -Order in process 3
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
          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();
                  
              }
              
          }
          
8a7e6ecf   Yarik   Namespaces
170
171
172
          public function attributeLabels()
          {
              return [
2b29877e   Alexey Boroda   -Added time colum...
173
174
175
176
177
178
179
                  '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', 'Дата удаления'),
ccfc5763   Alexey Boroda   -Order in process 3
180
181
182
183
                  'deadline'   => Yii::t('app', 'Дедлайн'),
                  'reason'     => Yii::t('app', 'Причина'),
                  'check'      => Yii::t('app', 'Чек'),
                  'sms'        => Yii::t('app', 'СМС'),
8a7e6ecf   Yarik   Namespaces
184
185
186
              ];
          }
          
8a7e6ecf   Yarik   Namespaces
187
188
189
190
          public function getUser()
          {
              return $this->hasOne(Customer::className(), [ 'id' => 'user_id' ]);
          }
ccfc5763   Alexey Boroda   -Order in process 3
191
          
25b21043   Administrator   add create_item t...
192
193
194
          /**
           * @return \yii\db\ActiveQuery
           */
8a7e6ecf   Yarik   Namespaces
195
196
197
198
          public function getProducts()
          {
              return $this->hasMany(OrderProduct::className(), [ 'order_id' => 'id' ]);
          }
ccfc5763   Alexey Boroda   -Order in process 3
199
          
25b21043   Administrator   add create_item t...
200
201
202
          /**
           * @return \yii\db\ActiveQuery
           */
ccfc5763   Alexey Boroda   -Order in process 3
203
204
205
          public function getOrderDelivery()
          {
              return $this->hasOne(Delivery::className(), [ 'id' => 'delivery' ]);
25b21043   Administrator   add create_item t...
206
          }
ccfc5763   Alexey Boroda   -Order in process 3
207
          
25b21043   Administrator   add create_item t...
208
209
210
          /**
           * @return \yii\db\ActiveQuery
           */
ccfc5763   Alexey Boroda   -Order in process 3
211
212
          public function getOrderLabel()
          {
ce75bc1d   Alexey Boroda   -Grid view half w...
213
              return $this->hasOne(Label::className(), [ 'id' => 'label' ]);
25b21043   Administrator   add create_item t...
214
          }
ccfc5763   Alexey Boroda   -Order in process 3
215
          
25b21043   Administrator   add create_item t...
216
217
218
          /**
           * @return \yii\db\ActiveQuery
           */
ccfc5763   Alexey Boroda   -Order in process 3
219
220
221
          public function getOrderPayment()
          {
              return $this->hasOne(OrderPayment::className(), [ 'id' => 'payment' ]);
25b21043   Administrator   add create_item t...
222
          }
ccfc5763   Alexey Boroda   -Order in process 3
223
          
25b21043   Administrator   add create_item t...
224
225
226
          /**
           * @return string
           */
ccfc5763   Alexey Boroda   -Order in process 3
227
228
229
230
231
          public function getDeliveryString()
          {
              if (!empty( $this->orderDelivery )) {
                  if (!empty( $this->orderDelivery->parent )) {
                      return $this->orderDelivery->parent->lang->title . ': ' . $this->orderDelivery->lang->title;
25b21043   Administrator   add create_item t...
232
                  } else {
fbfae870   Administrator   add create_item t...
233
                      return $this->orderDelivery->lang->title;
25b21043   Administrator   add create_item t...
234
235
236
237
238
                  }
              } else {
                  return '';
              }
          }
ce75bc1d   Alexey Boroda   -Grid view half w...
239
240
241
242
243
244
245
246
247
248
249
250
251
252
      
          /**
           * 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() > $this->deadline;
              }
          }
8a7e6ecf   Yarik   Namespaces
253
      }