/** * Creates a new CarPart model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new CarPart(); $modelPrice = new Price(); $modelPrice->tax = 12; if (isset($_GET['size_id'], $_GET['damage_id'], $_GET['color_id'])) { $model->size_id = $_GET['size_id']; $model->color_id = $_GET['color_id']; $model->damage_id = $_GET['damage_id']; } if ($model->load(Yii::$app->request->post()) && $model->validate() && $modelPrice->load(Yii::$app->request->post()) && $modelPrice->validate()) { $transaction = $model->getDb()->beginTransaction(); try { if (!$modelPrice->save(false)) { throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($modelPrice->tableName())), 'msj' => print_r($modelPrice->getErrors(), true)]), 500); } $model->price_id = $modelPrice->id; if (!$model->save(false)) { throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($model->tableName())), 'msj' => print_r($model->getErrors(), true)]), 500); } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); throw $e; } return $this->redirect(['index']); } else { $sizes = ArrayHelper::map(Size::find()->asArray()->all(), 'id', 'name'); $colors = ArrayHelper::map(Color::find()->asArray()->all(), 'id', 'name'); $damages = ArrayHelper::map(Damage::find()->asArray()->all(), 'id', 'name'); return $this->render('create', ['model' => $model, 'modelPrice' => $modelPrice, 'sizes' => $sizes, 'colors' => $colors, 'damages' => $damages]); } }
/** * Creates a new Item model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Item(); $modelPrice = new Price(); $modelPrice->tax = 12; if ($model->load(Yii::$app->request->post()) && $model->validate() && $modelPrice->load(Yii::$app->request->post()) && $modelPrice->validate()) { $transaction = $model->getDb()->beginTransaction(); try { if (!$model->save(false)) { throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($model->tableName())), 'msj' => print_r($model->getErrors(), true)]), 500); } if (!$modelPrice->save(false)) { throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($modelPrice->tableName())), 'msj' => print_r($modelPrice->getErrors(), true)]), 500); } $modelItemPrice = new ItemPrice(); $modelItemPrice->item_id = $model->id; $modelItemPrice->price_id = $modelPrice->id; if (!$modelItemPrice->save(false)) { throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($modelItemPrice->tableName())), 'msj' => print_r($modelItemPrice->getErrors(), true)]), 500); } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); throw $e; } return $this->redirect(['index']); } else { return $this->render('create', ['model' => $model, 'modelPrice' => $modelPrice]); } }
/** * Store a newly created resource in storage. * * @return Response */ public function store(Stantion $depo, Product $product, StoreStation $request) { $products = $product->all(); DB::transaction(function () use($depo, $request, $products) { $lastInsertedDepo = $depo->create($request->all()); $pricesIdArr = []; foreach ($products as $prod) { $price = new Price(); $price->price = 0; $price->save(); $prod->price()->attach($price->id); $pricesIdArr[] = $price->id; } $lastInsertedDepo->price()->sync($pricesIdArr); }); return redirect('stations')->with('alert-warning', 'Депо успешно добавлено! Внимание! Не забудьте проставить цену товарам для этого депо'); }
/** * @param \SimpleXMLElement $row * * @return bool */ private function update(\SimpleXMLElement $row) { $return = true; $type = AbstractCall::getXmlAttr($row); // buy /** @var Price $price */ $price = Price::findOne(['typeID' => $type['id'], 'type' => Price::TYPE_BUY]); if (!$price) { $price = new Price(); $price->typeID = $type['id']; $price->type = Price::TYPE_BUY; } $price->volume = (string) $row->buy->volume; $price->average = (string) $row->buy->avg; $price->min = (string) $row->buy->min; $price->max = (string) $row->buy->max; $price->stdDev = (string) $row->buy->stddev; $price->median = (string) $row->buy->median; $price->percentile = (string) $row->buy->percentile; $return = $price->save() && $return; // sell /** @var Price $price */ $price = Price::findOne(['typeID' => $type['id'], 'type' => Price::TYPE_SELL]); if (!$price) { $price = new Price(); $price->typeID = $type['id']; $price->type = Price::TYPE_SELL; } $price->volume = (string) $row->sell->volume; $price->average = (string) $row->sell->avg; $price->min = (string) $row->sell->min; $price->max = (string) $row->sell->max; $price->stdDev = (string) $row->sell->stddev; $price->median = (string) $row->sell->median; $price->percentile = (string) $row->sell->percentile; $return = $price->save() && $return; return $return; }
/** * Store a newly created resource in storage. * * @return Response */ public function store(Product $product, Request $request) { //$id = $request->route('products'); //dd($id); $validationRules = ['name' => 'required|alpha_spaces_numbers|max:35|unique:products', 'article' => 'required|alpha_spaces_numbers|max:15', 'description' => 'required|alpha_spaces_numbers', 'condition_id' => 'required|integer']; $depos = Stantion::all(); foreach ($depos as $depo) { $validationRules['price' . $depo->id] = 'required|numeric'; $validationRules['amount' . $depo->id] = 'required|numeric'; $validationRules['vat' . $depo->id] = 'required|numeric'; } $v = Validator::make($request->all(), $validationRules); if ($v->fails()) { return redirect()->back()->withErrors($v->errors())->withInput(); } DB::transaction(function () use($product, $request, $depos) { $product->article = $request->article; $product->name = $request->name; $product->description = $request->description; $product->condition_id = $request->condition_id; if (!(int) $request->category_id) { $product->category_id = null; } else { $product->category_id = $request->category_id; } $product->save(); $priceIdArr = []; foreach ($depos as $depo) { $price = new Price(); $priceInputName = 'price' . $depo->id; $priceInputAmount = 'amount' . $depo->id; $priceInputVAT = 'vat' . $depo->id; $price->price = $request->{$priceInputName}; $price->amount = $request->{$priceInputAmount}; $price->nds = $request->{$priceInputVAT}; $price->save(); $depo->price()->attach($price->id); $priceIdArr[] = $price->id; } $product->price()->sync($priceIdArr); }); return redirect('products')->with('alert-success', 'Товар успешно добавлен!'); }
/** * Creates a new Bill model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Bill(); if (Yii::$app->session['carPartTotal'] <= 0.0) { return; } if (isset($_POST['mode'], $_POST['discount'])) { $result = []; if ($_POST['mode'] == 0 || $_POST['mode'] == 1) { $transaction = $model->getDb()->beginTransaction(); try { $discount = (double) $_POST['discount']; if ($discount > Yii::$app->session['carPartTotal']) { $discount = 0.0; } $modelPrice = new Price(); $modelPrice->price = (double) Yii::$app->session['carPartTotal'] - (double) $discount; $modelPrice->calculate(); if (!$modelPrice->save(false)) { throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($modelPrice->tableName())), 'msj' => print_r($modelPrice->getErrors(), true)]), 500); } $model->price_id = $modelPrice->id; $model->discount = $discount; if (isset($_POST['draft']) && !empty($_POST['draft'])) { $model->draft = 1; } if (isset($_POST['Vehicle'])) { $vehicle = new Vehicle(); $vehicle->attributes = $_POST['Vehicle']; if (!empty($vehicle->brand) || !empty($vehicle->model) || !empty($vehicle->color) || !empty($vehicle->plaque)) { if (!$vehicle->save(false)) { throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($vehicle->tableName())), 'msj' => print_r($vehicle->getErrors(), true)]), 500); } $model->vehicle_id = $vehicle->id; } } if (isset($_POST['Customer'])) { $customer = new Customer(); $customer->attributes = $_POST['Customer']; if (!empty($customer->name) || !empty($customer->serial)) { if (!$customer->save(false)) { throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($customer->tableName())), 'msj' => print_r($customer->getErrors(), true)]), 500); } $model->customer_id = $customer->id; } } if ($model->save()) { $result['message'] = Yii::t('app', '{modelClass} saved', ['modelClass' => Yii::t('app', ucfirst($model->tableName()))]); } else { $result['message'] = Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($model->tableName())), 'msj' => print_r($model->errors, true)]); } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); $result['message'] = Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($model->tableName())), 'msj' => $e]); } } echo json_encode($result); } else { if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } } }