11import { Auth } from '@andes/auth' ;
22import { Plex } from '@andes/plex' ;
33import { Component , EventEmitter , OnDestroy , OnInit , Output , QueryList , ViewChildren } from '@angular/core' ;
4- import { combineLatest , Observable , of , Subscription } from 'rxjs' ;
4+ import { combineLatest , forkJoin , Observable , of , Subscription } from 'rxjs' ;
55import { auditTime , filter , map , switchMap , take } from 'rxjs/operators' ;
66import { IPaciente } from 'src/app/core/mpi/interfaces/IPaciente' ;
77import { ElementosRUPService } from 'src/app/modules/rup/services/elementosRUP.service' ;
@@ -25,6 +25,7 @@ import { cache } from '@andes/shared';
2525import { IMaquinaEstados } from '../../interfaces/IMaquinaEstados' ;
2626import { ListadoInternacionCapasService } from '../../views/listado-internacion-capas/listado-internacion-capas.service' ;
2727import { IObraSocial } from 'src/app/interfaces/IObraSocial' ;
28+ import { MapaCamasHTTP } from '../../services/mapa-camas.http' ;
2829
2930@Component ( {
3031 selector : 'app-ingresar-paciente' ,
@@ -94,6 +95,7 @@ export class IngresarPacienteComponent implements OnInit, OnDestroy {
9495 private subscription2 : Subscription ;
9596 public selectedOS = false ;
9697 public financiador ;
98+ public edicionFinanciador = false ;
9799 public selectorFinanciadores : IObraSocial [ ] = [ ] ;
98100 public obrasSociales : IObraSocial [ ] = [ ] ;
99101 public OSPrivada = false ;
@@ -107,6 +109,7 @@ export class IngresarPacienteComponent implements OnInit, OnDestroy {
107109 private organizacionService : OrganizacionService ,
108110 private servicioPrestacion : PrestacionesService ,
109111 public mapaCamasService : MapaCamasService ,
112+ private camasHTTP : MapaCamasHTTP ,
110113 private listadoInternacionService : ListadoInternacionService ,
111114 private historialInternacionService : ListadoInternacionCapasService ,
112115 private auth : Auth ,
@@ -472,25 +475,39 @@ export class IngresarPacienteComponent implements OnInit, OnDestroy {
472475
473476 // Sincroniza las fechas editadas entre la cama y elresumen/prestacion
474477 private sincronizarCamaInternacion ( idInternacion , fechaIngresoOriginal , fechaIngreso ) : Observable < IResumenInternacion > {
475- return this . mapaCamasService . snapshot ( this . fechaIngresoOriginal , idInternacion ) . pipe (
476- switchMap ( snapshot => {
477- const primeraCama = snapshot [ 0 ] ;
478- return this . mapaCamasService . changeTime ( primeraCama , fechaIngresoOriginal , fechaIngreso , idInternacion ) ;
479- } ) ,
478+ const obs$ = ( fechaIngresoOriginal . getTime ( ) !== fechaIngreso . getTime ( ) ) ?
479+ this . mapaCamasService . snapshot ( this . fechaIngresoOriginal , idInternacion ) . pipe (
480+ switchMap ( snapshot => {
481+ const primeraCama = snapshot [ 0 ] ;
482+ return this . mapaCamasService . changeTime ( primeraCama , fechaIngresoOriginal , fechaIngreso , idInternacion ) ;
483+ } )
484+ ) : of ( this . cama ) ;
485+
486+ return obs$ . pipe (
480487 switchMap ( cama => {
481- if ( ! cama . id ) {
488+ if ( ! cama ? .id ) {
482489 // algun error en changeTime
483490 throw new Error ( ) ;
484491 }
485- if ( this . capa === 'estadistica' ) {
486- return of ( null ) ;
487- }
488492 this . cama = cama ;
489- // Prestacion creada por capa estadistica-v2. Puede estar siendo actualizada por medica/enfermeria
490- return this . internacionResumenService . update ( cama . idInternacion , {
491- idPrestacion : this . prestacion ?. id ,
492- fechaIngreso : this . informeIngreso . fechaIngreso
493- } ) ;
493+
494+ const nextOps$ : Observable < any > [ ] = [ ] ;
495+ if ( this . capa === 'estadistica' && this . edicionFinanciador ) {
496+ nextOps$ . push ( this . camasHTTP . updateEstados ( this . mapaCamasService . ambito , this . capa , this . informeIngreso . fechaIngreso , this . cama , { edicionFinanciador : true } ) ) ;
497+ }
498+
499+ if ( this . capa !== 'estadistica' ) {
500+ // Prestacion creada por capa estadistica-v2. Puede estar siendo actualizada por medica/enfermeria
501+ nextOps$ . push ( this . internacionResumenService . update ( cama . idInternacion , {
502+ idPrestacion : this . prestacion ?. id ,
503+ fechaIngreso : this . informeIngreso . fechaIngreso
504+ } ) ) ;
505+ }
506+
507+ if ( nextOps$ . length > 0 ) {
508+ return forkJoin ( nextOps$ ) . pipe ( map ( ( ) => cama ) ) ;
509+ }
510+ return of ( cama ) ;
494511 } )
495512 ) as Observable < IResumenInternacion > ;
496513 }
@@ -510,7 +527,10 @@ export class IngresarPacienteComponent implements OnInit, OnDestroy {
510527 if ( this . prestacion && this . poseeMovimientos ) {
511528 // Se actualiza fecha y hora en camas
512529 this . cama . idInternacion = idInternacion ;
513- if ( this . informeIngreso . fechaIngreso . getTime ( ) !== this . fechaIngresoOriginal . getTime ( ) ) {
530+ const fechaChange = this . informeIngreso . fechaIngreso . getTime ( ) !== this . fechaIngresoOriginal . getTime ( ) ;
531+ const financiadorChange = this . capa === 'estadistica' && this . edicionFinanciador ;
532+
533+ if ( fechaChange || financiadorChange ) {
514534 // Recuperamos snapshot inicial, por si hay un cambio de cama
515535 this . sincronizarCamaInternacion ( idInternacion , this . fechaIngresoOriginal , this . informeIngreso . fechaIngreso ) . subscribe ( ( ) => {
516536 this . plex . info ( 'success' , 'Los datos se actualizaron correctamente' ) ;
@@ -895,5 +915,6 @@ export class IngresarPacienteComponent implements OnInit, OnDestroy {
895915 financiador : financiadorSeleccionado . financiador
896916 } ;
897917 }
918+ this . edicionFinanciador = true ;
898919 }
899920}
0 commit comments