aboutsummaryrefslogtreecommitdiff
path: root/src/components/Display/SummaryDisplay.js
blob: a44e0362d73101792d3a3903cc8664dd591eac4b (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * OpenBills - Self hosted browser app to generate and keep track of simple invoices
 * Version - 0
 * Licensed under the MIT license - https://opensource.org/licenses/MIT
 *
 * Copyright (c) 2021 Vidhu Kant Sharma
*/

import React from "react";
import "./Display.scss";

const getBasicSummary = (items) => {
  let totalRawPrice = 0;
  let totalQuantity = 0;

  for (let i = 0; i < items.length; i++) {
    totalRawPrice += items[i].TotalPrice;
    totalQuantity += items[i].Quantity
  }

  return (
    {
      "TotalRawPrice": totalRawPrice,
      "TotalQuantity": totalQuantity
    }
  );
}

const getFullSummary = (items) => {
  let totalRawPrice = 0;
  let totalDiscount = 0; // to be subtracted from totalRawPrice
  let totalTax = 0;

  for (let i = 0; i < items.length; i++) {
    const itemTotalPrice = items[i].TotalPrice;
    const itemDiscount = (items[i].Discount / 100) * itemTotalPrice;

    totalRawPrice += itemTotalPrice;
    totalDiscount += itemDiscount;
    totalTax += (items[i].GST / 100) * (itemTotalPrice - itemDiscount);
  }

  const totalPriceAfterTax = (totalRawPrice - totalDiscount) + totalTax;
  const totalRoundedOff = Math.abs(totalPriceAfterTax - Math.round(totalPriceAfterTax));
  return (
    {
      "TotalRawPrice":           parseFloat(totalRawPrice.toFixed(2)),
      "TotalDiscountPrice":      parseFloat(totalDiscount.toFixed(2)),
      "TotalPriceAfterDiscount": parseFloat((totalRawPrice - totalDiscount).toFixed(2)),
      "TotalTaxAmount":          parseFloat(totalTax.toFixed(2)),
      "TotalPriceAfterTax":      parseFloat(totalPriceAfterTax.toFixed(2)),
      "RoundedOff":              parseFloat(totalRoundedOff.toFixed(2)),
      "TotalPrice":              Math.round(totalPriceAfterTax)
    }
  );
}

export const SummaryDisplayTR = (props) => {
  const summary = getBasicSummary(props.items);

  return (
    <tr className={"SummaryDisplayTR"}>
      <td>Total</td>
      <td className={"disabledBorder"}></td>
      <td className={"disabledBorder"}></td>
      <td>{summary.TotalQuantity}</td>
      <td className={"disabledBorder"}></td>
      <td className={"disabledBorder"}></td>
      <td className={"disabledBorder"}></td>
      <td>{summary.TotalRawPrice}</td>
    </tr>
  );
}

const SummaryDisplay = (props) => {
  const summary = getFullSummary(props.items);

  return (
    <div className={"SummaryDisplay"}>
      <h1>Summary</h1>
      <table>
        <tr>
          <td>Base Total</td>
          <td>{summary.TotalRawPrice}</td>
        </tr>

        {summary.TotalDiscountPrice !== 0.00 &&
          <tr>
            <td>After Discount</td>
            <td>{summary.TotalPriceAfterDiscount}</td>
            <td>(-{summary.TotalDiscountPrice})</td>
          </tr>
        }

        <tr>
          <td>After Tax</td>
          <td>{summary.TotalPriceAfterTax}</td>
          <td>(+{summary.TotalTaxAmount})</td>
        </tr>

        {summary.RoundedOff !== 0.00 &&
          <tr>
            <td>Rounded Off</td>
            <td>{summary.RoundedOff}</td>
          </tr>
        }

        <tr className={"grandTotal"}>
          <td>Grand Total</td> 
          <td>{summary.TotalPrice}</td>
        </tr>
      </table>
    </div>
  );
}

export default SummaryDisplay;