This commit is contained in:
Sampanna Rimal
2024-07-22 18:01:09 +05:45
parent c349ab3173
commit 78c76c2ef0
7 changed files with 81 additions and 25 deletions

View File

@ -324,11 +324,14 @@
</div>
<script>
$(document).ready(function() {
var counter = $('.purchase-detail').length;
// Clone purchase detail div
var purchaseDetailClone = $(".purchase-detail").clone().show();
// Add Purchase Detail
$(document).on("click", ".addPurchaseDetail", function() {
counter+=1;
var newPurchaseDetail = purchaseDetailClone.clone();
newPurchaseDetail.find('.bbsn').text(counter);
$(".purchase-details-section").append(newPurchaseDetail);
});
@ -361,6 +364,7 @@
// Remove Purchase Detail
$(document).on("click", ".remove-purchase-detail", function() {
$(this).closest(".purchase-detail").remove();
updateSerialNumbers();
calculateTotals();
});
// Calculate totals on change of rate or quantity
@ -374,6 +378,17 @@
calculateTotals();
});
$(document).on("change", "input[name=discount]", function() {
calculateRowTotal($(this).closest(".purchase-detail"));
calculateTotals('amount');
});
function updateSerialNumbers(){
$('.purchase-detail').each(function(index){
$(this).find('.bbsn').text(index + 1);
})
counter = $('.purchase-detail').length;
}
// Calculate row total
function calculateRowTotal(row) {
var quantity = parseFloat(row.find("input[name^='quantity']").val()) || 0;
@ -382,20 +397,32 @@
row.find("input[name^='total']").val(total.toFixed(2));
}
// Calculate totals
function calculateTotals() {
function calculateTotals(type='') {
var subtotal = 0;
$(".purchase-detail").each(function() {
var total = parseFloat($(this).find("input[name^='total']").val()) || 0;
subtotal += total;
});
var discountpercent = parseFloat($("input[name=discountpercentage]").val());
var discountamount = subtotal * (discountpercent/100);
if(type=='amount'){
alert("amount");
discountamount = parseFloat($("input[name='discount']").val());
discountpercent = (discountamount * 100) / subtotal;
alert(discountamount);
$("#discountpercentage").val(discountpercent.toFixed(2));
}else{
discountpercent = parseFloat($("input[name='discountpercentage']").val());
discountamount = subtotal * (discountpercent/100);
$("#discount").val(discountamount.toFixed(2));
}
// var discountpercent = parseFloat($("input[name=discountpercentage]").val());
// var discountamount = subtotal * (discountpercent/100);
var taxable = subtotal - discountamount;
var tax = taxable * 0.13;
var grandTotal = taxable + tax;
$("#subtotal").val(subtotal.toFixed(2));
$("#discount").val(discountamount.toFixed(2));
// $("#discount").val(discountamount.toFixed(2));
$("#taxable").val(taxable.toFixed(2));
$("#tax").val(tax.toFixed(2));
$("#grand_total").val(grandTotal.toFixed(2));