diff --git a/models/baseModels/Item/Item.ts b/models/baseModels/Item/Item.ts index d921974b..d1aa2e44 100644 --- a/models/baseModels/Item/Item.ts +++ b/models/baseModels/Item/Item.ts @@ -8,13 +8,14 @@ import { HiddenMap, ListViewSettings, ReadOnlyMap, - ValidationMap + ValidationMap, } from 'fyo/model/types'; import { ValidationError } from 'fyo/utils/errors'; import { Money } from 'pesa'; import { AccountRootTypeEnum, AccountTypeEnum } from '../Account/types'; export class Item extends Doc { + trackItem?: boolean; itemType?: 'Product' | 'Service'; formulas: FormulaMap = { @@ -32,6 +33,11 @@ export class Item extends Doc { }, expenseAccount: { formula: async () => { + if (this.trackItem) { + return this.fyo.singles.InventorySettings + ?.stockReceivedButNotBilled as string; + } + const cogs = await this.fyo.db.getAllRaw('Account', { filters: { accountType: AccountTypeEnum['Cost of Goods Sold'], @@ -44,7 +50,7 @@ export class Item extends Doc { return cogs[0].name as string; } }, - dependsOn: ['itemType'], + dependsOn: ['itemType', 'trackItem'], }, }; @@ -53,9 +59,11 @@ export class Item extends Doc { isGroup: false, rootType: AccountRootTypeEnum.Income, }), - expenseAccount: () => ({ + expenseAccount: (doc) => ({ isGroup: false, - rootType: AccountRootTypeEnum.Expense, + rootType: doc.trackItem + ? AccountRootTypeEnum.Liability + : AccountRootTypeEnum.Expense, }), }; diff --git a/reports/inventory/StockLedger.ts b/reports/inventory/StockLedger.ts index f41e1b39..7d9e9bc6 100644 --- a/reports/inventory/StockLedger.ts +++ b/reports/inventory/StockLedger.ts @@ -2,6 +2,7 @@ import { Fyo, t } from 'fyo'; import { Action } from 'fyo/model/types'; import { cloneDeep } from 'lodash'; import { DateTime } from 'luxon'; +import { ValuationMethod } from 'models/inventory/types'; import { ModelNameEnum } from 'models/types'; import getCommonExportActions from 'reports/commonExporter'; import { Report } from 'reports/Report'; @@ -76,8 +77,13 @@ export class StockLedger extends Report { } async _setRawData() { + const valuationMethod = + (this.fyo.singles.InventorySettings?.valuationMethod as + | ValuationMethod + | undefined) ?? ValuationMethod.FIFO; + const rawSLEs = await getRawStockLedgerEntries(this.fyo); - this._rawData = getStockLedgerEntries(rawSLEs); + this._rawData = getStockLedgerEntries(rawSLEs, valuationMethod); } _getFilteredRawData(rawData: ComputedStockLedgerEntry[]) { diff --git a/reports/inventory/helpers.ts b/reports/inventory/helpers.ts index 7fdef982..e81ecb4e 100644 --- a/reports/inventory/helpers.ts +++ b/reports/inventory/helpers.ts @@ -1,5 +1,6 @@ import { Fyo } from 'fyo'; import { StockQueue } from 'models/inventory/stockQueue'; +import { ValuationMethod } from 'models/inventory/types'; import { ModelNameEnum } from 'models/types'; import { safeParseFloat, safeParseInt } from 'utils/index'; import { ComputedStockLedgerEntry, RawStockLedgerEntry } from './types'; @@ -24,7 +25,8 @@ export async function getRawStockLedgerEntries(fyo: Fyo) { } export function getStockLedgerEntries( - rawSLEs: RawStockLedgerEntry[] + rawSLEs: RawStockLedgerEntry[], + valuationMethod: ValuationMethod ): ComputedStockLedgerEntry[] { type Item = string; type Location = string; @@ -60,7 +62,11 @@ export function getStockLedgerEntries( } const balanceQuantity = q.quantity; - const valuationRate = q.fifo; + let valuationRate = q.fifo; + if (valuationMethod === ValuationMethod.MovingAverage) { + valuationRate = q.movingAverage; + } + const balanceValue = q.value; const valueChange = balanceValue - initialValue; diff --git a/schemas/app/Item.json b/schemas/app/Item.json index 152eb6a0..23a30650 100644 --- a/schemas/app/Item.json +++ b/schemas/app/Item.json @@ -71,7 +71,7 @@ }, { "fieldname": "incomeAccount", - "label": "Income", + "label": "Sales Acc.", "fieldtype": "Link", "target": "Account", "placeholder": "Income", @@ -80,7 +80,7 @@ }, { "fieldname": "expenseAccount", - "label": "Expense", + "label": "Purchase Acc.", "fieldtype": "Link", "target": "Account", "placeholder": "Expense",