【问题标题】:Yii2 - How to get the sum of computational figuresYii2 - 如何获得计算数字的总和
【发布时间】:2017-09-20 13:22:31
【问题描述】:

我正在 yii2 中处理发票,其中存储了项目 priceqnty,但每个项目的 amount 是由 qntyprice 的乘积计算的。 我现在想在发票下方获取发票的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>

【问题讨论】:

    标签: php mysql view yii2


    【解决方案1】:

    创建变量

    1. $lineItemPrice : 存储订单项价格
    2. $subTotal:存储所有行/行,即$lineItemPrice 商品价格。

    在 foreach 循环中的 $subTotal 中添加所有 $lineItemPrice。并且,显示在您想要显示的末尾。要显示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>
    

    【讨论】:

    • 哇,如此简单明了,我从没想过那一行。非常感谢。我会接受答案。
    • 是的。不过这很容易。没问题。我们从错误中学习。继续编码。享受。 @japheth
    猜你喜欢
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2022-12-17
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多