mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 14:20:31 +00:00
fix(server): customer/vendor opening balance:
This commit is contained in:
@@ -1,117 +1,103 @@
|
|||||||
// import { Service, Inject } from 'typedi';
|
import { Injectable } from '@nestjs/common';
|
||||||
// import { AccountNormal, ICustomer, ILedgerEntry } from '@/interfaces';
|
import { AccountNormal } from '@/interfaces/Account';
|
||||||
// import Ledger from '@/services/Accounting/Ledger';
|
import { ILedgerEntry } from '@/modules/Ledger/types/Ledger.types';
|
||||||
|
import { Ledger } from '@/modules/Ledger/Ledger';
|
||||||
|
import { Customer } from './models/Customer';
|
||||||
|
|
||||||
// @Service()
|
@Injectable()
|
||||||
// export class CustomerGLEntries {
|
export class CustomerGLEntries {
|
||||||
// /**
|
/**
|
||||||
// * Retrieves the customer opening balance common entry attributes.
|
* Retrieves the customer opening balance common entry attributes.
|
||||||
// * @param {ICustomer} customer
|
*/
|
||||||
// */
|
private getCustomerOpeningGLCommonEntry = (customer: Customer) => {
|
||||||
// private getCustomerOpeningGLCommonEntry = (customer: ICustomer) => {
|
return {
|
||||||
// return {
|
exchangeRate: customer.openingBalanceExchangeRate,
|
||||||
// exchangeRate: customer.openingBalanceExchangeRate,
|
currencyCode: customer.currencyCode,
|
||||||
// currencyCode: customer.currencyCode,
|
|
||||||
|
|
||||||
// transactionType: 'CustomerOpeningBalance',
|
transactionType: 'CustomerOpeningBalance',
|
||||||
// transactionId: customer.id,
|
transactionId: customer.id,
|
||||||
|
|
||||||
// date: customer.openingBalanceAt,
|
date: customer.openingBalanceAt,
|
||||||
// userId: customer.userId,
|
contactId: customer.id,
|
||||||
// contactId: customer.id,
|
|
||||||
|
|
||||||
// credit: 0,
|
credit: 0,
|
||||||
// debit: 0,
|
debit: 0,
|
||||||
|
|
||||||
// branchId: customer.openingBalanceBranchId,
|
branchId: customer.openingBalanceBranchId,
|
||||||
// };
|
};
|
||||||
// };
|
};
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Retrieves the customer opening GL credit entry.
|
* Retrieves the customer opening GL credit entry.
|
||||||
// * @param {number} ARAccountId
|
*/
|
||||||
// * @param {ICustomer} customer
|
private getCustomerOpeningGLCreditEntry = (
|
||||||
// * @returns {ILedgerEntry}
|
ARAccountId: number,
|
||||||
// */
|
customer: Customer
|
||||||
// private getCustomerOpeningGLCreditEntry = (
|
): ILedgerEntry => {
|
||||||
// ARAccountId: number,
|
const commonEntry = this.getCustomerOpeningGLCommonEntry(customer);
|
||||||
// customer: ICustomer
|
|
||||||
// ): ILedgerEntry => {
|
|
||||||
// const commonEntry = this.getCustomerOpeningGLCommonEntry(customer);
|
|
||||||
|
|
||||||
// return {
|
return {
|
||||||
// ...commonEntry,
|
...commonEntry,
|
||||||
// credit: 0,
|
credit: 0,
|
||||||
// debit: customer.localOpeningBalance,
|
debit: customer.localOpeningBalance,
|
||||||
// accountId: ARAccountId,
|
accountId: ARAccountId,
|
||||||
// accountNormal: AccountNormal.DEBIT,
|
accountNormal: AccountNormal.DEBIT,
|
||||||
// index: 1,
|
index: 1,
|
||||||
// };
|
};
|
||||||
// };
|
};
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Retrieves the customer opening GL debit entry.
|
* Retrieves the customer opening GL debit entry.
|
||||||
// * @param {number} incomeAccountId
|
*/
|
||||||
// * @param {ICustomer} customer
|
private getCustomerOpeningGLDebitEntry = (
|
||||||
// * @returns {ILedgerEntry}
|
incomeAccountId: number,
|
||||||
// */
|
customer: Customer
|
||||||
// private getCustomerOpeningGLDebitEntry = (
|
): ILedgerEntry => {
|
||||||
// incomeAccountId: number,
|
const commonEntry = this.getCustomerOpeningGLCommonEntry(customer);
|
||||||
// customer: ICustomer
|
|
||||||
// ): ILedgerEntry => {
|
|
||||||
// const commonEntry = this.getCustomerOpeningGLCommonEntry(customer);
|
|
||||||
|
|
||||||
// return {
|
return {
|
||||||
// ...commonEntry,
|
...commonEntry,
|
||||||
// credit: customer.localOpeningBalance,
|
credit: customer.localOpeningBalance,
|
||||||
// debit: 0,
|
debit: 0,
|
||||||
// accountId: incomeAccountId,
|
accountId: incomeAccountId,
|
||||||
// accountNormal: AccountNormal.CREDIT,
|
accountNormal: AccountNormal.CREDIT,
|
||||||
|
|
||||||
// index: 2,
|
index: 2,
|
||||||
// };
|
};
|
||||||
// };
|
};
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Retrieves the customer opening GL entries.
|
* Retrieves the customer opening GL entries.
|
||||||
// * @param {number} ARAccountId
|
*/
|
||||||
// * @param {number} incomeAccountId
|
public getCustomerOpeningGLEntries = (
|
||||||
// * @param {ICustomer} customer
|
ARAccountId: number,
|
||||||
// * @returns {ILedgerEntry[]}
|
incomeAccountId: number,
|
||||||
// */
|
customer: Customer
|
||||||
// public getCustomerOpeningGLEntries = (
|
) => {
|
||||||
// ARAccountId: number,
|
const debitEntry = this.getCustomerOpeningGLDebitEntry(
|
||||||
// incomeAccountId: number,
|
incomeAccountId,
|
||||||
// customer: ICustomer
|
customer
|
||||||
// ) => {
|
);
|
||||||
// const debitEntry = this.getCustomerOpeningGLDebitEntry(
|
const creditEntry = this.getCustomerOpeningGLCreditEntry(
|
||||||
// incomeAccountId,
|
ARAccountId,
|
||||||
// customer
|
customer
|
||||||
// );
|
);
|
||||||
// const creditEntry = this.getCustomerOpeningGLCreditEntry(
|
return [debitEntry, creditEntry];
|
||||||
// ARAccountId,
|
};
|
||||||
// customer
|
|
||||||
// );
|
|
||||||
// return [debitEntry, creditEntry];
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Retrieves the customer opening balance ledger.
|
* Retrieves the customer opening balance ledger.
|
||||||
// * @param {number} ARAccountId
|
*/
|
||||||
// * @param {number} incomeAccountId
|
public getCustomerOpeningLedger = (
|
||||||
// * @param {ICustomer} customer
|
ARAccountId: number,
|
||||||
// * @returns {ILedger}
|
incomeAccountId: number,
|
||||||
// */
|
customer: Customer
|
||||||
// public getCustomerOpeningLedger = (
|
) => {
|
||||||
// ARAccountId: number,
|
const entries = this.getCustomerOpeningGLEntries(
|
||||||
// incomeAccountId: number,
|
ARAccountId,
|
||||||
// customer: ICustomer
|
incomeAccountId,
|
||||||
// ) => {
|
customer
|
||||||
// const entries = this.getCustomerOpeningGLEntries(
|
);
|
||||||
// ARAccountId,
|
return new Ledger(entries);
|
||||||
// incomeAccountId,
|
};
|
||||||
// customer
|
}
|
||||||
// );
|
|
||||||
// return new Ledger(entries);
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -1,90 +1,79 @@
|
|||||||
// import { Knex } from 'knex';
|
import { Knex } from 'knex';
|
||||||
// import LedgerStorageService from '@/services/Accounting/LedgerStorageService';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
// import HasTenancyService from '@/services/Tenancy/TenancyService';
|
import { LedgerStorageService } from '@/modules/Ledger/LedgerStorage.service';
|
||||||
// import { Service, Inject } from 'typedi';
|
import { AccountRepository } from '@/modules/Accounts/repositories/Account.repository';
|
||||||
// import { CustomerGLEntries } from './CustomerGLEntries';
|
import { CustomerGLEntries } from './CustomerGLEntries';
|
||||||
|
import { Customer } from './models/Customer';
|
||||||
|
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||||
|
|
||||||
// @Service()
|
@Injectable()
|
||||||
// export class CustomerGLEntriesStorage {
|
export class CustomerGLEntriesStorage {
|
||||||
// @Inject()
|
constructor(
|
||||||
// private tenancy: HasTenancyService;
|
private readonly ledgerStorage: LedgerStorageService,
|
||||||
|
private readonly accountRepository: AccountRepository,
|
||||||
|
private readonly customerGLEntries: CustomerGLEntries,
|
||||||
|
|
||||||
// @Inject()
|
@Inject(Customer.name)
|
||||||
// private ledegrRepository: LedgerStorageService;
|
private readonly customerModel: TenantModelProxy<typeof Customer>,
|
||||||
|
) { }
|
||||||
|
|
||||||
// @Inject()
|
/**
|
||||||
// private customerGLEntries: CustomerGLEntries;
|
* Customer opening balance journals.
|
||||||
|
*/
|
||||||
|
public writeCustomerOpeningBalance = async (
|
||||||
|
customerId: number,
|
||||||
|
trx?: Knex.Transaction,
|
||||||
|
) => {
|
||||||
|
const customer = await this.customerModel()
|
||||||
|
.query(trx)
|
||||||
|
.findById(customerId);
|
||||||
|
|
||||||
// /**
|
// Finds the income account.
|
||||||
// * Customer opening balance journals.
|
const incomeAccount = await this.accountRepository.findOne({
|
||||||
// * @param {number} tenantId
|
slug: 'other-income',
|
||||||
// * @param {number} customerId
|
});
|
||||||
// * @param {Knex.Transaction} trx
|
// Find or create the A/R account.
|
||||||
// */
|
const ARAccount =
|
||||||
// public writeCustomerOpeningBalance = async (
|
await this.accountRepository.findOrCreateAccountReceivable(
|
||||||
// tenantId: number,
|
customer.currencyCode,
|
||||||
// customerId: number,
|
{},
|
||||||
// trx?: Knex.Transaction
|
trx,
|
||||||
// ) => {
|
);
|
||||||
// const { Customer } = this.tenancy.models(tenantId);
|
// Retrieves the customer opening balance ledger.
|
||||||
// const { accountRepository } = this.tenancy.repositories(tenantId);
|
const ledger = this.customerGLEntries.getCustomerOpeningLedger(
|
||||||
|
ARAccount.id,
|
||||||
|
incomeAccount.id,
|
||||||
|
customer,
|
||||||
|
);
|
||||||
|
// Commits the ledger entries to the storage.
|
||||||
|
await this.ledgerStorage.commit(ledger, trx);
|
||||||
|
};
|
||||||
|
|
||||||
// const customer = await Customer.query(trx).findById(customerId);
|
/**
|
||||||
|
* Reverts the customer opening balance GL entries.
|
||||||
|
*/
|
||||||
|
public revertCustomerOpeningBalance = async (
|
||||||
|
customerId: number,
|
||||||
|
trx?: Knex.Transaction,
|
||||||
|
) => {
|
||||||
|
await this.ledgerStorage.deleteByReference(
|
||||||
|
customerId,
|
||||||
|
'CustomerOpeningBalance',
|
||||||
|
trx,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
// // Finds the income account.
|
/**
|
||||||
// const incomeAccount = await accountRepository.findOne({
|
* Writes the customer opening balance GL entries.
|
||||||
// slug: 'other-income',
|
*/
|
||||||
// });
|
public rewriteCustomerOpeningBalance = async (
|
||||||
// // Find or create the A/R account.
|
customerId: number,
|
||||||
// const ARAccount = await accountRepository.findOrCreateAccountReceivable(
|
trx?: Knex.Transaction,
|
||||||
// customer.currencyCode,
|
) => {
|
||||||
// {},
|
// Reverts the customer opening balance entries.
|
||||||
// trx
|
await this.revertCustomerOpeningBalance(customerId, trx);
|
||||||
// );
|
|
||||||
// // Retrieves the customer opening balance ledger.
|
|
||||||
// const ledger = this.customerGLEntries.getCustomerOpeningLedger(
|
|
||||||
// ARAccount.id,
|
|
||||||
// incomeAccount.id,
|
|
||||||
// customer
|
|
||||||
// );
|
|
||||||
// // Commits the ledger entries to the storage.
|
|
||||||
// await this.ledegrRepository.commit(tenantId, ledger, trx);
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /**
|
// Write the customer opening balance entries.
|
||||||
// * Reverts the customer opening balance GL entries.
|
await this.writeCustomerOpeningBalance(customerId, trx);
|
||||||
// * @param {number} tenantId
|
};
|
||||||
// * @param {number} customerId
|
}
|
||||||
// * @param {Knex.Transaction} trx
|
|
||||||
// */
|
|
||||||
// public revertCustomerOpeningBalance = async (
|
|
||||||
// tenantId: number,
|
|
||||||
// customerId: number,
|
|
||||||
// trx?: Knex.Transaction
|
|
||||||
// ) => {
|
|
||||||
// await this.ledegrRepository.deleteByReference(
|
|
||||||
// tenantId,
|
|
||||||
// customerId,
|
|
||||||
// 'CustomerOpeningBalance',
|
|
||||||
// trx
|
|
||||||
// );
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Writes the customer opening balance GL entries.
|
|
||||||
// * @param {number} tenantId
|
|
||||||
// * @param {number} customerId
|
|
||||||
// * @param {Knex.Transaction} trx
|
|
||||||
// */
|
|
||||||
// public rewriteCustomerOpeningBalance = async (
|
|
||||||
// tenantId: number,
|
|
||||||
// customerId: number,
|
|
||||||
// trx?: Knex.Transaction
|
|
||||||
// ) => {
|
|
||||||
// // Reverts the customer opening balance entries.
|
|
||||||
// await this.revertCustomerOpeningBalance(tenantId, customerId, trx);
|
|
||||||
|
|
||||||
// // Write the customer opening balance entries.
|
|
||||||
// await this.writeCustomerOpeningBalance(tenantId, customerId, trx);
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -18,9 +18,14 @@ import { GetCustomers } from './queries/GetCustomers.service';
|
|||||||
import { DynamicListModule } from '../DynamicListing/DynamicList.module';
|
import { DynamicListModule } from '../DynamicListing/DynamicList.module';
|
||||||
import { BulkDeleteCustomersService } from './BulkDeleteCustomers.service';
|
import { BulkDeleteCustomersService } from './BulkDeleteCustomers.service';
|
||||||
import { ValidateBulkDeleteCustomersService } from './ValidateBulkDeleteCustomers.service';
|
import { ValidateBulkDeleteCustomersService } from './ValidateBulkDeleteCustomers.service';
|
||||||
|
import { LedgerModule } from '../Ledger/Ledger.module';
|
||||||
|
import { AccountsModule } from '../Accounts/Accounts.module';
|
||||||
|
import { CustomerGLEntries } from './CustomerGLEntries';
|
||||||
|
import { CustomerGLEntriesStorage } from './CustomerGLEntriesStorage';
|
||||||
|
import { CustomerWriteGLOpeningBalanceSubscriber } from './subscribers/CustomerGLEntriesSubscriber';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [TenancyDatabaseModule, DynamicListModule],
|
imports: [TenancyDatabaseModule, DynamicListModule, LedgerModule, AccountsModule],
|
||||||
controllers: [CustomersController],
|
controllers: [CustomersController],
|
||||||
providers: [
|
providers: [
|
||||||
ActivateCustomer,
|
ActivateCustomer,
|
||||||
@@ -41,6 +46,9 @@ import { ValidateBulkDeleteCustomersService } from './ValidateBulkDeleteCustomer
|
|||||||
GetCustomers,
|
GetCustomers,
|
||||||
BulkDeleteCustomersService,
|
BulkDeleteCustomersService,
|
||||||
ValidateBulkDeleteCustomersService,
|
ValidateBulkDeleteCustomersService,
|
||||||
|
CustomerGLEntries,
|
||||||
|
CustomerGLEntriesStorage,
|
||||||
|
CustomerWriteGLOpeningBalanceSubscriber,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class CustomersModule {}
|
export class CustomersModule { }
|
||||||
|
|||||||
@@ -1,91 +1,63 @@
|
|||||||
// import { Service, Inject } from 'typedi';
|
import { Injectable } from '@nestjs/common';
|
||||||
// import {
|
import { OnEvent } from '@nestjs/event-emitter';
|
||||||
// ICustomerEventCreatedPayload,
|
import {
|
||||||
// ICustomerEventDeletedPayload,
|
ICustomerEventCreatedPayload,
|
||||||
// ICustomerOpeningBalanceEditedPayload,
|
ICustomerEventDeletedPayload,
|
||||||
// } from '@/interfaces';
|
ICustomerOpeningBalanceEditedPayload,
|
||||||
// import events from '@/subscribers/events';
|
} from '../types/Customers.types';
|
||||||
// import { CustomerGLEntriesStorage } from '../CustomerGLEntriesStorage';
|
import { events } from '@/common/events/events';
|
||||||
|
import { CustomerGLEntriesStorage } from '../CustomerGLEntriesStorage';
|
||||||
|
|
||||||
// @Service()
|
@Injectable()
|
||||||
// export class CustomerWriteGLOpeningBalanceSubscriber {
|
export class CustomerWriteGLOpeningBalanceSubscriber {
|
||||||
// @Inject()
|
constructor(private readonly customerGLEntries: CustomerGLEntriesStorage) { }
|
||||||
// private customerGLEntries: CustomerGLEntriesStorage;
|
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Attaches events with handlers.
|
* Handles the writing opening balance journal entries once the customer created.
|
||||||
// */
|
*/
|
||||||
// public attach(bus) {
|
@OnEvent(events.customers.onCreated)
|
||||||
// bus.subscribe(
|
public async handleWriteOpenBalanceEntries({
|
||||||
// events.customers.onCreated,
|
customer,
|
||||||
// this.handleWriteOpenBalanceEntries
|
trx,
|
||||||
// );
|
}: ICustomerEventCreatedPayload) {
|
||||||
// bus.subscribe(
|
// Writes the customer opening balance journal entries.
|
||||||
// events.customers.onDeleted,
|
if (customer.openingBalance) {
|
||||||
// this.handleRevertOpeningBalanceEntries
|
await this.customerGLEntries.writeCustomerOpeningBalance(
|
||||||
// );
|
customer.id,
|
||||||
// bus.subscribe(
|
trx,
|
||||||
// events.customers.onOpeningBalanceChanged,
|
);
|
||||||
// this.handleRewriteOpeningEntriesOnChanged
|
}
|
||||||
// );
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Handles the writing opening balance journal entries once the customer created.
|
* Handles the deleting opening balance journal entries once the customer deleted.
|
||||||
// * @param {ICustomerEventCreatedPayload} payload -
|
*/
|
||||||
// */
|
@OnEvent(events.customers.onDeleted)
|
||||||
// private handleWriteOpenBalanceEntries = async ({
|
public async handleRevertOpeningBalanceEntries({
|
||||||
// tenantId,
|
customerId,
|
||||||
// customer,
|
trx,
|
||||||
// trx,
|
}: ICustomerEventDeletedPayload) {
|
||||||
// }: ICustomerEventCreatedPayload) => {
|
await this.customerGLEntries.revertCustomerOpeningBalance(customerId, trx);
|
||||||
// // Writes the customer opening balance journal entries.
|
}
|
||||||
// if (customer.openingBalance) {
|
|
||||||
// await this.customerGLEntries.writeCustomerOpeningBalance(
|
|
||||||
// tenantId,
|
|
||||||
// customer.id,
|
|
||||||
// trx
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Handles the deleting opeing balance journal entrise once the customer deleted.
|
* Handles the rewrite opening balance entries once opening balance changed.
|
||||||
// * @param {ICustomerEventDeletedPayload} payload -
|
*/
|
||||||
// */
|
@OnEvent(events.customers.onOpeningBalanceChanged)
|
||||||
// private handleRevertOpeningBalanceEntries = async ({
|
public async handleRewriteOpeningEntriesOnChanged({
|
||||||
// tenantId,
|
customer,
|
||||||
// customerId,
|
trx,
|
||||||
// trx,
|
}: ICustomerOpeningBalanceEditedPayload) {
|
||||||
// }: ICustomerEventDeletedPayload) => {
|
if (customer.openingBalance) {
|
||||||
// await this.customerGLEntries.revertCustomerOpeningBalance(
|
await this.customerGLEntries.rewriteCustomerOpeningBalance(
|
||||||
// tenantId,
|
customer.id,
|
||||||
// customerId,
|
trx,
|
||||||
// trx
|
);
|
||||||
// );
|
} else {
|
||||||
// };
|
await this.customerGLEntries.revertCustomerOpeningBalance(
|
||||||
|
customer.id,
|
||||||
// /**
|
trx,
|
||||||
// * Handles the rewrite opening balance entries once opening balnace changed.
|
);
|
||||||
// * @param {ICustomerOpeningBalanceEditedPayload} payload -
|
}
|
||||||
// */
|
}
|
||||||
// private handleRewriteOpeningEntriesOnChanged = async ({
|
}
|
||||||
// tenantId,
|
|
||||||
// customer,
|
|
||||||
// trx,
|
|
||||||
// }: ICustomerOpeningBalanceEditedPayload) => {
|
|
||||||
// if (customer.openingBalance) {
|
|
||||||
// await this.customerGLEntries.rewriteCustomerOpeningBalance(
|
|
||||||
// tenantId,
|
|
||||||
// customer.id,
|
|
||||||
// trx
|
|
||||||
// );
|
|
||||||
// } else {
|
|
||||||
// await this.customerGLEntries.revertCustomerOpeningBalance(
|
|
||||||
// tenantId,
|
|
||||||
// customer.id,
|
|
||||||
// trx
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -1,115 +1,116 @@
|
|||||||
// import { Service } from 'typedi';
|
import { Injectable } from '@nestjs/common';
|
||||||
// import { IVendor, AccountNormal, ILedgerEntry } from '@/interfaces';
|
import { AccountNormal } from '@/interfaces/Account';
|
||||||
// import Ledger from '@/services/Accounting/Ledger';
|
import { ILedgerEntry } from '@/modules/Ledger/types/Ledger.types';
|
||||||
|
import { Ledger } from '@/modules/Ledger/Ledger';
|
||||||
|
import { Vendor } from './models/Vendor';
|
||||||
|
|
||||||
// @Service()
|
@Injectable()
|
||||||
// export class VendorGLEntries {
|
export class VendorGLEntries {
|
||||||
// /**
|
/**
|
||||||
// * Retrieves the opening balance GL common entry.
|
* Retrieves the opening balance GL common entry.
|
||||||
// * @param {IVendor} vendor -
|
* @param {Vendor} vendor -
|
||||||
// */
|
*/
|
||||||
// private getOpeningBalanceGLCommonEntry = (vendor: IVendor) => {
|
private getOpeningBalanceGLCommonEntry = (vendor: Vendor) => {
|
||||||
// return {
|
return {
|
||||||
// exchangeRate: vendor.openingBalanceExchangeRate,
|
exchangeRate: vendor.openingBalanceExchangeRate,
|
||||||
// currencyCode: vendor.currencyCode,
|
currencyCode: vendor.currencyCode,
|
||||||
|
|
||||||
// transactionType: 'VendorOpeningBalance',
|
transactionType: 'VendorOpeningBalance',
|
||||||
// transactionId: vendor.id,
|
transactionId: vendor.id,
|
||||||
|
|
||||||
// date: vendor.openingBalanceAt,
|
date: vendor.openingBalanceAt,
|
||||||
// userId: vendor.userId,
|
contactId: vendor.id,
|
||||||
// contactId: vendor.id,
|
|
||||||
|
|
||||||
// credit: 0,
|
credit: 0,
|
||||||
// debit: 0,
|
debit: 0,
|
||||||
|
|
||||||
// branchId: vendor.openingBalanceBranchId,
|
branchId: vendor.openingBalanceBranchId,
|
||||||
// };
|
};
|
||||||
// };
|
};
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Retrieves the opening balance GL debit entry.
|
* Retrieves the opening balance GL debit entry.
|
||||||
// * @param {number} costAccountId -
|
* @param {number} costAccountId -
|
||||||
// * @param {IVendor} vendor
|
* @param {Vendor} vendor
|
||||||
// * @returns {ILedgerEntry}
|
* @returns {ILedgerEntry}
|
||||||
// */
|
*/
|
||||||
// private getOpeningBalanceGLDebitEntry = (
|
private getOpeningBalanceGLDebitEntry = (
|
||||||
// costAccountId: number,
|
costAccountId: number,
|
||||||
// vendor: IVendor
|
vendor: Vendor
|
||||||
// ): ILedgerEntry => {
|
): ILedgerEntry => {
|
||||||
// const commonEntry = this.getOpeningBalanceGLCommonEntry(vendor);
|
const commonEntry = this.getOpeningBalanceGLCommonEntry(vendor);
|
||||||
|
|
||||||
// return {
|
return {
|
||||||
// ...commonEntry,
|
...commonEntry,
|
||||||
// accountId: costAccountId,
|
accountId: costAccountId,
|
||||||
// accountNormal: AccountNormal.DEBIT,
|
accountNormal: AccountNormal.DEBIT,
|
||||||
// debit: vendor.localOpeningBalance,
|
debit: vendor.localOpeningBalance,
|
||||||
// credit: 0,
|
credit: 0,
|
||||||
// index: 2,
|
index: 2,
|
||||||
// };
|
};
|
||||||
// };
|
};
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Retrieves the opening balance GL credit entry.
|
* Retrieves the opening balance GL credit entry.
|
||||||
// * @param {number} APAccountId
|
* @param {number} APAccountId
|
||||||
// * @param {IVendor} vendor
|
* @param {Vendor} vendor
|
||||||
// * @returns {ILedgerEntry}
|
* @returns {ILedgerEntry}
|
||||||
// */
|
*/
|
||||||
// private getOpeningBalanceGLCreditEntry = (
|
private getOpeningBalanceGLCreditEntry = (
|
||||||
// APAccountId: number,
|
APAccountId: number,
|
||||||
// vendor: IVendor
|
vendor: Vendor
|
||||||
// ): ILedgerEntry => {
|
): ILedgerEntry => {
|
||||||
// const commonEntry = this.getOpeningBalanceGLCommonEntry(vendor);
|
const commonEntry = this.getOpeningBalanceGLCommonEntry(vendor);
|
||||||
|
|
||||||
// return {
|
return {
|
||||||
// ...commonEntry,
|
...commonEntry,
|
||||||
// accountId: APAccountId,
|
accountId: APAccountId,
|
||||||
// accountNormal: AccountNormal.CREDIT,
|
accountNormal: AccountNormal.CREDIT,
|
||||||
// credit: vendor.localOpeningBalance,
|
credit: vendor.localOpeningBalance,
|
||||||
// index: 1,
|
index: 1,
|
||||||
// };
|
};
|
||||||
// };
|
};
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Retrieves the opening balance GL entries.
|
* Retrieves the opening balance GL entries.
|
||||||
// * @param {number} APAccountId
|
* @param {number} APAccountId
|
||||||
// * @param {number} costAccountId -
|
* @param {number} costAccountId -
|
||||||
// * @param {IVendor} vendor
|
* @param {Vendor} vendor
|
||||||
// * @returns {ILedgerEntry[]}
|
* @returns {ILedgerEntry[]}
|
||||||
// */
|
*/
|
||||||
// public getOpeningBalanceGLEntries = (
|
public getOpeningBalanceGLEntries = (
|
||||||
// APAccountId: number,
|
APAccountId: number,
|
||||||
// costAccountId: number,
|
costAccountId: number,
|
||||||
// vendor: IVendor
|
vendor: Vendor
|
||||||
// ): ILedgerEntry[] => {
|
): ILedgerEntry[] => {
|
||||||
// const debitEntry = this.getOpeningBalanceGLDebitEntry(
|
const debitEntry = this.getOpeningBalanceGLDebitEntry(
|
||||||
// costAccountId,
|
costAccountId,
|
||||||
// vendor
|
vendor
|
||||||
// );
|
);
|
||||||
// const creditEntry = this.getOpeningBalanceGLCreditEntry(
|
const creditEntry = this.getOpeningBalanceGLCreditEntry(
|
||||||
// APAccountId,
|
APAccountId,
|
||||||
// vendor
|
vendor
|
||||||
// );
|
);
|
||||||
// return [debitEntry, creditEntry];
|
return [debitEntry, creditEntry];
|
||||||
// };
|
};
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Retrieves the opening balance ledger.
|
* Retrieves the opening balance ledger.
|
||||||
// * @param {number} APAccountId
|
* @param {number} APAccountId
|
||||||
// * @param {number} costAccountId -
|
* @param {number} costAccountId -
|
||||||
// * @param {IVendor} vendor
|
* @param {Vendor} vendor
|
||||||
// * @returns {Ledger}
|
* @returns {Ledger}
|
||||||
// */
|
*/
|
||||||
// public getOpeningBalanceLedger = (
|
public getOpeningBalanceLedger = (
|
||||||
// APAccountId: number,
|
APAccountId: number,
|
||||||
// costAccountId: number,
|
costAccountId: number,
|
||||||
// vendor: IVendor
|
vendor: Vendor
|
||||||
// ) => {
|
) => {
|
||||||
// const entries = this.getOpeningBalanceGLEntries(
|
const entries = this.getOpeningBalanceGLEntries(
|
||||||
// APAccountId,
|
APAccountId,
|
||||||
// costAccountId,
|
costAccountId,
|
||||||
// vendor
|
vendor
|
||||||
// );
|
);
|
||||||
// return new Ledger(entries);
|
return new Ledger(entries);
|
||||||
// };
|
};
|
||||||
// }
|
}
|
||||||
|
|||||||
@@ -1,88 +1,86 @@
|
|||||||
// import { Knex } from 'knex';
|
import { Knex } from 'knex';
|
||||||
// import { Service, Inject } from 'typedi';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
// import LedgerStorageService from '@/services/Accounting/LedgerStorageService';
|
import { LedgerStorageService } from '@/modules/Ledger/LedgerStorage.service';
|
||||||
// import HasTenancyService from '@/services/Tenancy/TenancyService';
|
import { AccountRepository } from '@/modules/Accounts/repositories/Account.repository';
|
||||||
// import { VendorGLEntries } from './VendorGLEntries';
|
import { VendorGLEntries } from './VendorGLEntries';
|
||||||
|
import { Vendor } from './models/Vendor';
|
||||||
|
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||||
|
|
||||||
// @Service()
|
@Injectable()
|
||||||
// export class VendorGLEntriesStorage {
|
export class VendorGLEntriesStorage {
|
||||||
// @Inject()
|
constructor(
|
||||||
// private tenancy: HasTenancyService;
|
private readonly ledgerStorage: LedgerStorageService,
|
||||||
|
private readonly accountRepository: AccountRepository,
|
||||||
|
private readonly vendorGLEntries: VendorGLEntries,
|
||||||
|
|
||||||
// @Inject()
|
@Inject(Vendor.name)
|
||||||
// private ledegrRepository: LedgerStorageService;
|
private readonly vendorModel: TenantModelProxy<typeof Vendor>,
|
||||||
|
) { }
|
||||||
|
|
||||||
// @Inject()
|
/**
|
||||||
// private vendorGLEntries: VendorGLEntries;
|
* Vendor opening balance journals.
|
||||||
|
* @param {number} vendorId
|
||||||
|
* @param {Knex.Transaction} trx
|
||||||
|
*/
|
||||||
|
public writeVendorOpeningBalance = async (
|
||||||
|
vendorId: number,
|
||||||
|
trx?: Knex.Transaction,
|
||||||
|
) => {
|
||||||
|
const vendor = await this.vendorModel()
|
||||||
|
.query(trx)
|
||||||
|
.findById(vendorId);
|
||||||
|
|
||||||
// /**
|
// Finds the expense account.
|
||||||
// * Vendor opening balance journals.
|
const expenseAccount = await this.accountRepository.findOrCreateOtherExpensesAccount(
|
||||||
// * @param {number} tenantId
|
{},
|
||||||
// * @param {number} vendorId
|
trx,
|
||||||
// * @param {Knex.Transaction} trx
|
);
|
||||||
// */
|
// Find or create the A/P account.
|
||||||
// public writeVendorOpeningBalance = async (
|
const APAccount =
|
||||||
// tenantId: number,
|
await this.accountRepository.findOrCreateAccountsPayable(
|
||||||
// vendorId: number,
|
vendor.currencyCode,
|
||||||
// trx?: Knex.Transaction
|
{},
|
||||||
// ) => {
|
trx,
|
||||||
// const { Vendor } = this.tenancy.models(tenantId);
|
);
|
||||||
// const { accountRepository } = this.tenancy.repositories(tenantId);
|
// Retrieves the vendor opening balance ledger.
|
||||||
|
const ledger = this.vendorGLEntries.getOpeningBalanceLedger(
|
||||||
|
APAccount.id,
|
||||||
|
expenseAccount.id,
|
||||||
|
vendor,
|
||||||
|
);
|
||||||
|
// Commits the ledger entries to the storage.
|
||||||
|
await this.ledgerStorage.commit(ledger, trx);
|
||||||
|
};
|
||||||
|
|
||||||
// const vendor = await Vendor.query(trx).findById(vendorId);
|
/**
|
||||||
|
* Reverts the vendor opening balance GL entries.
|
||||||
|
* @param {number} vendorId
|
||||||
|
* @param {Knex.Transaction} trx
|
||||||
|
*/
|
||||||
|
public revertVendorOpeningBalance = async (
|
||||||
|
vendorId: number,
|
||||||
|
trx?: Knex.Transaction,
|
||||||
|
) => {
|
||||||
|
await this.ledgerStorage.deleteByReference(
|
||||||
|
vendorId,
|
||||||
|
'VendorOpeningBalance',
|
||||||
|
trx,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
// // Finds the expense account.
|
/**
|
||||||
// const expenseAccount = await accountRepository.findOne({
|
* Writes the vendor opening balance GL entries.
|
||||||
// slug: 'other-expenses',
|
* @param {number} vendorId
|
||||||
// });
|
* @param {Knex.Transaction} trx
|
||||||
// // Find or create the A/P account.
|
*/
|
||||||
// const APAccount = await accountRepository.findOrCreateAccountsPayable(
|
public rewriteVendorOpeningBalance = async (
|
||||||
// vendor.currencyCode,
|
vendorId: number,
|
||||||
// {},
|
trx?: Knex.Transaction,
|
||||||
// trx
|
) => {
|
||||||
// );
|
// Reverts the vendor opening balance entries first.
|
||||||
// // Retrieves the vendor opening balance ledger.
|
await this.revertVendorOpeningBalance(vendorId, trx);
|
||||||
// const ledger = this.vendorGLEntries.getOpeningBalanceLedger(
|
|
||||||
// APAccount.id,
|
|
||||||
// expenseAccount.id,
|
|
||||||
// vendor
|
|
||||||
// );
|
|
||||||
// // Commits the ledger entries to the storage.
|
|
||||||
// await this.ledegrRepository.commit(tenantId, ledger, trx);
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /**
|
// Write the vendor opening balance entries.
|
||||||
// * Reverts the vendor opening balance GL entries.
|
await this.writeVendorOpeningBalance(vendorId, trx);
|
||||||
// * @param {number} tenantId
|
};
|
||||||
// * @param {number} vendorId
|
}
|
||||||
// * @param {Knex.Transaction} trx
|
|
||||||
// */
|
|
||||||
// public revertVendorOpeningBalance = async (
|
|
||||||
// tenantId: number,
|
|
||||||
// vendorId: number,
|
|
||||||
// trx?: Knex.Transaction
|
|
||||||
// ) => {
|
|
||||||
// await this.ledegrRepository.deleteByReference(
|
|
||||||
// tenantId,
|
|
||||||
// vendorId,
|
|
||||||
// 'VendorOpeningBalance',
|
|
||||||
// trx
|
|
||||||
// );
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Writes the vendor opening balance GL entries.
|
|
||||||
// * @param {number} tenantId
|
|
||||||
// * @param {number} vendorId
|
|
||||||
// * @param {Knex.Transaction} trx
|
|
||||||
// */
|
|
||||||
// public rewriteVendorOpeningBalance = async (
|
|
||||||
// tenantId: number,
|
|
||||||
// vendorId: number,
|
|
||||||
// trx?: Knex.Transaction
|
|
||||||
// ) => {
|
|
||||||
// await this.writeVendorOpeningBalance(tenantId, vendorId, trx);
|
|
||||||
|
|
||||||
// await this.revertVendorOpeningBalance(tenantId, vendorId, trx);
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -18,9 +18,14 @@ import { VendorsExportable } from './VendorsExportable';
|
|||||||
import { VendorsImportable } from './VendorsImportable';
|
import { VendorsImportable } from './VendorsImportable';
|
||||||
import { BulkDeleteVendorsService } from './BulkDeleteVendors.service';
|
import { BulkDeleteVendorsService } from './BulkDeleteVendors.service';
|
||||||
import { ValidateBulkDeleteVendorsService } from './ValidateBulkDeleteVendors.service';
|
import { ValidateBulkDeleteVendorsService } from './ValidateBulkDeleteVendors.service';
|
||||||
|
import { LedgerModule } from '../Ledger/Ledger.module';
|
||||||
|
import { AccountsModule } from '../Accounts/Accounts.module';
|
||||||
|
import { VendorGLEntries } from './VendorGLEntries';
|
||||||
|
import { VendorGLEntriesStorage } from './VendorGLEntriesStorage';
|
||||||
|
import { VendorsWriteGLOpeningSubscriber } from './subscribers/VendorGLEntriesSubscriber';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [TenancyDatabaseModule, DynamicListModule],
|
imports: [TenancyDatabaseModule, DynamicListModule, LedgerModule, AccountsModule],
|
||||||
controllers: [VendorsController],
|
controllers: [VendorsController],
|
||||||
providers: [
|
providers: [
|
||||||
ActivateVendorService,
|
ActivateVendorService,
|
||||||
@@ -38,7 +43,10 @@ import { ValidateBulkDeleteVendorsService } from './ValidateBulkDeleteVendors.se
|
|||||||
TransformerInjectable,
|
TransformerInjectable,
|
||||||
TenancyContext,
|
TenancyContext,
|
||||||
VendorsExportable,
|
VendorsExportable,
|
||||||
VendorsImportable
|
VendorsImportable,
|
||||||
|
VendorGLEntries,
|
||||||
|
VendorGLEntriesStorage,
|
||||||
|
VendorsWriteGLOpeningSubscriber,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class VendorsModule {}
|
export class VendorsModule { }
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ export class Vendor extends TenantBaseModel {
|
|||||||
openingBalance: number;
|
openingBalance: number;
|
||||||
openingBalanceExchangeRate: number;
|
openingBalanceExchangeRate: number;
|
||||||
openingBalanceAt: Date | string;
|
openingBalanceAt: Date | string;
|
||||||
|
openingBalanceBranchId?: number;
|
||||||
|
|
||||||
salutation: string;
|
salutation: string;
|
||||||
firstName: string;
|
firstName: string;
|
||||||
|
|||||||
@@ -1,91 +1,71 @@
|
|||||||
// import { Inject, Service } from 'typedi';
|
import { Injectable } from '@nestjs/common';
|
||||||
// import events from '@/subscribers/events';
|
import { OnEvent } from '@nestjs/event-emitter';
|
||||||
// import { VendorGLEntriesStorage } from '../VendorGLEntriesStorage';
|
import { events } from '@/common/events/events';
|
||||||
// import {
|
import { VendorGLEntriesStorage } from '../VendorGLEntriesStorage';
|
||||||
// IVendorEventCreatedPayload,
|
import {
|
||||||
// IVendorEventDeletedPayload,
|
IVendorEventCreatedPayload,
|
||||||
// IVendorOpeningBalanceEditedPayload,
|
IVendorEventDeletedPayload,
|
||||||
// } from '@/interfaces';
|
IVendorOpeningBalanceEditedPayload,
|
||||||
|
} from '../types/Vendors.types';
|
||||||
|
|
||||||
// @Service()
|
@Injectable()
|
||||||
// export class VendorsWriteGLOpeningSubscriber {
|
export class VendorsWriteGLOpeningSubscriber {
|
||||||
// @Inject()
|
constructor(
|
||||||
// private vendorGLEntriesStorage: VendorGLEntriesStorage;
|
private readonly vendorGLEntriesStorage: VendorGLEntriesStorage,
|
||||||
|
) {}
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Constructor method.
|
* Writes the open balance journal entries once the vendor created.
|
||||||
// */
|
* @param {IVendorEventCreatedPayload} payload -
|
||||||
// public attach(bus) {
|
*/
|
||||||
// bus.subscribe(
|
@OnEvent(events.vendors.onCreated)
|
||||||
// events.vendors.onCreated,
|
public async handleWriteOpeningBalanceEntries({
|
||||||
// this.handleWriteOpeningBalanceEntries
|
vendor,
|
||||||
// );
|
trx,
|
||||||
// bus.subscribe(
|
}: IVendorEventCreatedPayload) {
|
||||||
// events.vendors.onDeleted,
|
// Writes the vendor opening balance journal entries.
|
||||||
// this.handleRevertOpeningBalanceEntries
|
if (vendor.openingBalance) {
|
||||||
// );
|
await this.vendorGLEntriesStorage.writeVendorOpeningBalance(
|
||||||
// bus.subscribe(
|
vendor.id,
|
||||||
// events.vendors.onOpeningBalanceChanged,
|
trx,
|
||||||
// this.handleRewriteOpeningEntriesOnChanged
|
);
|
||||||
// );
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Writes the open balance journal entries once the vendor created.
|
* Revert the opening balance journal entries once the vendor deleted.
|
||||||
// * @param {IVendorEventCreatedPayload} payload -
|
* @param {IVendorEventDeletedPayload} payload -
|
||||||
// */
|
*/
|
||||||
// private handleWriteOpeningBalanceEntries = async ({
|
@OnEvent(events.vendors.onDeleted)
|
||||||
// tenantId,
|
public async handleRevertOpeningBalanceEntries({
|
||||||
// vendor,
|
vendorId,
|
||||||
// trx,
|
trx,
|
||||||
// }: IVendorEventCreatedPayload) => {
|
}: IVendorEventDeletedPayload) {
|
||||||
// // Writes the vendor opening balance journal entries.
|
await this.vendorGLEntriesStorage.revertVendorOpeningBalance(
|
||||||
// if (vendor.openingBalance) {
|
vendorId,
|
||||||
// await this.vendorGLEntriesStorage.writeVendorOpeningBalance(
|
trx,
|
||||||
// tenantId,
|
);
|
||||||
// vendor.id,
|
}
|
||||||
// trx
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Revert the opening balance journal entries once the vendor deleted.
|
* Handles the rewrite opening balance entries once opening balance changed.
|
||||||
// * @param {IVendorEventDeletedPayload} payload -
|
* @param {IVendorOpeningBalanceEditedPayload} payload -
|
||||||
// */
|
*/
|
||||||
// private handleRevertOpeningBalanceEntries = async ({
|
@OnEvent(events.vendors.onOpeningBalanceChanged)
|
||||||
// tenantId,
|
public async handleRewriteOpeningEntriesOnChanged({
|
||||||
// vendorId,
|
vendor,
|
||||||
// trx,
|
trx,
|
||||||
// }: IVendorEventDeletedPayload) => {
|
}: IVendorOpeningBalanceEditedPayload) {
|
||||||
// await this.vendorGLEntriesStorage.revertVendorOpeningBalance(
|
if (vendor.openingBalance) {
|
||||||
// tenantId,
|
await this.vendorGLEntriesStorage.rewriteVendorOpeningBalance(
|
||||||
// vendorId,
|
vendor.id,
|
||||||
// trx
|
trx,
|
||||||
// );
|
);
|
||||||
// };
|
} else {
|
||||||
|
await this.vendorGLEntriesStorage.revertVendorOpeningBalance(
|
||||||
// /**
|
vendor.id,
|
||||||
// * Handles the rewrite opening balance entries once opening balnace changed.
|
trx,
|
||||||
// * @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
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|||||||
Reference in New Issue
Block a user