2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 14:50:56 +00:00

refactor: removed hasBatchNumber validation

This commit is contained in:
akshayitzme 2023-02-17 15:09:57 +05:30
parent 261a3df48d
commit 7cecf4e8df
3 changed files with 5 additions and 54 deletions

View File

@ -132,23 +132,6 @@ 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
*/
const query = db.knex!(ModelNameEnum.StockLedgerEntry)
.select('item')
.where('item', item);
const value = (await query) as Record<string, string | null>[];
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]];
}
}
}

View File

@ -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<boolean | null> {
return (await this.#demux.callBespoke('itemHasTransactions', item)) as
| boolean
| null;
}
/**
* Internal methods
*/

View File

@ -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,
};
}