Blame view

common/models/Orders.php 5.5 KB
3f2bc3d0   Administrator   first commit
1
  <?php
3f2bc3d0   Administrator   first commit
2
3
4
  namespace common\models;
  
  use Yii;
90a6ed1a   Administrator   basket
5
6
7
  use yii\web\Session;
  use common\modules\product\models\ProductVariant;
  
3f2bc3d0   Administrator   first commit
8
9
10
  class Orders extends \yii\db\ActiveRecord
  {
  
3f2bc3d0   Administrator   first commit
11
12
13
14
15
      public static function tableName()
      {
          return 'orders';
      }
  
3f2bc3d0   Administrator   first commit
16
17
18
      public function rules()
      {
          return [
90a6ed1a   Administrator   basket
19
20
              [['name', 'phone'], 'required','whenClient' => true],
              //['email', 'email'],
84b2aead   Administrator   add active menu
21
              [['total','body','email','phone2','delivery','payment'], 'safe'],
90a6ed1a   Administrator   basket
22
              [['email'],'email'],
3f2bc3d0   Administrator   first commit
23
24
25
          ];
      }
  
90a6ed1a   Administrator   basket
26
      public function attributeLabels()
3f2bc3d0   Administrator   first commit
27
28
      {
          return [
84b2aead   Administrator   add active menu
29
              'name' => 'Ф.И.О',
90a6ed1a   Administrator   basket
30
31
32
33
34
              'phone'=>'Телефон',
              'phone2'=>'Дополнительный телефон',
              'body'=>'Сообщение',
              'adress'=>'Адрес',
              'city'=>'Город',
90a6ed1a   Administrator   basket
35
36
37
38
39
40
              'email'=>'E-mail',
              'date_time'=>'Дата',
              'total'=>'Сума',
              'status'=>'Статус',
              'delivery'=>'Вариант доставки',
              'payment'=>'Способы оплаты',
3f2bc3d0   Administrator   first commit
41
42
43
          ];
      }
  
90a6ed1a   Administrator   basket
44
45
46
47
48
      public function beforeSave($insert) {
          $this->user_id = Yii::$app->user->id;
          $this->date_time = new \yii\db\Expression('NOW()');
          return parent::beforeSave($insert);
      }
3f2bc3d0   Administrator   first commit
49
  
90a6ed1a   Administrator   basket
50
51
52
53
54
55
56
57
58
59
60
      public function beforeDelete() {
          return parent::beforeDelete();
      }
  
      public function contact($email,$body)
      {
          if ($this->validate()) {
              $body .= 'Вся сумма: '.$this->total;
              $body .= "\n\r";
              $body .= 'Имя: '.$this->name;
              $body .= "\n\r";
90a6ed1a   Administrator   basket
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
              $body .= 'E-mail: '.$this->email;
              $body .= "\n\r";
              $body .= 'Телефон: '.$this->phone;
              $body .= "\n\r";
              $body .= 'Адрес: '.$this->adress;
              $body .= "\n\r";
              $body .= 'Сообщение: '.$this->body;
              $body .= "\n\r";
  
              Yii::$app->mailer->compose()
                  ->setTo($email)
                  ->setFrom(['send@artweb.ua' => 'send'])
                  ->setSubject('Заказ на сайте Рюкзаки')
                  ->setTextBody($body)
                  ->send();
              return true;
3f2bc3d0   Administrator   first commit
77
          } else {
90a6ed1a   Administrator   basket
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
              return false;
          }
      }
      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++;
                  }
              }
          }
          if ($i == 0)
          {
              $data[] = ['id' => $mod_id, 'count' => $count];
              $session['basket'] = $data;
3f2bc3d0   Administrator   first commit
103
104
105
106
          }
      }
  
  
90a6ed1a   Administrator   basket
107
108
109
110
111
112
113
114
115
116
117
118
      public function rowBasket ()
      {
          $session = new Session;
          $session->open ();
          $cost = 0;
          $count = 0;
          if (isset($session['basket']) && count ($session['basket']))
          {
              foreach ($session['basket'] as $product)
              {
                  $count += $product['count'];
              }
3f2bc3d0   Administrator   first commit
119
          }
90a6ed1a   Administrator   basket
120
121
  
          return (object)['cost' => $cost, 'count' => $count];
3f2bc3d0   Administrator   first commit
122
123
      }
  
90a6ed1a   Administrator   basket
124
125
  
      public function deleteBasketMod ($id)
3f2bc3d0   Administrator   first commit
126
      {
90a6ed1a   Administrator   basket
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
          $session = new Session;
          $session->open ();
          $basket = $session['basket'];
          foreach ($basket as $key => $product)
          {
              if ($id == $product['id']) unset($basket[$key]);
          }
          $session['basket'] = $basket;
      }
  
      public function updateBasket ($row)
      {
          $session = new Session;
          $session->open ();
          //$data = array();
          if ($row['count'] > 0) $this->data[] = ['id' => $row['id'], 'count' => $row['count']];
          $session['basket'] = $this->data;
      }
  
  
      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;
      }
8df8067a   Administrator   basket
187
188
189
190
191
192
      public function clearBasket ()
      {
          $session = new Session;
          $session->open ();
          $session['basket'] = null;
      }
90a6ed1a   Administrator   basket
193
194
195
      public function getUser()
      {
          return $this->hasOne(User::className(), ['id' => 'user_id']);
3f2bc3d0   Administrator   first commit
196
197
      }
  
90a6ed1a   Administrator   basket
198
      public function getProducts()
3f2bc3d0   Administrator   first commit
199
      {
90a6ed1a   Administrator   basket
200
          return $this->hasMany(OrdersProducts::className(), ['order_id' => 'id']);
3f2bc3d0   Administrator   first commit
201
      }
90a6ed1a   Administrator   basket
202
  }