2022-12-01 14:01:23 +05:30
|
|
|
import { Fyo } from 'fyo';
|
|
|
|
import { Action, ListViewSettings } from 'fyo/model/types';
|
2022-05-03 18:43:47 +05:30
|
|
|
import { LedgerPosting } from 'models/Transactional/LedgerPosting';
|
2022-12-01 14:01:23 +05:30
|
|
|
import { ModelNameEnum } from 'models/types';
|
|
|
|
import { getInvoiceActions, getTransactionStatusColumn } from '../../helpers';
|
2022-04-14 14:52:45 +05:30
|
|
|
import { Invoice } from '../Invoice/Invoice';
|
|
|
|
import { PurchaseInvoiceItem } from '../PurchaseInvoiceItem/PurchaseInvoiceItem';
|
|
|
|
|
|
|
|
export class PurchaseInvoice extends Invoice {
|
|
|
|
items?: PurchaseInvoiceItem[];
|
|
|
|
|
|
|
|
async getPosting() {
|
2022-09-30 17:12:15 +05:30
|
|
|
const exchangeRate = this.exchangeRate ?? 1;
|
2022-05-03 18:43:47 +05:30
|
|
|
const posting: LedgerPosting = new LedgerPosting(this, this.fyo);
|
2023-09-23 16:45:22 +05:30
|
|
|
if (this.isReturn) {
|
|
|
|
await posting.debit(this.account!, this.baseGrandTotal!);
|
|
|
|
} else {
|
|
|
|
await posting.credit(this.account!, this.baseGrandTotal!);
|
|
|
|
}
|
2022-04-14 14:52:45 +05:30
|
|
|
|
|
|
|
for (const item of this.items!) {
|
2023-09-23 16:45:22 +05:30
|
|
|
if (this.isReturn) {
|
|
|
|
await posting.credit(item.account!, item.amount!.mul(exchangeRate));
|
|
|
|
continue;
|
|
|
|
}
|
2022-09-30 17:12:15 +05:30
|
|
|
await posting.debit(item.account!, item.amount!.mul(exchangeRate));
|
2022-04-14 14:52:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (this.taxes) {
|
|
|
|
for (const tax of this.taxes) {
|
2023-09-23 16:45:22 +05:30
|
|
|
if (this.isReturn) {
|
|
|
|
await posting.credit(tax.account!, tax.amount!.mul(exchangeRate));
|
|
|
|
continue;
|
|
|
|
}
|
2022-09-30 17:12:15 +05:30
|
|
|
await posting.debit(tax.account!, tax.amount!.mul(exchangeRate));
|
2022-04-14 14:52:45 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 14:22:54 +05:30
|
|
|
const discountAmount = this.getTotalDiscount();
|
2022-07-15 13:05:07 +05:30
|
|
|
const discountAccount = this.fyo.singles.AccountingSettings
|
|
|
|
?.discountAccount as string | undefined;
|
|
|
|
if (discountAccount && discountAmount.isPositive()) {
|
2023-09-23 16:45:22 +05:30
|
|
|
if (this.isReturn) {
|
|
|
|
await posting.debit(discountAccount, discountAmount.mul(exchangeRate));
|
|
|
|
} else {
|
|
|
|
await posting.credit(discountAccount, discountAmount.mul(exchangeRate));
|
|
|
|
}
|
2022-07-15 13:05:07 +05:30
|
|
|
}
|
|
|
|
|
2022-05-03 18:43:47 +05:30
|
|
|
await posting.makeRoundOffEntry();
|
|
|
|
return posting;
|
2022-04-14 14:52:45 +05:30
|
|
|
}
|
|
|
|
|
2022-04-28 18:07:07 +05:30
|
|
|
static getListViewSettings(): ListViewSettings {
|
2022-04-18 16:59:20 +05:30
|
|
|
return {
|
|
|
|
columns: [
|
|
|
|
'name',
|
2022-04-28 18:07:07 +05:30
|
|
|
getTransactionStatusColumn(),
|
2022-05-12 11:22:25 +05:30
|
|
|
'party',
|
2022-04-18 16:59:20 +05:30
|
|
|
'date',
|
2022-10-03 13:48:02 +05:30
|
|
|
'baseGrandTotal',
|
2022-04-18 16:59:20 +05:30
|
|
|
'outstandingAmount',
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
2022-12-01 14:01:23 +05:30
|
|
|
|
|
|
|
static getActions(fyo: Fyo): Action[] {
|
|
|
|
return getInvoiceActions(fyo, ModelNameEnum.PurchaseInvoice);
|
|
|
|
}
|
2022-04-14 14:52:45 +05:30
|
|
|
}
|