Commit a0f6eb205187d108e8b7523644c4300df08070d0
Exists in
develop
Merge branch 'develop' into 'develop'
Develop See merge request !120
Showing
1 changed file
Show diff stats
src/app/shared/promocion/promocion.component.ts
1 | import { Component, OnInit, HostListener } from '@angular/core'; | 1 | import { Component, OnInit, HostListener } from '@angular/core'; |
2 | import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; | 2 | import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; |
3 | import { IArticulo } from 'src/app/interfaces/IArticulo'; | 3 | import { IArticulo } from 'src/app/interfaces/IArticulo'; |
4 | import { ArticuloService } from 'src/app/services/articulo/articulo.service'; | 4 | import { ArticuloService } from 'src/app/services/articulo/articulo.service'; |
5 | import { PromocionService } from 'src/app/services/promocion/promocion.service'; | 5 | import { PromocionService } from 'src/app/services/promocion/promocion.service'; |
6 | import { Subject } from 'rxjs'; | 6 | import { Subject } from 'rxjs'; |
7 | import { APP_SETTINGS } from 'src/etc/AppSettings'; | 7 | import { APP_SETTINGS } from 'src/etc/AppSettings'; |
8 | import { InactiveScreenService } from 'src/app/services/inactive-screen/inactive-screen.service'; | 8 | import { InactiveScreenService } from 'src/app/services/inactive-screen/inactive-screen.service'; |
9 | import { SinonimoService } from 'src/app/services/sinonimo/sinonimo.service'; | 9 | import { SinonimoService } from 'src/app/services/sinonimo/sinonimo.service'; |
10 | import { ISinonimo } from 'src/app/interfaces/ISinonimo'; | 10 | import { ISinonimo } from 'src/app/interfaces/ISinonimo'; |
11 | import { SinonimoComponent } from '../sinonimo/sinonimo.component'; | 11 | import { SinonimoComponent } from '../sinonimo/sinonimo.component'; |
12 | import * as _ from 'lodash'; | 12 | import * as _ from 'lodash'; |
13 | 13 | ||
14 | @Component({ | 14 | @Component({ |
15 | selector: 'app-promocion', | 15 | selector: 'app-promocion', |
16 | templateUrl: './promocion.component.html', | 16 | templateUrl: './promocion.component.html', |
17 | styleUrls: ['./promocion.component.scss'] | 17 | styleUrls: ['./promocion.component.scss'] |
18 | }) | 18 | }) |
19 | export class PromocionComponent implements OnInit { | 19 | export class PromocionComponent implements OnInit { |
20 | articulosPromo: IArticulo[] = []; | 20 | articulosPromo: IArticulo[] = []; |
21 | promociones: IArticulo[] = []; | 21 | promociones: IArticulo[] = []; |
22 | onClose: Subject<any>; | 22 | onClose: Subject<any>; |
23 | urlImagenes = `${APP_SETTINGS.apiImagenes}/imagenes/`; | 23 | urlImagenes = `${APP_SETTINGS.apiImagenes}/imagenes/`; |
24 | loading = true; | 24 | loading = true; |
25 | modalSinonimo: BsModalRef; | 25 | modalSinonimo: BsModalRef; |
26 | isPromoSelected = false; | 26 | isPromoSelected = false; |
27 | 27 | ||
28 | constructor( | 28 | constructor( |
29 | private modalPromocion: BsModalRef, | 29 | private modalPromocion: BsModalRef, |
30 | private modalService: BsModalService, | 30 | private modalService: BsModalService, |
31 | private articuloService: ArticuloService, | 31 | private articuloService: ArticuloService, |
32 | private promocionService: PromocionService, | 32 | private promocionService: PromocionService, |
33 | private sinonimoService: SinonimoService, | 33 | private sinonimoService: SinonimoService, |
34 | private inactiveScreen: InactiveScreenService, | 34 | private inactiveScreen: InactiveScreenService, |
35 | ) { | 35 | ) { |
36 | this.onClose = new Subject(); | 36 | this.onClose = new Subject(); |
37 | } | 37 | } |
38 | 38 | ||
39 | ngOnInit() { | 39 | ngOnInit() { |
40 | this.getPromociones(); | 40 | this.getPromociones(); |
41 | } | 41 | } |
42 | 42 | ||
43 | selectPromo(promo: IArticulo) { | 43 | selectPromo(promo: IArticulo) { |
44 | if (this.isPromoSelected) return; | 44 | if (this.isPromoSelected) return; |
45 | this.isPromoSelected = true; | 45 | this.isPromoSelected = true; |
46 | this.sinonimoService.getSinonimos(promo.CodSec, promo.CodArt) | 46 | this.articuloService.getById(promo.id) |
47 | .subscribe((res: ISinonimo[]) => { | 47 | .subscribe((resPromo: IArticulo) => { |
48 | if (res.length) { | 48 | promo = resPromo; |
49 | const sinonimos = []; | 49 | this.sinonimoService.getSinonimos(promo.CodSec, promo.CodArt) |
50 | const gruposArticulos = _.groupBy(res[0].productos, 'ID_SIN'); | 50 | .subscribe((res: ISinonimo[]) => { |
51 | Object.keys(gruposArticulos).forEach(key => { | 51 | if (res.length) { |
52 | sinonimos.push({ productos: gruposArticulos[key] }); | 52 | const sinonimos = []; |
53 | }); | 53 | const gruposArticulos = _.groupBy(res[0].productos, 'ID_SIN'); |
54 | res = sinonimos; | 54 | Object.keys(gruposArticulos).forEach(key => { |
55 | this.openModalSinonimos(res, promo); | 55 | sinonimos.push({ productos: gruposArticulos[key] }); |
56 | } else { | 56 | }); |
57 | promo.cantidad = 1; | 57 | res = sinonimos; |
58 | this.articuloService.setArticulo(promo); | 58 | this.openModalSinonimos(res, promo); |
59 | this.onClose.next(); | 59 | } else { |
60 | this.modalPromocion.hide(); | 60 | promo.cantidad = 1; |
61 | } | 61 | this.articuloService.setArticulo(promo); |
62 | this.onClose.next(); | ||
63 | this.modalPromocion.hide(); | ||
64 | } | ||
65 | }, err => console.error(err)); | ||
62 | }, err => console.error(err)); | 66 | }, err => console.error(err)); |
63 | this.mediaPantalla(); | 67 | this.mediaPantalla(); |
64 | } | 68 | } |
65 | 69 | ||
66 | openModalSinonimos(sinonimosData: ISinonimo[], articulo: IArticulo) { | 70 | openModalSinonimos(sinonimosData: ISinonimo[], articulo: IArticulo) { |
67 | if (this.modalSinonimo) return; | 71 | if (this.modalSinonimo) return; |
68 | this.modalSinonimo = this.modalService.show(SinonimoComponent, { | 72 | this.modalSinonimo = this.modalService.show(SinonimoComponent, { |
69 | initialState: { | 73 | initialState: { |
70 | sinonimos: sinonimosData, | 74 | sinonimos: sinonimosData, |
71 | articulo | 75 | articulo |
72 | }, | 76 | }, |
73 | class: 'modal-dialog-centered', | 77 | class: 'modal-dialog-centered', |
74 | ignoreBackdropClick: true, | 78 | ignoreBackdropClick: true, |
75 | }); | 79 | }); |
76 | 80 | ||
77 | this.modalSinonimo.content.onClose | 81 | this.modalSinonimo.content.onClose |
78 | .subscribe((res: any) => { | 82 | .subscribe((res: any) => { |
79 | this.modalSinonimo = null; | 83 | this.modalSinonimo = null; |
80 | if (!res) { | 84 | if (!res) { |
81 | this.onClose.next(); | 85 | this.onClose.next(); |
82 | this.modalPromocion.hide(); | 86 | this.modalPromocion.hide(); |
83 | return; | 87 | return; |
84 | } | 88 | } |
85 | for (const a of articulo.productos) { | 89 | for (const a of articulo.productos) { |
86 | for (const aRes of res.articulos) { | 90 | for (const aRes of res.articulos) { |
87 | if (a.idSinonimo === aRes.ID_SIN) { | 91 | if (a.idSinonimo === aRes.ID_SIN) { |
88 | this.articuloService.changeArticulo(a, aRes); | 92 | this.articuloService.changeArticulo(a, aRes); |
89 | } | 93 | } |
90 | } | 94 | } |
91 | } | 95 | } |
92 | this.articuloService.setArticulo(articulo); | 96 | this.articuloService.setArticulo(articulo); |
93 | this.onClose.next(); | 97 | this.onClose.next(); |
94 | this.modalPromocion.hide(); | 98 | this.modalPromocion.hide(); |
95 | }); | 99 | }); |
96 | } | 100 | } |
97 | 101 | ||
98 | getPromociones() { | 102 | getPromociones() { |
99 | const sector = this.articulosPromo[0].CodSec; | 103 | const sector = this.articulosPromo[0].CodSec; |
100 | const codigo = this.articulosPromo[0].CodArt; | 104 | const codigo = this.articulosPromo[0].CodArt; |
101 | this.promocionService.getPromociones(sector, codigo) | 105 | this.promocionService.getPromociones(sector, codigo) |
102 | .subscribe((res: IArticulo[]) => { | 106 | .subscribe((res: IArticulo[]) => { |
103 | this.promociones = res; | 107 | this.promociones = res; |
104 | this.loading = false; | 108 | this.loading = false; |
105 | }, error => { console.error(error); }); | 109 | }, error => { console.error(error); }); |
106 | } | 110 | } |
107 | 111 | ||
108 | @HostListener('document:click', ['$event']) | 112 | @HostListener('document:click', ['$event']) |
109 | eventListener(event: Event) { | 113 | eventListener(event: Event) { |
110 | clearTimeout(this.inactiveScreen.timerReposo); | 114 | clearTimeout(this.inactiveScreen.timerReposo); |
111 | this.inactiveScreen.startTimeOutInactividad(); | 115 | this.inactiveScreen.startTimeOutInactividad(); |
112 | } | 116 | } |
113 | 117 | ||
114 | @HostListener('scroll', ['$event']) | 118 | @HostListener('scroll', ['$event']) |
115 | scrollEvent(event: Event) { | 119 | scrollEvent(event: Event) { |
116 | clearTimeout(this.inactiveScreen.timerReposo); | 120 | clearTimeout(this.inactiveScreen.timerReposo); |
117 | this.inactiveScreen.startTimeOutInactividad(); | 121 | this.inactiveScreen.startTimeOutInactividad(); |
118 | } | 122 | } |
119 | 123 | ||
120 | mediaPantalla() { | 124 | mediaPantalla() { |
121 | if ($('body').hasClass('media-pantalla')) { | 125 | if ($('body').hasClass('media-pantalla')) { |
122 | $('.modal-content').addClass('media-pantalla'); | 126 | $('.modal-content').addClass('media-pantalla'); |
123 | } | 127 | } |
124 | } | 128 | } |
125 | } | 129 | } |
126 | 130 |