aboutsummaryrefslogtreecommitdiff
path: root/src/components/Display/SummaryDisplay.js
blob: 97c24b57fcc5cc5d03e0e12a5d875f37548f5515 (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
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;

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

  // TODO: add support for calculating gst from TotalPriceAfterDiscount

  return (
    {
      "TotalRawPrice":           totalRawPrice,
      "TotalDiscountPrice":      totalDiscount,
      "TotalPriceAfterDiscount": totalRawPrice - totalDiscount,
      "TotalTaxAmount":          totalTax,
      "TotalPrice":              (totalRawPrice - totalDiscount) + totalTax,
    }
  );
}

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"}>
      <p>Total raw: {summary.TotalRawPrice}</p>
      <p>Total after discount: {summary.TotalRawPrice} - {summary.TotalDiscountPrice} = {summary.TotalPriceAfterDiscount}</p>
      <p>Total tax: {summary.TotalTaxAmount}</p>
      <p>Total: {summary.TotalPriceAfterDiscount} + {summary.TotalTaxAmount} = {summary.TotalPrice}</p>
    </div>
  );
}

export default SummaryDisplay;