mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-22 15:50:32 +00:00
refactor: migrate ledger subscribers to nestjs
This commit is contained in:
@@ -18,8 +18,8 @@ import { TemplateInjectableModule } from '../TemplateInjectable/TemplateInjectab
|
|||||||
import { GetCreditNote } from './queries/GetCreditNote.service';
|
import { GetCreditNote } from './queries/GetCreditNote.service';
|
||||||
import { CreditNoteBrandingTemplate } from './queries/CreditNoteBrandingTemplate.service';
|
import { CreditNoteBrandingTemplate } from './queries/CreditNoteBrandingTemplate.service';
|
||||||
import { AutoIncrementOrdersModule } from '../AutoIncrementOrders/AutoIncrementOrders.module';
|
import { AutoIncrementOrdersModule } from '../AutoIncrementOrders/AutoIncrementOrders.module';
|
||||||
import CreditNoteGLEntries from './commands/CreditNoteGLEntries';
|
import { CreditNoteGLEntries } from './commands/CreditNoteGLEntries';
|
||||||
import CreditNoteGLEntriesSubscriber from './subscribers/CreditNoteGLEntriesSubscriber';
|
import { CreditNoteGLEntriesSubscriber } from './subscribers/CreditNoteGLEntriesSubscriber';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -29,7 +29,7 @@ import CreditNoteGLEntriesSubscriber from './subscribers/CreditNoteGLEntriesSubs
|
|||||||
PdfTemplatesModule,
|
PdfTemplatesModule,
|
||||||
ChromiumlyTenancyModule,
|
ChromiumlyTenancyModule,
|
||||||
TemplateInjectableModule,
|
TemplateInjectableModule,
|
||||||
AutoIncrementOrdersModule
|
AutoIncrementOrdersModule,
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
CreateCreditNoteService,
|
CreateCreditNoteService,
|
||||||
@@ -44,7 +44,7 @@ import CreditNoteGLEntriesSubscriber from './subscribers/CreditNoteGLEntriesSubs
|
|||||||
CreditNoteApplication,
|
CreditNoteApplication,
|
||||||
CreditNoteBrandingTemplate,
|
CreditNoteBrandingTemplate,
|
||||||
CreditNoteGLEntries,
|
CreditNoteGLEntries,
|
||||||
CreditNoteGLEntriesSubscriber
|
CreditNoteGLEntriesSubscriber,
|
||||||
],
|
],
|
||||||
exports: [
|
exports: [
|
||||||
CreateCreditNoteService,
|
CreateCreditNoteService,
|
||||||
@@ -57,7 +57,7 @@ import CreditNoteGLEntriesSubscriber from './subscribers/CreditNoteGLEntriesSubs
|
|||||||
CreditNoteAutoIncrementService,
|
CreditNoteAutoIncrementService,
|
||||||
GetCreditNoteState,
|
GetCreditNoteState,
|
||||||
CreditNoteApplication,
|
CreditNoteApplication,
|
||||||
CreditNoteBrandingTemplate
|
CreditNoteBrandingTemplate,
|
||||||
],
|
],
|
||||||
controllers: [CreditNotesController],
|
controllers: [CreditNotesController],
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -0,0 +1,186 @@
|
|||||||
|
import { ILedgerEntry } from '@/modules/Ledger/types/Ledger.types';
|
||||||
|
import { CreditNote } from '../models/CreditNote';
|
||||||
|
import { AccountNormal } from '@/interfaces/Account';
|
||||||
|
import { Ledger } from '@/modules/Ledger/Ledger';
|
||||||
|
import { ItemEntry } from '@/modules/Items/models/ItemEntry';
|
||||||
|
|
||||||
|
export class CreditNoteGL {
|
||||||
|
creditNoteModel: CreditNote;
|
||||||
|
ARAccountId: number;
|
||||||
|
discountAccountId: number;
|
||||||
|
adjustmentAccountId: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {CreditNote} creditNoteModel - Credit note model.
|
||||||
|
*/
|
||||||
|
constructor(creditNoteModel: CreditNote) {
|
||||||
|
this.creditNoteModel = creditNoteModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the A/R account id.
|
||||||
|
* @param {number} ARAccountId - A/R account id.
|
||||||
|
*/
|
||||||
|
public setARAccountId(ARAccountId: number) {
|
||||||
|
this.ARAccountId = ARAccountId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the discount account id.
|
||||||
|
* @param {number} discountAccountId - Discount account id.
|
||||||
|
*/
|
||||||
|
public setDiscountAccountId(discountAccountId: number) {
|
||||||
|
this.discountAccountId = discountAccountId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the adjustment account id.
|
||||||
|
* @param {number} adjustmentAccountId - Adjustment account id.
|
||||||
|
*/
|
||||||
|
public setAdjustmentAccountId(adjustmentAccountId: number) {
|
||||||
|
this.adjustmentAccountId = adjustmentAccountId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the credit note common entry.
|
||||||
|
* @returns {ICreditNoteGLCommonEntry}
|
||||||
|
*/
|
||||||
|
private get creditNoteCommonEntry() {
|
||||||
|
return {
|
||||||
|
date: this.creditNoteModel.creditNoteDate,
|
||||||
|
userId: this.creditNoteModel.userId,
|
||||||
|
currencyCode: this.creditNoteModel.currencyCode,
|
||||||
|
exchangeRate: this.creditNoteModel.exchangeRate,
|
||||||
|
|
||||||
|
transactionType: 'CreditNote',
|
||||||
|
transactionId: this.creditNoteModel.id,
|
||||||
|
|
||||||
|
transactionNumber: this.creditNoteModel.creditNoteNumber,
|
||||||
|
referenceNumber: this.creditNoteModel.referenceNo,
|
||||||
|
|
||||||
|
createdAt: this.creditNoteModel.createdAt,
|
||||||
|
indexGroup: 10,
|
||||||
|
|
||||||
|
credit: 0,
|
||||||
|
debit: 0,
|
||||||
|
|
||||||
|
branchId: this.creditNoteModel.branchId,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the creidt note A/R entry.
|
||||||
|
* @param {ICreditNote} creditNote -
|
||||||
|
* @param {number} ARAccountId -
|
||||||
|
* @returns {ILedgerEntry}
|
||||||
|
*/
|
||||||
|
private get creditNoteAREntry() {
|
||||||
|
const commonEntry = this.creditNoteCommonEntry;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...commonEntry,
|
||||||
|
credit: this.creditNoteModel.totalLocal,
|
||||||
|
accountId: this.ARAccountId,
|
||||||
|
contactId: this.creditNoteModel.customerId,
|
||||||
|
index: 1,
|
||||||
|
accountNormal: AccountNormal.DEBIT,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the credit note item entry.
|
||||||
|
* @param {ItemEntry} entry
|
||||||
|
* @param {number} index
|
||||||
|
* @returns {ILedgerEntry}
|
||||||
|
*/
|
||||||
|
private getCreditNoteItemEntry(
|
||||||
|
entry: ItemEntry,
|
||||||
|
index: number,
|
||||||
|
): ILedgerEntry {
|
||||||
|
const commonEntry = this.creditNoteCommonEntry;
|
||||||
|
const totalLocal =
|
||||||
|
entry.totalExcludingTax * this.creditNoteModel.exchangeRate;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...commonEntry,
|
||||||
|
debit: totalLocal,
|
||||||
|
accountId: entry.sellAccountId || entry.item.sellAccountId,
|
||||||
|
note: entry.description,
|
||||||
|
index: index + 2,
|
||||||
|
itemId: entry.itemId,
|
||||||
|
itemQuantity: entry.quantity,
|
||||||
|
accountNormal: AccountNormal.CREDIT,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the credit note discount entry.
|
||||||
|
* @param {ICreditNote} creditNote
|
||||||
|
* @param {number} discountAccountId
|
||||||
|
* @returns {ILedgerEntry}
|
||||||
|
*/
|
||||||
|
private get discountEntry(): ILedgerEntry {
|
||||||
|
const commonEntry = this.creditNoteCommonEntry;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...commonEntry,
|
||||||
|
credit: this.creditNoteModel.discountAmountLocal,
|
||||||
|
accountId: this.discountAccountId,
|
||||||
|
accountNormal: AccountNormal.CREDIT,
|
||||||
|
index: 1,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the credit note adjustment entry.
|
||||||
|
* @param {ICreditNote} creditNote
|
||||||
|
* @param {number} adjustmentAccountId
|
||||||
|
* @returns {ILedgerEntry}
|
||||||
|
*/
|
||||||
|
private get adjustmentEntry(): ILedgerEntry {
|
||||||
|
const commonEntry = this.creditNoteCommonEntry;
|
||||||
|
const adjustmentAmount = Math.abs(this.creditNoteModel.adjustmentLocal);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...commonEntry,
|
||||||
|
credit: this.creditNoteModel.adjustmentLocal < 0 ? adjustmentAmount : 0,
|
||||||
|
debit: this.creditNoteModel.adjustmentLocal > 0 ? adjustmentAmount : 0,
|
||||||
|
accountId: this.adjustmentAccountId,
|
||||||
|
accountNormal: AccountNormal.CREDIT,
|
||||||
|
index: 1,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the credit note GL entries.
|
||||||
|
* @param {ICreditNote} creditNote - Credit note.
|
||||||
|
* @param {IAccount} receivableAccount - Receviable account.
|
||||||
|
* @returns {ILedgerEntry[]} - Ledger entries.
|
||||||
|
*/
|
||||||
|
public getCreditNoteGLEntries(): ILedgerEntry[] {
|
||||||
|
const AREntry = this.creditNoteAREntry;
|
||||||
|
|
||||||
|
const getItemEntry = this.getCreditNoteItemEntry;
|
||||||
|
const itemsEntries = this.creditNoteModel.entries.map(getItemEntry);
|
||||||
|
|
||||||
|
const discountEntry = this.discountEntry;
|
||||||
|
const adjustmentEntry = this.adjustmentEntry;
|
||||||
|
|
||||||
|
return [AREntry, discountEntry, adjustmentEntry, ...itemsEntries];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the credit note GL.
|
||||||
|
* @param {ICreditNote} creditNote
|
||||||
|
* @param {number} receivableAccount
|
||||||
|
* @returns {Ledger}
|
||||||
|
*/
|
||||||
|
public getCreditNoteLedger(): Ledger {
|
||||||
|
const ledgerEntries = this.getCreditNoteGLEntries();
|
||||||
|
|
||||||
|
return new Ledger(ledgerEntries);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,137 +1,75 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { Knex } from 'knex';
|
import { Knex } from 'knex';
|
||||||
import * as R from 'ramda';
|
import { CreditNoteGL } from './CreditNoteGL';
|
||||||
import {
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
AccountNormal,
|
import { LedgerStorageService } from '@/modules/Ledger/LedgerStorage.service';
|
||||||
IItemEntry,
|
import { CreditNote } from '../models/CreditNote';
|
||||||
ILedgerEntry,
|
import { AccountRepository } from '@/modules/Accounts/repositories/Account.repository';
|
||||||
ICreditNote,
|
|
||||||
ILedger,
|
|
||||||
ICreditNoteGLCommonEntry,
|
|
||||||
} from '@/interfaces';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import Ledger from '@/services/Accounting/Ledger';
|
|
||||||
import LedgerStorageService from '@/services/Accounting/LedgerStorageService';
|
|
||||||
import { SaleReceipt } from '@/models';
|
|
||||||
|
|
||||||
@Service()
|
@Injectable()
|
||||||
export default class CreditNoteGLEntries {
|
export class CreditNoteGLEntries {
|
||||||
@Inject()
|
constructor(
|
||||||
private tenancy: HasTenancyService;
|
private readonly ledgerStorage: LedgerStorageService,
|
||||||
|
private readonly accountRepository: AccountRepository,
|
||||||
|
|
||||||
@Inject()
|
@Inject(CreditNote.name)
|
||||||
private ledgerStorage: LedgerStorageService;
|
private readonly creditNoteModel: typeof CreditNote,
|
||||||
|
) {}
|
||||||
/**
|
|
||||||
* Retrieves the credit note GL.
|
|
||||||
* @param {ICreditNote} creditNote
|
|
||||||
* @param {number} receivableAccount
|
|
||||||
* @returns {Ledger}
|
|
||||||
*/
|
|
||||||
private getCreditNoteGLedger = (
|
|
||||||
creditNote: ICreditNote,
|
|
||||||
receivableAccount: number,
|
|
||||||
discountAccount: number,
|
|
||||||
adjustmentAccount: number
|
|
||||||
): Ledger => {
|
|
||||||
const ledgerEntries = this.getCreditNoteGLEntries(
|
|
||||||
creditNote,
|
|
||||||
receivableAccount,
|
|
||||||
discountAccount,
|
|
||||||
adjustmentAccount
|
|
||||||
);
|
|
||||||
return new Ledger(ledgerEntries);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Saves credit note GL entries.
|
|
||||||
* @param {number} tenantId -
|
|
||||||
* @param {ICreditNote} creditNote - Credit note model.
|
|
||||||
* @param {number} payableAccount - Payable account id.
|
|
||||||
* @param {Knex.Transaction} trx
|
|
||||||
*/
|
|
||||||
public saveCreditNoteGLEntries = async (
|
|
||||||
tenantId: number,
|
|
||||||
creditNote: ICreditNote,
|
|
||||||
payableAccount: number,
|
|
||||||
discountAccount: number,
|
|
||||||
adjustmentAccount: number,
|
|
||||||
trx?: Knex.Transaction
|
|
||||||
): Promise<void> => {
|
|
||||||
const ledger = this.getCreditNoteGLedger(
|
|
||||||
creditNote,
|
|
||||||
payableAccount,
|
|
||||||
discountAccount,
|
|
||||||
adjustmentAccount
|
|
||||||
);
|
|
||||||
|
|
||||||
await this.ledgerStorage.commit(tenantId, ledger, trx);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reverts the credit note associated GL entries.
|
* Reverts the credit note associated GL entries.
|
||||||
* @param {number} tenantId
|
* @param {number} creditNoteId - Credit note id.
|
||||||
* @param {number} vendorCreditId
|
|
||||||
* @param {Knex.Transaction} trx
|
* @param {Knex.Transaction} trx
|
||||||
*/
|
*/
|
||||||
public revertVendorCreditGLEntries = async (
|
public revertVendorCreditGLEntries = async (
|
||||||
creditNoteId: number,
|
creditNoteId: number,
|
||||||
trx?: Knex.Transaction
|
trx?: Knex.Transaction,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
await this.ledgerStorage.deleteByReference(
|
await this.ledgerStorage.deleteByReference(creditNoteId, 'CreditNote', trx);
|
||||||
creditNoteId,
|
|
||||||
'CreditNote',
|
|
||||||
trx
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes vendor credit associated GL entries.
|
* Writes vendor credit associated GL entries.
|
||||||
* @param {number} tenantId - Tenant id.
|
|
||||||
* @param {number} creditNoteId - Credit note id.
|
* @param {number} creditNoteId - Credit note id.
|
||||||
* @param {Knex.Transaction} trx - Knex transactions.
|
* @param {Knex.Transaction} trx - Knex transactions.
|
||||||
*/
|
*/
|
||||||
public createVendorCreditGLEntries = async (
|
public createVendorCreditGLEntries = async (
|
||||||
creditNoteId: number,
|
creditNoteId: number,
|
||||||
trx?: Knex.Transaction
|
trx?: Knex.Transaction,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
const { CreditNote } = this.tenancy.models(tenantId);
|
|
||||||
const { accountRepository } = this.tenancy.repositories(tenantId);
|
|
||||||
|
|
||||||
// Retrieve the credit note with associated entries and items.
|
// Retrieve the credit note with associated entries and items.
|
||||||
const creditNoteWithItems = await CreditNote.query(trx)
|
const creditNoteWithItems = await CreditNote.query(trx)
|
||||||
.findById(creditNoteId)
|
.findById(creditNoteId)
|
||||||
.withGraphFetched('entries.item');
|
.withGraphFetched('entries.item');
|
||||||
|
|
||||||
// Retreive the the `accounts receivable` account based on the given currency.
|
// Retreive the the `accounts receivable` account based on the given currency.
|
||||||
const ARAccount = await accountRepository.findOrCreateAccountReceivable(
|
const ARAccount =
|
||||||
creditNoteWithItems.currencyCode
|
await this.accountRepository.findOrCreateAccountReceivable(
|
||||||
);
|
creditNoteWithItems.currencyCode,
|
||||||
const discountAccount = await accountRepository.findOrCreateDiscountAccount(
|
);
|
||||||
{}
|
const discountAccount =
|
||||||
);
|
await this.accountRepository.findOrCreateDiscountAccount({});
|
||||||
|
|
||||||
const adjustmentAccount =
|
const adjustmentAccount =
|
||||||
await accountRepository.findOrCreateOtherChargesAccount({});
|
await this.accountRepository.findOrCreateOtherChargesAccount({});
|
||||||
|
|
||||||
|
const creditNoteLedger = new CreditNoteGL(creditNoteWithItems)
|
||||||
|
.setARAccountId(ARAccount.id)
|
||||||
|
.setDiscountAccountId(discountAccount.id)
|
||||||
|
.setAdjustmentAccountId(adjustmentAccount.id)
|
||||||
|
.getCreditNoteLedger();
|
||||||
|
|
||||||
// Saves the credit note GL entries.
|
// Saves the credit note GL entries.
|
||||||
await this.saveCreditNoteGLEntries(
|
await this.ledgerStorage.commit(creditNoteLedger, trx);
|
||||||
tenantId,
|
|
||||||
creditNoteWithItems,
|
|
||||||
ARAccount.id,
|
|
||||||
discountAccount.id,
|
|
||||||
adjustmentAccount.id,
|
|
||||||
trx
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Edits vendor credit associated GL entries.
|
* Edits vendor credit associated GL entries.
|
||||||
* @param {number} tenantId
|
* @param {number} creditNoteId - Credit note id.
|
||||||
* @param {number} creditNoteId
|
|
||||||
* @param {Knex.Transaction} trx
|
* @param {Knex.Transaction} trx
|
||||||
*/
|
*/
|
||||||
public editVendorCreditGLEntries = async (
|
public editVendorCreditGLEntries = async (
|
||||||
creditNoteId: number,
|
creditNoteId: number,
|
||||||
trx?: Knex.Transaction
|
trx?: Knex.Transaction,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
// Reverts vendor credit GL entries.
|
// Reverts vendor credit GL entries.
|
||||||
await this.revertVendorCreditGLEntries(creditNoteId, trx);
|
await this.revertVendorCreditGLEntries(creditNoteId, trx);
|
||||||
@@ -139,155 +77,4 @@ export default class CreditNoteGLEntries {
|
|||||||
// Creates vendor credit Gl entries.
|
// Creates vendor credit Gl entries.
|
||||||
await this.createVendorCreditGLEntries(creditNoteId, trx);
|
await this.createVendorCreditGLEntries(creditNoteId, trx);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the credit note common entry.
|
|
||||||
* @param {ICreditNote} creditNote -
|
|
||||||
* @returns {ICreditNoteGLCommonEntry}
|
|
||||||
*/
|
|
||||||
private getCreditNoteCommonEntry = (
|
|
||||||
creditNote: ICreditNote
|
|
||||||
): ICreditNoteGLCommonEntry => {
|
|
||||||
return {
|
|
||||||
date: creditNote.creditNoteDate,
|
|
||||||
userId: creditNote.userId,
|
|
||||||
currencyCode: creditNote.currencyCode,
|
|
||||||
exchangeRate: creditNote.exchangeRate,
|
|
||||||
|
|
||||||
transactionType: 'CreditNote',
|
|
||||||
transactionId: creditNote.id,
|
|
||||||
|
|
||||||
transactionNumber: creditNote.creditNoteNumber,
|
|
||||||
referenceNumber: creditNote.referenceNo,
|
|
||||||
|
|
||||||
createdAt: creditNote.createdAt,
|
|
||||||
indexGroup: 10,
|
|
||||||
|
|
||||||
credit: 0,
|
|
||||||
debit: 0,
|
|
||||||
|
|
||||||
branchId: creditNote.branchId,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the creidt note A/R entry.
|
|
||||||
* @param {ICreditNote} creditNote -
|
|
||||||
* @param {number} ARAccountId -
|
|
||||||
* @returns {ILedgerEntry}
|
|
||||||
*/
|
|
||||||
private getCreditNoteAREntry = (
|
|
||||||
creditNote: ICreditNote,
|
|
||||||
ARAccountId: number
|
|
||||||
): ILedgerEntry => {
|
|
||||||
const commonEntry = this.getCreditNoteCommonEntry(creditNote);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...commonEntry,
|
|
||||||
credit: creditNote.totalLocal,
|
|
||||||
accountId: ARAccountId,
|
|
||||||
contactId: creditNote.customerId,
|
|
||||||
index: 1,
|
|
||||||
accountNormal: AccountNormal.DEBIT,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the credit note item entry.
|
|
||||||
* @param {ICreditNote} creditNote
|
|
||||||
* @param {IItemEntry} entry
|
|
||||||
* @param {number} index
|
|
||||||
* @returns {ILedgerEntry}
|
|
||||||
*/
|
|
||||||
private getCreditNoteItemEntry = R.curry(
|
|
||||||
(
|
|
||||||
creditNote: ICreditNote,
|
|
||||||
entry: IItemEntry,
|
|
||||||
index: number
|
|
||||||
): ILedgerEntry => {
|
|
||||||
const commonEntry = this.getCreditNoteCommonEntry(creditNote);
|
|
||||||
const totalLocal = entry.totalExcludingTax * creditNote.exchangeRate;
|
|
||||||
|
|
||||||
return {
|
|
||||||
...commonEntry,
|
|
||||||
debit: totalLocal,
|
|
||||||
accountId: entry.sellAccountId || entry.item.sellAccountId,
|
|
||||||
note: entry.description,
|
|
||||||
index: index + 2,
|
|
||||||
itemId: entry.itemId,
|
|
||||||
itemQuantity: entry.quantity,
|
|
||||||
accountNormal: AccountNormal.CREDIT,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the credit note discount entry.
|
|
||||||
* @param {ICreditNote} creditNote
|
|
||||||
* @param {number} discountAccountId
|
|
||||||
* @returns {ILedgerEntry}
|
|
||||||
*/
|
|
||||||
private getDiscountEntry = (
|
|
||||||
creditNote: ICreditNote,
|
|
||||||
discountAccountId: number
|
|
||||||
): ILedgerEntry => {
|
|
||||||
const commonEntry = this.getCreditNoteCommonEntry(creditNote);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...commonEntry,
|
|
||||||
credit: creditNote.discountAmountLocal,
|
|
||||||
accountId: discountAccountId,
|
|
||||||
index: 1,
|
|
||||||
accountNormal: AccountNormal.CREDIT,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the credit note adjustment entry.
|
|
||||||
* @param {ICreditNote} creditNote
|
|
||||||
* @param {number} adjustmentAccountId
|
|
||||||
* @returns {ILedgerEntry}
|
|
||||||
*/
|
|
||||||
private getAdjustmentEntry = (
|
|
||||||
creditNote: ICreditNote,
|
|
||||||
adjustmentAccountId: number
|
|
||||||
): ILedgerEntry => {
|
|
||||||
const commonEntry = this.getCreditNoteCommonEntry(creditNote);
|
|
||||||
const adjustmentAmount = Math.abs(creditNote.adjustmentLocal);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...commonEntry,
|
|
||||||
credit: creditNote.adjustmentLocal < 0 ? adjustmentAmount : 0,
|
|
||||||
debit: creditNote.adjustmentLocal > 0 ? adjustmentAmount : 0,
|
|
||||||
accountId: adjustmentAccountId,
|
|
||||||
accountNormal: AccountNormal.CREDIT,
|
|
||||||
index: 1,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the credit note GL entries.
|
|
||||||
* @param {ICreditNote} creditNote - Credit note.
|
|
||||||
* @param {IAccount} receivableAccount - Receviable account.
|
|
||||||
* @returns {ILedgerEntry[]} - Ledger entries.
|
|
||||||
*/
|
|
||||||
public getCreditNoteGLEntries = (
|
|
||||||
creditNote: ICreditNote,
|
|
||||||
ARAccountId: number,
|
|
||||||
discountAccountId: number,
|
|
||||||
adjustmentAccountId: number
|
|
||||||
): ILedgerEntry[] => {
|
|
||||||
const AREntry = this.getCreditNoteAREntry(creditNote, ARAccountId);
|
|
||||||
|
|
||||||
const getItemEntry = this.getCreditNoteItemEntry(creditNote);
|
|
||||||
const itemsEntries = creditNote.entries.map(getItemEntry);
|
|
||||||
|
|
||||||
const discountEntry = this.getDiscountEntry(creditNote, discountAccountId);
|
|
||||||
const adjustmentEntry = this.getAdjustmentEntry(
|
|
||||||
creditNote,
|
|
||||||
adjustmentAccountId
|
|
||||||
);
|
|
||||||
|
|
||||||
return [AREntry, discountEntry, adjustmentEntry, ...itemsEntries];
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ export class CreditNote extends BaseModel {
|
|||||||
public invoicesAmount: number;
|
public invoicesAmount: number;
|
||||||
public creditNoteDate: Date;
|
public creditNoteDate: Date;
|
||||||
public creditNoteNumber: string;
|
public creditNoteNumber: string;
|
||||||
|
public referenceNo: string;
|
||||||
public currencyCode: string;
|
public currencyCode: string;
|
||||||
public customerId: number;
|
public customerId: number;
|
||||||
|
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ import {
|
|||||||
ICreditNoteEditedPayload,
|
ICreditNoteEditedPayload,
|
||||||
ICreditNoteOpenedPayload,
|
ICreditNoteOpenedPayload,
|
||||||
} from '../types/CreditNotes.types';
|
} from '../types/CreditNotes.types';
|
||||||
import CreditNoteGLEntries from '../commands/CreditNoteGLEntries';
|
import { CreditNoteGLEntries } from '../commands/CreditNoteGLEntries';
|
||||||
import { OnEvent } from '@nestjs/event-emitter';
|
import { OnEvent } from '@nestjs/event-emitter';
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { events } from '@/common/events/events';
|
import { events } from '@/common/events/events';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export default class CreditNoteGLEntriesSubscriber {
|
export class CreditNoteGLEntriesSubscriber {
|
||||||
constructor(private readonly creditNoteGLEntries: CreditNoteGLEntries) {}
|
constructor(private readonly creditNoteGLEntries: CreditNoteGLEntries) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ import { ILedger } from '@/modules/Ledger/types/Ledger.types';
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ExpenseGLEntriesService {
|
export class ExpenseGLEntriesService {
|
||||||
|
/**
|
||||||
|
* @param {typeof Expense} expense - Expense model.
|
||||||
|
*/
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(Expense.name)
|
@Inject(Expense.name)
|
||||||
private readonly expense: typeof Expense,
|
private readonly expense: typeof Expense,
|
||||||
@@ -33,7 +36,7 @@ export class ExpenseGLEntriesService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the given expense ledger.
|
* Retrieves the given expense ledger.
|
||||||
* @param {IExpense} expense
|
* @param {Expense} expense - Expense model.
|
||||||
* @returns {ILedger}
|
* @returns {ILedger}
|
||||||
*/
|
*/
|
||||||
public getExpenseLedger = (expense: Expense): ILedger => {
|
public getExpenseLedger = (expense: Expense): ILedger => {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import { events } from '@/common/events/events';
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class ExpensesWriteGLSubscriber {
|
export class ExpensesWriteGLSubscriber {
|
||||||
/**
|
/**
|
||||||
* @param {ExpenseGLEntriesStorageService} expenseGLEntries -
|
* @param {ExpenseGLEntriesStorageService} expenseGLEntries - Expense GL entries storage service.
|
||||||
*/
|
*/
|
||||||
constructor(
|
constructor(
|
||||||
private readonly expenseGLEntries: ExpenseGLEntriesStorageService,
|
private readonly expenseGLEntries: ExpenseGLEntriesStorageService,
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ export interface ILedgerEntry {
|
|||||||
taxRate?: number;
|
taxRate?: number;
|
||||||
|
|
||||||
entryId?: number;
|
entryId?: number;
|
||||||
createdAt?: Date;
|
createdAt?: Date | string;
|
||||||
|
|
||||||
costable?: boolean;
|
costable?: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
|
|
||||||
import { PaymentReceivesController } from './PaymentsReceived.controller';
|
import { PaymentReceivesController } from './PaymentsReceived.controller';
|
||||||
import { PaymentReceivesApplication } from './PaymentReceived.application';
|
import { PaymentReceivesApplication } from './PaymentReceived.application';
|
||||||
import { CreatePaymentReceivedService } from './commands/CreatePaymentReceived.serivce';
|
import { CreatePaymentReceivedService } from './commands/CreatePaymentReceived.serivce';
|
||||||
@@ -8,7 +7,7 @@ import { EditPaymentReceivedService } from './commands/EditPaymentReceived.servi
|
|||||||
import { GetPaymentReceivedStateService } from './queries/GetPaymentReceivedState.service';
|
import { GetPaymentReceivedStateService } from './queries/GetPaymentReceivedState.service';
|
||||||
import { GetPaymentReceivedService } from './queries/GetPaymentReceived.service';
|
import { GetPaymentReceivedService } from './queries/GetPaymentReceived.service';
|
||||||
import { GetPaymentReceivedInvoices } from './queries/GetPaymentReceivedInvoices.service';
|
import { GetPaymentReceivedInvoices } from './queries/GetPaymentReceivedInvoices.service';
|
||||||
import GetPaymentReceivedPdf from './queries/GetPaymentReceivedPdf.service';
|
import { GetPaymentReceivedPdfService } from './queries/GetPaymentReceivedPdf.service';
|
||||||
import { PaymentReceivedValidators } from './commands/PaymentReceivedValidators.service';
|
import { PaymentReceivedValidators } from './commands/PaymentReceivedValidators.service';
|
||||||
import { PaymentReceiveDTOTransformer } from './commands/PaymentReceivedDTOTransformer';
|
import { PaymentReceiveDTOTransformer } from './commands/PaymentReceivedDTOTransformer';
|
||||||
import { TenancyContext } from '../Tenancy/TenancyContext.service';
|
import { TenancyContext } from '../Tenancy/TenancyContext.service';
|
||||||
@@ -20,6 +19,11 @@ import { BranchesModule } from '../Branches/Branches.module';
|
|||||||
import { WarehousesModule } from '../Warehouses/Warehouses.module';
|
import { WarehousesModule } from '../Warehouses/Warehouses.module';
|
||||||
import { PdfTemplatesModule } from '../PdfTemplate/PdfTemplates.module';
|
import { PdfTemplatesModule } from '../PdfTemplate/PdfTemplates.module';
|
||||||
import { AutoIncrementOrdersModule } from '../AutoIncrementOrders/AutoIncrementOrders.module';
|
import { AutoIncrementOrdersModule } from '../AutoIncrementOrders/AutoIncrementOrders.module';
|
||||||
|
import { PaymentReceivedAutoIncrementSubscriber } from './subscribers/PaymentReceivedAutoIncrementSubscriber';
|
||||||
|
import { PaymentReceivedGLEntriesSubscriber } from './subscribers/PaymentReceivedGLEntriesSubscriber';
|
||||||
|
import { PaymentReceivedGLEntries } from './commands/PaymentReceivedGLEntries';
|
||||||
|
import { PaymentReceivedSyncInvoicesSubscriber } from './subscribers/PaymentReceivedSyncInvoices';
|
||||||
|
import { PaymentReceivedInvoiceSync } from './commands/PaymentReceivedInvoiceSync.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
controllers: [PaymentReceivesController],
|
controllers: [PaymentReceivesController],
|
||||||
@@ -31,12 +35,17 @@ import { AutoIncrementOrdersModule } from '../AutoIncrementOrders/AutoIncrementO
|
|||||||
GetPaymentReceivedStateService,
|
GetPaymentReceivedStateService,
|
||||||
GetPaymentReceivedService,
|
GetPaymentReceivedService,
|
||||||
GetPaymentReceivedInvoices,
|
GetPaymentReceivedInvoices,
|
||||||
GetPaymentReceivedPdf,
|
GetPaymentReceivedPdfService,
|
||||||
PaymentReceivedValidators,
|
PaymentReceivedValidators,
|
||||||
PaymentReceiveDTOTransformer,
|
PaymentReceiveDTOTransformer,
|
||||||
PaymentReceivedBrandingTemplate,
|
PaymentReceivedBrandingTemplate,
|
||||||
PaymentReceivedIncrement,
|
PaymentReceivedIncrement,
|
||||||
|
PaymentReceivedGLEntries,
|
||||||
TenancyContext,
|
TenancyContext,
|
||||||
|
PaymentReceivedInvoiceSync,
|
||||||
|
PaymentReceivedAutoIncrementSubscriber,
|
||||||
|
PaymentReceivedGLEntriesSubscriber,
|
||||||
|
PaymentReceivedSyncInvoicesSubscriber,
|
||||||
],
|
],
|
||||||
exports: [PaymentReceivesApplication],
|
exports: [PaymentReceivesApplication],
|
||||||
imports: [
|
imports: [
|
||||||
@@ -45,7 +54,7 @@ import { AutoIncrementOrdersModule } from '../AutoIncrementOrders/AutoIncrementO
|
|||||||
BranchesModule,
|
BranchesModule,
|
||||||
WarehousesModule,
|
WarehousesModule,
|
||||||
PdfTemplatesModule,
|
PdfTemplatesModule,
|
||||||
AutoIncrementOrdersModule
|
AutoIncrementOrdersModule,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class PaymentsReceivedModule {}
|
export class PaymentsReceivedModule {}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ export class CreatePaymentReceivedService {
|
|||||||
/**
|
/**
|
||||||
* Creates a new payment receive and store it to the storage
|
* Creates a new payment receive and store it to the storage
|
||||||
* with associated invoices payment and journal transactions.
|
* with associated invoices payment and journal transactions.
|
||||||
* @param {IPaymentReceivedCreateDTO} paymentReceiveDTO
|
* @param {IPaymentReceivedCreateDTO} paymentReceiveDTO - Payment receive create DTO.
|
||||||
* @param {Knex.Transaction} trx - Database transaction.
|
* @param {Knex.Transaction} trx - Database transaction.
|
||||||
*/
|
*/
|
||||||
public async createPaymentReceived(
|
public async createPaymentReceived(
|
||||||
|
|||||||
@@ -18,16 +18,31 @@ export class PaymentReceivedGL {
|
|||||||
this.paymentReceived = paymentReceived;
|
this.paymentReceived = paymentReceived;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the A/R account ID.
|
||||||
|
* @param {number} ARAccountId - A/R account ID.
|
||||||
|
* @returns {PaymentReceivedGL}
|
||||||
|
*/
|
||||||
setARAccountId(ARAccountId: number) {
|
setARAccountId(ARAccountId: number) {
|
||||||
this.ARAccountId = ARAccountId;
|
this.ARAccountId = ARAccountId;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the exchange gain/loss account ID.
|
||||||
|
* @param {number} exchangeGainOrLossAccountId - Exchange gain/loss account ID.
|
||||||
|
* @returns {PaymentReceivedGL}
|
||||||
|
*/
|
||||||
setExchangeGainOrLossAccountId(exchangeGainOrLossAccountId: number) {
|
setExchangeGainOrLossAccountId(exchangeGainOrLossAccountId: number) {
|
||||||
this.exchangeGainOrLossAccountId = exchangeGainOrLossAccountId;
|
this.exchangeGainOrLossAccountId = exchangeGainOrLossAccountId;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the base currency code.
|
||||||
|
* @param {string} baseCurrencyCode - Base currency code.
|
||||||
|
* @returns {PaymentReceivedGL}
|
||||||
|
*/
|
||||||
setBaseCurrencyCode(baseCurrencyCode: string) {
|
setBaseCurrencyCode(baseCurrencyCode: string) {
|
||||||
this.baseCurrencyCode = baseCurrencyCode;
|
this.baseCurrencyCode = baseCurrencyCode;
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import { PaymentReceivedPdfTemplateAttributes } from '../types/PaymentReceived.t
|
|||||||
import { events } from '@/common/events/events';
|
import { events } from '@/common/events/events';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export default class GetPaymentReceivedPdf {
|
export class GetPaymentReceivedPdfService {
|
||||||
constructor(
|
constructor(
|
||||||
private chromiumlyTenancy: ChromiumlyTenancy,
|
private chromiumlyTenancy: ChromiumlyTenancy,
|
||||||
private templateInjectable: TemplateInjectable,
|
private templateInjectable: TemplateInjectable,
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { OnEvent } from '@nestjs/event-emitter';
|
||||||
|
import { events } from '@/common/events/events';
|
||||||
|
import { PaymentReceivedIncrement } from '../commands/PaymentReceivedIncrement.service';
|
||||||
|
import { IPaymentReceivedCreatedPayload } from '../types/PaymentReceived.types';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class PaymentReceivedAutoIncrementSubscriber {
|
||||||
|
constructor(private readonly paymentIncrement: PaymentReceivedIncrement) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles increment next number of payment receive once be created.
|
||||||
|
* @param {IPaymentReceivedCreatedPayload} payload -
|
||||||
|
*/
|
||||||
|
@OnEvent(events.paymentReceive.onCreated)
|
||||||
|
private async handlePaymentNextNumberIncrement({}: IPaymentReceivedCreatedPayload) {
|
||||||
|
await this.paymentIncrement.incrementNextPaymentReceiveNumber();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import {
|
||||||
|
IPaymentReceivedCreatedPayload,
|
||||||
|
IPaymentReceivedDeletedPayload,
|
||||||
|
IPaymentReceivedEditedPayload,
|
||||||
|
} from '../types/PaymentReceived.types';
|
||||||
|
import { PaymentReceivedGLEntries } from '../commands/PaymentReceivedGLEntries';
|
||||||
|
import { OnEvent } from '@nestjs/event-emitter';
|
||||||
|
import { events } from '@/common/events/events';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class PaymentReceivedGLEntriesSubscriber {
|
||||||
|
/**
|
||||||
|
* @param {PaymentReceivedGLEntries} paymentReceivedGLEntries -
|
||||||
|
*/
|
||||||
|
constructor(
|
||||||
|
private readonly paymentReceivedGLEntries: PaymentReceivedGLEntries,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle journal entries writing once the payment receive created.
|
||||||
|
*/
|
||||||
|
@OnEvent(events.paymentReceive.onCreated)
|
||||||
|
private async handleWriteJournalEntriesOnceCreated({
|
||||||
|
paymentReceiveId,
|
||||||
|
trx,
|
||||||
|
}: IPaymentReceivedCreatedPayload) {
|
||||||
|
await this.paymentReceivedGLEntries.writePaymentGLEntries(
|
||||||
|
paymentReceiveId,
|
||||||
|
trx,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle journal entries writing once the payment receive edited.
|
||||||
|
*/
|
||||||
|
@OnEvent(events.paymentReceive.onEdited)
|
||||||
|
private async handleOverwriteJournalEntriesOnceEdited({
|
||||||
|
paymentReceive,
|
||||||
|
trx,
|
||||||
|
}: IPaymentReceivedEditedPayload) {
|
||||||
|
await this.paymentReceivedGLEntries.rewritePaymentGLEntries(
|
||||||
|
paymentReceive.id,
|
||||||
|
trx,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles revert journal entries once deleted.
|
||||||
|
*/
|
||||||
|
@OnEvent(events.paymentReceive.onDeleted)
|
||||||
|
private async handleRevertJournalEntriesOnceDeleted({
|
||||||
|
paymentReceiveId,
|
||||||
|
trx,
|
||||||
|
}: IPaymentReceivedDeletedPayload) {
|
||||||
|
await this.paymentReceivedGLEntries.revertPaymentGLEntries(
|
||||||
|
paymentReceiveId,
|
||||||
|
trx,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
// import { Service, Inject } from 'typedi';
|
||||||
|
// import events from '@/subscribers/events';
|
||||||
|
// import { PaymentReceiveNotifyBySms } from '@/services/Sales/PaymentReceived/PaymentReceivedSmsNotify';
|
||||||
|
// import { IPaymentReceivedCreatedPayload } from '@/interfaces';
|
||||||
|
// import { runAfterTransaction } from '@/services/UnitOfWork/TransactionsHooks';
|
||||||
|
|
||||||
|
// @Service()
|
||||||
|
// export default class SendSmsNotificationPaymentReceive {
|
||||||
|
// @Inject()
|
||||||
|
// private paymentReceiveSmsNotify: PaymentReceiveNotifyBySms;
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * Attach events.
|
||||||
|
// */
|
||||||
|
// public attach(bus) {
|
||||||
|
// bus.subscribe(
|
||||||
|
// events.paymentReceive.onCreated,
|
||||||
|
// this.handleNotifyViaSmsOncePaymentPublish
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * Handles send SMS notification after payment transaction creation.
|
||||||
|
// */
|
||||||
|
// private handleNotifyViaSmsOncePaymentPublish = ({
|
||||||
|
// tenantId,
|
||||||
|
// paymentReceiveId,
|
||||||
|
// trx,
|
||||||
|
// }: IPaymentReceivedCreatedPayload) => {
|
||||||
|
// // Notify via Sms after transactions complete running.
|
||||||
|
// runAfterTransaction(trx, async () => {
|
||||||
|
// try {
|
||||||
|
// await this.paymentReceiveSmsNotify.notifyViaSmsNotificationAfterCreation(
|
||||||
|
// tenantId,
|
||||||
|
// paymentReceiveId
|
||||||
|
// );
|
||||||
|
// } catch (error) { }
|
||||||
|
// });
|
||||||
|
// };
|
||||||
|
// }
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
|
||||||
|
import {
|
||||||
|
IPaymentReceivedCreatedPayload,
|
||||||
|
IPaymentReceivedDeletedPayload,
|
||||||
|
IPaymentReceivedEditedPayload,
|
||||||
|
} from '../types/PaymentReceived.types';
|
||||||
|
import { PaymentReceivedInvoiceSync } from '../commands/PaymentReceivedInvoiceSync.service';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { OnEvent } from '@nestjs/event-emitter';
|
||||||
|
import { events } from '@/common/events/events';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class PaymentReceivedSyncInvoicesSubscriber {
|
||||||
|
/**
|
||||||
|
* @param {PaymentReceivedInvoiceSync} paymentSyncInvoice -
|
||||||
|
*/
|
||||||
|
constructor(
|
||||||
|
private readonly paymentSyncInvoice: PaymentReceivedInvoiceSync,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle sale invoice increment/decrement payment amount
|
||||||
|
* once created, edited or deleted.
|
||||||
|
*/
|
||||||
|
@OnEvent(events.paymentReceive.onCreated)
|
||||||
|
private async handleInvoiceIncrementPaymentOnceCreated({
|
||||||
|
paymentReceive,
|
||||||
|
trx,
|
||||||
|
}: IPaymentReceivedCreatedPayload) {
|
||||||
|
await this.paymentSyncInvoice.saveChangeInvoicePaymentAmount(
|
||||||
|
paymentReceive.entries,
|
||||||
|
null,
|
||||||
|
trx
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle sale invoice increment/decrement payment amount once edited.
|
||||||
|
*/
|
||||||
|
@OnEvent(events.paymentReceive.onEdited)
|
||||||
|
private async handleInvoiceIncrementPaymentOnceEdited({
|
||||||
|
paymentReceive,
|
||||||
|
oldPaymentReceive,
|
||||||
|
trx,
|
||||||
|
}: IPaymentReceivedEditedPayload) {
|
||||||
|
await this.paymentSyncInvoice.saveChangeInvoicePaymentAmount(
|
||||||
|
paymentReceive.entries,
|
||||||
|
oldPaymentReceive?.entries || null,
|
||||||
|
trx
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle revert invoices payment amount once payment receive deleted.
|
||||||
|
*/
|
||||||
|
@OnEvent(events.paymentReceive.onDeleted)
|
||||||
|
private async handleInvoiceDecrementPaymentAmount({
|
||||||
|
paymentReceiveId,
|
||||||
|
oldPaymentReceive,
|
||||||
|
trx,
|
||||||
|
}: IPaymentReceivedDeletedPayload) {
|
||||||
|
await this.paymentSyncInvoice.saveChangeInvoicePaymentAmount(
|
||||||
|
oldPaymentReceive.entries.map((entry) => ({
|
||||||
|
...entry,
|
||||||
|
paymentAmount: 0,
|
||||||
|
})),
|
||||||
|
oldPaymentReceive.entries,
|
||||||
|
trx
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -16,6 +16,8 @@ import { BranchesModule } from '../Branches/Branches.module';
|
|||||||
import { WarehousesModule } from '../Warehouses/Warehouses.module';
|
import { WarehousesModule } from '../Warehouses/Warehouses.module';
|
||||||
import { VendorCreditsApplicationService } from './VendorCreditsApplication.service';
|
import { VendorCreditsApplicationService } from './VendorCreditsApplication.service';
|
||||||
import { OpenVendorCreditService } from './commands/OpenVendorCredit.service';
|
import { OpenVendorCreditService } from './commands/OpenVendorCredit.service';
|
||||||
|
import { VendorCreditGlEntriesSubscriber } from './subscribers/VendorCreditGLEntriesSubscriber';
|
||||||
|
import { VendorCreditGLEntries } from './commands/VendorCreditGLEntries';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -36,7 +38,9 @@ import { OpenVendorCreditService } from './commands/OpenVendorCredit.service';
|
|||||||
GetRefundVendorCreditService,
|
GetRefundVendorCreditService,
|
||||||
GetVendorCreditService,
|
GetVendorCreditService,
|
||||||
VendorCreditsApplicationService,
|
VendorCreditsApplicationService,
|
||||||
OpenVendorCreditService
|
OpenVendorCreditService,
|
||||||
|
VendorCreditGLEntries,
|
||||||
|
VendorCreditGlEntriesSubscriber,
|
||||||
],
|
],
|
||||||
exports: [
|
exports: [
|
||||||
CreateVendorCreditService,
|
CreateVendorCreditService,
|
||||||
|
|||||||
@@ -0,0 +1,163 @@
|
|||||||
|
import { ItemEntry } from '@/modules/Items/models/ItemEntry';
|
||||||
|
import { VendorCredit } from '../models/VendorCredit';
|
||||||
|
import { ILedgerEntry } from '@/modules/Ledger/types/Ledger.types';
|
||||||
|
import { AccountNormal } from '@/interfaces/Account';
|
||||||
|
import { Ledger } from '@/modules/Ledger/Ledger';
|
||||||
|
|
||||||
|
export class VendorCreditGL {
|
||||||
|
private APAccountId: number;
|
||||||
|
private purchaseDiscountAccountId: number;
|
||||||
|
private otherExpensesAccountId: number;
|
||||||
|
|
||||||
|
constructor(private vendorCredit: VendorCredit) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the payable account (A/P) ID.
|
||||||
|
* @param {number} APAccountId
|
||||||
|
* @returns {VendorCreditGL}
|
||||||
|
*/
|
||||||
|
public setAPAccountId(APAccountId: number) {
|
||||||
|
this.APAccountId = APAccountId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the purchase discount account ID.
|
||||||
|
* @param {number} purchaseDiscountAccountId
|
||||||
|
* @returns {VendorCreditGL}
|
||||||
|
*/
|
||||||
|
public setPurchaseDiscountAccountId(purchaseDiscountAccountId: number) {
|
||||||
|
this.purchaseDiscountAccountId = purchaseDiscountAccountId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the other expenses account ID.
|
||||||
|
* @param {number} otherExpensesAccountId
|
||||||
|
* @returns {VendorCreditGL}
|
||||||
|
*/
|
||||||
|
public setOtherExpensesAccountId(otherExpensesAccountId: number) {
|
||||||
|
this.otherExpensesAccountId = otherExpensesAccountId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the vendor credit GL common entry.
|
||||||
|
* @param {IVendorCredit} vendorCredit
|
||||||
|
*/
|
||||||
|
public get vendorCreditGLCommonEntry() {
|
||||||
|
return {
|
||||||
|
date: this.vendorCredit.vendorCreditDate,
|
||||||
|
currencyCode: this.vendorCredit.currencyCode,
|
||||||
|
exchangeRate: this.vendorCredit.exchangeRate,
|
||||||
|
|
||||||
|
transactionId: this.vendorCredit.id,
|
||||||
|
transactionType: 'VendorCredit',
|
||||||
|
transactionNumber: this.vendorCredit.vendorCreditNumber,
|
||||||
|
referenceNumber: this.vendorCredit.referenceNo,
|
||||||
|
|
||||||
|
credit: 0,
|
||||||
|
debit: 0,
|
||||||
|
|
||||||
|
branchId: this.vendorCredit.branchId,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the vendor credit payable GL entry.
|
||||||
|
* @returns {ILedgerEntry}
|
||||||
|
*/
|
||||||
|
public get vendorCreditPayableGLEntry(): ILedgerEntry {
|
||||||
|
const commonEntity = this.vendorCreditGLCommonEntry;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...commonEntity,
|
||||||
|
debit: this.vendorCredit.totalLocal,
|
||||||
|
accountId: this.APAccountId,
|
||||||
|
contactId: this.vendorCredit.vendorId,
|
||||||
|
accountNormal: AccountNormal.CREDIT,
|
||||||
|
index: 1,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the vendor credit item GL entry.
|
||||||
|
* @returns {ILedgerEntry}
|
||||||
|
*/
|
||||||
|
public getVendorCreditGLItemEntry(
|
||||||
|
entry: ItemEntry,
|
||||||
|
index: number,
|
||||||
|
): ILedgerEntry {
|
||||||
|
const commonEntity = this.vendorCreditGLCommonEntry;
|
||||||
|
const totalLocal = entry.totalExcludingTax * this.vendorCredit.exchangeRate;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...commonEntity,
|
||||||
|
credit: totalLocal,
|
||||||
|
index: index + 2,
|
||||||
|
itemId: entry.itemId,
|
||||||
|
itemQuantity: entry.quantity,
|
||||||
|
accountId:
|
||||||
|
'inventory' === entry.item.type
|
||||||
|
? entry.item.inventoryAccountId
|
||||||
|
: entry.costAccountId || entry.item.costAccountId,
|
||||||
|
accountNormal: AccountNormal.DEBIT,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the vendor credit discount GL entry.
|
||||||
|
* @returns {ILedgerEntry}
|
||||||
|
*/
|
||||||
|
public get discountEntry(): ILedgerEntry {
|
||||||
|
const commonEntry = this.vendorCreditGLCommonEntry;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...commonEntry,
|
||||||
|
debit: this.vendorCredit.discountAmountLocal,
|
||||||
|
accountId: this.purchaseDiscountAccountId,
|
||||||
|
accountNormal: AccountNormal.DEBIT,
|
||||||
|
index: 1,
|
||||||
|
indexGroup: 40,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the vendor credit adjustment GL entry.
|
||||||
|
* @returns {ILedgerEntry}
|
||||||
|
*/
|
||||||
|
public get adjustmentEntry(): ILedgerEntry {
|
||||||
|
const commonEntry = this.vendorCreditGLCommonEntry;
|
||||||
|
const adjustmentAmount = Math.abs(this.vendorCredit.adjustmentLocal);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...commonEntry,
|
||||||
|
credit: this.vendorCredit.adjustmentLocal > 0 ? adjustmentAmount : 0,
|
||||||
|
debit: this.vendorCredit.adjustmentLocal < 0 ? adjustmentAmount : 0,
|
||||||
|
accountId: this.otherExpensesAccountId,
|
||||||
|
accountNormal: AccountNormal.DEBIT,
|
||||||
|
index: 1,
|
||||||
|
indexGroup: 40,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the vendor credit GL entries.
|
||||||
|
* @return {ILedgerEntry[]}
|
||||||
|
*/
|
||||||
|
public getVendorCreditGLEntries(): ILedgerEntry[] {
|
||||||
|
const payableEntry = this.vendorCreditPayableGLEntry;
|
||||||
|
const getItemEntry = this.getVendorCreditGLItemEntry;
|
||||||
|
const itemsEntries = this.vendorCredit.entries.map(getItemEntry);
|
||||||
|
|
||||||
|
const discountEntry = this.discountEntry;
|
||||||
|
const adjustmentEntry = this.adjustmentEntry;
|
||||||
|
|
||||||
|
return [payableEntry, discountEntry, adjustmentEntry, ...itemsEntries];
|
||||||
|
}
|
||||||
|
|
||||||
|
public getVendorCreditLedger(): Ledger {
|
||||||
|
const entries = this.getVendorCreditGLEntries();
|
||||||
|
return new Ledger(entries);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,252 +1,88 @@
|
|||||||
// import { Inject, Service } from 'typedi';
|
import { Knex } from 'knex';
|
||||||
// import { Knex } from 'knex';
|
import { VendorCreditGL } from './VendorCreditGL';
|
||||||
// import * as R from 'ramda';
|
import { LedgerStorageService } from '@/modules/Ledger/LedgerStorage.service';
|
||||||
// import {
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
// IVendorCredit,
|
import { AccountRepository } from '@/modules/Accounts/repositories/Account.repository';
|
||||||
// ILedgerEntry,
|
import { VendorCredit } from '../models/VendorCredit';
|
||||||
// AccountNormal,
|
|
||||||
// IItemEntry,
|
|
||||||
// } from '@/interfaces';
|
|
||||||
// import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
// import LedgerStorageService from '@/services/Accounting/LedgerStorageService';
|
|
||||||
// import Ledger from '@/services/Accounting/Ledger';
|
|
||||||
|
|
||||||
// @Service()
|
@Injectable()
|
||||||
// export default class VendorCreditGLEntries {
|
export class VendorCreditGLEntries {
|
||||||
// @Inject()
|
constructor(
|
||||||
// private ledgerStorage: LedgerStorageService;
|
private readonly ledgerStorage: LedgerStorageService,
|
||||||
|
private readonly accountRepository: AccountRepository,
|
||||||
|
|
||||||
// @Inject()
|
@Inject(VendorCredit.name)
|
||||||
// private tenancy: HasTenancyService;
|
private readonly vendorCreditModel: typeof VendorCredit,
|
||||||
|
) {}
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Retrieve the vendor credit GL common entry.
|
* Creates vendor credit associated GL entries.
|
||||||
// * @param {IVendorCredit} vendorCredit
|
* @param {number} vendorCreditId
|
||||||
// * @returns {}
|
* @param {Knex.Transaction} trx
|
||||||
// */
|
*/
|
||||||
// public getVendorCreditGLCommonEntry = (vendorCredit: IVendorCredit) => {
|
public writeVendorCreditGLEntries = async (
|
||||||
// return {
|
vendorCreditId: number,
|
||||||
// date: vendorCredit.vendorCreditDate,
|
trx?: Knex.Transaction,
|
||||||
// currencyCode: vendorCredit.currencyCode,
|
) => {
|
||||||
// exchangeRate: vendorCredit.exchangeRate,
|
// Vendor credit with entries items.
|
||||||
|
const vendorCredit = await this.vendorCreditModel
|
||||||
|
.query(trx)
|
||||||
|
.findById(vendorCreditId)
|
||||||
|
.withGraphFetched('entries.item');
|
||||||
|
|
||||||
// transactionId: vendorCredit.id,
|
// Retrieve the payable account (A/P) account.
|
||||||
// transactionType: 'VendorCredit',
|
const APAccount = await this.accountRepository.findOrCreateAccountsPayable(
|
||||||
// transactionNumber: vendorCredit.vendorCreditNumber,
|
vendorCredit.currencyCode,
|
||||||
// referenceNumber: vendorCredit.referenceNo,
|
{},
|
||||||
|
trx,
|
||||||
|
);
|
||||||
|
// Retrieve the purchase discount account.
|
||||||
|
const purchaseDiscountAccount =
|
||||||
|
await this.accountRepository.findOrCreatePurchaseDiscountAccount({}, trx);
|
||||||
|
|
||||||
// credit: 0,
|
// Retrieve the other expenses account.
|
||||||
// debit: 0,
|
const otherExpensesAccount =
|
||||||
|
await this.accountRepository.findOrCreateOtherExpensesAccount({}, trx);
|
||||||
|
|
||||||
// branchId: vendorCredit.branchId,
|
const vendorCreditLedger = new VendorCreditGL(vendorCredit)
|
||||||
// };
|
.setAPAccountId(APAccount.id)
|
||||||
// };
|
.setPurchaseDiscountAccountId(purchaseDiscountAccount.id)
|
||||||
|
.setOtherExpensesAccountId(otherExpensesAccount.id)
|
||||||
|
.getVendorCreditLedger();
|
||||||
|
|
||||||
// /**
|
// Commits the ledger entries to the storage.
|
||||||
// * Retrieves the vendor credit payable GL entry.
|
await this.ledgerStorage.commit(vendorCreditLedger, trx);
|
||||||
// * @param {IVendorCredit} vendorCredit
|
};
|
||||||
// * @param {number} APAccountId
|
|
||||||
// * @returns {ILedgerEntry}
|
|
||||||
// */
|
|
||||||
// public getVendorCreditPayableGLEntry = (
|
|
||||||
// vendorCredit: IVendorCredit,
|
|
||||||
// APAccountId: number
|
|
||||||
// ): ILedgerEntry => {
|
|
||||||
// const commonEntity = this.getVendorCreditGLCommonEntry(vendorCredit);
|
|
||||||
|
|
||||||
// return {
|
/**
|
||||||
// ...commonEntity,
|
* Edits vendor credit associated GL entries.
|
||||||
// debit: vendorCredit.totalLocal,
|
* @param {number} vendorCreditId
|
||||||
// accountId: APAccountId,
|
* @param {Knex.Transaction} trx
|
||||||
// contactId: vendorCredit.vendorId,
|
*/
|
||||||
// accountNormal: AccountNormal.CREDIT,
|
public rewriteVendorCreditGLEntries = async (
|
||||||
// index: 1,
|
vendorCreditId: number,
|
||||||
// };
|
trx?: Knex.Transaction,
|
||||||
// };
|
) => {
|
||||||
|
// Reverts the GL entries.
|
||||||
|
await this.revertVendorCreditGLEntries(vendorCreditId, trx);
|
||||||
|
|
||||||
// /**
|
// Re-write the GL entries.
|
||||||
// * Retrieves the vendor credit item GL entry.
|
await this.writeVendorCreditGLEntries(vendorCreditId, trx);
|
||||||
// * @param {IVendorCredit} vendorCredit
|
};
|
||||||
// * @param {IItemEntry} entry
|
|
||||||
// * @returns {ILedgerEntry}
|
|
||||||
// */
|
|
||||||
// public getVendorCreditGLItemEntry = R.curry(
|
|
||||||
// (
|
|
||||||
// vendorCredit: IVendorCredit,
|
|
||||||
// entry: IItemEntry,
|
|
||||||
// index: number
|
|
||||||
// ): ILedgerEntry => {
|
|
||||||
// const commonEntity = this.getVendorCreditGLCommonEntry(vendorCredit);
|
|
||||||
// const totalLocal = entry.totalExcludingTax * vendorCredit.exchangeRate;
|
|
||||||
|
|
||||||
// return {
|
/**
|
||||||
// ...commonEntity,
|
* Reverts the vendor credit associated GL entries.
|
||||||
// credit: totalLocal,
|
* @param {number} vendorCreditId - Vendor credit identifier.
|
||||||
// index: index + 2,
|
* @param {Knex.Transaction} trx
|
||||||
// itemId: entry.itemId,
|
*/
|
||||||
// itemQuantity: entry.quantity,
|
public async revertVendorCreditGLEntries(
|
||||||
// accountId:
|
vendorCreditId: number,
|
||||||
// 'inventory' === entry.item.type
|
trx?: Knex.Transaction,
|
||||||
// ? entry.item.inventoryAccountId
|
): Promise<void> {
|
||||||
// : entry.costAccountId || entry.item.costAccountId,
|
await this.ledgerStorage.deleteByReference(
|
||||||
// accountNormal: AccountNormal.DEBIT,
|
vendorCreditId,
|
||||||
// };
|
'VendorCredit',
|
||||||
// }
|
trx,
|
||||||
// );
|
);
|
||||||
|
};
|
||||||
// /**
|
}
|
||||||
// * Retrieves the vendor credit discount GL entry.
|
|
||||||
// * @param {IVendorCredit} vendorCredit
|
|
||||||
// * @param {number} discountAccountId
|
|
||||||
// * @returns {ILedgerEntry}
|
|
||||||
// */
|
|
||||||
// public getDiscountEntry = (
|
|
||||||
// vendorCredit: IVendorCredit,
|
|
||||||
// purchaseDiscountAccountId: number
|
|
||||||
// ) => {
|
|
||||||
// const commonEntry = this.getVendorCreditGLCommonEntry(vendorCredit);
|
|
||||||
|
|
||||||
// return {
|
|
||||||
// ...commonEntry,
|
|
||||||
// debit: vendorCredit.discountAmountLocal,
|
|
||||||
// accountId: purchaseDiscountAccountId,
|
|
||||||
// accountNormal: AccountNormal.DEBIT,
|
|
||||||
// index: 1,
|
|
||||||
// indexGroup: 40,
|
|
||||||
// };
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Retrieves the vendor credit adjustment GL entry.
|
|
||||||
// * @param {IVendorCredit} vendorCredit
|
|
||||||
// * @param {number} adjustmentAccountId
|
|
||||||
// * @returns {ILedgerEntry}
|
|
||||||
// */
|
|
||||||
// public getAdjustmentEntry = (
|
|
||||||
// vendorCredit: IVendorCredit,
|
|
||||||
// otherExpensesAccountId: number
|
|
||||||
// ) => {
|
|
||||||
// const commonEntry = this.getVendorCreditGLCommonEntry(vendorCredit);
|
|
||||||
// const adjustmentAmount = Math.abs(vendorCredit.adjustmentLocal);
|
|
||||||
|
|
||||||
// return {
|
|
||||||
// ...commonEntry,
|
|
||||||
// credit: vendorCredit.adjustmentLocal > 0 ? adjustmentAmount : 0,
|
|
||||||
// debit: vendorCredit.adjustmentLocal < 0 ? adjustmentAmount : 0,
|
|
||||||
// accountId: otherExpensesAccountId,
|
|
||||||
// accountNormal: AccountNormal.DEBIT,
|
|
||||||
// index: 1,
|
|
||||||
// indexGroup: 40,
|
|
||||||
// };
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Retrieve the vendor credit GL entries.
|
|
||||||
// * @param {IVendorCredit} vendorCredit -
|
|
||||||
// * @param {number} receivableAccount -
|
|
||||||
// * @return {ILedgerEntry[]}
|
|
||||||
// */
|
|
||||||
// public getVendorCreditGLEntries = (
|
|
||||||
// vendorCredit: IVendorCredit,
|
|
||||||
// payableAccountId: number,
|
|
||||||
// purchaseDiscountAccountId: number,
|
|
||||||
// otherExpensesAccountId: number
|
|
||||||
// ): ILedgerEntry[] => {
|
|
||||||
// const payableEntry = this.getVendorCreditPayableGLEntry(
|
|
||||||
// vendorCredit,
|
|
||||||
// payableAccountId
|
|
||||||
// );
|
|
||||||
// const getItemEntry = this.getVendorCreditGLItemEntry(vendorCredit);
|
|
||||||
// const itemsEntries = vendorCredit.entries.map(getItemEntry);
|
|
||||||
|
|
||||||
// const discountEntry = this.getDiscountEntry(
|
|
||||||
// vendorCredit,
|
|
||||||
// purchaseDiscountAccountId
|
|
||||||
// );
|
|
||||||
// const adjustmentEntry = this.getAdjustmentEntry(
|
|
||||||
// vendorCredit,
|
|
||||||
// otherExpensesAccountId
|
|
||||||
// );
|
|
||||||
// return [payableEntry, discountEntry, adjustmentEntry, ...itemsEntries];
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Reverts the vendor credit associated GL entries.
|
|
||||||
// * @param {number} tenantId
|
|
||||||
// * @param {number} vendorCreditId
|
|
||||||
// * @param {Knex.Transaction} trx
|
|
||||||
// */
|
|
||||||
// public revertVendorCreditGLEntries = async (
|
|
||||||
// tenantId: number,
|
|
||||||
// vendorCreditId: number,
|
|
||||||
// trx?: Knex.Transaction
|
|
||||||
// ): Promise<void> => {
|
|
||||||
// await this.ledgerStorage.deleteByReference(
|
|
||||||
// tenantId,
|
|
||||||
// vendorCreditId,
|
|
||||||
// 'VendorCredit',
|
|
||||||
// trx
|
|
||||||
// );
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Creates vendor credit associated GL entries.
|
|
||||||
// * @param {number} tenantId
|
|
||||||
// * @param {number} vendorCreditId
|
|
||||||
// * @param {Knex.Transaction} trx
|
|
||||||
// */
|
|
||||||
// public writeVendorCreditGLEntries = async (
|
|
||||||
// tenantId: number,
|
|
||||||
// vendorCreditId: number,
|
|
||||||
// trx?: Knex.Transaction
|
|
||||||
// ) => {
|
|
||||||
// const { accountRepository } = this.tenancy.repositories(tenantId);
|
|
||||||
// const { VendorCredit } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
// // Vendor credit with entries items.
|
|
||||||
// const vendorCredit = await VendorCredit.query(trx)
|
|
||||||
// .findById(vendorCreditId)
|
|
||||||
// .withGraphFetched('entries.item');
|
|
||||||
|
|
||||||
// // Retrieve the payable account (A/P) account.
|
|
||||||
// const APAccount = await accountRepository.findOrCreateAccountsPayable(
|
|
||||||
// vendorCredit.currencyCode,
|
|
||||||
// {},
|
|
||||||
// trx
|
|
||||||
// );
|
|
||||||
// const purchaseDiscountAccount =
|
|
||||||
// await accountRepository.findOrCreatePurchaseDiscountAccount({}, trx);
|
|
||||||
|
|
||||||
// const otherExpensesAccount =
|
|
||||||
// await accountRepository.findOrCreateOtherExpensesAccount({}, trx);
|
|
||||||
// // Saves the vendor credit GL entries.
|
|
||||||
// const ledgerEntries = this.getVendorCreditGLEntries(
|
|
||||||
// vendorCredit,
|
|
||||||
// APAccount.id,
|
|
||||||
// purchaseDiscountAccount.id,
|
|
||||||
// otherExpensesAccount.id
|
|
||||||
// );
|
|
||||||
// const ledger = new Ledger(ledgerEntries);
|
|
||||||
|
|
||||||
// // Commits the ledger entries to the storage.
|
|
||||||
// await this.ledgerStorage.commit(tenantId, ledger, trx);
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Edits vendor credit associated GL entries.
|
|
||||||
// * @param {number} tenantId
|
|
||||||
// * @param {number} vendorCreditId
|
|
||||||
// * @param {Knex.Transaction} trx
|
|
||||||
// */
|
|
||||||
// public rewriteVendorCreditGLEntries = async (
|
|
||||||
// tenantId: number,
|
|
||||||
// vendorCreditId: number,
|
|
||||||
// trx?: Knex.Transaction
|
|
||||||
// ) => {
|
|
||||||
// // Reverts the GL entries.
|
|
||||||
// await this.revertVendorCreditGLEntries(tenantId, vendorCreditId, trx);
|
|
||||||
|
|
||||||
// // Re-write the GL entries.
|
|
||||||
// await this.writeVendorCreditGLEntries(tenantId, vendorCreditId, trx);
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -1,110 +1,83 @@
|
|||||||
// import { Service, Inject } from 'typedi';
|
import { Injectable } from '@nestjs/common';
|
||||||
// import events from '@/subscribers/events';
|
import {
|
||||||
// import {
|
IVendorCreditCreatedPayload,
|
||||||
// IVendorCreditCreatedPayload,
|
IVendorCreditDeletedPayload,
|
||||||
// IVendorCreditDeletedPayload,
|
IVendorCreditEditedPayload,
|
||||||
// IVendorCreditEditedPayload,
|
IVendorCreditOpenedPayload,
|
||||||
// IVendorCreditOpenedPayload,
|
} from '../types/VendorCredit.types';
|
||||||
// } from '@/interfaces';
|
import { OnEvent } from '@nestjs/event-emitter';
|
||||||
// import VendorCreditGLEntries from '../commands/VendorCreditGLEntries';
|
import { VendorCreditGLEntries } from '../commands/VendorCreditGLEntries';
|
||||||
|
import { events } from '@/common/events/events';
|
||||||
|
|
||||||
// @Service()
|
@Injectable()
|
||||||
// export default class VendorCreditGlEntriesSubscriber {
|
export class VendorCreditGlEntriesSubscriber {
|
||||||
// @Inject()
|
constructor(public readonly vendorCreditGLEntries: VendorCreditGLEntries) {}
|
||||||
// private vendorCreditGLEntries: VendorCreditGLEntries;
|
|
||||||
|
|
||||||
// /***
|
/**
|
||||||
// * Attaches events with handlers.
|
* Writes GL entries of vendor credit once the transaction created.
|
||||||
// */
|
* @param {IVendorCreditCreatedPayload} payload -
|
||||||
// public attach(bus) {
|
*/
|
||||||
// bus.subscribe(
|
@OnEvent(events.vendorCredit.onCreated)
|
||||||
// events.vendorCredit.onCreated,
|
public async writeGLEntriesOnceVendorCreditCreated({
|
||||||
// this.writeGLEntriesOnceVendorCreditCreated
|
vendorCredit,
|
||||||
// );
|
trx,
|
||||||
// bus.subscribe(
|
}: IVendorCreditCreatedPayload): Promise<void> {
|
||||||
// events.vendorCredit.onOpened,
|
// Can't continue if the vendor credit is not open yet.
|
||||||
// this.writeGLEntgriesOnceVendorCreditOpened
|
if (!vendorCredit.isPublished) return;
|
||||||
// );
|
|
||||||
// bus.subscribe(
|
|
||||||
// events.vendorCredit.onEdited,
|
|
||||||
// this.editGLEntriesOnceVendorCreditEdited
|
|
||||||
// );
|
|
||||||
// bus.subscribe(
|
|
||||||
// events.vendorCredit.onDeleted,
|
|
||||||
// this.revertGLEntriesOnceDeleted
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
// /**
|
await this.vendorCreditGLEntries.writeVendorCreditGLEntries(
|
||||||
// * Writes GL entries of vendor credit once the transaction created.
|
vendorCredit.id,
|
||||||
// * @param {IVendorCreditCreatedPayload} payload -
|
trx,
|
||||||
// */
|
);
|
||||||
// private writeGLEntriesOnceVendorCreditCreated = async ({
|
}
|
||||||
// tenantId,
|
|
||||||
// vendorCredit,
|
|
||||||
// trx,
|
|
||||||
// }: IVendorCreditCreatedPayload): Promise<void> => {
|
|
||||||
// // Can't continue if the vendor credit is not open yet.
|
|
||||||
// if (!vendorCredit.isPublished) return;
|
|
||||||
|
|
||||||
// await this.vendorCreditGLEntries.writeVendorCreditGLEntries(
|
/**
|
||||||
// tenantId,
|
* Writes Gl entries of vendor credit once the transaction opened.
|
||||||
// vendorCredit.id,
|
* @param {IVendorCreditOpenedPayload} payload -
|
||||||
// trx
|
*/
|
||||||
// );
|
@OnEvent(events.vendorCredit.onOpened)
|
||||||
// };
|
public async writeGLEntgriesOnceVendorCreditOpened({
|
||||||
|
vendorCreditId,
|
||||||
|
trx,
|
||||||
|
}: IVendorCreditOpenedPayload): Promise<void> {
|
||||||
|
await this.vendorCreditGLEntries.writeVendorCreditGLEntries(
|
||||||
|
vendorCreditId,
|
||||||
|
trx,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Writes Gl entries of vendor credit once the transaction opened.
|
* Edits associated GL entries once vendor credit edited.
|
||||||
// * @param {IVendorCreditOpenedPayload} payload -
|
* @param {IVendorCreditEditedPayload} payload
|
||||||
// */
|
*/
|
||||||
// private writeGLEntgriesOnceVendorCreditOpened = async ({
|
@OnEvent(events.vendorCredit.onEdited)
|
||||||
// tenantId,
|
public async editGLEntriesOnceVendorCreditEdited({
|
||||||
// vendorCreditId,
|
vendorCredit,
|
||||||
// trx,
|
trx,
|
||||||
// }: IVendorCreditOpenedPayload) => {
|
}: IVendorCreditEditedPayload): Promise<void> {
|
||||||
// await this.vendorCreditGLEntries.writeVendorCreditGLEntries(
|
// Can't continue if the vendor credit is not open yet.
|
||||||
// tenantId,
|
if (!vendorCredit.isPublished) return;
|
||||||
// vendorCreditId,
|
|
||||||
// trx
|
|
||||||
// );
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /**
|
await this.vendorCreditGLEntries.rewriteVendorCreditGLEntries(
|
||||||
// * Edits associated GL entries once vendor credit edited.
|
vendorCredit.id,
|
||||||
// * @param {IVendorCreditEditedPayload} payload
|
trx,
|
||||||
// */
|
);
|
||||||
// private editGLEntriesOnceVendorCreditEdited = async ({
|
}
|
||||||
// tenantId,
|
|
||||||
// vendorCreditId,
|
|
||||||
// vendorCredit,
|
|
||||||
// trx,
|
|
||||||
// }: IVendorCreditEditedPayload) => {
|
|
||||||
// // Can't continue if the vendor credit is not open yet.
|
|
||||||
// if (!vendorCredit.isPublished) return;
|
|
||||||
|
|
||||||
// await this.vendorCreditGLEntries.rewriteVendorCreditGLEntries(
|
/**
|
||||||
// tenantId,
|
* Reverts the GL entries once vendor credit deleted.
|
||||||
// vendorCreditId,
|
* @param {IVendorCreditDeletedPayload} payload -
|
||||||
// trx
|
*/
|
||||||
// );
|
@OnEvent(events.vendorCredit.onDeleted)
|
||||||
// };
|
public async revertGLEntriesOnceDeleted({
|
||||||
|
vendorCreditId,
|
||||||
|
oldVendorCredit,
|
||||||
|
}: IVendorCreditDeletedPayload): Promise<void> {
|
||||||
|
// Can't continue of the vendor credit is not open yet.
|
||||||
|
if (!oldVendorCredit.isPublished) return;
|
||||||
|
|
||||||
// /**
|
await this.vendorCreditGLEntries.revertVendorCreditGLEntries(
|
||||||
// * Reverts the GL entries once vendor credit deleted.
|
vendorCreditId,
|
||||||
// * @param {IVendorCreditDeletedPayload} payload -
|
);
|
||||||
// */
|
}
|
||||||
// private revertGLEntriesOnceDeleted = async ({
|
}
|
||||||
// vendorCreditId,
|
|
||||||
// tenantId,
|
|
||||||
// oldVendorCredit,
|
|
||||||
// }: IVendorCreditDeletedPayload): Promise<void> => {
|
|
||||||
// // Can't continue of the vendor credit is not open yet.
|
|
||||||
// if (!oldVendorCredit.isPublished) return;
|
|
||||||
|
|
||||||
// await this.vendorCreditGLEntries.revertVendorCreditGLEntries(
|
|
||||||
// tenantId,
|
|
||||||
// vendorCreditId
|
|
||||||
// );
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|||||||
Reference in New Issue
Block a user