fix : correct calculation logic for closing shift amount

This commit is contained in:
Gadha2311
2025-08-28 14:26:14 +05:30
parent 3b055cb0c4
commit efd922ffb6
+43 -20
View File
@@ -10,7 +10,6 @@ import DatabaseCore from './core';
import { BespokeFunction } from './types'; import { BespokeFunction } from './types';
import { DocItem, ReturnDocItem } from 'models/inventory/types'; import { DocItem, ReturnDocItem } from 'models/inventory/types';
import { safeParseFloat } from 'utils/index'; import { safeParseFloat } from 'utils/index';
import { Money } from 'pesa';
export class BespokeQueries { export class BespokeQueries {
[key: string]: BespokeFunction; [key: string]: BespokeFunction;
@@ -415,48 +414,72 @@ export class BespokeQueries {
fromDate: Date, fromDate: Date,
toDate: Date, toDate: Date,
lastShiftClosingDate?: Date lastShiftClosingDate?: Date
): Promise<Record<string, Money> | undefined> { ): Promise<Record<string, number> | undefined> {
const sinvNamesQuery = db.knex!(ModelNameEnum.SalesInvoice) const invoicesQuery = db.knex!(ModelNameEnum.SalesInvoice)
.select('name') .select('name', 'returnAgainst')
.where('isPOS', true) .where('isPOS', true)
.andWhereBetween('date', [fromDate.toISOString(), toDate.toISOString()]); .andWhereBetween('date', [fromDate.toISOString(), toDate.toISOString()]);
if (lastShiftClosingDate) { if (lastShiftClosingDate) {
sinvNamesQuery.andWhere( invoicesQuery.andWhere(
'created', 'created',
'>', '>',
lastShiftClosingDate.toISOString() lastShiftClosingDate.toISOString()
); );
} }
const sinvNames = (await sinvNamesQuery).map( const invoices = (await invoicesQuery) as {
(row: { name: string }) => row.name name: string;
); returnAgainst: string | null;
}[];
if (!sinvNames.length) { if (!invoices.length) {
return; return;
} }
const sinvNames = invoices.map((row) => row.name);
const invoiceSignMap = invoices.reduce<Record<string, number>>(
(map, inv) => {
map[inv.name] = inv.returnAgainst ? -1 : 1;
return map;
},
{}
);
const paymentEntryNames: string[] = ( const paymentEntryNames: string[] = (
await db.knex!(ModelNameEnum.PaymentFor) await db.knex!(ModelNameEnum.PaymentFor)
.select('parent') .select('parent', 'referenceName')
.whereIn('referenceName', sinvNames) .whereIn('referenceName', sinvNames)
).map((doc: { parent: string }) => doc.parent); ).map((doc: { parent: string }) => doc.parent);
const groupedAmounts = (await db.knex!(ModelNameEnum.Payment) if (!paymentEntryNames.length) {
.select('paymentMethod')
.whereIn('name', paymentEntryNames)
.groupBy('paymentMethod')
.sum({ amount: 'amount' })) as { paymentMethod: string; amount: Money }[];
const transactedAmounts = {} as { [paymentMethod: string]: Money };
if (!groupedAmounts) {
return; return;
} }
const groupedAmounts = (await db.knex!(ModelNameEnum.Payment)
.select('paymentMethod', 'name')
.whereIn('name', paymentEntryNames)
.groupBy('paymentMethod', 'name')
.sum({ amount: 'amount' })) as {
paymentMethod: string;
name: string;
amount: number;
}[];
const transactedAmounts: Record<string, number> = {};
for (const row of groupedAmounts) { for (const row of groupedAmounts) {
transactedAmounts[row.paymentMethod] = row.amount; const paymentRefs = (await db.knex!(ModelNameEnum.PaymentFor)
.select('referenceName')
.where('parent', row.name)) as { referenceName: string }[];
for (const ref of paymentRefs) {
const sign = invoiceSignMap[ref.referenceName] ?? 1;
const signedAmount = Number(row.amount) * sign;
transactedAmounts[row.paymentMethod] =
(transactedAmounts[row.paymentMethod] ?? 0) + signedAmount;
}
} }
return transactedAmounts; return transactedAmounts;