2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 03:19:01 +00:00

fix: item-batch validation

This commit is contained in:
akshayitzme 2023-01-18 17:32:26 +05:30
parent fc75a8b536
commit 81d84f4220
3 changed files with 53 additions and 3 deletions

View File

@ -132,6 +132,26 @@ export class BespokeQueries {
`); `);
} }
static async itemHasTransactions(
db: DatabaseCore,
item: string
): Promise<boolean> {
/*
* to check if an item of given name is present in the SLE
* taking the count of items from SLE which has the given item name
* if the count is greater than 0 considers item exists in SLE, returns true
* if the count is lesser than 0 considers item does not exist in SLE, returns false
*/
const query = db.knex!(ModelNameEnum.StockLedgerEntry)
.select('item')
.where('item', item);
const value = (await query) as Record<string, string | null>[];
if (!value) return false;
return true;
}
static async getStockQuantity( static async getStockQuantity(
db: DatabaseCore, db: DatabaseCore,
item: string, item: string,
@ -168,5 +188,3 @@ export class BespokeQueries {
return value[0][Object.keys(value[0])[0]]; return value[0][Object.keys(value[0])[0]];
} }
} }
//

View File

@ -325,6 +325,12 @@ export class DatabaseHandler extends DatabaseBase {
)) as number | null; )) as number | null;
} }
async itemHasTransactions(item: string): Promise<boolean | null> {
return (await this.#demux.callBespoke('itemHasTransactions', item)) as
| boolean
| null;
}
/** /**
* Internal methods * Internal methods
*/ */

View File

@ -20,6 +20,31 @@ export class Item extends Doc {
for?: 'Purchases' | 'Sales' | 'Both'; for?: 'Purchases' | 'Sales' | 'Both';
hasBatchNumber?: boolean; hasBatchNumber?: boolean;
async beforeSync() {
/*
* This code block is to prevent users from changing the value of Has Batch No Checkbox
of the items which already did transactions
* allowing users to change the value of Has Batch No of the items which already did
transactions will result in incorect SLEs
*/
const ifItemHasBatchNumber = await Boolean(
this.fyo.db.get('Item', this.name! || '', 'hasBatchNumber')
);
if (this.hasBatchNumber == ifItemHasBatchNumber) {
return;
}
const isItemExistsInSLE = await this.fyo.db.itemHasTransactions(this.name!);
if (ifItemHasBatchNumber && isItemExistsInSLE) {
throw new ValidationError(
this.fyo.t`Cannot change value of Has Batch No as ${this
.name!} already has transactions against it. `
);
}
}
formulas: FormulaMap = { formulas: FormulaMap = {
incomeAccount: { incomeAccount: {
formula: async () => { formula: async () => {
@ -121,6 +146,7 @@ export class Item extends Doc {
!this.fyo.singles.AccountingSettings?.enableInventory || !this.fyo.singles.AccountingSettings?.enableInventory ||
this.itemType !== 'Product' || this.itemType !== 'Product' ||
(this.inserted && !this.trackItem), (this.inserted && !this.trackItem),
hasBatchNumber: () => !this.trackItem || false,
}; };
readOnly: ReadOnlyMap = { readOnly: ReadOnlyMap = {