OrderProduct.php 5.59 KB
<?php
    
    namespace artbox\order\models;
    
    use artbox\catalog\models\Variant;
    use Yii;
    use yii\helpers\ArrayHelper;
    
    /**
     * This is the model class for table "order_product".
     *
     * @property integer $id
     * @property integer $order_id
     * @property integer $variant_id
     * @property string  $sku
     * @property float   $price
     * @property integer $count
     * @property Order   $order
     * @property Variant $variant
     */
    class OrderProduct extends \yii\db\ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'order_product';
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'order_id',
                        'sku',
                        'variant_id',
                    ],
                    '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' ]);
        }
    
        /**
         * @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();
            $newItems = [];
            foreach ($items as $item) {
                $id = $item[ 'variant_id' ];
                $count = $item[ 'count' ];
                foreach ($orderProducts as $orderProduct) {
                    if ($orderProduct->variant_id == $id) {
                        $orderProduct->count = $count;
                        
                        continue 2;
                    }
                }
                /**
                 * @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);
            
            foreach ($orderProducts as $orderProduct) {
                $orderProduct->save();
            }
            return $orderProducts;
        }
    }