Merge pull request #1226 from Gadha2311/uomfilter

fix:Uom and Batch filter
This commit is contained in:
Able k Saju
2025-06-24 13:58:28 +05:30
committed by GitHub
4 changed files with 100 additions and 18 deletions
+46 -11
View File
@@ -201,15 +201,31 @@ export abstract class InvoiceItem extends Doc {
},
transferUnit: {
formula: async (fieldname) => {
if (!this.item) {
return;
}
if (fieldname === 'quantity' || fieldname === 'unit') {
return this.unit;
}
return (await this.fyo.getValue(
'Item',
this.item as string,
'unit'
)) as string;
const conversionItems = await this.fyo.db.getAll(
ModelNameEnum.UOMConversionItem,
{
fields: ['uom'],
filters: { parent: this.item },
}
);
if (conversionItems.length) {
return this.unit;
}
const validUnits = conversionItems.map((i) => i.uom);
if (this.transferUnit && validUnits.includes(this.transferUnit)) {
return this.transferUnit;
}
return this.unit;
},
dependsOn: ['item', 'unit'],
},
@@ -622,6 +638,31 @@ export abstract class InvoiceItem extends Doc {
return { for: ['not in', [itemNotFor]] };
},
batch: async (doc: Doc) => {
const batches = await doc.fyo.db.getAll(ModelNameEnum.Batch, {
fields: ['name'],
filters: { item: doc.item as string },
});
const batchName = batches.map((b) => b.name) as string[];
return {
name: ['in', batchName],
};
},
transferUnit: async (doc: Doc) => {
const conversionItems = await doc.fyo.db.getAll(
ModelNameEnum.UOMConversionItem,
{
fields: ['uom'],
filters: { parent: doc.item as string },
}
);
const conversionUoms = conversionItems.map((i) => i.uom) as string[];
return {
name: ['in', conversionUoms],
};
},
};
static createFilters: FiltersMap = {
@@ -815,9 +856,6 @@ function getRate(
const isItemDiscountedTotal = !isItemTaxedTotal;
const discountBeforeTax = !discountAfterTax;
/**
* Rate calculated from itemDiscountedTotal
*/
if (isItemDiscountedTotal && discountBeforeTax && setItemDiscountAmount) {
return itemDiscountedTotal.add(itemDiscountAmount).div(quantity);
}
@@ -838,9 +876,6 @@ function getRate(
);
}
/**
* Rate calculated from itemTaxedTotal
*/
if (isItemTaxedTotal && discountAfterTax) {
return itemTaxedTotal.div(quantity * (1 + totalTaxRate / 100));
}
+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"]
}