ALTER TABLE TBCAIXABANCO_LANC_CLASS_SALDO
    ADD CHEQUE_ID INTEGER;

ALTER TABLE TBCHEQUE
    ADD ESTABELECIMENTO_ID INTEGER;
	
ALTER TABLE TBCAIXABANCO_LANC_CLASS_SALDO
    ADD COLABORADOR_CODIGO INTEGER;

ALTER TABLE TBCAIXABANCO_LANCAMENTO
    ADD CHEQUE_ID INTEGER;

ALTER TABLE TBCAIXABANCO_LANCAMENTO_REG
    ADD CHEQUE_ID INTEGER;

------------------------------------------------

alter table TBCHEQUE
alter ID position 1;

alter table TBCHEQUE
alter ESTABELECIMENTO_ID position 2;

alter table TBCHEQUE
alter SITUACAO position 3;

alter table TBCHEQUE
alter DATA_RECEBIMENTO position 4;

alter table TBCHEQUE
alter BANCO_CODIGO position 5;

alter table TBCHEQUE
alter AGENCIA position 6;

alter table TBCHEQUE
alter CONTA position 7;

alter table TBCHEQUE
alter CONTA_DIGITO position 8;

alter table TBCHEQUE
alter NUMERO_CHEQUE position 9;

alter table TBCHEQUE
alter NOME_TITULAR position 10;

alter table TBCHEQUE
alter DATA_CHEQUE position 11;

alter table TBCHEQUE
alter VALOR position 12;

alter table TBCHEQUE
alter OBSERVACAO position 13;

-------------------------------------------------------------------

SET TERM ^ ;

create or alter function FN_TO_JSON_V3 (
    V00 blob sub_type 1 segment size 80 = '',
    V01 blob sub_type 1 segment size 80 = '',
    V02 blob sub_type 1 segment size 80 = '',
    V03 blob sub_type 1 segment size 80 = '',
    V04 blob sub_type 1 segment size 80 = '',
    V05 blob sub_type 1 segment size 80 = '',
    V06 blob sub_type 1 segment size 80 = '',
    V07 blob sub_type 1 segment size 80 = '',
    V08 blob sub_type 1 segment size 80 = '',
    V09 blob sub_type 1 segment size 80 = '',
    V10 blob sub_type 1 segment size 80 = '',
    V11 blob sub_type 1 segment size 80 = '',
    V12 blob sub_type 1 segment size 80 = '',
    V13 blob sub_type 1 segment size 80 = '',
    V14 blob sub_type 1 segment size 80 = '',
    V15 blob sub_type 1 segment size 80 = '',
    V16 blob sub_type 1 segment size 80 = '',
    V17 blob sub_type 1 segment size 80 = '',
    V18 blob sub_type 1 segment size 80 = '',
    V19 blob sub_type 1 segment size 80 = '',
    V20 blob sub_type 1 segment size 80 = '',
    V21 blob sub_type 1 segment size 80 = '',
    V22 blob sub_type 1 segment size 80 = '',
    V23 blob sub_type 1 segment size 80 = '',
    V24 blob sub_type 1 segment size 80 = '',
    V25 blob sub_type 1 segment size 80 = '',
    V26 blob sub_type 1 segment size 80 = '',
    V27 blob sub_type 1 segment size 80 = '',
    V28 blob sub_type 1 segment size 80 = '',
    V29 blob sub_type 1 segment size 80 = '',
    V30 blob sub_type 1 segment size 80 = '',
    V31 blob sub_type 1 segment size 80 = '',
    V32 blob sub_type 1 segment size 80 = '',
    V33 blob sub_type 1 segment size 80 = '',
    V34 blob sub_type 1 segment size 80 = '',
    V35 blob sub_type 1 segment size 80 = '',
    V36 blob sub_type 1 segment size 80 = '',
    V37 blob sub_type 1 segment size 80 = '',
    V38 blob sub_type 1 segment size 80 = '',
    V39 blob sub_type 1 segment size 80 = '',
    V40 blob sub_type 1 segment size 80 = '',
    V41 blob sub_type 1 segment size 80 = '',
    V42 blob sub_type 1 segment size 80 = '',
    V43 blob sub_type 1 segment size 80 = '',
    V44 blob sub_type 1 segment size 80 = '',
    V45 blob sub_type 1 segment size 80 = '',
    V46 blob sub_type 1 segment size 80 = '',
    V47 blob sub_type 1 segment size 80 = '',
    V48 blob sub_type 1 segment size 80 = '',
    V49 blob sub_type 1 segment size 80 = ''
    )
returns blob sub_type 1 segment size 80
AS
declare variable json blob sub_type 1 segment size 80;
begin
 JSON = '';

 if(char_length(V00) > 0)then begin JSON = JSON||','||V00; end
 if(char_length(V01) > 0)then begin JSON = JSON||','||V01; end
 if(char_length(V02) > 0)then begin JSON = JSON||','||V02; end
 if(char_length(V03) > 0)then begin JSON = JSON||','||V03; end
 if(char_length(V04) > 0)then begin JSON = JSON||','||V04; end
 if(char_length(V05) > 0)then begin JSON = JSON||','||V05; end
 if(char_length(V06) > 0)then begin JSON = JSON||','||V06; end
 if(char_length(V07) > 0)then begin JSON = JSON||','||V07; end
 if(char_length(V08) > 0)then begin JSON = JSON||','||V08; end
 if(char_length(V09) > 0)then begin JSON = JSON||','||V09; end
 if(char_length(V10) > 0)then begin JSON = JSON||','||V10; end
 if(char_length(V11) > 0)then begin JSON = JSON||','||V11; end
 if(char_length(V12) > 0)then begin JSON = JSON||','||V12; end
 if(char_length(V13) > 0)then begin JSON = JSON||','||V13; end
 if(char_length(V14) > 0)then begin JSON = JSON||','||V14; end
 if(char_length(V15) > 0)then begin JSON = JSON||','||V15; end
 if(char_length(V16) > 0)then begin JSON = JSON||','||V16; end
 if(char_length(V17) > 0)then begin JSON = JSON||','||V17; end
 if(char_length(V18) > 0)then begin JSON = JSON||','||V18; end
 if(char_length(V19) > 0)then begin JSON = JSON||','||V19; end
 if(char_length(V20) > 0)then begin JSON = JSON||','||V20; end
 if(char_length(V21) > 0)then begin JSON = JSON||','||V21; end
 if(char_length(V22) > 0)then begin JSON = JSON||','||V22; end
 if(char_length(V23) > 0)then begin JSON = JSON||','||V23; end
 if(char_length(V24) > 0)then begin JSON = JSON||','||V24; end
 if(char_length(V25) > 0)then begin JSON = JSON||','||V25; end
 if(char_length(V26) > 0)then begin JSON = JSON||','||V26; end
 if(char_length(V27) > 0)then begin JSON = JSON||','||V27; end
 if(char_length(V28) > 0)then begin JSON = JSON||','||V28; end
 if(char_length(V29) > 0)then begin JSON = JSON||','||V29; end
 if(char_length(V30) > 0)then begin JSON = JSON||','||V30; end
 if(char_length(V31) > 0)then begin JSON = JSON||','||V31; end
 if(char_length(V32) > 0)then begin JSON = JSON||','||V32; end
 if(char_length(V33) > 0)then begin JSON = JSON||','||V33; end
 if(char_length(V34) > 0)then begin JSON = JSON||','||V34; end
 if(char_length(V35) > 0)then begin JSON = JSON||','||V35; end
 if(char_length(V36) > 0)then begin JSON = JSON||','||V36; end
 if(char_length(V37) > 0)then begin JSON = JSON||','||V37; end
 if(char_length(V38) > 0)then begin JSON = JSON||','||V38; end
 if(char_length(V39) > 0)then begin JSON = JSON||','||V39; end
 if(char_length(V40) > 0)then begin JSON = JSON||','||V40; end
 if(char_length(V41) > 0)then begin JSON = JSON||','||V41; end
 if(char_length(V42) > 0)then begin JSON = JSON||','||V42; end
 if(char_length(V43) > 0)then begin JSON = JSON||','||V43; end
 if(char_length(V44) > 0)then begin JSON = JSON||','||V44; end
 if(char_length(V45) > 0)then begin JSON = JSON||','||V45; end
 if(char_length(V46) > 0)then begin JSON = JSON||','||V46; end
 if(char_length(V47) > 0)then begin JSON = JSON||','||V47; end
 if(char_length(V48) > 0)then begin JSON = JSON||','||V48; end
 if(char_length(V49) > 0)then begin JSON = JSON||','||V49; end

 JSON = '{'||JSON||'}';
 JSON = replace(JSON,'{,','{');
 JSON = replace(JSON,',}','}');

 return coalesce(JSON,'{}');
end
^

SET TERM ; ^

--------------------------------------------------------------------------------------

SET TERM ^ ;

create or alter function FN_TO_PAR_JSON_V3 (
    CAMPO varchar(50),
    VALOR blob sub_type 1 segment size 80)
returns blob sub_type 1 segment size 80
AS
begin
  /* Function Text */
  return '"'||CAMPO||'":"'|| replace(replace(replace(replace(coalesce(VALOR,''),'"',''),'"',''),'"',''),'"','')||'"';
end
^

SET TERM ; ^

-----------------------------------------------------------------------------------------

SET TERM ^ ;

CREATE OR ALTER trigger ttbcaixabanco_lanc_class_sa_b2d for tbcaixabanco_lanc_class_saldo
active before delete position 2
AS
BEGIN

  if (OLD.SALDO < OLD.VALOR) Then
      Begin
      Exception "..." 'REGISTRO NAO PODE SER EXCLUIDO! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .';
      End

    IF(OLD.CHEQUE_ID > 0)THEN
    BEGIN
        UPDATE TBCHEQUE SET SITUACAO = 3 WHERE ID = OLD.CHEQUE_ID;
    END

END
^

SET TERM ; ^