From 5061a0ce2218f35931ee3deafe2b83be2bfae89d Mon Sep 17 00:00:00 2001 From: akshayitzme Date: Tue, 24 Dec 2024 22:17:40 +0530 Subject: [PATCH] refactor: pos shift --- fyo/model/types.ts | 6 +- models/index.ts | 6 +- models/inventory/InventorySettings.ts | 2 +- .../Point of Sale/POSClosingShift.ts | 33 +++++++++ .../{POSShift.ts => POSOpeningShift.ts} | 32 +++------ models/inventory/Point of Sale/POSSettings.ts | 1 + models/types.ts | 7 +- .../Point of Sale/POSClosingShift.json | 24 +++++++ .../Point of Sale/POSOpeningShift.json | 24 +++++++ .../inventory/Point of Sale/POSSettings.json | 7 ++ .../app/inventory/Point of Sale/POSShift.json | 43 ------------ schemas/schemas.ts | 6 +- src/pages/POS/ClosePOSShiftModal.vue | 70 ++++++++++--------- src/pages/POS/OpenPOSShiftModal.vue | 19 ++--- src/pages/POS/POS.vue | 2 +- src/utils/pos.ts | 26 ++++++- 16 files changed, 185 insertions(+), 123 deletions(-) create mode 100644 models/inventory/Point of Sale/POSClosingShift.ts rename models/inventory/Point of Sale/{POSShift.ts => POSOpeningShift.ts} (58%) create mode 100644 schemas/app/inventory/Point of Sale/POSClosingShift.json create mode 100644 schemas/app/inventory/Point of Sale/POSOpeningShift.json delete mode 100644 schemas/app/inventory/Point of Sale/POSShift.json diff --git a/fyo/model/types.ts b/fyo/model/types.ts index 2868a5c05..9ca3a5365 100644 --- a/fyo/model/types.ts +++ b/fyo/model/types.ts @@ -11,7 +11,8 @@ import type { PrintSettings } from 'models/baseModels/PrintSettings/PrintSetting import type { InventorySettings } from 'models/inventory/InventorySettings'; import type { Misc } from 'models/baseModels/Misc'; import type { POSSettings } from 'models/inventory/Point of Sale/POSSettings'; -import type { POSShift } from 'models/inventory/Point of Sale/POSShift'; +import type { POSOpeningShift } from 'models/inventory/Point of Sale/POSOpeningShift'; +import type { POSClosingShift } from 'models/inventory/Point of Sale/POSClosingShift'; /** * The functions below are used for dynamic evaluation @@ -57,7 +58,8 @@ export interface SinglesMap { AccountingSettings?: AccountingSettings; InventorySettings?: InventorySettings; POSSettings?: POSSettings; - POSShift?: POSShift; + POSOpeningShift?: POSOpeningShift; + POSClosingShift?: POSClosingShift; PrintSettings?: PrintSettings; Defaults?: Defaults; Misc?: Misc; diff --git a/models/index.ts b/models/index.ts index ed4973b3e..cb7c39906 100644 --- a/models/index.ts +++ b/models/index.ts @@ -48,7 +48,8 @@ import { ClosingCash } from './inventory/Point of Sale/ClosingCash'; import { OpeningAmounts } from './inventory/Point of Sale/OpeningAmounts'; import { OpeningCash } from './inventory/Point of Sale/OpeningCash'; import { POSSettings } from './inventory/Point of Sale/POSSettings'; -import { POSShift } from './inventory/Point of Sale/POSShift'; +import { POSOpeningShift } from './inventory/Point of Sale/POSOpeningShift'; +import { POSClosingShift } from './inventory/Point of Sale/POSClosingShift'; export const models = { Account, @@ -102,7 +103,8 @@ export const models = { OpeningAmounts, OpeningCash, POSSettings, - POSShift, + POSOpeningShift, + POSClosingShift, } as ModelMap; export async function getRegionalModels( diff --git a/models/inventory/InventorySettings.ts b/models/inventory/InventorySettings.ts index 8cb64bce8..bf3d90fb9 100644 --- a/models/inventory/InventorySettings.ts +++ b/models/inventory/InventorySettings.ts @@ -46,7 +46,7 @@ export class InventorySettings extends Doc { return !!this.enableStockReturns; }, enablePointOfSale: () => { - return !!this.fyo.singles.POSShift?.isShiftOpen; + return !!this.fyo.singles.POSSettings?.isShiftOpen; }, }; } diff --git a/models/inventory/Point of Sale/POSClosingShift.ts b/models/inventory/Point of Sale/POSClosingShift.ts new file mode 100644 index 000000000..3d80754f0 --- /dev/null +++ b/models/inventory/Point of Sale/POSClosingShift.ts @@ -0,0 +1,33 @@ +import { ListViewSettings } from 'fyo/model/types'; +import { ClosingAmounts } from './ClosingAmounts'; +import { ClosingCash } from './ClosingCash'; +import { Doc } from 'fyo/model/doc'; + +export class POSClosingShift extends Doc { + closingAmounts?: ClosingAmounts[]; + closingCash?: ClosingCash[]; + closingDate?: Date; + + get closingCashAmount() { + if (!this.closingCash) { + return this.fyo.pesa(0); + } + + let closingAmount = this.fyo.pesa(0); + + this.closingCash.map((row: ClosingCash) => { + const denomination = row.denomination ?? this.fyo.pesa(0); + const count = row.count ?? 0; + + const amount = denomination.mul(count); + closingAmount = closingAmount.add(amount); + }); + return closingAmount; + } + + static getListViewSettings(): ListViewSettings { + return { + columns: ['name', 'closingDate'], + }; + } +} diff --git a/models/inventory/Point of Sale/POSShift.ts b/models/inventory/Point of Sale/POSOpeningShift.ts similarity index 58% rename from models/inventory/Point of Sale/POSShift.ts rename to models/inventory/Point of Sale/POSOpeningShift.ts index b56fc8591..b9e023ad4 100644 --- a/models/inventory/Point of Sale/POSShift.ts +++ b/models/inventory/Point of Sale/POSOpeningShift.ts @@ -1,14 +1,9 @@ -import { ClosingAmounts } from './ClosingAmounts'; -import { ClosingCash } from './ClosingCash'; import { Doc } from 'fyo/model/doc'; import { OpeningAmounts } from './OpeningAmounts'; import { OpeningCash } from './OpeningCash'; +import { ListViewSettings } from 'fyo/model/types'; -export class POSShift extends Doc { - closingAmounts?: ClosingAmounts[]; - closingCash?: ClosingCash[]; - closingDate?: Date; - isShiftOpen?: boolean; +export class POSOpeningShift extends Doc { openingAmounts?: OpeningAmounts[]; openingCash?: OpeningCash[]; openingDate?: Date; @@ -30,23 +25,6 @@ export class POSShift extends Doc { return openingAmount; } - get closingCashAmount() { - if (!this.closingCash) { - return this.fyo.pesa(0); - } - - let closingAmount = this.fyo.pesa(0); - - this.closingCash.map((row: ClosingCash) => { - const denomination = row.denomination ?? this.fyo.pesa(0); - const count = row.count ?? 0; - - const amount = denomination.mul(count); - closingAmount = closingAmount.add(amount); - }); - return closingAmount; - } - get openingTransferAmount() { if (!this.openingAmounts) { return this.fyo.pesa(0); @@ -58,4 +36,10 @@ export class POSShift extends Doc { return transferAmountRow.amount ?? this.fyo.pesa(0); } + + static getListViewSettings(): ListViewSettings { + return { + columns: ['name', 'openingDate'], + }; + } } diff --git a/models/inventory/Point of Sale/POSSettings.ts b/models/inventory/Point of Sale/POSSettings.ts index 4226a9db3..9f8f615a7 100644 --- a/models/inventory/Point of Sale/POSSettings.ts +++ b/models/inventory/Point of Sale/POSSettings.ts @@ -6,6 +6,7 @@ import { } from 'models/baseModels/Account/types'; export class POSSettings extends Doc { + isShiftOpen?: boolean; inventory?: string; cashAccount?: string; writeOffAccount?: string; diff --git a/models/types.ts b/models/types.ts index 2420b8feb..dcd2397d0 100644 --- a/models/types.ts +++ b/models/types.ts @@ -59,7 +59,12 @@ export enum ModelNameEnum { CustomForm = 'CustomForm', CustomField = 'CustomField', POSSettings = 'POSSettings', - POSShift = 'POSShift' + POSOpeningShift = 'POSOpeningShift', + POSClosingShift = 'POSClosingShift', + + ERPNextSyncSettings= 'ERPNextSyncSettings', + ERPNextSyncQueue = 'ERPNextSyncQueue', + FetchFromERPNextQueue = 'FetchFromERPNextQueue', } export type ModelName = keyof typeof ModelNameEnum; diff --git a/schemas/app/inventory/Point of Sale/POSClosingShift.json b/schemas/app/inventory/Point of Sale/POSClosingShift.json new file mode 100644 index 000000000..16ddce861 --- /dev/null +++ b/schemas/app/inventory/Point of Sale/POSClosingShift.json @@ -0,0 +1,24 @@ +{ + "name": "POSClosingShift", + "label": "POS Closing Shift", + "naming": "random", + "fields": [ + { + "fieldname": "closingDate", + "label": "Closing Date", + "fieldtype": "Datetime" + }, + { + "fieldname": "closingCash", + "fieldtype": "Table", + "label": "Closing Cash", + "target": "ClosingCash" + }, + { + "fieldname": "closingAmounts", + "fieldtype": "Table", + "label": "Closing Amounts", + "target": "ClosingAmounts" + } + ] +} diff --git a/schemas/app/inventory/Point of Sale/POSOpeningShift.json b/schemas/app/inventory/Point of Sale/POSOpeningShift.json new file mode 100644 index 000000000..e39279266 --- /dev/null +++ b/schemas/app/inventory/Point of Sale/POSOpeningShift.json @@ -0,0 +1,24 @@ +{ + "name": "POSOpeningShift", + "label": "POS Opening Shift", + "naming": "random", + "fields": [ + { + "fieldname": "openingDate", + "label": "Opening Date", + "fieldtype": "Datetime" + }, + { + "fieldname": "openingCash", + "fieldtype": "Table", + "label": "Opening Cash", + "target": "OpeningCash" + }, + { + "fieldname": "openingAmounts", + "fieldtype": "Table", + "label": "Opening Amounts", + "target": "OpeningAmounts" + } + ] +} diff --git a/schemas/app/inventory/Point of Sale/POSSettings.json b/schemas/app/inventory/Point of Sale/POSSettings.json index b3e3746ae..5f8d12d54 100644 --- a/schemas/app/inventory/Point of Sale/POSSettings.json +++ b/schemas/app/inventory/Point of Sale/POSSettings.json @@ -4,6 +4,13 @@ "isSingle": true, "isChild": false, "fields": [ + { + "fieldname": "isShiftOpen", + "label": "Is POS Shift Open", + "fieldtype": "Check", + "default": false, + "hidden": false + }, { "fieldname": "inventory", "label": "Inventory", diff --git a/schemas/app/inventory/Point of Sale/POSShift.json b/schemas/app/inventory/Point of Sale/POSShift.json deleted file mode 100644 index c17b834fe..000000000 --- a/schemas/app/inventory/Point of Sale/POSShift.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "name": "POSShift", - "isSingle": true, - "isChild": false, - "fields": [ - { - "fieldname": "isShiftOpen", - "label": "Is POS Shift Open", - "fieldtype": "Check", - "default": false - }, - { - "fieldname": "openingDate", - "label": "Opening Date", - "fieldtype": "Datetime" - }, - { - "fieldname": "closingDate", - "label": "Closing Date", - "fieldtype": "Datetime" - }, - { - "fieldname": "openingCash", - "fieldtype": "Table", - "target": "OpeningCash" - }, - { - "fieldname": "closingCash", - "fieldtype": "Table", - "target": "ClosingCash" - }, - { - "fieldname": "openingAmounts", - "fieldtype": "Table", - "target": "OpeningAmounts" - }, - { - "fieldname": "closingAmounts", - "fieldtype": "Table", - "target": "ClosingAmounts" - } - ] -} diff --git a/schemas/schemas.ts b/schemas/schemas.ts index f3d56c23c..fcb3b1ba0 100644 --- a/schemas/schemas.ts +++ b/schemas/schemas.ts @@ -70,7 +70,8 @@ import DefaultCashDenominations from './app/inventory/Point of Sale/DefaultCashD import OpeningAmounts from './app/inventory/Point of Sale/OpeningAmounts.json'; import OpeningCash from './app/inventory/Point of Sale/OpeningCash.json'; import POSSettings from './app/inventory/Point of Sale/POSSettings.json'; -import POSShift from './app/inventory/Point of Sale/POSShift.json'; +import POSOpeningShift from './app/inventory/Point of Sale/POSOpeningShift.json'; +import POSClosingShift from './app/inventory/Point of Sale/POSClosingShift.json'; import POSShiftAmounts from './app/inventory/Point of Sale/POSShiftAmounts.json'; import { Schema, SchemaStub } from './types'; @@ -170,6 +171,7 @@ export const appSchemas: Schema[] | SchemaStub[] = [ OpeningAmounts as Schema, OpeningCash as Schema, POSSettings as Schema, - POSShift as Schema, + POSOpeningShift as Schema, + POSClosingShift as Schema, POSShiftAmounts as Schema, ]; diff --git a/src/pages/POS/ClosePOSShiftModal.vue b/src/pages/POS/ClosePOSShiftModal.vue index 57b5f31e5..c97254eb8 100644 --- a/src/pages/POS/ClosePOSShiftModal.vue +++ b/src/pages/POS/ClosePOSShiftModal.vue @@ -13,9 +13,8 @@ :df="getField('closingCash')" :show-header="true" :border="true" - :value="posShiftDoc?.closingCash ?? []" + :value="posClosingShiftDoc?.closingCash ?? []" :read-only="false" - @row-change="handleChange" />

@@ -27,9 +26,8 @@ :df="getField('closingAmounts')" :show-header="true" :border="true" - :value="posShiftDoc?.closingAmounts" + :value="posClosingShiftDoc?.closingAmounts" :read-only="true" - @row-change="handleChange" />
@@ -65,7 +63,7 @@ import Table from 'src/components/Controls/Table.vue'; import { ModelNameEnum } from 'models/types'; import { Money } from 'pesa'; import { OpeningAmounts } from 'models/inventory/Point of Sale/OpeningAmounts'; -import { POSShift } from 'models/inventory/Point of Sale/POSShift'; +import { POSOpeningShift } from 'models/inventory/Point of Sale/POSOpeningShift'; import { computed } from 'vue'; import { defineComponent } from 'vue'; import { fyo } from 'src/initFyo'; @@ -74,14 +72,16 @@ import { t } from 'fyo'; import { validateClosingAmounts, transferPOSCashAndWriteOff, + getPOSOpeningShiftDoc, } from 'src/utils/pos'; +import { POSClosingShift } from 'models/inventory/Point of Sale/POSClosingShift'; export default defineComponent({ name: 'ClosePOSShiftModal', components: { Button, Modal, Table }, provide() { return { - doc: computed(() => this.posShiftDoc), + doc: computed(() => this.posClosingShiftDoc), }; }, props: { @@ -95,7 +95,8 @@ export default defineComponent({ return { isValuesSeeded: false, - posShiftDoc: undefined as POSShift | undefined, + posOpeningShiftDoc: undefined as POSOpeningShift | undefined, + posClosingShiftDoc: undefined as POSClosingShift | undefined, transactedAmount: {} as Record | undefined, }; }, @@ -108,16 +109,22 @@ export default defineComponent({ }, }, async activated() { - this.posShiftDoc = fyo.singles[ModelNameEnum.POSShift]; + this.posClosingShiftDoc = fyo.doc.getNewDoc( + ModelNameEnum.POSClosingShift + ) as POSClosingShift; await this.seedValues(); await this.setTransactedAmount(); }, + async updated() { + this.posOpeningShiftDoc = await getPOSOpeningShiftDoc(fyo); + await this.seedValues(); + }, methods: { async setTransactedAmount() { if (!fyo.singles.POSShift?.openingDate) { return; } - const fromDate = fyo.singles.POSShift?.openingDate; + const fromDate = this.posOpeningShiftDoc?.openingDate as Date; this.transactedAmount = await fyo.db.getPOSTransactedAmount( fromDate, new Date(), @@ -125,29 +132,28 @@ export default defineComponent({ ); }, seedClosingCash() { - if (!this.posShiftDoc) { + if (!this.posClosingShiftDoc) { return; } - this.posShiftDoc.closingCash = []; + this.posClosingShiftDoc.closingCash = []; - this.posShiftDoc?.openingCash?.map(async (row) => { - await this.posShiftDoc?.append('closingCash', { + this.posOpeningShiftDoc?.openingCash?.map(async (row) => { + await this.posClosingShiftDoc?.append('closingCash', { count: row.count, denomination: row.denomination as Money, }); }); }, async seedClosingAmounts() { - if (!this.posShiftDoc) { + if (!this.posClosingShiftDoc || !this.posOpeningShiftDoc) { return; } - this.posShiftDoc.closingAmounts = []; - await this.posShiftDoc.sync(); + this.posClosingShiftDoc.closingAmounts = []; - const openingAmounts = this.posShiftDoc - .openingAmounts as OpeningAmounts[]; + const openingAmounts = this.posOpeningShiftDoc + ?.openingAmounts as OpeningAmounts[]; for (const row of openingAmounts) { if (!row.paymentMethod) { @@ -158,13 +164,13 @@ export default defineComponent({ if (row.paymentMethod === 'Cash') { expectedAmount = expectedAmount.add( - this.posShiftDoc.openingCashAmount as Money + this.posOpeningShiftDoc?.openingCashAmount as Money ); } if (row.paymentMethod === 'Transfer') { expectedAmount = expectedAmount.add( - this.posShiftDoc.openingTransferAmount as Money + this.posOpeningShiftDoc?.openingTransferAmount as Money ); } @@ -174,14 +180,13 @@ export default defineComponent({ ); } - await this.posShiftDoc.append('closingAmounts', { + await this.posClosingShiftDoc.append('closingAmounts', { paymentMethod: row.paymentMethod, openingAmount: row.amount, closingAmount: fyo.pesa(0), expectedAmount: expectedAmount, differenceAmount: fyo.pesa(0), }); - await this.posShiftDoc.sync(); } }, async seedValues() { @@ -191,19 +196,20 @@ export default defineComponent({ this.isValuesSeeded = true; }, getField(fieldname: string) { - return fyo.getField(ModelNameEnum.POSShift, fieldname); - }, - async handleChange() { - await this.posShiftDoc?.sync(); + return fyo.getField(ModelNameEnum.POSClosingShift, fieldname); }, async handleSubmit() { try { - validateClosingAmounts(this.posShiftDoc as POSShift); - await this.posShiftDoc?.set('isShiftOpen', false); - await this.posShiftDoc?.set('closingDate', new Date()); - await this.posShiftDoc?.sync(); - await transferPOSCashAndWriteOff(fyo, this.posShiftDoc as POSShift); - + validateClosingAmounts(this.posClosingShiftDoc as POSClosingShift); + await this.posClosingShiftDoc?.set('isShiftOpen', false); + await this.posClosingShiftDoc?.set('closingDate', new Date()); + await this.posClosingShiftDoc?.sync(); + await transferPOSCashAndWriteOff( + fyo, + this.posClosingShiftDoc as POSClosingShift + ); + + await this.fyo.singles.POSSettings?.setAndSync('isShiftOpen', false); this.$emit('toggleModal', 'ShiftClose'); } catch (error) { return showToast({ diff --git a/src/pages/POS/OpenPOSShiftModal.vue b/src/pages/POS/OpenPOSShiftModal.vue index ffd901c74..36c7dce03 100644 --- a/src/pages/POS/OpenPOSShiftModal.vue +++ b/src/pages/POS/OpenPOSShiftModal.vue @@ -71,13 +71,14 @@ import Table from 'src/components/Controls/Table.vue'; import { AccountTypeEnum } from 'models/baseModels/Account/types'; import { ModelNameEnum } from 'models/types'; import { Money } from 'pesa'; -import { POSShift } from 'models/inventory/Point of Sale/POSShift'; +import { POSOpeningShift } from 'models/inventory/Point of Sale/POSOpeningShift'; import { computed } from 'vue'; import { defineComponent } from 'vue'; import { fyo } from 'src/initFyo'; import { showToast } from 'src/utils/interactive'; import { t } from 'fyo'; import { ValidationError } from 'fyo/utils/errors'; +import { getPOSOpeningShiftDoc } from 'src/utils/pos'; export default defineComponent({ name: 'OpenPOSShift', @@ -90,7 +91,7 @@ export default defineComponent({ emits: ['toggleModal'], data() { return { - posShiftDoc: undefined as POSShift | undefined, + posShiftDoc: undefined as POSOpeningShift | undefined, isValuesSeeded: false, }; @@ -108,7 +109,7 @@ export default defineComponent({ }, async mounted() { this.isValuesSeeded = false; - this.posShiftDoc = fyo.singles[ModelNameEnum.POSShift]; + this.posShiftDoc = await getPOSOpeningShiftDoc(fyo); await this.seedDefaults(); this.isValuesSeeded = true; @@ -120,8 +121,6 @@ export default defineComponent({ } this.posShiftDoc.openingCash = []; - await this.posShiftDoc.sync(); - const denominations = this.getDefaultCashDenominations; if (!denominations) { @@ -133,8 +132,6 @@ export default defineComponent({ denomination: row.denomination, count: 0, }); - - await this.posShiftDoc.sync(); } }, async seedPaymentMethods() { @@ -143,7 +140,6 @@ export default defineComponent({ } this.posShiftDoc.openingAmounts = []; - await this.posShiftDoc.sync(); await this.posShiftDoc.set('openingAmounts', [ { @@ -155,7 +151,6 @@ export default defineComponent({ amount: fyo.pesa(0), }, ]); - await this.posShiftDoc.sync(); }, async seedDefaults() { if (!!this.posShiftDoc?.isShiftOpen) { @@ -166,7 +161,7 @@ export default defineComponent({ await this.seedPaymentMethods(); }, getField(fieldname: string) { - return this.fyo.getField(ModelNameEnum.POSShift, fieldname); + return this.fyo.getField(ModelNameEnum.POSOpeningShift, fieldname); }, setOpeningCashAmount() { if (!this.posShiftDoc?.openingAmounts) { @@ -179,8 +174,7 @@ export default defineComponent({ } }); }, - async handleChange() { - await this.posShiftDoc?.sync(); + handleChange() { this.setOpeningCashAmount(); }, async handleSubmit() { @@ -197,6 +191,7 @@ export default defineComponent({ }); await this.posShiftDoc?.sync(); + await this.fyo.singles.POSSettings?.setAndSync('isShiftOpen', true); if (!this.posShiftDoc?.openingCashAmount.isZero()) { const jvDoc = fyo.doc.getNewDoc(ModelNameEnum.JournalEntry, { diff --git a/src/pages/POS/POS.vue b/src/pages/POS/POS.vue index 27e56f8b7..6ce191632 100644 --- a/src/pages/POS/POS.vue +++ b/src/pages/POS/POS.vue @@ -219,7 +219,7 @@ export default defineComponent({ isDiscountingEnabled(): boolean { return !!fyo.singles.AccountingSettings?.enableDiscounting; }, - isPosShiftOpen: () => !!fyo.singles.POSShift?.isShiftOpen, + isPosShiftOpen: () => !!fyo.singles.POSSettings?.isShiftOpen, disablePayButton(): boolean { if (!this.sinvDoc.items?.length || !this.sinvDoc.party) { return true; diff --git a/src/utils/pos.ts b/src/utils/pos.ts index 80e1e50ab..4c3018b42 100644 --- a/src/utils/pos.ts +++ b/src/utils/pos.ts @@ -4,13 +4,33 @@ import { AccountTypeEnum } from 'models/baseModels/Account/types'; import { Item } from 'models/baseModels/Item/Item'; import { SalesInvoice } from 'models/baseModels/SalesInvoice/SalesInvoice'; import { SalesInvoiceItem } from 'models/baseModels/SalesInvoiceItem/SalesInvoiceItem'; -import { POSShift } from 'models/inventory/Point of Sale/POSShift'; +import { POSOpeningShift } from 'models/inventory/Point of Sale/POSOpeningShift'; import { ModelNameEnum } from 'models/types'; import { Money } from 'pesa'; import { ItemQtyMap, ItemSerialNumbers } from 'src/components/POS/types'; import { fyo } from 'src/initFyo'; import { safeParseFloat } from 'utils/index'; import { showToast } from './interactive'; +import { POSClosingShift } from 'models/inventory/Point of Sale/POSClosingShift'; + +export async function getPOSOpeningShiftDoc( + fyo: Fyo +): Promise { + const existingShiftDoc = await fyo.db.getAll(ModelNameEnum.POSOpeningShift, { + limit: 1, + orderBy: 'created', + fields: ['name'], + }); + + if (!fyo.singles.POSSettings?.isShiftOpen || !existingShiftDoc) { + return fyo.doc.getNewDoc(ModelNameEnum.POSOpeningShift) as POSOpeningShift; + } + + return (await fyo.doc.getDoc( + ModelNameEnum.POSOpeningShift, + existingShiftDoc[0].name as string + )) as POSOpeningShift; +} export function getTotalQuantity(items: SalesInvoiceItem[]): number { let totalQuantity = safeParseFloat(0); @@ -156,7 +176,7 @@ export function getTotalTaxedAmount(sinvDoc: SalesInvoice): Money { return totalTaxedAmount; } -export function validateClosingAmounts(posShiftDoc: POSShift) { +export function validateClosingAmounts(posShiftDoc: POSClosingShift) { try { if (!posShiftDoc) { throw new ValidationError( @@ -176,7 +196,7 @@ export function validateClosingAmounts(posShiftDoc: POSShift) { export async function transferPOSCashAndWriteOff( fyo: Fyo, - posShiftDoc: POSShift + posShiftDoc: POSClosingShift ) { const expectedCashAmount = posShiftDoc.closingAmounts?.find( (row) => row.paymentMethod === 'Cash'