/** * Deletes an existing Unit model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $amount_data = \backend\models\Amount::find()->select(['unit_id'])->andWhere('unit_id = :id', [':id' => $id])->groupBy('unit_id')->one(); $model = $this->findModel($id); if (empty($amount_data->unit_id)) { $model->delete(); Yii::$app->getSession()->setFlash('unit_delete_success', ['type' => Growl::TYPE_SUCCESS, 'title' => '<b>' . Yii::t('backend', 'Success') . '</b>', 'icon' => 'glyphicon glyphicon-ok-sign', 'body' => '<strong>' . $model->unit_text . '</strong> ' . Yii::t('backend', 'has been deleted.')]); } else { Yii::$app->getSession()->setFlash('unit_delete_failed', ['type' => \kartik\growl\Growl::TYPE_DANGER, 'title' => '<b>' . Yii::t('backend', 'Error') . '</b>', 'icon' => 'glyphicon glyphicon-remove-sign', 'body' => '<strong>' . $model->unit_text . '</strong> ' . Yii::t('backend', 'is currently being used and cannot be deleted.')]); } return $this->redirect(['index']); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Amount::find(); $query->joinWith('unit'); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['unit_text' => SORT_ASC, 'amount_text' => SORT_ASC], 'enableMultiSort' => true]]); $dataProvider->sort->attributes['unit_text'] = ['asc' => ['unit_text' => SORT_ASC], 'desc' => ['unit_text' => SORT_DESC]]; $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['amount_id' => $this->amount_id, 'amount_text' => $this->amount_text, 'unit_id' => $this->unit_id]); $query->andFilterWhere(['like', 'amount_img', $this->amount_img])->andFilterWhere(['like', 'note', $this->note])->andFilterWhere(['like', 'unit_text', $this->unit_text]); return $dataProvider; }
/** * Finds the Amount model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Amount the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Amount::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
private function generator($scenario) { $model = new Main(); if (!empty(Yii::$app->request->post())) { $main_id = Yii::$app->request->post()['Main']['main_id']; if (empty($main_id)) { // New record $model->load(Yii::$app->request->post()); // Load time_array manually, since it's not part of Main Table $model->time_array = Yii::$app->request->post()['Main']['time_array']; // Add Owner $model->user_id = Yii::$app->user->identity->id; $model->created = date('Y-m-d H:i:s'); $model->updated = date('Y-m-d H:i:s'); } else { // Update exist record $model = $this->findModel($main_id); $model->load(Yii::$app->request->post()); // Load time_array manually, since it's not part of Main Table $model->time_array = Yii::$app->request->post()['Main']['time_array']; // Update Timestamp $model->updated = date('Y-m-d H:i:s'); } if (!empty($model->time_array)) { // Convert array to bit $model->time_bit = $model->encodeBit($model->time_array); } // Force null $model->drug_name = empty($model->drug_name) ? null : $model->drug_name; } if (isset(Yii::$app->request->post()['save']) && $model->validate()) { if ($model->save()) { Yii::$app->getSession()->setFlash('main_create_success', ['type' => Growl::TYPE_SUCCESS, 'title' => '<b>' . Yii::t('backend', 'Success') . '</b>', 'icon' => 'glyphicon glyphicon-ok-sign', 'body' => '<strong>' . $model->profile_name . '</strong> ' . Yii::t('backend', 'has been saved.')]); } $model = new Main(); } elseif (isset(Yii::$app->request->post()['generate'])) { $model->scenario = $scenario; if ($model->validate()) { if ($model->scenario == 'generate-original') { // Load patient_name manually, since it's not part of Main Table $model->patient_name = Yii::$app->request->post()['Main']['patient_name']; } /* Get amount_img */ $amount_data = \backend\models\Amount::find()->select(['amount_img'])->andWhere('amount_id = :amount_id', [':amount_id' => $model->amount_id])->one(); $model->amount_id = $amount_data->amount_img; /* Get time_img */ $time_data = \backend\models\Time::find()->select(['time_id', 'time_img'])->orderBy('time_id')->asArray()->all(); foreach ($time_data as $time_all_key => $time_all_value) { foreach ($model->time_array as $time_select_key => $time_select_value) { if ($time_all_value['time_id'] == $time_select_value) { $time_img[] = $time_all_value['time_img']; } } } $model->time_array = $time_img; /* Get meal_img */ $meal_data = \backend\models\Meal::find()->select(['meal_img'])->andWhere('meal_id = :meal_id', [':meal_id' => $model->meal_id])->one(); $model->meal_id = $meal_data->meal_img; /* Build PDF */ $pdf = new Pdf(['mode' => Pdf::MODE_UTF8, 'format' => [48, 80], 'orientation' => Pdf::ORIENT_LANDSCAPE, 'marginLeft' => 2, 'marginRight' => 2, 'marginTop' => 10, 'marginBottom' => 0, 'cssInline' => ' body{font-family:garuda; color: #000;} hr{margin-top: 4px; margin-bottom: 4px; color: #000;} .text-ctl{font-size: 10px;} .table-ctl{text-align: center; width:80px;} .table-vr{border-left: solid #000;} ', 'content' => $this->renderPartial('_label', ['model' => $model]), 'filename' => 'label_' . date('YmdHi') . '.pdf', 'destination' => Pdf::DEST_BROWSER]); return $pdf->render(); } } return $this->render('generator', ['model' => $model, 'scenario' => $scenario]); }
/** * @return \yii\db\ActiveQuery */ public function getAmounts() { return $this->hasMany(Amount::className(), ['unit_id' => 'unit_id']); }
/** * @return \yii\db\ActiveQuery */ public function getAmount() { return $this->hasOne(Amount::className(), ['amount_id' => 'amount_id']); }