[英]Yii2 - How to get the sum of computational figures
我在yii2工作的發票,其中的項目price
和qnty
存儲,但amount
每個項目的是由產品來計算qnty
和price
。 我現在想使發票totals
低於發票,據此,該數字將是發票中所有項目amounts
的總和。 請記住, amount
是計算的而不是存儲的,我如何從這些數字中計算出totals
? 請注意,由於price
值是其他函數的派生產品,因此我無法直接從數據庫表中獲取值。
這是我的看法:
<?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>
創建變量
$lineItemPrice
:存儲訂單項的價格 $subTotal
:存儲所有行/行,即$lineItemPrice
項目價格。 添加全部$lineItemPrice
在$subTotal
foreach循環中。 並且,在想要顯示的末尾顯示。 要顯示Total
,您需要計算tax
的$subTotal
並顯示在Total
列中。
碼
<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>
對於總計,
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.