2
0
mirror of https://github.com/frappe/books.git synced 2024-11-15 09:54:04 +00:00
books/models/baseModels/InvoiceItem/InvoiceItem.ts

860 lines
22 KiB
TypeScript
Raw Normal View History

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';
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';
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';
import { Invoice } from '../Invoice/Invoice';
2022-11-22 09:12:49 +00:00
import { Item } from '../Item/Item';
import { StockTransfer } from 'models/inventory/StockTransfer';
2023-06-07 04:45:26 +00:00
import { PriceList } from '../PriceList/PriceList';
import { isPesa } from 'fyo/utils';
2024-01-30 12:55:50 +00:00
import { PricingRule } from '../PricingRule/PricingRule';
export abstract class InvoiceItem extends Doc {
2022-11-22 09:12:49 +00:00
item?: string;
account?: string;
amount?: Money;
parentdoc?: Invoice;
rate?: Money;
description?: string;
hsnCode?: number;
unit?: string;
transferUnit?: string;
quantity?: number;
transferQuantity?: number;
unitConversionFactor?: number;
2023-02-28 06:01:04 +00:00
batch?: string;
tax?: string;
2022-11-22 09:12:49 +00:00
stockNotTransferred?: number;
setItemDiscountAmount?: boolean;
itemDiscountAmount?: Money;
itemDiscountPercent?: number;
2022-07-12 11:41:24 +00:00
itemDiscountedTotal?: Money;
itemTaxedTotal?: Money;
2024-02-01 11:34:39 +00:00
isFreeItem?: boolean;
get isSales() {
2023-12-22 08:50:35 +00:00
return (
this.schemaName === 'SalesInvoiceItem' ||
this.schemaName === 'SalesQuoteItem'
);
}
2023-06-06 08:59:08 +00:00
get date() {
return this.parentdoc?.date ?? undefined;
}
get party() {
return this.parentdoc?.party ?? undefined;
}
get priceList() {
return this.parentdoc?.priceList ?? undefined;
}
get discountAfterTax() {
return !!this?.parentdoc?.discountAfterTax;
}
get enableDiscounting() {
return !!this.fyo.singles?.AccountingSettings?.enableDiscounting;
}
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;
}
2023-09-25 10:15:20 +00:00
get isReturn() {
return !!this.parentdoc?.isReturn;
}
2024-02-01 11:34:39 +00:00
get pricingRuleDetail() {
return this.parentdoc?.pricingRuleDetail;
}
2022-09-29 11:04:35 +00:00
constructor(schema: Schema, data: DocValueMap, fyo: Fyo) {
super(schema, data, fyo);
this._setGetCurrencies();
}
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);
}
formulas: FormulaMap = {
description: {
formula: async () =>
(await this.fyo.getValue(
'Item',
this.item as string,
'description'
)) as string,
dependsOn: ['item'],
},
2022-07-12 11:41:24 +00:00
rate: {
formula: async (fieldname) => {
2023-06-07 04:45:26 +00:00
const rate = await getItemRate(this);
if (!rate?.float && this.rate?.float) {
return this.rate;
}
2022-07-12 11:41:24 +00:00
if (
fieldname !== 'itemTaxedTotal' &&
fieldname !== 'itemDiscountedTotal'
) {
return rate?.div(this.exchangeRate) ?? this.fyo.pesa(0);
2022-07-12 11:41:24 +00:00
}
const quantity = this.quantity ?? 0;
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();
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
return rateFromTotals ?? rate ?? this.fyo.pesa(0);
2022-07-12 11:41:24 +00:00
},
dependsOn: [
2023-06-06 08:59:08 +00:00
'date',
'priceList',
'batch',
'party',
'exchangeRate',
'item',
'itemTaxedTotal',
'itemDiscountedTotal',
'setItemDiscountAmount',
2024-02-01 11:34:39 +00:00
'pricingRuleDetail',
],
2022-07-12 11:41:24 +00:00
},
unit: {
formula: async () =>
(await this.fyo.getValue(
'Item',
this.item as string,
'unit'
)) as string,
dependsOn: ['item'],
},
transferUnit: {
formula: async (fieldname) => {
if (fieldname === 'quantity' || fieldname === 'unit') {
return this.unit;
}
return (await this.fyo.getValue(
'Item',
this.item as string,
'unit'
)) as string;
},
dependsOn: ['item', 'unit'],
},
transferQuantity: {
formula: (fieldname) => {
if (fieldname === 'quantity' || this.unit === this.transferUnit) {
return this.quantity;
}
return this.transferQuantity;
},
dependsOn: ['item', 'quantity'],
},
2022-07-12 11:41:24 +00:00
quantity: {
formula: async (fieldname) => {
2022-07-12 11:41:24 +00:00
if (!this.item) {
return this.quantity as number;
}
const itemDoc = await this.fyo.doc.getDoc(
ModelNameEnum.Item,
this.item
2022-07-12 11:41:24 +00:00
);
2023-02-15 09:09:56 +00:00
const unitDoc = itemDoc.getLink('uom');
let quantity: number = this.quantity ?? 1;
2023-09-25 10:15:20 +00:00
if (this.isReturn && quantity > 0) {
quantity *= -1;
}
if (!this.isReturn && quantity < 0) {
quantity *= -1;
}
if (fieldname === 'transferQuantity') {
quantity = this.transferQuantity! * this.unitConversionFactor!;
}
2022-07-12 11:41:24 +00:00
if (unitDoc?.isWhole) {
return Math.round(quantity);
2022-07-12 11:41:24 +00:00
}
return safeParseFloat(quantity);
2023-02-15 09:09:56 +00:00
},
dependsOn: [
'quantity',
'transferQuantity',
'transferUnit',
'unitConversionFactor',
2023-09-25 10:15:20 +00:00
'item',
'isReturn',
],
2023-02-15 09:09:56 +00:00
},
unitConversionFactor: {
2023-02-15 09:09:56 +00:00
formula: async () => {
if (this.unit === this.transferUnit) {
return 1;
}
2023-02-15 09:09:56 +00:00
const conversionFactor = await this.fyo.db.getAll(
ModelNameEnum.UOMConversionItem,
2023-02-15 09:09:56 +00:00
{
fields: ['conversionFactor'],
2023-02-15 09:09:56 +00:00
filters: { parent: this.item! },
}
);
return safeParseFloat(conversionFactor[0]?.conversionFactor ?? 1);
2023-02-15 09:09:56 +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'],
},
itemDiscountedTotal: {
formula: async () => {
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.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);
}
if (!this.discountAfterTax) {
return getDiscountedTotalBeforeTaxation(
rate,
quantity,
itemDiscountAmount,
itemDiscountPercent,
!!this.setItemDiscountAmount
);
}
return getDiscountedTotalAfterTaxation(
totalTaxRate,
rate,
quantity,
itemDiscountAmount,
itemDiscountPercent,
!!this.setItemDiscountAmount
);
},
dependsOn: [
'itemDiscountAmount',
'itemDiscountPercent',
2022-07-12 11:41:24 +00:00
'itemTaxedTotal',
'setItemDiscountAmount',
2022-07-12 11:41:24 +00:00
'tax',
'rate',
'quantity',
'item',
],
},
itemTaxedTotal: {
formula: async () => {
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,
!!this.setItemDiscountAmount
);
}
return getTaxedTotalBeforeDiscounting(totalTaxRate, rate, quantity);
},
dependsOn: [
'itemDiscountAmount',
'itemDiscountPercent',
2022-07-12 11:41:24 +00:00
'itemDiscountedTotal',
'setItemDiscountAmount',
2022-07-12 11:41:24 +00:00
'tax',
'rate',
'quantity',
'item',
],
},
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;
}
const { backReference, stockTransferSchemaName } = this.parentdoc ?? {};
if (
!backReference ||
!stockTransferSchemaName ||
typeof this.quantity !== 'number'
) {
return this.quantity;
}
const refdoc = (await this.fyo.doc.getDoc(
stockTransferSchemaName,
backReference
)) as StockTransfer;
const transferred =
refdoc.items
?.filter((i) => i.item === this.item)
.reduce((acc, i) => i.quantity ?? 0 + acc, 0) ?? 0;
return Math.max(0, this.quantity - transferred);
2022-11-22 09:12:49 +00:00
},
dependsOn: ['item', 'quantity'],
},
2024-02-01 11:34:39 +00:00
setItemDiscountAmount: {
2024-01-30 12:55:50 +00:00
formula: async () => {
2024-02-01 11:34:39 +00:00
if (
!this.fyo.singles.AccountingSettings?.enablePricingRule ||
!this.parentdoc?.pricingRuleDetail
) {
return this.setItemDiscountAmount;
2024-01-30 12:55:50 +00:00
}
2024-02-01 11:34:39 +00:00
const pricingRule = this.parentdoc?.pricingRuleDetail?.filter(
(prDetail) => prDetail.referenceItem === this.item
);
if (!pricingRule) {
return this.setItemDiscountAmount;
}
const pricingRuleDoc = (await this.fyo.doc.getDoc(
ModelNameEnum.PricingRule,
pricingRule[0].referenceName
)) as PricingRule;
if (pricingRuleDoc.discountType === 'Product Discount') {
return this.setItemDiscountAmount;
}
2024-01-30 12:55:50 +00:00
2024-02-01 11:34:39 +00:00
if (pricingRuleDoc.priceDiscountType === 'amount') {
const discountAmount = pricingRuleDoc.discountAmount?.mul(
this.quantity as number
2024-01-30 12:55:50 +00:00
);
2024-02-01 11:34:39 +00:00
await this.set('itemDiscountAmount', discountAmount);
return true;
}
2024-01-30 12:55:50 +00:00
2024-02-01 11:34:39 +00:00
return this.setItemDiscountAmount;
},
dependsOn: ['pricingRuleDetail'],
},
itemDiscountPercent: {
formula: async () => {
if (
!this.fyo.singles.AccountingSettings?.enablePricingRule ||
!this.parentdoc?.pricingRuleDetail
) {
return this.itemDiscountPercent;
}
2024-01-30 12:55:50 +00:00
2024-02-01 11:34:39 +00:00
const pricingRule = this.parentdoc?.pricingRuleDetail?.filter(
(prDetail) => prDetail.referenceItem === this.item
);
if (!pricingRule) {
return this.itemDiscountPercent;
2024-01-30 12:55:50 +00:00
}
2024-02-01 11:34:39 +00:00
const pricingRuleDoc = (await this.fyo.doc.getDoc(
ModelNameEnum.PricingRule,
pricingRule[0].referenceName
)) as PricingRule;
if (pricingRuleDoc.discountType === 'Product Discount') {
return this.itemDiscountPercent;
}
if (pricingRuleDoc.priceDiscountType === 'percentage') {
await this.set('setItemDiscountAmount', false);
return pricingRuleDoc.discountPercentage;
}
2024-03-06 10:24:25 +00:00
return this.itemDiscountPercent;
2024-01-30 12:55:50 +00:00
},
2024-02-01 11:34:39 +00:00
dependsOn: ['pricingRuleDetail'],
2024-01-30 12:55:50 +00:00
},
};
validations: ValidationMap = {
rate: (value: DocValue) => {
if ((value as Money).gte(0)) {
return;
}
throw new ValidationError(
this.fyo.t`Rate (${this.fyo.format(
value,
'Currency'
)}) cannot be less zero.`
);
},
itemDiscountAmount: (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: (value: DocValue) => {
if ((value as number) < 100) {
return;
}
throw new ValidationError(
this.fyo.t`Discount Percent (${
value as number
}) cannot be greater than 100.`
);
},
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-15 09:09:56 +00:00
});
2023-02-15 09:09:56 +00:00
if (item.length < 1)
throw new ValidationError(
t`Transfer Unit ${value as string} is not applicable for Item ${
this.item
}`
2023-02-15 09:09:56 +00:00
);
},
};
2022-07-12 11:41:24 +00:00
hidden: HiddenMap = {
itemDiscountedTotal: () => {
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
},
setItemDiscountAmount: () => !this.enableDiscounting,
itemDiscountAmount: () =>
!(this.enableDiscounting && !!this.setItemDiscountAmount),
itemDiscountPercent: () =>
!(this.enableDiscounting && !this.setItemDiscountAmount),
2023-02-28 06:01:04 +00:00
batch: () => !this.fyo.singles.InventorySettings?.enableBatches,
transferUnit: () =>
!this.fyo.singles.InventorySettings?.enableUomConversions,
transferQuantity: () =>
!this.fyo.singles.InventorySettings?.enableUomConversions,
unitConversionFactor: () =>
!this.fyo.singles.InventorySettings?.enableUomConversions,
2022-07-12 11:41:24 +00:00
};
static filters: FiltersMap = {
item: (doc: Doc) => {
let itemNotFor = 'Sales';
if (doc.isSales) {
itemNotFor = 'Purchases';
}
return { for: ['not in', [itemNotFor]] };
},
};
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) {
this.getCurrencies[fieldname] ??= this._getCurrency.bind(this);
2022-09-29 11:04:35 +00:00
}
}
}
2023-06-07 04:45:26 +00:00
async function getItemRate(doc: InvoiceItem): Promise<Money | undefined> {
2024-02-01 11:34:39 +00:00
if (doc.isFreeItem) {
return doc.rate;
}
let pricingRuleRate: Money | undefined;
if (doc.fyo.singles.AccountingSettings?.enablePricingRule) {
pricingRuleRate = await getItemRateFromPricingRule(doc);
}
if (pricingRuleRate) {
return pricingRuleRate;
}
2023-06-07 04:45:26 +00:00
let priceListRate: Money | undefined;
2024-02-01 11:34:39 +00:00
2023-06-07 04:45:26 +00:00
if (doc.fyo.singles.AccountingSettings?.enablePriceList) {
priceListRate = await getItemRateFromPriceList(doc);
}
if (priceListRate) {
return priceListRate;
}
if (!doc.item) {
return;
}
const itemRate = await doc.fyo.getValue(ModelNameEnum.Item, doc.item, 'rate');
if (isPesa(itemRate)) {
return itemRate;
}
return;
}
2024-02-01 11:34:39 +00:00
async function getItemRateFromPricingRule(
doc: InvoiceItem
): Promise<Money | undefined> {
const pricingRule = doc.parentdoc?.pricingRuleDetail?.filter(
(prDetail) => prDetail.referenceItem === doc.item
);
if (!pricingRule) {
return;
}
const pricingRuleDoc = (await doc.fyo.doc.getDoc(
ModelNameEnum.PricingRule,
pricingRule[0].referenceName
)) as PricingRule;
if (pricingRuleDoc.discountType !== 'Price Discount') {
return;
}
if (pricingRuleDoc.priceDiscountType !== 'rate') {
return;
}
return pricingRuleDoc.discountRate;
}
2023-06-07 04:45:26 +00:00
async function getItemRateFromPriceList(
doc: InvoiceItem
): Promise<Money | undefined> {
const priceListName = doc.parentdoc?.priceList;
const item = doc.item;
if (!priceListName || !item) {
return;
}
const priceList = await doc.fyo.doc.getDoc(
ModelNameEnum.PriceList,
priceListName
);
if (!(priceList instanceof PriceList)) {
return;
}
const unit = doc.unit;
const transferUnit = doc.transferUnit;
const plItem = priceList.priceListItem?.find((pli) => {
if (pli.item !== item) {
return false;
}
if (transferUnit && pli.unit !== transferUnit) {
return false;
} else if (unit && pli.unit !== unit) {
return false;
}
return true;
});
return plItem?.rate;
}
function getDiscountedTotalBeforeTaxation(
rate: Money,
quantity: number,
itemDiscountAmount: Money,
itemDiscountPercent: number,
setDiscountAmount: boolean
) {
/**
* 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)
*/
const amount = rate.mul(quantity);
if (setDiscountAmount) {
return amount.sub(itemDiscountAmount);
}
return amount.mul(1 - itemDiscountPercent / 100);
}
function getTaxedTotalAfterDiscounting(
totalTaxRate: number,
rate: Money,
quantity: number,
itemDiscountAmount: Money,
itemDiscountPercent: number,
setItemDiscountAmount: boolean
) {
/**
* If Discount is applied before taxation
* Formula: Discounted Total * (1 + TotalTaxRate / 100)
*/
const discountedTotal = getDiscountedTotalBeforeTaxation(
rate,
quantity,
itemDiscountAmount,
itemDiscountPercent,
setItemDiscountAmount
);
return discountedTotal.mul(1 + totalTaxRate / 100);
}
function getDiscountedTotalAfterTaxation(
totalTaxRate: number,
rate: Money,
quantity: number,
itemDiscountAmount: Money,
itemDiscountPercent: number,
setItemDiscountAmount: boolean
) {
/**
* 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
);
if (setItemDiscountAmount) {
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
function getRate(
2022-07-12 11:41:24 +00:00
quantity: number,
itemDiscountPercent: number,
itemDiscountAmount: Money,
totalTaxRate: number,
itemTaxedTotal: Money,
itemDiscountedTotal: Money,
isItemTaxedTotal: boolean,
discountAfterTax: boolean,
setItemDiscountAmount: boolean
2022-07-12 11:41:24 +00:00
) {
const isItemDiscountedTotal = !isItemTaxedTotal;
const discountBeforeTax = !discountAfterTax;
2022-07-12 11:41:24 +00:00
/**
* Rate calculated from itemDiscountedTotal
*/
if (isItemDiscountedTotal && discountBeforeTax && setItemDiscountAmount) {
return itemDiscountedTotal.add(itemDiscountAmount).div(quantity);
}
2022-07-12 11:41:24 +00:00
if (isItemDiscountedTotal && discountBeforeTax && !setItemDiscountAmount) {
return itemDiscountedTotal.div(quantity * (1 - itemDiscountPercent / 100));
}
2022-07-12 11:41:24 +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
}