2023-02-15 09:09:56 +00:00
|
|
|
import { Fyo, t } from 'fyo';
|
2022-09-29 11:04:35 +00:00
|
|
|
import { DocValue, DocValueMap } from 'fyo/core/types';
|
2022-04-24 06:48:44 +00:00
|
|
|
import { Doc } from 'fyo/model/doc';
|
2022-07-12 11:41:24 +00:00
|
|
|
import {
|
2022-09-29 11:04:35 +00:00
|
|
|
CurrenciesMap,
|
2022-07-12 11:41:24 +00:00
|
|
|
FiltersMap,
|
|
|
|
FormulaMap,
|
|
|
|
HiddenMap,
|
2022-11-22 09:12:49 +00:00
|
|
|
ValidationMap,
|
2022-07-12 11:41:24 +00:00
|
|
|
} from 'fyo/model/types';
|
2022-09-29 11:04:35 +00:00
|
|
|
import { DEFAULT_CURRENCY } from 'fyo/utils/consts';
|
2022-04-19 05:59:36 +00:00
|
|
|
import { ValidationError } from 'fyo/utils/errors';
|
2022-07-08 17:51:21 +00:00
|
|
|
import { ModelNameEnum } from 'models/types';
|
2022-05-23 05:30:54 +00:00
|
|
|
import { Money } from 'pesa';
|
2022-09-29 11:04:35 +00:00
|
|
|
import { FieldTypeEnum, Schema } from 'schemas/types';
|
2023-02-15 09:09:56 +00:00
|
|
|
import { safeParseFloat } from 'utils/index';
|
2022-04-14 09:22:45 +00:00
|
|
|
import { Invoice } from '../Invoice/Invoice';
|
2022-11-22 09:12:49 +00:00
|
|
|
import { Item } from '../Item/Item';
|
2022-04-14 09:22:45 +00:00
|
|
|
|
|
|
|
export abstract class InvoiceItem extends Doc {
|
2022-11-22 09:12:49 +00:00
|
|
|
item?: string;
|
2022-04-14 09:22:45 +00:00
|
|
|
account?: string;
|
2022-05-11 10:44:31 +00:00
|
|
|
amount?: Money;
|
2022-04-14 09:22:45 +00:00
|
|
|
parentdoc?: Invoice;
|
2022-07-12 10:23:41 +00:00
|
|
|
rate?: Money;
|
2023-02-21 09:18:55 +00:00
|
|
|
|
|
|
|
unit?: string;
|
|
|
|
transferUnit?: string;
|
2022-07-12 10:23:41 +00:00
|
|
|
quantity?: number;
|
2023-02-21 09:18:55 +00:00
|
|
|
transferQuantity?: number;
|
|
|
|
unitConversionFactor?: number;
|
|
|
|
|
2022-07-12 10:23:41 +00:00
|
|
|
tax?: string;
|
2022-11-22 09:12:49 +00:00
|
|
|
stockNotTransferred?: number;
|
2022-07-13 17:48:20 +00:00
|
|
|
|
|
|
|
setItemDiscountAmount?: boolean;
|
|
|
|
itemDiscountAmount?: Money;
|
|
|
|
itemDiscountPercent?: number;
|
2022-07-12 11:41:24 +00:00
|
|
|
itemDiscountedTotal?: Money;
|
2022-07-13 17:48:20 +00:00
|
|
|
itemTaxedTotal?: Money;
|
2022-04-14 09:22:45 +00:00
|
|
|
|
|
|
|
get isSales() {
|
|
|
|
return this.schemaName === 'SalesInvoiceItem';
|
|
|
|
}
|
|
|
|
|
2022-07-12 10:23:41 +00:00
|
|
|
get discountAfterTax() {
|
|
|
|
return !!this?.parentdoc?.discountAfterTax;
|
|
|
|
}
|
|
|
|
|
2022-07-13 17:48:20 +00:00
|
|
|
get enableDiscounting() {
|
|
|
|
return !!this.fyo.singles?.AccountingSettings?.enableDiscounting;
|
|
|
|
}
|
|
|
|
|
2023-02-21 09:18:55 +00:00
|
|
|
get enableInventory() {
|
|
|
|
return !!this.fyo.singles?.AccountingSettings?.enableInventory;
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:04:35 +00:00
|
|
|
get currency() {
|
|
|
|
return this.parentdoc?.currency ?? DEFAULT_CURRENCY;
|
|
|
|
}
|
|
|
|
|
|
|
|
get exchangeRate() {
|
|
|
|
return this.parentdoc?.exchangeRate ?? 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isMultiCurrency() {
|
|
|
|
return this.parentdoc?.isMultiCurrency ?? false;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(schema: Schema, data: DocValueMap, fyo: Fyo) {
|
|
|
|
super(schema, data, fyo);
|
|
|
|
this._setGetCurrencies();
|
|
|
|
}
|
|
|
|
|
2022-07-12 10:23:41 +00:00
|
|
|
async getTotalTaxRate(): Promise<number> {
|
|
|
|
if (!this.tax) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const details =
|
|
|
|
((await this.fyo.getValue('Tax', this.tax, 'details')) as Doc[]) ?? [];
|
|
|
|
return details.reduce((acc, doc) => {
|
|
|
|
return (doc.rate as number) + acc;
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
2022-04-14 09:22:45 +00:00
|
|
|
formulas: FormulaMap = {
|
2022-04-29 13:00:24 +00:00
|
|
|
description: {
|
2022-05-02 10:15:16 +00:00
|
|
|
formula: async () =>
|
|
|
|
(await this.fyo.getValue(
|
2022-04-29 13:00:24 +00:00
|
|
|
'Item',
|
|
|
|
this.item as string,
|
|
|
|
'description'
|
2022-05-02 10:15:16 +00:00
|
|
|
)) as string,
|
2022-04-29 13:00:24 +00:00
|
|
|
dependsOn: ['item'],
|
|
|
|
},
|
2022-07-12 11:41:24 +00:00
|
|
|
rate: {
|
|
|
|
formula: async (fieldname) => {
|
2022-07-14 09:28:26 +00:00
|
|
|
const rate = (await this.fyo.getValue(
|
2022-07-12 11:41:24 +00:00
|
|
|
'Item',
|
|
|
|
this.item as string,
|
|
|
|
'rate'
|
|
|
|
)) as undefined | Money;
|
2022-07-14 09:28:26 +00:00
|
|
|
|
2022-10-11 11:26:04 +00:00
|
|
|
if (!rate?.float && this.rate?.float) {
|
|
|
|
return this.rate;
|
|
|
|
}
|
|
|
|
|
2022-07-12 11:41:24 +00:00
|
|
|
if (
|
|
|
|
fieldname !== 'itemTaxedTotal' &&
|
|
|
|
fieldname !== 'itemDiscountedTotal'
|
|
|
|
) {
|
2022-09-30 11:42:15 +00:00
|
|
|
return rate?.div(this.exchangeRate) ?? this.fyo.pesa(0);
|
2022-07-12 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const quantity = this.quantity ?? 0;
|
2022-07-14 09:28:26 +00:00
|
|
|
const itemDiscountPercent = this.itemDiscountPercent ?? 0;
|
|
|
|
const itemDiscountAmount = this.itemDiscountAmount ?? this.fyo.pesa(0);
|
2022-07-12 11:41:24 +00:00
|
|
|
const totalTaxRate = await this.getTotalTaxRate();
|
2022-07-14 09:28:26 +00:00
|
|
|
const itemTaxedTotal = this.itemTaxedTotal ?? this.fyo.pesa(0);
|
|
|
|
const itemDiscountedTotal =
|
|
|
|
this.itemDiscountedTotal ?? this.fyo.pesa(0);
|
|
|
|
const isItemTaxedTotal = fieldname === 'itemTaxedTotal';
|
|
|
|
const discountAfterTax = this.discountAfterTax;
|
|
|
|
const setItemDiscountAmount = !!this.setItemDiscountAmount;
|
|
|
|
|
|
|
|
const rateFromTotals = getRate(
|
|
|
|
quantity,
|
|
|
|
itemDiscountPercent,
|
|
|
|
itemDiscountAmount,
|
|
|
|
totalTaxRate,
|
|
|
|
itemTaxedTotal,
|
|
|
|
itemDiscountedTotal,
|
|
|
|
isItemTaxedTotal,
|
|
|
|
discountAfterTax,
|
|
|
|
setItemDiscountAmount
|
|
|
|
);
|
2022-07-12 11:41:24 +00:00
|
|
|
|
2022-07-14 09:28:26 +00:00
|
|
|
return rateFromTotals ?? rate ?? this.fyo.pesa(0);
|
2022-07-12 11:41:24 +00:00
|
|
|
},
|
2022-07-14 09:28:26 +00:00
|
|
|
dependsOn: [
|
2022-09-30 11:42:15 +00:00
|
|
|
'party',
|
|
|
|
'exchangeRate',
|
2022-07-14 09:28:26 +00:00
|
|
|
'item',
|
|
|
|
'itemTaxedTotal',
|
|
|
|
'itemDiscountedTotal',
|
|
|
|
'setItemDiscountAmount',
|
|
|
|
],
|
2022-07-12 11:41:24 +00:00
|
|
|
},
|
2023-02-21 09:18:55 +00:00
|
|
|
unit: {
|
|
|
|
formula: async () =>
|
|
|
|
(await this.fyo.getValue(
|
|
|
|
'Item',
|
|
|
|
this.item as string,
|
|
|
|
'unit'
|
|
|
|
)) as string,
|
|
|
|
dependsOn: ['item'],
|
|
|
|
},
|
|
|
|
transferUnit: {
|
|
|
|
formula: async () =>
|
|
|
|
(await this.fyo.getValue(
|
|
|
|
'Item',
|
|
|
|
this.item as string,
|
|
|
|
'unit'
|
|
|
|
)) as string,
|
|
|
|
dependsOn: ['item'],
|
|
|
|
},
|
|
|
|
transferQuantity: {
|
|
|
|
formula: async () => {
|
|
|
|
if (this.unit === this.transferUnit) {
|
|
|
|
return this.quantity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.transferQuantity;
|
|
|
|
},
|
|
|
|
dependsOn: ['item'],
|
|
|
|
},
|
2022-07-12 11:41:24 +00:00
|
|
|
quantity: {
|
|
|
|
formula: async () => {
|
|
|
|
if (!this.item) {
|
|
|
|
return this.quantity as number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const itemDoc = await this.fyo.doc.getDoc(
|
|
|
|
ModelNameEnum.Item,
|
|
|
|
this.item as string
|
|
|
|
);
|
2023-02-15 09:09:56 +00:00
|
|
|
const unitDoc = itemDoc.getLink('uom');
|
2022-07-12 11:41:24 +00:00
|
|
|
if (unitDoc?.isWhole) {
|
2023-02-21 09:18:55 +00:00
|
|
|
return Math.round(
|
|
|
|
this.transferQuantity! * this.unitConversionFactor!
|
|
|
|
);
|
2022-07-12 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 09:18:55 +00:00
|
|
|
return safeParseFloat(
|
|
|
|
this.transferQuantity! * this.unitConversionFactor!
|
2023-02-15 09:09:56 +00:00
|
|
|
);
|
|
|
|
},
|
2023-02-21 09:18:55 +00:00
|
|
|
dependsOn: ['quantity', 'transferQuantity', 'unitConversionFactor'],
|
2023-02-15 09:09:56 +00:00
|
|
|
},
|
2023-02-21 09:18:55 +00:00
|
|
|
unitConversionFactor: {
|
2023-02-15 09:09:56 +00:00
|
|
|
formula: async () => {
|
2023-02-21 09:18:55 +00:00
|
|
|
if (this.unit === this.transferUnit) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-02-15 09:09:56 +00:00
|
|
|
const conversionFactor = await this.fyo.db.getAll(
|
2023-02-21 09:18:55 +00:00
|
|
|
ModelNameEnum.UOMConversionItem,
|
2023-02-15 09:09:56 +00:00
|
|
|
{
|
2023-02-21 09:18:55 +00:00
|
|
|
fields: ['conversionFactor'],
|
2023-02-15 09:09:56 +00:00
|
|
|
filters: { parent: this.item! },
|
|
|
|
}
|
|
|
|
);
|
2023-02-21 09:18:55 +00:00
|
|
|
|
|
|
|
return safeParseFloat(conversionFactor[0]?.conversionFactor ?? 1);
|
2023-02-15 09:09:56 +00:00
|
|
|
},
|
2023-02-21 09:18:55 +00:00
|
|
|
dependsOn: ['transferUnit'],
|
2022-07-12 11:41:24 +00:00
|
|
|
},
|
|
|
|
account: {
|
|
|
|
formula: () => {
|
|
|
|
let accountType = 'expenseAccount';
|
|
|
|
if (this.isSales) {
|
|
|
|
accountType = 'incomeAccount';
|
|
|
|
}
|
|
|
|
return this.fyo.getValue('Item', this.item as string, accountType);
|
|
|
|
},
|
|
|
|
dependsOn: ['item'],
|
|
|
|
},
|
|
|
|
tax: {
|
|
|
|
formula: async () => {
|
|
|
|
return (await this.fyo.getValue(
|
|
|
|
'Item',
|
|
|
|
this.item as string,
|
|
|
|
'tax'
|
|
|
|
)) as string;
|
|
|
|
},
|
|
|
|
dependsOn: ['item'],
|
|
|
|
},
|
|
|
|
amount: {
|
|
|
|
formula: () => (this.rate as Money).mul(this.quantity as number),
|
|
|
|
dependsOn: ['item', 'rate', 'quantity'],
|
|
|
|
},
|
|
|
|
hsnCode: {
|
|
|
|
formula: async () =>
|
|
|
|
await this.fyo.getValue('Item', this.item as string, 'hsnCode'),
|
|
|
|
dependsOn: ['item'],
|
|
|
|
},
|
2022-07-12 10:23:41 +00:00
|
|
|
itemDiscountedTotal: {
|
2022-07-14 09:28:26 +00:00
|
|
|
formula: async () => {
|
2022-07-12 10:23:41 +00:00
|
|
|
const totalTaxRate = await this.getTotalTaxRate();
|
|
|
|
const rate = this.rate ?? this.fyo.pesa(0);
|
|
|
|
const quantity = this.quantity ?? 1;
|
|
|
|
const itemDiscountAmount = this.itemDiscountAmount ?? this.fyo.pesa(0);
|
|
|
|
const itemDiscountPercent = this.itemDiscountPercent ?? 0;
|
|
|
|
|
2022-07-14 09:28:26 +00:00
|
|
|
if (this.setItemDiscountAmount && this.itemDiscountAmount?.isZero()) {
|
|
|
|
return rate.mul(quantity);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.setItemDiscountAmount && this.itemDiscountPercent === 0) {
|
2022-07-12 11:41:24 +00:00
|
|
|
return rate.mul(quantity);
|
|
|
|
}
|
|
|
|
|
2022-07-12 10:23:41 +00:00
|
|
|
if (!this.discountAfterTax) {
|
|
|
|
return getDiscountedTotalBeforeTaxation(
|
|
|
|
rate,
|
|
|
|
quantity,
|
|
|
|
itemDiscountAmount,
|
|
|
|
itemDiscountPercent,
|
2022-07-14 09:28:26 +00:00
|
|
|
!!this.setItemDiscountAmount
|
2022-07-12 10:23:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return getDiscountedTotalAfterTaxation(
|
|
|
|
totalTaxRate,
|
|
|
|
rate,
|
|
|
|
quantity,
|
|
|
|
itemDiscountAmount,
|
|
|
|
itemDiscountPercent,
|
2022-07-14 09:28:26 +00:00
|
|
|
!!this.setItemDiscountAmount
|
2022-07-12 10:23:41 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
dependsOn: [
|
|
|
|
'itemDiscountAmount',
|
|
|
|
'itemDiscountPercent',
|
2022-07-12 11:41:24 +00:00
|
|
|
'itemTaxedTotal',
|
2022-07-14 09:28:26 +00:00
|
|
|
'setItemDiscountAmount',
|
2022-07-12 11:41:24 +00:00
|
|
|
'tax',
|
|
|
|
'rate',
|
|
|
|
'quantity',
|
|
|
|
'item',
|
2022-07-12 10:23:41 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
itemTaxedTotal: {
|
|
|
|
formula: async (fieldname) => {
|
|
|
|
const totalTaxRate = await this.getTotalTaxRate();
|
|
|
|
const rate = this.rate ?? this.fyo.pesa(0);
|
|
|
|
const quantity = this.quantity ?? 1;
|
|
|
|
const itemDiscountAmount = this.itemDiscountAmount ?? this.fyo.pesa(0);
|
|
|
|
const itemDiscountPercent = this.itemDiscountPercent ?? 0;
|
|
|
|
|
|
|
|
if (!this.discountAfterTax) {
|
|
|
|
return getTaxedTotalAfterDiscounting(
|
|
|
|
totalTaxRate,
|
|
|
|
rate,
|
|
|
|
quantity,
|
|
|
|
itemDiscountAmount,
|
|
|
|
itemDiscountPercent,
|
2022-07-14 09:28:26 +00:00
|
|
|
!!this.setItemDiscountAmount
|
2022-07-12 10:23:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return getTaxedTotalBeforeDiscounting(totalTaxRate, rate, quantity);
|
|
|
|
},
|
|
|
|
dependsOn: [
|
|
|
|
'itemDiscountAmount',
|
|
|
|
'itemDiscountPercent',
|
2022-07-12 11:41:24 +00:00
|
|
|
'itemDiscountedTotal',
|
2022-07-14 09:28:26 +00:00
|
|
|
'setItemDiscountAmount',
|
2022-07-12 11:41:24 +00:00
|
|
|
'tax',
|
|
|
|
'rate',
|
|
|
|
'quantity',
|
|
|
|
'item',
|
2022-07-12 10:23:41 +00:00
|
|
|
],
|
|
|
|
},
|
2022-11-22 09:12:49 +00:00
|
|
|
stockNotTransferred: {
|
|
|
|
formula: async () => {
|
|
|
|
if (this.parentdoc?.isSubmitted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = (await this.loadAndGetLink('item')) as Item;
|
|
|
|
if (!item.trackItem) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.quantity;
|
|
|
|
},
|
|
|
|
dependsOn: ['item', 'quantity'],
|
|
|
|
},
|
2022-04-14 09:22:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
validations: ValidationMap = {
|
|
|
|
rate: async (value: DocValue) => {
|
|
|
|
if ((value as Money).gte(0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-18 11:29:20 +00:00
|
|
|
throw new ValidationError(
|
2022-04-19 05:59:36 +00:00
|
|
|
this.fyo.t`Rate (${this.fyo.format(
|
2022-04-14 09:22:45 +00:00
|
|
|
value,
|
|
|
|
'Currency'
|
|
|
|
)}) cannot be less zero.`
|
|
|
|
);
|
|
|
|
},
|
2022-07-14 09:28:26 +00:00
|
|
|
itemDiscountAmount: async (value: DocValue) => {
|
|
|
|
if ((value as Money).lte(this.amount!)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ValidationError(
|
|
|
|
this.fyo.t`Discount Amount (${this.fyo.format(
|
|
|
|
value,
|
|
|
|
'Currency'
|
|
|
|
)}) cannot be greated than Amount (${this.fyo.format(
|
|
|
|
this.amount!,
|
|
|
|
'Currency'
|
|
|
|
)}).`
|
|
|
|
);
|
|
|
|
},
|
|
|
|
itemDiscountPercent: async (value: DocValue) => {
|
|
|
|
if ((value as number) < 100) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ValidationError(
|
|
|
|
this.fyo.t`Discount Percent (${
|
|
|
|
value as number
|
|
|
|
}) cannot be greater than 100.`
|
|
|
|
);
|
|
|
|
},
|
2023-02-21 09:18:55 +00:00
|
|
|
transferUnit: async (value: DocValue) => {
|
|
|
|
if (!this.item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = await this.fyo.db.getAll(ModelNameEnum.UOMConversionItem, {
|
2023-02-15 09:09:56 +00:00
|
|
|
fields: ['parent'],
|
|
|
|
filters: { uom: value as string, parent: this.item! },
|
|
|
|
});
|
2023-02-21 09:18:55 +00:00
|
|
|
|
2023-02-15 09:09:56 +00:00
|
|
|
if (item.length < 1)
|
|
|
|
throw new ValidationError(
|
2023-02-21 09:18:55 +00:00
|
|
|
t`Transfer Unit ${value as string} is not applicable for Item ${this
|
|
|
|
.item!}`
|
2023-02-15 09:09:56 +00:00
|
|
|
);
|
|
|
|
},
|
2022-04-14 09:22:45 +00:00
|
|
|
};
|
|
|
|
|
2022-07-12 11:41:24 +00:00
|
|
|
hidden: HiddenMap = {
|
|
|
|
itemDiscountedTotal: () => {
|
2022-07-14 09:28:26 +00:00
|
|
|
if (!this.enableDiscounting) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!!this.setItemDiscountAmount && this.itemDiscountAmount?.isZero()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.setItemDiscountAmount && this.itemDiscountPercent === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2022-07-12 11:41:24 +00:00
|
|
|
},
|
2022-07-13 17:48:20 +00:00
|
|
|
setItemDiscountAmount: () => !this.enableDiscounting,
|
|
|
|
itemDiscountAmount: () =>
|
|
|
|
!(this.enableDiscounting && !!this.setItemDiscountAmount),
|
|
|
|
itemDiscountPercent: () =>
|
|
|
|
!(this.enableDiscounting && !this.setItemDiscountAmount),
|
2023-02-21 09:18:55 +00:00
|
|
|
transferUnit: () => !this.enableInventory,
|
|
|
|
transferQuantity: () => !this.enableInventory,
|
2022-07-12 11:41:24 +00:00
|
|
|
};
|
|
|
|
|
2022-04-14 09:22:45 +00:00
|
|
|
static filters: FiltersMap = {
|
|
|
|
item: (doc: Doc) => {
|
|
|
|
const itemList = doc.parentdoc!.items as Doc[];
|
|
|
|
const items = itemList.map((d) => d.item as string).filter(Boolean);
|
|
|
|
|
2022-05-05 10:44:26 +00:00
|
|
|
let itemNotFor = 'Sales';
|
2022-04-14 09:22:45 +00:00
|
|
|
if (doc.isSales) {
|
2022-05-05 10:44:26 +00:00
|
|
|
itemNotFor = 'Purchases';
|
2022-04-14 09:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const baseFilter = { for: ['not in', [itemNotFor]] };
|
|
|
|
if (items.length <= 0) {
|
|
|
|
return baseFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: ['not in', items],
|
|
|
|
...baseFilter,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
2022-07-12 11:41:24 +00:00
|
|
|
|
2022-05-18 16:55:24 +00:00
|
|
|
static createFilters: FiltersMap = {
|
|
|
|
item: (doc: Doc) => {
|
|
|
|
return { for: doc.isSales ? 'Sales' : 'Purchases' };
|
|
|
|
},
|
|
|
|
};
|
2022-09-29 11:04:35 +00:00
|
|
|
|
|
|
|
getCurrencies: CurrenciesMap = {};
|
|
|
|
_getCurrency() {
|
|
|
|
if (this.exchangeRate === 1) {
|
|
|
|
return this.fyo.singles.SystemSettings?.currency ?? DEFAULT_CURRENCY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.currency;
|
|
|
|
}
|
|
|
|
_setGetCurrencies() {
|
|
|
|
const currencyFields = this.schema.fields.filter(
|
|
|
|
({ fieldtype }) => fieldtype === FieldTypeEnum.Currency
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const { fieldname } of currencyFields) {
|
2022-09-30 11:42:15 +00:00
|
|
|
this.getCurrencies[fieldname] ??= this._getCurrency.bind(this);
|
2022-09-29 11:04:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-14 09:22:45 +00:00
|
|
|
}
|
2022-07-12 10:23:41 +00:00
|
|
|
|
|
|
|
function getDiscountedTotalBeforeTaxation(
|
|
|
|
rate: Money,
|
|
|
|
quantity: number,
|
|
|
|
itemDiscountAmount: Money,
|
|
|
|
itemDiscountPercent: number,
|
2022-07-14 09:28:26 +00:00
|
|
|
setDiscountAmount: boolean
|
2022-07-12 10:23:41 +00:00
|
|
|
) {
|
|
|
|
/**
|
|
|
|
* If Discount is applied before taxation
|
|
|
|
* Use different formulas depending on how discount is set
|
|
|
|
* - if amount : Quantity * Rate - DiscountAmount
|
|
|
|
* - if percent: Quantity * Rate (1 - DiscountPercent / 100)
|
|
|
|
*/
|
2022-07-14 09:28:26 +00:00
|
|
|
|
2022-07-12 10:23:41 +00:00
|
|
|
const amount = rate.mul(quantity);
|
2022-07-14 09:28:26 +00:00
|
|
|
if (setDiscountAmount) {
|
2022-07-12 10:23:41 +00:00
|
|
|
return amount.sub(itemDiscountAmount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount.mul(1 - itemDiscountPercent / 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTaxedTotalAfterDiscounting(
|
|
|
|
totalTaxRate: number,
|
|
|
|
rate: Money,
|
|
|
|
quantity: number,
|
|
|
|
itemDiscountAmount: Money,
|
|
|
|
itemDiscountPercent: number,
|
2022-07-14 09:28:26 +00:00
|
|
|
setItemDiscountAmount: boolean
|
2022-07-12 10:23:41 +00:00
|
|
|
) {
|
|
|
|
/**
|
|
|
|
* If Discount is applied before taxation
|
|
|
|
* Formula: Discounted Total * (1 + TotalTaxRate / 100)
|
|
|
|
*/
|
|
|
|
|
|
|
|
const discountedTotal = getDiscountedTotalBeforeTaxation(
|
|
|
|
rate,
|
|
|
|
quantity,
|
|
|
|
itemDiscountAmount,
|
|
|
|
itemDiscountPercent,
|
2022-07-14 09:28:26 +00:00
|
|
|
setItemDiscountAmount
|
2022-07-12 10:23:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return discountedTotal.mul(1 + totalTaxRate / 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDiscountedTotalAfterTaxation(
|
|
|
|
totalTaxRate: number,
|
|
|
|
rate: Money,
|
|
|
|
quantity: number,
|
|
|
|
itemDiscountAmount: Money,
|
|
|
|
itemDiscountPercent: number,
|
2022-07-14 09:28:26 +00:00
|
|
|
setItemDiscountAmount: boolean
|
2022-07-12 10:23:41 +00:00
|
|
|
) {
|
|
|
|
/**
|
|
|
|
* If Discount is applied after taxation
|
|
|
|
* Use different formulas depending on how discount is set
|
|
|
|
* - if amount : Taxed Total - Discount Amount
|
|
|
|
* - if percent: Taxed Total * (1 - Discount Percent / 100)
|
|
|
|
*/
|
|
|
|
const taxedTotal = getTaxedTotalBeforeDiscounting(
|
|
|
|
totalTaxRate,
|
|
|
|
rate,
|
|
|
|
quantity
|
|
|
|
);
|
|
|
|
|
2022-07-14 09:28:26 +00:00
|
|
|
if (setItemDiscountAmount) {
|
2022-07-12 10:23:41 +00:00
|
|
|
return taxedTotal.sub(itemDiscountAmount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return taxedTotal.mul(1 - itemDiscountPercent / 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTaxedTotalBeforeDiscounting(
|
|
|
|
totalTaxRate: number,
|
|
|
|
rate: Money,
|
|
|
|
quantity: number
|
|
|
|
) {
|
|
|
|
/**
|
|
|
|
* If Discount is applied after taxation
|
|
|
|
* Formula: Rate * Quantity * (1 + Total Tax Rate / 100)
|
|
|
|
*/
|
|
|
|
|
|
|
|
return rate.mul(quantity).mul(1 + totalTaxRate / 100);
|
|
|
|
}
|
2022-07-12 11:41:24 +00:00
|
|
|
|
2022-07-14 09:28:26 +00:00
|
|
|
function getRate(
|
2022-07-12 11:41:24 +00:00
|
|
|
quantity: number,
|
2022-07-14 09:28:26 +00:00
|
|
|
itemDiscountPercent: number,
|
|
|
|
itemDiscountAmount: Money,
|
|
|
|
totalTaxRate: number,
|
|
|
|
itemTaxedTotal: Money,
|
|
|
|
itemDiscountedTotal: Money,
|
|
|
|
isItemTaxedTotal: boolean,
|
|
|
|
discountAfterTax: boolean,
|
|
|
|
setItemDiscountAmount: boolean
|
2022-07-12 11:41:24 +00:00
|
|
|
) {
|
2022-07-14 09:28:26 +00:00
|
|
|
const isItemDiscountedTotal = !isItemTaxedTotal;
|
|
|
|
const discountBeforeTax = !discountAfterTax;
|
2022-07-12 11:41:24 +00:00
|
|
|
|
2022-07-14 09:28:26 +00:00
|
|
|
/**
|
|
|
|
* Rate calculated from itemDiscountedTotal
|
|
|
|
*/
|
|
|
|
if (isItemDiscountedTotal && discountBeforeTax && setItemDiscountAmount) {
|
|
|
|
return itemDiscountedTotal.add(itemDiscountAmount).div(quantity);
|
|
|
|
}
|
2022-07-12 11:41:24 +00:00
|
|
|
|
2022-07-14 09:28:26 +00:00
|
|
|
if (isItemDiscountedTotal && discountBeforeTax && !setItemDiscountAmount) {
|
|
|
|
return itemDiscountedTotal.div(quantity * (1 - itemDiscountPercent / 100));
|
|
|
|
}
|
2022-07-12 11:41:24 +00:00
|
|
|
|
2022-07-14 09:28:26 +00:00
|
|
|
if (isItemDiscountedTotal && discountAfterTax && setItemDiscountAmount) {
|
|
|
|
return itemDiscountedTotal
|
|
|
|
.add(itemDiscountAmount)
|
|
|
|
.div(quantity * (1 + totalTaxRate / 100));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isItemDiscountedTotal && discountAfterTax && !setItemDiscountAmount) {
|
|
|
|
return itemDiscountedTotal.div(
|
|
|
|
(quantity * (100 - itemDiscountPercent) * (100 + totalTaxRate)) / 100
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rate calculated from itemTaxedTotal
|
|
|
|
*/
|
|
|
|
if (isItemTaxedTotal && discountAfterTax) {
|
|
|
|
return itemTaxedTotal.div(quantity * (1 + totalTaxRate / 100));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isItemTaxedTotal && discountBeforeTax && setItemDiscountAmount) {
|
|
|
|
return itemTaxedTotal
|
|
|
|
.div(1 + totalTaxRate / 100)
|
|
|
|
.add(itemDiscountAmount)
|
|
|
|
.div(quantity);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isItemTaxedTotal && discountBeforeTax && !setItemDiscountAmount) {
|
|
|
|
return itemTaxedTotal.div(
|
|
|
|
quantity * (1 - itemDiscountPercent / 100) * (1 + totalTaxRate / 100)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2022-07-12 11:41:24 +00:00
|
|
|
}
|