/**
  * Creates a new GlHeader model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate($es = null)
 {
     $sheets = EntriSheet::find()->asArray()->all();
     $sheets = \yii\helpers\ArrayHelper::map($sheets, 'id_esheet', 'nm_esheet');
     $model = new GlHeader();
     $details = [];
     if (!empty($es)) {
         foreach (EntriSheetDtl::findAll(['id_esheet' => $es]) as $eDtl) {
             /* @var $eDtl EntriSheetDtl */
             $glDtl = new GlDetail(['id_coa' => $eDtl->id_coa]);
             $details[$eDtl->nm_esheet_dtl] = $glDtl;
         }
         $post = Yii::$app->request->post();
         if ($model->load($post) && Model::loadMultiple($details, $post)) {
             $transaction = Yii::$app->db->beginTransaction();
             try {
                 $amount = 0.0;
                 $model->status = 1;
                 if ($model->save()) {
                     $id_hdr = $model->id_gl;
                     foreach ($details as $detail) {
                         $amount += $detail->amount;
                         $detail->id_gl = $id_hdr;
                         if (!$detail->save()) {
                             throw new \Exception(implode("\n", $detail->firstErrors));
                         }
                     }
                     if ($amount != 0.0) {
                         throw new \Exception('Not balance');
                     }
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id_gl]);
                 } else {
                     $transaction->rollBack();
                 }
             } catch (\Exception $exc) {
                 $transaction->rollBack();
                 $model->addError('', $exc->getMessage());
             }
         }
     }
     return $this->render('create', ['model' => $model, 'details' => $details, 'es' => $es, 'sheets' => $sheets]);
 }
Exemplo n.º 2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getEntriSheetDtls()
 {
     return $this->hasMany(EntriSheetDtl::className(), ['id_esheet' => 'id_esheet']);
 }
 /**
  * Finds the EntriSheetDtl model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id_esheet
  * @param integer $id_coa
  * @return EntriSheetDtl the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id_esheet, $id_coa)
 {
     if (($model = EntriSheetDtl::findOne(['id_esheet' => $id_esheet, 'id_coa' => $id_coa])) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * Deletes an existing EntriSheet model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  * @param integer $id
  * @return mixed
  */
 public function actionDelete($id)
 {
     $model = $this->findModel($id);
     try {
         $transaction = \Yii::$app->db->beginTransaction();
         EntriSheetDtl::deleteAll(['id_esheet' => $model->id_esheet]);
         $model->delete();
         $transaction->commit();
     } catch (\Exception $exc) {
         $transaction->rollBack();
         throw $exc;
     }
     return $this->redirect(['index']);
 }