$this->registerJs($this->render('_script-by-template.js'));
$detailFunc = function ($model, $key) {
    $result = Html::activeHiddenInput($model, "[{$key}]id", ['data-field' => 'id']);
    $result .= Html::tag('td', Html::tag('span', Html::getAttributeValue($model, "[{$key}]es[name]"), ['data-field' => 'name']));
    $result .= Html::tag('td', Html::activeTextInput($model, "[{$key}]amount", ['data-field' => 'amount', 'class' => 'form-control']));
    $result .= Html::tag('td', Html::tag('a', '<i class="fa fa-minus"></i>'), ['data-action' => 'delete']);
    return $result;
};
?>

<div class="col-lg-12 no-padding no-margin">
    <table class="table table-hover" id="table-templates">
        <thead>
            <tr>
                <th>Template</th>
                <th>Amount</th>
                <th style="width: 5%">#</th>
            </tr>
            <tr>
                <th><input type="hidden" id="inp-template-id">
                    <input class="form-control" id="inp-template"></th>
                <th><input class="form-control" id="inp-amount"></th>
                <th ><a class="btn btn-default text-green" id="add-template"><i class="fa fa-plus"></i></a></th>
            </tr>
        </thead>
        <?php 
echo TabularInput::widget(['id' => 'template-grid', 'allModels' => $templates, 'model' => GlTemplate::className(), 'tag' => 'tbody', 'itemOptions' => ['tag' => 'tr'], 'itemView' => $detailFunc, 'clientOptions' => []]);
?>
    </table>
</div>
 /**
  * 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]);
 }