2017-07-11 5 views
0

Für ein Projekt muss ich ein Tabellenformular für Invoice Taxes basierend auf Purchase order details in der Invoice Form generieren. Wenn ich das po_number Feld wähle, wird der Wert an die URL angehängt und das pjax div mit dem Namen paticular aktualisiert. Ich habe das Formular erfolgreich generiert und die Zeilen werden auch für den Tabellenformteil generiert und in die Invoice Taxes Tabelle eingefügt, aber nur die invoice_id und invoice_amt werden eingefügt.Yii2 Tabellenformdaten werden nicht in Tabelle eingefügt

Hier ist meine Form für 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(); ?> 

Und Hier ist der 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, 

       ]); 
      } 

Antwort

0

Ok! Ich habe mein Problem gelöst. Dies war das erste Mal, dass ich eine Tabellenform implementierte, so dass ich Schwierigkeiten hatte, es funktionierte zu bekommen. Ich habe den Code neu geschrieben. Hier ist der tabellarische Formteil.

// tabular form for invoice taxes 
    if (isset($_GET['pod'])) { 
     $id = $_GET['pod']; 
     if ($model->isNewRecord) { 
      $modelPods = purchaseorderdetails::find()->where(['purchaseorderid' => $id]); 
      $length = $modelPods->count(); 
      $modelPods = $modelPods->all(); 
      $x = 0; 
      foreach ($modelPods as $pod) { 
       $tempAmt[$x] = $pod->net_amt; 
       $x++; 
      } 
      for ($i = 0; $i < $length; $i++) { 
       echo '<div class="" style="border: 1px solid #ccc; border-radius: 2px; padding-top: 10px; margin-bottom:10px;">'; 

       foreach ($modelTaxes as $data) { 
?> 
    <div class='col-md-2'> 
    <?= $form->field($data, "[$i]net_amt")->textInput(['value' => $tempAmt[$i], 'disabled' => 'true'])->label(false) ?> 
    </div> 

    <div class='col-md-1'> 
    <?= $form->field($data, "[$i]ST")->textInput(['placeholder' => 'ST'])->label(false) ?> 
    </div> 

    <div class='col-md-2'> 
    <?= $form->field($data, "[$i]SBC")->textInput(['maxlength' => true, 'placeholder' => 'SBC'])->label(false) ?> 
    </div> 

    <div class='col-md-2'> 
    <?= $form->field($data, "[$i]KKC")->textInput(['maxlength' => true, 'placeholder' => 'KKC'])->label(false) ?> 
    </div> 

    <div class='col-md-1'> 
    <?= $form->field($data, "[$i]Vat")->textInput(['placeholder' => 'Vat'])->label(false) ?> 
    </div> 

    <div class='col-md-2'> 
    <?= $form->field($data, "[$i]total")->textInput(['placeholder' => 'Total'])->label(false) ?> 
    </div> 

    <div class='col-md-2'> 
    <?= $form->field($data, "[$i]invoice_amt")->textInput(['placeholder' => 'Invoice Amt'])->label(false) ?> 
    </div> 

    <?php // $form->field($data, '[]invoice_id')->hiddenInput()->label(false) ?> 
<?php 
       } 

       echo '<div class="clearfix"></div>'; 
       echo '</div>'; 
      } 
     } 
    } 

Und hier ist der Controller-Code. Der Controller-Code ist mit dem Rechnungsmodellcode verwechselt.

if ($model->load(Yii::$app->request->post())) { 

       // For tabular form 
       $count = count(Yii::$app->request->post('InvoiceTaxes', [])); 
       for($i = 1; $i < $count; $i++) { 
        $modelTaxes[] = new InvoiceTaxes(); 
       } 

       Model::loadMultiple($modelTaxes, Yii::$app->request->post()); 
       Model::validateMultiple($modelTaxes); 
       // end tabular form part 

       $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; 

       $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)) { 
        // for tabular form 
        foreach ($modelTaxes as $data) { 
         $data->invoice_id = $model->id; 
         $flag = $data->save(false); 
        // end tabular form 
         if (!($flag)) { 
          $transaction->rollBack(); 
          break; 
         } 
        } 
       } 
Verwandte Themen