Advertisement
Guest User

Untitled

a guest
Mar 12th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit, ViewChild } from '@angular/core';
  2. import { BecPeriod } from '../dataModel/becPeriod';
  3. import { AppService } from '../app.service';
  4. import { SelectionParamsService } from '../sharedComponents/selection-params/selection-params.service';
  5. import { Ratio } from '../dataModel/ratio';
  6. import { Category } from '../dataModel/category';
  7. import { Group } from '../dataModel/group';
  8. import { Subgroup } from '../dataModel/subgroup';
  9.  
  10. import * as _ from 'lodash';
  11.  
  12. import { FacingService } from '../facing/facing.service';
  13. import { RatioService } from './ratio.service';
  14. import { Table } from 'primeng/table';
  15. import { LaunchType } from '../dataModel/launchType';
  16. import { ShopSize } from '../dataModel/shopSize';
  17. import { MatDialog } from '@angular/material';
  18. import { RatioBulkComponent } from './ratio-bulk/ratio-bulk.component';
  19. import { FormControl } from '@angular/forms';
  20.  
  21. @Component({
  22.     selector: 'app-ratio',
  23.     templateUrl: './ratio.component.html',
  24.     styleUrls: ['./ratio.component.sass']
  25. })
  26. export class RatioComponent implements OnInit {
  27.  
  28.     selectionExpandState: boolean = false;
  29.     allSelected: boolean = false;
  30.     isExportInProgress: boolean = false;
  31.     isSaveInProgress: boolean = false;
  32.     aChangeWasMade: boolean = false;
  33.     excelPath: string;
  34.     readonly debounceTime: number = 1500;
  35.     areTableFiltersChanged: boolean = false;
  36.     searchTableFormControl = new FormControl('');
  37.  
  38.     selectedRows: Ratio[];
  39.     ratioIdsToUpdate: string[];
  40.     ratios: Ratio[] = [];
  41.  
  42.     becPeriods: BecPeriod[];
  43.     selectedBecPeriod: BecPeriod[];
  44.  
  45.     private categories: Category[];
  46.     private selectedCategories: Category[];
  47.  
  48.     groups: Group[];
  49.     selectedGroups: Group[];
  50.  
  51.     subgroups: Subgroup[];
  52.     selectedSubgroups: Subgroup[];
  53.  
  54.     launchTypes: LaunchType[]
  55.     selectedLaunchTypes: LaunchType[]
  56.  
  57.     shopSizes: ShopSize[]
  58.     selectedShopSizes: ShopSize[]
  59.  
  60.     becDropdownSettings = this._selectionParamsService.createSingleSelectOptions('BEC Period', 'becId', 'becDescription');
  61.  
  62.     cols = [
  63.         { field: 'selection', header: '', style: { 'width': '20px' }, class: "selection-checkbox" },
  64.         { field: 'deCategory', header: 'Category', style: { 'width': '100px' } },
  65.         { field: 'deGroup', header: 'Group', style: { 'width': '100px' } },
  66.         { field: 'deSubgroup', header: 'Subgroup', style: { 'width': '100px' } },
  67.         { field: 'deLaunchType', header: 'Launch Size', style: { 'width': '100px' } },
  68.         { field: 'deShopSize', header: 'Shop Size', style: { 'width': '100px' } },
  69.         { field: 'ratio', header: 'Ratio', style: { 'width': '100px' } },
  70.     ];
  71.  
  72.     constructor(
  73.         private _AppService: AppService,
  74.         private _selectionParamsService: SelectionParamsService,
  75.         private _RatioService: RatioService,
  76.         private _FacingService: FacingService,
  77.         public dialog: MatDialog
  78.     ) { }
  79.  
  80.     ngOnInit() {
  81.  
  82.         this._AppService.getBecPeriods().subscribe(
  83.             data => this.becPeriods = data,
  84.             error => alert(JSON.stringify(error)),
  85.             () => {
  86.                 this.selectedBecPeriod = [this._selectionParamsService.findCurrentBecPeriod(this.becPeriods)];
  87.  
  88.                 this.searchRatios();
  89.             });
  90.     }
  91.  
  92.     searchRatios() {
  93.         this._RatioService.getRatios(this.selectedBecPeriod[0].becId).subscribe(
  94.             data => this.ratios = data,
  95.             error => alert(JSON.stringify(error)),
  96.             () => {
  97.  
  98.                 _.forEach(this.ratios, function (o) {
  99.                     o.deCategory = o.becCategory.description;
  100.                     o.deGroup = o.becGroup.description;
  101.                     o.deSubgroup = o.becSubgroup.description;
  102.                     o.deLaunchType = o.becLaunchType.description;
  103.                     o.deShopSize = o.becShopSize.description;
  104.                 });
  105.  
  106.                 this.categories = _.uniqBy(_.map(this.ratios, 'becCategory'), 'description');
  107.                 this.selectedCategories = this.categories;
  108.  
  109.                 this.groups = _.uniqBy(_.map(this.ratios, 'becGroup'), 'description');
  110.                 this.selectedGroups = this.groups;
  111.  
  112.                 this.subgroups = _.uniqBy(_.map(this.ratios, 'becSubgroup'), 'description');
  113.                 this.selectedSubgroups = this.subgroups;
  114.  
  115.                 this.launchTypes = _.uniqBy(_.map(this.ratios, 'becLaunchType'), 'description');
  116.                 this.selectedLaunchTypes = this.launchTypes;
  117.  
  118.                 this.shopSizes = _.uniqBy(_.map(this.ratios, 'becShopSize'), 'description');
  119.                 this.selectedShopSizes = this.shopSizes;
  120.             });
  121.     }
  122.  
  123.     searchTable(tableRef: Table) {
  124.         tableRef.filterGlobal(this.searchTableFormControl.value, 'contains')
  125.     }
  126.  
  127.     clearFilters(tableRef: Table) {
  128.         tableRef.reset();
  129.         this.searchTableFormControl.setValue("");
  130.     }
  131.  
  132.     onEdit(id): void {
  133.         this.aChangeWasMade = true;
  134.  
  135.         this.addForSave(id);
  136.     }
  137.  
  138.     addForSave(idToSave) {
  139.  
  140.         if (_.isNil(this.ratioIdsToUpdate))
  141.             this.ratioIdsToUpdate = [];
  142.  
  143.         if (this.allreadyUpForSave(idToSave) == false)
  144.             this.ratioIdsToUpdate.push(idToSave);
  145.     }
  146.  
  147.     allreadyUpForSave(idToSave) {
  148.         return (_.findIndex(this.ratioIdsToUpdate, function (id) { return id == idToSave; }) >= 0)
  149.     }
  150.  
  151.     disableBulkButton(): boolean {
  152.         return _.isEmpty(this.selectedRows) || this.selectedRows.length < 2;
  153.     }
  154.  
  155.     bulkSetSize(): void {
  156.         let dialogRef = this.dialog.open(RatioBulkComponent, {
  157.             width: '500px'
  158.         });
  159.  
  160.         dialogRef.afterClosed().subscribe(result => {
  161.  
  162.             if (_.isNil(result)) return;
  163.  
  164.             this.aChangeWasMade = true;
  165.  
  166.             _.forEach(this.selectedRows, function (o) {
  167.                 _.forEach(this.ratios, function (i) {
  168.                     if (i.id == o.id) {
  169.  
  170.                         i.ratio = result;
  171.                         i.isChanged = true;
  172.                         this.onEdit(i.id);
  173.                         return false;
  174.                     }
  175.                 }.bind(this));
  176.             }.bind(this));
  177.         });
  178.     }
  179.  
  180.     filterColumn(tableRef: Table, event, colField) {
  181.         var values: string[] = _.map(event.value, 'description');
  182.         tableRef.filter(values, colField, 'in');
  183.     }
  184.  
  185.     disableSaveButton(): boolean {
  186.  
  187.         if (this.aChangeWasMade == false || this.isExportInProgress || this.isSaveInProgress)
  188.             return true;
  189.         return false;
  190.     }
  191.  
  192.     saveButtonStateInfo(): string {
  193.  
  194.         if (_.isNil(this.ratios) || _.isEmpty(this.ratios))
  195.             return "No item to save";
  196.         else if (this.isSaveInProgress)
  197.             return "Saving.."
  198.         return "Save";
  199.     }
  200.  
  201.     saveRatios(): void {
  202.  
  203.         var toSaveList = [];
  204.         _.forEach(this.ratios, function (ratio) {
  205.             if (this.allreadyUpForSave(ratio.id))
  206.                 toSaveList.push(ratio);
  207.         }.bind(this));
  208.  
  209.         if (_.isNil(toSaveList) || _.isEmpty(toSaveList))
  210.             return;
  211.  
  212.         this.isSaveInProgress = true;
  213.  
  214.         this._RatioService.saveRatios(toSaveList).subscribe(
  215.             data => data,
  216.             error => alert(JSON.stringify(error)),
  217.             () => {
  218.  
  219.                 this.isSaveInProgress = false;
  220.                 this.aChangeWasMade = false;
  221.                 this.ratioIdsToUpdate = null;
  222.  
  223.                 this.searchRatios();
  224.             }
  225.         );
  226.     }
  227.  
  228.     disableExportButton(): boolean {
  229.  
  230.         if (_.isNil(this.ratios) || _.isEmpty(this.ratios))
  231.             return true;
  232.         else if (this.isExportInProgress)
  233.             return true;
  234.         return false;
  235.     }
  236.  
  237.     exportButtonStateInfo(): string {
  238.  
  239.         if (this.disableExportButton())
  240.             return "No items to export";
  241.         if (this.isExportInProgress)
  242.             return "Exporting..";
  243.         return "Export";
  244.     }
  245.  
  246.     exportConfigurationReport(): void {
  247.  
  248.         this.isExportInProgress = true;
  249.  
  250.         this._FacingService.exportConfigurationReport(this.selectedBecPeriod[0]).subscribe(
  251.             data => this.excelPath = data,
  252.             error => alert(JSON.stringify(error)),
  253.             () => {
  254.                 this.isExportInProgress = false;
  255.                 window.open(this._AppService.download(this.excelPath));
  256.             }
  257.         );
  258.     }
  259.  
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement