import { Component, OnInit, ViewChild, QueryList, ViewChildren } from '@angular/core'; import { SelectItem, DialogService, TreeNode } from 'primeng/api'; import { DefaultTaxRatesComponent } from '../popups/default-tax-rates/default-tax-rates.component'; import { AddPriceBasedRateComponent } from '../popups/add-price-based-rate/add-price-based-rate.component'; import { GeneralService } from '../../services/general/general.service'; import { IContinent } from '../../core/general/IContinent'; import { CookieService } from 'ngx-cookie-service'; import { IShippingProfile } from '../../core/shipping/IShippingProfile'; import { IShippingCountry } from '../../core/shipping/IShippingCountry'; import { Router, ActivatedRoute } from '@angular/router'; import { AddDimensionBasedRateComponent } from '../popups/add-dimension-based-rate/add-dimension-based-rate.component'; import { AddWeightBasedRateComponent } from '../popups/add-weight-based-rate/add-weight-based-rate.component'; import { TreeviewItem, TreeviewConfig, TreeItem, DropdownTreeviewComponent } from '../../lib'; import { CountryService } from '../../services/shipping/country.service'; import { SubjectBehaviorService } from '../../services/subject-behavior.service'; import { isNil } from 'lodash'; @Component({ selector: 'app-create-shipping-profile', templateUrl: './create-shipping-profile.component.html', styleUrls: ['./create-shipping-profile.component.scss'], providers: [ CountryService ] }) export class CreateShippingProfileComponent implements OnInit { shippingCosts: any; countries: any; processigTimeList: SelectItem[]; customRange: boolean = false; FixedPrice: boolean = false; priceBased: boolean = false; fromList: SelectItem[] = []; toList: SelectItem[] = []; //continents:IContinent[]=[]; //selectedContinents:IContinent[]; @ViewChild(DropdownTreeviewComponent) dropdownTreeviewComponent: DropdownTreeviewComponent; @ViewChildren(DropdownTreeviewComponent) dropdownTreeviewComponentList: QueryList; itemList = []; selectedItems = []; settings = {}; selectedShippingFrom: IContinent[] = []; selectedShippingTo: IContinent[] = []; continents: IContinent[] = []; stateSettings = {}; companyId: number; shippingProfile: IShippingProfile; shippingProfileId: number = 0; currencyType: string; //Tree node isShowShippingOriginList: boolean = false; selectedFiles2: TreeNode[]; filesTree: TreeNode[]; packages: SelectItem[] = []; lengths: SelectItem[] = []; originCountries: TreeviewItem[] = []; zoneCountries: TreeviewItem[] = []; countryList: TreeItem[]; constructor(public dialogService: DialogService, private generalService: GeneralService, private cookieService: CookieService, private route: ActivatedRoute, public router: Router, private service: CountryService, private subjectBehaviorService: SubjectBehaviorService) { } ngOnInit() { // for tree view //this.items = this.service.getCountries(); this.companyId = +this.cookieService.get('CompanyId') this.shippingProfileId = +this.route.snapshot.paramMap.get("id"); // initialize this.shippingProfile = { Id: 0, ProcessTimeId: 0, ShippingCostType: 0, ShippingProcess: { Id: 0, CustomDuration: 0, FromTime: 0, IsCustomRange: false, ProcessTimeRangeType: 0, ToTime: 0 } } this.shippingProfile.FixedPrice = { OneItem: 0, Id: 0, EachAdditionalItem: 0 } this.shippingProfile.PriceBased = []; this.shippingProfile.WeightBased = []; this.shippingProfile.DimensionBased = []; this.shippingProfile.ShippingOrigin = []; this.shippingProfile.ShippingZone = [] this.getShippingCost(); this.getProcessingTime(); this.getFromList(); this.getToList(); this.getCurrencyFormat(); this.itemList = [ { "category": "ALL", "id": 0, "itemName": "", "capital": "", "image": "" }, { "category": "asia", "id": 2, "itemName": "Singapore", "capital": "Singapore", "image": "http://www.sciencekids.co.nz/images/pictures/flags96/Singapore.jpg" }, { "category": "europe", "id": 3, "itemName": "United Kingdom", "capital": "London", "image": "http://www.sciencekids.co.nz/images/pictures/flags96/United_Kingdom.jpg" }, { "category": "northamerica", "id": 4, "itemName": "Canada", "capital": "Ottawa", "image": "http://www.sciencekids.co.nz/images/pictures/flags96/Canada.jpg" }, { "category": "asia", "id": 5, "itemName": "South Korea", "capital": "Seoul", "image": "http://www.sciencekids.co.nz/images/pictures/flags96/South_Korea.jpg" }, { "category": "latinamerica", "id": 6, "itemName": "Brazil", "capital": "Brasilia", "image": "http://www.sciencekids.co.nz/images/pictures/flags96/Brazil.jpg" } ]; // this.selectedItems = [ // {"category":"asia","id":1,"itemName":"India","capital":"Delhi","image":"http://www.sciencekids.co.nz/images/pictures/flags96/India.jpg"}, // {"category":"asia","id":2,"itemName":"Singapore", "capital":"Singapore","image":"http://www.sciencekids.co.nz/images/pictures/flags96/Singapore.jpg"}, // {"category":"europe","id":3,"itemName":"United Kingdom", "capital":"London","image":"http://www.sciencekids.co.nz/images/pictures/flags96/United_Kingdom.jpg"}, // {"category":"northamerica","id":4,"itemName":"Canada","capital":"Ottawa","image":"http://www.sciencekids.co.nz/images/pictures/flags96/Canada.jpg"}] this.settings = { text: "Select country or region", selectAllText: '', unSelectAllText: '', enableSearchFilter: false, classes: "myclass custom-class", showCheckbox: true, groupBy: "Title", singleSelection: true }; this.stateSettings = { text: "", selectAllText: '', unSelectAllText: '', enableSearchFilter: false, classes: "myclass custom-class", showCheckbox: true, singleSelection: true }; if (this.shippingProfileId > 0) { this.selectedShippingFrom = null this.selectedShippingTo = null; this.GetShippingProfile(); } else { //this.getContinents(); this.getCountries(); this.selectedShippingFrom = null this.selectedShippingTo = null; } } async getCountries() { this.generalService.getCountryList().subscribe(data => { this.countryList = data; // convert country list to the tree view this.countryList.forEach(element => { element.type = "ORIG"; var objOrg = new TreeviewItem(element); this.originCountries.push(objOrg); element.type = "ZONE" var objZone = new TreeviewItem(element); this.zoneCountries.push(objZone); }); // set selected values to subject behaviour this.subjectBehaviorService.countryOriginList.next(this.shippingProfile.ShippingOrigin); this.subjectBehaviorService.countryZoneList.next(this.shippingProfile.ShippingZone); // get expandable origin countries var originCountryExpandList: SelectItem[] = []; var originCountryExpandable = this.shippingProfile.ShippingOrigin.filter(x => x.StateId > 0) originCountryExpandable.forEach(element => { originCountryExpandList.push({ label: element.ContinentId, value: element.CountryId }); }); //remove duplicates originCountryExpandList = originCountryExpandList.map(e => e['value']).map((e, i, final) => final.indexOf(e) === i && i) .filter(e => originCountryExpandList[e]).map(e => originCountryExpandList[e]); // get expandable zone countries var zoneCountryExpandList: SelectItem[] = []; var zoneCountryExpandable = this.shippingProfile.ShippingZone.filter(x => x.StateId > 0) zoneCountryExpandable.forEach(element => { zoneCountryExpandList.push({ label: element.ContinentId, value: element.CountryId }); }); //remove duplicates zoneCountryExpandList = zoneCountryExpandList.map(e => e['value']).map((e, i, final) => final.indexOf(e) === i && i) .filter(e => zoneCountryExpandList[e]).map(e => zoneCountryExpandList[e]); zoneCountryExpandList = zoneCountryExpandList.filter((elem, i, arr) => { if (arr.indexOf(elem) === i) { return elem } }); // 'checked' the selected countries in the tree view this.shippingProfile.ShippingOrigin.forEach(element => { // checked continents if (element.ContinentId != "" && element.CountryId == 0 && element.StateId == 0) { this.originCountries.find(x => x.value == element.ContinentId).internalChecked = true; this.dropdownTreeviewComponentList.first.treeviewComponent.onItemCheckedChange(this.originCountries.find(x => x.value == element.ContinentId), true); if(this.originCountries.find(x => x.value == element.ContinentId).internalChildren){ this.originCountries.find(x => x.value == element.ContinentId).internalChildren.forEach(x=>x.internalChecked = true); } } // checked countries else if (element.ContinentId != "" && element.CountryId > 0 && element.StateId == 0) { this.originCountries.find(x => x.value == element.ContinentId).internalChildren.find(y => y.value == element.CountryId).internalChecked = true; this.dropdownTreeviewComponentList.first.treeviewComponent.onItemCheckedChange (this.originCountries.find(x => x.value == element.ContinentId).internalChildren.find(y => y.value == element.CountryId), true); // mark parent as intermediate this.originCountries.find(x => x.value == element.ContinentId).checked = undefined; } }); // bind states of the expandable origin countries for (let index = 0; index < originCountryExpandList.length; index++) { const element = originCountryExpandList[index]; // get states from backend this.generalService.getStateList(element.value.toString(), "BYID").subscribe(data => { var children: TreeItem[] = []; var newChildren: TreeviewItem[] = []; children = data; if (!isNil(children) && children.length > 0) { children.map(child => { newChildren.push(new TreeviewItem(child)); }); } // bind the states this.originCountries.find(x => x.value == element.label).internalChildren.find(y => y.value == element.value).internalChildren = newChildren; // get the country node var countryNode = this.dropdownTreeviewComponentList.first.treeviewComponent.treeviewComponentList.find(x => x.item.value == element.label).treeviewComponentList. find(y => y.item.value == element.value); // get number of selected states var states = this.shippingProfile.ShippingOrigin.filter(x=>x.ContinentId == element.label && x.CountryId ==element.value && x.StateId>0) countryNode.item.selectedStateCount = (states!=undefined) ? states.length :0; // expand the country node countryNode.onCollapseExpand(); }); } // 'checked' the selected countries in the tree view this.shippingProfile.ShippingZone.forEach(element => { // checked continents if (element.ContinentId != "" && element.CountryId == 0 && element.StateId == 0) { this.zoneCountries.find(x => x.value == element.ContinentId).internalChecked = true; this.dropdownTreeviewComponentList.last.treeviewComponent.onItemCheckedChange(this.zoneCountries.find(x => x.value == element.ContinentId), true); if(this.zoneCountries.find(x => x.value == element.ContinentId).internalChildren){ this.zoneCountries.find(x => x.value == element.ContinentId).internalChildren.forEach(x=>x.internalChecked = true); } } // checked countries else if (element.ContinentId != "" && element.CountryId > 0 && element.StateId == 0) { this.zoneCountries.find(x => x.value == element.ContinentId).internalChildren.find(y => y.value == element.CountryId).internalChecked = true; this.dropdownTreeviewComponentList.last.treeviewComponent.onItemCheckedChange (this.zoneCountries.find(x => x.value == element.ContinentId).internalChildren.find(y => y.value == element.CountryId), true); // mark parent as intermediate this.zoneCountries.find(x => x.value == element.ContinentId).checked = undefined; } }); // bind states of the expandable zone countries for (let index = 0; index < zoneCountryExpandList.length; index++) { const element = zoneCountryExpandList[index]; // get states from backend this.generalService.getStateList(element.value.toString(), "BYID").subscribe(data => { var children: TreeItem[] = []; var newChildren: TreeviewItem[] = []; children = data; if (!isNil(children) && children.length > 0) { children.map(child => { newChildren.push(new TreeviewItem(child)); }); } // bind the states this.zoneCountries.find(x => x.value == element.label).internalChildren.find(y => y.value == element.value).internalChildren = newChildren; // get the country node var countryNode = this.dropdownTreeviewComponentList.last.treeviewComponent.treeviewComponentList.find(x => x.item.value == element.label).treeviewComponentList. find(y => y.item.value == element.value); // get number of selected states var states = this.shippingProfile.ShippingZone.filter(x=>x.ContinentId == element.label && x.CountryId ==element.value && x.StateId>0) countryNode.item.selectedStateCount = (states!=undefined) ? states.length :0; // expand the country node countryNode.onCollapseExpand(); }); } }); } getShippingCost() { this.generalService.GetShippingTypes(this.companyId).subscribe(data => { this.shippingCosts = data; }) } getFromList() { this.fromList = []; for (var i = 1; i <= 10; i++) { this.fromList.push({ label: i.toString(), value: i }); } } getToList() { this.toList = []; for (var i = 1; i <= 10; i++) { this.toList.push({ label: i.toString(), value: i }); } } backToShipping() { this.router.navigate(['/shipping']); } onChangeShippingCosts(e) { // if (e.value == 2) { // this.FixedPrice = true; // this.priceBased = false; // } else if (e.value == 3) { // this.priceBased = true; // this.FixedPrice = false; // } else if (e.value == 4) { // this.priceBased = true; // this.FixedPrice = false; // } else if (e.value == 5) { // this.priceBased = true; // this.FixedPrice = false; // } // else { // this.priceBased = false; // this.FixedPrice = false; // } } onItemSelect(item: any) { console.log(item); item.IsSelected = true; console.log(this.selectedShippingFrom); } OnItemDeSelect(item: any) { console.log(item); item.IsSelected = false; console.log(this.selectedShippingFrom); } onSelectAll(items: any) { console.log(items); } onDeSelectAll(items: any) { console.log(items); } getProcessingTime() { this.processigTimeList = [ { label: '1 business day', value: 1 }, { label: '1-2 business days', value: 2 }, { label: '1-3 business days', value: 3 }, { label: '3-5 business days', value: 4 }, { label: '1-2 weeks', value: 5 }, { label: '2-3 weeks', value: 6 }, { label: '3-4 weeks', value: 7 }, { label: '4-6 weeks', value: 8 }, { label: '6-8 weeks', value: 9 }, { label: 'Custom range', value: 10 }, ]; } onChangeProcessingTime(e) { if (e.value == 10) { this.shippingProfile.ShippingProcess.IsCustomRange = true; } else { this.shippingProfile.ShippingProcess.IsCustomRange = false; } } addWeightBasedRate() { const ref = this.dialogService.open(AddWeightBasedRateComponent, { header: 'Add Weight Based Rate', width: '45%', // data: {weightBased:this.shippingProfile.WeightBased} data: { weightBased: null } }); ref.onClose.subscribe(data => { // this.shippingProfile.WeightBased = data; this.shippingProfile.WeightBased.push(data); }); } addPriceBasedRate() { const ref = this.dialogService.open(AddPriceBasedRateComponent, { header: 'Add Price Based Rate', width: '45%', //data: {priceBased:this.shippingProfile.PriceBased} data: { priceBased: null } }); ref.onClose.subscribe(data => { // this.shippingProfile.PriceBased = data; this.shippingProfile.PriceBased.push(data); }); } addDimensionBasedRate() { const ref = this.dialogService.open(AddDimensionBasedRateComponent, { header: 'Add Dimension Based Rate', width: '55%', // data: { dimensionBased: this.shippingProfile.DimensionBased } data: { dimensionBased: null } }); ref.onClose.subscribe(data => { // this.shippingProfile.DimensionBased = data; this.shippingProfile.DimensionBased.push(data); }); } // get continents getContinents() { this.generalService.GetContinents().subscribe(data => { this.continents = data; // bind selected shipping origin if (this.shippingProfile.ShippingOrigin != undefined && this.shippingProfile.ShippingOrigin != null && this.shippingProfile.ShippingOrigin.length > 0) { // this.selectedShippingFrom = []; // this.shippingProfile.ShippingOrigin.forEach(element => { // this.selectedShippingFrom.push(this.continents.find(x => x.id == element.CountryId)); // }); } // bind selected shipping zone if (this.shippingProfile.ShippingZone != undefined && this.shippingProfile.ShippingZone != null && this.shippingProfile.ShippingZone.length > 0) { this.selectedShippingTo = []; this.shippingProfile.ShippingZone.forEach(element => { this.selectedShippingTo.push(this.continents.find(x => x.id == element.CountryId)); }); } }) } // save shipping profile save() { // get shipping origin this.shippingProfile.ShippingOrigin = []; if (this.originCountries != undefined && this.originCountries != null && this.originCountries.length > 0) { this.originCountries.forEach(continent => { if (continent.internalChecked == true) { var continentSelection: IShippingCountry = { ContinentId: continent.value, CountryId: 0, StateId: 0 } this.shippingProfile.ShippingOrigin.push(continentSelection); } else if (continent.internalChildren != undefined && continent.internalChildren != null && continent.internalChildren.length > 0) { continent.internalChildren.forEach(country => { if (country.internalChecked == true) { var countrySelection: IShippingCountry = { ContinentId: continent.value, CountryId: country.value, StateId: 0 } this.shippingProfile.ShippingOrigin.push(countrySelection); } else if (country.internalChildren != undefined && country.internalChildren != null && country.internalChildren.length > 0) { country.internalChildren.forEach(state => { if (state.internalChecked == true) { var stateSelection: IShippingCountry = { ContinentId: continent.value, CountryId: country.value, StateId: state.value } this.shippingProfile.ShippingOrigin.push(stateSelection); } }); } }); } }); } // if (this.selectedShippingFrom != undefined && this.selectedShippingFrom.length > 0) { // this.selectedShippingFrom.forEach(element => { // var country: IShippingCountry = { ContinentId: element.Code, CountryId: element.id, StateId: 0 } // this.shippingProfile.ShippingOrigin.push(country); // }); // } // get shipping zone this.shippingProfile.ShippingZone = []; if (this.zoneCountries != undefined && this.zoneCountries != null && this.zoneCountries.length > 0) { this.zoneCountries.forEach(continent => { if (continent.internalChecked == true) { var continentSelection: IShippingCountry = { ContinentId: continent.value, CountryId: 0, StateId: 0 } this.shippingProfile.ShippingZone.push(continentSelection); } else if (continent.internalChildren != undefined && continent.internalChildren != null && continent.internalChildren.length > 0) { continent.internalChildren.forEach(country => { if (country.internalChecked == true) { var countrySelection: IShippingCountry = { ContinentId: continent.value, CountryId: country.value, StateId: 0 } this.shippingProfile.ShippingZone.push(countrySelection); } else if (country.internalChildren != undefined && country.internalChildren != null && country.internalChildren.length > 0) { country.internalChildren.forEach(state => { if (state.internalChecked == true) { var stateSelection: IShippingCountry = { ContinentId: continent.value, CountryId: country.value, StateId: state.value } this.shippingProfile.ShippingZone.push(stateSelection); } }); } }); } }); } // if (this.selectedShippingTo != undefined && this.selectedShippingTo.length > 0) { // this.selectedShippingTo.forEach(element => { // var country: IShippingCountry = { ContinentId: element.Code, CountryId: element.id, StateId: 0 } // this.shippingProfile.ShippingZone.push(country); // }); // } var action = (this.shippingProfileId > 0) ? "UPDATE" : "SET"; this.generalService.SetShippingProfile(this.companyId, this.shippingProfile, action).subscribe(data => { this.router.navigate(['/shipping']); }) } cancel() { // initialize this.shippingProfile = { Id: 0, ProcessTimeId: 0, ShippingCostType: 0, ShippingProcess: { Id: 0, CustomDuration: 0, FromTime: 0, IsCustomRange: false, ProcessTimeRangeType: 0, ToTime: 0 } } this.selectedShippingFrom = null; this.selectedShippingTo = null; this.router.navigate(['/shipping']); } // Get shipping profile GetShippingProfile() { this.generalService.GetShippingProfile(this.companyId, this.shippingProfileId, "BYID").subscribe(data => { this.shippingProfile = data; if (this.shippingProfile.ShippingCostType == 5 && this.shippingProfile.DimensionBasedList != undefined && this.shippingProfile.DimensionBasedList.length > 0) { this.getPackages(); this.getLengths(); this.shippingProfile.DimensionBasedList.forEach(element => { element.PackageTypeStr = this.packages.find(x => x.value == element.PackageType).label; element.DimensionsTypeStr = this.lengths.find(x => x.value == element.DimensionsType).label; }); } //this.getContinents(); this.getCountries(); }) } getPackages() { this.packages = [ { label: 'Box', value: 1 }, { label: 'Envelope', value: 2 }, { label: 'Soft Package', value: 3 } ] } getLengths() { this.lengths = [ { label: 'MM', value: 1 }, { label: 'CM', value: 2 }, { label: 'M', value: 3 } ] } //Get general settings getCurrencyFormat() { this.generalService.GetCurrencyFormat(this.companyId).subscribe(data => { if (data.CustomCurrency != null && data.CustomCurrency != undefined && data.CustomCurrency != '') { this.currencyType = data.CustomCurrency } else { this.currencyType = data.StoreCurrencyCode; } }) } // To hide previously opened list option pop ups onDropdownMenuClick(event: Event) { //setTimeout(function (event: Event) { var allDropDownList = document.getElementsByClassName('shippingOptionDropdown') for (var i = 0; i < allDropDownList.length; i++) { if (allDropDownList[i].classList.contains('show')) { allDropDownList[i].classList.remove("show"); if (allDropDownList[i].parentElement.classList.contains('open')) { allDropDownList[i].parentElement.classList.remove("open"); } } } // }, 1000) } editPriceBasedRate(id: number, rangeId: number) { const ref = this.dialogService.open(AddPriceBasedRateComponent, { header: 'Edit Price Based Rate', width: '45%', data: { priceBased: this.shippingProfile.PriceBased.find(x => x.Id == id), rangeId: rangeId } }); ref.onClose.subscribe(data => { this.shippingProfile.PriceBased.forEach(element => { if (element.Id == id) { element = data; } }); }); } editWeightBasedRate(id: number, rangeId: number) { const ref = this.dialogService.open(AddWeightBasedRateComponent, { header: 'Edit Weight Based Rate', width: '45%', data: { weightBased: this.shippingProfile.WeightBased.find(x => x.Id == id), rangeId: rangeId } }); ref.onClose.subscribe(data => { this.shippingProfile.WeightBased.forEach(element => { if (element.Id == id) { element = data; } }); }); } editDimensionBasedRate(id: number, rangeId: number) { const ref = this.dialogService.open(AddDimensionBasedRateComponent, { header: 'Edit Dimension Based Rate', width: '45%', data: { dimensionBased: this.shippingProfile.DimensionBased.find(x => x.Id == id), rangeId: rangeId } }); ref.onClose.subscribe(data => { this.shippingProfile.DimensionBased.forEach(element => { if (element.Id == id) { element = data; } }); }); } // copy range copyRange(id: number, rangeId: number, type: string) { this.generalService.CopyRange(this.companyId, id, rangeId, type, "COPY").subscribe(data => { this.selectedShippingFrom = null this.selectedShippingTo = null; this.GetShippingProfile(); }) } // delete range deleteRange(id: number, rangeId: number, type: string) { this.generalService.CopyRange(this.companyId, id, rangeId, type, "DELETE").subscribe(data => { this.selectedShippingFrom = null this.selectedShippingTo = null; this.GetShippingProfile(); }) } // For tree view - start dropdownEnabled = true; //items: TreeviewItem[]; values: string[]; config = TreeviewConfig.create({ hasAllCheckBox: true, hasFilter: true, hasCollapseExpand: true, decoupleChildFromParent: false, maxHeight: 400 }); onFilterChange(value: string) { console.log('filter:', value); } // For tree view - end }