/** * 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) { $model = new GlHeader(); if (!empty($es)) { $details = []; foreach (EntriSheetDtl::findAll(['id_esheet' => $es]) as $eDtl) { /* @var $eDtl EntriSheetDtl */ $details[$eDtl->nm_esheet_dtl] = new GlDetail(['id_coa' => $eDtl->id_coa]); } $model->populateRelation('glDetails', $details); $post = Yii::$app->request->post(); if ($model->load($post)) { try { $transaction = Yii::$app->db->beginTransaction(); $success = $model->save(); $success = $model->saveRelated('glDetails', $post, $success); if ($success) { $error = false; $balance = 0.0; foreach ($model->glDetails as $detail) { $balance += $detail->amount; } if ($balance != 0) { $model->addError('', 'Details should be balance'); $error = true; } // if ($error) { $transaction->rollBack(); } else { $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, 'es' => $es]); }
/** * Creates a new GlHeader model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new GlHeader(); $post = Yii::$app->request->post(); if ($model->load($post)) { try { $transaction = Yii::$app->db->beginTransaction(); $success = $model->save(); $success = $model->saveRelated('glDetails', $post, $success); if ($success) { $error = false; if (count($model->glDetails) == 0) { $model->addError('', 'Detail cannot be blank'); $error = true; } $balance = 0.0; foreach ($model->glDetails as $detail) { $balance += $detail->amount; } if ($balance != 0) { $model->addError('', 'Details should be balance'); $error = true; } // if ($error) { $transaction->rollBack(); } else { $transaction->commit(); return $this->redirect(['view', 'id' => $model->id_gl]); } } else { $transaction->rollBack(); } } catch (\Exception $exc) { $transaction->rollBack(); $model->addError('', $exc->getMessage()); } $model->setIsNewRecord(true); } return $this->render('create', ['model' => $model]); }
public static function createGL($hdr, $dtls = []) { $blc = 0.0; foreach ($dtls as $row) { $blc += $row['ammount']; } if ($blc != 0) { throw new UserException('GL Balance Failed'); } $gl = new GlHeader(); $gl->gl_date = $hdr['date']; $gl->id_reff = $hdr['id_reff']; $gl->type_reff = $hdr['type_reff']; $gl->description = $hdr['description']; $gl->id_branch = $hdr['id_branch']; /* * Edited By Mujib Masyhudi * on 2014-07-07 */ $active_periode = self::getCurrentIdAccPeriode(); $gl->id_periode = $active_periode['id_periode']; $gl->status = 0; if (!$gl->save()) { throw new UserException(implode("\n", $gl->getFirstErrors())); } foreach ($dtls as $row) { $glDtl = new GlDetail(); $glDtl->id_gl = $gl->id_gl; $glDtl->id_coa = $row['id_coa']; $glDtl->amount = $row['ammount']; if (!$glDtl->save()) { throw new UserException(implode("\n", $glDtl->getFirstErrors())); } } }