Blame view

common/models/Order.php 8.14 KB
d8c1a2e0   Yarik   Big commit artbox
1
2
3
4
  <?php
      namespace common\models;
      
      use Yii;
5c2eb7c8   Yarik   Big commit almost...
5
6
      use yii\db\ActiveRecord;
      use yii\db\Expression;
d8c1a2e0   Yarik   Big commit artbox
7
8
9
      use yii\web\Session;
      use common\modules\product\models\ProductVariant;
      
5c2eb7c8   Yarik   Big commit almost...
10
      /**
8af13427   Yarik   For leha commit.
11
12
13
       * Class order
       *
       * @todo    Write docs and refactor
5c2eb7c8   Yarik   Big commit almost...
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
       * @package common\models
       * @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
       */
8af13427   Yarik   For leha commit.
42
      class order extends ActiveRecord
d8c1a2e0   Yarik   Big commit artbox
43
44
45
46
47
48
49
50
      {
          
          const SCENARIO_QUICK = 'quick';
          
          private $data;
          
          public static function tableName()
          {
8af13427   Yarik   For leha commit.
51
              return 'order';
d8c1a2e0   Yarik   Big commit artbox
52
53
54
55
          }
          
          public function scenarios()
          {
8af13427   Yarik   For leha commit.
56
57
58
59
60
61
              $scenarios = array_merge(
                  parent::scenarios(),
                  [
                      self::SCENARIO_QUICK => [ 'phone' ],
                  ]
              );
d8c1a2e0   Yarik   Big commit artbox
62
63
64
65
66
67
68
69
              return $scenarios;
          }
          
          public function rules()
          {
              return [
                  [
                      [
d8c1a2e0   Yarik   Big commit artbox
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
                          'phone',
                      ],
                      'required',
                  ],
                  [
                      [ 'comment' ],
                      'safe',
                  ],
                  [
                      [ 'email' ],
                      'email',
                  ],
                  [
                      [ 'phone' ],
                      'match',
                      'pattern' => '/^\+38\(\d{3}\)\d{3}-\d{2}-\d{2}$/',
5c2eb7c8   Yarik   Big commit almost...
86
                      'on'      => self::SCENARIO_QUICK,
d8c1a2e0   Yarik   Big commit artbox
87
                  ],
36d1807a   Yarik   Big commit.
88
89
                  [
                      [
cc658b4c   Yarik   Big commit
90
                          'name',
36d1807a   Yarik   Big commit.
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
                          'phone2',
                          'numbercard',
                          'body',
                          'declaration',
                          'stock',
                          'consignment',
                          'payment',
                          'insurance',
                          'amount_imposed',
                          'shipping_by',
                          'city',
                          'adress',
                          'total',
                          'status',
                      ],
                      'string',
                      'max' => 255,
                  ],
d8c1a2e0   Yarik   Big commit artbox
109
110
111
112
113
114
              ];
          }
          
          public function attributeLabels()
          {
              return [
b569ac34   Eugeny Galkovskiy   MESSAGES !!!! EVGEN!
115
116
117
118
                  'name'    => Yii::t('app', 'order_name'),
                  'phone'   => Yii::t('app', 'order_phone'),
                  'email'   => Yii::t('app', 'order_email'),
                  'comment' => Yii::t('app', 'order_comment'),
d8c1a2e0   Yarik   Big commit artbox
119
120
121
122
123
124
              ];
          }
          
          public function beforeSave($insert)
          {
              $this->user_id = Yii::$app->user->id;
5c2eb7c8   Yarik   Big commit almost...
125
              $this->date_time = new Expression('NOW()');
d8c1a2e0   Yarik   Big commit artbox
126
127
128
129
130
131
132
133
              return parent::beforeSave($insert);
          }
          
          public function beforeDelete()
          {
              return parent::beforeDelete();
          }
          
8af13427   Yarik   For leha commit.
134
          public function addBasket($product_variant_id, $count)
d8c1a2e0   Yarik   Big commit artbox
135
136
137
138
139
          {
              $session = new Session;
              $session->open();
              $data = $session[ 'basket' ];
              $i = 0;
8af13427   Yarik   For leha commit.
140
141
142
              if (isset( $session[ 'basket' ] )) {
                  foreach ($session[ 'basket' ] as $key => $basket) {
                      if ($product_variant_id == $basket[ 'id' ]) {
d8c1a2e0   Yarik   Big commit artbox
143
144
145
146
147
148
                          $data[ $key ][ 'count' ] += $count;
                          $session[ 'basket' ] = $data;
                          $i++;
                      }
                  }
              }
8af13427   Yarik   For leha commit.
149
              if ($i == 0) {
d8c1a2e0   Yarik   Big commit artbox
150
                  $data[] = [
8af13427   Yarik   For leha commit.
151
                      'id'    => $product_variant_id,
d8c1a2e0   Yarik   Big commit artbox
152
153
154
155
156
157
158
159
160
161
162
163
                      'count' => $count,
                  ];
                  $session[ 'basket' ] = $data;
              }
          }
          
          public function rowBasket()
          {
              $session = new Session;
              $session->open();
              $cost = 0;
              $count = 0;
8af13427   Yarik   For leha commit.
164
165
              if (isset( $session[ 'basket' ] ) && count($session[ 'basket' ])) {
                  foreach ($session[ 'basket' ] as $product) {
d8c1a2e0   Yarik   Big commit artbox
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
                      $count += $product[ 'count' ];
                  }
              }
              
              return (object) [
                  'cost'  => $cost,
                  'count' => $count,
              ];
          }
          
          public function deleteBasketMod($id)
          {
              $session = new Session;
              $session->open();
              $basket = $session[ 'basket' ];
8af13427   Yarik   For leha commit.
181
182
              foreach ($basket as $key => $product) {
                  if ($id == $product[ 'id' ]) {
d8c1a2e0   Yarik   Big commit artbox
183
184
185
186
187
188
189
190
191
192
193
                      unset( $basket[ $key ] );
                  }
              }
              $session[ 'basket' ] = $basket;
          }
          
          public function updateBasket($row)
          {
              $session = new Session;
              $session->open();
              //$data = array();
8af13427   Yarik   For leha commit.
194
              if ($row[ 'count' ] > 0) {
d8c1a2e0   Yarik   Big commit artbox
195
196
197
198
199
200
201
202
203
204
205
206
207
                  $this->data[] = [
                      'id'    => $row[ 'id' ],
                      'count' => $row[ 'count' ],
                  ];
              }
              $session[ 'basket' ] = $this->data;
          }
          
          public function getBasketMods()
          {
              $session = new Session;
              $session->open();
              $products = [];
8af13427   Yarik   For leha commit.
208
              if (empty( $session[ 'basket' ] )) {
d8c1a2e0   Yarik   Big commit artbox
209
210
                  return [];
              }
8af13427   Yarik   For leha commit.
211
              foreach ($session[ 'basket' ] as $product) {
d8c1a2e0   Yarik   Big commit artbox
212
                  $row = ProductVariant::find()
8af13427   Yarik   For leha commit.
213
214
215
216
217
218
219
                                       ->select(
                                           [
                                               'product_variant.*',
                                               'product.name as productName',
                                               'product.alias',
                                           ]
                                       )
4428da8c   Yarik   Almost all databa...
220
                                       ->where([ 'product_variant.id' => $product[ 'id' ] ])
8af13427   Yarik   For leha commit.
221
                                       ->leftJoin('product', 'product.id = product_variant.product_id')
d8c1a2e0   Yarik   Big commit artbox
222
223
224
225
226
227
228
229
230
231
232
233
234
235
                                       ->one();
                  $row->count = $product[ 'count' ];
                  $row->sum_cost = $product[ 'count' ] * $row->price;
                  $products[] = $row;
              }
              
              return $products;
          }
          
          public function getSumCost()
          {
              $session = new Session;
              $session->open();
              $cost = 0;
8af13427   Yarik   For leha commit.
236
              if (empty( $session[ 'basket' ] )) {
d8c1a2e0   Yarik   Big commit artbox
237
238
                  return false;
              }
8af13427   Yarik   For leha commit.
239
              foreach ($session[ 'basket' ] as $product) {
d8c1a2e0   Yarik   Big commit artbox
240
241
242
243
244
245
                  $cost += ( $this->getModCost($product[ 'id' ]) * $product[ 'count' ] );
              }
              
              return $cost;
          }
          
8af13427   Yarik   For leha commit.
246
          private function getModCost($product_variant_id)
d8c1a2e0   Yarik   Big commit artbox
247
          {
5c2eb7c8   Yarik   Big commit almost...
248
249
250
              /**
               * @var ProductVariant $mod
               */
d8c1a2e0   Yarik   Big commit artbox
251
              $mod = ProductVariant::find()
4428da8c   Yarik   Almost all databa...
252
                                   ->where([ 'id' => $product_variant_id ])
d8c1a2e0   Yarik   Big commit artbox
253
254
255
256
257
258
259
260
261
                                   ->one();
              
              return $mod->price;
          }
          
          public function clearBasket()
          {
              $session = new Session;
              $session->open();
8af13427   Yarik   For leha commit.
262
              $session[ 'basket' ] = null;
d8c1a2e0   Yarik   Big commit artbox
263
264
265
266
267
268
269
270
271
          }
          
          public function getUser()
          {
              return $this->hasOne(User::className(), [ 'id' => 'user_id' ]);
          }
          
          public function getProducts()
          {
8af13427   Yarik   For leha commit.
272
              return $this->hasMany(orderProduct::className(), [ 'order_id' => 'id' ]);
d8c1a2e0   Yarik   Big commit artbox
273
274
          }
      }