/** * Lists all BillItem models. * @return mixed */ public function actionIndex() { if (isset($_POST['hasEditable']) && $_POST['hasEditable'] == 1) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; if (isset($_POST['item'], $_POST['quantity_user'])) { if (!isset(Yii::$app->session['item'])) { Yii::$app->session['item'] = []; } $item = Yii::$app->session['item']; $item[$_POST['item']] = $_POST['quantity_user']; Yii::$app->session['item'] = $item; return ['output' => $_POST['quantity_user'], 'message' => '']; } else { return ['output' => '', 'message' => 'Validation error']; } } elseif (isset($_POST['iks'], $_POST['bks'], $_POST['assign_mode'])) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; //Create assignment if ($_POST['assign_mode'] == 1) { $items = Item::findAll($_POST['iks']); $bill = Bill::findOne($_POST['bks'][0]); $billItem = null; $transaction = Bill::getDb()->beginTransaction(); try { foreach ($items as $item) { $billItem = BillItem::find()->where(['item_id' => $item->id, 'bill_id' => $bill->id])->one(); if ($billItem == null) { $billItem = new BillItem(); $billItem->bill_id = $bill->id; $billItem->item_id = $item->id; } $itemSession = Yii::$app->session['item']; $oldQuantity = $billItem->quantity; $billItem->quantity = $itemSession[$item->id]; if ($oldQuantity > $billItem->quantity) { $item->quantity += $billItem->quantity; } elseif ($oldQuantity < $billItem->quantity) { $item->quantity -= $billItem->quantity; } $item->save(false); if (!$billItem->save(false)) { throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($billItem->tableName())), 'msj' => print_r($billItem->getErrors(), true)]), 500); } } $transaction->commit(); Yii::$app->session['item'] = []; return ['error' => false, 'message' => Yii::t('app', 'Saved')]; } catch (\Exception $e) { $transaction->rollBack(); return ['error' => true, 'message' => print_r($e, true)]; } } } $searchModel = new BillSearch(); $dataProvider = $searchModel->searchWithItem(Yii::$app->request->queryParams); $itemSearchModel = new ItemSearch(); $itemDataProvider = $itemSearchModel->searchWithItem(Yii::$app->request->queryParams); return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'itemSearchModel' => $itemSearchModel, 'itemDataProvider' => $itemDataProvider]); }
/** * 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]); } } }