mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
fix(Items): Not display inactive items in sales and purchases.
fix(item): Prevent edit inventory account of item once item has transactions.
This commit is contained in:
@@ -22,6 +22,7 @@ import {
|
||||
ACCOUNT_TYPE,
|
||||
} from 'data/AccountTypes';
|
||||
import { ERRORS } from './constants';
|
||||
import { AccountTransaction } from 'models';
|
||||
|
||||
@Service()
|
||||
export default class ItemsService implements IItemsService {
|
||||
@@ -288,6 +289,37 @@ export default class ItemsService implements IItemsService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the item inventory account whether modified and item
|
||||
* has assocaited inventory transactions.
|
||||
* @param {numnber} tenantId
|
||||
* @param {IItem} oldItem
|
||||
* @param {IItemDTO} newItemDTO
|
||||
* @returns
|
||||
*/
|
||||
async validateItemInvnetoryAccountModified(
|
||||
tenantId: number,
|
||||
oldItem: IItem,
|
||||
newItemDTO: IItemDTO
|
||||
) {
|
||||
const { AccountTransaction } = this.tenancy.models(tenantId);
|
||||
|
||||
if (
|
||||
newItemDTO.type !== 'inventory' ||
|
||||
oldItem.inventoryAccountId === newItemDTO.inventoryAccountId
|
||||
) {
|
||||
return;
|
||||
}
|
||||
// Inventory transactions associated to the given item id.
|
||||
const transactions = await AccountTransaction.query().where({
|
||||
itemId: oldItem.id,
|
||||
});
|
||||
// Throw the service error in case item has associated inventory transactions.
|
||||
if (transactions.length > 0) {
|
||||
throw new ServiceError(ERRORS.INVENTORY_ACCOUNT_CANNOT_MODIFIED);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new item.
|
||||
* @param {number} tenantId DTO
|
||||
@@ -387,6 +419,12 @@ export default class ItemsService implements IItemsService {
|
||||
);
|
||||
}
|
||||
|
||||
await this.validateItemInvnetoryAccountModified(
|
||||
tenantId,
|
||||
oldItem,
|
||||
itemDTO
|
||||
);
|
||||
|
||||
const newItem = await Item.query().patchAndFetchById(itemId, {
|
||||
...itemModel,
|
||||
});
|
||||
|
||||
@@ -19,4 +19,5 @@ export const ERRORS = {
|
||||
'ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT',
|
||||
ITEM_CANNOT_CHANGE_INVENTORY_TYPE: 'ITEM_CANNOT_CHANGE_INVENTORY_TYPE',
|
||||
TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS: 'TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS',
|
||||
INVENTORY_ACCOUNT_CANNOT_MODIFIED: 'INVENTORY_ACCOUNT_CANNOT_MODIFIED'
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user