/**
  * Deletes an existing GlHeader model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  * @param integer $id
  * @return mixed
  */
 public function actionReverse($id)
 {
     $oldGl = $this->findModel($id);
     $trans = \Yii::$app->db->beginTransaction();
     try {
         $newGl = new GlHeader();
         $newGl->attributes = $oldGl->attributes;
         $newGl->date = date('Y-m-d');
         $newDtls = [];
         foreach ($oldGl->glDetails as $ddtl) {
             $ndtl = new \backend\models\accounting\GlDetail();
             $ndtl->attributes = $ddtl->attributes;
             $ndtl->header_id = null;
             $ndtl->amount = -1 * $ddtl->amount;
             $newDtls[] = $ndtl;
         }
         $newGl->status = $newGl::STATUS_CANCELED;
         $newGl->reff_type = $newGl::REFF_JOURNAL;
         $newGl->reff_id = $oldGl->id;
         $newGl->number = null;
         $newGl->description = 'Reverse of ' . $oldGl->number;
         $newGl->glDetails = $newDtls;
         $newGl->created_at = null;
         $newGl->created_by = null;
         $newGl->updated_at = null;
         $newGl->updated_by = null;
         if ($newGl->save()) {
             $oldGl->status = $oldGl::STATUS_CANCELED;
             //                $oldGl->reff_type = $oldGl::REFF_JOURNAL;
             //                $oldGl->reff_id = $newGl->id;
             $oldGl->description = $oldGl->description . ' canceled by ' . $newGl->number;
             if ($oldGl->save()) {
                 $trans->commit();
                 $this->redirect(['index']);
             }
         } else {
             print_r($newGl->getErrors());
             print_r($newGl->getRelatedErrors());
         }
     } catch (Exception $ex) {
         $trans->rollBack();
     }
     return;
     //$this->redirect(['view','id'=>$id]);
 }
Пример #2
0
 /**
  *
  * @return boolean
  */
 public function postGL($factor)
 {
     /*
      * Header Journal
      */
     $model_journal = new \backend\models\accounting\GlHeader();
     $model_journal->periode_id = $this->findActivePeriode();
     $model_journal->date = date('Y-m-d');
     $model_journal->status = \backend\models\accounting\GlHeader::STATUS_RELEASED;
     $model_journal->reff_type = \backend\models\accounting\GlHeader::REFF_GOODS_MOVEMENT;
     $model_journal->reff_id = $this->id;
     $model_journal->branch_id = isset(Yii::$app->profile->branch_id) ? Yii::$app->profile->branch_id : -1;
     $esheet = null;
     switch ($this->reff_type) {
         case self::REFF_TRANSFER:
             $esheet = $this->type == self::TYPE_ISSUE ? EntriSheet::find()->where('code=:dcode', [':dcode' => 'ES004'])->one() : EntriSheet::find()->where('code=:dcode', [':dcode' => 'ES005'])->one();
             break;
         default:
             $esheet = $factor == 1 ? EntriSheet::find()->where('code=:dcode', [':dcode' => 'ES001'])->one() : EntriSheet::find()->where('code=:dcode', [':dcode' => 'ES003'])->one();
     }
     $model_journal->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 = $this->totalValue;
     $newDtls[] = $ndtl;
     $ndtl1 = new \backend\models\accounting\GlDetail();
     $ndtl1->coa_id = $esheet->k_coa_id;
     $ndtl1->header_id = null;
     $ndtl1->amount = $this->totalValue * -1;
     $newDtls[] = $ndtl1;
     $model_journal->glDetails = $newDtls;
     if (!$model_journal->save()) {
         print_r($model_journal->getErrors());
         print_r($model_journal->getRelatedErrors());
         return false;
     }
     return true;
 }