From bfff49e353701a40b44c7b6e0685a85309450867 Mon Sep 17 00:00:00 2001 From: akshayitzme Date: Thu, 31 Aug 2023 14:57:14 +0530 Subject: [PATCH] chore: code cleanup --- src/components/POS/SelectedItemTable.vue | 1 - src/pages/POS/POS.vue | 308 ++++++++--------------- src/pages/POS/PaymentModal.vue | 2 + src/utils/pos.ts | 149 +++++++++++ 4 files changed, 249 insertions(+), 211 deletions(-) create mode 100644 src/utils/pos.ts diff --git a/src/components/POS/SelectedItemTable.vue b/src/components/POS/SelectedItemTable.vue index 1dd4c261c..5813705c0 100644 --- a/src/components/POS/SelectedItemTable.vue +++ b/src/components/POS/SelectedItemTable.vue @@ -51,7 +51,6 @@ import { defineComponent } from 'vue'; import { Field } from 'schemas/types'; import { SalesInvoice } from 'models/baseModels/SalesInvoice/SalesInvoice'; import { SalesInvoiceItem } from 'models/baseModels/SalesInvoiceItem/SalesInvoiceItem'; -import { log } from 'console'; export default defineComponent({ name: 'SelectedItemTable', diff --git a/src/pages/POS/POS.vue b/src/pages/POS/POS.vue index a301ba311..67a011de3 100644 --- a/src/pages/POS/POS.vue +++ b/src/pages/POS/POS.vue @@ -35,7 +35,7 @@
{ + const itemQtyMap: ItemQtyMap = {}; + const valuationMethod = + fyo.singles.InventorySettings?.valuationMethod ?? ValuationMethod.FIFO; + + const rawSLEs = await getRawStockLedgerEntries(fyo); + const rawData = getStockLedgerEntries(rawSLEs, valuationMethod); + + const stockBalance = getStockBalanceEntries(rawData, {}); + + for (const row of stockBalance) { + if (!itemQtyMap[row.item]) { + itemQtyMap[row.item] = { availableQty: 0 }; + } + + if (row.batch) { + itemQtyMap[row.item][row.batch] = row.balanceQuantity; + } + + itemQtyMap[row.item].availableQty += row.balanceQuantity; + } + return itemQtyMap; +} + +export function getTotalQuantity(items: SalesInvoiceItem[]): number { + let totalQuantity = safeParseFloat(0); + + if (!items.length) { + return totalQuantity; + } + + for (const item of items) { + const quantity = item.quantity ?? 0; + totalQuantity = safeParseFloat(totalQuantity + quantity); + } + return totalQuantity; +} + +export function getItemDiscounts(items: SalesInvoiceItem[]): Money { + let itemDiscounts = fyo.pesa(0); + + if (!items.length) { + return itemDiscounts; + } + + for (const item of items) { + if (!item.itemDiscountAmount?.isZero()) { + itemDiscounts = itemDiscounts.add(item.itemDiscountAmount as Money); + } + + if (item.amount && (item.itemDiscountPercent as number) > 1) { + itemDiscounts = itemDiscounts.add( + item.amount.percent(item.itemDiscountPercent as number) + ); + } + } + return itemDiscounts; +} + +export async function getItem(item: string): Promise { + const itemDoc = (await fyo.doc.getDoc(ModelNameEnum.Item, item)) as Item; + if (!itemDoc) { + return; + } + + return itemDoc; +} + +export function validateSinv( + items: SalesInvoiceItem[], + itemQtyMap: ItemQtyMap +) { + if (!items) { + return; + } + + for (const item of items) { + if (!item.quantity || item.quantity < 1) { + throw new ValidationError( + t`Invalid Quantity for Item ${item.item as string}` + ); + } + + if (!itemQtyMap[item.item as string]) { + throw new ValidationError(t`Item ${item.item as string} not in Stock`); + } + + if (item.quantity > itemQtyMap[item.item as string].availableQty) { + throw new ValidationError( + t`Insufficient Quantity. Item ${item.item as string} has only ${ + itemQtyMap[item.item as string].availableQty + } quantities available. you selected ${item.quantity}` + ); + } + } +} + +export async function validateShipment(itemSerialNumbers: ItemSerialNumbers) { + if (!itemSerialNumbers) { + return; + } + + for (const idx in itemSerialNumbers) { + const serialNumbers = itemSerialNumbers[idx].split('\n'); + + for (const serialNumber of serialNumbers) { + const status = await fyo.getValue( + ModelNameEnum.SerialNumber, + serialNumber, + 'status' + ); + + if (status !== 'Active') { + throw new ValidationError( + t`Serial Number ${serialNumber} status is not Active.` + ); + } + } + } +} + +export function getTotalTaxedAmount(sinvDoc: SalesInvoice): Money { + let totalTaxedAmount = fyo.pesa(0); + if (!sinvDoc.items?.length || !sinvDoc.taxes?.length) { + return totalTaxedAmount; + } + + for (const row of sinvDoc.taxes) { + totalTaxedAmount = totalTaxedAmount.add(row.amount as Money); + } + return totalTaxedAmount; +}