-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(IN): implementa periodos censables en internacion
- Loading branch information
Showing
7 changed files
with
283 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
src/app/apps/rup/mapa-camas/sidebar/periodos-censables/periodos-censables.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import { Plex } from '@andes/plex'; | ||
import { Component, Input, OnInit } from '@angular/core'; | ||
import { PrestacionesService } from 'src/app/modules/rup/services/prestaciones.service'; | ||
import { MapaCamasService } from '../../services/mapa-camas.service'; | ||
|
||
interface Periodo { | ||
desde: moment.Moment; | ||
hasta: moment.Moment; | ||
} | ||
|
||
@Component({ | ||
selector: 'app-periodos-censables', | ||
templateUrl: './periodos-censables.html', | ||
styleUrls: ['./periodos-censables.scss'] | ||
}) | ||
|
||
export class PeriodosCensablesComponent implements OnInit { | ||
prestacion; | ||
agregar = false; | ||
nuevoPeriodo: Periodo; | ||
periodos: Periodo[] = []; | ||
error = null; | ||
camaEsCensable = false; | ||
|
||
@Input() editable; | ||
|
||
constructor( | ||
private mapaCamasService: MapaCamasService, | ||
private servicioPrestacion: PrestacionesService, | ||
private plex: Plex, | ||
) { } | ||
|
||
ngOnInit() { | ||
this.mapaCamasService.prestacion$.subscribe((prestacion) => { | ||
this.prestacion = prestacion; | ||
|
||
const primerRegistro = prestacion?.ejecucion?.registros[0]; | ||
|
||
if (!!primerRegistro?.esCensable) { | ||
this.periodos = this.prestacion?.periodosCensables; | ||
|
||
if (!this.periodos.length) { | ||
const fechaInicio = primerRegistro.createdAt; | ||
|
||
this.periodos.push({ desde: moment(fechaInicio), hasta: moment() }); | ||
} | ||
} | ||
}); | ||
|
||
this.initNuevoPeriodo(); | ||
} | ||
|
||
private initNuevoPeriodo() { | ||
this.nuevoPeriodo = { | ||
desde: moment(), | ||
hasta: null | ||
}; | ||
} | ||
|
||
private periodoInvalido(desde, hasta) { | ||
if (desde > hasta) { | ||
this.error = 'El periodo elegido es inválido'; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private periodoDuplicado(desde, hasta) { | ||
if (this.periodos.find((periodo) => moment(periodo.desde)?.isSame(desde, 'day') && moment(periodo.hasta)?.isSame(hasta, 'day'))) { | ||
this.error = 'El periodo ingresado ya existe'; | ||
return true; | ||
}; | ||
|
||
return false; | ||
} | ||
|
||
public periodoSuperpuesto(desde, hasta) { | ||
let superpuesto = false; | ||
const existeIlimitado = this.periodos.some(periodo => periodo.hasta === null); | ||
|
||
if (!hasta && existeIlimitado) { | ||
superpuesto = true; | ||
} | ||
|
||
for (const periodo of this.periodos) { | ||
if (hasta) { | ||
if (desde < periodo.hasta && hasta > periodo.desde) { | ||
superpuesto = true; | ||
} | ||
} else { | ||
if (desde <= periodo.hasta) { | ||
superpuesto = true; | ||
} | ||
} | ||
} | ||
|
||
if (superpuesto) { | ||
this.error = 'El periodo no debe superponerse a otro'; | ||
} | ||
|
||
return superpuesto; | ||
} | ||
|
||
private guardarPrestacion() { | ||
const registros = this.prestacion.ejecucion.registros; | ||
|
||
if (!!this.periodos?.length) { | ||
registros[0].esCensable = true; | ||
} else { | ||
delete registros[0].esCensable; | ||
} | ||
|
||
const params: any = { | ||
op: 'periodosCensables', | ||
periodosCensables: this.periodos, | ||
registros, | ||
}; | ||
|
||
return this.servicioPrestacion.patch(this.prestacion.id, params); | ||
} | ||
|
||
private addNuevo(nuevoPeriodo) { | ||
this.error = null; | ||
this.periodos.push(nuevoPeriodo); | ||
this.initNuevoPeriodo(); | ||
} | ||
|
||
private addPeriodoSinLimite(desde) { | ||
const nuevoPeriodo = { desde, hasta: null }; | ||
|
||
if (!this.periodoSuperpuesto(desde, null)) { | ||
this.addNuevo(nuevoPeriodo); | ||
} | ||
} | ||
|
||
public addPeriodoLimitado(desde, hasta) { | ||
if (!this.periodoInvalido(desde, hasta)) { | ||
if (!this.periodoDuplicado(desde, hasta) && !this.periodoSuperpuesto(desde, hasta)) { | ||
this.addNuevo({ ...this.nuevoPeriodo, hasta }); | ||
} | ||
} | ||
} | ||
|
||
public agregarPeriodo() { | ||
const { desde, hasta } = this.nuevoPeriodo; | ||
|
||
if (hasta) { | ||
if (!this.periodoInvalido(desde, hasta)) { | ||
this.addPeriodoLimitado(desde, hasta); | ||
} | ||
} else { | ||
this.addPeriodoSinLimite(desde); | ||
} | ||
} | ||
|
||
public toggleAgregar() { | ||
this.agregar = !this.agregar; | ||
} | ||
|
||
public eliminarPeriodo(index) { | ||
this.periodos?.splice(index, 1); | ||
this.error = null; | ||
|
||
this.guardarPrestacion().subscribe({ | ||
error: () => { | ||
this.plex.toast('danger', 'Ha ocurrido un error al eliminar el periodo'); | ||
} | ||
}); | ||
} | ||
|
||
public guardar() { | ||
this.guardarPrestacion().subscribe({ | ||
complete: () => { | ||
this.plex.info('success', 'Periodos guardados exitosamente'); | ||
this.toggleAgregar(); | ||
}, | ||
error: () => { | ||
this.plex.toast('danger', 'Ha ocurrido un error al guardar el periodo'); | ||
} | ||
}); | ||
} | ||
|
||
public cancelar() { | ||
this.agregar = false; | ||
this.error = null; | ||
this.initNuevoPeriodo(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/app/apps/rup/mapa-camas/sidebar/periodos-censables/periodos-censables.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<ng-container> | ||
<plex-title size="sm" justify titulo="PERIODOS CENSABLES"> | ||
<plex-button *ngIf="!agregar && editable" icon="pencil" type="warning" size="sm" class="mr-1" (click)="toggleAgregar()"> | ||
</plex-button> | ||
<plex-button *ngIf="agregar" label="Guardar" type="success" size="sm" class="mr-1" (click)="guardar()">Guardar | ||
</plex-button> | ||
<plex-button *ngIf="agregar" icon="close" type="danger" size="sm" class="mr-1" (click)="cancelar()"> | ||
</plex-button> | ||
</plex-title> | ||
<div *ngIf="agregar"> | ||
<div class="nuevo-periodo"> | ||
<div class="fechas-periodo"> | ||
<plex-datetime type="date" label="Inicio de censo" [(ngModel)]="nuevoPeriodo.desde" name="fechaDesde" | ||
grow="full" [max]="nuevoPeriodo.hasta"> | ||
</plex-datetime> | ||
<plex-datetime type="date" label="Fin de censo" [(ngModel)]="nuevoPeriodo.hasta" name="fechaHasta" | ||
grow="full" [min]="nuevoPeriodo.desde"> | ||
</plex-datetime> | ||
<plex-button icon="check" type="success" size="md" (click)="agregarPeriodo()" | ||
class="mr-1"></plex-button> | ||
</div> | ||
<div class="periodo-controls mt-2"> | ||
<span *ngIf="error" class="periodo-error text-danger">{{ error }}</span> | ||
</div> | ||
|
||
</div> | ||
</div> | ||
<div *ngIf="periodos?.length" class="lista-periodos"> | ||
<div *ngFor="let periodo of periodos; let i = index" class="periodo"> | ||
<plex-label titulo="Desde" subtitulo="{{periodo.desde | fecha}}"> | ||
</plex-label> | ||
<plex-label titulo="Hasta" subtitulo="{{periodo.hasta ? (periodo.hasta | fecha) : 'Sin fecha'}}"> | ||
</plex-label> | ||
<div> | ||
<plex-button *ngIf="agregar" type="danger" tooltip="Eliminar periodo" tooltipPosition="left" size="sm" | ||
icon="delete" (click)="eliminarPeriodo(i)" ariaLabel="Eliminar periodo"> | ||
</plex-button> | ||
</div> | ||
</div> | ||
</div> | ||
<div justify="center" *ngIf="!agregar && !periodos?.length"> | ||
<plex-label class="mt-2" direction="column" size="md" icon="" titulo="No hay periodos censables cargados" | ||
icon="calendario-rango-bold"> | ||
</plex-label> | ||
</div> | ||
</ng-container> |
32 changes: 32 additions & 0 deletions
32
src/app/apps/rup/mapa-camas/sidebar/periodos-censables/periodos-censables.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
.nuevo-periodo { | ||
display: flex; | ||
flex-direction: column; | ||
margin-top: 10px; | ||
} | ||
|
||
.periodo-controls { | ||
display: flex; | ||
justify-content: flex-end; | ||
|
||
.periodo-error { | ||
display: flex; | ||
flex-grow: 1; | ||
} | ||
} | ||
|
||
.fechas-periodo { | ||
display: flex; | ||
column-gap: 5px; | ||
align-items: flex-end; | ||
} | ||
|
||
.lista-periodos { | ||
margin-top: 10px; | ||
|
||
.periodo { | ||
display: flex; | ||
padding: 5px; | ||
align-items: center; | ||
justify-content: space-between; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters