mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 13:50:31 +00:00
refactor: inventory adjustments to GL
This commit is contained in:
@@ -1,226 +0,0 @@
|
||||
// import { Service, Inject } from 'typedi';
|
||||
// import { Knex } from 'knex';
|
||||
// import * as R from 'ramda';
|
||||
// import TenancyService from '@/services/Tenancy/TenancyService';
|
||||
// import {
|
||||
// AccountNormal,
|
||||
// IInventoryAdjustment,
|
||||
// IInventoryAdjustmentEntry,
|
||||
// ILedgerEntry,
|
||||
// } from '@/interfaces';
|
||||
// import Ledger from '@/services/Accounting/Ledger';
|
||||
// import LedgerStorageService from '@/services/Accounting/LedgerStorageService';
|
||||
// import { TenantMetadata } from '@/system/models';
|
||||
|
||||
// @Service()
|
||||
// export default class InventoryAdjustmentsGL {
|
||||
// @Inject()
|
||||
// private tenancy: TenancyService;
|
||||
|
||||
// @Inject()
|
||||
// private ledgerStorage: LedgerStorageService;
|
||||
|
||||
// /**
|
||||
// * Retrieves the inventory adjustment common GL entry.
|
||||
// * @param {InventoryAdjustment} inventoryAdjustment -
|
||||
// * @param {string} baseCurrency -
|
||||
// * @returns {ILedgerEntry}
|
||||
// */
|
||||
// private getAdjustmentGLCommonEntry = (
|
||||
// inventoryAdjustment: IInventoryAdjustment,
|
||||
// baseCurrency: string
|
||||
// ) => {
|
||||
// return {
|
||||
// currencyCode: baseCurrency,
|
||||
// exchangeRate: 1,
|
||||
|
||||
// transactionId: inventoryAdjustment.id,
|
||||
// transactionType: 'InventoryAdjustment',
|
||||
// referenceNumber: inventoryAdjustment.referenceNo,
|
||||
|
||||
// date: inventoryAdjustment.date,
|
||||
|
||||
// userId: inventoryAdjustment.userId,
|
||||
// branchId: inventoryAdjustment.branchId,
|
||||
|
||||
// createdAt: inventoryAdjustment.createdAt,
|
||||
|
||||
// credit: 0,
|
||||
// debit: 0,
|
||||
// };
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Retrieve the inventory adjustment inventory GL entry.
|
||||
// * @param {IInventoryAdjustment} inventoryAdjustment -Inventory adjustment model.
|
||||
// * @param {string} baseCurrency - Base currency of the organization.
|
||||
// * @param {IInventoryAdjustmentEntry} entry -
|
||||
// * @param {number} index -
|
||||
// * @returns {ILedgerEntry}
|
||||
// */
|
||||
// private getAdjustmentGLInventoryEntry = R.curry(
|
||||
// (
|
||||
// inventoryAdjustment: IInventoryAdjustment,
|
||||
// baseCurrency: string,
|
||||
// entry: IInventoryAdjustmentEntry,
|
||||
// index: number
|
||||
// ): ILedgerEntry => {
|
||||
// const commonEntry = this.getAdjustmentGLCommonEntry(
|
||||
// inventoryAdjustment,
|
||||
// baseCurrency
|
||||
// );
|
||||
// const amount = entry.cost * entry.quantity;
|
||||
|
||||
// return {
|
||||
// ...commonEntry,
|
||||
// debit: amount,
|
||||
// accountId: entry.item.inventoryAccountId,
|
||||
// accountNormal: AccountNormal.DEBIT,
|
||||
// index,
|
||||
// };
|
||||
// }
|
||||
// );
|
||||
|
||||
// /**
|
||||
// * Retrieves the inventory adjustment
|
||||
// * @param {IInventoryAdjustment} inventoryAdjustment
|
||||
// * @param {IInventoryAdjustmentEntry} entry
|
||||
// * @returns {ILedgerEntry}
|
||||
// */
|
||||
// private getAdjustmentGLCostEntry = R.curry(
|
||||
// (
|
||||
// inventoryAdjustment: IInventoryAdjustment,
|
||||
// baseCurrency: string,
|
||||
// entry: IInventoryAdjustmentEntry,
|
||||
// index: number
|
||||
// ): ILedgerEntry => {
|
||||
// const commonEntry = this.getAdjustmentGLCommonEntry(
|
||||
// inventoryAdjustment,
|
||||
// baseCurrency
|
||||
// );
|
||||
// const amount = entry.cost * entry.quantity;
|
||||
|
||||
// return {
|
||||
// ...commonEntry,
|
||||
// accountId: inventoryAdjustment.adjustmentAccountId,
|
||||
// accountNormal: AccountNormal.DEBIT,
|
||||
// credit: amount,
|
||||
// index: index + 2,
|
||||
// };
|
||||
// }
|
||||
// );
|
||||
|
||||
// /**
|
||||
// * Retrieve the inventory adjustment GL item entry.
|
||||
// * @param {InventoryAdjustment} adjustment
|
||||
// * @param {string} baseCurrency
|
||||
// * @param {InventoryAdjustmentEntry} entry
|
||||
// * @param {number} index
|
||||
// * @returns {}
|
||||
// */
|
||||
// private getAdjustmentGLItemEntry = R.curry(
|
||||
// (
|
||||
// adjustment: IInventoryAdjustment,
|
||||
// baseCurrency: string,
|
||||
// entry: IInventoryAdjustmentEntry,
|
||||
// index: number
|
||||
// ): ILedgerEntry[] => {
|
||||
// const getInventoryEntry = this.getAdjustmentGLInventoryEntry(
|
||||
// adjustment,
|
||||
// baseCurrency
|
||||
// );
|
||||
// const inventoryEntry = getInventoryEntry(entry, index);
|
||||
// const costEntry = this.getAdjustmentGLCostEntry(
|
||||
// adjustment,
|
||||
// baseCurrency,
|
||||
// entry,
|
||||
// index
|
||||
// );
|
||||
// return [inventoryEntry, costEntry];
|
||||
// }
|
||||
// );
|
||||
|
||||
// /**
|
||||
// * Writes increment inventroy adjustment GL entries.
|
||||
// * @param {InventoryAdjustment} inventoryAdjustment -
|
||||
// * @param {JournalPoster} jorunal -
|
||||
// * @returns {ILedgerEntry[]}
|
||||
// */
|
||||
// public getIncrementAdjustmentGLEntries(
|
||||
// inventoryAdjustment: IInventoryAdjustment,
|
||||
// baseCurrency: string
|
||||
// ): ILedgerEntry[] {
|
||||
// const getItemEntry = this.getAdjustmentGLItemEntry(
|
||||
// inventoryAdjustment,
|
||||
// baseCurrency
|
||||
// );
|
||||
// return inventoryAdjustment.entries.map(getItemEntry).flat();
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Writes inventory increment adjustment GL entries.
|
||||
// * @param {number} tenantId
|
||||
// * @param {number} inventoryAdjustmentId
|
||||
// */
|
||||
// public writeAdjustmentGLEntries = async (
|
||||
// tenantId: number,
|
||||
// inventoryAdjustmentId: number,
|
||||
// trx?: Knex.Transaction
|
||||
// ): Promise<void> => {
|
||||
// const { InventoryAdjustment } = this.tenancy.models(tenantId);
|
||||
|
||||
// // Retrieves the inventory adjustment with associated entries.
|
||||
// const adjustment = await InventoryAdjustment.query(trx)
|
||||
// .findById(inventoryAdjustmentId)
|
||||
// .withGraphFetched('entries.item');
|
||||
|
||||
// const tenantMeta = await TenantMetadata.query().findOne({ tenantId });
|
||||
|
||||
// // Retrieves the inventory adjustment GL entries.
|
||||
// const entries = this.getIncrementAdjustmentGLEntries(
|
||||
// adjustment,
|
||||
// tenantMeta.baseCurrency
|
||||
// );
|
||||
// const ledger = new Ledger(entries);
|
||||
|
||||
// // Commits the ledger entries to the storage.
|
||||
// await this.ledgerStorage.commit(tenantId, ledger, trx);
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Reverts the adjustment transactions GL entries.
|
||||
// * @param {number} tenantId
|
||||
// * @param {number} inventoryAdjustmentId
|
||||
// * @returns {Promise<void>}
|
||||
// */
|
||||
// public revertAdjustmentGLEntries = (
|
||||
// tenantId: number,
|
||||
// inventoryAdjustmentId: number,
|
||||
// trx?: Knex.Transaction
|
||||
// ): Promise<void> => {
|
||||
// return this.ledgerStorage.deleteByReference(
|
||||
// tenantId,
|
||||
// inventoryAdjustmentId,
|
||||
// 'InventoryAdjustment',
|
||||
// trx
|
||||
// );
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Rewrite inventory adjustment GL entries.
|
||||
// * @param {number} tenantId
|
||||
// * @param {number} inventoryAdjustmentId
|
||||
// * @param {Knex.Transaction} trx
|
||||
// */
|
||||
// public rewriteAdjustmentGLEntries = async (
|
||||
// tenantId: number,
|
||||
// inventoryAdjustmentId: number,
|
||||
// trx?: Knex.Transaction
|
||||
// ) => {
|
||||
// // Reverts GL entries of the given inventory adjustment.
|
||||
// await this.revertAdjustmentGLEntries(tenantId, inventoryAdjustmentId, trx);
|
||||
|
||||
// // Writes GL entries of th egiven inventory adjustment.
|
||||
// await this.writeAdjustmentGLEntries(tenantId, inventoryAdjustmentId, trx);
|
||||
// };
|
||||
// }
|
||||
@@ -5,25 +5,30 @@ import { InventoryAdjustmentEntry } from './models/InventoryAdjustmentEntry';
|
||||
import { CreateQuickInventoryAdjustmentService } from './commands/CreateQuickInventoryAdjustment.service';
|
||||
import { PublishInventoryAdjustmentService } from './commands/PublishInventoryAdjustment.service';
|
||||
import { GetInventoryAdjustmentService } from './queries/GetInventoryAdjustment.service';
|
||||
import { GetInventoryAdjustmentsService } from './queries/GetInventoryAdjustments.service';
|
||||
// import { GetInventoryAdjustmentsService } from './queries/GetInventoryAdjustments.service';
|
||||
import { DeleteInventoryAdjustmentService } from './commands/DeleteInventoryAdjustment.service';
|
||||
import { InventoryAdjustmentsApplicationService } from './InventoryAdjustmentsApplication.service';
|
||||
import { InventoryAdjustmentsController } from './InventoryAdjustments.controller';
|
||||
import { BranchesModule } from '../Branches/Branches.module';
|
||||
import { WarehousesModule } from '../Warehouses/Warehouses.module';
|
||||
import { InventoryAdjustmentsGLSubscriber } from './subscribers/InventoryAdjustmentGL.subscriber';
|
||||
|
||||
const models = [
|
||||
RegisterTenancyModel(InventoryAdjustment),
|
||||
RegisterTenancyModel(InventoryAdjustmentEntry),
|
||||
];
|
||||
@Module({
|
||||
imports: [BranchesModule, WarehousesModule],
|
||||
controllers: [InventoryAdjustmentsController],
|
||||
providers: [
|
||||
...models,
|
||||
CreateQuickInventoryAdjustmentService,
|
||||
PublishInventoryAdjustmentService,
|
||||
GetInventoryAdjustmentsService,
|
||||
// GetInventoryAdjustmentsService,
|
||||
GetInventoryAdjustmentService,
|
||||
DeleteInventoryAdjustmentService,
|
||||
InventoryAdjustmentsApplicationService,
|
||||
InventoryAdjustmentsGLSubscriber,
|
||||
],
|
||||
exports: [...models],
|
||||
})
|
||||
|
||||
@@ -4,6 +4,7 @@ import { PublishInventoryAdjustmentService } from './commands/PublishInventoryAd
|
||||
import { CreateQuickInventoryAdjustmentService } from './commands/CreateQuickInventoryAdjustment.service';
|
||||
import { IQuickInventoryAdjustmentDTO } from './types/InventoryAdjustments.types';
|
||||
import { InventoryAdjustment } from './models/InventoryAdjustment';
|
||||
import { GetInventoryAdjustmentService } from './queries/GetInventoryAdjustment.service';
|
||||
|
||||
@Injectable()
|
||||
export class InventoryAdjustmentsApplicationService {
|
||||
@@ -11,8 +12,22 @@ export class InventoryAdjustmentsApplicationService {
|
||||
private readonly createQuickInventoryAdjustmentService: CreateQuickInventoryAdjustmentService,
|
||||
private readonly deleteInventoryAdjustmentService: DeleteInventoryAdjustmentService,
|
||||
private readonly publishInventoryAdjustmentService: PublishInventoryAdjustmentService,
|
||||
private readonly getInventoryAdjustmentService: GetInventoryAdjustmentService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieves the inventory adjustment transaction.
|
||||
* @param {number} inventoryAdjustmentId - Inventory adjustment id.
|
||||
* @returns {Promise<InventoryAdjustment>}
|
||||
*/
|
||||
public async getInventoryAdjustment(
|
||||
inventoryAdjustmentId: number,
|
||||
): Promise<InventoryAdjustment> {
|
||||
return this.getInventoryAdjustmentService.getInventoryAdjustment(
|
||||
inventoryAdjustmentId,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a quick inventory adjustment transaction.
|
||||
* @param {IQuickInventoryAdjustmentDTO} quickAdjustmentDTO - Quick inventory adjustment DTO.
|
||||
|
||||
@@ -17,21 +17,20 @@ import { Item } from '@/modules/Items/models/Item';
|
||||
import { Account } from '@/modules/Accounts/models/Account.model';
|
||||
import { BranchTransactionDTOTransformer } from '@/modules/Branches/integrations/BranchTransactionDTOTransform';
|
||||
import { WarehouseTransactionDTOTransform } from '@/modules/Warehouses/Integrations/WarehouseTransactionDTOTransform';
|
||||
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
|
||||
|
||||
export class CreateQuickInventoryAdjustmentService {
|
||||
constructor(
|
||||
@Inject(InventoryAdjustment.name)
|
||||
private readonly inventoryAdjustmentModel: typeof InventoryAdjustment,
|
||||
|
||||
@Inject(InventoryAdjustmentEntry.name)
|
||||
private readonly inventoryAdjustmentEntryModel: typeof InventoryAdjustmentEntry,
|
||||
|
||||
|
||||
@Inject(Item.name)
|
||||
private readonly itemModel: typeof Item,
|
||||
|
||||
|
||||
@Inject(Account.name)
|
||||
private readonly accountModel: typeof Account,
|
||||
|
||||
|
||||
private readonly tenancyContext: TenancyContext,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly warehouseDTOTransform: WarehouseTransactionDTOTransform,
|
||||
@@ -43,9 +42,10 @@ export class CreateQuickInventoryAdjustmentService {
|
||||
* @param {IQuickInventoryAdjustmentDTO} adjustmentDTO -
|
||||
* @return {IInventoryAdjustment}
|
||||
*/
|
||||
private transformQuickAdjToModel(
|
||||
private async transformQuickAdjToModel(
|
||||
adjustmentDTO: IQuickInventoryAdjustmentDTO,
|
||||
): InventoryAdjustment {
|
||||
): Promise<InventoryAdjustment> {
|
||||
const authorizedUser = await this.tenancyContext.getSystemUser();
|
||||
const entries = [
|
||||
{
|
||||
index: 1,
|
||||
@@ -104,7 +104,8 @@ export class CreateQuickInventoryAdjustmentService {
|
||||
|
||||
// Transform the DTO to inventory adjustment model.
|
||||
const invAdjustmentObject =
|
||||
this.transformQuickAdjToModel(quickAdjustmentDTO);
|
||||
await this.transformQuickAdjToModel(quickAdjustmentDTO);
|
||||
|
||||
// Writes inventory adjustment transaction with associated transactions
|
||||
// under unit-of-work envirment.
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
|
||||
@@ -18,9 +18,6 @@ export class PublishInventoryAdjustmentService {
|
||||
|
||||
@Inject(InventoryAdjustment.name)
|
||||
private readonly inventoryAdjustmentModel: typeof InventoryAdjustment,
|
||||
|
||||
@Inject(InventoryAdjustmentEntry.name)
|
||||
private readonly inventoryAdjustmentEntryModel: typeof InventoryAdjustmentEntry,
|
||||
) {}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
import * as R from 'ramda';
|
||||
import { InventoryAdjustment } from '../../models/InventoryAdjustment';
|
||||
import { InventoryAdjustmentEntry } from '../../models/InventoryAdjustmentEntry';
|
||||
import { ILedgerEntry } from '../../../Ledger/types/Ledger.types';
|
||||
import { AccountNormal } from '@/interfaces/Account';
|
||||
import { Ledger } from '../../../Ledger/Ledger';
|
||||
|
||||
export class InventoryAdjustmentsGL {
|
||||
private inventoryAdjustment: InventoryAdjustment;
|
||||
private baseCurrency: string;
|
||||
|
||||
constructor(inventoryAdjustmentModel: InventoryAdjustment) {
|
||||
this.inventoryAdjustment = inventoryAdjustmentModel;
|
||||
}
|
||||
|
||||
setBaseCurrency(baseCurrency: string) {
|
||||
this.baseCurrency = baseCurrency;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the inventory adjustment common GL entry.
|
||||
* @returns {ILedgerEntry}
|
||||
*/
|
||||
private get adjustmentGLCommonEntry() {
|
||||
return {
|
||||
currencyCode: this.baseCurrency,
|
||||
exchangeRate: 1,
|
||||
|
||||
transactionId: this.inventoryAdjustment.id,
|
||||
transactionType: 'InventoryAdjustment',
|
||||
referenceNumber: this.inventoryAdjustment.referenceNo,
|
||||
|
||||
date: this.inventoryAdjustment.date,
|
||||
|
||||
userId: this.inventoryAdjustment.userId,
|
||||
branchId: this.inventoryAdjustment.branchId,
|
||||
|
||||
createdAt: this.inventoryAdjustment.createdAt,
|
||||
|
||||
credit: 0,
|
||||
debit: 0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the inventory adjustment inventory GL entry.
|
||||
* @param {InventoryAdjustmentEntry} entry - Inventory adjustment entry.
|
||||
* @param {number} index - Entry index.
|
||||
* @returns {ILedgerEntry}
|
||||
*/
|
||||
private getAdjustmentGLInventoryEntry = R.curry(
|
||||
(entry: InventoryAdjustmentEntry, index: number): ILedgerEntry => {
|
||||
const commonEntry = this.adjustmentGLCommonEntry;
|
||||
const amount = entry.cost * entry.quantity;
|
||||
|
||||
return {
|
||||
...commonEntry,
|
||||
debit: amount,
|
||||
accountId: entry.item.inventoryAccountId,
|
||||
accountNormal: AccountNormal.DEBIT,
|
||||
index,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
/**
|
||||
* Retrieves the inventory adjustment
|
||||
* @param {IInventoryAdjustment} inventoryAdjustment
|
||||
* @param {IInventoryAdjustmentEntry} entry
|
||||
* @returns {ILedgerEntry}
|
||||
*/
|
||||
private getAdjustmentGLCostEntry(
|
||||
entry: InventoryAdjustmentEntry,
|
||||
index: number,
|
||||
): ILedgerEntry {
|
||||
const commonEntry = this.adjustmentGLCommonEntry;
|
||||
const amount = entry.cost * entry.quantity;
|
||||
|
||||
return {
|
||||
...commonEntry,
|
||||
accountId: this.inventoryAdjustment.adjustmentAccountId,
|
||||
accountNormal: AccountNormal.DEBIT,
|
||||
credit: amount,
|
||||
index: index + 2,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the inventory adjustment GL item entry.
|
||||
* @param {InventoryAdjustmentEntry} entry - Inventory adjustment entry.
|
||||
* @param {number} index - Entry index.
|
||||
* @returns {ILedgerEntry[]}
|
||||
*/
|
||||
private getAdjustmentGLItemEntry(
|
||||
entry: InventoryAdjustmentEntry,
|
||||
index: number,
|
||||
): ILedgerEntry[] {
|
||||
const getInventoryEntry = this.getAdjustmentGLInventoryEntry();
|
||||
const inventoryEntry = getInventoryEntry(entry, index);
|
||||
const costEntry = this.getAdjustmentGLCostEntry(entry, index);
|
||||
|
||||
return [inventoryEntry, costEntry];
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes increment inventroy adjustment GL entries.
|
||||
* @param {InventoryAdjustment} inventoryAdjustment -
|
||||
* @param {JournalPoster} jorunal -
|
||||
* @returns {ILedgerEntry[]}
|
||||
*/
|
||||
public getIncrementAdjustmentGLEntries(): ILedgerEntry[] {
|
||||
return this.inventoryAdjustment.entries
|
||||
.map((entry, index) => this.getAdjustmentGLItemEntry(entry, index))
|
||||
.flat();
|
||||
}
|
||||
|
||||
public getAdjustmentGL(): Ledger {
|
||||
const entries = this.getIncrementAdjustmentGLEntries();
|
||||
|
||||
return new Ledger(entries);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Inject } from '@nestjs/common';
|
||||
import { LedgerStorageService } from '../../../Ledger/LedgerStorage.service';
|
||||
import { InventoryAdjustment } from '../../models/InventoryAdjustment';
|
||||
import { TenancyContext } from '../../../Tenancy/TenancyContext.service';
|
||||
import { InventoryAdjustmentsGL } from './InventoryAdjustmentGL';
|
||||
|
||||
export class InventoryAdjustmentsGLEntries {
|
||||
constructor(
|
||||
private readonly ledgerStorage: LedgerStorageService,
|
||||
private readonly tenancyContext: TenancyContext,
|
||||
|
||||
@Inject(InventoryAdjustment.name)
|
||||
private readonly inventoryAdjustment: typeof InventoryAdjustment,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Writes inventory increment adjustment GL entries.
|
||||
* @param {number} inventoryAdjustmentId - Inventory adjustment ID.
|
||||
* @param {Knex.Transaction} trx - Knex transaction.
|
||||
*/
|
||||
public writeAdjustmentGLEntries = async (
|
||||
inventoryAdjustmentId: number,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> => {
|
||||
// Retrieves the inventory adjustment with associated entries.
|
||||
const adjustment = await this.inventoryAdjustment.query(trx)
|
||||
.findById(inventoryAdjustmentId)
|
||||
.withGraphFetched('entries.item');
|
||||
|
||||
const tenantMeta = await this.tenancyContext.getTenantMetadata();
|
||||
|
||||
// Retrieves the inventory adjustment GL entries.
|
||||
const ledger = new InventoryAdjustmentsGL(adjustment)
|
||||
.setBaseCurrency(tenantMeta.baseCurrency)
|
||||
.getAdjustmentGL();
|
||||
|
||||
// Commits the ledger entries to the storage.
|
||||
await this.ledgerStorage.commit(ledger, trx);
|
||||
};
|
||||
|
||||
/**
|
||||
* Reverts the adjustment transactions GL entries.
|
||||
* @param {number} tenantId
|
||||
* @param {number} inventoryAdjustmentId
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public revertAdjustmentGLEntries = (
|
||||
inventoryAdjustmentId: number,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> => {
|
||||
return this.ledgerStorage.deleteByReference(
|
||||
inventoryAdjustmentId,
|
||||
'InventoryAdjustment',
|
||||
trx,
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Rewrite inventory adjustment GL entries.
|
||||
* @param {number} tenantId
|
||||
* @param {number} inventoryAdjustmentId
|
||||
* @param {Knex.Transaction} trx
|
||||
*/
|
||||
public rewriteAdjustmentGLEntries = async (
|
||||
inventoryAdjustmentId: number,
|
||||
trx?: Knex.Transaction,
|
||||
) => {
|
||||
// Reverts GL entries of the given inventory adjustment.
|
||||
await this.revertAdjustmentGLEntries(inventoryAdjustmentId, trx);
|
||||
|
||||
// Writes GL entries of th egiven inventory adjustment.
|
||||
await this.writeAdjustmentGLEntries(inventoryAdjustmentId, trx);
|
||||
};
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { Model, mixin } from 'objection';
|
||||
// import InventoryAdjustmentSettings from './InventoryAdjustment.Settings';
|
||||
// import ModelSetting from './ModelSetting';
|
||||
import { BaseModel } from '@/models/Model';
|
||||
import { InventoryAdjustmentEntry } from './InventoryAdjustmentEntry';
|
||||
|
||||
export class InventoryAdjustment extends BaseModel {
|
||||
date!: string;
|
||||
@@ -17,6 +18,10 @@ export class InventoryAdjustment extends BaseModel {
|
||||
branchId!: number;
|
||||
warehouseId!: number;
|
||||
|
||||
createdAt!: Date | string;
|
||||
|
||||
entries: InventoryAdjustmentEntry[];
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
|
||||
@@ -1,8 +1,18 @@
|
||||
import { Model } from 'objection';
|
||||
import { BaseModel } from '@/models/Model';
|
||||
import { Item } from '@/modules/Items/models/Item';
|
||||
// import TenantModel from 'models/TenantModel';
|
||||
|
||||
export class InventoryAdjustmentEntry extends BaseModel {
|
||||
adjustmentId!: number;
|
||||
index!: number;
|
||||
itemId!: number;
|
||||
quantity!: number;
|
||||
cost!: number;
|
||||
value!: number;
|
||||
|
||||
item!: Item;
|
||||
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
|
||||
@@ -18,10 +18,8 @@ export class GetInventoryAdjustmentService {
|
||||
const inventoryAdjustment = await InventoryAdjustment.query()
|
||||
.findById(inventoryAdjustmentId)
|
||||
.withGraphFetched('entries.item')
|
||||
.withGraphFetched('adjustmentAccount');
|
||||
|
||||
// Throw not found if the given adjustment transaction not exists.
|
||||
this.throwIfAdjustmentNotFound(inventoryAdjustment);
|
||||
.withGraphFetched('adjustmentAccount')
|
||||
.throwIfNotFound();
|
||||
|
||||
return this.transformer.transform(
|
||||
inventoryAdjustment,
|
||||
|
||||
@@ -1,56 +1,56 @@
|
||||
import { InventoryAdjustmentTransformer } from "../InventoryAdjustmentTransformer";
|
||||
import { InventoryAdjustment } from "../models/InventoryAdjustment";
|
||||
import { IInventoryAdjustmentsFilter } from "../types/InventoryAdjustments.types";
|
||||
// import { InventoryAdjustmentTransformer } from "../InventoryAdjustmentTransformer";
|
||||
// import { InventoryAdjustment } from "../models/InventoryAdjustment";
|
||||
// import { IInventoryAdjustmentsFilter } from "../types/InventoryAdjustments.types";
|
||||
|
||||
import { TransformerInjectable } from "@/modules/Transformer/TransformerInjectable.service";
|
||||
// import { TransformerInjectable } from "@/modules/Transformer/TransformerInjectable.service";
|
||||
|
||||
export class GetInventoryAdjustmentsService {
|
||||
// export class GetInventoryAdjustmentsService {
|
||||
|
||||
constructor(
|
||||
public readonly transformer: TransformerInjectable,
|
||||
private readonly inventoryAdjustmentModel: typeof InventoryAdjustment,
|
||||
) {
|
||||
// constructor(
|
||||
// public readonly transformer: TransformerInjectable,
|
||||
// private readonly inventoryAdjustmentModel: typeof InventoryAdjustment,
|
||||
// ) {
|
||||
|
||||
}
|
||||
/**
|
||||
* Retrieve the inventory adjustments paginated list.
|
||||
* @param {number} tenantId
|
||||
* @param {IInventoryAdjustmentsFilter} adjustmentsFilter
|
||||
*/
|
||||
public async getInventoryAdjustments(
|
||||
tenantId: number,
|
||||
filterDTO: IInventoryAdjustmentsFilter,
|
||||
): Promise<{
|
||||
inventoryAdjustments: IInventoryAdjustment[];
|
||||
pagination: IPaginationMeta;
|
||||
}> {
|
||||
// }
|
||||
// /**
|
||||
// * Retrieve the inventory adjustments paginated list.
|
||||
// * @param {number} tenantId
|
||||
// * @param {IInventoryAdjustmentsFilter} adjustmentsFilter
|
||||
// */
|
||||
// public async getInventoryAdjustments(
|
||||
// tenantId: number,
|
||||
// filterDTO: IInventoryAdjustmentsFilter,
|
||||
// ): Promise<{
|
||||
// inventoryAdjustments: IInventoryAdjustment[];
|
||||
// pagination: IPaginationMeta;
|
||||
// }> {
|
||||
|
||||
// Parses inventory adjustments list filter DTO.
|
||||
const filter = this.parseListFilterDTO(filterDTO);
|
||||
// // Parses inventory adjustments list filter DTO.
|
||||
// const filter = this.parseListFilterDTO(filterDTO);
|
||||
|
||||
// Dynamic list service.
|
||||
const dynamicFilter = await this.dynamicListService.dynamicList(
|
||||
tenantId,
|
||||
InventoryAdjustment,
|
||||
filter,
|
||||
);
|
||||
const { results, pagination } = await this.inventoryAdjustmentModel.query()
|
||||
.onBuild((query) => {
|
||||
query.withGraphFetched('entries.item');
|
||||
query.withGraphFetched('adjustmentAccount');
|
||||
// // Dynamic list service.
|
||||
// const dynamicFilter = await this.dynamicListService.dynamicList(
|
||||
// tenantId,
|
||||
// InventoryAdjustment,
|
||||
// filter,
|
||||
// );
|
||||
// const { results, pagination } = await this.inventoryAdjustmentModel.query()
|
||||
// .onBuild((query) => {
|
||||
// query.withGraphFetched('entries.item');
|
||||
// query.withGraphFetched('adjustmentAccount');
|
||||
|
||||
dynamicFilter.buildQuery()(query);
|
||||
})
|
||||
.pagination(filter.page - 1, filter.pageSize);
|
||||
// dynamicFilter.buildQuery()(query);
|
||||
// })
|
||||
// .pagination(filter.page - 1, filter.pageSize);
|
||||
|
||||
// Retrieves the transformed inventory adjustments.
|
||||
const inventoryAdjustments = await this.transformer.transform(
|
||||
results,
|
||||
new InventoryAdjustmentTransformer(),
|
||||
);
|
||||
return {
|
||||
inventoryAdjustments,
|
||||
pagination,
|
||||
};
|
||||
}
|
||||
}
|
||||
// // Retrieves the transformed inventory adjustments.
|
||||
// const inventoryAdjustments = await this.transformer.transform(
|
||||
// results,
|
||||
// new InventoryAdjustmentTransformer(),
|
||||
// );
|
||||
// return {
|
||||
// inventoryAdjustments,
|
||||
// pagination,
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { InventoryAdjustmentsGLEntries } from '../commands/ledger/InventoryAdjustmentsGLEntries';
|
||||
import { IInventoryAdjustmentEventDeletedPayload } from '../types/InventoryAdjustments.types';
|
||||
import { IInventoryAdjustmentEventCreatedPayload } from '../types/InventoryAdjustments.types';
|
||||
import { events } from '@/common/events/events';
|
||||
|
||||
@Injectable()
|
||||
export class InventoryAdjustmentsGLSubscriber {
|
||||
constructor(
|
||||
private readonly inventoryAdjustmentGL: InventoryAdjustmentsGLEntries,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Handles writing increment inventory adjustment GL entries.
|
||||
*/
|
||||
@OnEvent(events.inventoryAdjustment.onQuickCreated)
|
||||
@OnEvent(events.inventoryAdjustment.onPublished)
|
||||
public async handleGLEntriesOnceIncrementAdjustmentCreated({
|
||||
inventoryAdjustmentId,
|
||||
inventoryAdjustment,
|
||||
trx,
|
||||
}: IInventoryAdjustmentEventCreatedPayload) {
|
||||
// Can't continue if the inventory adjustment is not published.
|
||||
if (!inventoryAdjustment.isPublished) {
|
||||
return;
|
||||
}
|
||||
// Can't continue if the inventory adjustment direction is not `IN`.
|
||||
if (inventoryAdjustment.type !== 'increment') {
|
||||
return;
|
||||
}
|
||||
await this.inventoryAdjustmentGL.writeAdjustmentGLEntries(
|
||||
inventoryAdjustmentId,
|
||||
trx,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverts the inventory adjustment GL entries once the transaction deleted.
|
||||
* @param {IInventoryAdjustmentEventDeletedPayload} payload -
|
||||
*/
|
||||
@OnEvent(events.inventoryAdjustment.onDeleted)
|
||||
public async revertAdjustmentGLEntriesOnceDeleted({
|
||||
inventoryAdjustmentId,
|
||||
oldInventoryAdjustment,
|
||||
}: IInventoryAdjustmentEventDeletedPayload) {
|
||||
// Can't continue if the inventory adjustment is not published.
|
||||
if (!oldInventoryAdjustment.isPublished) {
|
||||
return;
|
||||
}
|
||||
await this.inventoryAdjustmentGL.revertAdjustmentGLEntries(
|
||||
inventoryAdjustmentId,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles writing inventory transactions once the quick adjustment created.
|
||||
* @param {IInventoryAdjustmentEventPublishedPayload} payload
|
||||
* @param {IInventoryAdjustmentEventCreatedPayload} payload -
|
||||
*/
|
||||
// private handleWriteInventoryTransactionsOncePublished = async ({
|
||||
// inventoryAdjustment,
|
||||
// trx,
|
||||
// }:
|
||||
// | IInventoryAdjustmentEventPublishedPayload
|
||||
// | IInventoryAdjustmentEventCreatedPayload) => {
|
||||
// await this.inventoryAdjustment.writeInventoryTransactions(
|
||||
// tenantId,
|
||||
// inventoryAdjustment,
|
||||
// false,
|
||||
// trx
|
||||
// );
|
||||
// };
|
||||
|
||||
/**
|
||||
* Handles reverting invetory transactions once the inventory adjustment deleted.
|
||||
* @param {IInventoryAdjustmentEventDeletedPayload} payload -
|
||||
*/
|
||||
// private handleRevertInventoryTransactionsOnceDeleted = async ({
|
||||
// tenantId,
|
||||
// inventoryAdjustmentId,
|
||||
// oldInventoryAdjustment,
|
||||
// trx,
|
||||
// }: IInventoryAdjustmentEventDeletedPayload) => {
|
||||
// // Can't continue if the inventory adjustment is not published.
|
||||
// if (!oldInventoryAdjustment.isPublished) {
|
||||
// return;
|
||||
// }
|
||||
// // Reverts the inventory transactions of adjustment transaction.
|
||||
// await this.inventoryAdjustment.revertInventoryTransactions(
|
||||
// tenantId,
|
||||
// inventoryAdjustmentId,
|
||||
// trx
|
||||
// );
|
||||
// };
|
||||
}
|
||||
Reference in New Issue
Block a user