From ad67099a4b332f227705dc6842874469841b4cae Mon Sep 17 00:00:00 2001 From: MikunoNaka Date: Sun, 25 Apr 2021 16:49:32 +0530 Subject: added functionality to calculate the price after applying the discount on each product --- src/components/Display/SummaryDisplay.js | 33 ++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'src/components/Display') diff --git a/src/components/Display/SummaryDisplay.js b/src/components/Display/SummaryDisplay.js index 27ebe0b..aabd111 100644 --- a/src/components/Display/SummaryDisplay.js +++ b/src/components/Display/SummaryDisplay.js @@ -1,7 +1,7 @@ import React from "react"; import "./Display.css"; -const getSummary = (items) => { +const getBasicSummary = (items) => { let totalRawPrice = 0; let totalQuantity = 0; @@ -18,8 +18,30 @@ const getSummary = (items) => { ); } +const getFullSummary = (items) => { + let totalRawPrice = 0; + let totalDiscountPrice = 0; // to be subtracted from totalRawPrice + + for (let i = 0; i < items.length; i++) { + const itemTotalPrice = items[i].TotalPrice; + const itemDiscount = items[i].Discount; + + totalRawPrice += itemTotalPrice; + totalDiscountPrice += (itemDiscount / 100) * itemTotalPrice; + } + + // TODO: add support for calculating gst from TotalPriceAfterDiscount + + return ( + { + "TotalRawPrice": totalRawPrice, + "TotalPriceAfterDiscount": totalRawPrice - totalDiscountPrice + } + ); +} + export const SummaryDisplayTR = (props) => { - const summary = getSummary(props.items); + const summary = getBasicSummary(props.items); return ( @@ -36,10 +58,13 @@ export const SummaryDisplayTR = (props) => { } const SummaryDisplay = (props) => { - const summary = getSummary(props.items); + const summary = getFullSummary(props.items); return ( -

Total: {summary.TotalRawPrice}

+ <> +

Total: {summary.TotalRawPrice}

+

Total after discount: {summary.TotalPriceAfterDiscount}

+ ); } -- cgit v1.2.3