mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
96 lines
2.9 KiB
TypeScript
96 lines
2.9 KiB
TypeScript
import { Inject, Service } from 'typedi';
|
|
import { toSafeInteger } from 'lodash';
|
|
import { IInventoryTransaction, IItemsQuantityChanges } from '@/interfaces';
|
|
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
import Knex from 'knex';
|
|
|
|
/**
|
|
* 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[],
|
|
trx?: Knex.Transaction
|
|
): 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,
|
|
trx
|
|
);
|
|
opers.push(changeQuantityOper);
|
|
});
|
|
await Promise.all(opers);
|
|
}
|
|
}
|