unit uconfRelGerencia; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, System.IniFiles, System.UITypes, System.DateUtils, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, umstMaster, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls, Vcl.DBCtrls, Data.DB, ZAbstractRODataset, ZDataset; type TconfRelGerencia = class(TmstMaster) gb_data: TGroupBox; Label1: TLabel; dtp_inicio: TDateTimePicker; dtp_fim: TDateTimePicker; btn_imprimir: TButton; rg_tipo: TRadioGroup; lbl_emp: TLabel; lbl_tit: TLabel; GroupBox1: TGroupBox; CheckBox1: TCheckBox; DBLookupComboBox1: TDBLookupComboBox; ZReadOnlyQuery1: TZReadOnlyQuery; Button1: TButton; CheckBox2: TCheckBox; pbRel: TProgressBar; lblProgress: TLabel; procedure btn_imprimirClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure CheckBox1Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure CheckBox2Click(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private sqlEmp: string; { Private declarations } public { Public declarations } end; var confRelGerencia: TconfRelGerencia; implementation {$R *.dfm} uses urelPrestacaoAnaSin, udtmSystem, urelPrestacaoAnaSin2, urelMulti; procedure TconfRelGerencia.btn_imprimirClick(Sender: TObject); var soma_p, soma_j, soma_m, soma_e, soma_c, soma_d, soma_r, soma_rp: currency; id_emp, i, qtde_mes, counter: integer; d1, d2, str_dep, sql_emp: string; faltando: boolean; Dh1, M1, Y1: Word; Dh2, M2, Y2: Word; Dh3, M3, Y3: Word; d3, d4, firstDate: TDate; d: integer; begin if rg_tipo.ItemIndex = -1 then begin rg_tipo.ItemIndex := 0; end; if (rg_tipo.ItemIndex = 0) and (CheckBox2.Checked) then begin Messagedlg ('Você selecionou relátorio analítico e sintético ao mesmo tempo. Isso não pode ser realizado.', mtWarning, [mbok], 0); abort; end; Screen.Cursor := crHourGlass; with dtmSystem do begin lblProgress.Caption := 'Filtrando empresas.'; pbRel.Position := 0; pbRel.Max := 1; if not tblEmpresaRepasse.Active then begin tblEmpresaRepasse.Open; end; DecodeDate(dtp_inicio.Date, Y1, M1, Dh1); d3 := StrToDate('01/' + inttostr(M1) + '/' + inttostr(Y1)); DecodeDate(dtp_fim.Date, Y2, M2, Dh2); d4 := StrToDate('01/' + inttostr(M2) + '/' + inttostr(Y2)); qtde_mes := MonthsBetween(d3, d4) + 1; counter := 0; sql_emp := tblCHGEmpresa.SQL.Text; if CheckBox1.Checked then begin if CheckBox2.Checked then begin tblCHGEmpresa.SQL.Text := 'select * from chg_empresas where id_empresa in (select id_empresa from chg_titulos' + ' where cast(dt_pagamento as date) >= cast(' + QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_inicio.Date)) + ' as date) and' + ' cast(dt_pagamento as date) <= cast(' + QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_fim.Date)) + ' as date)) and id_departamento in (select ID_DEPARTAMENTOS from chg_departamentos)'; end else begin tblCHGEmpresa.SQL.Text := 'select * from chg_empresas where id_empresa in (select id_empresa from chg_titulos' + ' where cast(dt_pagamento as date) >= cast(' + QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_inicio.Date)) + ' as date) and' + ' cast(dt_pagamento as date) <= cast(' + QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_fim.Date)) + ' as date)) and id_departamento = ' + inttostr(DBLookupComboBox1.KeyValue); end; end else begin tblCHGEmpresa.SQL.Text := 'select * from chg_empresas where id_empresa in (select id_empresa from chg_titulos' + ' where cast(dt_pagamento as date) >= cast(' + QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_inicio.Date)) + ' as date) and' + ' cast(dt_pagamento as date) <= cast(' + QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_fim.Date)) + ' as date))'; end; tblCHGEmpresa.Open; lblProgress.Caption := 'Empresas Filtradas.'; pbRel.Step := 1; Sleep(1000); pbRel.Position := 0; pbRel.Max := tblCHGEmpresa.RecordCount; lblProgress.Caption := 'Verificando repasses.'; tblCHGEmpresa.First; while not tblCHGEmpresa.Eof do begin firstDate := StartOfTheMonth(d3); tblEmpresaRepasse.SQL.Clear; tblEmpresaRepasse.SQL.Text := 'SELECT * FROM CHG_EMPRESA_REPASSE WHERE ID_EMPRESA =' + inttostr(id_credor) + ' order by ID_ANO, ID_MES'; tblEmpresaRepasse.Open; tblEmpresaRepasse.Filtered := False; tblEmpresaRepasse.Filter := '(ID_ANO >= ' + inttostr(Y1) + ' and ID_ANO <= ' + inttostr(Y2) + ')'; tblEmpresaRepasse.Filtered := True; for d := 0 to qtde_mes - 1 do begin DecodeDate(firstDate, Y3, M3, Dh3); faltando := tblEmpresaRepasse.Locate('ID_ANO;ID_MES', VarArrayOf([Y3, M3]), []); firstDate := IncMonth(firstDate, 1); if faltando = False then begin Messagedlg('A empresa ' + tblCHGEmpresaTX_NOME.AsString + ' não possui modelos de repasse para todos os mêses dentro do período do pagamento mais antigo e o mais novo.' + ' Por favor crie os modelos de repasses que faltam antes de realizar esta operação (Entre ' + meses[M1] + ' de ' + inttostr(Y1) + ' e ' + meses[M2] + ' de ' + inttostr(Y2) + '.', mtWarning, [mbok], 0); tblCHGEmpresa.SQL.Text := sql_emp; tblEmpresaRepasse.Filtered := False; tblCHGEmpresa.Open; pbRel.Max := pbRel.Position - 1; pbRel.StepIt; lblProgress.Caption := 'Há empresas com repasses faltando no período selecionado.'; Close; abort; end; end; tblCHGEmpresa.Next; pbRel.StepIt; end; end; lblProgress.Caption := 'Repasses Verificados.'; pbRel.Position := 0; Sleep(1000); str_dep := ''; if rg_tipo.ItemIndex = 0 then begin pbRel.Max := 2; relPrestacaoAnaSin := TrelPrestacaoAnaSin.Create(self); with relPrestacaoAnaSin do begin // RLMemo1.Lines[0] := StringReplace(RLMemo1.Lines[0], 'DT1', // FormatDateTime('dd/mm/yyyy', dtp_inicio.Date), // [rfReplaceAll, rfIgnoreCase]); // RLMemo1.Lines[0] := StringReplace(RLMemo1.Lines[0], 'DT2', // FormatDateTime('dd/mm/yyyy', dtp_fim.Date), // [rfReplaceAll, rfIgnoreCase]); RLMemo1.Lines[0] := StringReplace(RLMemo1.Lines[0], 'DT1', FormatDateTime('dd/mm/yyyy', dtp_inicio.Date), [rfReplaceAll, rfIgnoreCase]); RLMemo1.Lines[0] := StringReplace(RLMemo1.Lines[0], 'DT2', FormatDateTime('dd/mm/yyyy', dtp_fim.Date), [rfReplaceAll, rfIgnoreCase]); lbl_emp.Caption := 'Pesquisando títulos.'; Application.ProcessMessages; if CheckBox1.Checked then begin if CheckBox2.Checked then begin frlbl_dep.Caption := 'Todos departamentos.'; end else begin frlbl_dep.Caption := 'Dep.: ' + dtmSystem.tblDepartamentosTX_NOME.AsString; end; end; lblProgress.Caption := 'Preparando Empresas.'; with zroqryCredores.SQL do begin Clear; add('select distinct'); add('e.*'); add('from chg_devedores A'); add('inner join chg_titulos B on (A.id_devedor = B.id_devedor)'); add('inner join chg_empresas e on (E.id_empresa = B.id_empresa)'); add('where (B.tp_situacao = ' + QuotedStr('QC') + ' or B.tp_situacao = ' + QuotedStr('B') + ')'); add('and'); add('CAST(B.dt_pagamento AS DATE) >= '); add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_inicio.Date))); add('and'); add('CAST(B.dt_pagamento AS DATE) <= '); add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_fim.Date))); if CheckBox1.Checked then begin // if CheckBox2.Checked then // begin // dtmSystem.tblDepartamentos.First; // while not dtmSystem.tblDepartamentos.Eof do // begin // str_dep := str_dep + dtmSystem.tblDepartamentosID_DEPARTAMENTOS. // AsString + ','; // dtmSystem.tblDepartamentos.Next; // end; // setlength(str_dep, length(str_dep) - 1); // add('and e.id_departamento in (' + str_dep + ')'); // end // else // begin add('and e.id_departamento = ' + inttostr(DBLookupComboBox1.KeyValue)); // end; end; add('and e.id_empresa <> 265'); add('order by e.tx_fantasia'); end; pbRel.StepIt; zroqryCredores.Open; lbl_emp.Caption := 'Montando lista de títulos.'; lblProgress.Caption := 'Preparando Títulos.'; Application.ProcessMessages; with zroqryTitulos.SQL do begin Clear; add('select distinct'); add('A.tx_nome,'); add('A.id_devedor,'); add('B.tx_produto,'); add('B.dt_vencimento,'); add('B.dt_pagamento,'); // add('B.vl_titulo,'); add('case when B.id_titulo in (select id_parcela from sys_acordos) then (B.vl_corrigido - B.vl_juros - B.vl_multa - B.vl_encargos)'); add('when B.id_titulo not in (select id_parcela from sys_acordos) then (B.vl_titulo)'); add('end as vl_titulo,'); add('B.vl_desc_principal,'); add('B.vl_repasse_prin,'); add('B.vl_juros,'); add('B.vl_desc_juros,'); add('B.vl_repasse_juros,'); add('B.vl_multa,'); add('B.vl_desc_multa,'); add('B.vl_repasse_multa,'); add('B.vl_encargos,'); add('B.vl_indicador,'); add('B.vl_desc_indicador,'); add('B.vl_repasse_encargos,'); add('B.vl_corrigido,'); add('B.vl_repasse_corrigido,'); add('B.vl_desc_principal,'); add('B.id_titulo,'); add('B.id_empresa,'); add('B.tp_prestacao,'); add('AC.id_original,'); add('(cast(B.dt_pagamento as date)- cast(B.dt_vencimento as date)) as atraso'); add('from chg_titulos B'); add('left join chg_devedores A on (A.id_devedor = B.id_devedor)'); add('left join sys_acordos AC on (AC.id_original = B.id_titulo)'); add('where (B.tp_situacao = ' + QuotedStr('QC') + ' or B.tp_situacao = ' + QuotedStr('B') + ')'); add('and'); add('B.tp_motivo <> ' + QuotedStr('Q')); add('and'); add('CAST(B.dt_pagamento AS DATE) >= '); add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_inicio.Date))); add('and'); add('CAST(B.dt_pagamento AS DATE) <= '); add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_fim.Date))); add('and (AC.id_original is NULL'); add('or AC.id_parcela in (select id_titulo from chg_titulos where id_empresa = 220))'); if CheckBox1.Checked then begin // if CheckBox2.Checked then // begin // add('and b.id_empresa in (select chg_empresas.id_empresa from chg_empresas where chg_empresas.id_departamento in (' // + str_dep + '))'); // end // else // begin add('and b.id_empresa in (select chg_empresas.id_empresa from chg_empresas where chg_empresas.id_departamento = ' + inttostr(DBLookupComboBox1.KeyValue) + ')'); // end; end; add('order by A.tx_nome'); end; zroqryTitulos.Open; frlbl_headertop.Caption := UpperCase(dtmSystem.tblEmpresaTX_TITULO.AsString + ' - CNPJ: ' + dtmSystem.tblEmpresaTX_CNPJ.AsString); Screen.Cursor := crDefault; pbRel.StepIt; relPrestacao.PreviewModal; Screen.Cursor := crHourGlass; lblProgress.Caption := 'Verificando Múltiplos Pagamentos.'; with ZReadOnlyQuery1.SQL do begin Clear; add('select distinct'); add('A.tx_nome,'); add('B.tx_produto,'); add('B.dt_vencimento,'); add('B.dt_pagamento,'); // add('B.vl_titulo,'); add('case when B.id_titulo in (select id_parcela from sys_acordos) then (B.vl_corrigido - B.vl_juros - B.vl_multa - B.vl_encargos)'); add('when B.id_titulo not in (select id_parcela from sys_acordos) then (B.vl_titulo)'); add('end as vl_titulo,'); add('B.vl_juros,'); add('B.vl_multa,'); add('B.vl_encargos,'); add('B.vl_corrigido,'); add('B.vl_desc_principal,'); add('B.id_titulo,'); add('B.id_empresa,'); add('(cast(B.dt_pagamento as date)- cast(B.dt_vencimento as date)) as atraso,'); add('AC.id_original'); // Add('(cast(B.dt_pagamento as date)- cast(B.dt_vencimento as date)) as atraso'); add('from chg_titulos B'); add('left join chg_devedores A on (A.id_devedor = B.id_devedor)'); add('left join sys_acordos AC on (AC.id_original = B.id_titulo)'); add('where (B.tp_situacao = ' + QuotedStr('QC') + ' or B.tp_situacao = ' + QuotedStr('B') + ')'); add('and'); add('B.tp_motivo = ' + QuotedStr('Q')); add('and'); add('CAST(B.dt_pagamento AS DATE) >= '); add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_inicio.Date))); add('and'); add('CAST(B.dt_pagamento AS DATE) <= '); add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_fim.Date))); add('and (AC.id_original is NULL'); add('or AC.id_parcela in (select id_titulo from chg_titulos where id_empresa = 220))'); if CheckBox1.Checked then begin // if CheckBox2.Checked then // begin // add('and b.id_empresa in (select chg_empresas.id_empresa from chg_empresas where chg_empresas.id_departamento in (' // + str_dep + '))'); // end // else // begin add('and b.id_empresa in (select chg_empresas.id_empresa from chg_empresas where chg_empresas.id_departamento = ' + inttostr(DBLookupComboBox1.KeyValue) + ')'); // end; end; add('order by A.tx_nome'); end; ZReadOnlyQuery1.Open; if not(ZReadOnlyQuery1.IsEmpty) then begin Messagedlg ('Há títulos com múltiplas formas de pagamento neste intervalo de gerência.' + ' Por favor procure suas prestações manuais.', mtInformation, [mbok], 0); end; zroqryCredores.Close; zroqryTitulos.Close; lblProgress.Caption := 'Processo terminado.'; // dtmSystem.tblCHGEmpresa.SQL.Text := sql_emp; // dtmSystem.tblCHGEmpresa.Open; // abort; end; end; if rg_tipo.ItemIndex = 1 then begin relPrestacaoAnaSin2 := TrelPrestacaoAnaSin2.Create(self); with relPrestacaoAnaSin2 do begin // RLMemo2.Lines[0] := StringReplace(RLMemo2.Lines[0], 'DT1', // FormatDateTime('dd/mm/yyyy', dtp_inicio.Date), // [rfReplaceAll, rfIgnoreCase]); // RLMemo2.Lines[0] := StringReplace(RLMemo2.Lines[0], 'DT2', // FormatDateTime('dd/mm/yyyy', dtp_fim.Date), // [rfReplaceAll, rfIgnoreCase]); RLMemo2.Lines[0] := StringReplace(RLMemo2.Lines[0], 'DT1', FormatDateTime('dd/mm/yyyy', dtp_inicio.Date), [rfReplaceAll, rfIgnoreCase]); RLMemo2.Lines[0] := StringReplace(RLMemo2.Lines[0], 'DT2', FormatDateTime('dd/mm/yyyy', dtp_fim.Date), [rfReplaceAll, rfIgnoreCase]); cdsSintetico.Close; cdsSintetico.CreateDataSet; Application.ProcessMessages; lbl_emp.Caption := 'Pesquisando títulos.'; Application.ProcessMessages; if CheckBox1.Checked then begin frlbl_dep.Caption := 'Dep.: ' + dtmSystem.tblDepartamentosTX_NOME.AsString; end; lblProgress.Caption := 'Preparando Empresas.'; with zroqryCredores.SQL do begin Clear; add('select distinct'); add('e.*'); add('from chg_empresas e'); add('where e.id_empresa in ('); add('select b.id_empresa from chg_titulos B where'); add('(B.tp_situacao = ' + QuotedStr('QC') + ' or B.tp_situacao = ' + QuotedStr('B') + ')'); add('and'); add('CAST(B.dt_pagamento AS DATE) >= '); add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_inicio.Date))); add('and'); add('CAST(B.dt_pagamento AS DATE) <= '); add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_fim.Date))); add(')'); if CheckBox1.Checked then begin if CheckBox2.Checked then begin dtmSystem.tblDepartamentos.First; while not dtmSystem.tblDepartamentos.Eof do begin str_dep := str_dep + dtmSystem.tblDepartamentosID_DEPARTAMENTOS. AsString + ','; dtmSystem.tblDepartamentos.Next; end; setlength(str_dep, length(str_dep) - 1); add('and e.id_departamento in (' + str_dep + ')'); end else begin add('and e.id_departamento = ' + inttostr(DBLookupComboBox1.KeyValue)); end; end; add('and e.id_empresa <> 265'); add('order by e.tx_fantasia'); // clear; // add('select distinct'); // add('e.*'); // add('from chg_devedores A'); // add('inner join chg_titulos B on (A.id_devedor = B.id_devedor)'); // add('inner join chg_empresas e on (E.id_empresa = B.id_empresa)'); // add('where (B.tp_situacao = ' + QuotedStr('QC') + ' or B.tp_situacao = ' // + QuotedStr('B') + ')'); // add('and'); // add('CAST(B.dt_pagamento AS DATE) >= '); // add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_inicio.Date))); // add('and'); // add('CAST(B.dt_pagamento AS DATE) <= '); // add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_fim.Date))); // add('order by e.tx_fantasia'); end; zroqryCredores.Open; pbRel.Max := zroqryCredores.RecordCount + 1; pbRel.StepIt; Application.ProcessMessages; lbl_emp.Caption := 'Montando lista de títulos.'; lblProgress.Caption := 'Preparando Títulos.'; with zroqryTitulos.SQL do begin Clear; add('select distinct'); add('B.tx_produto,'); add('B.dt_vencimento,'); add('B.dt_pagamento,'); // add('B.vl_titulo,'); add('case when B.id_titulo in (select id_parcela from sys_acordos) then (B.vl_corrigido - B.vl_juros - B.vl_multa - B.vl_encargos)'); add('when B.id_titulo not in (select id_parcela from sys_acordos) then (B.vl_titulo)'); add('end as vl_titulo,'); add('B.vl_desc_principal,'); add('B.vl_repasse_prin,'); add('B.vl_juros,'); add('B.vl_desc_juros,'); add('B.vl_repasse_juros,'); add('B.vl_multa,'); add('B.vl_desc_multa,'); add('B.vl_repasse_multa,'); add('B.vl_encargos,'); add('B.vl_indicador,'); add('B.vl_desc_indicador,'); add('B.vl_repasse_encargos,'); add('B.vl_corrigido,'); add('B.vl_repasse_corrigido,'); add('B.vl_desc_principal,'); add('B.id_titulo,'); add('B.id_empresa,'); add('B.id_devedor,'); add('e.tx_fantasia,'); add('e.tx_cnpj,'); add('B.tp_prestacao,'); add('(cast(B.dt_pagamento as date)- cast(B.dt_vencimento as date)) as atraso,'); add('AC.id_original'); // Add('(cast(B.dt_pagamento as date)- cast(B.dt_vencimento as date)) as atraso'); add('from chg_titulos B'); add('left join chg_empresas E on (B.id_empresa = E.id_empresa)'); add('left join sys_acordos AC on (AC.id_original = B.id_titulo)'); add('where (B.tp_situacao = ' + QuotedStr('QC') + ' or B.tp_situacao = ' + QuotedStr('B') + ')'); add('and'); add('B.tp_motivo <> ' + QuotedStr('Q')); add('and'); add('CAST(B.dt_pagamento AS DATE) >= '); add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_inicio.Date))); add('and'); add('CAST(B.dt_pagamento AS DATE) <= '); add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_fim.Date))); add('and (AC.id_original is NULL'); add('or AC.id_parcela in (select id_titulo from chg_titulos where id_empresa = 220))'); if CheckBox1.Checked then begin if CheckBox2.Checked then begin add('and b.id_empresa in (select chg_empresas.id_empresa from chg_empresas where chg_empresas.id_departamento in (' + str_dep + '))'); end else begin add('and b.id_empresa in (select chg_empresas.id_empresa from chg_empresas where chg_empresas.id_departamento = ' + inttostr(DBLookupComboBox1.KeyValue) + ')'); end; end; add('order by B.id_empresa'); // clear; // add('select'); // add('A.tx_nome,'); // add('B.tx_produto,'); // add('B.dt_vencimento,'); // add('B.dt_pagamento,'); // add('B.vl_titulo,'); // add('B.vl_juros,'); // add('B.vl_multa,'); // add('B.vl_encargos,'); // add('B.vl_corrigido,'); // add('B.vl_desc_principal,'); // add('B.id_titulo,'); // add('B.id_empresa,'); // add('e.tx_fantasia,'); // add('e.tx_cnpj,'); // add('(cast(B.dt_pagamento as date)- cast(B.dt_vencimento as date)) as atraso'); // add('from chg_devedores A'); // add('inner join chg_titulos B on (A.id_devedor = B.id_devedor)'); // add('inner join chg_empresas E on (B.id_empresa = E.id_empresa)'); // add('where (B.tp_situacao = ' + QuotedStr('QC') + ' or B.tp_situacao = ' // + QuotedStr('B') + ')'); // add('and'); // add('CAST(B.dt_pagamento AS DATE) >= '); // add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_inicio.Date))); // add('and'); // add('CAST(B.dt_pagamento AS DATE) <= '); // add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_fim.Date))); // add('and'); // add('b.id_titulo not in (select ac.id_original from sys_acordos ac)'); // add('order by B.id_empresa'); Application.ProcessMessages; end; soma_p := 0; soma_j := 0; soma_m := 0; soma_e := 0; soma_c := 0; soma_d := 0; soma_r := 0; soma_rp := 0; zroqryTitulos.Open; Application.ProcessMessages; lblProgress.Caption := 'Calculando valores.'; if CheckBox2.Checked then begin deps := True; dtmSystem.tblDepartamentos.First; while not dtmSystem.tblDepartamentos.Eof do begin Application.ProcessMessages; zroqryCredores.Filtered := False; zroqryCredores.Filter := 'ID_DEPARTAMENTO = ' + dtmSystem.tblDepartamentosID_DEPARTAMENTOS.AsString; zroqryCredores.Filtered := True; zroqryCredores.First; while not zroqryCredores.Eof do begin lbl_emp.Caption := 'Progresso: Empresa ' + inttostr(zroqryCredores.RecNo) + ' de ' + inttostr(zroqryCredores.RecordCount); zroqryTitulos.Filtered := False; zroqryTitulos.Filter := 'ID_EMPRESA = ' + zroqryCredoresID_EMPRESA.AsString; zroqryTitulos.Filtered := True; zroqryTitulos.First; while not zroqryTitulos.Eof do begin lbl_tit.Caption := 'Título ' + inttostr(zroqryTitulos.RecNo) + ' de ' + inttostr(zroqryTitulos.RecordCount); soma_p := soma_p + zroqryTitulosVL_TITULO.ascurrency; soma_m := soma_m + zroqryTitulosVL_MULTA.ascurrency; soma_j := soma_j + zroqryTitulosVL_JUROS.ascurrency; soma_e := soma_e + zroqryTitulosVL_ENCARGOS.ascurrency; soma_c := soma_c + zroqryTitulosVL_CORRIGIDO.ascurrency; soma_d := soma_d + zroqryTitulosVL_DESC_PRINCIPAL.ascurrency; soma_r := soma_r + zroqryTitulosVL_RETIDO.ascurrency; soma_rp := soma_rp + zroqryTitulosVL_REPASSE.ascurrency; zroqryTitulos.Next; Application.ProcessMessages; end; zroqryCredores.Next; pbRel.StepIt; end; cdsSintetico.Append; cdsSinteticoVL_TITULO.ascurrency := soma_p; cdsSinteticoVL_JUROS.ascurrency := soma_j; cdsSinteticoVL_MULTA.ascurrency := soma_m; cdsSinteticoVL_ENCARGOS.ascurrency := soma_e; cdsSinteticoVL_DESC_PRINCIPAL.ascurrency := soma_d; cdsSinteticoVL_CORRIGIDO.ascurrency := soma_c; cdsSinteticoVL_RETIDO.ascurrency := soma_r; cdsSinteticoVL_REPASSADO.ascurrency := soma_rp; cdsSinteticoID_EMPRESA.AsInteger := dtmSystem.tblDepartamentosID_DEPARTAMENTOS.AsInteger; cdsSinteticoTX_FANTASIA.AsString := dtmSystem.tblDepartamentosTX_NOME.AsString; cdsSinteticoTX_CNPJ.AsString := zroqryTitulosTX_CNPJ.AsString; cdsSintetico.Post; soma_p := 0; soma_j := 0; soma_m := 0; soma_e := 0; soma_c := 0; soma_d := 0; soma_r := 0; soma_rp := 0; Application.ProcessMessages; dtmSystem.tblDepartamentos.Next; end; RLLabel14.Caption := UpperCase(dtmSystem.tblEmpresaTX_TITULO.AsString + ' - CNPJ: ' + dtmSystem.tblEmpresaTX_CNPJ.AsString); Screen.Cursor := crDefault; lbl_emp.Caption := 'Pronto!'; lbl_tit.Caption := ''; relPrestacao2.PreviewModal; Screen.Cursor := crHourGlass; lblProgress.Caption := 'Verificando Múltiplos Pagamentos.'; with ZReadOnlyQuery1.SQL do begin Clear; add('select distinct'); add('B.tx_produto,'); add('B.dt_vencimento,'); add('B.dt_pagamento,'); // add('B.vl_titulo,'); add('case when B.id_titulo in (select id_parcela from sys_acordos) then (B.vl_corrigido - B.vl_juros - B.vl_multa - B.vl_encargos)'); add('when B.id_titulo not in (select id_parcela from sys_acordos) then (B.vl_titulo)'); add('end as vl_titulo,'); add('B.vl_juros,'); add('B.vl_multa,'); add('B.vl_encargos,'); add('B.vl_corrigido,'); add('B.vl_desc_principal,'); add('B.id_titulo,'); add('B.id_empresa,'); add('e.tx_fantasia,'); add('e.tx_cnpj,'); add('(cast(B.dt_pagamento as date)- cast(B.dt_vencimento as date)) as atraso,'); add('AC.id_original'); // Add('(cast(B.dt_pagamento as date)- cast(B.dt_vencimento as date)) as atraso'); add('from chg_titulos B'); add('left join chg_empresas E on (B.id_empresa = E.id_empresa)'); add('left join sys_acordos AC on (AC.id_original = B.id_titulo)'); add('where (B.tp_situacao = ' + QuotedStr('QC') + ' or B.tp_situacao = ' + QuotedStr('B') + ')'); add('and'); add('B.tp_motivo = ' + QuotedStr('Q')); add('and'); add('CAST(B.dt_pagamento AS DATE) >= '); add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_inicio.Date))); add('and'); add('CAST(B.dt_pagamento AS DATE) <= '); add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_fim.Date))); add('and (AC.id_original is NULL'); add('or AC.id_parcela in (select id_titulo from chg_titulos where id_empresa = 220))'); if CheckBox1.Checked then begin if CheckBox2.Checked then begin add('and b.id_empresa in (select chg_empresas.id_empresa from chg_empresas where chg_empresas.id_departamento in (' + str_dep + '))'); end else begin add('and b.id_empresa in (select chg_empresas.id_empresa from chg_empresas where chg_empresas.id_departamento = ' + inttostr(DBLookupComboBox1.KeyValue) + ')'); end; end; add('order by B.id_empresa'); end; Application.ProcessMessages; ZReadOnlyQuery1.Open; if not(ZReadOnlyQuery1.IsEmpty) then begin Messagedlg ('Há títulos com múltiplas formas de pagamento neste intervalo de gerência.' + ' Por favor procure suas prestações manuais.', mtInformation, [mbok], 0); end; Screen.Cursor := crDefault; zroqryCredores.Close; zroqryTitulos.Close; end else begin Application.ProcessMessages; zroqryCredores.First; lblProgress.Caption := 'Calculando Valores.'; while not zroqryCredores.Eof do begin lbl_emp.Caption := 'Progresso: Empresa ' + inttostr(zroqryCredores.RecNo) + ' de ' + inttostr(zroqryCredores.RecordCount); zroqryTitulos.Filtered := False; zroqryTitulos.Filter := 'ID_EMPRESA = ' + zroqryCredoresID_EMPRESA.AsString; zroqryTitulos.Filtered := True; zroqryTitulos.First; while not zroqryTitulos.Eof do begin lbl_tit.Caption := 'Título ' + inttostr(zroqryTitulos.RecNo) + ' de ' + inttostr(zroqryTitulos.RecordCount); soma_p := soma_p + zroqryTitulosVL_TITULO.ascurrency; soma_m := soma_m + zroqryTitulosVL_MULTA.ascurrency; soma_j := soma_j + zroqryTitulosVL_JUROS.ascurrency; soma_e := soma_e + zroqryTitulosVL_ENCARGOS.ascurrency; soma_c := soma_c + zroqryTitulosVL_CORRIGIDO.ascurrency; soma_d := soma_d + zroqryTitulosVL_DESC_PRINCIPAL.ascurrency; soma_r := soma_r + zroqryTitulosVL_RETIDO.ascurrency; soma_rp := soma_rp + zroqryTitulosVL_REPASSE.ascurrency; zroqryTitulos.Next; Application.ProcessMessages; end; if zroqryTitulos.Eof then begin cdsSintetico.Append; cdsSinteticoVL_TITULO.ascurrency := soma_p; cdsSinteticoVL_JUROS.ascurrency := soma_j; cdsSinteticoVL_MULTA.ascurrency := soma_m; cdsSinteticoVL_ENCARGOS.ascurrency := soma_e; cdsSinteticoVL_DESC_PRINCIPAL.ascurrency := soma_d; cdsSinteticoVL_CORRIGIDO.ascurrency := soma_c; cdsSinteticoVL_RETIDO.ascurrency := soma_r; cdsSinteticoVL_REPASSADO.ascurrency := soma_rp; cdsSinteticoID_EMPRESA.AsInteger := zroqryCredoresID_EMPRESA.AsInteger; cdsSinteticoTX_FANTASIA.AsString := zroqryTitulosTX_FANTASIA.AsString; cdsSinteticoTX_CNPJ.AsString := zroqryTitulosTX_CNPJ.AsString; cdsSintetico.Post; soma_p := 0; soma_j := 0; soma_m := 0; soma_e := 0; soma_c := 0; soma_d := 0; soma_r := 0; soma_rp := 0; Application.ProcessMessages; end; Application.ProcessMessages; pbRel.StepIt; zroqryCredores.Next; end; RLLabel14.Caption := UpperCase(dtmSystem.tblEmpresaTX_TITULO.AsString + ' - CNPJ: ' + dtmSystem.tblEmpresaTX_CNPJ.AsString); Screen.Cursor := crDefault; lbl_emp.Caption := 'Pronto!'; lbl_tit.Caption := ''; relPrestacao2.PreviewModal; Screen.Cursor := crHourGlass; Application.ProcessMessages; lblProgress.Caption := 'Verificando Múltiplos Pagamentos.'; with zroqryTitulos.SQL do begin Clear; add('select distinct'); add('B.tx_produto,'); add('B.dt_vencimento,'); add('B.dt_pagamento,'); // add('B.vl_titulo,'); add('case when B.id_titulo in (select id_parcela from sys_acordos) then (B.vl_corrigido - B.vl_juros - B.vl_multa - B.vl_encargos)'); add('when B.id_titulo not in (select id_parcela from sys_acordos) then (B.vl_titulo)'); add('end as vl_titulo,'); add('B.vl_desc_principal,'); add('B.vl_repasse_prin,'); add('B.vl_juros,'); add('B.vl_desc_juros,'); add('B.vl_repasse_juros,'); add('B.vl_multa,'); add('B.vl_desc_multa,'); add('B.vl_repasse_multa,'); add('B.vl_encargos,'); add('B.vl_indicador,'); add('B.vl_desc_indicador,'); add('B.vl_repasse_encargos,'); add('B.vl_corrigido,'); add('B.vl_repasse_corrigido,'); add('B.vl_desc_principal,'); add('B.id_titulo,'); add('B.id_empresa,'); add('e.tx_fantasia,'); add('e.tx_cnpj,'); add('B.tp_prestacao,'); add('(cast(B.dt_pagamento as date)- cast(B.dt_vencimento as date)) as atraso,'); add('AC.id_original'); // Add('(cast(B.dt_pagamento as date)- cast(B.dt_vencimento as date)) as atraso'); add('from chg_titulos B'); add('left join chg_empresas E on (B.id_empresa = E.id_empresa)'); add('left join sys_acordos AC on (AC.id_original = B.id_titulo)'); add('where (B.tp_situacao = ' + QuotedStr('QC') + ' or B.tp_situacao = ' + QuotedStr('B') + ')'); add('and'); add('B.tp_motivo = ' + QuotedStr('Q')); add('and'); add('CAST(B.dt_pagamento AS DATE) >= '); add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_inicio.Date))); add('and'); add('CAST(B.dt_pagamento AS DATE) <= '); add(QuotedStr(FormatDateTime('yyyy-mm-dd', dtp_fim.Date))); add('and (AC.id_original is NULL'); add('or AC.id_parcela in (select id_titulo from chg_titulos where id_empresa = 220))'); if CheckBox1.Checked then begin if CheckBox2.Checked then begin add('and b.id_empresa in (select chg_empresas.id_empresa from chg_empresas where chg_empresas.id_departamento in (' + str_dep + '))'); end else begin add('and b.id_empresa in (select chg_empresas.id_empresa from chg_empresas where chg_empresas.id_departamento = ' + inttostr(DBLookupComboBox1.KeyValue) + ')'); end; end; add('order by B.id_empresa'); end; Application.ProcessMessages; zroqryTitulos.Open; if not(zroqryTitulos.IsEmpty) then begin Messagedlg ('Há títulos com múltiplas formas de pagamento neste intervalo de gerência.' + ' Por favor procure suas prestações manuais.', mtInformation, [mbok], 0); end; Screen.Cursor := crDefault; zroqryCredores.Close; zroqryTitulos.Close; cdsSintetico.First; // with TIniFile.Create(ExtractFilePath(Application.ExeName) + // 'sintetico.ini') do with TMemIniFile.Create(dtmSystem.path_executavel + '\sintetico.ini', TEncoding.UTF8) do begin try i := 1; EraseSection('Dados'); while SectionExists('Empresa_' + inttostr(i)) do begin EraseSection('Empresa_' + inttostr(i)); Inc(i, 1); end; Writeinteger('Dados', 'Qtde_Empresas', cdsSintetico.RecordCount); WriteString('Dados', 'Periodo', FormatDateTime('dd/mm/yyyy', dtp_inicio.Date) + ' - ' + FormatDateTime('dd/mm/yyyy', dtp_fim.Date)); i := 1; while not cdsSintetico.Eof do begin WriteString('Empresa_' + inttostr(i), 'TX_FANTASIA', cdsSinteticoTX_FANTASIA.AsString); WriteString('Empresa_' + inttostr(i), 'CNPJ', cdsSinteticoTX_CNPJ.AsString); WriteString('Empresa_' + inttostr(i), 'Valor', cdsSinteticoVL_RETIDO.AsString); Inc(i, 1); cdsSintetico.Next; end; finally Free end; end; end; end; lblProgress.Caption := 'Processo terminado.'; end; dtmSystem.tblCHGEmpresa.SQL.Text := sql_emp; dtmSystem.tblCHGEmpresa.Open; Screen.Cursor := crDefault; end; procedure TconfRelGerencia.Button1Click(Sender: TObject); begin inherited; if (ZReadOnlyQuery1.RecordCount = 0) or (ZReadOnlyQuery1.Active = False) then begin abort; end; relMulti := TrelMulti.Create(self); with relMulti do begin dtsRel.DataSet := ZReadOnlyQuery1; rldbtxt1.DataField := 'tx_nome'; rldbtxt2.DataField := 'tx_produto'; rldbtxt3.DataField := 'vl_titulo'; rldbtxt4.DataField := 'dt_vencimento'; RLReport1.PreviewModal; end; end; procedure TconfRelGerencia.CheckBox1Click(Sender: TObject); begin inherited; DBLookupComboBox1.Enabled := CheckBox1.Checked; CheckBox2.Enabled := CheckBox1.Checked; end; procedure TconfRelGerencia.CheckBox2Click(Sender: TObject); begin inherited; DBLookupComboBox1.Enabled := not(CheckBox2.Checked); end; procedure TconfRelGerencia.FormClose(Sender: TObject; var Action: TCloseAction); begin inherited; dtmSystem.tblCHGEmpresa.SQL.Text := sqlEmp; dtmSystem.tblCHGEmpresa.Open; dtmSystem.tblEmpresaRepasse.Filtered := False; end; procedure TconfRelGerencia.FormCreate(Sender: TObject); begin inherited; dtp_inicio.Date := Date; dtp_fim.Date := Date; lbl_emp.Caption := ''; lbl_tit.Caption := ''; sqlEmp := dtmSystem.tblCHGEmpresa.SQL.Text; end; end.