mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +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:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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