import { Injectable } from '@angular/core'; import * as global from '../../core/common'; import { Observable, throwError } from 'rxjs'; import { Http, Response } from '@angular/http'; import { ITaxSettings } from '../../core/tax/ITaxSettings'; import { ICountryTaxRate } from '../../core/tax/ICountryTaxRate'; import { IStateTaxRate } from '../../core/tax/IStateTaxRate'; import { IProductType } from '../../core/tax/IProductType'; import { ITaxOverride } from '../../core/tax/ITaxOverride'; import { IProductTaxOverride } from '../../core/tax/IProductTaxOverride'; @Injectable({ providedIn: 'root' }) export class TaxService { private SetTaxSettingsUrl = global.serverBase + '/api/Tax/SetTaxSettings'; private GetTaxSettingsUrl = global.serverBase + '/api/Tax/GetTaxSettings'; private GetBaseTaxRatesUrl = global.serverBase + '/api/Tax/GetBaseTaxRates'; private SetCountryTaxRateUrl = global.serverBase + '/api/Tax/SetCountryTaxRate'; private SetStateTaxRateUrl = global.serverBase + '/api/Tax/SetStateTaxRate'; private GetProductTypesUrl = global.serverBase + '/api/Tax/GetProductTypes'; private SetProductTaxOverrideUrl = global.serverBase + '/api/Tax/SetProductTaxOverride'; private SetTaxOverrideUrl = global.serverBase + '/api/Tax/SetTaxOverride'; private GetTaxOverrideUrl = global.serverBase + '/api/Tax/GetShippingTaxOverride'; private GetProductTaxOverridesUrl = global.serverBase + '/api/Tax/GetProductTaxOverrides'; constructor(private http: Http) { } // Get tax settings GetTaxSettings(companyId: number, actionType: string = "All"): Observable { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.get(this.GetTaxSettingsUrl, { params: options }) .map((response: Response) => response.json()) .catch(this.handleError); } // Set tax settings SetTaxSettings(companyId: number, taxSettings: ITaxSettings, actionType: string = "SET"): Observable { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.post(this.SetTaxSettingsUrl, taxSettings, { params: options }) .map((response: Response) => response.json()) .catch(this.handleError); } // Get base tax GetBaseTaxRates(companyId: number, shippingProfileId: number, actionType: string): Observable { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('shippingProfileId', shippingProfileId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.get(this.GetBaseTaxRatesUrl, { params: options }) .map((response: Response) => response.json()) .catch(this.handleError); } // Set country tax SetCountryTaxRate(companyId: number, countryTax: ICountryTaxRate, actionType: string = "SET"): Observable { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.post(this.SetCountryTaxRateUrl, countryTax, { params: options }) .map((response: Response) => response.json()) .catch(this.handleError); } // Set state tax SetStateTaxRate(companyId: number, stateTax: IStateTaxRate, actionType: string = "SET"): Observable { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.post(this.SetStateTaxRateUrl, stateTax, { params: options }) .map((response: Response) => response.json()) .catch(this.handleError); } // Get product types GetProductTypes(companyId: number, actionType: string = "All"): Observable { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.get(this.GetProductTypesUrl, { params: options }) .map((response: Response) => response.json()) .catch(this.handleError); } // Set product tax override SetProductTaxOverride(companyId: number, taxOverride: ITaxOverride, actionType: string = "SET"): Observable { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.post(this.SetProductTaxOverrideUrl, taxOverride, { params: options }) .map((response: Response) => response.json()) .catch(this.handleError); } // Set tax override SetTaxOverride(companyId: number, taxOverride: ITaxOverride, actionType: string = "SET"): Observable { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.post(this.SetTaxOverrideUrl, taxOverride, { params: options }) .map((response: Response) => response.json()) .catch(this.handleError); } // Get tax override GetTaxOverride(companyId: number, shippingProfileId: number, actionType: string = "All"): Observable { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('shippingProfileId', shippingProfileId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.get(this.GetTaxOverrideUrl, { params: options }) .map((response: Response) => response.json()) .catch(this.handleError); } // Get product tax override GetProductTaxOverrides(companyId: number, shippingProfileId: number, actionType: string = "All"): Observable { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('shippingProfileId', shippingProfileId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.get(this.GetProductTaxOverridesUrl, { params: options }) .map((response: Response) => response.json()) .catch(this.handleError); } private handleError(error): Observable { const message = error.statusText || 'Server error'; return throwError(error); } }