unit UnitBuscCliClave;

interface

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

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

  end;

var
  fmBuscCliClave: TfmBuscCliClave;

implementation

uses Unit1;



{$R *.dfm}



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

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

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

procedure TfmBuscCliClave.bttnBuscarClick(Sender: TObject);
begin
   Parametros();
   dsBusca.CloseOpen(false);
   Focus();
end;

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

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

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

procedure TfmBuscCliClave.bttnAceptarClick(Sender: TObject);
begin

  Regresar();
end;

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

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

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

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

procedure TfmBuscCliClave.Regresar();
begin
  if Tipo = '1' then
     form1.dbClaveCli.Text := dsBusca.FieldByName('CLAVE_CLIENTE').AsString;    

  dsBusca.Close;
  Close();
end;

end.
