unit uViewVendedor;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewContacto, dxLayoutLookAndFeels, DB, uDADataTable,
  dxLayoutControl, cxMemo, cxDBEdit, cxContainer, cxEdit, cxTextEdit, cxControls,
  cxMaskEdit, cxSpinEdit, ImgList, PngImageList, ActnList, cxHyperLinkEdit,
  Buttons, PngSpeedButton, cxDropDownEdit, cxCalendar, cxGraphics,
  uDAInterfaces, uCustomView, uViewBase, uViewTienda, uBizContactos,
  uViewObservaciones;

type
  IViewVendedor = interface(IViewContacto)
  ['{245F4A36-39A4-4081-9826-F05FBBC729AE}']
  end;

  TfrViewVendedor = class(TfrViewContacto, IViewVendedor)
    dxLayoutControlContactoItem19: TdxLayoutItem;
    cbUsuario: TcxComboBox;
    dxLayoutControlContactoItem20: TdxLayoutItem;
    edtComision: TcxDBSpinEdit;
    procedure CustomViewCreate(Sender: TObject);
    procedure CustomViewDestroy(Sender: TObject);
    procedure cbUsuarioPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);

  private
    FDataItem : TDADataTable;
    FListaUsuarios : TStringList;
    function GetDataItem: TDADataTable;
    procedure SetDataItem(const Value: TDADataTable);

  protected
    procedure SetContacto(const Value: IBizContacto); override;

  public
    property DataItem : TDADataTable read GetDataItem write SetDataItem;
    procedure ElegirUsuario(const AIDUsuario : Integer);    
  end;

implementation
{$R *.dfm}

uses uFactuGES_App;

procedure TfrViewVendedor.cbUsuarioPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
var
  AIndex : integer;
begin
  inherited;

  if Assigned(FDataItem) and FDataItem.Active then
  begin
    AIndex := StrToInt(FListaUsuarios.Values[DisplayValue]);

    FDataItem.Edit;
    if Assigned(FDataItem.FindField('ID_USUARIO')) then
      FDataItem.FieldByName('ID_USUARIO').AsInteger := AIndex;

//    if Assigned(FDataItem.FindField('USUARIO')) then
//      FDataItem.FieldByName('USUARIO').AsString := DisplayValue;

    FDataItem.post;
  end;

end;

procedure TfrViewVendedor.CustomViewCreate(Sender: TObject);
var
  i : integer;
begin
  inherited;


  FListaUsuarios := AppFactuGES.UsuariosController.DarListaUsuarios;
  with cbUsuario.Properties.Items do
  begin
    BeginUpdate;
    try
      Clear;
      for i := 0 to FListaUsuarios.Count - 1 do
        Add(FListaUsuarios.Names[i]);
    finally
      EndUpdate;
    end;
  end;
end;

procedure TfrViewVendedor.CustomViewDestroy(Sender: TObject);
begin
  FreeAndNIL(FListaUsuarios);
  inherited;
end;

procedure TfrViewVendedor.ElegirUsuario(const AIDUsuario: Integer);
var
  i : integer;
begin
  for i := 0 to FListaUsuarios.Count-1 do
  begin
    if FListaUsuarios.ValueFromIndex[i] = IntToStr(AIDUsuario) then
    begin
      cbUsuario.Text := FListaUsuarios.Names[i];
      Break;
    end;
  end;
end;

function TfrViewVendedor.GetDataItem: TDADataTable;
begin
  Result := FDataItem;
end;

procedure TfrViewVendedor.SetContacto(const Value: IBizContacto);
begin
  inherited;
  DataItem := FContacto.DataTable;
end;

procedure TfrViewVendedor.SetDataItem(const Value: TDADataTable);
begin
  FDataItem := Value;
  if Assigned(FDataItem.FindField('ID_USUARIO')) then
     ElegirUsuario(FDataItem.FieldByName('ID_USUARIO').AsInteger);
end;

end.
