unit uViewTiposVenta;

interface

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

type
  IViewTiposVenta = interface(IViewBase)
  ['{B9371DA3-A682-4062-9BBE-EA688B9EACD5}']
  end;

  TfrViewTiposVenta = class(TfrViewBase, IViewTiposVenta)
    cbTiposVenta: TcxComboBox;
    dxLayoutControl1Group_Root: TdxLayoutGroup;
    dxLayoutControl1: TdxLayoutControl;
    dxLayoutControl1Item1: TdxLayoutItem;
    procedure CustomViewCreate(Sender: TObject);
    procedure CustomViewDestroy(Sender: TObject);
    procedure cbTiposVentaPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
  private
    FController : ITiposVentaController;
    FDataItem : TDADataTable;
    FListaTiposVenta : TStringList;
    function GetDataItem: TDADataTable;
    procedure SetDataItem(const Value: TDADataTable);
  public
    property DataItem : TDADataTable read GetDataItem write SetDataItem;
    procedure ElegirTiposVenta(const AIDTiposVenta : Integer);
    function getIDTipoVenta: Integer;
    procedure SetIDTipoVenta(const ID: Integer);

  end;


implementation
{$R *.dfm}


procedure TfrViewTiposVenta.cbTiposVentaPropertiesValidate(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(FListaTiposVenta.Values[DisplayValue]);

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

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

end;

procedure TfrViewTiposVenta.CustomViewCreate(Sender: TObject);
var
  i : integer;
begin
  inherited;
  FController := TTiposVentaController.create;
  with FController do
    begin
    FListaTiposVenta := DarListaTiposVenta;
    with cbTiposVenta.Properties.Items do
    begin
      BeginUpdate;
      try
        Clear;
        for i := 0 to FListaTiposVenta.Count - 1 do
          Add(FListaTiposVenta.Names[i]);
      finally
        EndUpdate;
      end;
    end;
  end;
end;

procedure TfrViewTiposVenta.CustomViewDestroy(Sender: TObject);
begin
  inherited;
  FreeAndNIL(FListaTiposVenta);
  FController := NIL;
end;

procedure TfrViewTiposVenta.ElegirTiposVenta(const AIDTiposVenta: Integer);
var
  i : integer;
begin
  for i := 0 to FListaTiposVenta.Count-1 do
  begin
    if FListaTiposVenta.ValueFromIndex[i] = IntToStr(AIDTiposVenta) then
    begin
      cbTiposVenta.Text := FListaTiposVenta.Names[i];
      Break;
    end;
  end;
end;

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

function TfrViewTiposVenta.getIDTipoVenta: Integer;
begin
{  if FListaTiposVentas.IndexOf(cbTiposVenta.Text) < 0 then
    Result := FListaTiposVentas.IndexOf(cbTiposVenta.Text)
  else
    Result := FListaIDTiposVentas.Integers[FListaTiposVentas.IndexOf(cbTiposVenta.Text)];}
end;

procedure TfrViewTiposVenta.SetDataItem(const Value: TDADataTable);
begin
  FDataItem := Value;
  if Assigned(FDataItem.FindField('ID_TIPOVENTA')) then
    ElegirTiposVenta(FDataItem.FieldByName('ID_TIPOVENTA').AsInteger);
end;

procedure TfrViewTiposVenta.SetIDTipoVenta(const ID: Integer);
var
  AIndex: Integer;
begin
{  if FListaIDTiposVentas.Find(ID, AIndex) then
     cbTiposVenta.Text := FListaTiposVentas.Strings[AIndex];}
end;

end.
