Skip to content

Commit

Permalink
fix: unable to pay multiple invoices
Browse files Browse the repository at this point in the history
  • Loading branch information
18alantom committed Oct 7, 2022
1 parent 7c03b98 commit 5f937b0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
24 changes: 15 additions & 9 deletions models/baseModels/Payment/Payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ export class Payment extends Transactional {
?.label!} does not exist`
);
}
console.log(refDoc);

if (!refDoc) {
continue;
Expand Down Expand Up @@ -259,8 +258,9 @@ export class Payment extends Transactional {

for (const row of forReferences) {
this.validateReferenceType(row);
await this.validateReferenceOutstanding(row);
}

await this.validateReferenceOutstanding();
}

validateReferenceType(row: PaymentFor) {
Expand All @@ -274,13 +274,19 @@ export class Payment extends Transactional {
}
}

async validateReferenceOutstanding(row: PaymentFor) {
const referenceDoc = await this.fyo.doc.getDoc(
row.referenceType as string,
row.referenceName as string
);
async validateReferenceOutstanding() {
let outstandingAmount = this.fyo.pesa(0);
for (const row of this.for ?? []) {
const referenceDoc = (await this.fyo.doc.getDoc(
row.referenceType as string,
row.referenceName as string
)) as Invoice;

outstandingAmount = outstandingAmount.add(
referenceDoc.outstandingAmount ?? 0
);
}

const outstandingAmount = referenceDoc.outstandingAmount as Money;
const amount = this.amount as Money;

if (amount.gt(0) && amount.lte(outstandingAmount)) {
Expand Down Expand Up @@ -317,7 +323,7 @@ export class Payment extends Transactional {
);

const previousOutstandingAmount = referenceDoc.outstandingAmount as Money;
const outstandingAmount = previousOutstandingAmount.sub(this.amount!);
const outstandingAmount = previousOutstandingAmount.sub(row.amount!);
await referenceDoc.setAndSync({ outstandingAmount });
}
}
Expand Down
1 change: 0 additions & 1 deletion models/baseModels/PaymentFor/PaymentFor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export class PaymentFor extends Doc {

validations: ValidationMap = {
referenceName: async (value: DocValue) => {
console.log(value);
const exists = await this.fyo.db.exists(
this.referenceType!,
value as string
Expand Down

0 comments on commit 5f937b0

Please sign in to comment.