Пример #1
0
 public function saveProducts($products)
 {
     foreach ($products as $product) {
         if (!$product['name']) {
             continue;
         }
         $productModel = Product::findOrCreate(['shop_id' => $this->shop_id, 'name' => $product['name']]);
         $productModel->product_unit_id = is_int($product['unit']) ? $product['unit'] : 1;
         if (!$productModel->save()) {
             throw new Exception(\Yii::t('backend', 'Cannot save product model'));
         }
         ProductPrice::updateIfRequired($productModel->id, $product['price']);
         $receiptProduct = new ReceiptProduct();
         $receiptProduct->receipt_id = $this->id;
         $receiptProduct->product_id = $productModel->id;
         $receiptProduct->count = (double) str_replace(',', '.', $product['count']);
         $receiptProduct->total_price = (double) str_replace(',', '.', $product['totalPrice']);
         if (!$receiptProduct->save()) {
             throw new Exception(\Yii::t('backend', 'Cannot save {modelName} model', ['{modelName}' => get_class($receiptProduct)]));
         }
     }
     return true;
 }
Пример #2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getProductPrices()
 {
     return $this->hasMany(\common\models\ProductPrice::className(), ['receipt_id' => 'id']);
 }