/**
  * 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]);
 }
 /**
  * 
  * @param GlHeader $model
  * @return array
  * @throws Exception
  */
 protected function saveGl($model)
 {
     $post = Yii::$app->request->post();
     $details = $model->glDetails;
     $success = false;
     if ($model->load($post)) {
         $transaction = Yii::$app->db->beginTransaction();
         try {
             $formName = (new GlDetail())->formName();
             $postDetails = empty($post[$formName]) ? [] : $post[$formName];
             if ($postDetails === []) {
                 throw new Exception('Detail tidak boleh kosong');
             }
             $objs = [];
             foreach ($details as $detail) {
                 $objs[$detail->id_gl_detail] = $detail;
             }
             if ($model->save()) {
                 $success = true;
                 $id_hdr = $model->id_transfer;
                 $details = [];
                 $amount = 0.0;
                 foreach ($postDetails as $dataDetail) {
                     $id_dtl = $dataDetail['id_gl_detail'];
                     if (isset($objs[$id_dtl])) {
                         $detail = $objs[$id_dtl];
                         unset($objs[$id_dtl]);
                     } else {
                         $detail = new GlDetail();
                     }
                     $detail->setAttributes($dataDetail);
                     $detail->id_gl = $id_hdr;
                     if (!$detail->save()) {
                         $success = false;
                         $model->addError('', implode("\n", $detail->firstErrors));
                         break;
                     }
                     $details[] = $detail;
                     $amount += $detail->amount;
                 }
                 if ($amount != 0.0) {
                     throw new Exception("Not balance");
                 }
                 if ($success && count($objs)) {
                     $success = GlDetail::deleteAll(['id_gl' => $id_hdr, 'id_gl_detail' => array_keys($objs)]);
                 }
             }
             if ($success) {
                 $transaction->commit();
             } else {
                 $transaction->rollBack();
             }
         } catch (Exception $exc) {
             $model->addError('', $exc->getMessage());
             $transaction->rollBack();
             $success = false;
         }
         if (!$success) {
             $details = [];
             foreach ($postDetails as $value) {
                 $detail = new GlDetail();
                 $detail->setAttributes($value);
                 $details[] = $detail;
             }
         }
     }
     return [$details, $success];
 }