diff --git a/models/baseModels/Invoice/Invoice.ts b/models/baseModels/Invoice/Invoice.ts index 01b21778..9e4d2620 100644 --- a/models/baseModels/Invoice/Invoice.ts +++ b/models/baseModels/Invoice/Invoice.ts @@ -11,6 +11,7 @@ import { import { DEFAULT_CURRENCY } from 'fyo/utils/consts'; import { ValidationError } from 'fyo/utils/errors'; import { addItem, getExchangeRate, getNumberSeries } from 'models/helpers'; +import { validateBatchNumber } from 'models/inventory/helpers'; import { InventorySettings } from 'models/inventory/InventorySettings'; import { StockTransfer } from 'models/inventory/StockTransfer'; import { Transactional } from 'models/Transactional/Transactional'; diff --git a/models/baseModels/Item/Item.ts b/models/baseModels/Item/Item.ts index 51193c6e..8bac0c06 100644 --- a/models/baseModels/Item/Item.ts +++ b/models/baseModels/Item/Item.ts @@ -93,7 +93,7 @@ export class Item extends Doc { if (itemEntriesInSLE > 0) { throw new ValidationError( - this.fyo.t`Cannot change value of Has Batch No as Item ${this + this.fyo.t`Cannot change value of Has Batch Number as Item ${this .name!} already has transactions against it. ` ); } diff --git a/models/inventory/StockMovement.ts b/models/inventory/StockMovement.ts index bc8d13c4..d539ec44 100644 --- a/models/inventory/StockMovement.ts +++ b/models/inventory/StockMovement.ts @@ -15,6 +15,7 @@ import { import { LedgerPosting } from 'models/Transactional/LedgerPosting'; import { ModelNameEnum } from 'models/types'; import { Money } from 'pesa'; +import { validateBatchNumber } from './helpers'; import { StockMovementItem } from './StockMovementItem'; import { Transfer } from './Transfer'; import { MovementType } from './types'; @@ -49,6 +50,11 @@ export class StockMovement extends Transfer { async validate() { await super.validate(); + this.validateManufacture(); + await validateBatchNumber(this); + } + + validateManufacture() { if (this.movementType !== MovementType.Manufacture) { return; } diff --git a/models/inventory/StockTransfer.ts b/models/inventory/StockTransfer.ts index fcbceaf7..18b28896 100644 --- a/models/inventory/StockTransfer.ts +++ b/models/inventory/StockTransfer.ts @@ -15,6 +15,7 @@ import { addItem, getLedgerLinkAction, getNumberSeries } from 'models/helpers'; import { LedgerPosting } from 'models/Transactional/LedgerPosting'; import { ModelNameEnum } from 'models/types'; import { Money } from 'pesa'; +import { validateBatchNumber } from './helpers'; import { StockTransferItem } from './StockTransferItem'; import { Transfer } from './Transfer'; @@ -151,6 +152,11 @@ export abstract class StockTransfer extends Transfer { } } + override async validate(): Promise { + await super.validate(); + await validateBatchNumber(this); + } + static getActions(fyo: Fyo): Action[] { return [getLedgerLinkAction(fyo, false), getLedgerLinkAction(fyo, true)]; } diff --git a/models/inventory/helpers.ts b/models/inventory/helpers.ts index 8b137891..8974f113 100644 --- a/models/inventory/helpers.ts +++ b/models/inventory/helpers.ts @@ -1 +1,40 @@ +import { ValidationError } from 'fyo/utils/errors'; +import { Invoice } from 'models/baseModels/Invoice/Invoice'; +import { InvoiceItem } from 'models/baseModels/InvoiceItem/InvoiceItem'; +import { ModelNameEnum } from 'models/types'; +import { StockMovement } from './StockMovement'; +import { StockMovementItem } from './StockMovementItem'; +import { StockTransfer } from './StockTransfer'; +import { StockTransferItem } from './StockTransferItem'; +export async function validateBatchNumber( + doc: StockMovement | StockTransfer | Invoice +) { + for (const row of doc.items ?? []) { + await validateItemRowBatchNumber(row); + } +} + +async function validateItemRowBatchNumber( + doc: StockMovementItem | StockTransferItem | InvoiceItem +) { + const item = doc.item; + const batchNumber = doc.batchNumber; + if (!item || batchNumber) { + return; + } + + const hasBatchNumber = await doc.fyo.getValue( + ModelNameEnum.Item, + item, + 'hasBatchNumber' + ); + + if (hasBatchNumber && batchNumber) { + return; + } + + throw new ValidationError( + [`Batch Number not set.`, `Item ${item} is a batched item`].join(' ') + ); +} diff --git a/schemas/app/Item.json b/schemas/app/Item.json index 2420297b..bd9bf838 100644 --- a/schemas/app/Item.json +++ b/schemas/app/Item.json @@ -133,7 +133,7 @@ }, { "fieldname": "hasBatchNumber", - "label": "Has Batch No", + "label": "Has Batch Number", "fieldtype": "Check", "default": false, "section": "Inventory"