unit uViewFiltroClientes;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, cxControls, cxContainer, cxEdit, cxTextEdit, cxDBEdit,
  StdCtrls, Buttons, SpTBXDkPanels, uViewParametrosInforme, uBizContactos,
  uClientesController, SpTBXControls, SpTBXItem;

type
  IViewFiltroClientes = interface(IViewParametrosInforme)
    ['{129906DA-8E64-4312-8095-A04F8007CF7F}']
  end;
  
  TfrViewFiltroClientes = class(TfrViewParametrosInforme, IViewFiltroClientes)
    TBXLabel2: TSpTBXLabel;
    rbTodosClientes: TRadioButton;
    rbUnCliente: TRadioButton;
    cbxDesglosado: TCheckBox;
    bElegirCliente: TBitBtn;
    edtCliente: TcxTextEdit;
    procedure rbTodosClientesClick(Sender: TObject);
    procedure rbUnClienteClick(Sender: TObject);
    procedure CustomViewCreate(Sender: TObject);
    procedure bElegirClienteClick(Sender: TObject);
    procedure CustomViewDestroy(Sender: TObject);
  private
    FCliente : IBizCliente;
    FClientesController : IClientesController; 
  public
    property Cliente : IBizCliente read FCliente;
  end;

implementation

{$R *.dfm}

procedure TfrViewFiltroClientes.bElegirClienteClick(Sender: TObject);
var
  AClientes : IBizCliente;
begin
  inherited;
  AClientes := IBizCliente(FClientesController.BuscarTodos);
  try
    FCliente := IBizCliente(FClientesController.ElegirContacto(AClientes, '', False));
    if Assigned(FCliente) then
    begin
      FCliente.Open;
      edtCliente.Text := FCliente.NOMBRE;
    end;
  finally
    AClientes := NIL;
  end;
end;

procedure TfrViewFiltroClientes.CustomViewCreate(Sender: TObject);
begin
  inherited;
  FCliente := NIL;
  FClientesController := NIL;
end;

procedure TfrViewFiltroClientes.CustomViewDestroy(Sender: TObject);
begin
  inherited;
  FCliente := NIL;
  FClientesController := NIL;
end;

procedure TfrViewFiltroClientes.rbTodosClientesClick(Sender: TObject);
begin
  if rbTodosClientes.Checked then
  begin
    cbxDesglosado.Enabled := True;
    FCliente := NIL;
    bElegirCliente.Enabled := False;
    edtCliente.Clear;
    edtCliente.Enabled := False;    
  end;
end;

procedure TfrViewFiltroClientes.rbUnClienteClick(Sender: TObject);
begin
  if rbUnCliente.Checked then
  begin
    cbxDesglosado.Enabled := False;
    bElegirCliente.Enabled := True;
    edtCliente.Enabled := True;
    if not Assigned(FClientesController) then
      FClientesController := TClientesController.Create;
  end;
end;

end.
