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

refactor: hasBatchNumber validation

This commit is contained in:
akshayitzme 2023-02-17 14:56:42 +05:30
parent 261a3df48d
commit f10399074f
3 changed files with 27 additions and 53 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

@ -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[] {