/**
  * Creates a new GlHeader model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreateByTemplate()
 {
     $dPost = Yii::$app->request->post();
     $model = new GlHeader();
     $model->reff_type = '0';
     $model->status = $model::STATUS_RELEASED;
     $model->date = date('Y-m-d');
     $dPost = Yii::$app->request->post();
     $templates = [];
     if ($model->load($dPost)) {
         $transaction = Yii::$app->db->beginTransaction();
         try {
             //entri details from templates
             $templates = Helper::createMultiple(GlTemplate::className(), $dPost);
             $amounts = [];
             /* @var $template GlTemplate */
             foreach ($templates as $template) {
                 $amounts[$template->es->code] = $template->amount;
             }
             $model->addFromTemplate($amounts, true);
             if ($model->save()) {
                 $transaction->commit();
                 \Yii::$app->getSession()->setFlash('success', $model->number . ' succesfully created');
                 return $this->redirect(['view', 'id' => $model->id]);
             } else {
                 $errors = $model->firstErrors;
                 Yii::$app->getSession()->setFlash('error', reset($errors));
                 $transaction->rollback();
             }
         } catch (Exception $e) {
             $transaction->rollback();
             throw $e;
         }
     }
     return $this->render('create-by-template', ['model' => $model, 'templates' => $templates]);
 }
Пример #2
0
 /**
  * 
  * @param GlHeader $model
  */
 public function createUpdateJournal($model = null)
 {
     if ($model === null) {
         $model = new GlHeader(['status' => GlHeader::STATUS_RELEASED, 'reff_type' => GlHeader::REFF_SALES, 'reff_id' => $this->id, 'date' => date('Y-m-d'), 'vendor_id' => $this->vendor_id, 'periode_id' => GlHeader::getActivePeriode(), 'branch_id' => $this->branch_id]);
     }
     $value = 0;
     foreach ($this->items as $item) {
         $value += $item->cogs * $item->qty * $item->productUom->isi;
     }
     $model->addFromTemplate(['ES006' => $value]);
     return $model;
 }