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

@@ -7,7 +7,7 @@ import {
useBill,
useSettings,
useCreateBill,
useEditBill
useEditBill,
} from 'hooks/query';
const BillFormContext = createContext();
@@ -26,9 +26,14 @@ function BillFormProvider({ billId, ...props }) {
} = useVendors({ page_size: 10000 });
// Filter all purchasable items only.
const stringifiedFilterRoles = React.useMemo(() => JSON.stringify(
[{ "fieldKey": "purchasable", "value":true, "condition":"equals"}]
), []);
const stringifiedFilterRoles = React.useMemo(
() =>
JSON.stringify([
{ index: 1, fieldKey: 'purchasable', value: true, condition: '&&', comparator: 'equals' },
{ index: 2, fieldKey: 'active', value: true, condition: '&&', comparator: 'equals' },
]),
[],
);
// Handle fetch Items data table or list
const {
@@ -36,7 +41,7 @@ function BillFormProvider({ billId, ...props }) {
isFetching: isItemsLoading,
} = useItems({
page_size: 10000,
stringified_filter_roles: stringifiedFilterRoles
stringified_filter_roles: stringifiedFilterRoles,
});
// Handle fetch bill details.
@@ -72,7 +77,7 @@ function BillFormProvider({ billId, ...props }) {
createBillMutate,
editBillMutate,
setSubmitPayload
setSubmitPayload,
};
return (

View File

@@ -512,9 +512,19 @@ export default class ItemsController extends BaseController {
errors: [{
type: 'TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS',
message: 'Cannot change item type to inventory with item has associated transactions.',
code: 350,
}],
});
}
if (error.errorType === 'INVENTORY_ACCOUNT_CANNOT_MODIFIED') {
return res.status(400).send({
errors: [{
type: 'INVENTORY_ACCOUNT_CANNOT_MODIFIED',
message: 'Cannot change item inventory account while the item has transactions.',
code: 360,
}]
})
}
}
next(error);
}

View File

@@ -178,6 +178,10 @@ export default class Item extends TenantModel {
relation: "items_categories.id",
relationColumn: "items_categories.name",
},
active: {
label: "Active",
column: "active",
},
// user: {
// label: 'User',
// column: 'user_id',

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