Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DROP FUNCTION pa_facturacion_eliminacionprevia( idproyecto character varying, anho smallint, mes smallint, idusuario integer, OUT rpta character varying, OUT titulomsje character varying, OUT contenidomsje character varying);
  2.  
  3.  
  4. CREATE FUNCTION pa_facturacion_eliminacionprevia(
  5.     tipofacturacion smallint,
  6.     idproyecto character varying,
  7.     idpropiedad character varying,
  8.     anho smallint,
  9.     mes smallint,
  10.     idusuario integer
  11.     OUT rpta character varying,
  12.     OUT titulomsje character varying,
  13.     OUT contenidomsje character varying)
  14.     RETURNS record
  15.     LANGUAGE plpgsql
  16.     AS $$
  17.  
  18. DECLARE FACTURAS integer;
  19.  
  20. BEGIN
  21.     IF (SELECT COUNT(*) FROM tm_proceso WHERE tm_idestadoproceso  = 1 AND tm_idproyecto = idproyecto AND tm_per_ano = anho AND tm_per_mes = mes AND activo = 1) = 0 THEN
  22.       rpta := '0';
  23.       titulomsje := 'El proceso actual no ha sido abierto';
  24.       contenidomsje := 'La operación no se puede realizar';
  25.       RETURN;
  26.     END IF;
  27.  
  28.             IF tipoFacturacion = 1 THEN          
  29.  
  30.                 SELECT COUNT(*) INTO FACTURAS from tm_facturacion WHERE tm_idproyecto = idproyecto AND tm_per_ano = anho AND tm_per_mes = mes AND activo = 1;
  31.  
  32.                 IF FACTURAS > 0 THEN
  33.  
  34.                     UPDATE td_facturacionpropietario fprop SET activo = 0 FROM tm_facturacion fac
  35.                     WHERE fac.tm_idproyecto = idproyecto AND fac.tm_per_ano = anho AND fac.tm_per_mes = mes AND fac.tm_idfacturacion = fprop.tm_idfacturacion AND fprop.activo = 1;
  36.  
  37.                     UPDATE tm_facturacion SET activo = 0  WHERE tm_idproyecto = idproyecto AND tm_per_ano = anho AND tm_per_mes = mes AND activo = 1;
  38.            
  39.                     UPDATE td_conceptosfacturacion SET activo = 0  WHERE tm_idproyecto = idproyecto AND tm_per_ano = anho AND tm_per_mes = mes AND activo = 1;
  40.  
  41.                 END IF;
  42.             ELSE
  43.  
  44.                 SELECT COUNT(*) INTO FACTURAS FROM td_conceptosfacturacion where tm_idpropiedad  = idpropiedad and tm_per_ano = anho AND tm_per_mes = mes AND activo = 1
  45.                
  46.                 IF FACTURAS >  0 THEN
  47.                    
  48.                     UPDATE td_facturacionpropietario SET activo = 0
  49.                     WHERE tm_idfacturacion in (select DISTINCT(tm_idfacturacion) from td_conceptosfacturacion where tm_idpropiedad  = idpropiedad and tm_per_ano = anho AND tm_per_mes = mes AND activo = 1  );
  50.  
  51.                     UPDATE tm_facturacion SET activo = 0  
  52.                     WHERE  tm_idfacturacion in (select DISTINCT(tm_idfacturacion) from td_conceptosfacturacion where tm_idpropiedad  = idpropiedad and tm_per_ano = anho AND tm_per_mes = mes AND activo = 1  );
  53.  
  54.                     UPDATE td_conceptosfacturacion SET activo = 0 WHERE tm_idpropiedad  = idpropiedad and tm_per_ano = anho AND tm_per_mes = mes AND activo = 1  
  55.  
  56.                 END IF
  57.             END IF;
  58.  
  59.     rpta := '1';
  60.     titulomsje := '';
  61.     contenidomsje := '';
  62.     RETURN;
  63. END;
  64. $$;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement