Exemplo n.º 1
0
 /**
  * Post an existing Payment model.
  * If posting is successful, the browser will be redirected to the 'index' page.
  * @param integer $id
  * @return mixed
  */
 public function actionPost($id)
 {
     $model = $this->findModel($id);
     if ($model->status != Payment::STATUS_DRAFT) {
         throw new UserException('Tidak bisa diposting');
     }
     $transc = \Yii::$app->db->beginTransaction();
     try {
         $model->status = Payment::STATUS_RELEASED;
         if ($model->save()) {
             /*
              * Create journal
              * PaymentMethod->coa_id (+)
              * Hutang Dagang (-) --> if invoice->type = Invoice::TYPE_INCOMING
              */
             $coa_payment = ['hutang dagang' => 12];
             //GL Header
             $gl = new \backend\models\accounting\GlHeader();
             $gl->periode_id = $this->findPeriode();
             $gl->date = date('Y-m-d');
             $gl->status = \backend\models\accounting\GlHeader::STATUS_RELEASED;
             $gl->reff_type = \backend\models\accounting\GlHeader::REFF_INVOICE;
             $gl->reff_id = $model->invoices[0]->id;
             $gl->description = 'Invoice Payment';
             $gl->branch_id = isset(Yii::$app->profile->branch_id) ? Yii::$app->profile->branch_id : -1;
             $glDtls = [];
             $toPayment = 0;
             foreach ($model->items as $itemRow) {
                 $toPayment += $itemRow->value;
             }
             //Kredit Penjualan
             $ndtl = new \backend\models\accounting\GlDetail();
             $ndtl->coa_id = $model->paymentMethod->coa_id;
             $ndtl->header_id = null;
             $ndtl->amount = $toPayment;
             $glDtls[] = $ndtl;
             $ndtl2 = new \backend\models\accounting\GlDetail();
             $ndtl2->coa_id = $coa_payment['hutang dagang'];
             $ndtl2->header_id = null;
             $ndtl2->amount = $toPayment * -1;
             $glDtls[] = $ndtl2;
             $gl->glDetails = $glDtls;
             if ($gl->save()) {
                 $transc->commit();
                 return $this->redirect(['view', 'id' => $model->id]);
             } else {
                 print_r($gl->getErrors());
             }
         }
     } catch (Exception $exc) {
         echo $exc->getTraceAsString();
     }
 }
Exemplo n.º 2
0
 /**
  * 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]);
 }