feat: avoid invoice writes GL entry with zero amount

This commit is contained in:
Ahmed Bouhuolia
2023-09-20 17:22:39 +02:00
parent 5aaa33e585
commit 601434b107
4 changed files with 85 additions and 8 deletions

View File

@@ -0,0 +1,14 @@
import { TenantSeeder } from '@/lib/Seeder/TenantSeeder';
import { InitialTaxRates } from '../data/TaxRates';
export default class SeedTaxRates extends TenantSeeder {
/**
* Seeds initial tax rates to the organization.
*/
up(knex) {
return knex('tax_rates').then(async () => {
// Inserts seed entries.
return knex('tax_rates').insert(InitialTaxRates);
});
}
}

View File

@@ -0,0 +1,30 @@
export const InitialTaxRates = [
{
name: 'Tax Exempt',
code: 'TAX-EXEMPT',
description: 'Exempts goods or services from taxes.',
rate: 0,
active: 1,
},
{
name: 'Tax on Purchases',
code: 'TAX-PURCHASES',
description: 'Fee added to the cost when you buy items.',
rate: 0,
active: 1,
},
{
name: 'Tax on Sales',
code: 'TAX-SALES',
description: 'Fee added to the cost when you sell items.',
rate: 0,
active: 1,
},
{
name: 'Sales Tax on Imports',
code: 'TAX-IMPORTS',
description: 'Fee added to the cost when you sale to another country.',
rate: 0,
active: 1,
},
];

View File

@@ -264,4 +264,13 @@ export default class ItemsEntriesService {
public getTotalItemsEntries(entries: ItemEntry[]): number { public getTotalItemsEntries(entries: ItemEntry[]): number {
return sumBy(entries, (e) => ItemEntry.calcAmount(e)); return sumBy(entries, (e) => ItemEntry.calcAmount(e));
} }
/**
* Retrieve the non-zero tax items entries.
* @param {IItemEntry[]} entries -
* @returns {IItemEntry[]}
*/
public getNonZeroEntries(entries: IItemEntry[]): IItemEntry[] {
return entries.filter((e) => e.taxRate > 0);
}
} }

View File

@@ -6,12 +6,12 @@ import {
ILedgerEntry, ILedgerEntry,
AccountNormal, AccountNormal,
ILedger, ILedger,
ITaxTransaction,
} from '@/interfaces'; } from '@/interfaces';
import { Service, Inject } from 'typedi'; import { Service, Inject } from 'typedi';
import Ledger from '@/services/Accounting/Ledger'; import Ledger from '@/services/Accounting/Ledger';
import LedgerStorageService from '@/services/Accounting/LedgerStorageService'; import LedgerStorageService from '@/services/Accounting/LedgerStorageService';
import HasTenancyService from '@/services/Tenancy/TenancyService'; import HasTenancyService from '@/services/Tenancy/TenancyService';
import ItemsEntriesService from '@/services/Items/ItemsEntriesService';
@Service() @Service()
export class SaleInvoiceGLEntries { export class SaleInvoiceGLEntries {
@@ -21,6 +21,9 @@ export class SaleInvoiceGLEntries {
@Inject() @Inject()
private ledegrRepository: LedgerStorageService; private ledegrRepository: LedgerStorageService;
@Inject()
private itemsEntriesService: ItemsEntriesService;
/** /**
* Writes a sale invoice GL entries. * Writes a sale invoice GL entries.
* @param {number} tenantId - Tenant id. * @param {number} tenantId - Tenant id.
@@ -53,7 +56,7 @@ export class SaleInvoiceGLEntries {
saleInvoice, saleInvoice,
ARAccount.id, ARAccount.id,
taxPayableAccount.id taxPayableAccount.id
); );
// Commits the ledger entries to the storage as UOW. // Commits the ledger entries to the storage as UOW.
await this.ledegrRepository.commit(tenantId, ledger, trx); await this.ledegrRepository.commit(tenantId, ledger, trx);
}; };
@@ -93,7 +96,7 @@ export class SaleInvoiceGLEntries {
'SaleInvoice', 'SaleInvoice',
trx trx
); );
}; };
/** /**
* Retrieves the given invoice ledger. * Retrieves the given invoice ledger.
@@ -218,11 +221,33 @@ export class SaleInvoiceGLEntries {
index: index + 3, index: index + 3,
accountNormal: AccountNormal.CREDIT, accountNormal: AccountNormal.CREDIT,
taxRateId: entry.taxRateId, taxRateId: entry.taxRateId,
taxRate : entry.taxRate, taxRate: entry.taxRate,
}; };
} }
); );
/**
* Retrieves the invoice tax GL entries.
* @param {ISaleInvoice} saleInvoice
* @param {number} taxPayableAccountId
* @returns {ILedgerEntry[]}
*/
private getInvoiceTaxEntries = (
saleInvoice: ISaleInvoice,
taxPayableAccountId: number
): ILedgerEntry[] => {
// Retrieves the non-zero tax entries.
const nonZeroTaxEntries = this.itemsEntriesService.getNonZeroEntries(
saleInvoice.entries
);
const transformTaxEntry = this.getInvoiceTaxEntry(
saleInvoice,
taxPayableAccountId
);
// Transforms the non-zero tax entries to GL entries.
return nonZeroTaxEntries.map(transformTaxEntry);
};
/** /**
* Retrieves the invoice GL entries. * Retrieves the invoice GL entries.
* @param {ISaleInvoice} saleInvoice * @param {ISaleInvoice} saleInvoice
@@ -239,13 +264,12 @@ export class SaleInvoiceGLEntries {
ARAccountId ARAccountId
); );
const transformItemEntry = this.getInvoiceItemEntry(saleInvoice); const transformItemEntry = this.getInvoiceItemEntry(saleInvoice);
const transformTaxEntry = this.getInvoiceTaxEntry( const creditEntries = saleInvoice.entries.map(transformItemEntry);
const taxEntries = this.getInvoiceTaxEntries(
saleInvoice, saleInvoice,
taxPayableAccountId taxPayableAccountId
); );
const creditEntries = saleInvoice.entries.map(transformItemEntry);
const taxEntries = saleInvoice.entries.map(transformTaxEntry);
return [receivableEntry, ...creditEntries, ...taxEntries]; return [receivableEntry, ...creditEntries, ...taxEntries];
}; };
} }