Merge pull request #1469 from Gadha2311/return-quantity

fix: return batch quantity resetting
This commit is contained in:
Able k Saju
2026-02-26 12:25:53 +05:30
committed by GitHub
3 changed files with 36 additions and 20 deletions
+33 -6
View File
@@ -199,6 +199,23 @@ export abstract class Invoice extends Transactional {
if (this.isQuote) { if (this.isQuote) {
return; return;
} }
if (!this.submitted && this.loyaltyProgram) {
const isExpiredOrMaxed = await isLoyaltyProgramExpiredAndMaxed(
this.fyo,
this.loyaltyProgram
);
if (isExpiredOrMaxed) {
const { showToast } = await import('src/utils/interactive');
showToast({
type: 'warning',
message: t`Loyalty program has expired or reached maximum usage`,
duration: 'short',
});
}
}
if ( if (
this.enableDiscounting && this.enableDiscounting &&
!this.fyo.singles?.AccountingSettings?.discountAccount !this.fyo.singles?.AccountingSettings?.discountAccount
@@ -759,17 +776,26 @@ export abstract class Invoice extends Transactional {
if (item.batch) { if (item.batch) {
const returnData = totalQtyOfReturnedItems[item.item as string]; const returnData = totalQtyOfReturnedItems[item.item as string];
if (typeof returnData === 'object' && returnData?.batches) { if (typeof returnData === 'object' && returnData?.batches) {
returnDocItems = docItems.map((docItem) => ({ returnDocItems = docItems.map((docItem: DocValueMap) => {
...docItem, const qty = -returnData?.batches![docItem.batch as string] || 0;
name: undefined, const transferQty =
quantity: -returnData?.batches![docItem.batch as string] || 0, qty / ((docItem.unitConversionFactor as number) || 1);
})); return {
...docItem,
name: undefined,
quantity: qty,
transferQuantity: transferQty,
};
});
} }
} else { } else {
returnDocItems = docItems.map((docItem) => ({ returnDocItems = docItems.map((docItem: DocValueMap) => ({
...docItem, ...docItem,
name: undefined, name: undefined,
quantity: -(totalQtyOfReturnedItems[docItem.item as string] || 0), quantity: -(totalQtyOfReturnedItems[docItem.item as string] || 0),
qty:
-(totalQtyOfReturnedItems[docItem.item as string] as number) /
(item.unitConversionFactor as number),
transferQuantity: -( transferQuantity: -(
(totalQtyOfReturnedItems[docItem.item as string] as number) / (totalQtyOfReturnedItems[docItem.item as string] as number) /
(item.unitConversionFactor as number) (item.unitConversionFactor as number)
@@ -835,6 +861,7 @@ export abstract class Invoice extends Transactional {
serialNumber, serialNumber,
name: undefined, name: undefined,
quantity: quantity, quantity: quantity,
qty: transferQuantity,
transferQuantity, transferQuantity,
}); });
} }
@@ -118,9 +118,7 @@ export class SalesInvoice extends Invoice {
today.setHours(0, 0, 0, 0); today.setHours(0, 0, 0, 0);
if (toDate && new Date(toDate).getTime() < today.getTime()) { if (toDate && new Date(toDate).getTime() < today.getTime()) {
throw new ValidationError( return;
t`Loyalty program has expired and cannot be applied`
);
} }
if (!this?.grandTotal) { if (!this?.grandTotal) {
+2 -11
View File
@@ -1642,20 +1642,12 @@ export async function validateLoyaltyProgram(
filters: { name: loyaltyProgramName }, filters: { name: loyaltyProgramName },
}); });
if (!loyaltyProgram[0]?.isEnabled) {
throw new ValidationError(
'Loyalty program cannot be applied as it is not enabled'
);
}
if ( if (
(loyaltyProgram[0]?.maximumUse as number) > 0 && (loyaltyProgram[0]?.maximumUse as number) > 0 &&
(loyaltyProgram[0]?.used as number) >= (loyaltyProgram[0]?.used as number) >=
(loyaltyProgram[0]?.maximumUse as number) (loyaltyProgram[0]?.maximumUse as number)
) { ) {
throw new ValidationError( return;
'Loyalty program has reached maximum usage limit'
);
} }
if ( if (
@@ -1672,9 +1664,8 @@ export async function validateLoyaltyProgram(
const normalizedToDate = new Date(toDate); const normalizedToDate = new Date(toDate);
normalizedToDate.setHours(0, 0, 0, 0); normalizedToDate.setHours(0, 0, 0, 0);
// Only throw error if toDate is clearly in the past
if (normalizedToDate.getTime() < today.getTime()) { if (normalizedToDate.getTime() < today.getTime()) {
throw new ValidationError('Loyalty program has expired'); return;
} }
} }
} }