diff --git a/Database/scripts/factuges.sql b/Database/scripts/factuges.sql index c48bd788..8131eab7 100644 --- a/Database/scripts/factuges.sql +++ b/Database/scripts/factuges.sql @@ -213,48 +213,154 @@ SET TERM ^ ; /**** Stored Procedures ****/ /******************************************************************************/ -CREATE PROCEDURE PRO_PRES_CAPITULOS ( - AID INTEGER) -RETURNS ( - ID INTEGER, - ID_PRESUPUESTO INTEGER, - POSICION INTEGER, - TIPO_DETALLE VARCHAR(10), - CONCEPTO VARCHAR(2000), - IMPORTE_TOTAL NUMERIC(11,2), - VISIBLE SMALLINT) -AS -BEGIN - SUSPEND; -END^ +SET TERM ^ ; +CREATE PROCEDURE PRO_PRES_CAPITULOS ( + aid integer) +returns ( + id integer, + id_presupuesto integer, + posicion integer, + tipo_detalle varchar(10), + concepto varchar(2000), + importe_total numeric(11,2), + visible smallint) +as +declare variable num_filas integer; +declare variable contador integer; +declare variable existe numeric(11,2); +declare variable total_acumulado numeric(11,2); +begin + existe = 0; + total_acumulado = 0.0; + contador = 0; + num_filas = 0; + + /* żExiste el presupuesto? */ + for select count(*) + from presupuestos_cliente_detalles + where id_presupuesto = :AID + into :num_filas + do + begin + if (num_filas = 0) then + suspend; + end + + /* Ver si hay conceptos al principio sin capitulos */ + for select id, id_presupuesto, posicion, tipo_detalle + from presupuestos_cliente_detalles + where id_presupuesto = :AID + order by id_presupuesto, posicion + rows 1 + into :ID, :ID_PRESUPUESTO, :POSICION, :TIPO_DETALLE + do + begin + if (TIPO_DETALLE = 'Concepto') then + EXISTE = 1; + end + + if (existe = 1) then + begin + contador = 0; + /* Existen conceptos sin capitulo */ + for select tipo_detalle, importe_total, coalesce(visible, 1) + from presupuestos_cliente_detalles + where id_presupuesto = :AID + order by id_presupuesto, posicion + into :TIPO_DETALLE, :IMPORTE_TOTAL, :VISIBLE + do + begin + contador = contador + 1; + if ((tipo_detalle <> 'Concepto') or (contador = num_filas)) then + begin + importe_total = total_acumulado; + tipo_detalle = 'Titulo'; + concepto = 'General'; + visible = 1; + ID = -1; + posicion = -1; + suspend; + break; + end + else begin + if (visible <> 0) then + total_acumulado = total_acumulado + importe_total; + end + end + end + + + for select id, id_presupuesto, posicion, tipo_detalle, F_RTFTOTEXT(concepto) as concepto, + importe_total, coalesce(visible, 1) + from presupuestos_cliente_detalles + where tipo_detalle <> 'Concepto' and id_presupuesto = :AID + order by id_presupuesto, posicion + into :ID, :ID_PRESUPUESTO, :POSICION, :TIPO_DETALLE, :CONCEPTO, + :IMPORTE_TOTAL, :VISIBLE + do + suspend; + +end^ + +SET TERM ; ^ +GRANT SELECT ON PRESUPUESTOS_CLIENTE_DETALLES TO PROCEDURE PRO_PRES_CAPITULOS; +GRANT EXECUTE ON PROCEDURE PRO_PRES_CAPITULOS TO SYSDBA; + + + +SET TERM ^ ; CREATE PROCEDURE PRO_PRES_CAPITULOS_CONCEPTOS ( - AID INTEGER) -RETURNS ( - ID INTEGER, - ID_PRESUPUESTO INTEGER, - POSICION INTEGER, - TIPO_DETALLE VARCHAR(10), - ID_CAPITULO INTEGER, - ID_ARTICULO INTEGER, - REFERENCIA VARCHAR(255), - CONCEPTO VARCHAR(2000), - CANTIDAD INTEGER, - IMPORTE_UNIDAD NUMERIC(11,2), - DESCUENTO FLOAT, - IMPORTE_PORTE NUMERIC(11,2), - IMPORTE_TOTAL NUMERIC(11,2), - VISIBLE SMALLINT) -AS -BEGIN - SUSPEND; -END^ - - + aid integer) +returns ( + id integer, + id_presupuesto integer, + posicion integer, + tipo_detalle varchar(10), + id_capitulo integer, + id_articulo integer, + referencia varchar(255), + concepto varchar(2000), + cantidad integer, + importe_unidad numeric(11,2), + descuento float, + importe_porte numeric(11,2), + importe_total numeric(11,2), + visible smallint) +as +declare variable capitulo_actual integer; +begin +capitulo_actual = -1; +ID_CAPITULO = -1; +for select pre.id, pre.id_presupuesto, pre.posicion, pre.tipo_detalle, + pre.id_articulo, articulos.referencia, pre.concepto, pre.cantidad, + pre.importe_unidad, pre.descuento, pre.importe_porte, + pre.importe_total, coalesce(pre.visible, 1) + from presupuestos_cliente_detalles pre + left join articulos on (pre.id_articulo = articulos.id) + where pre.id_presupuesto = :AID + order by pre.id_presupuesto, pre.posicion + into :ID, :ID_PRESUPUESTO, :POSICION, :TIPO_DETALLE, :ID_ARTICULO, + :REFERENCIA, :CONCEPTO, :CANTIDAD, :IMPORTE_UNIDAD, :DESCUENTO, + :IMPORTE_PORTE, :IMPORTE_TOTAL, :VISIBLE +do + begin + if (:tipo_detalle <> 'Concepto') then + begin + capitulo_actual = :ID; + ID_CAPITULO = capitulo_actual; + end + else + suspend; + end +end^ SET TERM ; ^ +GRANT SELECT ON PRESUPUESTOS_CLIENTE_DETALLES TO PROCEDURE PRO_PRES_CAPITULOS_CONCEPTOS; +GRANT SELECT ON ARTICULOS TO PROCEDURE PRO_PRES_CAPITULOS_CONCEPTOS; +GRANT EXECUTE ON PROCEDURE PRO_PRES_CAPITULOS_CONCEPTOS TO SYSDBA; /******************************************************************************/ /**** Tables ****/