-- drop function if exists par.set_trans_credito_debito(integer)createorreplacefunctionpar.set_trans_credito_debito(i_odeidinteger)returnsvoidlanguageplpgsqlas$$declareromid_arrayinteger[]:=ARRAY(SELECTromidFROMpar.responsaveisomissowhereodeid=i_odeidorderbydtfimmandato);valor_debitonumeric(20,2);data_debitotimestamp;beginFORiIN1..(array_length(romid_array,1)-1)LOOPif((selectcoalesce(valorcredito,'0.00')frompar.responsaveisomissowhereromid=romid_array[i])>0)thenselectvalorcredito,datacreditointovalor_debito,data_debitofrompar.responsaveisomissowhereromid=romid_array[i];updatepar.responsaveisomissosetdatadebito=dtiniciomandato,valordebito=valor_debitowhereromid=romid_array[i+1];endif;ENDLOOP;updatepar.responsaveisomissosetromstatus='A',carga='CREDITOVIRADEBITO'whereodeid=i_odeid;end;$$;