discountcodereport.component.ts 10.1 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 { DiscountCodeReports } from '../reports/reportdatamodel';
import { ReportService } from '../reports/report.service';
import { ManageDiscountCodeService } from '../managediscountcode/managediscountcode.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: './discountcodereport.component.html'
})

export class DiscountCodeReport implements OnInit {
    public lstDiscountCodeReport: any;
    public lstDiscountCode: any;
    DiscountCodeReportForm: FormGroup;
    DiscountCodeReports: DiscountCodeReports[];
    NewSubscription = new DiscountCodeReports();
    numberOfDiscountCodeReport: 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();
    bsConfig: Partial<BsDatepickerConfig>;
    NoRecord: string;
    alerts: string;
    ExportingStart: boolean;
    buttonStatus: boolean;
    @ViewChild(PagerComponent) pagerComponent: PagerComponent;
    recordCount: number;
    pageNo: number;
    pageLength: number;
	actualPageNo: number;
    actualPageLength: number;
	SearchField:any={
		sFromDate:new Date(),
		sToDate:new Date(),
		iDiscountCode:0,
		iAccountNumber:'',		
		sortColumn:'StartDate', 
		sortOrder:'asc',
		pageNo:1,
		pageLength:10	  
    };
    // 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-400;
    }
	 if(this.scrHeight<=360) this.scrHeight=360;
	
      $(".ft_container").css("height",this.scrHeight);
    }
    constructor(private router: Router, private reportservice: ReportService, 
      private manageDiscountCodeService: ManageDiscountCodeService, 
      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 DiscountCodeReport = new DiscountCodeReports();
        this.previousdate.setDate(this.previousdate.getDate());
        this.DiscountCodeReportForm = this.fb.group({
            sFromDate: [this.previousdate],
            sToDate: [this.date],
            iDiscountCode: [0],
            iAccountNumber: []
        });
        this.alerts = '';
        this._loadingService.ShowLoading("global-loading");
        //this.GetDiscountCode();
        this.SearchDiscountCodes();
        this._loadingService.HideLoading("global-loading");
        //this.GetSubscriptionReport();
        $('#fixed_hdr2').fxdHdrCol({
            fixedCols: 0,
            width: "100%",
            height: this.scrHeight,
            colModal: [
                { width: 300, align: 'center' },
                { width: 230, align: 'center' },
                { width: 250, align: 'Center' },
                { width: 250, align: 'Center' },
                { width: 350, align: 'Center' },
                { width: 400, align: 'Center' }             
            ],
            sort: true
        });
        this.pageNo = 1;
        this.pageLength = 10;
        this.NoRecord = this.global.NoRecords;
        this.recordCount = 0;
        this.pagerComponent = new PagerComponent();
            
        var thisObject=this;//class object
		$(".ft_rwrapper table thead tr th").on("click",function(event){  
				
		  if(event.currentTarget.id!=undefined && event.currentTarget.id!="" && event.currentTarget.id!=null)
		  {
			var fieldName=event.currentTarget.id;
			var sortType=''
			var isAscSort = $(".ft_rwrapper table thead tr #"+fieldName).hasClass('fx_sort_asc'); 
			$(".ft_rwrapper table thead tr th").removeClass('fx_sort_asc fx_sort_desc'); 
		  
			if (isAscSort) { 
			  $(".ft_rwrapper table thead tr #"+fieldName).addClass('fx_sort_desc').removeClass('fx_sort_asc'); 
			  sortType="desc";
			} else { 
			  $(".ft_rwrapper table thead tr #"+fieldName).addClass('fx_sort_asc').removeClass('fx_sort_desc'); 
			  sortType="asc";
			}
	  
			thisObject.SortTableRecords(fieldName,sortType);  

		  }
		 
		});
        
    }
	
	FromDateChange(dateValue: any){
	this.alerts = '';
		this.global.compareTwoDates(this.DiscountCodeReportForm.controls['sToDate'].value, dateValue._bsValue);
      if (this.global.ValidationMsg != '') {
        this.alerts = this.global.ValidationMsg;
      }		
  }
  
  ToDateChange(dateValue: any){
	this.alerts = '';	
	this.global.compareTwoDates(dateValue._bsValue, this.DiscountCodeReportForm.controls['sFromDate'].value);
	  if (this.global.ValidationMsg != '') {
		this.alerts = this.global.ValidationMsg;
	  }		
  }
	
  SortTableRecords(fieldName:string,sortType:string) {  
	this.SearchField.sortColumn=fieldName;
	this.SearchField.sortOrder=sortType;
	
	this.GetSubscriptionReportList();
   }

    GetDiscountCode() {
        this.reportservice.GetDiscountCodes().subscribe(st => { this.lstDiscountCode = st; }, error => this.error = <any>error);
    }

    SearchDiscountCodes() {
      this.manageDiscountCodeService.GetDiscountCodes(
      {
          discountCode: '',
          startDate: '',
          endDate: '',
          pageNo: 1, 
          pageLength: 10000
      })
      .subscribe(x => { this.lstDiscountCode = x.DiscountCodeList; }, error => this.error = error);
  }

    GetDiscountReport(evt: any) {
	  this.actualPageNo = this.pageNo;
      this.actualPageLength =this.pageLength;	
	  
	  var tempArr = evt.split(',');     
	  this.pageNo = parseInt(tempArr[0]);
      this.pageLength = parseInt(tempArr[1]);
      
	  this.SearchField.pageNo=this.pageNo;
	  this.SearchField.pageLength=this.pageLength;
		
	  this.GetSubscriptionReportList();   
    }
	
	GetSubscriptionReportList() {
       this._loadingService.ShowLoading("global-loading");
	   $(".ft_rwrapper table thead tr th").removeClass('fx_sort_asc fx_sort_desc'); 
	  
		if (this.SearchField.sortOrder=='asc') { 
		  $(".ft_rwrapper table thead tr #"+this.SearchField.sortColumn).addClass('fx_sort_asc').removeClass('fx_sort_desc');
		} else 
		{ 
		  $(".ft_rwrapper table thead tr #"+this.SearchField.sortColumn).addClass('fx_sort_desc').removeClass('fx_sort_asc'); 
		}
	  if (this.ExportingStart) {
        this.reportservice.GetDiscountReport(this.SearchField).subscribe((DiscountCodeReports: DiscountCodeReports[]) => {
        //reset length after csvexport
        this.pageNo = this.actualPageNo;
		this.pageLength=this.actualPageLength;
			
		this.SearchField.pageNo=this.pageNo;
	    this.SearchField.pageLength=this.pageLength;
        this.ExportService(DiscountCodeReports); }, error => this.error = <any>error);      
      }
      else {
        this.reportservice.GetDiscountReport(this.SearchField).subscribe((DiscountCodeReports: DiscountCodeReports[]) => { this.BindFormFields(DiscountCodeReports); }, error => this.error = <any>error);
      }
        
      this.ExportingStart = false;
   }
    SearchRecords() {
	  this.ExportingStart = false;
	  this.pageNo=1;
	  this.SearchField.sFromDate=this.DiscountCodeReportForm.controls['sFromDate'].value;
	  this.SearchField.sToDate=this.DiscountCodeReportForm.controls['sToDate'].value;
	  this.SearchField.iDiscountCode=this.DiscountCodeReportForm.controls['iDiscountCode'].value;
	  this.SearchField.iAccountNumber=this.DiscountCodeReportForm.controls['iAccountNumber'].value;
	 
	  this.SearchField.pageNo=this.pageNo;
	  
	  this.GetSubscriptionReportList();
    }

    BindFormFields(data) {
      this.recordCount = data.RecordCount;
      this.lstDiscountCodeReport = data.DiscountReportList;
      this.limit = this.lstDiscountCodeReport.length;
      if (this.lstDiscountCodeReport.length > 0) {
        this.NoRecord = '';
        this.buttonStatus = true;
        this._loadingService.HideLoading("global-loading");
      }
      if (this.lstDiscountCodeReport.length == 0) {
        this.NoRecord = this.global.NoRecords;
        this._loadingService.HideLoading("global-loading");
        this.buttonStatus = false;
      }
    }

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

    ExportService(data: any) {
		
	 for (let i = 0; i < data.DiscountReportList.length; i++) {
		data.DiscountReportList[i]['StartDate']= this.datePipe.transform(data.DiscountReportList[i]['StartDate'], 'MM/dd/yyyy');
		data.DiscountReportList[i]['EndDate']= this.datePipe.transform(data.DiscountReportList[i]['EndDate'], 'MM/dd/yyyy');		
	   }
	 
	  var reportname=+ this.date.getDay() + "-" + (this.date.getMonth()+1) + "-" + this.date.getSeconds()  
      this.global.ExportToCSV(data.DiscountReportList, "DiscountReport-"+reportname);
     
	 this._loadingService.HideLoading("global-loading");
    }
}