Skip to content

Commit

Permalink
fix: payment account
Browse files Browse the repository at this point in the history
  • Loading branch information
akshayitzme committed Jul 5, 2023
1 parent 8160219 commit ee655b4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
27 changes: 19 additions & 8 deletions models/baseModels/Invoice/Invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Payment } from '../Payment/Payment';
import { Tax } from '../Tax/Tax';
import { TaxSummary } from '../TaxSummary/TaxSummary';
import { isPesa } from 'fyo/utils';
import { AccountFieldEnum, PaymentTypeEnum } from '../Payment/types';

export abstract class Invoice extends Transactional {
_taxes: Record<string, Tax> = {};
Expand Down Expand Up @@ -732,18 +733,28 @@ export abstract class Invoice extends Transactional {
return null;
}

const accountField =
this.isSales && !this.outstandingAmount?.isNegative()
? 'account'
: 'paymentAccount';
let accountField: AccountFieldEnum = AccountFieldEnum.Account;
let paymentType: PaymentTypeEnum = PaymentTypeEnum.Receive;

if (this.isSales && this.isItemsReturned) {
accountField = AccountFieldEnum.PaymentAccount;
paymentType = PaymentTypeEnum.Pay;
}

if (!this.isSales) {
accountField = AccountFieldEnum.PaymentAccount;
paymentType = PaymentTypeEnum.Pay;

if (this.isItemsReturned) {
accountField = AccountFieldEnum.Account;
paymentType = PaymentTypeEnum.Receive;
}
}

const data = {
party: this.party,
date: new Date().toISOString().slice(0, 10),
paymentType:
this.isSales && !this.outstandingAmount?.isNegative()
? 'Receive'
: 'Pay',
paymentType,
amount: this.outstandingAmount?.abs(),
[accountField]: this.account,
for: [
Expand Down
9 changes: 9 additions & 0 deletions models/baseModels/Payment/Payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,15 @@ export class Payment extends Transactional {
'referenceName'
)) as Invoice | null;

if (
refDoc &&
refDoc.schema.name === ModelNameEnum.SalesInvoice &&
refDoc.isItemsReturned
) {
const accountsMap = await this._getAccountsMap();
return accountsMap[AccountTypeEnum.Cash]?.[0];
}

return refDoc?.account ?? null;
}

Expand Down
10 changes: 10 additions & 0 deletions models/baseModels/Payment/types.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
export type PaymentType = 'Receive' | 'Pay';
export type PaymentMethod = 'Cash' | 'Cheque' | 'Transfer';

export enum PaymentTypeEnum{
Receive = 'Receive',
Pay = 'Pay'
}

export enum AccountFieldEnum{
Account = 'account',
PaymentAccount = 'paymentAccount'
}

0 comments on commit ee655b4

Please sign in to comment.