mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
fix: sync inventory items quantity with services.
This commit is contained in:
@@ -183,7 +183,7 @@ export default class InventoryAdjustmentService {
|
||||
inventoryAdjustmentId: number
|
||||
): Promise<void> {
|
||||
// Retrieve the inventory adjustment or throw not found service error.
|
||||
const adjustment = await this.getInventoryAdjustmentOrThrowError(
|
||||
const oldInventoryAdjustment = await this.getInventoryAdjustmentOrThrowError(
|
||||
tenantId,
|
||||
inventoryAdjustmentId
|
||||
);
|
||||
@@ -208,6 +208,7 @@ export default class InventoryAdjustmentService {
|
||||
await this.eventDispatcher.dispatch(events.inventoryAdjustment.onDeleted, {
|
||||
tenantId,
|
||||
inventoryAdjustmentId,
|
||||
oldInventoryAdjustment
|
||||
});
|
||||
this.logger.info(
|
||||
'[inventory_adjustment] the adjustment deleted successfully.',
|
||||
@@ -317,7 +318,7 @@ export default class InventoryAdjustmentService {
|
||||
});
|
||||
});
|
||||
// Saves the given inventory transactions to the storage.
|
||||
this.inventoryService.recordInventoryTransactions(
|
||||
await this.inventoryService.recordInventoryTransactions(
|
||||
tenantId,
|
||||
inventoryTransactions,
|
||||
override
|
||||
|
||||
92
server/src/services/Inventory/InventoryItemsQuantitySync.ts
Normal file
92
server/src/services/Inventory/InventoryItemsQuantitySync.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { toSafeInteger } from 'lodash';
|
||||
import { IInventoryTransaction, IItemsQuantityChanges } from 'interfaces';
|
||||
import HasTenancyService from 'services/Tenancy/TenancyService';
|
||||
|
||||
/**
|
||||
* Syncs the inventory transactions with inventory items quantity.
|
||||
*/
|
||||
@Service()
|
||||
export default class InventoryItemsQuantitySync {
|
||||
@Inject()
|
||||
tenancy: HasTenancyService;
|
||||
|
||||
/**
|
||||
* Reverse the given inventory transactions.
|
||||
* @param {IInventoryTransaction[]} inventroyTransactions
|
||||
* @return {IInventoryTransaction[]}
|
||||
*/
|
||||
reverseInventoryTransactions(
|
||||
inventroyTransactions: IInventoryTransaction[]
|
||||
): IInventoryTransaction[] {
|
||||
return inventroyTransactions.map((transaction) => ({
|
||||
...transaction,
|
||||
direction: transaction.direction === 'OUT' ? 'IN' : 'OUT',
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses the inventory transactions.
|
||||
* @param {IInventoryTransaction[]} inventroyTransactions -
|
||||
* @return {IItemsQuantityChanges[]}
|
||||
*/
|
||||
getReverseItemsQuantityChanges(
|
||||
inventroyTransactions: IInventoryTransaction[]
|
||||
): IItemsQuantityChanges[] {
|
||||
const reversedTransactions = this.reverseInventoryTransactions(
|
||||
inventroyTransactions
|
||||
);
|
||||
return this.getItemsQuantityChanges(reversedTransactions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the items quantity changes from the given inventory transactions.
|
||||
* @param {IInventoryTransaction[]} inventroyTransactions - Inventory transactions.
|
||||
* @return {IItemsQuantityChanges[]}
|
||||
*/
|
||||
getItemsQuantityChanges(
|
||||
inventroyTransactions: IInventoryTransaction[]
|
||||
): IItemsQuantityChanges[] {
|
||||
const balanceMap: { [itemId: number]: number } = {};
|
||||
|
||||
inventroyTransactions.forEach(
|
||||
(inventoryTransaction: IInventoryTransaction) => {
|
||||
const { itemId, direction, quantity } = inventoryTransaction;
|
||||
|
||||
if (!balanceMap[itemId]) {
|
||||
balanceMap[itemId] = 0;
|
||||
}
|
||||
balanceMap[itemId] += direction === 'IN' ? quantity : 0;
|
||||
balanceMap[itemId] -= direction === 'OUT' ? quantity : 0;
|
||||
}
|
||||
);
|
||||
|
||||
return Object.entries(balanceMap).map(([itemId, balanceChange]) => ({
|
||||
itemId: toSafeInteger(itemId),
|
||||
balanceChange,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the items quantity changes.
|
||||
* @param {IItemsQuantityChanges[]} itemsQuantity - Items quantity changes.
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async changeItemsQuantity(
|
||||
tenantId: number,
|
||||
itemsQuantity: IItemsQuantityChanges[]
|
||||
): Promise<void> {
|
||||
const { itemRepository } = this.tenancy.repositories(tenantId);
|
||||
const opers = [];
|
||||
|
||||
itemsQuantity.forEach((itemQuantity: IItemsQuantityChanges) => {
|
||||
const changeQuantityOper = itemRepository.changeNumber(
|
||||
{ id: itemQuantity.itemId, type: 'inventory' },
|
||||
'quantityOnHand',
|
||||
itemQuantity.balanceChange
|
||||
);
|
||||
opers.push(changeQuantityOper);
|
||||
});
|
||||
await Promise.all(opers);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user