Skip to content

Instantly share code, notes, and snippets.

@shantanuhashtaag
Last active March 16, 2018 08:30
Show Gist options
  • Save shantanuhashtaag/59eabab2410ce4e477a4c769306a98a1 to your computer and use it in GitHub Desktop.
Save shantanuhashtaag/59eabab2410ce4e477a4c769306a98a1 to your computer and use it in GitHub Desktop.
import {Component,ViewChild,OnInit} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {ActivatedRoute} from '@angular/router';
import {RouterModule,Routes,Router} from '@angular/router';
import { TollService } from "../../../services/toll.service"
import { FormBuilder, FormArray, FormGroup, Validators, FormControl } from "@angular/forms";
import { CommonModal } from "../../../common/modal/modal.component";
@Component({
templateUrl: "./register-branch.component.html"
})
export class TollRegisterBranchComponent implements OnInit {
filetype: any;
proofs: any;
activatedroute: any;
quantity: any;
validation: any;
stateCodes: any;
tolls: any;
company: any;
companyId: any;
branch: any;
token: any;
company2: any;
branches: any;
branchReqObj: any;
registertoken: any;
errorMessage: any;
items: any;
register_branch: FormGroup;
fileURL: any;
filetypeid: any;
contractor: any;
currentBranch : any;
@ViewChild(CommonModal) private modal: CommonModal;
formErrors = {
"branches": [{
"branchname": "",
"managerName": "",
"landlineNumber": "",
"mobilenumber": "",
"email": "",
"pannumber": "",
"GST": "",
"addressLine1": "",
"addressLine2": "",
"pincode": "",
"accountnumber": "",
"bankname": "",
"ifsccode": "",
"kilometer": "",
"tollboothlength": "",
"highwaynumber": "",
"area": "",
"stretch": "",
"feeeffectivedate": "",
"contactpersons": [{
"contactperson": "",
"mobilenumber": ""
}],
"primaryCardNumber": "",
"secondaryCardNumber": ""
}]
}
validationMessages = {
"branches": {
"branchname": {
"required": "Branch Name number required",
"pattern": "Enter a valid Branch Name"
},
"managerName": {
"required": "Bank Manager Name required",
"pattern": "Enter a valid Bank Manager Name"
},
"landlineNumber": {
"required": "Landline Number required",
"pattern": "Enter a valid Landline Number"
},
"mobilenumber": {
"required": "Mobile Number required",
"pattern": "Enter a valid Mobile Number"
},
"email": {
"required": "Email Id required",
"pattern": "Enter a valid email Id"
},
"pannumber": {
"required": "PAN Number required",
"pattern": "Enter a valid PAN Number"
},
"town": {
"required": "Town/City required",
"pattern": "Enter a valid Town/City"
},
"pincode": {
"required": "PIN Code is required",
"pattern": "Enter a valid PIN Code"
},
"accountnumber": {
"required": "Account Number is required",
"pattern": "Enter a valid Account Number"
},
"bankname": {
"required": "Bank Name is required",
"pattern": "Enter a valid Bank Name"
},
"ifsccode": {
"required": "IFSC Code is required",
"pattern": "Enter a valid IFSC Code"
},
"kilometer": {
"required": "Distance is required",
"pattern": "Enter a valid Distance"
},
"tollboothlength": {
"required": "Tollbooth Length is required",
"pattern": "Enter a valid Tollbooth Length"
},
"highwaynumber": {
"required": "Highway Number is required",
"pattern": "Enter a valid Highway Number"
},
"area": {
"required": "Area is required",
"pattern": "Enter a valid Area"
},
"stretch": {
"required": "Stretch is required",
"pattern": "Enter a valid Stretch"
},
"feeeffectivedate": {
"required": "feeeffectivedate is required",
"pattern": "Pattern Unknown"
},
"contactpersons": {
"contactperson": {
"required": "Enter Contact Person",
"pattern": "Enter a valid Contact Person Name"
},
"mobilenumber": {
"required": "Mobile Number required",
"pattern": "Enter valid 10 digit mobile number"
},
},
"primaryCardNumber": {
"required": "Card Number is required",
"pattern": "Enter a valid Card Number",
},
"secondaryCardNumber": {
"required": "Card Number is required",
"pattern": "Enter a valid Card Number",
},
"username": {
"required": "Username is required"
},
"password": {
"required": "Password is required",
"minlength": "Password needs to be 8 characters or longer"
},
"repassword": {
"required": "Repassword is required",
"minlength": "Password needs to be 8 characters or longer"
}
}
}
ngOnInit() {
this.register_branch = this.fb.group({
branches: this.fb.array([
this.initbranch()
])
});
this.register_branch.valueChanges
.subscribe(data => this.onValueChanged(data));
this.onValueChanged();
// this.register_branch.valueChanges.subscribe(data =>
// this.OnFieldChange(data)
// );
// this.OnFieldChange();
// console.log("abc", this.register_branch);
}
initbranch() {
return this.fb.group({
branchname: ['', [Validators.required, Validators.pattern('([a-zA-z ]{0,32})')]],
managerName: ['', [Validators.required, Validators.pattern('([a-zA-z ]{0,32})')]],
landlineNumber: ['', [Validators.required, Validators.pattern('[0-9]{10,11}')]],
mobilenumber: ['', [Validators.required, Validators.pattern('[0-9]{10}')]],
email : [''],
pannumber: ['', [Validators.required, Validators.pattern('[a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}')]],
addressLine1: [''],
addressLine2: [''],
town: ['', [Validators.required, Validators.pattern('([a-zA-z ]{0,32})')]],
pincode: ['', [Validators.required, Validators.pattern('[0-9]{6}')]],
stateCode: ['UT'],
countryCode: ['IN'],
kilometer: ['', [Validators.required, Validators.pattern('[0-9]{3}')]],
highwaynumber: ['', [Validators.required, Validators.pattern('[0-9]{0,3}')]],
area: ['', [Validators.required, Validators.pattern('([a-zA-z ]{0,32})')]],
stretch: [''],
tollboothlength: ['', [Validators.required, Validators.pattern('[0-9]{3}')]],
feeeffectivedate: [''],
duedate: [''],
contactpersons: this.fb.array([
this.initContactPersons()
]),
primaryCardNumber: ['', [Validators.required, Validators.pattern('[0-9]{16}')]],
secondaryCardNumber: ['', [Validators.required, Validators.pattern('[0-9]{16}')]],
username: ['', [Validators.required]],
password: ['', [Validators.required]],
repassword: ['', [Validators.required]]
});
}
formInvalid: any;
addBranch() {
this.onValueChanged();
if(this.register_branch.controls['branches']['controls'][this.currentBranch].valid){
const control = < FormArray > this.register_branch.controls['branches'];
control.push(this.initbranch());
this.fetchingValues();
}
console.log(this.branch);
console.log("branch req object data here------------->");
console.log(this.branchReqObj);
console.log(this.register_branch);
} // add branch closing.....
fetchingValues(){
this.branch = {
"toll": {
"tollCode": "",
"name": this.register_branch.controls['branches']['controls'][this.currentBranch].value.branchname,
// "feeEffectiveDate": "",
// "feeDueDate": "",
"location": {
"state": "TN",
"highwayType": "",
"stretch": this.register_branch.controls['branches']['controls'][this.currentBranch].value.stretch,
"tollableLength": this.register_branch.controls['branches']['controls'][this.currentBranch].value.tollboothlength,
"kiloMeter": this.register_branch.controls['branches']['controls'][this.currentBranch].value.kilometer,
"area": this.register_branch.controls['branches']['controls'][this.currentBranch].value.area,
"highwayNumber": this.register_branch.controls['branches']['controls'][this.currentBranch].value.highwaynumber
}
},
"businessLevel": {
"name": this.register_branch.controls['branches']['controls'][this.currentBranch].value.branchname,
"shortName": "",
"parentId": this.activatedroute,
"domainType": "TOLL",
"domainQualifiers": ["BRANCH"],
"contact": {
"applicable": true,
"details": {
"mobileNumber": this.register_branch.controls['branches']['controls'][this.currentBranch].value.mobilenumber,
"landlineNumber": this.register_branch.controls['branches']['controls'][this.currentBranch].value.landlineNumber,
"email": this.register_branch.controls['branches']['controls'][this.currentBranch].value.email,
"managerName": this.register_branch.controls['branches']['controls'][this.currentBranch].value.managerName,
"address": {
"addressLine1": this.register_branch.controls['branches']['controls'][this.currentBranch].value.addressLine1,
"addressLine2": this.register_branch.controls['branches']['controls'][this.currentBranch].value.addressLine2,
"addressLine3": "Aminjikarai",
"city": this.register_branch.controls['branches']['controls'][this.currentBranch].value.town,
"stateCode": this.register_branch.controls['branches']['controls'][this.currentBranch].value.stateCode,
"postalCode": this.register_branch.controls['branches']['controls'][this.currentBranch].value.pincode,
"countryCode": this.register_branch.controls['branches']['controls'][this.currentBranch].value.countryCode,
},
"communicationAddress": {
"addressLine1": "20, Govindan Street",
"addressLine2": "Ayyaavoo Colony",
"addressLine3": "Aminjikarai",
"city": "Chennai",
"stateCode": "TN",
"postalCode": "600029",
"countryCode": "IN"
},
"addressProof": {
"cmsId": "7687624876234"
},
"person": this.getPersonsObject()
}
},
"registration": {
"applicable": false
},
"settlement": {
"applicable": true,
"details": {
"useParentBankAccount": true,
"creditLimit": {
"value": "100"
}
}
},
"feeConfiguration": {
"applicable": true
},
"devices": {
"applicable": false
},
"login": {
"applicable": true,
"details": {
"primaryUser": {
"mobileNumber": this.register_branch.controls['branches']['controls'][this.currentBranch].value.mobilenumber,
"password": this.register_branch.controls['branches']['controls'][this.currentBranch].value.password,
"primaryCardNumber": this.register_branch.controls['branches']['controls'][this.currentBranch].value.primaryCardNumber,
"secondaryCardNumber": this.register_branch.controls['branches']['controls'][this.currentBranch].value.secondaryCardNumber,
"loginIdentifiers": {
"username": {
"identifier": this.register_branch.controls['branches']['controls'][this.currentBranch].value.username
}
}
}
}
}
}
}
this.branchReqObj.items.push(this.branch);
}
addcontactperson(index) {
const control = < FormArray > this.register_branch.controls['branches']['controls'][index]['controls']['contactpersons'];
control.push(this.initContactPersons());
console.log(this.register_branch);
//this.validateContactPersons();
console.log(this.register_branch);
}
initContactPersons() {
return this.fb.group({
// ---------------------forms fields on z level ------------------------
contactperson: ['', [Validators.required, Validators.pattern('([a-zA-z0-9 ]{0,32})')]],
mobilenumber: ['', [Validators.required, Validators.pattern('[0-9]{10,11}')]]
// ---------------------------------------------------------------------
});
}
validateContactPersons() {
let m = 0;
while (m < this.register_branch['controls'].branches['controls'][this.currentBranch]['controls'].contactpersons['controls'].length) {
let contactpersons = < FormArray > this.register_branch['controls'].branches['controls'][this.currentBranch]['controls'].contactpersons;
this.formErrors.branches[this.currentBranch].contactpersons = [];
let n = 0;
while (n < contactpersons.length) {
this.formErrors.branches[n].contactpersons.push({
contactperson: '',
mobilenumber: ''
});
let contactperson = < FormGroup > contactpersons.at(n);
console.log("here is the contactperson");
console.log(contactpersons);
for (let field in contactperson['controls']) {
let input = contactperson.get(field);
if (input.invalid && input.dirty) {
for (let error in input.errors) {
this.formErrors["branches"][this.currentBranch]['contactpersons'][n][field] = this.validationMessages.branches.contactpersons[field][error];
}
}
}
n++;
console.log(n);
}
m++;
}
}
onValueChanged(data ? : any) {
let branchesA = < FormArray > this.register_branch['controls'].branches;
this.formErrors.branches = [];
let n = 1;
while (n <= branchesA.length) {
this.formErrors.branches.push({
"branchname": "",
"managerName": "",
"landlineNumber": "",
"mobilenumber": "",
"email": "",
"pannumber": "",
"GST": "",
"addressLine1": "",
"addressLine2": "",
"pincode": "",
"accountnumber": "",
"bankname": "",
"ifsccode": "",
"kilometer": "",
"tollboothlength": "",
"highwaynumber": "",
"area": "",
"stretch": "",
"feeeffectivedate": "",
"contactpersons": [{
"contactperson": "",
"mobilenumber": ""
}],
"primaryCardNumber": "",
"secondaryCardNumber": ""
});
n++;
console.log(n);
}
let branch = < FormGroup > branchesA.at(this.currentBranch);
for (let field in branch['controls']) {
let input = branch.get(field);
if (input.invalid && input.dirty) {
for (let error in input.errors) {
this.formErrors.branches[this.currentBranch][field] = this.validationMessages.branches[field][error];
}
}
}
console.log("form here---------->");
console.log(this.register_branch);
console.log(this.formErrors);
//this.validateContactPersons();
}
onSubmit() {
if (this.register_branch['controls'].branches['controls'][this.currentBranch].valid) {
console.log("register_branch is valid-------------->");
this.fetchingValues();
this.branchRegister();
}
else{
console.log("XXXXXXXXX THE BRANCH IS NOT VALID XXXXXXXXX");
}
}
constructor(private tollService: TollService, private activateroute: ActivatedRoute, private route: Router, private fb: FormBuilder) {
this.companyId = localStorage.getItem('companyId');
this.activatedroute = this.activateroute.snapshot.params['id'] ? this.activateroute.snapshot.params['id'] : "";
this.proofs = {};
this.proofs.cheque = "";
this.proofs.pan = "";
this.validation = {};
this.validation.password = "password@123";
this.validation.tollcodeError = false;
this.validation.passwordError = false;
this.validation.serverError = false;
this.validation.validationError = false;
this.currentBranch = 0;
this.token = {
"authentication": ""
}
this.tolls = [];
this.tollService.jsonGetStatecodes().subscribe(response => {
this.stateCodes = response;
});
this.branch = {};
this.branchReqObj = {};
this.branchReqObj.items = [];
}
// constructor ends
getPersonsObject(){
let contactPersonFG = this.register_branch['controls'].branches['controls'][this.currentBranch]['controls'].contactpersons['controls'];
let persons = {};
for(let cp of contactPersonFG){
console.log(cp['controls']);
persons[cp['controls']['mobilenumber'].value] = {
name : cp['controls']['contactperson'].value
};
}
return persons;
}
branchRegister() {
console.log(this.branch);
this.tollService.apiRegisterBranch(this.branchReqObj)
.subscribe(response => {
console.log("Branch register response---------------->");
console.log(response);
this.token = response;
console.log("this.branchReqObj--=-=->");
console.log(this.branchReqObj);
this.route.navigate([
"./toll/register/list"
]);
}, error => {
console.log("Branch register eror---------------->");
console.log(error);
})
}
onChange(event) {
let files = event.srcElement.files;
console.log(files);
console.log(this.filetype);
let data = files[0];
let reader = new FileReader();
reader.onload = e => {
this.fileURL = e;
switch (this.filetype) {
case "PAN": this.proofs.pan = this.fileURL.target.result;
break;
case "GST": this.proofs.primaryRegistration = this.fileURL.target.result;
break;
case "cheque": this.proofs.cheque = this.fileURL.target.result;
break;
// case "ROC": this.proofs.secondaryRegistration = this.fileURL.target.result;
// break;
}
};
reader.readAsDataURL(data);
this.tollService.apiFileUpload(data).subscribe(response => {
console.log(response);
this.filetypeid = response;
switch (this.filetype) {
case "cheque": this.contractor.settlement.details.bankAccountProof.cmsId = this.filetypeid.fileId;
break;
case "GST": this.contractor.settlement.details.bankAccountProof.cmsId = this.filetypeid.fileId;
break;
// case "ROC": this.contractor.registration.details.secondaryRegistrationProof.cmsId = this.filetypeid.fileId;
// break;
case "PAN": this.contractor.settlement.details.PANProof.cmsId = this.filetypeid.fileId;
break;
}
});
}
updateStore(token, service) {
let data = this.tollService.getStoreData("TOLL");
data.branchAccesstoken = token;
this.tollService.setStoreData(data, "TOLL");
}
getTollBranch() {
let id = "4059e833-538e-4017-91ed-27bef311078f";
this.tollService.apiGetTollBranch(id)
.subscribe(response => {
console.log("Branch register response---------------->");
console.log(response);
// this.token = response;
// this.updateStore (this.token.authentication.accessToken, "TOLL");
}, error => {
console.log("Branch register eror---------------->");
console.log(error);
})
}
updateSelectedBranch(index){
if(this.register_branch.controls['branches']['controls'][this.currentBranch].status == "VALID"){
console.log(index);
let currentBranch = "branchPannel" + this.currentBranch;
let branch = document.getElementById(currentBranch);
console.log(branch)
this.currentBranch = index;
console.log("Previous Branch is valid, will open another branch......>");
let currentBranch2 = "branchPannel" + this.currentBranch;
let branch2 = document.getElementById(currentBranch2);
console.log(branch2)
}
else{
console.log("Previous Branch is not valid......>", index);
}
console.log(this.register_branch.controls['branches']['controls'][this.currentBranch]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment