diff --git a/server/src/interfaces/InventoryTransaction.ts b/server/src/interfaces/InventoryTransaction.ts index 34c4e7a66..04b3a5b89 100644 --- a/server/src/interfaces/InventoryTransaction.ts +++ b/server/src/interfaces/InventoryTransaction.ts @@ -10,7 +10,9 @@ export interface IInventoryTransaction { transactionType: string, transactionId: number, lotNumber: string, - entryId: number + entryId: number, + createdAt?: Date, + updatedAt?: Date, }; export interface IInventoryLotCost { @@ -18,11 +20,12 @@ export interface IInventoryLotCost { date: Date, direction: string, itemId: number, + quantity: number, rate: number, remaining: number, cost: number, lotNumber: string|number, transactionType: string, - transactionId: string, + transactionId: number, entryId: number } \ No newline at end of file diff --git a/server/src/services/Inventory/Inventory.ts b/server/src/services/Inventory/Inventory.ts index 739309067..714d001bd 100644 --- a/server/src/services/Inventory/Inventory.ts +++ b/server/src/services/Inventory/Inventory.ts @@ -4,7 +4,7 @@ import { EventDispatcher, EventDispatcherInterface, } from 'decorators/eventDispatcher'; -import { IInventoryTransaction, IItem, IItemEntry } from 'interfaces' +import { IInventoryLotCost, IInventoryTransaction, IItem, IItemEntry } from 'interfaces' import InventoryAverageCost from 'services/Inventory/InventoryAverageCost'; import InventoryCostLotTracker from 'services/Inventory/InventoryCostLotTracker'; import TenancyService from 'services/Tenancy/TenancyService'; @@ -146,7 +146,7 @@ export default class InventoryService { tenantId: number, inventoryEntry: IInventoryTransaction, deleteOld: boolean = false, - ) { + ): Promise { const { InventoryTransaction, Item } = this.tenancy.models(tenantId); if (deleteOld) { @@ -156,7 +156,7 @@ export default class InventoryService { inventoryEntry.transactionType, ); } - await InventoryTransaction.query().insert({ + return InventoryTransaction.query().insert({ ...inventoryEntry, lotNumber: inventoryEntry.lotNumber, }); @@ -182,6 +182,23 @@ export default class InventoryService { .delete(); } + /** + * Records the inventory cost lot transaction. + * @param {number} tenantId + * @param {IInventoryLotCost} inventoryLotEntry + * @return {Promise} + */ + async recordInventoryCostLotTransaction( + tenantId: number, + inventoryLotEntry: IInventoryLotCost, + ): Promise { + const { InventoryCostLotTracker } = this.tenancy.models(tenantId); + + return InventoryCostLotTracker.query().insert({ + ...inventoryLotEntry, + }); + } + /** * Retrieve the lot number after the increment. * @param {number} tenantId - Tenant id. diff --git a/server/src/services/Items/ItemsService.ts b/server/src/services/Items/ItemsService.ts index 646ae465a..79157313a 100644 --- a/server/src/services/Items/ItemsService.ts +++ b/server/src/services/Items/ItemsService.ts @@ -1,4 +1,4 @@ -import { defaultTo, difference } from 'lodash'; +import { defaultTo, difference, omit } from 'lodash'; import { Service, Inject } from 'typedi'; import { EventDispatcher, @@ -283,11 +283,11 @@ export default class ItemsService implements IItemsService { /** * Records the opening items inventory transaction. - * @param {number} tenantId - * @param itemId - * @param openingQuantity - * @param openingCost - * @param openingDate + * @param {number} tenantId - + * @param itemId - + * @param openingQuantity - + * @param openingCost - + * @param openingDate - */ public async recordOpeningItemsInventoryTransaction( tenantId: number, @@ -299,14 +299,24 @@ export default class ItemsService implements IItemsService { // Gets the next inventory lot number. const lotNumber = this.inventoryService.getNextLotNumber(tenantId); - await this.inventoryService.recordInventoryTransaction(tenantId, { - date: openingDate, - quantity: openingQuantity, - rate: openingCost, - direction: 'IN', - transactionType: 'OpeningItem', - itemId, - lotNumber, + // Records the inventory transaction. + const inventoryTransaction = await this.inventoryService.recordInventoryTransaction( + tenantId, + { + date: openingDate, + quantity: openingQuantity, + rate: openingCost, + direction: 'IN', + transactionType: 'OpeningItem', + itemId, + lotNumber, + } + ); + // Records the inventory cost lot transaction. + await this.inventoryService.recordInventoryCostLotTransaction(tenantId, { + ...omit(inventoryTransaction, ['updatedAt', 'createdAt']), + cost: openingQuantity * openingCost, + remaining: 0, }); await this.inventoryService.incrementNextLotNumber(tenantId); }