Skip to content

Commit 60e2345

Browse files
committed
fix : set outstanding to zero for return payments when invoice is fully paid
1 parent 6ea2e21 commit 60e2345

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

models/baseModels/Invoice/Invoice.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,19 +1047,16 @@ export abstract class Invoice extends Transactional {
10471047
if (sinvreturnedDoc.outstandingAmount?.isZero()) {
10481048
return this.grandTotal?.abs();
10491049
} else {
1050-
const totalPaid = sinvreturnedDoc.grandTotal
1051-
?.abs()
1052-
.sub(sinvreturnedDoc.outstandingAmount as Money);
1050+
const totalPaid = sinvreturnedDoc
1051+
.grandTotal!.abs()
1052+
.sub(sinvreturnedDoc.outstandingAmount!);
10531053

1054-
if (!sinvreturnedDoc.isReturn) {
1055-
return totalPaid?.abs();
1056-
}
1057-
1058-
if (!this.grandTotal?.isNegative()) {
1059-
this.grandTotal = this.grandTotal?.neg();
1060-
}
1054+
const returnAmount = this.grandTotal!.abs();
10611055

1062-
return this.grandTotal?.add(totalPaid as Money).abs();
1056+
const result = returnAmount.lte(totalPaid)
1057+
? returnAmount
1058+
: totalPaid;
1059+
return result;
10631060
}
10641061
}
10651062

@@ -1290,18 +1287,21 @@ export abstract class Invoice extends Transactional {
12901287
paymentType = PaymentTypeEnum.Receive;
12911288
}
12921289
}
1290+
const paymentAmount = this.isReturn
1291+
? outstandingAmount
1292+
: outstandingAmount?.abs();
12931293

12941294
const data = {
12951295
party: this.party,
12961296
date: new Date().toISOString(),
12971297
paymentType,
1298-
amount: this.outstandingAmount?.abs(),
1298+
amount: paymentAmount,
12991299
[accountField]: this.account,
13001300
for: [
13011301
{
13021302
referenceType: this.schemaName,
13031303
referenceName: this.name,
1304-
amount: this.outstandingAmount,
1304+
amount: this.isReturn ? this.grandTotal : outstandingAmount,
13051305
},
13061306
],
13071307
};
@@ -1313,7 +1313,6 @@ export abstract class Invoice extends Transactional {
13131313

13141314
return this.fyo.doc.getNewDoc(ModelNameEnum.Payment, data) as Payment;
13151315
}
1316-
13171316
async getStockTransfer(isAuto = false): Promise<StockTransfer | null> {
13181317
if (!this.isSubmitted) {
13191318
return null;

0 commit comments

Comments
 (0)