From 7cecf4e8dfcce593153f2eb7cf98c177858b6782 Mon Sep 17 00:00:00 2001 From: akshayitzme Date: Fri, 17 Feb 2023 15:09:57 +0530 Subject: [PATCH] refactor: removed hasBatchNumber validation --- backend/database/bespoke.ts | 19 +------------------ fyo/core/dbHandler.ts | 10 ++-------- models/baseModels/Item/Item.ts | 30 ++---------------------------- 3 files changed, 5 insertions(+), 54 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..ce609582 100644 --- a/models/baseModels/Item/Item.ts +++ b/models/baseModels/Item/Item.ts @@ -20,33 +20,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 () => { @@ -148,7 +121,7 @@ export class Item extends Doc { !this.fyo.singles.AccountingSettings?.enableInventory || this.itemType !== 'Product' || (this.inserted && !this.trackItem), - hasBatchNumber: () => !this.trackItem || false, + hasBatchNumber: () => !this.trackItem, barcode: () => !this.fyo.singles.InventorySettings?.enableBarcodes, }; @@ -156,5 +129,6 @@ export class Item extends Doc { unit: () => this.inserted, itemType: () => this.inserted, trackItem: () => this.inserted, + hasBatchNumber: () => this.inserted, }; }