示例#1
0
 /**
  * Creates a new Amount model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Amount();
     $model->scenario = 'create';
     if ($model->load(Yii::$app->request->post())) {
         // Load metadata and get path from uploaded file
         $upload = UploadedFile::getInstance($model, 'amount_img_temp');
         if (!empty($upload)) {
             $previous = \backend\models\Amount::find()->select(['max(amount_id) as amount_id'])->one();
             $model->amount_id = $previous->amount_id + 1;
             $model->amount_img = 'upload/amount/' . $model->amount_id . '.' . $upload->extension;
             $model->amount_img_temp = true;
         }
         // Look-up Unit
         $unit_data = \backend\models\Unit::find()->select(['unit_text'])->andWhere('unit_id = :unit_id', [':unit_id' => $model->unit_id])->one();
         // Force null
         $model->note = empty($model->note) ? null : $model->note;
         if ($upload->saveAs($model->amount_img) && $model->save()) {
             Yii::$app->getSession()->setFlash('amount_create_success', ['type' => Growl::TYPE_SUCCESS, 'title' => '<b>' . Yii::t('backend', 'Success') . '</b>', 'icon' => 'glyphicon glyphicon-ok-sign', 'body' => '<strong>' . $model->amount_text . ' ' . $unit_data->unit_text . '</strong> ' . Yii::t('backend', 'has been added.')]);
             return $this->redirect(['index']);
         } else {
             if ($upload->hasError) {
                 $error_msg = Yii::t('backend', 'Upload Falied.');
             } else {
                 $error_msg = Yii::t('backend', 'Failed to save data.');
             }
             Yii::$app->getSession()->setFlash('amount_create_failed', ['type' => \kartik\growl\Growl::TYPE_DANGER, 'title' => '<b>' . Yii::t('backend', 'Error') . '</b>', 'icon' => 'glyphicon glyphicon-remove-sign', 'body' => $error_msg]);
             return $this->render('create', ['model' => $model]);
         }
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
示例#2
0
 /**
  * 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']);
 }
示例#3
0
 /**
  * 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;
 }
示例#4
0
 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]);
 }