diff --git a/models/baseModels/Invoice/Invoice.ts b/models/baseModels/Invoice/Invoice.ts index 0cac0054..3b60e3b3 100644 --- a/models/baseModels/Invoice/Invoice.ts +++ b/models/baseModels/Invoice/Invoice.ts @@ -12,7 +12,6 @@ import { DEFAULT_CURRENCY } from 'fyo/utils/consts'; import { ValidationError } from 'fyo/utils/errors'; import { Transactional } from 'models/Transactional/Transactional'; import { addItem, getExchangeRate, getNumberSeries } from 'models/helpers'; -import { InventorySettings } from 'models/inventory/InventorySettings'; import { StockTransfer } from 'models/inventory/StockTransfer'; import { validateBatch } from 'models/inventory/helpers'; import { ModelNameEnum } from 'models/types'; @@ -547,8 +546,8 @@ export abstract class Invoice extends Transactional { }), numberSeries: (doc: Doc) => ({ referenceType: doc.schemaName }), priceList: (doc: Doc) => ({ - enabled: true, - ...(doc.isSales ? { selling: true } : { buying: true }), + isEnabled: true, + ...(doc.isSales ? { isSales: true } : { isPurchase: true }), }), }; diff --git a/models/baseModels/InvoiceItem/InvoiceItem.ts b/models/baseModels/InvoiceItem/InvoiceItem.ts index 0bbff395..03760fec 100644 --- a/models/baseModels/InvoiceItem/InvoiceItem.ts +++ b/models/baseModels/InvoiceItem/InvoiceItem.ts @@ -17,7 +17,8 @@ import { safeParseFloat } from 'utils/index'; import { Invoice } from '../Invoice/Invoice'; import { Item } from '../Item/Item'; import { StockTransfer } from 'models/inventory/StockTransfer'; -import { getPriceListRate } from 'models/helpers'; +import { PriceList } from '../PriceList/PriceList'; +import { isPesa } from 'fyo/utils'; export abstract class InvoiceItem extends Doc { item?: string; @@ -114,15 +115,7 @@ export abstract class InvoiceItem extends Doc { }, rate: { formula: async (fieldname) => { - const priceListRate = await getPriceListRate(this); - const itemRate = (await this.fyo.getValue( - 'Item', - this.item as string, - 'rate' - )) as undefined | Money; - - const rate = priceListRate instanceof Money ? priceListRate : itemRate; - + const rate = await getItemRate(this); if (!rate?.float && this.rate?.float) { return this.rate; } @@ -529,6 +522,65 @@ export abstract class InvoiceItem extends Doc { } } +async function getItemRate(doc: InvoiceItem): Promise { + let priceListRate: Money | undefined; + 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; +} + +async function getItemRateFromPriceList( + doc: InvoiceItem +): Promise { + 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, diff --git a/models/baseModels/PriceList/PriceListItem.ts b/models/baseModels/PriceList/PriceListItem.ts index 3a90b250..6d9164fe 100644 --- a/models/baseModels/PriceList/PriceListItem.ts +++ b/models/baseModels/PriceList/PriceListItem.ts @@ -1,8 +1,8 @@ import { Doc } from 'fyo/model/doc'; -import { FormulaMap } from 'fyo/model/types'; -import { Money } from 'pesa'; -import type { PriceList } from './PriceList'; +import type { FormulaMap } from 'fyo/model/types'; import { ModelNameEnum } from 'models/types'; +import type { Money } from 'pesa'; +import type { PriceList } from './PriceList'; export class PriceListItem extends Doc { item?: string; @@ -10,18 +10,6 @@ export class PriceListItem extends Doc { rate?: Money; parentdoc?: PriceList; - get isBuying() { - return !!this.parentdoc?.buying; - } - - get isSelling() { - return !!this.parentdoc?.selling; - } - - get priceList() { - return this.parentdoc?.name; - } - formulas: FormulaMap = { unit: { formula: async () => { diff --git a/models/helpers.ts b/models/helpers.ts index f56b0e8d..4c508d0d 100644 --- a/models/helpers.ts +++ b/models/helpers.ts @@ -17,8 +17,6 @@ import { Invoice } from './baseModels/Invoice/Invoice'; import { StockMovement } from './inventory/StockMovement'; import { StockTransfer } from './inventory/StockTransfer'; import { InvoiceStatus, ModelNameEnum } from './types'; -import { InvoiceItem } from './baseModels/InvoiceItem/InvoiceItem'; -import { ItemPrice } from './baseModels/ItemPrice/ItemPrice'; export function getInvoiceActions( fyo: Fyo, @@ -332,19 +330,15 @@ export function getPriceListStatusColumn(): ColumnConfig { label: t`Enabled For`, fieldname: 'enabledFor', fieldtype: 'Select', - render(doc) { + render({ isSales, isPurchase }) { let status = t`None`; - if (doc.buying && !doc.selling) { - status = t`Buying`; - } - - if (doc.selling && !doc.buying) { - status = t`Selling`; - } - - if (doc.buying && doc.selling) { - status = t`Buying & Selling`; + if (isSales && isPurchase) { + status = t`Sales and Purchase`; + } else if (isSales) { + status = t`Sales`; + } else if (isPurchase) { + status = t`Purchase`; } return { @@ -360,9 +354,9 @@ export function getPriceListEnabledColumn(): ColumnConfig { fieldname: 'enabled', fieldtype: 'Data', render(doc) { - let status = t`Unenabled`; + let status = t`Disabled`; let color = 'orange'; - if (doc.enabled) { + if (doc.isEnabled) { status = t`Enabled`; color = 'green'; } @@ -374,98 +368,6 @@ export function getPriceListEnabledColumn(): ColumnConfig { }; } -export async function getItemPrice( - doc: InvoiceItem | ItemPrice, - validFrom?: Date, - validUpto?: Date -): Promise { - if (!doc.item || !doc.priceList) { - return; - } - - const { isUomDependent, enabled, buying, selling } = await doc.fyo.doc.getDoc( - ModelNameEnum.PriceList, - doc.priceList, - ); - - if(!enabled || doc.isSales && !selling || !doc.isSales && !buying){ - return - } - - const itemPriceQuery = Object.values( - await doc.fyo.db.getAll(ModelNameEnum.ItemPrice, { - filters: { - enabled: true, - item: doc.item, - // ...(doc.isSales ? { selling: true } : { buying: true }), - ...(doc.batch ? { batch: doc.batch as string } : { batch: null }), - }, - fields: ['name', 'unit', 'party', 'batch', 'validFrom', 'validUpto'], - }) - )[0]; - - if (!itemPriceQuery) { - return; - } - - const { name, unit, party } = itemPriceQuery; - const validFromDate = validFrom ?? itemPriceQuery.validFrom; - const validUptoDate = validFrom ?? itemPriceQuery.validUpto; - let date; - - if (doc.date) { - date = new Date((doc.date as Date).setHours(0, 0, 0)); - } - - if (isUomDependent && unit !== doc.unit) { - return; - } - - if (party && doc.party !== party) { - return; - } - - if (date instanceof Date) { - if (validFromDate && date < validFromDate) { - return; - } - - if (validUptoDate && date > validUptoDate) { - return; - } - } - - if (validFrom && validUpto) { - if (validFromDate && validFrom < validFromDate) { - return; - } - - if (validUptoDate && validFrom > validUptoDate) { - return; - } - } - - return name as string; -} - -export async function getPriceListRate( - doc: InvoiceItem -): Promise { - const itemPrice = await getItemPrice(doc); - - if (!itemPrice) { - return; - } - - const itemPriceRate = (await doc.fyo.getValue( - ModelNameEnum.ItemPrice, - itemPrice, - 'rate' - )) as Money; - - return itemPriceRate; -} - export async function getExchangeRate({ fromCurrency, toCurrency,