Is there a way to determine the Array of Discounts based on the Base Price initially and then calculate them against the Amount After Discount?
In the image provided below, we have the Base Price. This base price may include multiple discounts.
Each discount should only be applied after the Base Price has been calculated.
My PHP script is as follows;
<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>
Considering the screenshot, I computed the base price which gave me the initial Amount after Discount Base Price * 15%
. For the second discount, I aim to compute the
First Amount After Discount * 10%
. The same logic applies to subsequent discounts until the final Amount after Discount is displayed in the Sub Discount Amount field.
Here is my JavaScript code,
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);
ISSUE: It appears that all percentages are currently being calculated against the Base Price