【问题标题】:Calculate Array of Discounts计算折扣数组
【发布时间】:2020-03-13 18:11:14
【问题描述】:

如何在初始时根据基本价格计算折扣数组,然后根据折扣后金额计算?

在下面的屏幕截图中,我有基本价格。这个底价可以有多个折扣。

每个折扣只能在计算出的基本价格之后提供。

我的 PHP 脚本如下;

<tbody>
    <div class="form-group">
        <label class=\"col-sm-3\" style='color: green'><strong>Base Price</strong></label>
        <div class='col-sm-8'>
            <input type='number' class='form-control' id='basePrice' name="basePrice" value='5985'>
        </div>
    </div>
    <?php
    $query = $connect->prepare("SELECT * FROM `manage_customers_discount` WHERE status = 1 AND company_id = '1' AND discount_is_deleted = 0");
    $query->execute();
    $query->store_result();

    $rows = $query->num_rows;
    $rows = $rows + 1;

    $arrayDiscountNumber = 0;
    for ($y = 1; $y < $rows; $y++) { ?>

        <tr id="row<?php echo $y; ?>" class="<?php echo $arrayDiscountNumber; ?>">
            <td style="margin-left:20px;">

                <div class="form-group">

                    <select class="form-control" name="discountName[]" id="discountName<?php echo $y; ?>" onchange="getDiscountData(<?php echo $y; ?>)">
                        <option value="">~~SELECT~~</option>
                        <?php
                            $discountSql = "SELECT * FROM `manage_customers_discount` WHERE status = 1 AND company_id = '1' AND discount_is_deleted = 0 ORDER BY discount_order ASC";
                            $discountData = $connect->query($discountSql);

                            while ($row = $discountData->fetch_array()) {
                                echo "<option value='" . $row['id'] . "' id='changeDiscount" . $row['id'] . "'>" . $row['discount_name'] . "</option>";
                            } // /while

                            ?>
                    </select>
                </div>
            </td>
            <td style="padding-left:20px;">
                <input type="text" name="rateDiscount[]" id="rateDiscount<?php echo $y; ?>" autocomplete="off" disabled="true" class="form-control" />
                <input type="hidden" name="rateDiscountValue[]" id="rateDiscountValue<?php echo $y; ?>" autocomplete="off" class="form-control" />
            </td>
            <td style="padding-left:20px;">
                <input type="text" name="totalDiscount[]" id="totalDiscount<?php echo $y; ?>" autocomplete="off" class="form-control" disabled="true" />
                <input type="hidden" name="totalDiscountValue[]" id="totalDiscountValue<?php echo $y; ?>" autocomplete="off" class="form-control" />
            </td>
            <td style="padding-left:20px;">
                <input type="text" name="amountAfterDiscount[]" id="amountAfterDiscount<?php echo $y; ?>" autocomplete="off" class="form-control" disabled="true" />
                <input type="hidden" name="amountAfterDiscountValue[]" id="amountAfterDiscountValue<?php echo $y; ?>" autocomplete="off" class="form-control" />
            </td>
            <td>

                <button class="btn btn-default removeDiscountRowBtn" type="button" id="removeDiscountRowBtn" onclick="removeDiscountRow(<?php echo $y; ?>)"><i class="glyphicon glyphicon-trash"></i></button>
            </td>
        </tr>
    <?php
        $arrayDiscountNumber++;
    } // /for
    ?>
</tbody>

正如您在屏幕截图中看到的,我计算了基价,它给了我折扣后的第一个金额Base Price * 15%。在第二个折扣中,我需要计算First Amount After Discount * 10%。与第三次折扣相同。折扣后的最后一个金额将显示在子折扣金额中。

下面是我的JS,

var subTotalValue = $('#basePrice').val();

$("#rateDiscount" + row).val(response.percentage);
$("#rateDiscountValue" + row).val(response.percentage);

var total = Number(response.percentage) * Number(subTotalValue);
total = total.toFixed(2);
$("#totalDiscount" + row).val(total);
$("#totalDiscountValue" + row).val(total);

var total = Number(subTotalValue) - Number(total);
total = total.toFixed(2);
$("#amountAfterDiscount" + row).val(total);
$("#amountAfterDiscountValue" + row).val(total);

问题:它正在计算相对于基本价格的所有百分比

【问题讨论】:

    标签: javascript percentage


    【解决方案1】:

    我找到了一个解决方案,但它是一个丑陋的解决方案。我只想与您分享这一点,因为我知道您可以将此代码翻译成更好的代码。我不是专业的程序员,所以请不要笑我的解决方案或投反对票。我只是想和你分享这个,而不是告诉你这是正确的。再次,这是给你一个想法。

    现在,

    因为我在我的SELECT 选项onchange="getDiscountData(&lt;?php echo $y; ?&gt;)" 上有这个, 我只是手动检查该行。如果row == 1 那么它应该采取基本价格。 否则,它将采用第一个Amount After Discount

    请参阅下面的丑陋代码。

                            if(row == 1){
                                var subTotalValue = $("#basePrice").val();
    
                                $("#rateDiscount"+row).val(response.percentage);
    
                                var total = Number(response.percentage) * Number(subTotalValue);
                                total = total.toFixed(2);
                                $("#totalDiscount"+row).val(total);
    
                                var total = Number(subTotalValue) - Number(total);
                                total = total.toFixed(2);
                                $("#amountAfterDiscount"+row).val(total);
                            }
                            if(row == 2){
                                var subTotalValue = $("#amountAfterDiscount"+1).val();
    
                                $("#rateDiscount"+row).val(response.percentage);
    
                                var total = Number(response.percentage) * Number(subTotalValue);
                                total = total.toFixed(2);
                                $("#totalDiscount"+row).val(total);
    
                                var total = Number(subTotalValue) - Number(total);
                                total = total.toFixed(2);
                                $("#amountAfterDiscount"+row).val(total);
                            }if(row==3){
                                var subTotalValue = $("#amountAfterDiscount"+2).val();
    
                                $("#rateDiscount"+row).val(response.percentage);
    
                                var total = Number(response.percentage) * Number(subTotalValue);
                                total = total.toFixed(2);
                                $("#totalDiscount"+row).val(total);
    
                                var total = Number(subTotalValue) - Number(total);
                                total = total.toFixed(2);
                                $("#amountAfterDiscount"+row).val(total);
                                $("#subDiscountAmount"+row).val(total);
                            }
    

    如果您可能需要相同的解决方案,请尽可能做到最好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-05
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      相关资源
      最近更新 更多