For a project I need to generate a tabular form for Invoice Taxes based on Purchase order details in the Invoice form. When I select the po_number field it will append the value on the url and refresh the pjax div named paticular. I've successfully generated the form and rows are getting generated for the tabular form part also and getting inserted in the Invoice Taxes table but only the invoice_id and invoice_amt are getting inserted.
Here's my form for Invoice
<div class="invoice-form">
<?php $form = ActiveForm::begin(['id' => 'dynamic-form','options' => ['enctype' => 'multipart/form-data']]);
<div class="col-md-12">
<div class="col-md-4">
<?= $form->field($model, 'po_number')->textInput(['readonly' => true,'maxlength' => true]) ?>
</div>
<div class="col-md-4">
<?= $form->field($model,'po_number')->dropDownList(
ArrayHelper::map(purchaseorder::find()->where(['type'=> 2])->all(),'id','po'),
['prompt'=>'Select PO ', 'onchange' => '
var poid;
poid=$(this).val();
//alert(poid);
var url = "index.php?r=invoice/create&pod="+poid;
$.pjax({url: url, container: "#particular"});
'
]);
?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'invoice_no')->textInput(['maxlength' => true]) ?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'net_amt')->textInput() ?>
</div>
</div>
<div class="col-md-12">
<div class="col-md-4">
<?php
if (isset(Yii::$app->request->queryParams['customer'])) {
$customer= Yii::$app->request->queryParams['customer'];
$customers = customer::find()->where(['id'=>$customer])->all();
$listdata =ArrayHelper::map($customers,'id','name');
?>
<?=
$form->field($model,'customerid')->widget(Select2::classname(),
[
'data' => $listdata,
//'data' => ArrayHelper::map(customer::find()->where(['id'=>$customer])->One(),'id','name'),
'language' => 'english',
'options' => ['placeholder' => 'Customer','id'=>'customer'],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
<?php
$serviceid= Yii::$app->request->queryParams['service'];
$servi = service::find()->where(['id'=>$serviceid])->all();
$listdata =ArrayHelper::map($servi,'id','name');
?>
<?php
}
else
{
$cust = new customer();
$customers = customer::find()->where(['id'=>$cust->id])->all();
$listdata =ArrayHelper::map($customers,'id','name');
?>
<?php
}
?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'po_desc')->textInput(['readonly' => true,'maxlength' => true]) ?>
<?= $form->field($model, 'status')->dropDownList(['Sent'=>'Sent','On hold'=>'On hold', 'Partially Recieved'=>'Partially Recieved','Fully Paid'=>'Fully Paid']); ?>
</div>
</div>
<div class='clearfix'></div>
<?php Pjax::begin(['id' => 'particular']) ?>
<div id=''>
<?php
// **********************************
// * Here the tabular form part start
// **********************************
if ( isset($_GET['pod']) ) {
$id = $_GET['pod'];
$modelPods = purchaseorderdetails::findAll(['purchaseorderid' => $id]);
/* $length = purchaseorderdetails::find()->where(['purchaseorderid' => $id])->count();
$x = 0;
foreach ( $modelPods as $pod ) {
$tempNet[$x] = $pod->net_amt;
$x++;
} */
foreach ( $modelPods as $pod ) {
//for ( $i = 0; $i < $length; $i++ ) {
echo '<div class="" style="border: 1px solid #ccc; border-radius: 2px; padding-top: 5px; margin-bottom:10px;">';
foreach ( $modelTaxes as $i => $data ) {
?>
<div class='col-md-2'>
<?= $form->field($data, '[$i]net_amt')->textInput(['value' => $pod->net_amt]) ?>
</div>
<div class='col-md-1'>
<?= $form->field($data, '[$i]ST')->textInput() ?>
</div>
<div class='col-md-2'>
<?= $form->field($data, '[$i]SBC')->textInput(['maxlength' => true]) ?>
</div>
<div class='col-md-2'>
<?= $form->field($data, '[$i]KKC')->textInput(['maxlength' => true]) ?>
</div>
<div class='col-md-1'>
<?= $form->field($data, '[$i]Vat')->textInput() ?>
</div>
<div class='col-md-2'>
<?= $form->field($data, '[$i]total')->textInput() ?>
</div>
<div class='col-md-2'>
<?= $form->field($data, '[$i]invoice_amt')->textInput() ?>
</div>
<?php // $form->field($data, '[]invoice_id')->hiddenInput()->label(false) ?>
<?php
}
echo '<div class="clearfix"></div>';
echo '</div>';
}
}
?>
</div>
<?php Pjax::end() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
And Here's the controller
$model = new invoice();
// $model_po = new purchaseorder();
$modelTaxes = [new InvoiceTaxes];
if ($model->load(Yii::$app->request->post())) {
$modelTaxes = Model::createMultiple(InvoiceTaxes::classname());
Model::loadMultiple($modelTaxes, Yii::$app->request->post());
$p =purchaseorder::find()->where(['id'=>$model->abc])->one();
$poid = $model->abc;
$model->poid =$poid;
$model->type ='External';
$model->po_date = $p->orderdate;
$po_noo = purchaseorder::find()->where(['id'=>$model->poid])->One();
$model->po_number =$po_noo->po;
$valid = $model->validate();
$valid = Model::validateMultiple($modelTaxes) && $valid;
$count = invoice::find()->where(['invoice_no' => $model->invoice_no])->count();
if($count > 0) {
Yii::$app->getSession()->setFlash("error", "Same Invoice No. is already in used, please use another one");
return $this->render('create', [
'model' => $model,
]);
}
if ($flag = $model->save(false)) {
foreach ($modelTaxes as $data) {
$data->invoice_id = $model->id;
if (! ($flag = $data->save(false))) {
$transaction->rollBack();
break;
}
}
}
return $this->redirect(['index']);
} else {
return $this->render('create', [
'model' => $model,
'modelTaxes' => (empty($modelTaxes)) ? [new InvoiceTaxes] : $modelTaxes,
]);
}
Ok! I have solved my problem. This was the first time I was implementing tabular form so I was having some trouble getting it worked. I have rewrite the code. Here's the tabular form part.
And here's the controller code. The controller code is mixed up with the invoice model code.