You are on page 1of 6

<body>

<div id="bill">
</div>
<script>

let Categories = [
{
id : "C1",
categoryName : "Platters",
superCategory : {

superCategoryName : "South Indian",


id : "SC1"
}
},

{
id : "C2",
categoryName : "Tandoor",
superCategory : {

superCategoryName : "North Indian",


id : "SC2"
}
},

{
id : "C3",
categoryName : "Dosa",
superCategory : {

superCategoryName : "South Indian",


id : "SC3"
}
},

{
id : "C4",
categoryName : "Vegetables",
superCategory : {

superCategoryName : "North Indian",


id : "SC4"
}
}

var items = [
{
id : "item1",
itemName : "Butter Roti",
rate : 20,
taxes : [
{
name : "Service Charge",
rate : 10,
isInPercent : 'Y'
},
{
name : "GST",
rate : 18,
isInPercent : 'Y'
},

],
category : {
categoryId : "C2"
}

},

{
id : "item2",
itemName : "Paneer Butter Masala",
rate : 120,
taxes : [
{
name : "Service Charge",
rate : 10,
isInPercent : 'Y'
},
{
name : "GST",
rate : 18,
isInPercent : 'Y'
},
{
name : "Service Tax",
rate : 10,
isInPercent : 'Y'
},

],
category : {
categoryId : "C4"
}

},

{
id : "item3",
itemName : "Masala Dosa",
rate : 50,
taxes : [
{
name : "GST",
rate : 18,
isInPercent : 'Y'
},
{
name : "Service Tax",
rate : 10,
isInPercent : 'Y'
},
],
category : {
categoryId : "C3"
}

},

{
id : "item4",
itemName : "Dosa Platter",
rate : 150,
taxes : [

{
name : "Service Tax",
rate : 10,
isInPercent : 'Y'
},

],
category : {
categoryId : "C1"
}

},

]
var Bill = {
id : "B1",
billNumber : 1,
opentime : "06 Nov 2020 14:19",
customerName : "CodeQuotient",
billItems : [
{
id : "item2",
quantity : 3,
discount : {
rate : 10,
isInPercent : 'Y'
}

},
{
id : "item1",
quantity : 9,
discount : {
rate : 10,
isInPercent : 'Y'
}

},
{
id : "item4",
quantity : 2,
discount : {
rate : 15,
isInPercent : 'Y'
}

}
]
}

function calc_bill(){
let totalPrice = 0;
let billItems = Bill.billItems; // items in the bill
billItems.forEach(billitem=>{ // billitem is individual item in the bill
let quantity = billitem.quantity; // each item quantity
let item_id = billitem.id;
let itemDetails = getItemDetails(item_id);
if(itemDetails)
{
// console.log(itemDetails);
let priceTag = itemDetails.rate; // initial price
let discountAmount = calculateDiscount(priceTag, billitem.discount); //
calculate unit item discout amount that will get deducted
// console.log(priceTag+"\t"+discountAmount);
let itemPriceWithDiscount = (priceTag-discountAmount)*quantity; // after
discount over n quantity of a single item without tax
let taxAmount = calculateTaxAmount(itemPriceWithDiscount,
itemDetails.taxes);
let totalItemPrice = itemPriceWithDiscount + taxAmount; // applying all
taxes
billitem.name = itemDetails.itemName;
billitem["taxes"] = itemDetails.taxes;
billitem["amount"] = totalItemPrice;
totalPrice += totalItemPrice;

//fetching category details


let categoryDetails = getCategoryDetails(itemDetails.category.categoryId);
// console.log(categoryDetails);
let categoryName = categoryDetails.categoryName;
let superCategoryName = categoryDetails.superCategory.superCategoryName;
// console.log(categoryName+" "+superCategoryName);
billitem.superCategoryName = superCategoryName;
billitem.categoryName = categoryName;
// console.log(itemDetails.itemName);
}
else
{
console.log(`${billitem} doesn't exist in items list`);
}
})
Bill["Total Amount"] = totalPrice;
}

function calculateTaxAmount(price, taxes){


let totalTaxAmount = 0; // after adding up all types of tax amount
taxes.forEach(tax=>{
let currentTaxAmount = 0; // for each type of taxes
if(tax.isInPercent=='Y')
{
currentTaxAmount = price*tax.rate/100;
}
else
{
currentTaxAmount = tax.rate;
}
// console.log(`price ${price} rate ${tax.rate} taxAmount $
{currentTaxAmount}`);
totalTaxAmount += currentTaxAmount;
})
return totalTaxAmount;
}

function calculateDiscount(priceTag, discount)


{
let discountAmount = 0;
if(discount==undefined)
return discountAmount;
if(discount.isInPercent=='Y')
{
discountAmount = priceTag*discount.rate/100;
}
else
{
discountAmount += discount.rate;
}
return discountAmount;
}

function getItemDetails(item_id) // by default it is searching on items array


{
let itemDetail = items.filter(item => {
return item.id == item_id;
})
if(itemDetail.length==0)
return null;
else
return itemDetail[0];
}

function getCategoryDetails(categoryId) // by default it is working on the


Categories array
{
let categoryDetails = Categories.filter(category=>{
return category.id == categoryId;
})
if(categoryDetails.length<=0)
return null;
else if(categoryDetails.length>1)
{
console.log(`${categoryId} doesn't exist`);
return null;
}
else
{
return categoryDetails[0];
}
}
calc_bill();
let printBill = JSON.stringify(Bill) // Converted object to string.
document.getElementById("bill").innerHTML = printBill; // to print in div

</script>
</body>

You might also like