import currency from "currency.js" export default class InvoiceItem { UnitOfMeasure: string Quantity: string Name: string Description: string HSN: string UnitPrice: string GSTPercentage: string BrandName: string constructor() { this.Name = '' this.Description = '' this.HSN = '' this.UnitPrice = '' this.GSTPercentage = '' this.UnitOfMeasure = '' this.Quantity = "" this.BrandName = "" } } export const calculate = (x: InvoiceItem) => { const quantity = currency(x.Quantity) const unitPrice = currency(x.UnitPrice) const gstPercentage = currency(x.GSTPercentage) const gstValue = unitPrice.multiply(gstPercentage).divide(100) const totalGSTValue = gstValue.multiply(quantity) const amountWithoutGST = unitPrice.multiply(quantity) return({ ...x , Quantity: quantity , UnitPrice: unitPrice , GSTValue: gstValue , TotalGSTValue: totalGSTValue , AmountWithoutGST: amountWithoutGST , TotalAmount: amountWithoutGST.multiply(gstPercentage).divide(100).add(amountWithoutGST) }) } export const calculateArr = (items: InvoiceItem[]) => items.map((x: InvoiceItem) => calculate(x)) export const calculateTotal = (items: InvoiceItem[]) => items.reduce((total, item) => { const c = calculate(item) return ({ TotalQuantity: total.TotalQuantity.add(c.Quantity), TotalGSTValue: total.TotalGSTValue.add(c.TotalGSTValue), TotalWithoutGST: total.TotalWithoutGST.add(c.AmountWithoutGST), TotalWithGST: total.TotalWithGST.add(c.TotalAmount) }) }, { TotalQuantity: currency(0), TotalGSTValue: currency(0), TotalWithoutGST: currency(0), TotalWithGST: currency(0) })