mirror of
https://github.com/frappe/books.git
synced 2024-11-15 09:54:04 +00:00
93 lines
2.7 KiB
TypeScript
93 lines
2.7 KiB
TypeScript
import { Fyo } from 'fyo';
|
|
import { Action, ListViewSettings } from 'fyo/model/types';
|
|
import { LedgerPosting } from 'models/Transactional/LedgerPosting';
|
|
import { ModelNameEnum } from 'models/types';
|
|
import { getAddedLPWithGrandTotal, getInvoiceActions, getTransactionStatusColumn } from '../../helpers';
|
|
import { Invoice } from '../Invoice/Invoice';
|
|
import { SalesInvoiceItem } from '../SalesInvoiceItem/SalesInvoiceItem';
|
|
import { LoyaltyProgram } from '../LoyaltyProgram/LoyaltyProgram';
|
|
|
|
export class SalesInvoice extends Invoice {
|
|
items?: SalesInvoiceItem[];
|
|
|
|
async getPosting() {
|
|
const exchangeRate = this.exchangeRate ?? 1;
|
|
const posting: LedgerPosting = new LedgerPosting(this, this.fyo);
|
|
if (this.isReturn) {
|
|
await posting.credit(this.account!, this.baseGrandTotal!);
|
|
} else {
|
|
await posting.debit(this.account!, this.baseGrandTotal!);
|
|
}
|
|
|
|
for (const item of this.items!) {
|
|
if (this.isReturn) {
|
|
await posting.debit(item.account!, item.amount!.mul(exchangeRate));
|
|
continue;
|
|
}
|
|
await posting.credit(item.account!, item.amount!.mul(exchangeRate));
|
|
}
|
|
|
|
if (this.redeemLoyaltyPoints) {
|
|
const loyaltyProgramDoc = (await this.fyo.doc.getDoc(
|
|
ModelNameEnum.LoyaltyProgram,
|
|
this.loyaltyProgram
|
|
)) as LoyaltyProgram;
|
|
|
|
const totalAmount = await getAddedLPWithGrandTotal(
|
|
this.fyo,
|
|
this.loyaltyProgram as string,
|
|
this.loyaltyPoints as number
|
|
);
|
|
|
|
await posting.debit(
|
|
loyaltyProgramDoc.expenseAccount as string,
|
|
totalAmount
|
|
);
|
|
|
|
await posting.credit(this.account!, totalAmount);
|
|
}
|
|
|
|
|
|
if (this.taxes) {
|
|
for (const tax of this.taxes) {
|
|
if (this.isReturn) {
|
|
await posting.debit(tax.account!, tax.amount!.mul(exchangeRate));
|
|
continue;
|
|
}
|
|
await posting.credit(tax.account!, tax.amount!.mul(exchangeRate));
|
|
}
|
|
}
|
|
|
|
const discountAmount = this.getTotalDiscount();
|
|
const discountAccount = this.fyo.singles.AccountingSettings
|
|
?.discountAccount as string | undefined;
|
|
if (discountAccount && discountAmount.isPositive()) {
|
|
if (this.isReturn) {
|
|
await posting.credit(discountAccount, discountAmount.mul(exchangeRate));
|
|
} else {
|
|
await posting.debit(discountAccount, discountAmount.mul(exchangeRate));
|
|
}
|
|
}
|
|
|
|
await posting.makeRoundOffEntry();
|
|
return posting;
|
|
}
|
|
|
|
static getListViewSettings(): ListViewSettings {
|
|
return {
|
|
columns: [
|
|
'name',
|
|
getTransactionStatusColumn(),
|
|
'party',
|
|
'date',
|
|
'baseGrandTotal',
|
|
'outstandingAmount',
|
|
],
|
|
};
|
|
}
|
|
|
|
static getActions(fyo: Fyo): Action[] {
|
|
return getInvoiceActions(fyo, ModelNameEnum.SalesInvoice);
|
|
}
|
|
}
|