/** * Lists all Bill models. * @return mixed */ public function actionIndex() { $searchModel = new BillSearch(); if (isset($_POST['bill_mode'])) { $searchModel->filter = $_POST['bill_mode']; } $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]); }
/** * Displays a single Person model. * @param string $id * @return mixed */ public function actionView($id) { if (isset($_POST['hasEditable']) && $_POST['hasEditable'] == 1) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $bill = $_POST['bill']; $person = $_POST['person']; $billPerson = BillPersonal::find()->where(['bill_id' => $bill, 'personal_id' => $person])->one(); if ($billPerson == null) { $billPerson = new BillPersonal(); $billPerson->bill_id = $bill; $billPerson->personal_id = $person; $billPersonAmount = isset($_POST['bp_amount']) ? $_POST['bp_amount'] : 0.0; $billPersonDescription = isset($_POST['bp_description']) ? $_POST['bp_description'] : ""; } else { $billPersonAmount = isset($_POST['bp_amount']) ? $_POST['bp_amount'] : $billPerson->amount; $billPersonDescription = isset($_POST['bp_description']) ? $_POST['bp_description'] : $billPerson->description; } $billPerson->description = $billPersonDescription; $billPerson->amount = $billPersonAmount; if (!$billPerson->save()) { return ['output' => '', 'message' => print_r($billPerson->errors, true)]; } else { if (isset($_POST['split-bill']) && $_POST['split-bill'] == 1) { $n_quotes = 2; for ($i = 0; $i < $n_quotes; $i++) { $quote = new BillPersonalQuotes(); $quote->amount = (double) $billPerson->amount / (double) $n_quotes; $quote->bill_person_id = $billPerson->id; if (!$quote->save()) { return ['output' => '', 'message' => print_r($quote->errors, true)]; } } } if (isset($_POST['bp_amount'])) { return ['output' => $billPersonAmount, 'message' => '']; } elseif (isset($_POST['bp_description'])) { return ['output' => $billPersonDescription, 'message' => '']; } } return ['output' => '', 'message' => 'Validation error']; } $total = 0.0; if (isset($_POST['selected'], $_GET['id'])) { $bill_ids = $_POST['selected']; $billPersonals = BillPersonal::find()->where(['bill_id' => $bill_ids, 'personal_id' => $_GET['id']])->all(); if (isset($billPersonals)) { $saveMode = isset($_POST['mode']) && $_POST['mode'] == 1; if ($saveMode) { $transaction = BillPersonal::getDb()->beginTransaction(); } try { foreach ($billPersonals as $billPersonal) { $total += $billPersonal->amount; if ($saveMode) { if ($billPersonal->paid == 0) { $billPersonal->paid = 1; } else { $billPersonal->paid = 1; } if (!$billPersonal->save()) { throw new Exception(print_r($billPersonal->errors, true)); } } } if ($saveMode) { $transaction->commit(); } } catch (\Exception $e) { if ($saveMode) { $transaction->rollBack(); } } } } $billSearchModel = new BillSearch(); $billDataProvider = $billSearchModel->searchWithPerson(Yii::$app->request->queryParams); return $this->render('view', ['billSearchModel' => $billSearchModel, 'billDataProvider' => $billDataProvider, 'model' => $this->findModel($id), 'total' => $total]); }
/** * 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]); }