Blame view

models/OrderProduct.php 5.7 KB
11ecfb79   Yarik   Basket
1
2
3
4
5
6
  <?php
      
      namespace artbox\order\models;
      
      use artbox\catalog\models\Variant;
      use Yii;
e5e827a8   Yarik   Clean up
7
      use yii\db\ActiveRecord;
fd40a9e1   Yarik   Products to order
8
      use yii\helpers\ArrayHelper;
11ecfb79   Yarik   Basket
9
10
11
      
      /**
       * This is the model class for table "order_product".
11ecfb79   Yarik   Basket
12
       *
306e64da   Yarik   Fixes
13
       * @property integer $id
11ecfb79   Yarik   Basket
14
15
16
       * @property integer $order_id
       * @property integer $variant_id
       * @property string  $sku
306e64da   Yarik   Fixes
17
       * @property float   $price
11ecfb79   Yarik   Basket
18
19
20
21
       * @property integer $count
       * @property Order   $order
       * @property Variant $variant
       */
e5e827a8   Yarik   Clean up
22
      class OrderProduct extends ActiveRecord
11ecfb79   Yarik   Basket
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
      {
          /**
           * @inheritdoc
           */
          public static function tableName()
          {
              return 'order_product';
          }
          
          /**
           * @inheritdoc
           */
          public function rules()
          {
              return [
                  [
                      [
                          'order_id',
                          'sku',
2a050410   Yarik   Order
42
                          'variant_id',
11ecfb79   Yarik   Basket
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
                      ],
                      'required',
                  ],
                  [
                      [
                          'order_id',
                          'variant_id',
                          'count',
                      ],
                      'integer',
                  ],
                  [
                      [ 'price' ],
                      'number',
                  ],
                  [
                      [ 'sku' ],
                      'string',
                      'max' => 255,
                  ],
                  [
                      [ 'order_id' ],
                      'exist',
                      'skipOnError'     => true,
                      'targetClass'     => Order::className(),
                      'targetAttribute' => [ 'order_id' => 'id' ],
                  ],
                  [
                      [ 'variant_id' ],
                      'exist',
                      'skipOnError'     => true,
                      'targetClass'     => Variant::className(),
                      'targetAttribute' => [ 'variant_id' => 'id' ],
                  ],
              ];
          }
          
          /**
           * @inheritdoc
           */
          public function attributeLabels()
          {
              return [
                  'id'         => Yii::t('order', 'ID'),
                  'order_id'   => Yii::t('order', 'Order ID'),
                  'variant_id' => Yii::t('order', 'Variant ID'),
                  'sku'        => Yii::t('order', 'Sku'),
                  'price'      => Yii::t('order', 'Price'),
                  'count'      => Yii::t('order', 'Count'),
              ];
          }
          
          /**
           * @return \yii\db\ActiveQuery
           */
          public function getOrder()
          {
              return $this->hasOne(Order::className(), [ 'id' => 'order_id' ]);
          }
          
          /**
           * @return \yii\db\ActiveQuery
           */
          public function getVariant()
          {
              return $this->hasOne(Variant::className(), [ 'id' => 'variant_id' ]);
          }
fd40a9e1   Yarik   Products to order
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
      
          /**
           * @param array $items
           * @param int   $orderId
           *
           * @return \artbox\order\models\OrderProduct[]
           */
          public static function saveItems(array $items, int $orderId): array
          {
              $variantIds = ArrayHelper::getColumn($items, 'variant_id', false);
              /**
               * @var \artbox\order\models\OrderProduct[] $deletion
               */
              $deletion = self::find()
                              ->where(
                                  [
                                      'not',
                                      [ 'variant_id' => $variantIds ],
                                  ]
                              )
                              ->andWhere([ 'order_id' => $orderId ])
                              ->all();
              foreach ($deletion as $record) {
                  $record->delete();
              }
              /**
               * @var \artbox\order\models\OrderProduct[] $orderProducts
               */
              $orderProducts = self::find()
                                   ->where(
                                       [
                                           'variant_id' => $variantIds,
                                           'order_id'   => $orderId,
                                       ]
                                   )
                                   ->all();
8fa44a60   Anastasia   bug fix artbox-order
146
             // print_r($orderProducts); die();
fd40a9e1   Yarik   Products to order
147
148
149
150
151
152
153
              $newItems = [];
              foreach ($items as $item) {
                  $id = $item[ 'variant_id' ];
                  $count = $item[ 'count' ];
                  foreach ($orderProducts as $orderProduct) {
                      if ($orderProduct->variant_id == $id) {
                          $orderProduct->count = $count;
8fa44a60   Anastasia   bug fix artbox-order
154
                          continue 2;
fd40a9e1   Yarik   Products to order
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
                      }
                  }
                  /**
                   * @var Variant $variant
                   */
                  $variant = Variant::find()
                                    ->where([ 'id' => $id ])
                                    ->one();
                  if ($variant) {
                      $newItems[] = new OrderProduct(
                          [
                              'order_id'   => $orderId,
                              'variant_id' => $id,
                              'sku'        => $variant->sku,
                              'price'      => $variant->price,
                              'count'      => $count,
                          ]
                      );
                  }
              }
              $orderProducts = array_merge($orderProducts, $newItems);
8fa44a60   Anastasia   bug fix artbox-order
176
             // print_r($orderProducts); die();
fd40a9e1   Yarik   Products to order
177
              foreach ($orderProducts as $orderProduct) {
8fa44a60   Anastasia   bug fix artbox-order
178
                 var_dump( $orderProduct->save());
fd40a9e1   Yarik   Products to order
179
              }
8fa44a60   Anastasia   bug fix artbox-order
180
             // die();
fd40a9e1   Yarik   Products to order
181
182
              return $orderProducts;
          }
11ecfb79   Yarik   Basket
183
      }