feat: add vendor credit discount and adjustment GL entries

This commit is contained in:
Ahmed Bouhuolia
2024-12-09 11:06:42 +02:00
parent 1d54947764
commit c633fa8522

View File

@@ -56,7 +56,7 @@ export default class VendorCreditGLEntries {
return { return {
...commonEntity, ...commonEntity,
debit: vendorCredit.localAmount, debit: vendorCredit.totalLocal,
accountId: APAccountId, accountId: APAccountId,
contactId: vendorCredit.vendorId, contactId: vendorCredit.vendorId,
accountNormal: AccountNormal.CREDIT, accountNormal: AccountNormal.CREDIT,
@@ -94,6 +94,52 @@ export default class VendorCreditGLEntries {
} }
); );
/**
* 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. * Retrieve the vendor credit GL entries.
* @param {IVendorCredit} vendorCredit - * @param {IVendorCredit} vendorCredit -
@@ -102,7 +148,9 @@ export default class VendorCreditGLEntries {
*/ */
public getVendorCreditGLEntries = ( public getVendorCreditGLEntries = (
vendorCredit: IVendorCredit, vendorCredit: IVendorCredit,
payableAccountId: number payableAccountId: number,
purchaseDiscountAccountId: number,
otherExpensesAccountId: number
): ILedgerEntry[] => { ): ILedgerEntry[] => {
const payableEntry = this.getVendorCreditPayableGLEntry( const payableEntry = this.getVendorCreditPayableGLEntry(
vendorCredit, vendorCredit,
@@ -111,7 +159,15 @@ export default class VendorCreditGLEntries {
const getItemEntry = this.getVendorCreditGLItemEntry(vendorCredit); const getItemEntry = this.getVendorCreditGLItemEntry(vendorCredit);
const itemsEntries = vendorCredit.entries.map(getItemEntry); const itemsEntries = vendorCredit.entries.map(getItemEntry);
return [payableEntry, ...itemsEntries]; const discountEntry = this.getDiscountEntry(
vendorCredit,
purchaseDiscountAccountId
);
const adjustmentEntry = this.getAdjustmentEntry(
vendorCredit,
otherExpensesAccountId
);
return [payableEntry, discountEntry, adjustmentEntry, ...itemsEntries];
}; };
/** /**
@@ -158,10 +214,17 @@ export default class VendorCreditGLEntries {
{}, {},
trx trx
); );
const purchaseDiscountAccount =
await accountRepository.findOrCreatePurchaseDiscountAccount({}, trx);
const otherExpensesAccount =
await accountRepository.findOrCreateOtherExpensesAccount({}, trx);
// Saves the vendor credit GL entries. // Saves the vendor credit GL entries.
const ledgerEntries = this.getVendorCreditGLEntries( const ledgerEntries = this.getVendorCreditGLEntries(
vendorCredit, vendorCredit,
APAccount.id APAccount.id,
purchaseDiscountAccount.id,
otherExpensesAccount.id
); );
const ledger = new Ledger(ledgerEntries); const ledger = new Ledger(ledgerEntries);