2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 19:39:07 +00:00
books/models/baseModels/PurchaseInvoice/PurchaseInvoice.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

71 lines
2.1 KiB
TypeScript
Raw Normal View History

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