feat(ManualJournals): Auto-increment.

fix(BillPayment): Validate the opened payment bills.
fix(redux): presist redux state.
fix(useRequestQuery): hook.
This commit is contained in:
a.bouhuolia
2021-03-18 14:23:37 +02:00
parent 4e8bdee97a
commit 9ff8e3159d
79 changed files with 1326 additions and 889 deletions

View File

@@ -244,11 +244,11 @@ export default class ItemsService implements IItemsService {
}
/**
* Validate item type in edit item mode, cannot change item inventory type.
* Validate edit item type from inventory to another type that not allowed.
* @param {IItemDTO} itemDTO
* @param {IItem} oldItem
*/
private validateEditItemInventoryType(itemDTO: IItemDTO, oldItem: IItem) {
private validateEditItemFromInventory(itemDTO: IItemDTO, oldItem: IItem) {
if (
itemDTO.type &&
oldItem.type === 'inventory' &&
@@ -258,6 +258,36 @@ export default class ItemsService implements IItemsService {
}
}
/**
* Validates edit item type from service/non-inventory to inventory.
* Should item has no any relations with accounts transactions.
* @param {number} tenantId - Tenant id.
* @param {number} itemId - Item id.
*/
private async validateEditItemTypeToInventory(
tenantId: number,
oldItem: IItem,
newItemDTO: IItemDTO
) {
const { AccountTransaction } = this.tenancy.models(tenantId);
// We have no problem in case the item type not modified.
if (newItemDTO.type === oldItem.type || oldItem.type === 'inventory') {
return;
}
// Retrieve all transactions that associated to the given item id.
const itemTransactionsCount = await AccountTransaction.query()
.where('item_id', oldItem.id)
.count('item_id', { as: 'transactions' })
.first();
if (itemTransactionsCount.transactions > 0) {
throw new ServiceError(
ERRORS.TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS
);
}
}
/**
* Creates a new item.
* @param {number} tenantId DTO
@@ -319,7 +349,11 @@ export default class ItemsService implements IItemsService {
// Validates the given item existance on the storage.
const oldItem = await this.getItemOrThrowError(tenantId, itemId);
this.validateEditItemInventoryType(itemDTO, oldItem);
// Validate edit item type from inventory type.
this.validateEditItemFromInventory(itemDTO, oldItem);
// Validate edit item type to inventory type.
await this.validateEditItemTypeToInventory(tenantId, oldItem, itemDTO);
// Transform the edit item DTO to model.
const itemModel = this.transformEditItemDTOToModel(itemDTO, oldItem);
@@ -580,8 +614,7 @@ export default class ItemsService implements IItemsService {
const { ItemEntry } = this.tenancy.models(tenantId);
const ids = Array.isArray(itemId) ? itemId : [itemId];
const foundItemEntries = await ItemEntry.query()
.whereIn('item_id', ids);
const foundItemEntries = await ItemEntry.query().whereIn('item_id', ids);
if (foundItemEntries.length > 0) {
throw new ServiceError(

View File

@@ -18,4 +18,5 @@ export const ERRORS = {
ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT:
'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',
};