unit uViewTienda;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, ExtCtrls, StdCtrls, DB, uDADataTable, cxGraphics,
  dxLayoutControl, cxContainer, cxEdit, cxTextEdit, cxMaskEdit, cxDropDownEdit,
  cxControls;

type
  IViewTienda = interface(IViewBase)
    ['{9FD357AB-2E87-4CAF-8AEB-04368AD075AF}']

    function GetDataItem: TDADataTable;
    procedure SetDataItem(const Value: TDADataTable);

    property DataItem : TDADataTable read GetDataItem write SetDataItem;
    procedure ElegirTienda(const AIDTienda : Integer);
  end;

  TfrViewTienda = class(TfrViewBase, IViewTienda)
    cbTienda: TcxComboBox;
    dxLayoutControl1Group_Root: TdxLayoutGroup;
    dxLayoutControl1: TdxLayoutControl;
    dxLayoutControl1Item1: TdxLayoutItem;
    procedure CustomViewCreate(Sender: TObject);
    procedure CustomViewDestroy(Sender: TObject);
    procedure cbTiendaPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
  private
    FDataItem : TDADataTable;
    FListaTiendas : TStringList;
    function GetDataItem: TDADataTable;
    procedure SetDataItem(const Value: TDADataTable);
  public
    property DataItem : TDADataTable read GetDataItem write SetDataItem;
    procedure ElegirTienda(const AIDTienda : Integer);
  end;


implementation

{$R *.dfm}

uses
  uFactuGES_App;

procedure TfrViewTienda.cbTiendaPropertiesValidate(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(FListaTiendas.Values[DisplayValue]);

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

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

    FDataItem.post;  
  end;
end;

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

  FListaTiendas := AppFactuGES.EmpresasController.DarListaTiendas(AppFactuGES.EmpresaActiva);
  with cbTienda.Properties.Items do
  begin
    BeginUpdate;
    try
      Clear;
      for i := 0 to FListaTiendas.Count - 1 do
        Add(FListaTiendas.Names[i]);
    finally
      EndUpdate;
    end;
  end;
end;

procedure TfrViewTienda.CustomViewDestroy(Sender: TObject);
begin
  FreeAndNIL(FListaTiendas);
  inherited;
end;

procedure TfrViewTienda.ElegirTienda(const AIDTienda: Integer);
var
  i : integer;
begin
  for i := 0 to FListaTiendas.Count-1 do
  begin
    if FListaTiendas.ValueFromIndex[i] = IntToStr(AIDTienda) then
    begin
      cbTienda.Text := FListaTiendas.Names[i];
      Break;
    end;
  end;
end;

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

procedure TfrViewTienda.SetDataItem(const Value: TDADataTable);
begin
  FDataItem := Value;
  if Assigned(FDataItem.FindField('ID_TIENDA')) then
     ElegirTienda(FDataItem.FieldByName('ID_TIENDA').AsInteger);
end;

end.
