fix(server): customer/vendor opening balance:

This commit is contained in:
Ahmed Bouhuolia
2026-01-23 17:43:22 +02:00
parent 17651e0768
commit ca910ee489
9 changed files with 495 additions and 552 deletions

View File

@@ -1,91 +1,71 @@
// import { Inject, Service } from 'typedi';
// import events from '@/subscribers/events';
// import { VendorGLEntriesStorage } from '../VendorGLEntriesStorage';
// import {
// IVendorEventCreatedPayload,
// IVendorEventDeletedPayload,
// IVendorOpeningBalanceEditedPayload,
// } from '@/interfaces';
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { events } from '@/common/events/events';
import { VendorGLEntriesStorage } from '../VendorGLEntriesStorage';
import {
IVendorEventCreatedPayload,
IVendorEventDeletedPayload,
IVendorOpeningBalanceEditedPayload,
} from '../types/Vendors.types';
// @Service()
// export class VendorsWriteGLOpeningSubscriber {
// @Inject()
// private vendorGLEntriesStorage: VendorGLEntriesStorage;
@Injectable()
export class VendorsWriteGLOpeningSubscriber {
constructor(
private readonly vendorGLEntriesStorage: VendorGLEntriesStorage,
) {}
// /**
// * Constructor method.
// */
// public attach(bus) {
// bus.subscribe(
// events.vendors.onCreated,
// this.handleWriteOpeningBalanceEntries
// );
// bus.subscribe(
// events.vendors.onDeleted,
// this.handleRevertOpeningBalanceEntries
// );
// bus.subscribe(
// events.vendors.onOpeningBalanceChanged,
// this.handleRewriteOpeningEntriesOnChanged
// );
// }
/**
* Writes the open balance journal entries once the vendor created.
* @param {IVendorEventCreatedPayload} payload -
*/
@OnEvent(events.vendors.onCreated)
public async handleWriteOpeningBalanceEntries({
vendor,
trx,
}: IVendorEventCreatedPayload) {
// Writes the vendor opening balance journal entries.
if (vendor.openingBalance) {
await this.vendorGLEntriesStorage.writeVendorOpeningBalance(
vendor.id,
trx,
);
}
}
// /**
// * Writes the open balance journal entries once the vendor created.
// * @param {IVendorEventCreatedPayload} payload -
// */
// private handleWriteOpeningBalanceEntries = async ({
// tenantId,
// vendor,
// trx,
// }: IVendorEventCreatedPayload) => {
// // Writes the vendor opening balance journal entries.
// if (vendor.openingBalance) {
// await this.vendorGLEntriesStorage.writeVendorOpeningBalance(
// tenantId,
// vendor.id,
// trx
// );
// }
// };
/**
* Revert the opening balance journal entries once the vendor deleted.
* @param {IVendorEventDeletedPayload} payload -
*/
@OnEvent(events.vendors.onDeleted)
public async handleRevertOpeningBalanceEntries({
vendorId,
trx,
}: IVendorEventDeletedPayload) {
await this.vendorGLEntriesStorage.revertVendorOpeningBalance(
vendorId,
trx,
);
}
// /**
// * Revert the opening balance journal entries once the vendor deleted.
// * @param {IVendorEventDeletedPayload} payload -
// */
// private handleRevertOpeningBalanceEntries = async ({
// tenantId,
// vendorId,
// trx,
// }: IVendorEventDeletedPayload) => {
// await this.vendorGLEntriesStorage.revertVendorOpeningBalance(
// tenantId,
// vendorId,
// trx
// );
// };
// /**
// * Handles the rewrite opening balance entries once opening balnace changed.
// * @param {ICustomerOpeningBalanceEditedPayload} payload -
// */
// private handleRewriteOpeningEntriesOnChanged = async ({
// tenantId,
// vendor,
// trx,
// }: IVendorOpeningBalanceEditedPayload) => {
// if (vendor.openingBalance) {
// await this.vendorGLEntriesStorage.rewriteVendorOpeningBalance(
// tenantId,
// vendor.id,
// trx
// );
// } else {
// await this.vendorGLEntriesStorage.revertVendorOpeningBalance(
// tenantId,
// vendor.id,
// trx
// );
// }
// };
// }
/**
* Handles the rewrite opening balance entries once opening balance changed.
* @param {IVendorOpeningBalanceEditedPayload} payload -
*/
@OnEvent(events.vendors.onOpeningBalanceChanged)
public async handleRewriteOpeningEntriesOnChanged({
vendor,
trx,
}: IVendorOpeningBalanceEditedPayload) {
if (vendor.openingBalance) {
await this.vendorGLEntriesStorage.rewriteVendorOpeningBalance(
vendor.id,
trx,
);
} else {
await this.vendorGLEntriesStorage.revertVendorOpeningBalance(
vendor.id,
trx,
);
}
}
}