Skip to content

Commit

Permalink
Creates pedido-actions-bar component to reuse its functionality in pe…
Browse files Browse the repository at this point in the history
…didos and ver-pedido components.
  • Loading branch information
facundososalopez committed Mar 11, 2023
1 parent c344cd3 commit cbe5009
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Router } from '@angular/router';
import { FacturaVenta } from './../../models/factura-venta';
import { Component, Input, OnInit, Output, EventEmitter } from '@angular/core';

export type fvActionButtonName = 'show'|'email'|'show-remito'|'new-remito'|'create-nota-credito';
type FacturaVentaActionButtonName = 'show'|'email'|'show-remito'|'new-remito'|'create-nota-credito';

@Component({
selector: 'app-factura-venta-actions-bar',
Expand All @@ -31,9 +31,9 @@ export class FacturaVentaActionsBarComponent implements OnInit {
@Input() set facturaVenta(value: FacturaVenta) { this.pFacturaVenta = value; }
get facturaVenta(): FacturaVenta { return this.pFacturaVenta; }

private pHiddenButtons: fvActionButtonName[] = [];
@Input() set hiddenButtons(value: fvActionButtonName[]) { this.pHiddenButtons = value; }
get hiddenButtons(): fvActionButtonName[] { return this.pHiddenButtons; }
private pHiddenButtons: FacturaVentaActionButtonName[] = [];
@Input() set hiddenButtons(value: FacturaVentaActionButtonName[]) { this.pHiddenButtons = value; }
get hiddenButtons(): FacturaVentaActionButtonName[] { return this.pHiddenButtons; }

@Output() afterAutorizar = new EventEmitter<void>();
@Output() afterNoAutorizar = new EventEmitter<void>();
Expand Down Expand Up @@ -75,7 +75,7 @@ export class FacturaVentaActionsBarComponent implements OnInit {
}

ngOnInit(): void {
Object.keys(this.hiddenButtonsValues).forEach((k: fvActionButtonName) => {
Object.keys(this.hiddenButtonsValues).forEach((k: FacturaVentaActionButtonName) => {
this.hiddenButtonsValues[k] = this.hiddenButtons.indexOf(k) >= 0;
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<ul class="list-inline mb-0 text-center d-inline-block">
<li class="list-inline-item" *ngIf="!hiddenButtonsValues['show']">
<a class="btn btn-primary fake-cursor" ngbPopover="Ver Detalle" triggers="mouseenter:mouseleave" placement="left"
(click)="verPedido()">
<fa-icon [icon]="['far', 'eye']"></fa-icon>
</a>
</li>
<li class="list-inline-item" *ngIf="!hiddenButtonsValues['download']">
<button class="btn btn-primary" (click)="downloadPedidoPdf(pedido)" placement="left" ngbPopover="Descargar Archivo" triggers="mouseenter:mouseleave">
<fa-icon [icon]="['fas', 'file-download']"></fa-icon>
</button>
</li>
<li class="list-inline-item" *ngIf="puedeEditarPedido && !hiddenButtonsValues['edit']">
<a class="btn btn-primary fake-cursor" ngbPopover="Editar" triggers="mouseenter:mouseleave" placement="left"
(click)="editarPedido()">
<fa-icon [icon]="['fas', 'pen']"></fa-icon>
</a>
</li>
<li class="list-inline-item" *ngIf="!hiddenButtonsValues['clone']">
<a class="btn btn-primary fake-cursor" ngbPopover="Clonar" triggers="mouseenter:mouseleave" placement="left"
(click)="clonarPedido()">
<fa-icon [icon]="['fas', 'copy']"></fa-icon>
</a>
</li>
<li class="list-inline-item" *ngIf="puedeEliminarPedido && !hiddenButtonsValues['delete']">
<a class="btn btn-primary fake-cursor" ngbPopover="Cancelar" triggers="mouseenter:mouseleave" placement="left"
(click)="cancelarPedido()">
<fa-icon [icon]="['fas', 'times']"></fa-icon>
</a>
</li>
<li class="list-inline-item" *ngIf="puedeFacturarPedido && !hiddenButtonsValues['invoice']">
<a class="btn btn-primary fake-cursor" ngbPopover="Facturar" triggers="mouseenter:mouseleave" placement="left"
(click)="facturarPedido()">
<fa-icon [icon]="['fas', 'cash-register']"></fa-icon>
</a>
</li>
<li class="list-inline-item" *ngIf="puedeVerFacturas && !hiddenButtonsValues['show-invoice']">
<a class="btn btn-primary fake-cursor" ngbPopover="Ver Facturas" triggers="mouseenter:mouseleave" placement="left"
(click)="verFacturas()">
<fa-icon [icon]="['fas', 'link']"></fa-icon>
</a>
</li>
</ul>
140 changes: 140 additions & 0 deletions src/app/components/pedido-actions-bar/pedido-actions-bar.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { AuthService } from './../../services/auth.service';
import { finalize } from 'rxjs/operators';
import { PedidosService } from './../../services/pedidos.service';
import { LoadingOverlayService } from './../../services/loading-overlay.service';
import { StorageKeys, StorageService } from './../../services/storage.service';
import { MensajeModalType } from './../mensaje-modal/mensaje-modal.component';
import { MensajeService } from './../../services/mensaje.service';
import { Router } from '@angular/router';
import { Rol } from './../../models/rol';
import { EstadoPedido } from './../../models/estado-pedido';
import { Pedido } from './../../models/pedido';
import { Component, OnInit, Input } from '@angular/core';

type PedidoActionButtonName = 'show'|'download'|'edit'|'clone'|'delete'|'invoice'|'show-invoice';

@Component({
selector: 'app-pedido-actions-bar',
templateUrl: './pedido-actions-bar.component.html'
})
export class PedidoActionsBarComponent implements OnInit {
private pPedido: Pedido;
@Input() set pedido(value: Pedido) { this.pPedido = value; }
get pedido(): Pedido { return this.pPedido; }

private pHiddenButtons: PedidoActionButtonName[] = [];
@Input() set hiddenButtons(value: PedidoActionButtonName[]) { this.pHiddenButtons = value; }
get hiddenButtons(): PedidoActionButtonName[] { return this.pHiddenButtons; }

hiddenButtonsValues = {
'show': false,
'download': false,
'edit': false,
'clone': false,
'delete': false,
'invoice': false,
'show-invoice': false,
}

allowedRolesToDelete: Rol[] = [ Rol.ADMINISTRADOR, Rol.ENCARGADO, Rol.VENDEDOR ];
hasRolToDelete = false;

allowedRolesToEdit: Rol[] = [ Rol.ADMINISTRADOR, Rol.ENCARGADO, Rol.VENDEDOR ];
hasRolToEdit = false;

puedeEliminarPedido = false;
puedeEditarPedido = false;
puedeFacturarPedido = false;
puedeVerFacturas = false;

constructor(private router: Router,
private authService: AuthService,
private mensajeService: MensajeService,
private loadingOverlayService: LoadingOverlayService,
private pedidosService: PedidosService,
private storageService: StorageService) {
this.hasRolToDelete = this.authService.userHasAnyOfTheseRoles(this.allowedRolesToDelete);
this.hasRolToEdit = this.authService.userHasAnyOfTheseRoles(this.allowedRolesToEdit);
}

ngOnInit(): void {
this.puedeEliminarPedido = this.hasRolToDelete && this.pedido.estado === EstadoPedido.ABIERTO;
this.puedeEditarPedido = this.hasRolToEdit && this.pedido.estado === EstadoPedido.ABIERTO;
this.puedeFacturarPedido = this.hasRolToEdit && this.pedido.estado === EstadoPedido.ABIERTO;
this.puedeVerFacturas = [EstadoPedido.CERRADO].indexOf(this.pedido.estado) >= 0;

Object.keys(this.hiddenButtonsValues).forEach((k: PedidoActionButtonName) => {
this.hiddenButtonsValues[k] = this.hiddenButtons.indexOf(k) >= 0;
});
}

verPedido() {
this.router.navigate(['/pedidos/ver', this.pedido.idPedido]);
}

cancelarPedido() {
if (!this.puedeEliminarPedido) {
this.mensajeService.msg('No posee permiso para cancelar un pedido.', MensajeModalType.ERROR);
return;
}

const msg = `¿Está seguro que desea cancelar el pedido #${this.pedido.nroPedido}?`;

this.mensajeService.msg(msg, MensajeModalType.CONFIRM).then((result) => {
if (result) {
this.loadingOverlayService.activate();
this.pedidosService.cancelarPedido(this.pedido.idPedido)
.pipe(finalize(() => this.loadingOverlayService.deactivate()))
.subscribe({
next: () => { location.reload(); },
error: err => this.mensajeService.msg(`Error: ${err.error}`, MensajeModalType.ERROR),
})
;
}
}, () => { return; });
}

editarPedido() {
if (!this.puedeEditarPedido) {
this.mensajeService.msg('No posee permiso para editar un pedido.', MensajeModalType.ERROR);
return;
}
this.storageService.removeItem(StorageKeys.PEDIDO_EDITAR);
this.router.navigate(['/pedidos/editar', this.pedido.idPedido]);
}

clonarPedido() {
this.storageService.removeItem(StorageKeys.PEDIDO_NUEVO);
this.router.navigate(['/pedidos/nuevo'], { queryParams: { idToClone: this.pedido.idPedido }});
}

facturarPedido() {
if (!this.puedeFacturarPedido) {
this.mensajeService.msg('No posee permiso para facturar un pedido.', MensajeModalType.ERROR);
return;
}
this.storageService.removeItem(StorageKeys.PEDIDO_FACTURAR);
this.router.navigate(['/facturas-venta/de-pedido', this.pedido.idPedido]);
}

verFacturas() {
if (this.puedeVerFacturas) {
this.router.navigate(['/facturas-venta'], { queryParams: { nroPedido: this.pedido.nroPedido }});
}
}

downloadPedidoPdf() {
this.loadingOverlayService.activate();
this.pedidosService.getPedidoPdf(this.pedido.idPedido)
.pipe(finalize(() => this.loadingOverlayService.deactivate()))
.subscribe({
next: (res) => {
const file = new Blob([res], {type: 'application/pdf'});
const fileURL = URL.createObjectURL(file);
window.open(fileURL, '_blank');
},
error: () => this.mensajeService.msg('Error al generar el reporte', MensajeModalType.ERROR),
})
;
}
}
47 changes: 5 additions & 42 deletions src/app/components/pedidos/pedidos.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ <h1 class="h3 m-0 flex-fill text-truncate"><fa-icon [icon]="['fas', 'clipboard-l
<div class="pedidos my-2">
<ng-template #info let-item>
<div class="d-flex flex-row flex-wrap">
<span class="badge ms-2 mb-2 px-2 py-1"
[ngClass]="getEstadoValue(item.estado) === estado.ABIERTO ? 'text-bg-light border border-dark' : ''"
[class.text-bg-success]="getEstadoValue(item.estado) === estado.CERRADO"
[class.text-bg-danger]="getEstadoValue(item.estado) === estado.CANCELADO">
<span class="badge ms-2 mb-2"
[ngClass]="item.estado === estado.ABIERTO ? 'text-bg-light border border-dark' : ''"
[class.text-bg-success]="item.estado === estado.CERRADO"
[class.text-bg-danger]="item.estado === estado.CANCELADO">
{{ item.estado }}
</span>
<span class="badge text-bg-dark ms-2 mb-2 px-2 py-1">
Expand All @@ -75,44 +75,7 @@ <h1 class="h3 m-0 flex-fill text-truncate"><fa-icon [icon]="['fas', 'clipboard-l
</div>
</ng-template>
<ng-template #actions let-item>
<ul class="list-inline mb-0 text-center d-inline-block">
<li class="list-inline-item">
<a class="btn btn-primary fake-cursor" ngbPopover="Ver Detalle" triggers="mouseenter:mouseleave" placement="left"
(click)="verPedido(item)">
<fa-icon [icon]="['far', 'eye']"></fa-icon>
</a>
</li>
<li class="list-inline-item" *ngIf="puedeEditarPedido(item)">
<a class="btn btn-primary fake-cursor" ngbPopover="Editar" triggers="mouseenter:mouseleave" placement="left"
(click)="editarPedido(item)">
<fa-icon [icon]="['fas', 'pen']"></fa-icon>
</a>
</li>
<li class="list-inline-item">
<a class="btn btn-primary fake-cursor" ngbPopover="Clonar" triggers="mouseenter:mouseleave" placement="left"
(click)="clonarPedido(item)">
<fa-icon [icon]="['fas', 'copy']"></fa-icon>
</a>
</li>
<li class="list-inline-item" *ngIf="puedeElimarPedido(item)">
<a class="btn btn-primary fake-cursor" ngbPopover="Cancelar" triggers="mouseenter:mouseleave" placement="left"
(click)="cancelarPedido(item)">
<fa-icon [icon]="['fas', 'times']"></fa-icon>
</a>
</li>
<li class="list-inline-item" *ngIf="puedeFacturarPedido(item)">
<a class="btn btn-primary fake-cursor" ngbPopover="Facturar" triggers="mouseenter:mouseleave" placement="left"
(click)="facturarPedido(item)">
<fa-icon [icon]="['fas', 'cash-register']"></fa-icon>
</a>
</li>
<li class="list-inline-item" *ngIf="puedeVerFacturas(item)">
<a class="btn btn-primary fake-cursor" ngbPopover="Ver Facturas" triggers="mouseenter:mouseleave" placement="left"
(click)="verFacturas(item)">
<fa-icon [icon]="['fas', 'link']"></fa-icon>
</a>
</li>
</ul>
<app-pedido-actions-bar [pedido]="item"></app-pedido-actions-bar>
</ng-template>
<app-lista [size]="size" [items]="items" [infoTemplate]="info" [actionsTemplate]="actions"
[page]="page" [totalPages]="totalPages" [totalElements]="totalElements" (pageChange)="loadPage($event)"></app-lista>
Expand Down
Loading

0 comments on commit cbe5009

Please sign in to comment.