{*******************************************************}
{                                                       }
{       Administración de puntos de venta               }
{                                                       }
{       Copyright (C) 2006 Rodax Software S.L.          }
{                                                       }
{*******************************************************}

unit uEditorListaRecepcionesAlbaran;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uBizRecepciones, uEditorGridBase, JvTabBar, uCustomView, uViewBase,
  uViewGridBase, uViewGrid, Menus, DB, uDADataTable, JvAppStorage,
  JvAppRegistryStorage, JvComponentBase, JvFormPlacement, ImgList, PngImageList,
  StdActns, ActnList, ComCtrls, TB2ExtItems, TBXExtItems, TBX, TB2Item, TB2Dock,
  TB2Toolbar, ExtCtrls, JvExControls, JvComponent, JvNavigationPane, StdCtrls,
  uViewListaRecepcionesAlbaran;


type
  IEditorListaRecepcionesAlbaran = interface(IEditorGridBase)
  ['{143EEF14-E999-4570-85A6-3F06F0F22044}']
    function GetItems: IBizRecepcionesAlbaran;
    procedure SetItems(const Value: IBizRecepcionesAlbaran);
    property Items: IBizRecepcionesAlbaran read GetItems write SetItems;
  end;

  TfEditorListaRecepcionesAlbaran = class(TfEditorGridBase, IEditorListaRecepcionesAlbaran)
    JvTabBar1: TJvTabBar;
    JvModernTabBarPainter1: TJvModernTabBarPainter;
    TBXSeparatorItem16: TTBXSeparatorItem;
    frViewListaRecepcionesAlbaran1: TfrViewListaRecepcionesAlbaran;
    procedure FormShow(Sender: TObject);
    procedure JvTabBar1TabSelected(Sender: TObject; Item: TJvTabBarItem);
    procedure actExpandirExecute(Sender: TObject);
    procedure actContraerExecute(Sender: TObject);
    procedure actAnchoAutoExecute(Sender: TObject);
    procedure actAnchoAutoUpdate(Sender: TObject);
    procedure actRefrescarExecute(Sender: TObject);
  protected
    FItems : IBizRecepcionesAlbaran;
    function GetItems: IBizRecepcionesAlbaran;
    procedure SetItems(const Value: IBizRecepcionesAlbaran);
  public
    property Items: IBizRecepcionesAlbaran read GetItems write SetItems;
    constructor Create(AOwner: TComponent); override;
  end;

implementation

uses uEditorBase, cxGridTableView, cxGridCustomTableView, cxControls,
  uDBSelectionList, cxGridDBTableView, cxGridDBDataDefinitions,
  uDataModuleRecepciones, uEditorDBBase;


{$R *.dfm}

constructor TfEditorListaRecepcionesAlbaran.Create(AOwner: TComponent);
begin
  inherited;
  ViewGrid := frViewListaRecepcionesAlbaran1;
end;

function TfEditorListaRecepcionesAlbaran.GetItems: IBizRecepcionesAlbaran;
begin
  Result := FItems;
end;

procedure TfEditorListaRecepcionesAlbaran.SetItems(const Value: IBizRecepcionesAlbaran);
begin
  FItems := Value;
  if Assigned(FItems) then
  begin
    ShowHourglassCursor;
    try
      dsDataTable.DataTable := FItems.DataTable;
      frViewListaRecepcionesAlbaran1.dsDataSource.DataTable := FItems.DataTable;

      if not FItems.DataTable.Active then
         FItems.DataTable.Active := True;
    finally
      HideHourglassCursor;
    end;
  end;
end;

procedure TfEditorListaRecepcionesAlbaran.FormShow(Sender: TObject);
begin
  inherited;
  if not Assigned(FItems) then
    raise Exception.Create('No hay items asignados');

  if not Assigned(ViewGrid) then
    raise Exception.Create('No hay una vista asignada');

  with (ViewGrid as IViewListaRecepcionesAlbaran) do
  begin
    AjustarAncho;
    ContraerTodo;
    GotoFirst;
  end;
end;

procedure TfEditorListaRecepcionesAlbaran.JvTabBar1TabSelected(
  Sender: TObject; Item: TJvTabBarItem);
begin
  inherited;
  ShowHourglassCursor;
  Items.DataTable.Active := False;
  Application.ProcessMessages;
  ViewGrid._FocusedView.BeginUpdate;
  Application.ProcessMessages;
  try
    case Item.Index of
      0     : begin
                Items := dmRecepciones.GetItemsAlbaran(tiraPendiente);
              end;

      1     : begin
                Items := dmRecepciones.GetItemsAlbaran(tiraRecibido);
              end;

      2     : begin
                Items := dmRecepciones.GetItemsAlbaran;
              end;
    end;
  finally
    dsDataTable.DataTable := Items.DataTable;
    with (ViewGrid as IViewListaRecepcionesAlbaran) do
    begin
      ExpandirTodo;
      AjustarAncho;
      ContraerTodo;
      _FocusedView.EndUpdate;
      GotoFirst;
    end;
    HideHourglassCursor;
  end;
end;

procedure TfEditorListaRecepcionesAlbaran.actExpandirExecute(
  Sender: TObject);
begin
  inherited;
  ViewGrid.ExpandirTodo;
end;

procedure TfEditorListaRecepcionesAlbaran.actContraerExecute(
  Sender: TObject);
begin
  inherited;
  ViewGrid.ContraerTodo;
end;

procedure TfEditorListaRecepcionesAlbaran.actAnchoAutoExecute(
  Sender: TObject);
begin
  inherited;
  ViewGrid.AjustarAncho;
end;

procedure TfEditorListaRecepcionesAlbaran.actAnchoAutoUpdate(
  Sender: TObject);
begin
  inherited;
  if Assigned(dsDataTable.DataTable) then
    (Sender as TAction).Enabled := (not dsDataTable.DataTable.IsEmpty)
      and Assigned(ViewGrid)
  else
    (Sender as TAction).Enabled := False;
end;

procedure TfEditorListaRecepcionesAlbaran.actRefrescarExecute(
  Sender: TObject);
begin
  inherited;
  ViewGrid.ContraerTodo;
  ViewGrid.GotoFirst;
end;

end.
