fix: discount transactions GL entries

This commit is contained in:
Ahmed Bouhuolia
2024-12-08 14:20:11 +02:00
parent 14ae978bde
commit 46719ef361
17 changed files with 375 additions and 58 deletions

View File

@@ -52,10 +52,22 @@ export class BillGLEntries {
{},
trx
);
// Find or create other expenses account.
const otherExpensesAccount = await accountRepository.findOrCreateOtherExpensesAccount(
{},
trx
);
// Find or create purchase discount account.
const purchaseDiscountAccount = await accountRepository.findOrCreatePurchaseDiscountAccount(
{},
trx
);
const billLedger = this.getBillLedger(
bill,
APAccount.id,
taxPayableAccount.id
taxPayableAccount.id,
purchaseDiscountAccount.id,
otherExpensesAccount.id
);
// Commit the GL enties on the storage.
await this.ledgerStorage.commit(tenantId, billLedger, trx);
@@ -240,6 +252,51 @@ export class BillGLEntries {
return nonZeroTaxEntries.map(transformTaxEntry);
};
/**
* Retrieves the purchase discount GL entry.
* @param {IBill} bill
* @param {number} purchaseDiscountAccountId
* @returns {ILedgerEntry}
*/
private getPurchaseDiscountEntry = (
bill: IBill,
purchaseDiscountAccountId: number
) => {
const commonEntry = this.getBillCommonEntry(bill);
return {
...commonEntry,
credit: bill.discountAmount,
accountId: purchaseDiscountAccountId,
accountNormal: AccountNormal.DEBIT,
index: 1,
indexGroup: 40,
};
};
/**
* Retrieves the purchase other charges GL entry.
* @param {IBill} bill
* @param {number} otherChargesAccountId
* @returns {ILedgerEntry}
*/
private getAdjustmentEntry = (
bill: IBill,
otherExpensesAccountId: number
) => {
const commonEntry = this.getBillCommonEntry(bill);
return {
...commonEntry,
debit: bill.adjustment < 0 ? bill.adjustment : 0,
credit: bill.adjustment > 0 ? bill.adjustment : 0,
accountId: otherExpensesAccountId,
accountNormal: AccountNormal.DEBIT,
index: 1,
indexGroup: 40,
};
};
/**
* Retrieves the given bill GL entries.
* @param {IBill} bill
@@ -249,7 +306,9 @@ export class BillGLEntries {
private getBillGLEntries = (
bill: IBill,
payableAccountId: number,
taxPayableAccountId: number
taxPayableAccountId: number,
purchaseDiscountAccountId: number,
otherExpensesAccountId: number
): ILedgerEntry[] => {
const payableEntry = this.getBillPayableEntry(payableAccountId, bill);
@@ -262,8 +321,21 @@ export class BillGLEntries {
);
const taxEntries = this.getBillTaxEntries(bill, taxPayableAccountId);
const purchaseDiscountEntry = this.getPurchaseDiscountEntry(
bill,
purchaseDiscountAccountId
);
const adjustmentEntry = this.getAdjustmentEntry(bill, otherExpensesAccountId);
// Allocate cost entries journal entries.
return [payableEntry, ...itemsEntries, ...landedCostEntries, ...taxEntries];
return [
payableEntry,
...itemsEntries,
...landedCostEntries,
...taxEntries,
purchaseDiscountEntry,
adjustmentEntry,
];
};
/**
@@ -275,14 +347,17 @@ export class BillGLEntries {
private getBillLedger = (
bill: IBill,
payableAccountId: number,
taxPayableAccountId: number
taxPayableAccountId: number,
purchaseDiscountAccountId: number,
otherExpensesAccountId: number
) => {
const entries = this.getBillGLEntries(
bill,
payableAccountId,
taxPayableAccountId
taxPayableAccountId,
purchaseDiscountAccountId,
otherExpensesAccountId
);
return new Ledger(entries);
};
}

View File

@@ -44,18 +44,31 @@ export class SaleInvoiceGLEntries {
// Find or create the A/R account.
const ARAccount = await accountRepository.findOrCreateAccountReceivable(
saleInvoice.currencyCode, {}, trx
saleInvoice.currencyCode,
{},
trx
);
// Find or create tax payable account.
const taxPayableAccount = await accountRepository.findOrCreateTaxPayable(
{},
trx
);
// Find or create the discount expense account.
const discountAccount = await accountRepository.findOrCreateDiscountAccount(
{},
trx
);
// Find or create the other charges account.
const otherChargesAccount =
await accountRepository.findOrCreateOtherChargesAccount({}, trx);
// Retrieves the ledger of the invoice.
const ledger = this.getInvoiceGLedger(
saleInvoice,
ARAccount.id,
taxPayableAccount.id
taxPayableAccount.id,
discountAccount.id,
otherChargesAccount.id
);
// Commits the ledger entries to the storage as UOW.
await this.ledegrRepository.commit(tenantId, ledger, trx);
@@ -107,12 +120,16 @@ export class SaleInvoiceGLEntries {
public getInvoiceGLedger = (
saleInvoice: ISaleInvoice,
ARAccountId: number,
taxPayableAccountId: number
taxPayableAccountId: number,
discountAccountId: number,
otherChargesAccountId: number
): ILedger => {
const entries = this.getInvoiceGLEntries(
saleInvoice,
ARAccountId,
taxPayableAccountId
taxPayableAccountId,
discountAccountId,
otherChargesAccountId
);
return new Ledger(entries);
};
@@ -127,6 +144,7 @@ export class SaleInvoiceGLEntries {
): Partial<ILedgerEntry> => ({
credit: 0,
debit: 0,
currencyCode: saleInvoice.currencyCode,
exchangeRate: saleInvoice.exchangeRate,
@@ -249,6 +267,50 @@ export class SaleInvoiceGLEntries {
return nonZeroTaxEntries.map(transformTaxEntry);
};
/**
* Retrieves the invoice discount GL entry.
* @param {ISaleInvoice} saleInvoice
* @param {number} discountAccountId
* @returns {ILedgerEntry}
*/
private getInvoiceDiscountEntry = (
saleInvoice: ISaleInvoice,
discountAccountId: number
): ILedgerEntry => {
const commonEntry = this.getInvoiceGLCommonEntry(saleInvoice);
return {
...commonEntry,
debit: saleInvoice.discountAmount,
accountId: discountAccountId,
accountNormal: AccountNormal.CREDIT,
index: 1,
} as ILedgerEntry;
};
/**
* Retrieves the invoice adjustment GL entry.
* @param {ISaleInvoice} saleInvoice
* @param {number} adjustmentAccountId
* @returns {ILedgerEntry}
*/
private getAdjustmentEntry = (
saleInvoice: ISaleInvoice,
otherChargesAccountId: number
): ILedgerEntry => {
const commonEntry = this.getInvoiceGLCommonEntry(saleInvoice);
const adjustmentAmount = Math.abs(saleInvoice.adjustment);
return {
...commonEntry,
debit: saleInvoice.adjustment < 0 ? adjustmentAmount : 0,
credit: saleInvoice.adjustment > 0 ? adjustmentAmount : 0,
accountId: otherChargesAccountId,
accountNormal: AccountNormal.CREDIT,
index: 1,
};
};
/**
* Retrieves the invoice GL entries.
* @param {ISaleInvoice} saleInvoice
@@ -258,7 +320,9 @@ export class SaleInvoiceGLEntries {
public getInvoiceGLEntries = (
saleInvoice: ISaleInvoice,
ARAccountId: number,
taxPayableAccountId: number
taxPayableAccountId: number,
discountAccountId: number,
otherChargesAccountId: number
): ILedgerEntry[] => {
const receivableEntry = this.getInvoiceReceivableEntry(
saleInvoice,
@@ -271,6 +335,20 @@ export class SaleInvoiceGLEntries {
saleInvoice,
taxPayableAccountId
);
return [receivableEntry, ...creditEntries, ...taxEntries];
const discountEntry = this.getInvoiceDiscountEntry(
saleInvoice,
discountAccountId
);
const adjustmentEntry = this.getAdjustmentEntry(
saleInvoice,
otherChargesAccountId
);
return [
receivableEntry,
...creditEntries,
...taxEntries,
discountEntry,
adjustmentEntry,
];
};
}