blob: e06eac5468685c525a8365b5fd7e6495796a7822 (
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/*
* 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";
interface props {
}
const getBasicSummary = (items) => {
let totalRawPrice = 0;
let totalQuantity = 0;
items.some((i) => {
totalRawPrice += i.TotalPrice;
totalQuantity += i.Quantity;
return null;
}
)
return (
{
"TotalRawPrice": totalRawPrice,
"TotalQuantity": totalQuantity
}
);
}
// TODO: remove mutability from this function
const getFullSummary = (items) => {
let totalRawPrice = 0;
let totalDiscount = 0; // to be subtracted from totalRawPrice
let totalTax = 0;
items.some((i) => {
const itemTotalPrice = i.TotalPrice;
const itemDiscount = (i.Discount / 100) * itemTotalPrice;
totalRawPrice += itemTotalPrice;
totalDiscount += itemDiscount;
totalTax += i.igst === ""
? ((i.sgst + i.cgst) / 100) * (itemTotalPrice - itemDiscount)
: (i.igst / 100) * (itemTotalPrice - itemDiscount);
return null;
})
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: React.FC<props> = (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 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>
<tbody>
<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>
</tbody>
</table>
</div>
);
}
export default SummaryDisplay;
|