unit ufrmAchaRecibo; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, ShellApi, System.UITypes, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, umstMaster, Data.DB, ZAbstractRODataset, System.StrUtils, ZAbstractDataset, ZDataset, Vcl.StdCtrls, Vcl.Grids, Vcl.DBGrids, Vcl.ComCtrls, Vcl.Menus; type TfrmAchaRecibo = class(TmstMaster) gb_pesquisar: TGroupBox; lbl_pesquisar: TLabel; edt_campop: TEdit; btn_pesquisar: TButton; dbgrd_pesquisar: TDBGrid; cbx_tiponome: TComboBox; qryPesquisa: TZQuery; dtsPesquisa: TDataSource; gb_recibo: TGroupBox; dbgrd_recibo: TDBGrid; dtsRecibos: TDataSource; PopupMenu1: TPopupMenu; ppmnu_cancelarecibo: TMenuItem; ppmnu_reativarrecibo: TMenuItem; procedure btn_pesquisarClick(Sender: TObject); procedure dbgrd_pesquisarDblClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure dbgrd_reciboDblClick(Sender: TObject); procedure WMMoving(var MSG: TWMMoving); message WM_MOVING; procedure FormCanResize(Sender: TObject; var NewWidth, NewHeight: Integer; var Resize: Boolean); procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure ppmnu_cancelareciboClick(Sender: TObject); procedure ppmnu_reativarreciboClick(Sender: TObject); procedure dbgrd_reciboDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); private { Private declarations } public { Public declarations } end; var frmAchaRecibo: TfrmAchaRecibo; implementation {$R *.dfm} uses udtmSystem; procedure TfrmAchaRecibo.btn_pesquisarClick(Sender: TObject); var string_query, tx_pesquisa, help_cpfcnpj, tx_tel, query2: string; i, tam1: Integer; begin try if edt_campop.Text <> '' then begin tx_pesquisa := UpperCase(stringreplace(edt_campop.Text, ' ', '%', [rfReplaceAll])); string_query := 'select ID_DEVEDOR, TX_NOME, case when TP_CLIENTE = ''F'' ' + 'then CD_CPF when TP_CLIENTE = ''J'' then CD_CNPJ end as TX_DOCUMENTO from ' + 'CHG_DEVEDORES where '; query2 := string_query; case cbx_tiponome.ItemIndex of 0: begin string_query := string_query + 'upper(TX_NOME COLLATE WIN_PTBR) LIKE ' + QuotedStr('%' + tx_pesquisa + '%'); end; 1: begin string_query := string_query + 'upper(TX_PAI) LIKE ' + QuotedStr('%' + tx_pesquisa + '%'); end; 2: begin string_query := string_query + 'upper(TX_MAE) LIKE ' + QuotedStr('%' + tx_pesquisa + '%'); end; 3: begin tx_pesquisa := stringreplace(tx_pesquisa, '.', '', [rfReplaceAll]); tx_pesquisa := stringreplace(tx_pesquisa, '-', '', [rfReplaceAll]); tx_pesquisa := stringreplace(tx_pesquisa, '/', '', [rfReplaceAll]); if length(tx_pesquisa) < 14 then begin tx_pesquisa := stringreplace(tx_pesquisa, 'TX_DOCUMENTO', 'CD_CPF', [rfReplaceAll]); help_cpfcnpj := copy(tx_pesquisa, 1, 3) + '.' + copy(tx_pesquisa, 4, 3) + '.' + copy(tx_pesquisa, 7, 3) + '-' + RightStr(tx_pesquisa, 2); string_query := string_query + 'CD_CPF like ' + QuotedStr(help_cpfcnpj); end else if length(tx_pesquisa) >= 14 then begin tx_pesquisa := stringreplace(tx_pesquisa, 'TX_DOCUMENTO', 'CD_CNPJ', [rfReplaceAll]); help_cpfcnpj := copy(tx_pesquisa, 1, 2) + '.' + copy(tx_pesquisa, 3, 3) + '.' + copy(tx_pesquisa, 6, 3) + '/' + copy(tx_pesquisa, 9, 4) + '-' + RightStr(tx_pesquisa, 2); string_query := string_query + 'CD_CNPJ like ' + QuotedStr(help_cpfcnpj); string_query := stringreplace(string_query, 'CD_CPF', 'CD_CNPJ', [rfReplaceAll]); end; end; 4: begin string_query := string_query + 'ID_DEVEDOR in (select id_devedor from chg_titulos where cd_devedor like ' + QuotedStr('%' + tx_pesquisa + '%') + ')'; end; 5: begin tx_pesquisa := stringreplace(tx_pesquisa, '-', '', [rfReplaceAll, rfIgnoreCase]); tx_tel := tx_pesquisa; if (length(tx_pesquisa) < 8) or (length(tx_pesquisa) > 9) then begin MessageDlg ('Número de telefone inválido. Ele deve conter ao menos 8 dígitos e no máximo 9 para que a pesquisa possa ser realizada.', mtWarning, [mbOK], 0); abort; end; if length(tx_pesquisa) = 8 then begin tx_pesquisa := copy(tx_pesquisa, 1, 4) + '-' + copy(tx_pesquisa, 5, 4); end else begin tx_pesquisa := copy(tx_pesquisa, 1, 5) + '-' + copy(tx_pesquisa, 6, 4); end; string_query := string_query + 'ID_DEVEDOR in (SELECT ID_DEVEDOR FROM CHG_TELEFONES WHERE NR_NUMERO = ' + QuotedStr(tx_pesquisa) + ')'; query2 := query2 + 'ID_DEVEDOR in (SELECT ID_DEVEDOR FROM CHG_TELEFONES WHERE NR_NUMERO = ' + QuotedStr(tx_tel) + ')'; end; end; end; string_query := string_query + ' order by tx_nome'; query2 := query2 + ' order by tx_nome'; qryPesquisa.SQL.Text := string_query; qryPesquisa.ExecSQL; qryPesquisa.Active := true; if (qryPesquisa.IsEmpty) and (cbx_tiponome.ItemIndex <> 1) then // se vazio, avisa que não achou nada begin qryPesquisa.Active := false; MessageDlg('Nenhuma entrada foi encontrada.', mtWarning, [mbOK], 0); end else begin if (cbx_tiponome.ItemIndex = 1) and (qryPesquisa.IsEmpty) then begin qryPesquisa.SQL.Text := query2; qryPesquisa.ExecSQL; qryPesquisa.Active := true; if qryPesquisa.IsEmpty then begin qryPesquisa.Active := false; MessageDlg('Nenhuma entrada foi encontrada.', mtWarning, [mbOK], 0); end; end; end; for i := 0 to qryPesquisa.FieldCount - 1 do // arruma o tamanho das colunas conforme os dados begin if AnsiContainsText(dbgrd_pesquisar.Columns[i].Title.Caption, 'ID_') then begin dbgrd_pesquisar.Columns[i].Title.Caption := 'Id'; end; if dbgrd_pesquisar.Columns[i].Title.Caption = 'TX_NOME' then begin dbgrd_pesquisar.Columns[i].Title.Caption := 'Nome'; end; if dbgrd_pesquisar.Columns[i].Title.Caption = 'TX_DOCUMENTO' then begin dbgrd_pesquisar.Columns[i].Title.Caption := 'Nº do documento'; tam1 := 543; end; if dbgrd_pesquisar.Columns[i].Title.Caption = 'CD_CPF' then begin dbgrd_pesquisar.Columns[i].Title.Caption := 'CPF'; tam1 := 584; end; if dbgrd_pesquisar.Columns[i].Title.Caption = 'CD_CNPJ' then begin dbgrd_pesquisar.Columns[i].Title.Caption := 'CNPJ'; tam1 := 567; end; if dbgrd_pesquisar.Columns[i].Title.Caption = 'TX_CNPJ' then begin dbgrd_pesquisar.Columns[i].Title.Caption := 'CNPJ'; tam1 := 543; end; end; for i := 0 to qryPesquisa.FieldCount - 1 do begin if AnsiContainsText(dbgrd_pesquisar.Columns[i].Title.Caption, 'Id') then begin dbgrd_pesquisar.Columns[i].Width := 40; end; if dbgrd_pesquisar.Columns[i].Title.Caption = 'Nome' then begin dbgrd_pesquisar.Columns[i].Width := tam1; end; end; except end; end; procedure TfrmAchaRecibo.dbgrd_pesquisarDblClick(Sender: TObject); begin dtmSystem.tblDevedores.Locate('TX_NOME', qryPesquisa.FieldByName('TX_NOME') .AsString, []); dtmSystem.tblRecibo.Filtered := False; dtmSystem.tblRecibo.Filter := 'ID_DEVEDOR = ' + QuotedStr(dtmSystem.tblDevedoresID_DEVEDOR.AsString); dtmSystem.tblRecibo.Filtered := True; dtmSystem.tblRecibo.Last; end; procedure TfrmAchaRecibo.dbgrd_reciboDblClick(Sender: TObject); begin if not(dbgrd_recibo.Columns[5].Field.AsString = '') then begin ShellExecute(Handle, nil, PChar(dbgrd_recibo.Columns[5].Field.AsString), nil, nil, SW_SHOWNORMAL); end else begin Showmessage('Caminho para o recibo não encontrado.'); end; end; procedure TfrmAchaRecibo.dbgrd_reciboDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin inherited; case AnsiIndexStr(dtmSystem.tblRecibo.FieldByName('TP_CANCELADO').AsString, ['S', 'N']) of 0: dbgrd_recibo.Canvas.Brush.Color := clRed; 1: dbgrd_recibo.Canvas.Brush.Color := clWhite; end; // end; dbgrd_recibo.DefaultDrawColumnCell(Rect, DataCol, Column, State); end; procedure TfrmAchaRecibo.FormCanResize(Sender: TObject; var NewWidth, NewHeight: Integer; var Resize: Boolean); begin inherited; Resize := false; end; procedure TfrmAchaRecibo.FormClose(Sender: TObject; var Action: TCloseAction); begin dtmSystem.tblRecibo.Filtered := false; end; procedure TfrmAchaRecibo.FormCreate(Sender: TObject); begin with dtmSystem do begin if not(tblDevedores.Active) then begin tblDevedores.Open; end; if not(tblRecibo.Active) then begin tblRecibo.Open; end; tblRecibo.Filter := 'ID_DEVEDOR = '''''; tblRecibo.Filtered := true; tblRecibo.SortType := stDescending; end; end; procedure TfrmAchaRecibo.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin inherited; if Key = VK_ESCAPE then begin Key := 0; Close; end; end; procedure TfrmAchaRecibo.ppmnu_cancelareciboClick(Sender: TObject); begin inherited; with dtmSystem do begin if tblRecibo.Filter = 'ID_DEVEDOR = ''''' then begin abort; end else begin tblRecibo.Edit; tblReciboTP_CANCELADO.AsString := 'S'; tblRecibo.Post; end; end; end; procedure TfrmAchaRecibo.ppmnu_reativarreciboClick(Sender: TObject); begin inherited; with dtmSystem do begin if tblRecibo.Filter = 'ID_DEVEDOR = ''''' then begin abort; end else begin tblRecibo.Edit; tblReciboTP_CANCELADO.AsString := 'N'; tblRecibo.Post; end; end; end; procedure TfrmAchaRecibo.WMMoving(var MSG: TWMMoving); var workArea: TRect; begin workArea := Screen.WorkareaRect; with MSG.DragRect^ do begin if left < workArea.left then OffsetRect(MSG.DragRect^, workArea.left - left, 0); if top < workArea.top then OffsetRect(MSG.DragRect^, 0, workArea.top - top); if Right > workArea.Right then OffsetRect(MSG.DragRect^, workArea.Right - Right, 0); if Bottom > workArea.Bottom then OffsetRect(MSG.DragRect^, 0, workArea.Bottom - Bottom); end; end; end.