/* * 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, {Dispatch, SetStateAction} from "react"; import { Item, InvoiceSummary } from "./../../Interfaces/interfaces" import "./Display.scss"; interface Props { items: Item[] setShowSubmitMenu: Dispatch> } interface PropsTR { items: Item[] } const getSummary = (items: Item[]): InvoiceSummary => { var rawPrice: number = 0; var totalDiscount: number = 0; var totalGST: number = 0; var totalQTY: number = 0; for (let i in items) { const item = items[i]; totalQTY += item.Quantity; rawPrice += item.TotalValue; totalDiscount += item.DiscountValue; totalGST += item.TotalGSTValue; } const totalPriceAfterDiscount: number = rawPrice - totalDiscount; const totalPriceAfterGST: number = totalPriceAfterDiscount + totalGST; const totalRoundedOff: number = parseFloat(Math.abs( (totalPriceAfterGST) - Math.round(totalPriceAfterGST) ).toFixed(2)); // rounded off value in 0.00 format return { TotalQuantity: totalQTY, TotalRawPrice: rawPrice, TotalDiscount: totalDiscount, TotalGST: totalGST, TotalPriceAfterDiscount: totalPriceAfterDiscount, TotalPriceAfterGST: totalPriceAfterGST, TotalRoundedOff: totalRoundedOff } } export const SummaryDisplayTR: React.FC = (props) => { const summary: InvoiceSummary = getSummary(props.items); return ( Total {summary.TotalQuantity} {summary.TotalRawPrice} {summary.TotalDiscount} {summary.TotalGST} {summary.TotalPriceAfterGST} ); } const SummaryDisplay: React.FC = (props) => { const summary: InvoiceSummary = getSummary(props.items); return (

Summary

{summary.TotalDiscount !== 0.00 && } {summary.TotalRoundedOff !== 0.00 && }
Base Total {summary.TotalRawPrice}
After Discount {(summary.TotalPriceAfterDiscount).toFixed(2)} (-{summary.TotalDiscount})
After Tax {(summary.TotalRawPrice + summary.TotalGST).toFixed(2)} (+{summary.TotalGST})
Rounded Off {summary.TotalRoundedOff}
Grand Total {Math.round(summary.TotalRawPrice + (summary.TotalGST - summary.TotalDiscount))}
{props.items.length > 0 && }
); } export default SummaryDisplay;