feat: import and export tax rates

This commit is contained in:
Ahmed Bouhuolia
2024-08-11 19:51:16 +02:00
parent c7c021c969
commit 72678bb936
13 changed files with 254 additions and 29 deletions

View File

@@ -1,9 +1,10 @@
import { ServiceError } from '@/exceptions';
import { Knex } from 'knex';
import { Inject, Service } from 'typedi';
import { difference } from 'lodash';
import { ServiceError } from '@/exceptions';
import HasTenancyService from '../Tenancy/TenancyService';
import { IItemEntryDTO, ITaxRate } from '@/interfaces';
import { ERRORS } from './constants';
import { difference } from 'lodash';
@Service()
export class CommandTaxRatesValidators {
@@ -44,11 +45,16 @@ export class CommandTaxRatesValidators {
* Validates the tax code uniquiness.
* @param {number} tenantId
* @param {string} taxCode
* @param {Knex.Transaction} trx -
*/
public async validateTaxCodeUnique(tenantId: number, taxCode: string) {
public async validateTaxCodeUnique(
tenantId: number,
taxCode: string,
trx?: Knex.Transaction
) {
const { TaxRate } = this.tenancy.models(tenantId);
const foundTaxCode = await TaxRate.query().findOne({ code: taxCode });
const foundTaxCode = await TaxRate.query(trx).findOne({ code: taxCode });
if (foundTaxCode) {
throw new ServiceError(ERRORS.TAX_CODE_NOT_UNIQUE);

View File

@@ -1,3 +1,4 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import {
ICreateTaxRateDTO,
@@ -7,7 +8,6 @@ import {
import UnitOfWork from '../UnitOfWork';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import HasTenancyService from '../Tenancy/TenancyService';
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import { CommandTaxRatesValidators } from './CommandTaxRatesValidators';
@@ -32,36 +32,41 @@ export class CreateTaxRate {
*/
public async createTaxRate(
tenantId: number,
createTaxRateDTO: ICreateTaxRateDTO
createTaxRateDTO: ICreateTaxRateDTO,
trx?: Knex.Transaction
) {
const { TaxRate } = this.tenancy.models(tenantId);
// Validates the tax code uniquiness.
await this.validators.validateTaxCodeUnique(
tenantId,
createTaxRateDTO.code
createTaxRateDTO.code,
trx
);
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onTaxRateCreating` event.
await this.eventPublisher.emitAsync(events.taxRates.onCreating, {
createTaxRateDTO,
tenantId,
trx,
} as ITaxRateCreatingPayload);
return this.uow.withTransaction(
tenantId,
async (trx: Knex.Transaction) => {
// Triggers `onTaxRateCreating` event.
await this.eventPublisher.emitAsync(events.taxRates.onCreating, {
createTaxRateDTO,
tenantId,
trx,
} as ITaxRateCreatingPayload);
const taxRate = await TaxRate.query(trx).insertAndFetch({
...createTaxRateDTO,
});
const taxRate = await TaxRate.query(trx).insertAndFetch({
...createTaxRateDTO,
});
// Triggers `onTaxRateCreated` event.
await this.eventPublisher.emitAsync(events.taxRates.onCreated, {
createTaxRateDTO,
taxRate,
tenantId,
trx,
} as ITaxRateCreatedPayload);
// Triggers `onTaxRateCreated` event.
await this.eventPublisher.emitAsync(events.taxRates.onCreated, {
createTaxRateDTO,
taxRate,
tenantId,
trx,
} as ITaxRateCreatedPayload);
return taxRate;
});
return taxRate;
},
trx
);
}
}

View File

@@ -0,0 +1,18 @@
import { Inject, Service } from 'typedi';
import { Exportable } from '../Export/Exportable';
import { TaxRatesApplication } from './TaxRatesApplication';
@Service()
export class TaxRatesExportable extends Exportable {
@Inject()
private taxRatesApplication: TaxRatesApplication;
/**
* Retrieves the accounts data to exportable sheet.
* @param {number} tenantId
* @returns
*/
public exportable(tenantId: number) {
return this.taxRatesApplication.getTaxRates(tenantId);
}
}

View File

@@ -0,0 +1,18 @@
export const TaxRatesSampleData = [
{
'Tax Name': 'Value Added Tax',
Code: 'VAT-STD',
Rate: '20',
Description: 'Standard VAT rate applied to most goods and services.',
'Is Non Recoverable': 'F',
Active: 'T',
},
{
'Tax Name': 'Luxury Goods Tax',
Code: 'TAX-LUXURY',
Rate: '25',
Description: 'Tax imposed on the sale of luxury items.',
'Is Non Recoverable': 'T',
Active: 'T',
},
];

View File

@@ -0,0 +1,46 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import { ICreateTaxRateDTO } from '@/interfaces';
import { CreateTaxRate } from './CreateTaxRate';
import { Importable } from '../Import/Importable';
import { TaxRatesSampleData } from './TaxRatesImportable.SampleData';
@Service()
export class TaxRatesImportable extends Importable {
@Inject()
private createTaxRateService: CreateTaxRate;
/**
* Importing to tax rate creating service.
* @param {number} tenantId -
* @param {ICreateTaxRateDTO} ICreateTaxRateDTO -
* @param {Knex.Transaction} trx -
* @returns
*/
public importable(
tenantId: number,
createAccountDTO: ICreateTaxRateDTO,
trx?: Knex.Transaction
) {
return this.createTaxRateService.createTaxRate(
tenantId,
createAccountDTO,
trx
);
}
/**
* Concurrrency controlling of the importing process.
* @returns {number}
*/
public get concurrency() {
return 1;
}
/**
* Retrieves the sample data that used to download accounts sample sheet.
*/
public sampleData(): any[] {
return TaxRatesSampleData;
}
}