addlicense.component.ts 15.8 KB
import { Component, OnInit, AfterViewInit, Input, Output, EventEmitter, Pipe, PipeTransform, TemplateRef } from '@angular/core';
import { LicenseService } from './license.service';
import { GlobalService } from '../../Shared/global';
import { Router, ActivatedRoute } from '@angular/router';
import { FormControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { License } from '../UserEntity/datamodel';
import { BsDatepickerModule } from 'ngx-bootstrap';
import { Http, Response } from '@angular/http';
import { DatePipe } from '@angular/common';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { ContenteditableModelDirective } from '../../shared/contenteditabledirective'

@Component({
    templateUrl: './addlicense.component.html' 
})

export class AddLicense implements OnInit {

    lstLicenceType: any;
    lstCountry: any;
    lstState: any;
    lstLicense: any;
    lstAccountType: any;
    lstEdition: any;
    lstSecurityQuestion: any;
    lstEditionLogins: any;
    totalLogins: number = 0;
    license: License;
    insertUpdateLicenseFrm: FormGroup;
    error: any;
    alerts: string;
    modalAlerts: string;
    divClass: string = '';
    topPos: string = '2000px';
    datePipe: DatePipe = new DatePipe('en-US');
    bsValue1: Date = new Date();
    bsValue2: Date = new Date();
    bsValue3: Date = new Date();
    modalRef: BsModalRef;
    editionLoginsText: string;  
    rowIndex: number = 0;
    ParamsObj: any;
    
    constructor(private licenseService: LicenseService, private globalService: GlobalService, private router: Router, private activeRoute: ActivatedRoute, private fb: FormBuilder, private modalService: BsModalService) { }
    
    ngOnInit(): void
    {
        this.divClass = 'col-sm-12';
        this.license = new License();
        this.alerts = '';
        this.insertUpdateLicenseFrm = this.fb.group({
            licenseId: [0],
            accountNumber: ['', Validators.required],
            accountTypeId: [1],
            productId: [''],
            licenseeFirstName: ['', Validators.required],
            licenseeLastName: ['', Validators.required],
            licenseTypeId: [1],
            institutionName: ['', Validators.required],
            address1: ['', Validators.required],
            address2: [''],
            city: ['', Validators.required],
            stateId: [1],
            countryId: [1],
            zip: ['', Validators.required],
            emailId: ['', Validators.required],
            phone: ['', Validators.required],
            editionLoginArr: this.fb.array([]),
            editionLogins: [''],
            totalLogins: [0],
            subscriptionPrice: ['', [Validators.required, Validators.pattern('[0-9.]*')]],
            subscriptionStartDate: ['', Validators.required],
            subscriptionEndDate: ['', Validators.required],  
            noOfImages: [0, [Validators.required, Validators.pattern('[0-9]*')]],
            masterSiteUrl: ['a', Validators.required],
            siteUrlFrom: [''],
            siteUrlTo: [''],
            login: ['a', Validators.required],
            password: ['a', Validators.required],
            securityQuestionId: [1],
            answer: ['a', Validators.required],
            testLicenseEditionId: [1],
            creatorId: [this.globalService.loggedInUser.Id],
            renew: [false],
            renewDate: [''],
            totalRenewals: [0],
            isActive: ['false']
        });
        this.GetLicenseType();
        this.GetCountry();
        this.GetState();
        this.GeAccountType();
        this.GeEditions();
        this.GeSecurityQuestions();
        this.license.LicenseId = 0;
        this.activeRoute.queryParams
        .subscribe(params => {
            if(params['Id'] != null){
                this.ParamsObj = params;
                this.license.LicenseId = +params['Id']; 
                this.rowIndex = +params['row'];  
                this.GetLicenseById();
            }
        });
    }

    openModal(template: TemplateRef<any>) {
        this.modalRef = this.modalService.show(template);
    }

    onChange(item: any, isChecked: boolean){
        if(this.license.LicenseTypeId == 3){
            if(isChecked){
                item.Login = 1;
            }
            else{
                item.Login = 0;
            }
            this.OnLoginBlur();
        }
    }

    GetLicenseType() {
        this.licenseService.GetLicenceType()
        .subscribe(x => { this.lstLicenceType = x; }, error => this.error = <any>error);
    }

    GeAccountType() {
        this.licenseService.GetAccountType()
        .subscribe(x => { this.lstAccountType = x; }, error => this.error = <any>error);
    }

    GeEditions() {
        this.licenseService.GetEditions()
        .subscribe(x => { 
            this.lstEdition = x; 
            this.lstEditionLogins = new Array();
            this.lstEdition.forEach(element => {
                this.lstEditionLogins.push({Id: element.Id, Title: element.Title, Login: 0});
            });
            this.insertUpdateLicenseFrm.setControl('editionLoginArr', this.fb.array(this.lstEditionLogins));
        }, error => this.error = <any>error);
    }

    GeSecurityQuestions() {
        this.licenseService.GetSecurityQuestions()
        .subscribe(x => { this.lstSecurityQuestion = x; }, error => this.error = <any>error);
    }

    GetCountry() {
        this.licenseService.GetCountry()
        .subscribe(y => { this.lstCountry = y; }, error => this.error = <any>error);
    }

    GetState() { 
        this.licenseService.GetState()
        .subscribe(st => { this.lstState = st; }, error => this.error = <any>error); 
    }

    GetLicenseById() { 
        if(this.license.LicenseId != 0)
        {
            this.licenseService.GetLicenseById(this.license.LicenseId)
            .subscribe(st => { 
                this.license = st; 
                if(this.license.LicenseId > 0){
                    this.license.LoginId = 'a';
                    this.license.Password = 'a';
                    this.license.SecurityQuestionId = 1;
                    this.license.Answer = 'a';
                }
                if(this.license.ProductKey == null){
                    this.license.ProductKey = '';
                }
                if(this.license.LicenseTypeId != 3){
                    this.license.MasterSiteUrl = 'a';
                }
                if(this.license.SiteUrlFrom == null){
                    this.license.SiteUrlFrom = '';
                }
                if(this.license.SiteUrlTo == null){
                    this.license.SiteUrlTo = '';
                }
                this.insertUpdateLicenseFrm.controls['licenseId'].setValue(this.license.LicenseId);
                this.insertUpdateLicenseFrm.controls['licenseTypeId'].setValue(this.license.LicenseTypeId);
                this.insertUpdateLicenseFrm.controls['accountNumber'].setValue(this.license.AccountNumber);
                this.insertUpdateLicenseFrm.controls['accountTypeId'].setValue(this.license.AccountTypeId);
                this.insertUpdateLicenseFrm.controls['productId'].setValue(this.license.ProductKey);
                this.insertUpdateLicenseFrm.controls['licenseeFirstName'].setValue(this.license.LicenseeFirstName);
                this.insertUpdateLicenseFrm.controls['licenseeLastName'].setValue(this.license.LicenseeLastName);
                this.insertUpdateLicenseFrm.controls['institutionName'].setValue(this.license.InstitutionName);
                this.insertUpdateLicenseFrm.controls['address1'].setValue(this.license.Address1);
                this.insertUpdateLicenseFrm.controls['address2'].setValue(this.license.Address2);
                this.insertUpdateLicenseFrm.controls['city'].setValue(this.license.City);
                this.insertUpdateLicenseFrm.controls['stateId'].setValue(this.license.StateId);
                this.insertUpdateLicenseFrm.controls['countryId'].setValue(this.license.CountryId);
                this.insertUpdateLicenseFrm.controls['zip'].setValue(this.license.Zip);
                this.insertUpdateLicenseFrm.controls['emailId'].setValue(this.license.EmailId);
                this.insertUpdateLicenseFrm.controls['phone'].setValue(this.license.Phone);
                this.insertUpdateLicenseFrm.controls['editionLogins'].setValue(this.license.EditionLogins);
                this.insertUpdateLicenseFrm.controls['totalLogins'].setValue(this.license.TotalLogins);
                this.insertUpdateLicenseFrm.controls['subscriptionPrice'].setValue(this.license.Price);
                this.insertUpdateLicenseFrm.controls['subscriptionStartDate'].setValue(this.datePipe.transform(this.license.SubscriptionStartDate, 'MM/dd/yyyy'));
                this.insertUpdateLicenseFrm.controls['subscriptionEndDate'].setValue(this.datePipe.transform(this.license.SubscriptionEndDate, 'MM/dd/yyyy'));
                this.insertUpdateLicenseFrm.controls['renewDate'].setValue(this.datePipe.transform(this.license.Renewdate, 'MM/dd/yyyy'));
                this.insertUpdateLicenseFrm.controls['noOfImages'].setValue(this.license.NoOfImages);
                this.insertUpdateLicenseFrm.controls['masterSiteUrl'].setValue(this.license.MasterSiteUrl);
                this.insertUpdateLicenseFrm.controls['siteUrlFrom'].setValue(this.license.SiteUrlFrom);
                this.insertUpdateLicenseFrm.controls['siteUrlTo'].setValue(this.license.SiteUrlTo);
                this.insertUpdateLicenseFrm.controls['login'].setValue(this.license.LoginId);
                this.insertUpdateLicenseFrm.controls['password'].setValue(this.license.Password);
                this.insertUpdateLicenseFrm.controls['securityQuestionId'].setValue(this.license.SecurityQuestionId);
                this.insertUpdateLicenseFrm.controls['answer'].setValue(this.license.Answer);
                this.insertUpdateLicenseFrm.controls['totalRenewals'].setValue(this.license.TotalRenewals);
                this.insertUpdateLicenseFrm.controls['renew'].setValue(false);
                //this.insertUpdateLicenseFrm.controls['creatorId'].setValue(this.license.UserId);
                if(this.license.IsActive){
                    this.insertUpdateLicenseFrm.controls['isActive'].setValue('true');
                }
                else{
                    this.insertUpdateLicenseFrm.controls['isActive'].setValue('false');
                }

                if(this.license.EditionLogins == null) return;
                var TempArr = this.license.EditionLogins.split('|');
                this.lstEditionLogins = new Array();
                this.lstEdition.forEach(element => {
                    var Check: boolean = false;
                    TempArr.forEach(elm => {
                        var TempInnerArr = elm.split('-');
                        if(TempInnerArr[0] == element.Id){
                            this.lstEditionLogins.push({Id: element.Id, Title: element.Title, Login: parseInt(TempInnerArr[1])});
                            Check = true;
                        }
                    });
                    if(!Check){
                        this.lstEditionLogins.push({Id: element.Id, Title: element.Title, Login: 0});
                    }
                });
                this.insertUpdateLicenseFrm.setControl('editionLoginArr', this.fb.array(this.lstEditionLogins));
            }, 
            error => this.error = <any>error); 
        }
    }

    AfterInsertData(data, template) {
        if (data.Status == "false") {
            this.alerts = "<span>License save unsuccessfull</span>";
        } else {
            this.modalAlerts = "<p>License saved successfully</p>";
            this.modalRef = this.modalService.show(template);
        }
    }

    AfterUpdateData(data, template) {
        if (data.Status == "false") {
            this.alerts = "<span>License update unsuccessfull</span>";
        } else {
            this.modalAlerts = "<p>License updated successfully</p>";
            this.modalRef = this.modalService.show(template);
        }
    }

    OnLoginBlur(){
        this.totalLogins = 0;
        this.insertUpdateLicenseFrm.controls['editionLoginArr'].value.forEach(element => {
            this.totalLogins += parseInt(element.Login);
        });
        this.insertUpdateLicenseFrm.controls['totalLogins'].setValue(this.totalLogins);
        this.license.TotalLogins = this.totalLogins;
    }

    LicenseTypeChanged(LicenseTypeId: number){
        if(LicenseTypeId == 5){
            this.insertUpdateLicenseFrm.controls['noOfImages'].setValue(0);
            this.insertUpdateLicenseFrm.controls['securityQuestionId'].setValue(1);
            this.insertUpdateLicenseFrm.controls['answer'].setValue('a');
        }
        if(LicenseTypeId == 3){
            this.insertUpdateLicenseFrm.controls['masterSiteUrl'].setValue('');
        }
        else{
            this.insertUpdateLicenseFrm.controls['masterSiteUrl'].setValue('a');
        }
        if(LicenseTypeId == 2){
            this.insertUpdateLicenseFrm.controls['login'].setValue('');
            this.insertUpdateLicenseFrm.controls['password'].setValue('');
            this.insertUpdateLicenseFrm.controls['securityQuestionId'].setValue(0);
            this.insertUpdateLicenseFrm.controls['answer'].setValue('');
        }
        else{
            this.insertUpdateLicenseFrm.controls['login'].setValue('a');
            this.insertUpdateLicenseFrm.controls['password'].setValue('a');
            this.insertUpdateLicenseFrm.controls['securityQuestionId'].setValue(1);
            this.insertUpdateLicenseFrm.controls['answer'].setValue('a');
        }

    }    

    TestLicenseEditionChanged(EditionId: number){
        this.insertUpdateLicenseFrm.controls['testLicenseEditionId'].setValue(EditionId);
    }

    InsertUpdateLicense(template: TemplateRef<any>){
        this.alerts = '';
        if(Date.parse(this.insertUpdateLicenseFrm.controls['subscriptionStartDate'].value) > 
            Date.parse(this.insertUpdateLicenseFrm.controls['subscriptionEndDate'].value)){
            this.alerts = '<span>Subscription start date must be lower than subscription end date</span><br/>';
        }
        if(Date.parse(this.insertUpdateLicenseFrm.controls['subscriptionEndDate'].value) > 
            Date.parse(this.insertUpdateLicenseFrm.controls['renewDate'].value)){
            this.alerts += '<span>Subscription end date must be lower than license renew date</span>';
        }
        if(this.alerts == ''){
            this.editionLoginsText = '';
            this.insertUpdateLicenseFrm.controls['editionLoginArr'].value.forEach(element => {
                this.editionLoginsText += element.Id.toString() + '-' + element.Login + '|';
            });
            if(this.editionLoginsText != ''){
                this.editionLoginsText = this.editionLoginsText.substr(0, this.editionLoginsText.length - 1);
            }
            this.insertUpdateLicenseFrm.controls['editionLogins'].setValue(this.editionLoginsText);
            var obj = this.insertUpdateLicenseFrm.value;
            if(obj.licenseId == 0){
                return this.licenseService.InsertLicense(obj)
                .subscribe(
                n => (this.AfterInsertData(n, template)),
                error => this.error = <any>error);
            }
            else{
                if(this.insertUpdateLicenseFrm.controls['renew'].value && this.insertUpdateLicenseFrm.controls['renewDate'].value == undefined){
                    this.alerts = 'Renew date is required';
                    return;
                }
                return this.licenseService.UpdateLicense(obj)
                .subscribe(
                n => (this.AfterUpdateData(n, template)),
                error => this.error = <any>error);
            }
        }
    }
    
    CancelAddEdit(){
        this.router.navigate(['/searchlicense'], 
        { queryParams: 
            this.ParamsObj
            , skipLocationChange: true 
        });   
    }

}