-- Function: interfaz_cambioturnosistema() -- DROP FUNCTION interfaz_cambioturnosistema(); CREATE OR REPLACE FUNCTION interfaz_cambioturnosistema() RETURNS trigger AS $BODY$ DECLARE cursor_mangueras CURSOR FOR SELECT lado_vox as cara,manguera_vox as manguera,codigo_vox as producto FROM public.lado la inner join articulo ar on ar.codarti=la.codarti where la.estado::int=1 ORDER BY lado_vox ASC, manguera_vox ASC; fecha_actual date;hora_actual time; vproducto varchar(20);vmanguera varchar; vtotsoles numeric(18,2);vtotgalones numeric(18,3); resultado varchar;nrows integer; BEGIN nrows=0; if NEW.estado <> OLD.estado THEN if NEW.tipo='T' and NEW.categoria='C' and NEW.estado='1' then SELECT ejecuta('/usr/bin/php /var/www/html/jsonPTS/reinicia.php') into resultado; select DATE(clock_timestamp()) into fecha_actual; select clock_timestamp()::time into hora_actual; FOR row IN cursor_mangueras LOOP if row."producto"='1' then vproducto:='GASOHOL 84'; elsif row."producto"='2' then vproducto:='GASOHOL 90'; elsif row."producto"='3' then vproducto:='GASOHOL 95'; elsif row."producto"='4' then vproducto:='GASOHOL 97'; elsif row."producto"='5' then vproducto:='GNV'; elsif row."producto"='6' then vproducto:='DIESEL B5'; elsif row."producto"='7' then vproducto:='GLP'; elsif row."producto"='8' then vproducto:='GASOHOL REGULAR'; elsif row."producto"='9' then vproducto:='GASOHOL PREMIUM'; elsif row."producto"='--' then vproducto:='INDEFINIDO'; end if; select count(*) into nrows from public."TRAN" where cara=row."cara" and manguera=row."manguera" group by fecha::date,hora::time order by fecha::date desc,hora::time desc limit 1; if (nrows>0) then select total_soles,total_galones into vtotsoles,vtotgalones from public."TRAN" where cara=row."cara" and manguera=row."manguera" order by fecha::date desc,hora::time desc limit 1; insert into "TOTALIZADORES" (cara,manguera,producto,total_volumen,total_monto,fecha,hora) values (row."cara",row."manguera",vproducto,vtotgalones,vtotsoles,fecha_actual,hora_actual); else insert into "TOTALIZADORES" (cara,manguera,producto,total_volumen,total_monto,fecha,hora) values (row."cara",row."manguera",vproducto,0.001,0.001,fecha_actual,hora_actual); end if; END LOOP; END IF; END IF; update "INTERACCION" set estado=0 where tipo='T' and categoria='C'; RETURN NEW; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION interfaz_cambioturnosistema() OWNER TO postgres; ------------------------------ -- Trigger: interfaz_cambioturno on "INTERACCION" -- DROP TRIGGER interfaz_cambioturno ON "INTERACCION"; CREATE TRIGGER interfaz_cambioturno AFTER UPDATE ON "INTERACCION" FOR EACH ROW WHEN ((old.estado IS DISTINCT FROM new.estado)) EXECUTE PROCEDURE interfaz_cambioturnosistema(); ------------------------------- -- Function: ejecuta(text) -- DROP FUNCTION ejecuta(text); CREATE OR REPLACE FUNCTION ejecuta(comando text) RETURNS text AS $BODY$ use warnings; use strict; my $comando = $_[0]; my $output = `$comando`; return($output); $BODY$ LANGUAGE plperlu VOLATILE COST 100; ALTER FUNCTION ejecuta(text) OWNER TO postgres; -------------------------------------------- -- Type: interfaz_transaccion -- DROP TYPE interfaz_transaccion; CREATE TYPE interfaz_transaccion AS (outstate boolean, outid integer, outerrornumber integer, outdescription character varying); ALTER TYPE interfaz_transaccion OWNER TO postgres; -------------------------- -- Function: interfaz_patransaccion(character varying, character varying, character varying, character varying, numeric, numeric, numeric, numeric, numeric) -- DROP FUNCTION interfaz_patransaccion(character varying, character varying, character varying, character varying, numeric, numeric, numeric, numeric, numeric); CREATE OR REPLACE FUNCTION interfaz_patransaccion( infechafin character varying, insurtidor character varying, inmanguera character varying, incombustiblenom character varying, involumen numeric, inprecio numeric, invtasoles numeric, intotsoles numeric, intotgalones numeric) RETURNS SETOF interfaz_transaccion AS $BODY$ declare vState boolean default true; vID integer default 0; vError integer default 0; vDescripcion varchar default ''; lastvolumen numeric(9,3);lastsoles numeric(9,2);newvolumen numeric(18,3);solesvta numeric(9,2);volumenvta numeric(9,3); vmanguera character varying; BEGIN if inmanguera='1' then vmanguera:='A'; elsif inmanguera='2' then vmanguera:='B'; elsif inmanguera='3' then vmanguera:='C'; elsif inmanguera='4' then vmanguera:='D'; elsif inmanguera='5' then vmanguera:='E'; elsif inmanguera='6' then vmanguera:='F'; elsif inmanguera='7' then vmanguera:='G'; elsif inmanguera='8' then vmanguera:='H'; end if; /*if (intotsoles<=0) then vState:=false; vID:=0; vError:=1; vDescripcion:='Error recibiendo totalizadores'; else vState:=true; end if; */ if vState Then if not exists(select * from public."TRAN" where ((cara=insurtidor) and (manguera=vmanguera) and fecha=substring(infechafin,1,10) and hora=substring(infechafin,12,8))) then -- select total_galones,total_soles into lastvolumen,lastsoles from public."TRAN" where (cara=insurtidor and manguera=vmanguera) order by fecha desc, hora desc limit 1; --newvolumen:=lastvolumen+((intotsoles-lastsoles)/inprecio); --solesvta :=intotsoles-lastsoles; --volumenvta:=solesvta/inprecio; -- volumenvta:=invtasoles/inprecio; insert into public."TRAN" (cara,manguera,producto,soles,galones,precio,fecha,hora,total_soles,total_galones) values (insurtidor,vmanguera,trim(incombustiblenom),invtasoles,involumen,inprecio,substring(infechafin,1,10),substring(infechafin,12,8),intotsoles,intotgalones); vError:=0;vDescripcion:='Transaccion insertada'; else vState:=false;vError:=2;vDescripcion:='Transaccion ya existe...'; end if; end if; return query select vState, vID, vError, vDescripcion; END $BODY$ LANGUAGE plpgsql VOLATILE COST 100 ROWS 1000; ALTER FUNCTION interfaz_patransaccion(character varying, character varying, character varying, character varying, numeric, numeric, numeric, numeric, numeric) OWNER TO postgres; ---------------