aboutsummaryrefslogtreecommitdiff
path: root/src/components/Display/SummaryDisplay.js
blob: d823713430930cb7bf41715d34af3ad22fce0d86 (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
import React from "react";
import "./Display.css";

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":           totalRawPrice.toFixed(2),
      "TotalDiscountPrice":      totalDiscount.toFixed(2),
      "TotalPriceAfterDiscount": (totalRawPrice - totalDiscount).toFixed(2),
      "TotalTaxAmount":          totalTax.toFixed(2),
      "TotalPriceAfterTax":      totalPriceAfterTax.toFixed(2),
      "RoundedOff":              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>

        {true &&// summary.TotalDiscountPrice !== 0 &&
          <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>

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

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

export default SummaryDisplay;