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

unit uEditorListaTraspasos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, 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,
  uViewListaTraspasos, uBizTraspasos;


type
  IEditorListaTraspasos = interface(IEditorGridBase)
    ['{729D3CD7-88E7-4D97-A9A0-8E0E1CF74716}']
    function GetItems: IBizTraspasos;
    procedure SetItems(const Value: IBizTraspasos);
    property Items: IBizTraspasos read GetItems
            write SetItems;
  end;

  TfEditorListaTraspasos = class(TfEditorGridBase, IEditorListaTraspasos)
    JvTabBar1: TJvTabBar;
    JvModernTabBarPainter1: TJvModernTabBarPainter;
    actPendiente: TAction;
    Cambiarelestado1: TMenuItem;
    Pendiente1: TMenuItem;
    Enespera1: TMenuItem;
    TBXSubmenuItem2: TTBXSubmenuItem;
    TBXSeparatorItem16: TTBXSeparatorItem;
    TBXItem36: TTBXItem;
    TBXItem37: TTBXItem;
    actAprobado: TAction;
    frViewListaTraspasos1: TfrViewListaTraspasos;
    procedure FormShow(Sender: TObject);
    procedure JvTabBar1TabSelected(Sender: TObject; Item: TJvTabBarItem);
    procedure actModificarExecute(Sender: TObject);
    procedure actExpandirExecute(Sender: TObject);
    procedure actContraerExecute(Sender: TObject);
    procedure actAnchoAutoExecute(Sender: TObject);
    procedure actAnchoAutoUpdate(Sender: TObject);
    procedure actModificarUpdate(Sender: TObject);
    procedure actRefrescarExecute(Sender: TObject);
    procedure actPendienteExecute(Sender: TObject);
    procedure actPendienteUpdate(Sender: TObject);
    procedure actAprobadoExecute(Sender: TObject);
    procedure actAprobadoUpdate(Sender: TObject);
    procedure actEliminarExecute(Sender: TObject);
  protected
    FItems : IBizTraspasos;
    function GetItems: IBizTraspasos;
    procedure SetItems(const Value: IBizTraspasos);
    procedure CambiarEstado(const NuevoEstado: TInvTraspasoEstado);
    function GetModified: Boolean; override;    
  public
    property Items: IBizTraspasos read GetItems write SetItems;
    constructor Create(AOwner: TComponent); override;
  end;

implementation

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


{$R *.dfm}

constructor TfEditorListaTraspasos.Create(AOwner: TComponent);
begin
  inherited;
  ViewGrid := frViewListaTraspasos1;
end;

function TfEditorListaTraspasos.GetItems: IBizTraspasos;
begin
  Result := FItems;
end;

procedure TfEditorListaTraspasos.SetItems(
  const Value: IBizTraspasos);
begin
  FItems := Value;
  if Assigned(FItems) then
  begin
    dsDataTable.DataTable := FItems.DataTable;
    frViewListaTraspasos1.dsDataSource.DataTable := FItems.DataTable;
  end;
end;

procedure TfEditorListaTraspasos.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');

  FItems.DataTable.Active := True;

  with (ViewGrid as IViewListaTraspasos) do
  begin
    VerPendientes;
    ContraerTodo;
    GotoFirst;
  end;
end;

procedure TfEditorListaTraspasos.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 := dmTraspasos.GetItems(titPendiente);
                (ViewGrid as IViewListaTraspasos).VerPendientes;
              end;

      1     : begin
                Items := dmTraspasos.GetItems(titAprobado);
                (ViewGrid as IViewListaTraspasos).VerAprobados;
              end;

      2     : begin
                Items := dmTraspasos.GetItems;
                (ViewGrid as IViewListaTraspasos).VerTodos;
              end;
    end;
  finally
    dsDataTable.DataTable := Items.DataTable;
    ViewGrid._FocusedView.EndUpdate;
    Items.DataTable.Active := True;
    ViewGrid.ContraerTodo;
    ViewGrid.GotoFirst;
    HideHourglassCursor;
  end;
end;

procedure TfEditorListaTraspasos.actModificarExecute(
  Sender: TObject);
var
  bm : TBookMark ;
begin
  bm := FItems.DataTable.GetBookMark;
  FItems.DataTable.DisableControls;
  try
    FItems.Show;
    try
      // No hay BookmarkIsValid
      FItems.DataTable.GotoBookMark(bm);
    except
      //
    end;
  finally
    FItems.DataTable.FreeBookMark(BM);
    FItems.DataTable.EnableControls;
    if ViewGrid._FocusedView.Controller.SelectedRowCount > 0 then
      ViewGrid._FocusedView.Controller.SelectedRows[0].Focused := True;
  end;
end;

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

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

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

procedure TfEditorListaTraspasos.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 TfEditorListaTraspasos.actModificarUpdate(
  Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (Sender as TAction).Enabled and
    (ViewGrid._FocusedView.Controller.SelectedRecordCount < 2) and
    (Assigned(ViewGrid._FocusedView.Controller.FocusedRow) and
     not (ViewGrid._FocusedView.Controller.FocusedRow.Expandable))
end;

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

procedure TfEditorListaTraspasos.actPendienteExecute(
  Sender: TObject);
begin
  CambiarEstado(titPendiente);
end;

procedure TfEditorListaTraspasos.actPendienteUpdate(
  Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled :=
    (Assigned(ViewGrid._FocusedView.Controller.FocusedRow) and
     not (ViewGrid._FocusedView.Controller.FocusedRow.Expandable));

  (Sender as TAction).Checked := Assigned(FItems) and
    (FItems.Estado = titPendiente);
end;

procedure TfEditorListaTraspasos.actAprobadoExecute(
  Sender: TObject);
begin
  CambiarEstado(titAprobado);
end;

procedure TfEditorListaTraspasos.actAprobadoUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled :=
    (Assigned(ViewGrid._FocusedView.Controller.FocusedRow) and
     not (ViewGrid._FocusedView.Controller.FocusedRow.Expandable));

  (Sender as TAction).Checked := Assigned(FItems) and
    (FItems.Estado = titAprobado);
end;

procedure TfEditorListaTraspasos.CambiarEstado(
  const NuevoEstado: TInvTraspasoEstado);
var
  ARecord: TcxCustomGridRecord;
  i : Integer;
  bm : TBookMark ;
begin
  if Assigned(FItems) and Assigned(ViewGrid) and (ViewGrid.MultiSelect) then
  begin
    ShowHourglassCursor;
    bm := FItems.DataTable.GetBookMark;
    FItems.DataTable.DisableControls;
    try
      for i := 0 to ViewGrid._FocusedView.Controller.SelectedRecordCount-1 do
      begin
        ARecord := ViewGrid._FocusedView.Controller.SelectedRecords[i];
        if (ARecord is TcxGridDataRow) then
        begin
          (ARecord as TcxGridDataRow).Focused := True;
          FItems.Edit;
          FItems.Estado := NuevoEstado;
          FItems.Post;
        end;
      end;
      FItems.DataTable.ApplyUpdates;
      FItems.DataTable.GotoBookMark(bm);
    finally
      FItems.DataTable.FreeBookMark(BM);
      FItems.DataTable.EnableControls;
      ViewGrid._FocusedView.Controller.ClearSelection;
      HideHourglassCursor;
    end;
  end;
end;

procedure TfEditorListaTraspasos.actEliminarExecute(
  Sender: TObject);
begin
//  inherited;
end;


function TfEditorListaTraspasos.GetModified: Boolean;
begin
  Result := False;
end;

end.
