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

unit uEditorListaRecepciones;

interface

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

const
  CM_SETFOCUSEDRECORD = WM_USER + 1002;
  
type
  IEditorListaRecepciones = interface(IEditorGridBase)
    ['{5CF9C341-A9BB-4A42-A132-0145827FBD9C}']
    function GetItems: IBizRecepciones;
    procedure SetItems(const Value: IBizRecepciones);
    property Items: IBizRecepciones read GetItems
            write SetItems;
  end;

  TfEditorListaRecepciones = class(TfEditorGridBase, IEditorListaRecepciones)
    JvTabBar1: TJvTabBar;
    JvModernTabBarPainter1: TJvModernTabBarPainter;
    actPendiente: TAction;
    Cambiarelestado1: TMenuItem;
    Pendiente1: TMenuItem;
    Enespera1: TMenuItem;
    TBXSubmenuItem2: TTBXSubmenuItem;
    TBXSeparatorItem16: TTBXSeparatorItem;
    TBXItem36: TTBXItem;
    TBXItem37: TTBXItem;
    actAprobado: TAction;
    frViewListaRecepciones1: TfrViewListaRecepciones;
    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 : IBizRecepciones;
    function GetItems: IBizRecepciones;
    procedure SetItems(const Value: IBizRecepciones);
    procedure CambiarEstado(const NuevoEstado: TInvRecepcionEstado);
    function GetModified: Boolean; override;
    procedure CMSetFocusedRecord(var Msg: TMessage); message CM_SETFOCUSEDRECORD;
  public
    property Items: IBizRecepciones read GetItems write SetItems;
    constructor Create(AOwner: TComponent); override;
  end;

implementation

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


{$R *.dfm}

constructor TfEditorListaRecepciones.Create(AOwner: TComponent);
begin
  inherited;
  ViewGrid := frViewListaRecepciones1;
end;

function TfEditorListaRecepciones.GetItems: IBizRecepciones;
begin
  Result := FItems;
end;

procedure TfEditorListaRecepciones.SetItems(
  const Value: IBizRecepciones);
begin
  FItems := Value;
  if Assigned(FItems) then
  begin
    dsDataTable.DataTable := FItems.DataTable;
    frViewListaRecepciones1.dsDataSource.DataTable := FItems.DataTable;
  end;
end;

procedure TfEditorListaRecepciones.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 IViewListaRecepciones) do
  begin
    VerPendientes;
    ContraerTodo;
    GotoFirst;
  end;
end;

procedure TfEditorListaRecepciones.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.GetItems(tirPendiente);
                (ViewGrid as IViewListaRecepciones).VerPendientes;
              end;

      1     : begin
                Items := dmRecepciones.GetItems(tirAprobado);
                (ViewGrid as IViewListaRecepciones).VerAprobados;
              end;

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

procedure TfEditorListaRecepciones.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 TfEditorListaRecepciones.actExpandirExecute(
  Sender: TObject);
begin
  inherited;
  ViewGrid.ExpandirTodo;
end;

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

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

procedure TfEditorListaRecepciones.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 TfEditorListaRecepciones.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 TfEditorListaRecepciones.actRefrescarExecute(
  Sender: TObject);
begin
  inherited;
  ViewGrid.ContraerTodo;
  ViewGrid.GotoFirst;  
end;

procedure TfEditorListaRecepciones.actPendienteExecute(
  Sender: TObject);
begin
  CambiarEstado(tirPendiente);
end;

procedure TfEditorListaRecepciones.actPendienteUpdate(
  Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled :=
    (Assigned(ViewGrid._FocusedView.Controller.FocusedRow) and
    (ViewGrid._FocusedView.Controller.FocusedRow.Level <> 0));

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

procedure TfEditorListaRecepciones.actAprobadoExecute(
  Sender: TObject);
begin
  CambiarEstado(tirAprobado);
end;

procedure TfEditorListaRecepciones.actAprobadoUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled :=
    (Assigned(ViewGrid._FocusedView.Controller.FocusedRow) and
    (ViewGrid._FocusedView.Controller.FocusedRow.Level <> 0));

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

procedure TfEditorListaRecepciones.CambiarEstado(
  const NuevoEstado: TInvRecepcionEstado);
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; <- No descomentar
    ViewGrid._FocusedView.BeginUpdate;
    try
      for i := 0 to ViewGrid._FocusedView.Controller.SelectedRecordCount-1 do
      begin
        ARecord := ViewGrid._FocusedView.Controller.SelectedRecords[i];
        if (ARecord is TcxGridDataRow) then
        begin
          if (ARecord as TcxGridDataRow).CanFocus then
          begin
            (ARecord as TcxGridDataRow).Focused := True;
            FItems.Edit;
            FItems.Estado := NuevoEstado;
            FItems.Post;
          end;
        end;
      end;
      FItems.DataTable.ApplyUpdates;
      FItems.DataTable.GotoBookMark(bm);
    finally
      FItems.DataTable.FreeBookMark(BM);
      //FItems.DataTable.EnableControls; <- No descomentar
      ViewGrid._FocusedView.EndUpdate;
      ViewGrid._FocusedView.Controller.ClearSelection;
      PostMessage(Handle, CM_SETFOCUSEDRECORD, Integer(ViewGrid._FocusedView),
        MakeLParam(ViewGrid._FocusedView.Controller.FocusedRowIndex,
        ViewGrid._FocusedView.Controller.TopRowIndex));
      HideHourglassCursor;
    end;
  end;
end;

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


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

procedure TfEditorListaRecepciones.CMSetFocusedRecord(var Msg: TMessage);
begin
  TcxGridDBTableView(msg.WParam).Controller.FocusedRowIndex := Msg.LParamLo;
  TcxGridDBTableView(msg.WParam).Controller.TopRowIndex := Msg.LParamHi;
end;

end.
