From f10399074f3385b9a88f02f889cec615227648f6 Mon Sep 17 00:00:00 2001 From: akshayitzme Date: Fri, 17 Feb 2023 14:56:42 +0530 Subject: [PATCH] refactor: hasBatchNumber validation --- backend/database/bespoke.ts | 19 +------------ fyo/core/dbHandler.ts | 10 ++----- models/baseModels/Item/Item.ts | 51 ++++++++++++++++------------------ 3 files changed, 27 insertions(+), 53 deletions(-) diff --git a/backend/database/bespoke.ts b/backend/database/bespoke.ts index 842a137e..87c3ab7b 100644 --- a/backend/database/bespoke.ts +++ b/backend/database/bespoke.ts @@ -132,23 +132,6 @@ export class BespokeQueries { `); } - static async itemHasTransactions( - db: DatabaseCore, - item: string - ): Promise { - /* - * to check if an item of given name is present in the SLE - */ - const query = db.knex!(ModelNameEnum.StockLedgerEntry) - .select('item') - .where('item', item); - - const value = (await query) as Record[]; - - if (!value || value.length == 0) return false; - return true; - } - static async getStockQuantity( db: DatabaseCore, item: string, @@ -184,4 +167,4 @@ export class BespokeQueries { return value[0][Object.keys(value[0])[0]]; } -} \ No newline at end of file +} diff --git a/fyo/core/dbHandler.ts b/fyo/core/dbHandler.ts index b84c61df..0193aea4 100644 --- a/fyo/core/dbHandler.ts +++ b/fyo/core/dbHandler.ts @@ -10,7 +10,7 @@ import { DatabaseBase, DatabaseDemuxBase, GetAllOptions, - QueryFilter + QueryFilter, } from 'utils/db/types'; import { schemaTranslateables } from 'utils/translationHelpers'; import { LanguageMap } from 'utils/types'; @@ -19,7 +19,7 @@ import { DatabaseDemuxConstructor, DocValue, DocValueMap, - RawValueMap + RawValueMap, } from './types'; // Return types of Bespoke Queries @@ -325,12 +325,6 @@ export class DatabaseHandler extends DatabaseBase { )) as number | null; } - async itemHasTransactions(item: string): Promise { - return (await this.#demux.callBespoke('itemHasTransactions', item)) as - | boolean - | null; - } - /** * Internal methods */ diff --git a/models/baseModels/Item/Item.ts b/models/baseModels/Item/Item.ts index 7f50e5ac..5f33e420 100644 --- a/models/baseModels/Item/Item.ts +++ b/models/baseModels/Item/Item.ts @@ -11,6 +11,7 @@ import { ValidationMap, } from 'fyo/model/types'; import { ValidationError } from 'fyo/utils/errors'; +import { ModelNameEnum } from 'models/types'; import { Money } from 'pesa'; import { AccountRootTypeEnum, AccountTypeEnum } from '../Account/types'; @@ -20,33 +21,6 @@ export class Item extends Doc { for?: 'Purchases' | 'Sales' | 'Both'; 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 this.fyo.db.get( - 'Item', - this.name!, - 'hasBatchNumber' - ); - - if (this.hasBatchNumber == ifItemHasBatchNumber.hasBatchNumber) { - return; - } - - const isItemExistsInSLE = await this.fyo.db.itemHasTransactions(this.name!); - - if (isItemExistsInSLE) { - throw new ValidationError( - this.fyo.t`Cannot change value of Has Batch No as ${this - .name!} already has transactions against it. ` - ); - } - } - formulas: FormulaMap = { incomeAccount: { formula: async () => { @@ -102,6 +76,29 @@ export class Item extends Doc { throw new ValidationError(this.fyo.t`Rate can't be negative.`); } }, + hasBatchNumber: async (value: DocValue) => { + const { hasBatchNumber } = await this.fyo.db.get( + 'Item', + this.name!, + 'hasBatchNumber' + ); + + if (hasBatchNumber && value !== hasBatchNumber) { + const itemEntriesInSLE = await this.fyo.db.count( + ModelNameEnum.StockLedgerEntry, + { + filters: { item: this.name! }, + } + ); + + if (itemEntriesInSLE > 0) { + throw new ValidationError( + this.fyo.t`Cannot change value of Has Batch No as Item ${this + .name!} already has transactions against it. ` + ); + } + } + }, }; static getActions(fyo: Fyo): Action[] {