mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 13:50:31 +00:00
feat: compute items cost of inventory adjustments transcations.
This commit is contained in:
@@ -39,7 +39,7 @@ export default class InventoryService {
|
||||
direction: TInventoryTransactionDirection,
|
||||
date: Date | string,
|
||||
lotNumber: number
|
||||
) {
|
||||
): IInventoryTransaction[] {
|
||||
return itemEntries.map((entry: IItemEntry) => ({
|
||||
...pick(entry, ['itemId', 'quantity', 'rate']),
|
||||
lotNumber,
|
||||
@@ -150,19 +150,36 @@ export default class InventoryService {
|
||||
*/
|
||||
async recordInventoryTransactions(
|
||||
tenantId: number,
|
||||
inventoryEntries: IInventoryTransaction[],
|
||||
deleteOld: boolean
|
||||
transactions: IInventoryTransaction[],
|
||||
override: boolean = false
|
||||
): Promise<void> {
|
||||
inventoryEntries.forEach(async (entry: IInventoryTransaction) => {
|
||||
await this.recordInventoryTransaction(tenantId, entry, deleteOld);
|
||||
const bulkInsertOpers = [];
|
||||
|
||||
transactions.forEach((transaction: IInventoryTransaction) => {
|
||||
const oper = this.recordInventoryTransaction(
|
||||
tenantId,
|
||||
transaction,
|
||||
override
|
||||
);
|
||||
bulkInsertOpers.push(oper);
|
||||
});
|
||||
const inventoryTransactions = await Promise.all(bulkInsertOpers);
|
||||
|
||||
// Triggers `onInventoryTransactionsCreated` event.
|
||||
this.eventDispatcher.dispatch(
|
||||
events.inventory.onInventoryTransactionsCreated,
|
||||
{
|
||||
tenantId,
|
||||
inventoryTransactions,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the inventory transactiosn on the storage from the given
|
||||
* Writes the inventory transactiosn on the storage from the given
|
||||
* inventory transactions entries.
|
||||
*
|
||||
* @param {number} tenantId -
|
||||
*
|
||||
* @param {number} tenantId -
|
||||
* @param {IInventoryTransaction} inventoryEntry -
|
||||
* @param {boolean} deleteOld -
|
||||
*/
|
||||
@@ -203,7 +220,7 @@ export default class InventoryService {
|
||||
transactionDirection: TInventoryTransactionDirection,
|
||||
override: boolean = false
|
||||
): Promise<void> {
|
||||
// Gets the next inventory lot number.
|
||||
// Retrieve the next inventory lot number.
|
||||
const lotNumber = this.getNextLotNumber(tenantId);
|
||||
|
||||
// Loads the inventory items entries of the given sale invoice.
|
||||
@@ -231,20 +248,6 @@ export default class InventoryService {
|
||||
);
|
||||
// Increment and save the next lot number settings.
|
||||
await this.incrementNextLotNumber(tenantId);
|
||||
|
||||
// Triggers `onInventoryTransactionsCreated` event.
|
||||
this.eventDispatcher.dispatch(
|
||||
events.inventory.onInventoryTransactionsCreated,
|
||||
{
|
||||
tenantId,
|
||||
inventoryEntries,
|
||||
transactionId,
|
||||
transactionType,
|
||||
transactionDate,
|
||||
transactionDirection,
|
||||
override,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -253,7 +256,7 @@ export default class InventoryService {
|
||||
* @param {string} transactionType
|
||||
* @param {number} transactionId
|
||||
* @return {Promise<{
|
||||
* oldInventoryTransactions: IInventoryTransaction[]
|
||||
* oldInventoryTransactions: IInventoryTransaction[]
|
||||
* }>}
|
||||
*/
|
||||
async deleteInventoryTransactions(
|
||||
@@ -261,7 +264,9 @@ export default class InventoryService {
|
||||
transactionId: number,
|
||||
transactionType: string
|
||||
): Promise<{ oldInventoryTransactions: IInventoryTransaction[] }> {
|
||||
const { inventoryTransactionRepository } = this.tenancy.repositories(tenantId);
|
||||
const { inventoryTransactionRepository } = this.tenancy.repositories(
|
||||
tenantId
|
||||
);
|
||||
|
||||
// Retrieve the inventory transactions of the given sale invoice.
|
||||
const oldInventoryTransactions = await inventoryTransactionRepository.find({
|
||||
|
||||
@@ -11,11 +11,13 @@ import {
|
||||
IPaginationMeta,
|
||||
IInventoryAdjustmentsFilter,
|
||||
ISystemUser,
|
||||
IInventoryTransaction,
|
||||
} from 'interfaces';
|
||||
import events from 'subscribers/events';
|
||||
import AccountsService from 'services/Accounts/AccountsService';
|
||||
import ItemsService from 'services/Items/ItemsService';
|
||||
import HasTenancyService from 'services/Tenancy/TenancyService';
|
||||
import InventoryService from './Inventory';
|
||||
|
||||
const ERRORS = {
|
||||
INVENTORY_ADJUSTMENT_NOT_FOUND: 'INVENTORY_ADJUSTMENT_NOT_FOUND',
|
||||
@@ -39,6 +41,9 @@ export default class InventoryAdjustmentService {
|
||||
@EventDispatcher()
|
||||
eventDispatcher: EventDispatcherInterface;
|
||||
|
||||
@Inject()
|
||||
inventoryService: InventoryService;
|
||||
|
||||
/**
|
||||
* Transformes the quick inventory adjustment DTO to model object.
|
||||
* @param {IQuickInventoryAdjustmentDTO} adjustmentDTO -
|
||||
@@ -55,14 +60,15 @@ export default class InventoryAdjustmentService {
|
||||
{
|
||||
index: 1,
|
||||
itemId: adjustmentDTO.itemId,
|
||||
...(['increment', 'decrement'].indexOf(adjustmentDTO.type) !== -1
|
||||
? {
|
||||
quantity: adjustmentDTO.quantity,
|
||||
}
|
||||
: {}),
|
||||
...('increment' === adjustmentDTO.type
|
||||
? {
|
||||
cost: adjustmentDTO.cost,
|
||||
quantity: adjustmentDTO.quantity,
|
||||
cost: adjustmentDTO.cost,
|
||||
}
|
||||
: {}),
|
||||
...('decrement' === adjustmentDTO.type
|
||||
? {
|
||||
quantity: adjustmentDTO.quantity,
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
@@ -138,6 +144,7 @@ export default class InventoryAdjustmentService {
|
||||
quickAdjustmentDTO,
|
||||
authorizedUser
|
||||
);
|
||||
// Saves the inventory adjustment with assocaited entries to the storage.
|
||||
const inventoryAdjustment = await InventoryAdjustment.query().upsertGraph({
|
||||
...invAdjustmentObject,
|
||||
});
|
||||
@@ -146,6 +153,8 @@ export default class InventoryAdjustmentService {
|
||||
events.inventoryAdjustment.onQuickCreated,
|
||||
{
|
||||
tenantId,
|
||||
inventoryAdjustment,
|
||||
inventoryAdjustmentId: inventoryAdjustment.id,
|
||||
}
|
||||
);
|
||||
this.logger.info(
|
||||
@@ -192,6 +201,7 @@ export default class InventoryAdjustmentService {
|
||||
// Triggers `onInventoryAdjustmentDeleted` event.
|
||||
await this.eventDispatcher.dispatch(events.inventoryAdjustment.onDeleted, {
|
||||
tenantId,
|
||||
inventoryAdjustmentId,
|
||||
});
|
||||
this.logger.info(
|
||||
'[inventory_adjustment] the adjustment deleted successfully.',
|
||||
@@ -226,4 +236,61 @@ export default class InventoryAdjustmentService {
|
||||
pagination,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the inventory transactions from the inventory adjustment transaction.
|
||||
* @param {number} tenantId
|
||||
* @param {IInventoryAdjustment} inventoryAdjustment
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async writeInventoryTransactions(
|
||||
tenantId: number,
|
||||
inventoryAdjustment: IInventoryAdjustment,
|
||||
override: boolean = false,
|
||||
): Promise<void> {
|
||||
// Gets the next inventory lot number.
|
||||
const lotNumber = this.inventoryService.getNextLotNumber(tenantId);
|
||||
|
||||
const commonTransaction = {
|
||||
direction: inventoryAdjustment.inventoryDirection,
|
||||
date: inventoryAdjustment.date,
|
||||
transactionType: 'InventoryAdjustment',
|
||||
transactionId: inventoryAdjustment.id,
|
||||
};
|
||||
const inventoryTransactions = [];
|
||||
|
||||
inventoryAdjustment.entries.forEach((entry) => {
|
||||
inventoryTransactions.push({
|
||||
...commonTransaction,
|
||||
itemId: entry.itemId,
|
||||
quantity: entry.quantity,
|
||||
rate: entry.cost,
|
||||
lotNumber,
|
||||
});
|
||||
});
|
||||
// Saves the given inventory transactions to the storage.
|
||||
this.inventoryService.recordInventoryTransactions(
|
||||
tenantId,
|
||||
inventoryTransactions,
|
||||
override
|
||||
);
|
||||
// Increment and save the next lot number settings.
|
||||
await this.inventoryService.incrementNextLotNumber(tenantId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverts the inventory transactions from the inventory adjustment transaction.
|
||||
* @param {number} tenantId
|
||||
* @param {number} inventoryAdjustmentId
|
||||
*/
|
||||
async revertInventoryTransactions(
|
||||
tenantId: number,
|
||||
inventoryAdjustmentId: number
|
||||
): Promise<{ oldInventoryTransactions: IInventoryTransaction[] }> {
|
||||
return this.inventoryService.deleteInventoryTransactions(
|
||||
tenantId,
|
||||
inventoryAdjustmentId,
|
||||
'InventoryAdjustment'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user