feat: apply credit note to invoice module

This commit is contained in:
Ahmed Bouhuolia
2025-05-04 01:32:08 +02:00
parent 1d53063e09
commit 4f6ad2b293
28 changed files with 633 additions and 707 deletions

View File

@@ -1,160 +1,162 @@
// import { Inject, Service } from 'typedi';
// import { Knex } from 'knex';
// import { AccountNormal, ILedgerEntry, IRefundCreditNote } from '@/interfaces';
// import HasTenancyService from '@/services/Tenancy/TenancyService';
// import LedgerStorageService from '@/services/Accounting/LedgerStorageService';
// import Ledger from '@/services/Accounting/Ledger';
import { LedgerStorageService } from '@/modules/Ledger/LedgerStorage.service';
import { Inject, Injectable } from '@nestjs/common';
import { RefundCreditNote } from '../models/RefundCreditNote';
import { Ledger } from '@/modules/Ledger/Ledger';
import { Knex } from 'knex';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
import { Account } from '@/modules/Accounts/models/Account.model';
import { ILedgerEntry } from '@/modules/Ledger/types/Ledger.types';
import { AccountNormal } from '@/interfaces/Account';
// @Service()
// export default class RefundCreditNoteGLEntries {
// @Inject()
// ledgerStorage: LedgerStorageService;
@Injectable()
export class RefundCreditNoteGLEntries {
constructor(
private readonly ledgerStorage: LedgerStorageService,
// @Inject()
// tenancy: HasTenancyService;
@Inject(Account.name)
private readonly accountModel: TenantModelProxy<typeof Account>,
// /**
// * Retrieves the refund credit common GL entry.
// * @param {IRefundCreditNote} refundCreditNote
// * @returns
// */
// private getRefundCreditCommonGLEntry = (
// refundCreditNote: IRefundCreditNote
// ) => {
// return {
// currencyCode: refundCreditNote.currencyCode,
// exchangeRate: refundCreditNote.exchangeRate,
@Inject(RefundCreditNote.name)
private readonly refundCreditNoteModel: TenantModelProxy<
typeof RefundCreditNote
>,
) {}
// transactionType: 'RefundCreditNote',
// transactionId: refundCreditNote.id,
// date: refundCreditNote.date,
// userId: refundCreditNote.userId,
/**
* Retrieves the refund credit common GL entry.
* @param {IRefundCreditNote} refundCreditNote
* @returns
*/
private getRefundCreditCommonGLEntry = (
refundCreditNote: RefundCreditNote,
) => {
return {
currencyCode: refundCreditNote.currencyCode,
exchangeRate: refundCreditNote.exchangeRate,
// referenceNumber: refundCreditNote.referenceNo,
transactionType: 'RefundCreditNote',
transactionId: refundCreditNote.id,
date: refundCreditNote.date,
userId: refundCreditNote.userId,
// createdAt: refundCreditNote.createdAt,
// indexGroup: 10,
referenceNumber: refundCreditNote.referenceNo,
// credit: 0,
// debit: 0,
createdAt: refundCreditNote.createdAt,
indexGroup: 10,
// note: refundCreditNote.description,
// branchId: refundCreditNote.branchId,
// };
// };
credit: 0,
debit: 0,
// /**
// * Retrieves the refudn credit receivable GL entry.
// * @param {IRefundCreditNote} refundCreditNote
// * @param {number} ARAccountId
// * @returns {ILedgerEntry}
// */
// private getRefundCreditGLReceivableEntry = (
// refundCreditNote: IRefundCreditNote,
// ARAccountId: number
// ): ILedgerEntry => {
// const commonEntry = this.getRefundCreditCommonGLEntry(refundCreditNote);
note: refundCreditNote.description,
branchId: refundCreditNote.branchId,
};
};
// return {
// ...commonEntry,
// debit: refundCreditNote.amount,
// accountId: ARAccountId,
// contactId: refundCreditNote.creditNote.customerId,
// index: 1,
// accountNormal: AccountNormal.DEBIT,
// };
// };
/**
* Retrieves the refudn credit receivable GL entry.
* @param {IRefundCreditNote} refundCreditNote
* @param {number} ARAccountId
* @returns {ILedgerEntry}
*/
private getRefundCreditGLReceivableEntry = (
refundCreditNote: RefundCreditNote,
ARAccountId: number,
): ILedgerEntry => {
const commonEntry = this.getRefundCreditCommonGLEntry(refundCreditNote);
// /**
// * Retrieves the refund credit withdrawal GL entry.
// * @param {number} refundCreditNote
// * @returns {ILedgerEntry}
// */
// private getRefundCreditGLWithdrawalEntry = (
// refundCreditNote: IRefundCreditNote
// ): ILedgerEntry => {
// const commonEntry = this.getRefundCreditCommonGLEntry(refundCreditNote);
return {
...commonEntry,
debit: refundCreditNote.amount,
accountId: ARAccountId,
contactId: refundCreditNote.creditNote.customerId,
index: 1,
accountNormal: AccountNormal.DEBIT,
};
};
// return {
// ...commonEntry,
// credit: refundCreditNote.amount,
// accountId: refundCreditNote.fromAccountId,
// index: 2,
// accountNormal: AccountNormal.DEBIT,
// };
// };
/**
* Retrieves the refund credit withdrawal GL entry.
* @param {number} refundCreditNote
* @returns {ILedgerEntry}
*/
private getRefundCreditGLWithdrawalEntry = (
refundCreditNote: RefundCreditNote,
): ILedgerEntry => {
const commonEntry = this.getRefundCreditCommonGLEntry(refundCreditNote);
// /**
// * Retrieve the refund credit note GL entries.
// * @param {IRefundCreditNote} refundCreditNote
// * @param {number} receivableAccount
// * @returns {ILedgerEntry[]}
// */
// public getRefundCreditGLEntries(
// refundCreditNote: IRefundCreditNote,
// ARAccountId: number
// ): ILedgerEntry[] {
// const receivableEntry = this.getRefundCreditGLReceivableEntry(
// refundCreditNote,
// ARAccountId
// );
// const withdrawalEntry =
// this.getRefundCreditGLWithdrawalEntry(refundCreditNote);
return {
...commonEntry,
credit: refundCreditNote.amount,
accountId: refundCreditNote.fromAccountId,
index: 2,
accountNormal: AccountNormal.DEBIT,
};
};
// return [receivableEntry, withdrawalEntry];
// }
/**
* Retrieve the refund credit note GL entries.
* @param {IRefundCreditNote} refundCreditNote
* @param {number} receivableAccount
* @returns {ILedgerEntry[]}
*/
public getRefundCreditGLEntries(
refundCreditNote: RefundCreditNote,
ARAccountId: number,
): ILedgerEntry[] {
const receivableEntry = this.getRefundCreditGLReceivableEntry(
refundCreditNote,
ARAccountId,
);
const withdrawalEntry =
this.getRefundCreditGLWithdrawalEntry(refundCreditNote);
// /**
// * Creates refund credit GL entries.
// * @param {number} tenantId
// * @param {IRefundCreditNote} refundCreditNote
// * @param {Knex.Transaction} trx
// */
// public createRefundCreditGLEntries = async (
// tenantId: number,
// refundCreditNoteId: number,
// trx?: Knex.Transaction
// ) => {
// const { Account, RefundCreditNote } = this.tenancy.models(tenantId);
return [receivableEntry, withdrawalEntry];
}
// // Retrieve the refund with associated credit note.
// const refundCreditNote = await RefundCreditNote.query(trx)
// .findById(refundCreditNoteId)
// .withGraphFetched('creditNote');
/**
* Creates refund credit GL entries.
* @param {IRefundCreditNote} refundCreditNote
* @param {Knex.Transaction} trx
*/
public createRefundCreditGLEntries = async (
refundCreditNoteId: number,
trx?: Knex.Transaction,
) => {
// Retrieve the refund with associated credit note.
const refundCreditNote = await this.refundCreditNoteModel().query(trx)
.findById(refundCreditNoteId)
.withGraphFetched('creditNote');
// // Receivable account A/R.
// const receivableAccount = await Account.query().findOne(
// 'slug',
// 'accounts-receivable'
// );
// // Retrieve refund credit GL entries.
// const refundGLEntries = this.getRefundCreditGLEntries(
// refundCreditNote,
// receivableAccount.id
// );
// const ledger = new Ledger(refundGLEntries);
// Receivable account A/R.
const receivableAccount = await this.accountModel().query().findOne(
'slug',
'accounts-receivable',
);
// Retrieve refund credit GL entries.
const refundGLEntries = this.getRefundCreditGLEntries(
refundCreditNote,
receivableAccount.id,
);
const ledger = new Ledger(refundGLEntries);
// // Saves refund ledger entries.
// await this.ledgerStorage.commit(tenantId, ledger, trx);
// };
// Saves refund ledger entries.
await this.ledgerStorage.commit(ledger, trx);
};
// /**
// * Reverts refund credit note GL entries.
// * @param {number} tenantId
// * @param {number} refundCreditNoteId
// * @param {number} receivableAccount
// * @param {Knex.Transaction} trx
// */
// public revertRefundCreditGLEntries = async (
// tenantId: number,
// refundCreditNoteId: number,
// trx?: Knex.Transaction
// ) => {
// await this.ledgerStorage.deleteByReference(
// tenantId,
// refundCreditNoteId,
// 'RefundCreditNote',
// trx
// );
// };
// }
/**
* Reverts refund credit note GL entries.
* @param {number} refundCreditNoteId
* @param {number} receivableAccount
* @param {Knex.Transaction} trx
*/
public revertRefundCreditGLEntries = async (
refundCreditNoteId: number,
trx?: Knex.Transaction,
) => {
await this.ledgerStorage.deleteByReference(
refundCreditNoteId,
'RefundCreditNote',
trx,
);
};
}