import { Component, OnInit } from '@angular/core'; import { DialogService, SelectItem } from 'primeng/api'; import { AddTaxOverridesComponent } from '../popups/add-tax-overrides/add-tax-overrides.component'; import { CookieService } from 'ngx-cookie-service'; import { Router,ActivatedRoute } from '@angular/router'; import { TaxService } from '../../services/tax/tax.service'; import { ICountryTaxRate } from '../../core/tax/ICountryTaxRate'; import { ITaxOverride } from '../../core/tax/ITaxOverride'; import { IProductTaxOverride } from '../../core/tax/IProductTaxOverride'; @Component({ selector: 'app-tax-overrides', templateUrl: './tax-overrides.component.html', styleUrls: ['./tax-overrides.component.scss'] }) export class TaxOverridesComponent implements OnInit { shippingProfileId: number; companyId: number; baseTax: ICountryTaxRate; shippingTaxList:ITaxOverride[]=[]; productTaxList:IProductTaxOverride[]=[]; taxType:SelectItem[]=[]; constructor(public dialogService: DialogService, private cookieService: CookieService, private route: ActivatedRoute, private taxService: TaxService, public router: Router) { } ngOnInit() { this.companyId = +this.cookieService.get('CompanyId') this.shippingProfileId = +this.route.snapshot.paramMap.get("id"); this.baseTax = {CountryId:0,ShippingProfileId:this.shippingProfileId,CountryName:'',StateTaxRates:[],TaxRate:0} // get country tax this.getBaseTax() } // get country tax getBaseTax() { this.taxService.GetBaseTaxRates(this.companyId, this.shippingProfileId, 'BYCOUNTRY').subscribe(data => { this.baseTax = data; // get state tax type this.taxType = [ {label:'Added to '+ this.baseTax.TaxRate + '% federal tax',value:1}, {label:'Instead of '+ this.baseTax.TaxRate + '% federal tax',value:2}, {label:'Compound on top of '+ this.baseTax.TaxRate + '% federal tax',value:3}, ] this.getShippingTaxList(); this.getProductTaxList(); }) } addTaxOverRides() { const ref = this.dialogService.open(AddTaxOverridesComponent, { header: 'Add Tax Overrides', width: '35%', data:{countryTaxRate:this.baseTax,itemType:0, IsAddArea:false} }); // execute when close the add new popup ref.onClose.subscribe(data=>{ if(data==2){ this.getShippingTaxList(); } else{ this.getProductTaxList(); } }); } // get details getShippingTaxList(){ this.taxService.GetTaxOverride(this.companyId,this.shippingProfileId).subscribe(data=>{ this.shippingTaxList = data; this.shippingTaxList.forEach(element => { element.TaxTypeStr = this.taxType.find(x=>x.value==element.TaxType).label; }); }) } // get details getProductTaxList(){ this.taxService.GetProductTaxOverrides(this.companyId,this.shippingProfileId).subscribe(data=>{ this.productTaxList = data; this.productTaxList.forEach(product => { product.DesignatedList.forEach(element => { element.TaxTypeStr = this.taxType.find(x=>x.value==element.TaxType).label; }); }); }) } AddDestinedArea(itemType:number){ const ref = this.dialogService.open(AddTaxOverridesComponent, { header: 'Add Another Designated Area', width: '35%', data:{countryTaxRate:this.baseTax, itemType:itemType, IsAddArea:true} }); // execute when close the add new popup ref.onClose.subscribe(data=>{ if(data==2){ this.getShippingTaxList(); } else{ this.getProductTaxList(); } }); } AddProductDestinedArea(product:IProductTaxOverride){ const ref = this.dialogService.open(AddTaxOverridesComponent, { header: 'Add Another Designated Area', width: '35%', data:{countryTaxRate:this.baseTax, itemType:1, IsAddArea:true,productTax:product} }); // execute when close the add new popup ref.onClose.subscribe(data=>{ if(data==2){ this.getShippingTaxList(); } else{ this.getProductTaxList(); } }); } showEditShippingPopup(tax:ITaxOverride){ const ref = this.dialogService.open(AddTaxOverridesComponent, { header: 'Edit Tax Overrides', width: '35%', data:{countryTaxRate:this.baseTax, itemType:2, IsAddArea:true,selectedTax:tax} }); // execute when close the add new popup ref.onClose.subscribe(data=>{ if(data==2){ this.getShippingTaxList(); } else{ this.getProductTaxList(); } }); } showProductTaxEditPopup(tax:ITaxOverride, product:IProductTaxOverride){ const ref = this.dialogService.open(AddTaxOverridesComponent, { header: 'Edit Tax Overrides', width: '35%', data:{countryTaxRate:this.baseTax, itemType:1, IsAddArea:true,selectedTax:tax,productTax:product} }); // execute when close the add new popup ref.onClose.subscribe(data=>{ if(data==2){ this.getShippingTaxList(); } else{ this.getProductTaxList(); } }); } showProductEditPopup(tax:IProductTaxOverride){ const ref = this.dialogService.open(AddTaxOverridesComponent, { header: 'Edit Tax Overrides', width: '35%', data:{countryTaxRate:this.baseTax, itemType:1,IsAddArea:false, IsEditProduct:true,productTax:tax} }); // execute when close the add new popup ref.onClose.subscribe(data=>{ if(data==2){ this.getShippingTaxList(); } else{ this.getProductTaxList(); } }); } // delete whole product tax deleteProduct(product:IProductTaxOverride){ var taxOverride = { ShippingProfileId: product.ShippingProfileId, CountryId: 0, StateId: 0, TaxRate: 0, TaxType: 0, ItemId: 0, ItemType: 0, ProductTaxId: product.Id, TaxOverrideId: 0 } this.taxService.SetProductTaxOverride(this.companyId,taxOverride,"DELETE").subscribe(data=>{ this.getProductTaxList(); }) } deleteProductDesignation(tax:ITaxOverride){ this.taxService.SetTaxOverride(this.companyId,tax,"DELETE").subscribe(data=>{ this.getProductTaxList(); }) } deleteShippingTax(tax:ITaxOverride){ this.taxService.SetTaxOverride(this.companyId,tax,"DELETE").subscribe(data=>{ this.getShippingTaxList(); }) } backToTax() { this.router.navigate(['/taxes']); } }