Skip to content

Commit

Permalink
PT-12993: clean DB context initialization from the module. no db rel…
Browse files Browse the repository at this point in the history
…ation more. (#54)

feat: clean DB context initialization from the module. no db relation more. (#54)
  • Loading branch information
OlegoO authored Aug 15, 2023
1 parent 496f075 commit c420279
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 241 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# Avalara.Tax
# Avalara Tax Provider

[![CI status](https://github.com/VirtoCommerce/vc-module-avatax/workflows/Module%20CI/badge.svg?branch=dev)](https://github.com/VirtoCommerce/vc-module-avatax/actions?query=workflow%3A"Module+CI") [![Quality gate](https://sonarcloud.io/api/project_badges/measure?project=VirtoCommerce_vc-module-avatax&metric=alert_status&branch=dev)](https://sonarcloud.io/dashboard?id=VirtoCommerce_vc-module-avatax) [![Reliability rating](https://sonarcloud.io/api/project_badges/measure?project=VirtoCommerce_vc-module-avatax&metric=reliability_rating&branch=dev)](https://sonarcloud.io/dashboard?id=VirtoCommerce_vc-module-avatax) [![Security rating](https://sonarcloud.io/api/project_badges/measure?project=VirtoCommerce_vc-module-avatax&metric=security_rating&branch=dev)](https://sonarcloud.io/dashboard?id=VirtoCommerce_vc-module-avatax) [![Sqale rating](https://sonarcloud.io/api/project_badges/measure?project=VirtoCommerce_vc-module-avatax&metric=sqale_rating&branch=dev)](https://sonarcloud.io/dashboard?id=VirtoCommerce_vc-module-avatax)

Avalara.Tax module provides real time integration with Avalara Tax automation. This module is officially <a href="https://www.avalara.com/integrations/virto-commerce" target="_blank">certified by Avalara</a> to be compatible with Avalara API.

![Avalara Tax UI](https://user-images.githubusercontent.com/1835759/48475050-84442c00-e82e-11e8-899f-10452b382ec1.png)

# Documentation
User guide: <a href="https://virtocommerce.com/docs/vc2userguide/order-management/working-with-taxes" target="_blank">Working with taxes in Avalara tax module</a>.
## Documentation
[Working with taxes in Avalara tax module](https://docs.virtocommerce.org/new/user_docs/integrations/avalara/taxes-calculation/)

# Installation
## Installation
Installing the module:
* Automatically: in VC Manager go to Configuration -> Modules -> Avalara tax -> Install
* Manually:
* Download module zip package from https://github.com/VirtoCommerce/vc-module-avatax/releases.
* Open the VC Platform Manager, go to Configuration -> Modules -> Advanced -> upload module package -> Install and choose the package downloaded on previous step.

# Settings
## Avalara connection settings
## Settings
### Avalara connection settings
The module can be configured in three places:
* Platfrom config file: appsettings.json
* Platform-wide settings: Settings -> Taxes -> Avalara or Modules -> Installed -> Avalara tax -> Settings
Expand Down Expand Up @@ -57,7 +57,7 @@ Also, both of these blades have the "Test connection with AvaTax" widget. You ca
> Note: if the tax calculation is disabled (**Avalara.Tax.IsEnabled** is turned off), the Avalara tax provider will ignore any tax requests, and this may lead to incorrect tax calculation.

## Sending transactions to AvaTax
### Sending transactions to AvaTax
`Avalara.Tax` module allows to create AvaTax transactions for orders. It creates a `SalesInvoice` transaction for orders:
* Document code matches order number;
* Document date matches order date;
Expand All @@ -82,7 +82,7 @@ It displays the following information:
* Link to the AvaTax transaction corresponding to the order;
* Raw AvaTax transaction representation.

## Address validation
### Address validation
This module also contains an action for validating addresses using AvaTax.

```cmd
Expand All @@ -92,7 +92,7 @@ Parameters: `request` - an instance of [`AddressValidationRequest` class](https:

This method can be used in storefront theme to prevent creation of order with invalid (not acceptable by AvaTax) address.

## Configuring tax types
### Configuring tax types
Cart/order items should be assigned to tax category in order to calculate taxes correctly. That can be done by applying tax codes to the catalog items. That is called "Tax type" in VirtoCommerce platform. If none of the codes assigned to the item Avalara will calculate taxes by applying default code. So if that is the right choice in your case, you can leave "Tax Type" property value blank. Otherwise define available tax types in general settings of VirtoCommerce platform and apply appropriate types to the items. Note that you can apply tax type to the whole category of items. In that case all items in particular category and in nested subcategories will have the selected tax type code.

The tax type can be selected in the following locations:
Expand All @@ -102,14 +102,14 @@ The tax type can be selected in the following locations:

> Note that the available tax types can be configured in VC Platform settings: Settings -> Commerce -> General -> Tax types.
## Tax exemptions
### Tax exemptions
This module can also provide the exemption number for selected customers to the Avalara Tax API. To configure it, follow these steps:
1. Open the customer details for the customer you want to configure exemption for;
2. Open dynamic properties for that customer;
3. Add the dynamic property named `Tax exempt` and select the `ShortText` type.
4. Fill the exemption certificate number to the value of this property.

# License
## License
Copyright (c) Virto Solutions LTD. All rights reserved.

Licensed under the Virto Commerce Open Software License (the "License"); you
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

12 changes: 0 additions & 12 deletions src/AvaTax.TaxModule.Data/Repositories/AvaTaxDbContext.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalara.AvaTax" Version="18.9.0.234" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.13" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="VirtoCommerce.InventoryModule.Core" Version="3.402.0" />
<PackageReference Include="VirtoCommerce.ShippingModule.Core" Version="3.400.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ public OrdersSynchronizationJob(IOrdersSynchronizationService ordersSynchronizat
_settingsManager = settingsManager;
}

[DisableConcurrentExecution(10)]
// "DisableConcurrentExecutionAttribute" prevents to start simultaneous job payloads.
// Should have short timeout, because this attribute implemented by following manner: newly started job falls into "processing" state immediately.
// Then it tries to receive job lock during timeout. If the lock received, the job starts payload.
// When the job is awaiting desired timeout for lock release, it stucks in "processing" anyway. (Therefore, you should not to set long timeouts (like 24*60*60), this will cause a lot of stucked jobs and performance degradation.)
// Then, if timeout is over and the lock NOT acquired, the job falls into "scheduled" state (this is default fail-retry scenario).
// Failed job goes to "Failed" state (by default) after retries exhausted.
public async Task RunScheduled(IJobCancellationToken cancellationToken, PerformContext context)
{
var currentTime = DateTime.UtcNow;
Expand All @@ -57,16 +50,11 @@ public async Task RunScheduled(IJobCancellationToken cancellationToken, PerformC
var intervalEndTime = lastRunTime == null ? null : (DateTime?)currentTime;
var ordersFeed = new ChangeLogBasedOrdersFeed(_changeLogService, _orderSearchService, lastRunTime, intervalEndTime, BatchSize);

void ProgressCallback(AvaTaxOrdersSynchronizationProgress progress)
{
}

await PerformOrderSynchronization(ordersFeed, ProgressCallback, cancellationToken);
await PerformOrderSynchronization(ordersFeed, (AvaTaxOrdersSynchronizationProgress x) => { }, cancellationToken);

_settingsManager.SetValue(ModuleConstants.Settings.ScheduledOrdersSynchronization.LastExecutionDate.Name, currentTime);
}

[DisableConcurrentExecution(10)]
public async Task RunManually(string[] orderIds, OrdersSynchronizationPushNotification notification,
IJobCancellationToken cancellationToken, PerformContext context)
{
Expand Down Expand Up @@ -105,11 +93,11 @@ void ProgressCallback(AvaTaxOrdersSynchronizationProgress x)
}
}

private async Task PerformOrderSynchronization(IIndexDocumentChangeFeed ordersFeed, Action<AvaTaxOrdersSynchronizationProgress> progressCallback,
private Task PerformOrderSynchronization(IIndexDocumentChangeFeed ordersFeed, Action<AvaTaxOrdersSynchronizationProgress> progressCallback,
IJobCancellationToken cancellationToken)
{
var cancellationTokenWrapper = new JobCancellationTokenWrapper(cancellationToken);
await _ordersSynchronizationService.SynchronizeOrdersAsync(ordersFeed, progressCallback, cancellationTokenWrapper);
return _ordersSynchronizationService.SynchronizeOrdersAsync(ordersFeed, progressCallback, cancellationTokenWrapper);
}
}
}
14 changes: 0 additions & 14 deletions src/AvaTax.TaxModule.Web/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
using AvaTax.TaxModule.Core;
using AvaTax.TaxModule.Core.Services;
using AvaTax.TaxModule.Data.Providers;
using AvaTax.TaxModule.Data.Repositories;
using AvaTax.TaxModule.Data.Services;
using AvaTax.TaxModule.Web.BackgroundJobs;
using Hangfire;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Platform.Core.Modularity;
using VirtoCommerce.Platform.Core.Security;
using VirtoCommerce.Platform.Core.Settings;
using VirtoCommerce.Platform.Data.Extensions;
using VirtoCommerce.TaxModule.Core.Model;
using ModuleConstants = AvaTax.TaxModule.Core.ModuleConstants;

Expand All @@ -33,12 +29,6 @@ public class Module : IModule, IHasConfiguration

public void Initialize(IServiceCollection serviceCollection)
{
serviceCollection.AddDbContext<AvaTaxDbContext>((provider, options) =>
{
var configuration = provider.GetRequiredService<IConfiguration>();
options.UseSqlServer(configuration.GetConnectionString(ModuleInfo.Id) ?? configuration.GetConnectionString("VirtoCommerce"));
});

serviceCollection.AddTransient<Func<IAvaTaxSettings, AvaTaxClient>>(provider => settings =>
{
var machineName = Environment.MachineName;
Expand Down Expand Up @@ -87,10 +77,6 @@ public void PostInitialize(IApplicationBuilder appBuilder)
{
RecurringJob.RemoveIfExists("SendOrdersToAvaTaxJob");
}

using var serviceScope = appBuilder.ApplicationServices.CreateScope();
var dbContext = serviceScope.ServiceProvider.GetRequiredService<AvaTaxDbContext>();
dbContext.Database.MigrateIfNotApplied(MigrationName.GetUpdateV2MigrationName(ModuleInfo.Id));
}
public void Uninstall()
{
Expand Down
8 changes: 4 additions & 4 deletions src/AvaTax.TaxModule.Web/module.manifest
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<module xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<id>VirtoCommerce.AvalaraTax</id>
<version>3.401.0</version>
Expand All @@ -12,8 +12,8 @@
<dependency id="VirtoCommerce.Store" version="3.400.0" />
<dependency id="VirtoCommerce.Tax" version="3.400.0" />
</dependencies>
<title>Avalara tax</title>
<description>APIs and UI for Avalara tax integration</description>
<title>Avalara Tax Provider</title>
<description>Provides real time integration with Avalara Tax https://www.avalara.com/</description>
<authors>
<author>Virto Commerce</author>
</authors>
Expand All @@ -29,4 +29,4 @@
<assemblyFile>VirtoCommerce.AvalaraTaxModule.Web.dll</assemblyFile>
<moduleType>AvaTax.TaxModule.Web.Module, VirtoCommerce.AvalaraTaxModule.Web</moduleType>
<useFullTypeNameInSwagger>false</useFullTypeNameInSwagger>
</module>
</module>

0 comments on commit c420279

Please sign in to comment.