mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 13:50:31 +00:00
feat: assign default sell/purchase tax rates to items (#261)
This commit is contained in:
@@ -115,6 +115,7 @@ export class EditTaxRateService {
|
||||
// Triggers `onTaxRateEdited` event.
|
||||
await this.eventPublisher.emitAsync(events.taxRates.onEdited, {
|
||||
editTaxRateDTO,
|
||||
oldTaxRate,
|
||||
taxRate,
|
||||
tenantId,
|
||||
trx,
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import HasTenancyService from '../Tenancy/TenancyService';
|
||||
|
||||
@Service()
|
||||
export class SyncItemTaxRateOnEditTaxRate {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
/**
|
||||
* Syncs the new tax rate created to item default sell tax rate.
|
||||
* @param {number} tenantId
|
||||
* @param {number} itemId
|
||||
* @param {number} sellTaxRateId
|
||||
*/
|
||||
public updateItemSellTaxRate = async (
|
||||
tenantId: number,
|
||||
oldSellTaxRateId: number,
|
||||
sellTaxRateId: number,
|
||||
trx?: Knex.Transaction
|
||||
) => {
|
||||
const { Item } = this.tenancy.models(tenantId);
|
||||
|
||||
// Can't continue if the old and new sell tax rate id are equal.
|
||||
if (oldSellTaxRateId === sellTaxRateId) return;
|
||||
|
||||
await Item.query().where('sellTaxRateId', oldSellTaxRateId).update({
|
||||
sellTaxRateId,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Syncs the new tax rate created to item default purchase tax rate.
|
||||
* @param {number} tenantId
|
||||
* @param {number} itemId
|
||||
* @param {number} purchaseTaxRateId
|
||||
*/
|
||||
public updateItemPurchaseTaxRate = async (
|
||||
tenantId: number,
|
||||
oldPurchaseTaxRateId: number,
|
||||
purchaseTaxRateId: number,
|
||||
trx?: Knex.Transaction
|
||||
) => {
|
||||
const { Item } = this.tenancy.models(tenantId);
|
||||
|
||||
// Can't continue if the old and new sell tax rate id are equal.
|
||||
if (oldPurchaseTaxRateId === purchaseTaxRateId) return;
|
||||
|
||||
await Item.query(trx)
|
||||
.where('purchaseTaxRateId', oldPurchaseTaxRateId)
|
||||
.update({
|
||||
purchaseTaxRateId,
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { SyncItemTaxRateOnEditTaxRate } from './SyncItemTaxRateOnEditTaxRate';
|
||||
import events from '@/subscribers/events';
|
||||
import { ITaxRateEditedPayload } from '@/interfaces';
|
||||
import { runAfterTransaction } from '../UnitOfWork/TransactionsHooks';
|
||||
|
||||
@Service()
|
||||
export class SyncItemTaxRateOnEditTaxSubscriber {
|
||||
@Inject()
|
||||
private syncItemRateOnEdit: SyncItemTaxRateOnEditTaxRate;
|
||||
|
||||
/**
|
||||
* Attaches events with handles.
|
||||
*/
|
||||
public attach(bus) {
|
||||
bus.subscribe(
|
||||
events.taxRates.onEdited,
|
||||
this.handleSyncNewTaxRateToItemTaxRate
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncs the new tax rate created to default item tax rates.
|
||||
* @param {ITaxRateEditedPayload} payload -
|
||||
*/
|
||||
private handleSyncNewTaxRateToItemTaxRate = async ({
|
||||
taxRate,
|
||||
tenantId,
|
||||
oldTaxRate,
|
||||
trx,
|
||||
}: ITaxRateEditedPayload) => {
|
||||
runAfterTransaction(trx, async () => {
|
||||
await this.syncItemRateOnEdit.updateItemPurchaseTaxRate(
|
||||
tenantId,
|
||||
oldTaxRate.id,
|
||||
taxRate.id
|
||||
);
|
||||
await this.syncItemRateOnEdit.updateItemSellTaxRate(
|
||||
tenantId,
|
||||
oldTaxRate.id,
|
||||
taxRate.id
|
||||
);
|
||||
});
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user