diff --git a/__mocks__/bills.mock.ts b/__mocks__/bills.mock.ts index 321371dd2..b9e8f32fa 100644 --- a/__mocks__/bills.mock.ts +++ b/__mocks__/bills.mock.ts @@ -70,7 +70,6 @@ export const billsSummary = [ description: 'Mpesa', retired: false, }, - attributes: [ { uuid: '84aacfcc-9788-4f7b-a56a-407d84af1b54', diff --git a/packages/esm-billing-app/src/helpers/functions.ts b/packages/esm-billing-app/src/helpers/functions.ts new file mode 100644 index 000000000..b648cf164 --- /dev/null +++ b/packages/esm-billing-app/src/helpers/functions.ts @@ -0,0 +1,32 @@ +import { Payment, LineItem } from '../types'; + +// amount already paid +export function calculateTotalAmountTendered(payments: Array) { + return Array.isArray(payments) + ? payments.reduce((totalAmount, item) => { + // Ensure that "amount" property is present and numeric + if (typeof item.amount === 'number' && item.voided !== true) { + return totalAmount + item.amount; + } + return totalAmount; + }, 0) + : 0; +} + +// balance +export function calculateTotalBalance(lineItems: Array, payments: Array) { + return Math.min(this.calculateTotalAmount(lineItems) - this.calculateTotalAmountTendered(payments)); +} + +// total bill +export function calculateTotalAmount(lineItems: Array) { + return Array.isArray(lineItems) + ? lineItems.reduce((totalAmount, item) => { + // Ensure that "price" and "quantity" properties are present and numeric + if (typeof item.price === 'number' && typeof item.quantity === 'number' && item.voided !== true) { + return totalAmount + item.price * item.quantity; + } + return totalAmount; + }, 0) + : 0; +} diff --git a/packages/esm-billing-app/src/helpers/index.ts b/packages/esm-billing-app/src/helpers/index.ts new file mode 100644 index 000000000..484b71595 --- /dev/null +++ b/packages/esm-billing-app/src/helpers/index.ts @@ -0,0 +1 @@ +export * from './functions'; diff --git a/packages/esm-billing-app/src/types/index.ts b/packages/esm-billing-app/src/types/index.ts index 4b4ac4f7b..8cff93682 100644 --- a/packages/esm-billing-app/src/types/index.ts +++ b/packages/esm-billing-app/src/types/index.ts @@ -46,7 +46,7 @@ interface Provider { links: ProviderLink[]; } -interface LineItem { +export interface LineItem { uuid: string; display: string; voided: boolean; @@ -103,7 +103,7 @@ interface PaymentInstanceType { retired: boolean; } -interface Payment { +export interface Payment { uuid: string; instanceType: PaymentInstanceType; attributes: Attribute[];