unit UnitBuscCliNom;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, Wwdbigrd, Wwdbgrid, DB, FIBDataSet, pFIBDataSet,
  FIBDatabase, pFIBDatabase, Wwdatsrc;

type
  TfmBuscCliNom = class(TForm)
    gbPrincipal: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    eClave: TEdit;
    bttnBuscar: TButton;
    dbgBusc: TwwDBGrid;
    bttnAceptar: TButton;
    bttnCancelar: TButton;
    cbFiltro1: TCheckBox;
    cbFiltro2: TCheckBox;
    dsBusca: TpFIBDataSet;
    trRead: TpFIBTransaction;
    dsoBuscCli: TwwDataSource;
    dsBuscaCLIENTE_ID: TFIBIntegerField;
    dsBuscaNOMBRE: TFIBStringField;
    dsBuscaCLAVE_CLIENTE: TFIBStringField;
    procedure bttnBuscarClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure bttnBuscarKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure bttnAceptarClick(Sender: TObject);
    procedure bttnCancelarClick(Sender: TObject);
    procedure dbgBuscDblClick(Sender: TObject);
    procedure dbgBuscKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure eClaveKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
  public
    Tipo : String;
    procedure Parametros();
    procedure Focus();
    procedure Regresar();

  end;

var
  fmBuscCliNom: TfmBuscCliNom;

implementation

uses fmActivarTarjeta2;


{$R *.dfm}

procedure TfmBuscCliNom.bttnAceptarClick(Sender: TObject);
begin
  Regresar();
end;

procedure TfmBuscCliNom.bttnBuscarClick(Sender: TObject);
begin

   Parametros();
   dsBusca.CloseOpen(false);
   Focus();
end;

procedure TfmBuscCliNom.bttnBuscarKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if (key = vk_return) or (key = vk_down) then
    dbgBusc.SetFocus;
end;

procedure TfmBuscCliNom.bttnCancelarClick(Sender: TObject);
begin
  Close();
end;

procedure TfmBuscCliNom.dbgBuscDblClick(Sender: TObject);
begin
  Regresar();
end;

procedure TfmBuscCliNom.dbgBuscKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if key = vk_ESCAPE then
    Close;
if key = vk_return then
    bttnAceptar.SetFocus;
end;

procedure TfmBuscCliNom.eClaveKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if key = vk_return then
    bttnBuscar.SetFocus;
if key = vk_ESCAPE then
    Close;
end;

procedure TfmBuscCliNom.Regresar();
begin
  if Tipo = '1' then
    fmActivarTar2.dbNombreCli.Text := dsBusca.FieldByName('NOMBRE').AsString;

  dsBusca.Close;
  Close();
end;

procedure TfmBuscCliNom.Focus();
begin
  If not dsBusca.IsEmpty then
    dbgBusc.SetFocus
else
    eClave.SetFocus;
end;

procedure TfmBuscCliNom.FormShow(Sender: TObject);
begin
       dsBusca.CloseOpen(false);
       Focus();
end;

procedure TfmBuscCliNom.Parametros();
begin
   trRead.Active := true;
   dsBusca.Close;
   dsBusca.SelectSQL.Clear;
   dsBusca.SelectSQL.Add('SELECT A.CLIENTE_ID, A.NOMBRE, B.CLAVE_CLIENTE FROM CLIENTES A ');
   dsBusca.SelectSQL.Add('LEFT JOIN GET_CLAVE_CLI(A.CLIENTE_ID) B ON (A.CLIENTE_ID = B.CLIENTE_ID)');

   if cbFiltro1.Checked then
       dsBusca.SelectSQL.Add('WHERE (NOMBRE) LIKE (:NOMBRE) AND A.ESTATUS = ''A'' ORDER BY NOMBRE ');
   if Not cbFiltro1.Checked then
      dsBusca.SelectSQL.Add('WHERE UPPER(NOMBRE) LIKE UPPER(:NOMBRE) AND A.ESTATUS = ''A'' ORDER BY NOMBRE ');

   if cbFiltro2.Checked then
      dsBusca.ParamByName('NOMBRE').ASSTRING := '%'+eClave.Text+'%';
   if Not cbFiltro2.Checked then
      dsBusca.ParamByName('NOMBRE').ASSTRING :=eClave.Text+'%';
   if eClave.Text = '' then
      dsBusca.ParamByName('NOMBRE').Clear;

end;


end.
