Exemplo n.º 1
0
 protected 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->gl_memo = $hdr['memo'];
     $gl->description = $hdr['description'];
     $gl->id_branch = $hdr['id_branch'];
     $active_periode = Helper::getCurrentIdAccPeriode();
     $gl->id_periode = $active_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()));
         }
     }
 }
 /**
  * 
  * @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];
 }
Exemplo n.º 3
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getGlDetails()
 {
     return $this->hasMany(GlDetail::className(), ['id_coa' => 'id_coa']);
 }