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

unit uEditorListaDevoluciones;

interface

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


type
  IEditorListaDevoluciones = interface(IEditorGridBase)
    ['{F7023263-A26E-4C09-87AF-02275AD594C4}']
    function GetItems: IBizDevoluciones;
    procedure SetItems(const Value: IBizDevoluciones);
    property Items: IBizDevoluciones read GetItems
            write SetItems;
  end;

  TfEditorListaDevoluciones = class(TfEditorGridBase, IEditorListaDevoluciones)
    JvTabBar1: TJvTabBar;
    JvModernTabBarPainter1: TJvModernTabBarPainter;
    actPendiente: TAction;
    Cambiarelestado1: TMenuItem;
    Pendiente1: TMenuItem;
    Enespera1: TMenuItem;
    TBXSubmenuItem2: TTBXSubmenuItem;
    TBXSeparatorItem16: TTBXSeparatorItem;
    TBXItem36: TTBXItem;
    TBXItem37: TTBXItem;
    actAprobado: TAction;
    frViewListaDevoluciones1: TfrViewListaDevoluciones;
    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 : IBizDevoluciones;
    function GetItems: IBizDevoluciones;
    procedure SetItems(const Value: IBizDevoluciones);
    procedure CambiarEstado(const NuevoEstado: TInvDevolucionestado);
    function GetModified: Boolean; override;     
  public
    property Items: IBizDevoluciones read GetItems write SetItems;
    constructor Create(AOwner: TComponent); override;
  end;

implementation

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


{$R *.dfm}

constructor TfEditorListaDevoluciones.Create(AOwner: TComponent);
begin
  inherited;
  ViewGrid := frViewListaDevoluciones1;
end;

function TfEditorListaDevoluciones.GetItems: IBizDevoluciones;
begin
  Result := FItems;
end;

procedure TfEditorListaDevoluciones.SetItems(
  const Value: IBizDevoluciones);
begin
  FItems := Value;
  if Assigned(FItems) then
  begin
    dsDataTable.DataTable := FItems.DataTable;
    frViewListaDevoluciones1.dsDataSource.DataTable := FItems.DataTable;
  end;
end;

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

procedure TfEditorListaDevoluciones.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 := dmDevoluciones.GetItems(tidPendiente);
                (ViewGrid as IViewListaDevoluciones).VerPendientes;
              end;

      1     : begin
                Items := dmDevoluciones.GetItems(tidAprobado);
                (ViewGrid as IViewListaDevoluciones).VerAprobados;
              end;

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

procedure TfEditorListaDevoluciones.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;
  end;
end;

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

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

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

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

procedure TfEditorListaDevoluciones.actPendienteExecute(
  Sender: TObject);
begin
  CambiarEstado(tidPendiente);
end;

procedure TfEditorListaDevoluciones.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 = tidPendiente);
end;

procedure TfEditorListaDevoluciones.actAprobadoExecute(
  Sender: TObject);
begin
  CambiarEstado(tidAprobado);
end;

procedure TfEditorListaDevoluciones.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 = tidAprobado);
end;

procedure TfEditorListaDevoluciones.CambiarEstado(
  const NuevoEstado: TInvDevolucionestado);
var
  ARecord: TcxCustomGridRecord;
  i : Integer;
begin
  if Assigned(FItems) and Assigned(ViewGrid) and (ViewGrid.MultiSelect) then
  begin
    ShowHourglassCursor;
    //ViewGrid._FocusedView.BeginUpdate;
    FItems.DataTable.DisableControls;

    Application.ProcessMessages;
    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;
        Application.ProcessMessages;
      end;
      FItems.DataTable.ApplyUpdates;
    finally
      //ViewGrid._FocusedView.EndUpdate;
      FItems.DataTable.EnableControls;
      ViewGrid._FocusedView.Controller.ClearSelection;
      HideHourglassCursor;
      Application.ProcessMessages;
    end;
  end;
end;

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


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

end.
