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:
a.bouhuolia
2021-03-24 11:10:58 +02:00
parent 96f20bf51b
commit 8646f3dcf9
5 changed files with 64 additions and 6 deletions

View File

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

View File

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