fix:uom and batch filter

This commit is contained in:
Gadha Venu
2025-05-29 15:23:23 +05:30
parent 044e8000a1
commit 269c25c10d
4 changed files with 71 additions and 15 deletions
+17 -8
View File
@@ -209,10 +209,7 @@ export abstract class InvoiceItem extends Doc {
}
const validUnits = conversionItems.map((i) => i.uom as string);
if (
this.transferUnit &&
validUnits.includes(this.transferUnit as string)
) {
if (this.transferUnit && validUnits.includes(this.transferUnit)) {
return this.transferUnit;
}
@@ -612,19 +609,31 @@ export abstract class InvoiceItem extends Doc {
return { for: ['not in', [itemNotFor]] };
},
transferUnit: async (doc: Doc) => {
if (!doc.item) return {};
batch: async (doc: Doc) => {
const batches = await doc.fyo.db.getAll(ModelNameEnum.Batch, {
fields: ['name'],
filters: { item: doc.item as string },
});
return {
name: ['in', batches.map((b) => b.name as string)],
};
},
transferUnit: async (doc: Doc) => {
const conversionItems = await doc.fyo.db.getAll(
ModelNameEnum.UOMConversionItem,
{
fields: ['uom'],
filters: { parent: doc.item },
filters: { parent: doc.item as string },
}
);
const allUoms = [
doc.unit,
...conversionItems.map((i) => i.uom as string),
];
return {
name: ['in', conversionItems.map((i) => i.uom as string)],
name: ['in', allUoms] as [string, string[]],
};
},
};
+24 -6
View File
@@ -8,11 +8,7 @@ import {
} from 'fyo/model/types';
import { ValidationError } from 'fyo/utils/errors';
import { LedgerPosting } from 'models/Transactional/LedgerPosting';
import {
addItem,
getDocStatusListColumn,
getLedgerLinkAction,
} from 'models/helpers';
import { getDocStatusListColumn, getLedgerLinkAction } from 'models/helpers';
import { ModelNameEnum } from 'models/types';
import { Money } from 'pesa';
import { SerialNumber } from './SerialNumber';
@@ -141,7 +137,29 @@ export class StockMovement extends Transfer {
}
async addItem(name: string) {
return await addItem(name, this);
const itemDoc = await this.fyo.doc.getDoc(ModelNameEnum.Item, name);
if (!itemDoc) {
throw new ValidationError(t`Item ${name} not found`);
}
const item = {
name: itemDoc.name,
batch: itemDoc.defaultBatch ?? null,
};
if (item.batch) {
const batchDoc = await this.fyo.doc.getDoc(
ModelNameEnum.Batch,
item.batch as string
);
if (batchDoc && batchDoc.item !== name) {
throw new ValidationError(
t`Batch ${item.batch as string} does not belong to Item ${name}`
);
}
}
return item;
}
}
+29
View File
@@ -56,6 +56,28 @@ export class StockMovementItem extends TransferItem {
item: () => ({ trackItem: true }),
};
async validate() {
await super.validate();
await this.validateBatchAndItemConsistency();
}
async validateBatchAndItemConsistency() {
if (!this.batch || !this.item) {
return;
}
const batchDoc = await this.fyo.doc.getDoc(ModelNameEnum.Batch, this.batch);
if (!batchDoc) {
return;
}
if (batchDoc.item !== this.item) {
throw new ValidationError(
t`Batch ${this.batch} does not belong to Item ${this.item}`
);
}
}
formulas: FormulaMap = {
rate: {
formula: async () => {
@@ -209,6 +231,13 @@ export class StockMovementItem extends TransferItem {
);
}
},
batch: () => {
if (this.fyo.singles.InventorySettings?.enableBatches && !this.batch) {
throw new ValidationError(
t`Batch is required for Item ${this.item as string}`
);
}
},
transferUnit: async (value: DocValue) => {
if (!this.item) {
return;
+1 -1
View File
@@ -29,6 +29,6 @@
"required": false
}
],
"quickEditFields": ["expiryDate", "manufactureDate"],
"quickEditFields": ["item", "expiryDate", "manufactureDate"],
"keywordFields": ["name"]
}