我正在处理yii2中的发票,其中存储了项price
和qnty
,但每个项目的amount
都是按qnty
和{{}}的乘积计算的{1}}。
我现在想要在发票下面得到发票的price
,这个数字将是发票项目的所有totals
的总和。请记住,amounts
值是计算的而不是存储的,如何根据这些数字计算amount
?
请注意,我无法直接从数据库表中获取值,因为totals
值是其他函数的派生。
以下是我的观点:
price
答案 0 :(得分:1)
创建变量
$lineItemPrice
:存储订单项价格$subTotal
:存储所有行/行,即$lineItemPrice
项目价格。在foreach循环中添加$lineItemPrice
中的所有$subTotal
。并且,显示在您想要显示的末尾。要显示Total
,您需要使用$subTotal
计算tax
并显示在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>