Skip to content

Commit

Permalink
fix: expectedAmount calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
akshayitzme committed Dec 4, 2023
1 parent a769537 commit 7ed2a08
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
31 changes: 21 additions & 10 deletions backend/database/bespoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,17 +396,28 @@ export class BespokeQueries {
static async getPOSTransactedAmount(
db: DatabaseCore,
fromDate: Date,
toDate: Date
toDate: Date,
lastShiftClosingDate?: Date
): Promise<Record<string, Money> | 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;
Expand Down
6 changes: 4 additions & 2 deletions fyo/core/dbHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,14 @@ export class DatabaseHandler extends DatabaseBase {

async getPOSTransactedAmount(
fromDate: Date,
toDate: Date
toDate: Date,
lastShiftClosingDate?: Date
): Promise<Record<string, Money> | undefined> {
return (await this.#demux.callBespoke(
'getPOSTransactedAmount',
fromDate,
toDate
toDate,
lastShiftClosingDate
)) as Promise<Record<string, Money> | undefined>;
}

Expand Down
5 changes: 1 addition & 4 deletions models/inventory/InventorySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
};
}
4 changes: 3 additions & 1 deletion src/pages/POS/ClosePOSShiftModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/utils/pos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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,
Expand Down

0 comments on commit 7ed2a08

Please sign in to comment.