Blame view

frontend/models/Order.php 5.43 KB
4bf9edb7   Yarik   Order
1
2
3
  <?php
      
      namespace frontend\models;
6c8e3423   Yarik   Fast buy
4
5
  
      use artbox\catalog\models\Variant;
4bf9edb7   Yarik   Order
6
7
      use artbox\order\models\Delivery;
      use artbox\order\models\Payment;
543f1653   Yarik   Fixed
8
9
      use yii\behaviors\BlameableBehavior;
      
4bf9edb7   Yarik   Order
10
11
      class Order extends \artbox\order\models\Order
      {
6c8e3423   Yarik   Fast buy
12
13
          public $variantId;
      
4bf9edb7   Yarik   Order
14
15
16
17
          const SCENARIO_INFO = 'info';
          const SCENARIO_DELIVERY = 'delivery';
          const SCENARIO_PAYMENT = 'payment';
          const SCENARIO_CONFIRM = 'confirm';
6c8e3423   Yarik   Fast buy
18
19
          const SCENARIO_FAST = 'fast';
      
4bf9edb7   Yarik   Order
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
          /**
           * @inheritdoc
           */
          public function scenarios()
          {
              return array_merge(
                  parent::scenarios(),
                  [
                      self::SCENARIO_INFO     => [
                          'name',
                          'phone',
                          'email',
                          'city',
                          'address',
                          'comment',
                      ],
                      self::SCENARIO_DELIVERY => [
                          'delivery_id',
                      ],
                      self::SCENARIO_PAYMENT  => [
                          'payment_id',
                      ],
6c8e3423   Yarik   Fast buy
42
43
44
45
46
                      self::SCENARIO_FAST     => [
                          'name',
                          'phone',
                          'variantId',
                      ],
4bf9edb7   Yarik   Order
47
48
49
                  ]
              );
          }
6c8e3423   Yarik   Fast buy
50
      
543f1653   Yarik   Fixed
51
52
          public function behaviors()
          {
fa8dafba   Alexey Boroda   -User's password ...
53
54
55
56
57
              $behaviors = parent::behaviors();
              $behaviors[] = [
                  'class'              => BlameableBehavior::className(),
                  'createdByAttribute' => 'user_id',
                  'updatedByAttribute' => false,
543f1653   Yarik   Fixed
58
              ];
fa8dafba   Alexey Boroda   -User's password ...
59
              return $behaviors;
543f1653   Yarik   Fixed
60
61
          }
          
4bf9edb7   Yarik   Order
62
63
64
65
66
67
68
69
70
71
72
73
74
          /**
           * @inheritdoc
           */
          public function rules()
          {
              return [
                  [
                      [
                          'name',
                          'phone',
                          'email',
                          'delivery_id',
                          'payment_id',
4bf9edb7   Yarik   Order
75
76
77
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
                      ],
                      'required',
                  ],
                  [
                      [
                          'name',
                          'phone',
                          'email',
                          'city',
                          'address',
                      ],
                      'string',
                      'max' => 255,
                  ],
                  [
                      [
                          'comment',
                      ],
                      'string',
                  ],
                  [
                      [
                          'delivery_id',
                          'payment_id',
                      ],
                      'integer',
                  ],
                  [
                      [
                          'delivery_id',
                      ],
                      'exist',
                      'targetClass'     => Delivery::className(),
                      'targetAttribute' => 'id',
                  ],
                  [
                      [
                          'payment_id',
                      ],
                      'exist',
                      'targetClass'     => Payment::className(),
                      'targetAttribute' => 'id',
                  ],
6c8e3423   Yarik   Fast buy
118
119
                  [
                      [
e67c95f9   Yarik   Fast buy
120
                          'variantId',
6c8e3423   Yarik   Fast buy
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
                      ],
                      'exist',
                      'targetClass'     => Variant::className(),
                      'targetAttribute' => 'id',
                      'filter'          => function ($query) {
                          /**
                           * @var \yii\db\Query $query
                           */
                          $query->andWhere(
                              [
                                  '>',
                                  'price',
                                  0,
                              ]
                          )
                                ->andWhere([ 'status' => true ])
                                ->andWhere(
                                    [
                                        '>',
                                        'stock',
                                        0,
                                    ]
                                );
                      },
e67c95f9   Yarik   Fast buy
145
146
147
148
149
150
151
152
                      'on'              => self::SCENARIO_FAST,
                  ],
                  [
                      [
                          'variantId',
                      ],
                      'required',
                      'on' => self::SCENARIO_FAST,
6c8e3423   Yarik   Fast buy
153
                  ],
4bf9edb7   Yarik   Order
154
155
              ];
          }
6c8e3423   Yarik   Fast buy
156
      
4bf9edb7   Yarik   Order
157
158
159
160
161
162
163
164
165
166
167
          /**
           * Save active attributes to session variable 'order'
           */
          public function saveActive()
          {
              $order = \Yii::$app->session->get('order', []);
              foreach ($this->activeAttributes() as $attribute) {
                  $order[ $attribute ] = $this->getAttribute($attribute);
              }
              \Yii::$app->session[ 'order' ] = $order;
          }
6c8e3423   Yarik   Fast buy
168
      
4bf9edb7   Yarik   Order
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
          /**
           * Load active attributes from session variable 'order'
           */
          public function loadActive()
          {
              $order = \Yii::$app->session->get('order');
              if (!empty($order)) {
                  foreach ($this->activeAttributes() as $attribute) {
                      if (!empty($order[ $attribute ])) {
                          $this->setAttribute($attribute, $order[ $attribute ]);
                      }
                  }
              }
          }
      }