diff --git a/backend/database/bespoke.ts b/backend/database/bespoke.ts index d8f65ba7..c5ab61b8 100644 --- a/backend/database/bespoke.ts +++ b/backend/database/bespoke.ts @@ -396,17 +396,28 @@ export class BespokeQueries { static async getPOSTransactedAmount( db: DatabaseCore, fromDate: Date, - toDate: Date + toDate: Date, + lastShiftClosingDate?: Date ): Promise | undefined> { - const sinvNames = ( - await db.knex!(ModelNameEnum.SalesInvoice) - .select('name') - .where('isPOS', true) - .andWhereBetween('date', [ - DateTime.fromJSDate(fromDate).toISODate(), - DateTime.fromJSDate(toDate).toISODate(), - ]) - ).map((row: { name: string }) => row.name); + const sinvNamesQuery = db.knex!(ModelNameEnum.SalesInvoice) + .select('name') + .where('isPOS', true) + .andWhereBetween('date', [ + DateTime.fromJSDate(fromDate).toSQLDate(), + DateTime.fromJSDate(toDate).toSQLDate(), + ]); + + if (lastShiftClosingDate) { + sinvNamesQuery.andWhere( + 'created', + '>', + DateTime.fromJSDate(lastShiftClosingDate).toUTC().toString() + ); + } + + const sinvNames = (await sinvNamesQuery).map( + (row: { name: string }) => row.name + ); if (!sinvNames.length) { return; diff --git a/fyo/core/dbHandler.ts b/fyo/core/dbHandler.ts index 509f350d..35847bc6 100644 --- a/fyo/core/dbHandler.ts +++ b/fyo/core/dbHandler.ts @@ -345,12 +345,14 @@ export class DatabaseHandler extends DatabaseBase { async getPOSTransactedAmount( fromDate: Date, - toDate: Date + toDate: Date, + lastShiftClosingDate?: Date ): Promise | undefined> { return (await this.#demux.callBespoke( 'getPOSTransactedAmount', fromDate, - toDate + toDate, + lastShiftClosingDate )) as Promise | undefined>; } diff --git a/models/inventory/InventorySettings.ts b/models/inventory/InventorySettings.ts index d2d1439d..8cb64bce 100644 --- a/models/inventory/InventorySettings.ts +++ b/models/inventory/InventorySettings.ts @@ -46,10 +46,7 @@ export class InventorySettings extends Doc { return !!this.enableStockReturns; }, enablePointOfSale: () => { - return ( - !!this.fyo.singles.POSShift?.isShiftOpen && - !!this.fyo.singles.AccountingSettings?.enableDiscounting - ); + return !!this.fyo.singles.POSShift?.isShiftOpen; }, }; } diff --git a/src/pages/POS/ClosePOSShiftModal.vue b/src/pages/POS/ClosePOSShiftModal.vue index 28078dea..75dfd334 100644 --- a/src/pages/POS/ClosePOSShiftModal.vue +++ b/src/pages/POS/ClosePOSShiftModal.vue @@ -111,7 +111,8 @@ export default defineComponent({ const fromDate = fyo.singles.POSShift?.openingDate; this.transactedAmount = await fyo.db.getPOSTransactedAmount( fromDate, - new Date() + new Date(), + fyo.singles.POSShift.closingDate as Date ); }, seedClosingCash() { @@ -190,6 +191,7 @@ export default defineComponent({ 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); diff --git a/src/utils/pos.ts b/src/utils/pos.ts index ad5d8308..25047527 100644 --- a/src/utils/pos.ts +++ b/src/utils/pos.ts @@ -211,11 +211,11 @@ export async function transferPOSCashAndWriteOff( fyo: Fyo, posShiftDoc: POSShift ) { - const differenceAmount = posShiftDoc?.closingAmounts?.find( + const expectedCashAmount = posShiftDoc.closingAmounts?.find( (row) => row.paymentMethod === 'Cash' - )?.differenceAmount as Money; + )?.expectedAmount as Money; - if (differenceAmount.isZero()) { + if (expectedCashAmount.isZero()) { return; } @@ -237,6 +237,10 @@ export async function transferPOSCashAndWriteOff( credit: closingCashAmount, }); + const differenceAmount = posShiftDoc?.closingAmounts?.find( + (row) => row.paymentMethod === 'Cash' + )?.differenceAmount as Money; + if (differenceAmount.isNegative()) { await jvDoc.append('accounts', { account: AccountTypeEnum.Cash,