import { Component, OnInit, NgZone, ElementRef } from '@angular/core'; import { ActivatedRoute, Router } from "@angular/router"; import { CustomerAdministration } from "../../../../../models/admin/company/customer-administration/customer-administration"; import { CustomerAdministrationService } from '../../../../../services/admin/company/customer-administration/customer-administration.service'; import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; import { CommonService } from '../../../../../services/common.service'; import { CommonlistRequest, CommonListResponse } from '../../../../../models/shared/commonlist'; import { Subject } from 'rxjs'; import { CustomValidator } from '../../../../../core/custom-validator'; @Component({ selector: 'app-customer-administration-add', templateUrl: './customer-administration-add.component.html', styleUrls: ['./customer-administration-add.component.css'] }) export class CustomerAdministrationAddComponent implements OnInit { modelCustomerAdministration = new CustomerAdministration(); commonlistRequest = new CommonlistRequest(); commonListResponse: CommonListResponse[]; tableNameForDropdown: string; lstBuildingPricingSegment: any[]; lstComponentPricingSegment: any[]; lstBuildingGeoRegionGroup: any[]; lstComponentGeoRegionGroup: any[]; lstPermissionGroup: any[]; CustomerAddForm: FormGroup; CustomerAPIResponse: any; data: any; _paramId: number = 0; companyid: number = 1; _catId: number = 0; dtOptions: any = {} dtTrigger: Subject = new Subject(); constructor( private formBuilder: FormBuilder, private route: ActivatedRoute, private router: Router, private service: CustomerAdministrationService, private common: CommonService, private ngZone: NgZone, private elementRef: ElementRef ) { } ngOnInit(): void { this.dtOptions = this.common.dataTableOptionsForSN(false, false, false, true, true, false, true); this._paramId = Number(this.route.snapshot.params.id); this._catId = Number(this.route.snapshot.params.catid); this.setFormBuilder(); this.getCustomerList(); this.CommonListfunction(); this.lstPermissionGroupfunction(); } getCustomerList() { if (this._paramId == 0) { this.setAddForm(); } else { this.service.getCustomerById(this._paramId, data => { this.CustomerAPIResponse = data; this.data = this.CustomerAPIResponse.objResponse.responseContent; this.setEditForm(); }, (error) => { console.error('Error: ' + error); return error(error); }); } } CommonListfunction() { this.tableNameForDropdown = 'CustomerAdmin'; this.service.GetCommonMultiDropDownList(this.tableNameForDropdown, data => { this.CustomerAPIResponse = data.objResponse.responseContent; this.lstBuildingPricingSegment = this.CustomerAPIResponse.filter(x => x.Type == "PricingBuildings"); this.lstComponentPricingSegment = this.CustomerAPIResponse.filter(x => x.Type == "PricingComponents"); this.lstBuildingGeoRegionGroup = this.CustomerAPIResponse.filter(x => x.Type == "ShippingBuildings"); this.lstComponentGeoRegionGroup = this.CustomerAPIResponse.filter(x => x.Type == "ShippingComponents"); }, (error) => { console.error('Error: ' + error); return error(error); }); } lstPermissionGroupfunction() { this.commonlistRequest.IsFromCommon = true; this.commonlistRequest.strCategory = 'PermissionGroup'; this.common.CommonList(this.commonlistRequest, data => { this.commonListResponse = data.objResponse.responseContent; this.lstPermissionGroup = this.commonListResponse.filter(x => x.Category == "PermissionGroup"); }, (error) => { console.error('Error: ' + error); return error(error); }); } navigateTo(url) { this.router.navigate([url]); } ngOnDestroy(): void { // Do not forget to unsubscribe the event this.dtTrigger.unsubscribe(); } setFormBuilder() { this.CustomerAddForm = this.formBuilder.group({ CompanyId:[''], CustomerId: [''], CustomerNo: ['', [Validators.required, CustomValidator.noWhitespaceValidator]], CustomerName: ['', [Validators.required, CustomValidator.noWhitespaceValidator, Validators.pattern('^[a-zA-Z \-\']+')]], AdministratorFName: ['', [Validators.required, CustomValidator.noWhitespaceValidator, Validators.pattern('^[a-zA-Z \-\']+')]], AdministratorLName: ['', [Validators.required, CustomValidator.noWhitespaceValidator, Validators.pattern('^[a-zA-Z \-\']+')]], AdministratorEmail: ['', [Validators.required, Validators.email, Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$')]], BuildingsPriceSegment: ['', Validators.required], BuildingsPriceSegmentName: [''], ComponentPricingSegment: ['', Validators.required], PermissionGroup: ['', Validators.required], PriceMultiplier: ['', [Validators.required, Validators.min(0), Validators.pattern(/^[0-9]+(\.[0-9]{1,3})?$/)]], FinalPriceMultiplier: ['', [Validators.required, Validators.min(0), Validators.pattern(/^[0-9]+(\.[0-9]{1,3})?$/)]], FinalFreightMultiplier: ['', [Validators.required, Validators.min(0), Validators.pattern(/^[0-9]+(\.[0-9]{1,3})?$/)]], ComponentsPriceMultiplier: ['', [Validators.required, Validators.min(0), Validators.pattern(/^[0-9]+(\.[0-9]{1,3})?$/)]], ComponentsFinalPriceMultiplier: ['', [Validators.required, Validators.min(0), Validators.pattern(/^[0-9]+(\.[0-9]{1,3})?$/)]], ComponentsFinalFreightMultiplier: ['', [Validators.required, Validators.min(0), Validators.pattern(/^[0-9]+(\.[0-9]{1,3})?$/)]], ShippingZoneGroupId: ['', Validators.required], ComponentsShippingZoneGroupId: ['', Validators.required], IsDefaultCustomer: [false], IsActive: [false], IsQuickQuoteAvailable: [false], IsPrivateLabelBuilder: [false], EnforceSignatureRule: [false], }); } setAddForm() { this.CustomerAddForm.setValue({ CompanyId: this.companyid, CustomerId: 0, CustomerNo: '', CustomerName: '', AdministratorEmail: '', AdministratorFName: '', AdministratorLName: '', BuildingsPriceSegment: '', BuildingsPriceSegmentName: '', ComponentPricingSegment: '', PermissionGroup: '', PriceMultiplier: 0, FinalPriceMultiplier: 0, FinalFreightMultiplier: 0, ComponentsPriceMultiplier: 0, ComponentsFinalPriceMultiplier: 0, ComponentsFinalFreightMultiplier: 0, ShippingZoneGroupId: '', ComponentsShippingZoneGroupId: '', IsDefaultCustomer: false, IsActive: false, IsQuickQuoteAvailable: false, IsPrivateLabelBuilder: false, EnforceSignatureRule: false, }); } setEditForm() { this.CustomerAddForm.setValue({ CompanyId: this.companyid, CustomerId: this.data.CustomerId, CustomerNo: this.data.CustomerNo == null ? "" : this.data.CustomerNo, CustomerName: this.data.CustomerName == null ? "" : this.data.CustomerName, AdministratorEmail: this.data.AdministratorEmail == null ? "" : this.data.AdministratorEmail, AdministratorFName: this.data.AdministratorFName == null ? "" : this.data.AdministratorFName, AdministratorLName: this.data.AdministratorLName == null ? "" : this.data.AdministratorLName, BuildingsPriceSegment: this.data.BuildingsPriceSegment, BuildingsPriceSegmentName: this.data.BuildingsPriceSegmentName, ComponentPricingSegment: this.data.ComponentPricingSegment, PermissionGroup: this.data.PermissionGroup, PriceMultiplier: this.data.PriceMultiplier, FinalPriceMultiplier: this.data.FinalPriceMultiplier, FinalFreightMultiplier: this.data.FinalFreightMultiplier, ComponentsPriceMultiplier: this.data.ComponentsPriceMultiplier, ComponentsFinalPriceMultiplier: this.data.ComponentsFinalPriceMultiplier, ComponentsFinalFreightMultiplier: this.data.ComponentsFinalFreightMultiplier, ShippingZoneGroupId: this.data.ShippingZoneGroupId, ComponentsShippingZoneGroupId: this.data.ComponentsShippingZoneGroupId, IsDefaultCustomer: this.data.IsDefaultCustomer == true ? 1 : 0, IsActive: this.data.IsActive == true ? 1 : 0, IsQuickQuoteAvailable: this.data.IsQuickQuoteAvailable == true ? 1 : 0, IsPrivateLabelBuilder: this.data.IsPrivateLabelBuilder == true ? 1 : 0, EnforceSignatureRule: this.data.EnforceSignatureRule == true ? 1 : 0, }); } submitform() { if (this.CustomerAddForm.valid) { this.modelCustomerAdministration = this.CustomerAddForm.value; this.setModelBeforeSubmit(); this.service.saveCustomer(this.modelCustomerAdministration, data => { this.CustomerAPIResponse = data; if (this.CustomerAPIResponse.objResponse.statusCode == "400") { this.common.showError("Customer name Or Number already exist", "Validation Alert!"); } else if (this.CustomerAPIResponse.objResponse.statusCode == "200") { this.ngZone.run(() => this.navigateTo('/admin/admin-company/customer-administration-list')); this.common.showSuccess("Customer changes applied successfully.", "Success") } else { this.common.showError("Transaction not completed.", "Transaction Fail") } }, (error) => { console.error('Error: ' + error); return error(error); }); } else { this.common.validateAllFormFields(this.CustomerAddForm); this.common.validationfocus(this.CustomerAddForm, this.elementRef); this.common.showError("Please fill required fields.", "Validation Alert!") } } setModelBeforeSubmit() { this.modelCustomerAdministration.CompanyId = Number(this.companyid); this.modelCustomerAdministration.CustomerId = Number(this.modelCustomerAdministration.CustomerId); this.modelCustomerAdministration.CustomerNo = String(this.modelCustomerAdministration.CustomerNo); this.modelCustomerAdministration.CustomerName = String(this.modelCustomerAdministration.CustomerName); this.modelCustomerAdministration.AdministratorEmail = String(this.modelCustomerAdministration.AdministratorEmail); this.modelCustomerAdministration.AdministratorFName = String(this.modelCustomerAdministration.AdministratorFName); this.modelCustomerAdministration.AdministratorLName = String(this.modelCustomerAdministration.AdministratorLName); this.modelCustomerAdministration.BuildingsPriceSegment = Number(this.modelCustomerAdministration.BuildingsPriceSegment); this.modelCustomerAdministration.BuildingsPriceSegmentName = Number(this.modelCustomerAdministration.BuildingsPriceSegmentName); this.modelCustomerAdministration.ComponentPricingSegment = Number(this.modelCustomerAdministration.ComponentPricingSegment); this.modelCustomerAdministration.PermissionGroup = Number(this.modelCustomerAdministration.PermissionGroup); this.modelCustomerAdministration.PriceMultiplier = Number(this.modelCustomerAdministration.PriceMultiplier); this.modelCustomerAdministration.FinalPriceMultiplier = Number(this.modelCustomerAdministration.FinalPriceMultiplier); this.modelCustomerAdministration.FinalFreightMultiplier = Number(this.modelCustomerAdministration.FinalFreightMultiplier); this.modelCustomerAdministration.ComponentsPriceMultiplier = Number(this.modelCustomerAdministration.ComponentsPriceMultiplier); this.modelCustomerAdministration.ComponentsFinalPriceMultiplier = Number(this.modelCustomerAdministration.ComponentsFinalPriceMultiplier); this.modelCustomerAdministration.ComponentsFinalFreightMultiplier = Number(this.modelCustomerAdministration.ComponentsFinalFreightMultiplier); this.modelCustomerAdministration.ShippingZoneGroupId = Number(this.modelCustomerAdministration.ShippingZoneGroupId); this.modelCustomerAdministration.ComponentsShippingZoneGroupId = Number(this.modelCustomerAdministration.ComponentsShippingZoneGroupId); this.modelCustomerAdministration.IsDefaultCustomer = Boolean(Number(this.modelCustomerAdministration.IsDefaultCustomer)); this.modelCustomerAdministration.IsActive = Boolean(Number(this.modelCustomerAdministration.IsActive)); this.modelCustomerAdministration.IsQuickQuoteAvailable = Boolean(Number(this.modelCustomerAdministration.IsQuickQuoteAvailable)); this.modelCustomerAdministration.IsPrivateLabelBuilder = Boolean(Number(this.modelCustomerAdministration.IsPrivateLabelBuilder)); this.modelCustomerAdministration.EnforceSignatureRule = Boolean(Number(this.modelCustomerAdministration.EnforceSignatureRule)); } displayFieldCss(field: string, ctrlType: string) { if (ctrlType.toLowerCase() == "txt") { return { 'plain-valid': this.CustomerAddForm.get(field).valid, 'is-invalid': (this.CustomerAddForm.get(field).invalid && (this.CustomerAddForm.get(field).dirty || this.CustomerAddForm.get(field).touched)) }; } else { return { 'plain-valid': this.CustomerAddForm.get(field).valid, 'plain-invalid': (this.CustomerAddForm.get(field).invalid && (this.CustomerAddForm.get(field).dirty || this.CustomerAddForm.get(field).touched)) }; } } }