In one of my project I wanted to add the Tax Jurisdiction, Jurisdiction Group and Tax value through code, instead of editor going through commerce back-end system.
Therefore, i wrote little Migration Step to add Australian Tax Jurisdiction Group, Jurisdiction and Value.
In AddJurisdictionAndTax we will first delete any existing australian Tax and Jurisdiction and then will re-add it again.
Removing the Existing Australian Jurisdiction and Tax
Adding Australian Jurisdiction and Tax
Migraiton Step only runs once in life. So once you initialize a site for the first time. Migration step will kick in and will add tax value.
Therefore, i wrote little Migration Step to add Australian Tax Jurisdiction Group, Jurisdiction and Value.
[ServiceConfiguration(typeof(IMigrationStep))]
public class ImportAustraliaTreasureSiteContent : IMigrationStep
{
public bool Execute(IProgressMessenger processMessenger)
{
_progressMessenger = processMessenger;
_progressMessenger.AddProgressMessageText("Adding/Updating Australia Market, Warehouse, Tax, Shipping and Payment Provider", false, 0);
try
{ /* Adding Tax */
_progressMessenger.AddProgressMessageText("Adding/Updating Australia Jurisdiction and Tax", false, 0);
AddJurisdictionAndTax(new MarketId("AUS"));
}
catch (Exception exception)
{
processMessenger.AddProgressMessageText("ImportTreasureSiteContent failed: " + exception.Message + "Stack trace:" + exception.StackTrace, true, 0);
Log.Fatal(exception.Message, exception);
return false;
}
return true;
}
In AddJurisdictionAndTax we will first delete any existing australian Tax and Jurisdiction and then will re-add it again.
private static void AddJurisdictionAndTax(IMarket market)
{
/* Get Jurisdiction DTO to add/remove jurisdiction */
var jurisdictions = JurisdictionManager.GetJurisdictions(JurisdictionManager.JurisdictionType.Tax);
RemoveAustralianJurisdictionAndTax(jurisdictions, market);
AddAustralianJurisdictionAndTaxValue(jurisdictions, market);
}
Removing the Existing Australian Jurisdiction and Tax
private static void RemoveAustralianJurisdictionAndTax(JurisdictionDto jurisdictions, IMarket market)
{
var australiaJurisdictions = jurisdictions.Jurisdiction.Where(w => w.CountryCode == market.MarketId);
var jurisdictionGroups = JurisdictionManager.GetJurisdictionGroups(JurisdictionManager.JurisdictionType.Tax);
/* Get relation many to many table for australian jurisdiction */
var relationsGrp = australiaJurisdictions.SelectMany(s => s.GetJurisdictionRelationRows());
/* Save id for future jurisdiction group use */
var relations = relationsGrp.Select(s => new { s.JurisdictionGroupId, s.JurisdictionId }).ToList();
var ausJuriGrps =
jurisdictionGroups.JurisdictionGroup.Where(
s => relations.Select(g => g.JurisdictionGroupId).Contains(s.JurisdictionGroupId));
/* Deleting manay to many middle table */
foreach (var rel in relationsGrp)
{
rel.Delete();
}
/* Deleting australian jurisdiction */
foreach (var ausJuri in australiaJurisdictions)
{
ausJuri.Delete();
}
/* Get JurisfictionsGroup belong to Australian Jurisdictions */
foreach (var ausJuriGrp in ausJuriGrps)
{
ausJuriGrp.Delete();
}
/* Saving delete objects */
JurisdictionManager.SaveJurisdiction(jurisdictions);
/* Fetching Tax DTO belongs to Australian Language */
var taxDto = TaxManager.GetTaxDto(TaxType.SalesTax);
var taxLanguage = taxDto.TaxLanguage.FirstOrDefault(w => w.LanguageCode == market.DefaultLanguage.Name);
if (taxLanguage != null)
{
/* Deleting Australian language and Tax relation */
taxLanguage.Delete();
/* Get tax */
var tax = taxDto.Tax.FirstOrDefault(t => t.TaxId == taxLanguage.TaxId);
/* Deleting Australian Tax */
tax.Delete();
}
TaxManager.SaveTax(taxDto);
}
Adding Australian Jurisdiction and Tax
private static void AddAustralianJurisdictionAndTaxValue(JurisdictionDto jurisdictions, IMarket market)
{
/* Add jurisdictions row */
var jrow = jurisdictions.Jurisdiction.AddJurisdictionRow("Australian Tax", "NSW", market.Countries.FirstOrDefault(),
(int)JurisdictionManager.JurisdictionType.Tax, null, null, null, null,
null, null, AppContext.Current.ApplicationId, "Australian Tax");
/* Add jurisdiction group row */
var jgrow = jurisdictions.JurisdictionGroup.AddJurisdictionGroupRow(AppContext.Current.ApplicationId, "Australian Tax", (int)JurisdictionManager.JurisdictionType.Tax, "Australian Tax");
/* Add relation between jurisdiction and jurisdiction group */
var jrela = jurisdictions.JurisdictionRelation.AddJurisdictionRelationRow(jrow, jgrow);
JurisdictionManager.SaveJurisdiction(jurisdictions);
/* Add Tax */
var taxDto = TaxManager.GetTaxDto(TaxType.SalesTax);
var trow = taxDto.Tax.AddTaxRow((int)TaxType.SalesTax, "Australian Tax", 0, AppContext.Current.ApplicationId);
var tlrow = taxDto.TaxLanguage.AddTaxLanguageRow("Australian Tax", market.DefaultLanguage.Name,
trow);
var tvrow = taxDto.TaxValue.AddTaxValueRow(10d, trow, "Sales", jgrow.JurisdictionGroupId, DateTime.Now,
Guid.Empty);
TaxManager.SaveTax(taxDto);
}
Migraiton Step only runs once in life. So once you initialize a site for the first time. Migration step will kick in and will add tax value.
No comments:
Post a Comment