mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 22: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:
@@ -7,7 +7,7 @@ import {
|
|||||||
useBill,
|
useBill,
|
||||||
useSettings,
|
useSettings,
|
||||||
useCreateBill,
|
useCreateBill,
|
||||||
useEditBill
|
useEditBill,
|
||||||
} from 'hooks/query';
|
} from 'hooks/query';
|
||||||
|
|
||||||
const BillFormContext = createContext();
|
const BillFormContext = createContext();
|
||||||
@@ -26,9 +26,14 @@ function BillFormProvider({ billId, ...props }) {
|
|||||||
} = useVendors({ page_size: 10000 });
|
} = useVendors({ page_size: 10000 });
|
||||||
|
|
||||||
// Filter all purchasable items only.
|
// Filter all purchasable items only.
|
||||||
const stringifiedFilterRoles = React.useMemo(() => JSON.stringify(
|
const stringifiedFilterRoles = React.useMemo(
|
||||||
[{ "fieldKey": "purchasable", "value":true, "condition":"equals"}]
|
() =>
|
||||||
), []);
|
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
|
// Handle fetch Items data table or list
|
||||||
const {
|
const {
|
||||||
@@ -36,7 +41,7 @@ function BillFormProvider({ billId, ...props }) {
|
|||||||
isFetching: isItemsLoading,
|
isFetching: isItemsLoading,
|
||||||
} = useItems({
|
} = useItems({
|
||||||
page_size: 10000,
|
page_size: 10000,
|
||||||
stringified_filter_roles: stringifiedFilterRoles
|
stringified_filter_roles: stringifiedFilterRoles,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle fetch bill details.
|
// Handle fetch bill details.
|
||||||
@@ -72,7 +77,7 @@ function BillFormProvider({ billId, ...props }) {
|
|||||||
|
|
||||||
createBillMutate,
|
createBillMutate,
|
||||||
editBillMutate,
|
editBillMutate,
|
||||||
setSubmitPayload
|
setSubmitPayload,
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -512,9 +512,19 @@ export default class ItemsController extends BaseController {
|
|||||||
errors: [{
|
errors: [{
|
||||||
type: 'TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS',
|
type: 'TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS',
|
||||||
message: 'Cannot change item type to inventory with item has associated 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);
|
next(error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -178,6 +178,10 @@ export default class Item extends TenantModel {
|
|||||||
relation: "items_categories.id",
|
relation: "items_categories.id",
|
||||||
relationColumn: "items_categories.name",
|
relationColumn: "items_categories.name",
|
||||||
},
|
},
|
||||||
|
active: {
|
||||||
|
label: "Active",
|
||||||
|
column: "active",
|
||||||
|
},
|
||||||
// user: {
|
// user: {
|
||||||
// label: 'User',
|
// label: 'User',
|
||||||
// column: 'user_id',
|
// column: 'user_id',
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import {
|
|||||||
ACCOUNT_TYPE,
|
ACCOUNT_TYPE,
|
||||||
} from 'data/AccountTypes';
|
} from 'data/AccountTypes';
|
||||||
import { ERRORS } from './constants';
|
import { ERRORS } from './constants';
|
||||||
|
import { AccountTransaction } from 'models';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class ItemsService implements IItemsService {
|
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.
|
* Creates a new item.
|
||||||
* @param {number} tenantId DTO
|
* @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, {
|
const newItem = await Item.query().patchAndFetchById(itemId, {
|
||||||
...itemModel,
|
...itemModel,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -19,4 +19,5 @@ export const ERRORS = {
|
|||||||
'ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT',
|
'ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT',
|
||||||
ITEM_CANNOT_CHANGE_INVENTORY_TYPE: 'ITEM_CANNOT_CHANGE_INVENTORY_TYPE',
|
ITEM_CANNOT_CHANGE_INVENTORY_TYPE: 'ITEM_CANNOT_CHANGE_INVENTORY_TYPE',
|
||||||
TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS: 'TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS',
|
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