aboutsummaryrefslogtreecommitdiff
path: root/src/classes/invoice_item.ts
blob: fe92f105625e7a7c4c719c68616a973dc8656b78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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)
  })