Blame view

common/models/Orders.php 6.32 KB
4253cbec   root   first commit
1
  <?php
7f87d6f9   Yarik   Modals.
2
3
4
5
6
      namespace common\models;
      
      use Yii;
      use yii\web\Session;
      use common\modules\product\models\ProductVariant;
8467c8d3   Yarik   Google ecommerce
7
8
9
10
11
12
  
      /**
       * Class Orders
       *
       * @property OrdersProducts[] $products
       */
7f87d6f9   Yarik   Modals.
13
      class Orders extends \yii\db\ActiveRecord
4253cbec   root   first commit
14
      {
7f87d6f9   Yarik   Modals.
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
          
          const SCENARIO_QUICK = 'quick';
          
          private $data;
          
          public static function tableName()
          {
              return 'orders';
          }
          
          public function scenarios()
          {
              $scenarios = array_merge(parent::scenarios(), [
                  self::SCENARIO_QUICK => [ 'phone' ],
              ]);
              return $scenarios;
          }
          
          public function rules()
          {
              return [
                  [
                      [
                          'name',
                          'phone',
                      ],
                      'required',
                  ],
                  [
                      [ 'comment' ],
                      'safe',
                  ],
                  [
                      [ 'email' ],
                      'email',
                  ],
                  [
                      [ 'phone' ],
                      'match',
                      'pattern' => '/^\+38\(\d{3}\)\d{3}-\d{2}-\d{2}$/',
                      'on' => self::SCENARIO_QUICK,
                  ],
              ];
          }
          
          public function attributeLabels()
          {
              return [
                  'name'    => 'Ф.И.О',
                  'phone'   => 'Контактный телефон',
                  'email'   => 'email',
                  'comment' => 'Комментарии',
              ];
          }
          
          public function beforeSave($insert)
          {
              $this->user_id = Yii::$app->user->id;
              $this->date_time = new \yii\db\Expression('NOW()');
              return parent::beforeSave($insert);
          }
          
          public function beforeDelete()
          {
              return parent::beforeDelete();
          }
          
          public function addBasket($mod_id, $count)
          {
              $session = new Session;
              $session->open();
              $data = $session[ 'basket' ];
              $i = 0;
              if(isset( $session[ 'basket' ] )) {
                  foreach($session[ 'basket' ] as $key => $basket) {
                      if($mod_id == $basket[ 'id' ]) {
                          $data[ $key ][ 'count' ] += $count;
                          $session[ 'basket' ] = $data;
                          $i++;
                      }
4253cbec   root   first commit
95
96
                  }
              }
7f87d6f9   Yarik   Modals.
97
98
99
100
101
102
103
              if($i == 0) {
                  $data[] = [
                      'id'    => $mod_id,
                      'count' => $count,
                  ];
                  $session[ 'basket' ] = $data;
              }
4253cbec   root   first commit
104
          }
7f87d6f9   Yarik   Modals.
105
106
          
          public function rowBasket()
4253cbec   root   first commit
107
          {
7f87d6f9   Yarik   Modals.
108
109
110
111
112
113
114
115
116
117
118
119
120
121
              $session = new Session;
              $session->open();
              $cost = 0;
              $count = 0;
              if(isset( $session[ 'basket' ] ) && count($session[ 'basket' ])) {
                  foreach($session[ 'basket' ] as $product) {
                      $count += $product[ 'count' ];
                  }
              }
              
              return (object) [
                  'cost'  => $cost,
                  'count' => $count,
              ];
4253cbec   root   first commit
122
          }
7f87d6f9   Yarik   Modals.
123
124
125
126
127
128
129
130
131
132
          
          public function deleteBasketMod($id)
          {
              $session = new Session;
              $session->open();
              $basket = $session[ 'basket' ];
              foreach($basket as $key => $product) {
                  if($id == $product[ 'id' ]) {
                      unset( $basket[ $key ] );
                  }
4253cbec   root   first commit
133
              }
7f87d6f9   Yarik   Modals.
134
              $session[ 'basket' ] = $basket;
4253cbec   root   first commit
135
          }
7f87d6f9   Yarik   Modals.
136
137
          
          public function updateBasket($row)
4253cbec   root   first commit
138
          {
7f87d6f9   Yarik   Modals.
139
140
141
142
143
144
145
146
147
148
              $session = new Session;
              $session->open();
              //$data = array();
              if($row[ 'count' ] > 0) {
                  $this->data[] = [
                      'id'    => $row[ 'id' ],
                      'count' => $row[ 'count' ],
                  ];
              }
              $session[ 'basket' ] = $this->data;
4253cbec   root   first commit
149
          }
7f87d6f9   Yarik   Modals.
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
          
          public function getBasketMods()
          {
              $session = new Session;
              $session->open();
              $products = [];
              if(empty( $session[ 'basket' ] )) {
                  return [];
              }
              foreach($session[ 'basket' ] as $product) {
                  $row = ProductVariant::find()
                                       ->select([
                                           'product_variant.*',
                                           'product.name as product_name',
                                           'product.alias',
                                       ])
                                       ->where([ 'product_variant.product_variant_id' => $product[ 'id' ] ])
                                       ->leftJoin('product', 'product.product_id = product_variant.product_id')
                                       ->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;
              if(empty( $session[ 'basket' ] )) {
                  return false;
              }
              foreach($session[ 'basket' ] as $product) {
                  $cost += ( $this->getModCost($product[ 'id' ]) * $product[ 'count' ] );
              }
              
              return $cost;
          }
          
          private function getModCost($mod_id)
          {
              $mod = ProductVariant::find()
                                   ->where([ 'product_variant_id' => $mod_id ])
                                   ->one();
              
              return $mod->price;
          }
          
          public function clearBasket()
          {
              $session = new Session;
              $session->open();
              $session[ 'basket' ] = NULL;
          }
          
          public function getUser()
          {
              return $this->hasOne(User::className(), [ 'id' => 'user_id' ]);
          }
          
          public function getProducts()
          {
              return $this->hasMany(OrdersProducts::className(), [ 'order_id' => 'id' ]);
          }
      }