netadsubscriptionreport.component.ts 8.7 KB
import { Component, OnInit, ViewChild ,HostListener} from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
//import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
import { NetAdSubscriptionReports } from '../reports/reportdatamodel';
import { ReportService } from '../reports/report.service';
import { GlobalService } from '../../shared/global';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { DatePipe } from '@angular/common';
import { BsDatepickerModule, BsDatepickerConfig } from 'ngx-bootstrap';
import { Http, Response } from '@angular/http';
import { LoadingService } from '../../shared/loading.service';
import { PagerComponent } from '../../shared/pager/pager.component';
import { ConfirmService } from '../../shared/confirm/confirm.service';
declare var $: any;

@Component({
    templateUrl: './netadsubscriptionreport.component.html'
})
export class NetAdSubscriptionReport implements OnInit {
    public lstNetAdSubscriptionReport: any;
    public TotalNetAdSubscription: any;
    public lstLicenceType: any;
    NetAdSubscriptionReportForm: FormGroup;
    NetAdSubscriptionReports: NetAdSubscriptionReports[];
    NetAdSubscriptionReport = new NetAdSubscriptionReports();
    numberOfNetAdSubscriptionReport: number;
    limit: number;
    page: number = 1;
    error;
    modalAlerts: string;
    divClass: string = '';
    topPos: string = '2000px';
    selectedRow: number = 0;
    datePipe: DatePipe = new DatePipe('en-US');
    bsValue1: Date = new Date();
    bsValue2: Date = new Date();
    selectedId: number = 0;
    modalRef: BsModalRef;
    date = new Date();
    previousdate = new Date();
    NoRecord: string;
    alerts: string;
    bsConfig: Partial<BsDatepickerConfig>;
    ExportingStart: boolean;
    buttonStatus: boolean;
    @ViewChild(PagerComponent) pagerComponent: PagerComponent;
    recordCount: number;
    pageNo: number;
    pageLength: number;
    // Declare height and width variables
    scrHeight:any;
    scrWidth:any;
    @HostListener('window:resize', ['$event'])
    getScreenSize(event?) {

    var $ua = navigator.userAgent;
    if (($ua.match(/(iPod|iPhone|iPad|android)/i))) {
      this.scrHeight = window.innerHeight-470;
    }
    else
    {
      this.scrHeight = window.innerHeight-405;
    }
    }
    constructor(private router: Router, private reportservice: ReportService, private fb: FormBuilder,
      private modalService: BsModalService, public global: GlobalService, 
      private _loadingService: LoadingService, private _confirmService: ConfirmService) {this.getScreenSize(); }

    ngOnInit(): void {
      this.ExportingStart = false;
      this.buttonStatus = false;
      this.bsConfig = Object.assign({}, { containerClass: 'theme-dark-blue' });
      this.NoRecord = this.global.NoRecords;
        let NetAdSubscriptionReport = new NetAdSubscriptionReports();
        this.previousdate.setDate(this.previousdate.getDate());
        this.NetAdSubscriptionReportForm = this.fb.group({
            sFromDate: [this.previousdate],
            sToDate: [this.date],            
            iLicenseTypeId: [0],
            iStartPrice: [0],
            iEndPrice: [''],
        });
        this.alerts = '';
        this._loadingService.ShowLoading("global-loading");
        this.GetLicenceType();
        this.pageNo = 1;
        this.pageLength = 10;
        this.pagerComponent = new PagerComponent(); 
        this._loadingService.HideLoading("global-loading");
        $('#fixed_hdr2').fxdHdrCol({
          fixedCols: 0,
          width: "100%",
          height: this.scrHeight,
          colModal: [
            { width: 180, align: 'center' },
            { width: 230, align: 'center' },
            { width: 150, align: 'Center' },
            { width: 150, align: 'Center' },
            { width: 350, align: 'Center' },
            { width: 200, align: 'Center' },
            { width: 200, align: 'Center' },
            { width: 120, align: 'center' },
            { width: 280, align: 'Center' },
            { width: 180, align: 'center' },
            { width: 200, align: 'center' },
            { width: 170, align: 'center' },
            { width: 80, align: 'center' },
            { width: 150, align: 'center' },
            { width: 150, align: 'center' },
            { width: 180, align: 'Center' },
          ],
          sort: true
        });
        if(document.getElementById("fixed_table_rc") != null){
            document.getElementById("fixed_table_rc").remove();
            var testScript = document.createElement("script");
            testScript.setAttribute("id", "fixed_table_rc");
            testScript.setAttribute("src", "../assets/scripts/fixed_table_rc.js");
            testScript.setAttribute("type", "text/javascript");
            document.body.appendChild(testScript);
        }
    }
  
    GetLicenceType() {
        this.reportservice.GetLicenceType().subscribe(st => { this.lstLicenceType = st; }, error => this.error = <any>error);
    }

    GetNetAdSummaryReport(evt: any) {
      this.alerts = '';
      this.global.compareTwoDates(this.NetAdSubscriptionReportForm.controls['sToDate'].value, this.NetAdSubscriptionReportForm.controls['sFromDate'].value);
      if (this.global.ValidationMsg != '') {
        this.alerts = this.global.ValidationMsg
      } 
      if (this.alerts == '') {
        this._loadingService.ShowLoading("global-loading");
        var tempArr = evt.split(',');
        this.pageNo = parseInt(tempArr[0]);
        var actulalength=this.pageLength;
        this.pageLength = parseInt(tempArr[1]);
        this._loadingService.ShowLoading("global-loading");
        this.NetAdSubscriptionReport = this.NetAdSubscriptionReportForm.value;
        var obj = this.NetAdSubscriptionReport;
        if (this.ExportingStart) {          
          this.reportservice.GetNetAdSummaryReport(obj, this.pageNo, this.pageLength).subscribe((NetAdSubscriptionReports: NetAdSubscriptionReports[]) => {
            //reset length after csvexport
            this.pageLength=actulalength;
            this.ExportService(NetAdSubscriptionReports); }, error => this.error = <any>error);
        }
        else {
          this.reportservice.GetNetAdSummaryReport(obj, this.pageNo, this.pageLength).subscribe((NetAdSubscriptionReports: NetAdSubscriptionReports[]) => { this.BindFormFields(NetAdSubscriptionReports); }, error => this.error = <any>error);
        }
      }
    }

    SearchRecords() {
      this.ExportingStart = false;
      this.GetNetAdSummaryReport('1, ' + this.pageLength);
    }
    
    BindFormFields(data) {
      this.recordCount = data.RecordCount;
      this.TotalNetAdSubscription=[];
      this.lstNetAdSubscriptionReport = data.NetAdSubscriptionList;
      var index = this.lstNetAdSubscriptionReport.length;
      var lastentry=this.lstNetAdSubscriptionReport[index-1];
      if(lastentry!=undefined && lastentry.LicenseType=='Total')
      {
        this.TotalNetAdSubscription=[{
          AccountType:lastentry.AccountType,
          ActiveSubscription:lastentry.ActiveSubscription,
          AccounInActiveSubscriptiontType:lastentry.InActiveSubscription,
          InstitutionName:lastentry.InstitutionName,
          LicenseType:lastentry.LicenseType,
          NetAdSubscription:lastentry.NetAdSubscription,
          RenewSubscription:lastentry.RenewSubscription,
        }]

      }
     
      this.lstNetAdSubscriptionReport.splice(index-1,1)

      if (this.lstNetAdSubscriptionReport.length > 0) {
        this.NoRecord = '';
        this.buttonStatus = true;
        this._loadingService.HideLoading("global-loading");
      }
      if (this.lstNetAdSubscriptionReport.length == 0) {
        this.NoRecord = this.global.NoRecords;
        this._loadingService.HideLoading("global-loading");
        this.buttonStatus = false;
      }
    }

    ExportEvent() {
      if (this.buttonStatus) {
        this.ExportingStart = true;
        this.GetNetAdSummaryReport('1, ' + this.recordCount);
        this.ExportingStart = false;
      }
    }

    ExportService(data: any) {
     
      for (let c = 0; c < this.global.RemoveColumns.length; c++) {
        for (let i = 0; i < data.NetAdSubscriptionList.length; i++) {
          delete data.NetAdSubscriptionList[i][this.global.RemoveColumns[c].toString()];
        } 
      }

      this._loadingService.HideLoading("global-loading");
      this.global.ExportToCSV(data.NetAdSubscriptionList, "NetAdSummaryReport-" + this.date.getDay() + "-" + this.date.getMonth() + "-" + this.date.getSeconds())
     
    }
}