2017-09-20 4 views
0

Je travaille sur une facture dans laquelle l'élément yii2 price et qnty sont stockés mais le amount de chaque élément est calculé par le produit de qnty et price. Je veux maintenant obtenir le totals de la facture au-dessous de la facture de sorte que ce chiffre sera la somme de tous les amounts des articles de la facture. En gardant à l'esprit que la valeur amount est calculée et non stockée, comment puis-je calculer le totals à partir de ces chiffres? Notez que je ne peux pas obtenir les valeurs directement à partir de la table de base de données car la valeur price est dérivée d'autres fonctions.Yii2 - Comment obtenir la somme des chiffres de calcul

Voici mon avis:

<?php 

use yii\helpers\Html; 
use yii\widgets\DetailView; 

/* @var $this yii\web\View */ 
/* @var $model app\models\RepeatInvoice */ 

$this->title = 'Invoice'; 
$this->params['breadcrumbs'][] = ['label' => 'Repeat Invoices', 'url' => ['index']]; 
$this->params['breadcrumbs'][] = $this->title; 
?> 

<div class="repeat-invoice-view"> 

    <h1 align="center"><?= Html::encode($this->title) ?></h1> 

    <p align="right"> 
     <?= Html::a('Print', ['update', '#' => $model->id], ['class' => 'btn btn-default']) ?> 
     <?= Html::a('Export to PDF', ['#', 'id' => $model->id], ['class' => 'btn btn-default']) ?> 
     <?= Html::a('Email Invoice', ['#', 'id' => $model->id], ['class' => 'btn btn-default']) ?> 
    </p> 

    <!-- invoice --> 
    <section class="invoice"> 
     <!-- title row --> 
     <div class="row"> 
     <div class="col-xs-12"> 
      <h2 class="page-header"> 
      <i class="fa fa-globe"></i> <?php echo $model->company->name; ?> 
      <small class="pull-right">Date Created: <?php echo date("jS F, Y", strtotime($model->created_at)) ?></small> 
      </h2> 
     </div> 
     <!-- /.col --> 
     </div> 
     <!-- info row --> 
     <div class="well row"> 
      <div class="col-sm-3"> 
       <p> 
        Repeat the invoice every 
       </p> 
       <p><strong> 
        <?php echo $model->repeat_every.' '.$model->repeat_period; ?> 
       </strong></p> 
      </div> 
      <div class="col-sm-3"> 
       <p> 
        Start Date 
       </p> 
       <p><strong> 
        <?php echo date("jS F, Y", strtotime($model->start_date)) ?> 
       </strong></p> 
      </div> 
      <div class="col-sm-3"> 
       <p> 
        Due Date 
       </p> 
       <p><strong> 
        <?php echo $model->dueDate; ?> 
       </strong></p> 
      </div> 
      <div class="col-sm-3"> 
       <p> 
        End Date 
       </p> 
       <p><strong> 
        <?php echo date("jS F, Y", strtotime($model->end_date)) ?> 
       </strong></p> 
      </div> 
     </div> 

     <div class="well row"> 
      <div class="col-sm-3"> 
       <p> 
        Tenant 
       </p> 
       <p><strong> 
        <?php echo $model->lease->tenant->tenantName; ?> 
       </strong></p> 
      </div> 
      <div class="col-sm-6"> 
       <p> 
        Tenancy 
       </p> 
       <p><strong> 
        <?php echo $model->lease->leaseDetails; ?> 
       </strong></p> 
      </div> 
      <div class="col-sm-3"> 
       <p> 
        Payable to: 
       </p> 
       <p><strong> 
        <?php echo $model->due_after_every.' '.$model->due_after_period; ?> 
       </strong></p> 
      </div> 
     </div> 

     <!-- Table row --> 
     <div class="row"> 
     <div class="col-xs-12 table-responsive"> 
      <table class="table table-striped"> 
      <thead> 
      <tr> 
       <th>Item</th> 
       <th>Description</th> 
       <th>Qty</th> 
       <th>Price</th> 
       <th>Amount</th> 
      </tr> 
      </thead> 
      <tbody> 
       <?php foreach ($repeatInvoiceItems as $indexItem => $repeatInvoiceItem): ?> 
        <tr> 
         <td> 
          <?php echo $repeatInvoiceItem->item->item; ?> 
         </td> 
         <td> 
          <?php echo $repeatInvoiceItem->desc; ?> 
         </td> 
         <td> 
          <?php echo $repeatInvoiceItem->qnty; ?> 
         </td> 
         <td> 
          <?php echo $repeatInvoiceItem->itemPrice; ?> 
         </td> 
         <td> 
          <?php echo $repeatInvoiceItem->qnty * $repeatInvoiceItem->itemPrice; ?> 
         </td> 
        </tr> 
       <?php endforeach; ?> 
      </tbody> 
      </table> 
     </div> 
     <!-- /.col --> 
     </div> 
     <!-- /.row --> 

     <div class="row"> 
     <!-- accepted payments column --> 
     <div class="col-xs-6"> 
      <p class="lead">Payment Instructions:</p> 

      <p class="text-muted well well-sm no-shadow" style="margin-top: 10px;"> 
      <?php echo $model->payment_instructions; ?> 
      </p> 
     </div> 
     <!-- /.col --> 
     <div class="col-xs-6"> 

      <div class="table-responsive"> 
      <table class="table"> 
       <tr> 
       <th style="width:50%">Subtotal:</th> 
       <td>$250.30</td> 
       </tr> 
       <tr> 
       <th>Tax (9.3%)</th> 
       <td>$10.34</td> 
       </tr> 
       <tr> 
       <th>Total:</th> 
       <td>$265.24</td> 
       </tr> 
      </table> 
      </div> 
     </div> 
     <!-- /.col --> 
     </div> 
     <!-- /.row --> 

     <!-- this row will not appear when printing --> 
     <div class="row no-print"> 
     <div class="col-xs-12"> 
      <a href="#" target="_blank" class="btn btn-default"><i class="fa fa-print"></i> Back</a> 
      <button type="button" class="btn btn-default pull-right"><i class="fa fa-credit-card"></i> Edit Invoice 
      </button> 
      <button type="button" class="btn btn-default pull-right" style="margin-right: 5px;"> 
      <i class="fa fa-download"></i> Add Note 
      </button> 
     </div> 
     </div> 
    </section> 
    <!-- /.invoice --> 
</div> 

Répondre

1

Créer une variable

  1. $lineItemPrice: pour stocker les articles de ligne prix
  2. $subTotal: pour stocker toute la ligne/rangée à savoir le prix $lineItemPrice articles.

Ajouter tout $lineItemPrice dans $subTotal à l'intérieur de foreach loop. Et, afficher à la fin où vous vouliez afficher. Pour afficher Total, vous devez calculer le $subTotal avec tax et afficher dans la colonne Total.

code

<div class="row"> 
    <div class="col-xs-12 table-responsive"> 
    <table class="table table-striped"> 
     <thead> 
     <tr> 
      <th>Item</th> 
      <th>Description</th> 
      <th>Qty</th> 
      <th>Price</th> 
      <th>Amount</th> 
     </tr> 
     </thead> 
     <tbody> 
     <?php 
     $subTotal = 0; 
     foreach ($repeatInvoiceItems as $indexItem => $repeatInvoiceItem): ?> 
      <tr> 
      <td> 
       <?php echo $repeatInvoiceItem->item->item; ?> 
      </td> 
      <td> 
       <?php echo $repeatInvoiceItem->desc; ?> 
      </td> 
      <td> 
       <?php echo $repeatInvoiceItem->qnty; ?> 
      </td> 
      <td> 
       <?php echo $repeatInvoiceItem->itemPrice; ?> 
      </td> 
      <td> 
       <?php 
       $lineItemPrice = $repeatInvoiceItem->qnty * $repeatInvoiceItem->itemPrice; 
       echo $lineItemPrice; 
       $subTotal += $lineItemPrice; 
       ?> 
      </td> 
      </tr> 
     <?php endforeach; ?> 
     </tbody> 
    </table> 
    </div> 
</div> 

<div class="row"> 
    <div class="col-xs-6"> 
    <p class="lead">Payment Instructions:</p> 
    <p class="text-muted well well-sm no-shadow" style="margin-top: 10px;"> 
     <?php echo $model->payment_instructions; ?> 
    </p> 
    </div> 
    <div class="col-xs-6"> 
    <div class="table-responsive"> 
     <table class="table"> 
     <tr> 
      <th style="width:50%">Subtotal:</th> 
      <td>$<?php echo number_format($subTotal,2);?></td> 
     </tr> 
     <tr> 
      <th>Tax (9.3%)</th> 
      <td>$10.34</td> 
     </tr> 
     <tr> 
      <th>Total:</th> 
      <td>$265.24</td> 
     </tr> 
     </table> 
    </div> 
    </div> 
</div> 

Pour Total,

<div class="col-xs-6"> 
    <div class="table-responsive"> 
    <table class="table"> 
     <tr> 
     <th style="width:50%">Subtotal:</th> 
     <td>$<?php echo number_format($subTotal,2);?></td> 
     </tr> 
     <?php 
     $tax = 9.3; 
     ?> 
     <tr> 
     <th>Tax (9.3%)</th> 
     <td> 
      <?php 
      echo $taxAmount = ($tax * $subTotal)/100; 
      ?> 
     </td> 
     </tr> 
     <tr> 
     <th>Total:</th> 
     <td> 
      <?php echo $grandTotal = $subTotal + $taxAmount;?> 
     </td> 
     </tr> 
    </table> 
    </div> 
</div> 
+0

Wow, si simple et clair, je ne pensais pas à cette ligne. Merci beaucoup. Je vais accepter la réponse. – japheth

+0

Oui. C'était facile cependant. Aucun problème. Nous apprenons des erreurs. Continuez à coder. Prendre plaisir. @japheth –