/**
  * Deletes an existing GoodsMovement model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  * @param integer $id
  * @return mixed
  */
 public function actionPost($id)
 {
     $model = $this->findModel($id);
     $model->status = Invoice::STATUS_RELEASED;
     $transaction = Yii::$app->db->beginTransaction();
     try {
         if ($model->save()) {
             //post journal first
             $gl = new \backend\models\accounting\GlHeader();
             $gl->branch_id = 1;
             $aPeriode = \backend\models\accounting\AccPeriode::find()->active()->one();
             if ($aPeriode == null) {
                 throw new NotFoundHttpException('No active periode exist for now.');
             }
             $gl->periode_id = $aPeriode->id;
             $gl->reff_type = $gl::REFF_INVOICE;
             $gl->reff_id = $model->id;
             $gl->status = $gl::STATUS_RELEASED;
             $gl->date = date('Y-m-d');
             $esheet = \backend\models\accounting\EntriSheet::find()->where('code=:dcode', [':dcode' => 'ES002'])->one();
             $gl->description = $esheet->name;
             /*
              * Detail Journal
              */
             $newDtls = [];
             $ndtl = new \backend\models\accounting\GlDetail();
             $ndtl->coa_id = $esheet->d_coa_id;
             $ndtl->header_id = null;
             $ndtl->amount = $model->value;
             $newDtls[] = $ndtl;
             $ndtl1 = new \backend\models\accounting\GlDetail();
             $ndtl1->coa_id = $esheet->k_coa_id;
             $ndtl1->header_id = null;
             $ndtl1->amount = $model->value * -1;
             $newDtls[] = $ndtl1;
             $gl->glDetails = $newDtls;
             if ($gl->save()) {
                 $transaction->commit();
             } else {
                 foreach ($gl->getErrors() as $dkey => $vald) {
                     if ($vald[0] == 'Related error') {
                         foreach ($gl->getRelatedErrors() as $dkey => $valr) {
                             foreach ($valr as $tkey => $valt) {
                                 \Yii::$app->getSession()->setFlash('error', $valt);
                             }
                             break;
                         }
                     } else {
                         \Yii::$app->getSession()->setFlash('error', $vald[0]);
                         break;
                     }
                 }
                 $transaction->rollback();
             }
             return $this->redirect(['view', 'id' => $model->id]);
         } else {
             print_r($model->getErrors());
         }
     } catch (\Exception $exc) {
         $transaction->rollBack();
         echo $exc->getMessage();
     }
     return $this->render('view', ['model' => $model]);
 }