-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: assign default sell/purchase tax rates to items (#261)
- Loading branch information
Showing
25 changed files
with
400 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...ges/server/src/database/migrations/20231004020636_add_sell_purchase_tax_to_items_table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
exports.up = (knex) => { | ||
return knex.schema.table('items', (table) => { | ||
table | ||
.integer('sell_tax_rate_id') | ||
.unsigned() | ||
.references('id') | ||
.inTable('tax_rates'); | ||
table | ||
.integer('purchase_tax_rate_id') | ||
.unsigned() | ||
.references('id') | ||
.inTable('tax_rates'); | ||
}); | ||
}; | ||
|
||
exports.down = (knex) => { | ||
return knex.schema.dropTableIfExists('tax_rates'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/server/src/services/TaxRates/SyncItemTaxRateOnEditTaxRate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); | ||
}; | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/server/src/services/TaxRates/SyncItemTaxRateOnEditTaxSubscriber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
); | ||
}); | ||
}; | ||
} |
Oops, something went wrong.