Commit c6f77ae9bac7837b8e1614fe3c69f1b95f4fd7b6

Authored by Marcelo Puebla
1 parent b50c944e9b
Exists in develop

Fix

En seleccion de sinonimo
src/app/modules/seleccion-articulos/seleccion-articulos.component.ts
1 import { Component, OnInit, OnDestroy, HostListener, ViewChild, AfterViewInit } from '@angular/core'; 1 import { Component, OnInit, OnDestroy, HostListener, ViewChild, AfterViewInit } from '@angular/core';
2 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; 2 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
3 import { ArticuloService } from 'src/app/services/articulo/articulo.service'; 3 import { ArticuloService } from 'src/app/services/articulo/articulo.service';
4 import { IArticulo } from 'src/app/interfaces/IArticulo'; 4 import { IArticulo } from 'src/app/interfaces/IArticulo';
5 import { APP_SETTINGS } from 'src/etc/AppSettings'; 5 import { APP_SETTINGS } from 'src/etc/AppSettings';
6 import { ICategoria } from 'src/app/interfaces/ICategoria'; 6 import { ICategoria } from 'src/app/interfaces/ICategoria';
7 import { ISinonimo } from 'src/app/interfaces/ISinonimo'; 7 import { ISinonimo } from 'src/app/interfaces/ISinonimo';
8 import { PromocionComponent } from 'src/app/shared/promocion/promocion.component'; 8 import { PromocionComponent } from 'src/app/shared/promocion/promocion.component';
9 import { InactiveScreenService } from 'src/app/services/inactive-screen/inactive-screen.service'; 9 import { InactiveScreenService } from 'src/app/services/inactive-screen/inactive-screen.service';
10 import { SinonimoService } from 'src/app/services/sinonimo/sinonimo.service'; 10 import { SinonimoService } from 'src/app/services/sinonimo/sinonimo.service';
11 import { SinonimoComponent } from 'src/app/shared/sinonimo/sinonimo.component'; 11 import { SinonimoComponent } from 'src/app/shared/sinonimo/sinonimo.component';
12 import { FiltroCategoriasComponent } from './filtro-categorias/filtro-categorias.component'; 12 import { FiltroCategoriasComponent } from './filtro-categorias/filtro-categorias.component';
13 import * as _ from 'lodash'; 13 import * as _ from 'lodash';
14 import { ANIMATIONS } from 'src/app/utils/animations'; 14 import { ANIMATIONS } from 'src/app/utils/animations';
15 import { NoStockComponent } from './no-stock/no-stock.component'; 15 import { NoStockComponent } from './no-stock/no-stock.component';
16 import { DateExtension } from 'src/app/utils/dateExtension'; 16 import { DateExtension } from 'src/app/utils/dateExtension';
17 17
18 @Component({ 18 @Component({
19 selector: 'app-seleccion-articulos', 19 selector: 'app-seleccion-articulos',
20 templateUrl: './seleccion-articulos.component.html', 20 templateUrl: './seleccion-articulos.component.html',
21 styleUrls: ['./seleccion-articulos.component.scss'], 21 styleUrls: ['./seleccion-articulos.component.scss'],
22 animations: [ANIMATIONS.EnterLeaveY] 22 animations: [ANIMATIONS.EnterLeaveY]
23 }) 23 })
24 export class SeleccionArticulosComponent implements OnInit, AfterViewInit, OnDestroy { 24 export class SeleccionArticulosComponent implements OnInit, AfterViewInit, OnDestroy {
25 loading = true; 25 loading = true;
26 timeoutHandler: any; 26 timeoutHandler: any;
27 urlImagenes = `${APP_SETTINGS.apiImagenes}/imagenes/`; 27 urlImagenes = `${APP_SETTINGS.apiImagenes}/imagenes/`;
28 articulos: IArticulo[] = []; 28 articulos: IArticulo[] = [];
29 auxArticulos: IArticulo[] = []; 29 auxArticulos: IArticulo[] = [];
30 showQuantity = 100; 30 showQuantity = 100;
31 searchTerm = ''; 31 searchTerm = '';
32 ordenandoByVendidos = true; 32 ordenandoByVendidos = true;
33 modalRef: BsModalRef; 33 modalRef: BsModalRef;
34 total = 0; 34 total = 0;
35 @ViewChild(FiltroCategoriasComponent, { static: false }) filtroCategorias: FiltroCategoriasComponent; 35 @ViewChild(FiltroCategoriasComponent, { static: false }) filtroCategorias: FiltroCategoriasComponent;
36 36
37 constructor( 37 constructor(
38 public articuloService: ArticuloService, 38 public articuloService: ArticuloService,
39 private sinonimoService: SinonimoService, 39 private sinonimoService: SinonimoService,
40 private modalService: BsModalService, 40 private modalService: BsModalService,
41 private inactiveScreen: InactiveScreenService, 41 private inactiveScreen: InactiveScreenService,
42 ) { } 42 ) { }
43 43
44 ngOnInit() { } 44 ngOnInit() { }
45 45
46 ngAfterViewInit(): void { 46 ngAfterViewInit(): void {
47 this.filtroCategorias.getCategorias(); 47 this.filtroCategorias.getCategorias();
48 this.mediaPantalla(); 48 this.mediaPantalla();
49 } 49 }
50 50
51 ngOnDestroy() { 51 ngOnDestroy() {
52 for (let i = 1; i <= this.modalService.getModalsCount(); i++) { 52 for (let i = 1; i <= this.modalService.getModalsCount(); i++) {
53 this.modalService.hide(i); 53 this.modalService.hide(i);
54 } 54 }
55 } 55 }
56 56
57 getProductos() { 57 getProductos() {
58 this.articuloService.getAll() 58 this.articuloService.getAll()
59 .subscribe((result: IArticulo[]) => { 59 .subscribe((result: IArticulo[]) => {
60 this.articuloService.setArticulosSinImagen(result); 60 this.articuloService.setArticulosSinImagen(result);
61 if (this.filtroCategorias.queMostrar === 'ordenar') { 61 if (this.filtroCategorias.queMostrar === 'ordenar') {
62 this.filtroCategorias.categorias.forEach((categoria: ICategoria) => { 62 this.filtroCategorias.categorias.forEach((categoria: ICategoria) => {
63 const tempArticulos = result.filter((articulo: IArticulo) => { 63 const tempArticulos = result.filter((articulo: IArticulo) => {
64 return articulo.categoria_selfservice === categoria.id; 64 return articulo.categoria_selfservice === categoria.id;
65 }); 65 });
66 result = tempArticulos; 66 result = tempArticulos;
67 }); 67 });
68 } 68 }
69 localStorage.setItem('articulos', JSON.stringify(result)); 69 localStorage.setItem('articulos', JSON.stringify(result));
70 this.setProductos(); 70 this.setProductos();
71 }, (error) => { 71 }, (error) => {
72 console.error(error); 72 console.error(error);
73 }); 73 });
74 } 74 }
75 75
76 setProductos() { 76 setProductos() {
77 this.articulos = JSON.parse(localStorage.getItem('articulos')); 77 this.articulos = JSON.parse(localStorage.getItem('articulos'));
78 this.filterItems(); 78 this.filterItems();
79 this.loading = false; 79 this.loading = false;
80 } 80 }
81 81
82 filterItems() { 82 filterItems() {
83 if (this.filtroCategorias.categoriaActive === 0) { 83 if (this.filtroCategorias.categoriaActive === 0) {
84 this.auxArticulos = this.articulos; 84 this.auxArticulos = this.articulos;
85 return; 85 return;
86 } 86 }
87 this.auxArticulos = this.articulos.filter(x => { 87 this.auxArticulos = this.articulos.filter(x => {
88 return x.categoria_selfservice === this.filtroCategorias.categoriaActive; 88 return x.categoria_selfservice === this.filtroCategorias.categoriaActive;
89 }); 89 });
90 this.ordenar(); 90 this.ordenar();
91 } 91 }
92 92
93 ordenar() { 93 ordenar() {
94 if (this.ordenandoByVendidos) { 94 if (this.ordenandoByVendidos) {
95 this.auxArticulos.sort((a, b) => { 95 this.auxArticulos.sort((a, b) => {
96 return b.cantidadVendida - a.cantidadVendida; 96 return b.cantidadVendida - a.cantidadVendida;
97 }); 97 });
98 } 98 }
99 } 99 }
100 100
101 selectArticulo(articulo: IArticulo) { 101 selectArticulo(articulo: IArticulo) {
102 if (articulo.ExiVta < 1) { 102 if (articulo.ExiVta < 1) {
103 if (this.modalRef) return; 103 if (this.modalRef) return;
104 this.modalRef = this.modalService.show(NoStockComponent, { 104 this.modalRef = this.modalService.show(NoStockComponent, {
105 class: 'modal-dialog-centered', 105 class: 'modal-dialog-centered',
106 backdrop: false, 106 backdrop: false,
107 ignoreBackdropClick: true, 107 ignoreBackdropClick: true,
108 }); 108 });
109 this.modalRef.content.onClose 109 this.modalRef.content.onClose
110 .subscribe(() => this.modalRef = null); 110 .subscribe(() => this.modalRef = null);
111 } else { 111 } else {
112 this.getByID(articulo.id); 112 this.getByID(articulo.id);
113 } 113 }
114 } 114 }
115 115
116 getByID(id: number) { 116 getByID(id: number) {
117 this.articuloService.getById(id) 117 this.articuloService.getById(id)
118 .subscribe((res: IArticulo) => { 118 .subscribe((res: IArticulo) => {
119 if (res.FPP) { 119 if (res.FPP) {
120 this.openModalPromos(res); 120 this.openModalPromos(res);
121 } else { 121 } else {
122 this.getSinonimos(res); 122 this.getSinonimos(res);
123 } 123 }
124 }, err => console.error(err)); 124 }, err => console.error(err));
125 } 125 }
126 126
127 getSinonimos(articulo: IArticulo) { 127 getSinonimos(articulo: IArticulo) {
128 this.sinonimoService.getSinonimos(articulo.CodSec, articulo.CodArt) 128 this.sinonimoService.getSinonimos(articulo.CodSec, articulo.CodArt)
129 .subscribe((res: any[]) => { 129 .subscribe((res: any[]) => {
130 if (res.length) { 130 if (res.length) {
131 const sinonimos = []; 131 const sinonimos = [];
132 const gruposArticulos = _.groupBy(res[0].productos, 'ORDEN_P'); 132 const gruposArticulos = _.groupBy(res[0].productos, 'ORDEN_P');
133 Object.keys(gruposArticulos).forEach(key => { 133 Object.keys(gruposArticulos).forEach(key => {
134 sinonimos.push({ productos: gruposArticulos[key], }); 134 sinonimos.push({ productos: gruposArticulos[key], });
135 }); 135 });
136 for (const a of articulo.productos) { 136 for (const a of articulo.productos) {
137 Object.keys(gruposArticulos).forEach((key, i) => { 137 Object.keys(gruposArticulos).forEach((key, i) => {
138 if (gruposArticulos[key][i].ID_SIN.toString() === a.idSinonimo.toString()) { 138 if (gruposArticulos[key][i].ID_SIN.toString() === a.idSinonimo.toString()) {
139 sinonimos[i].cantidad = sinonimos[i].cantidadRestante = a.cantidad; 139 sinonimos[i].cantidad = sinonimos[i].cantidadRestante = a.cantidad;
140 } 140 }
141 }); 141 });
142 } 142 }
143 res = sinonimos; 143 res = sinonimos;
144 this.openModalSinonimos(res, articulo); 144 this.openModalSinonimos(res, articulo);
145 } else { 145 } else {
146 this.articuloService.setArticulo(articulo); 146 this.articuloService.setArticulo(articulo);
147 } 147 }
148 }); 148 });
149 } 149 }
150 150
151 openModalPromos(articulo: IArticulo) { 151 openModalPromos(articulo: IArticulo) {
152 if (this.modalRef) return; 152 if (this.modalRef) return;
153 this.articuloService.setArticulosSinImagen([articulo]); 153 this.articuloService.setArticulosSinImagen([articulo]);
154 this.modalRef = this.modalService.show(PromocionComponent, { 154 this.modalRef = this.modalService.show(PromocionComponent, {
155 initialState: { articulosPromo: [articulo] }, 155 initialState: { articulosPromo: [articulo] },
156 class: 'modal-dialog-centered', 156 class: 'modal-dialog-centered',
157 ignoreBackdropClick: true, 157 ignoreBackdropClick: true,
158 }); 158 });
159 this.modalRef.content.onClose 159 this.modalRef.content.onClose
160 .subscribe(() => this.modalRef = null); 160 .subscribe(() => this.modalRef = null);
161 this.mediaPantalla(); 161 this.mediaPantalla();
162 } 162 }
163 163
164 openModalSinonimos(sinonimosData: ISinonimo[], articulo: IArticulo) { 164 openModalSinonimos(sinonimosData: ISinonimo[], articulo: IArticulo) {
165 if (this.modalRef) return; 165 if (this.modalRef) return;
166 this.modalRef = this.modalService.show(SinonimoComponent, { 166 this.modalRef = this.modalService.show(SinonimoComponent, {
167 initialState: { 167 initialState: {
168 sinonimos: sinonimosData, 168 sinonimos: sinonimosData,
169 articulo 169 articulo
170 }, 170 },
171 class: 'modal-dialog-centered', 171 class: 'modal-dialog-centered',
172 ignoreBackdropClick: true, 172 ignoreBackdropClick: true,
173 }); 173 });
174 174
175 this.modalRef.content.onClose 175 this.modalRef.content.onClose
176 .subscribe((res: any) => { 176 .subscribe((res: any) => {
177 this.modalRef = null; 177 this.modalRef = null;
178 if (!res) return; 178 if (!res) return;
179 articulo.productos = res.articulos; 179 articulo.productos = articulo.productos.filter(a => a.idSinonimo === 0);
180 Array.prototype.push.apply(articulo.productos, res.articulos);
180 this.articuloService.setArticulo(articulo); 181 this.articuloService.setArticulo(articulo);
181 }); 182 });
182 this.mediaPantalla(); 183 this.mediaPantalla();
183 } 184 }
184 185
185 deleteArticulo(index: number) { 186 deleteArticulo(index: number) {
186 this.articuloService.deleteArticulo(index); 187 this.articuloService.deleteArticulo(index);
187 } 188 }
188 189
189 increaseShow() { 190 increaseShow() {
190 this.showQuantity += 100; 191 this.showQuantity += 100;
191 } 192 }
192 193
193 194
194 validarFecha(fechaInicio: string, fechaFin: string) { 195 validarFecha(fechaInicio: string, fechaFin: string) {
195 return DateExtension.ValidateDateRange(fechaInicio, fechaFin); 196 return DateExtension.ValidateDateRange(fechaInicio, fechaFin);
196 } 197 }
197 198
198 @HostListener('scroll', ['$event']) 199 @HostListener('scroll', ['$event'])
199 scrollEvent(event: Event) { 200 scrollEvent(event: Event) {
200 clearTimeout(this.inactiveScreen.timerReposo); 201 clearTimeout(this.inactiveScreen.timerReposo);
201 this.inactiveScreen.startTimeOutInactividad(); 202 this.inactiveScreen.startTimeOutInactividad();
202 } 203 }
203 204
204 mouseup() { 205 mouseup() {
205 if (!this.timeoutHandler) return; 206 if (!this.timeoutHandler) return;
206 clearInterval(this.timeoutHandler); 207 clearInterval(this.timeoutHandler);
207 } 208 }
208 209
209 scrollY(el: HTMLElement, value) { 210 scrollY(el: HTMLElement, value) {
210 el.scroll({ behavior: 'smooth', top: value + el.scrollTop }); 211 el.scroll({ behavior: 'smooth', top: value + el.scrollTop });
211 this.timeoutHandler = setInterval(() => { 212 this.timeoutHandler = setInterval(() => {
212 el.scroll({ behavior: 'smooth', top: value + el.scrollTop }); 213 el.scroll({ behavior: 'smooth', top: value + el.scrollTop });
213 }, 500); 214 }, 500);
214 } 215 }
215 216
216 scrollX(el: HTMLElement, value) { 217 scrollX(el: HTMLElement, value) {
217 el.scroll({ behavior: 'smooth', left: value + el.scrollLeft }); 218 el.scroll({ behavior: 'smooth', left: value + el.scrollLeft });
218 this.timeoutHandler = setInterval(() => { 219 this.timeoutHandler = setInterval(() => {
219 el.scroll({ behavior: 'smooth', left: value + el.scrollLeft }); 220 el.scroll({ behavior: 'smooth', left: value + el.scrollLeft });
220 }, 500); 221 }, 500);
221 } 222 }
222 223
223 mediaPantalla() { 224 mediaPantalla() {
224 if ($('body').hasClass('media-pantalla')) { 225 if ($('body').hasClass('media-pantalla')) {
225 $(`.cat-content,#cat-content,#content,.cat-btn,#boxCarrito, 226 $(`.cat-content,#cat-content,#content,.cat-btn,#boxCarrito,
226 .cat-box,.img-categoria, .modal-content`) 227 .cat-box,.img-categoria, .modal-content`)
227 .addClass('media-pantalla') 228 .addClass('media-pantalla')
228 .addBack('media-pantalla'); 229 .addBack('media-pantalla');
229 } 230 }
230 } 231 }
231 } 232 }
232 233