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

unit uEditorListaReferenciaGenerica;

interface

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

type
  IEditorListaReferenciaGenerica = interface(IEditorGridBase)
    ['{875E6A40-3243-4325-A28B-D415BEC291BF}']
    function GetItems: IBizReferenciaGenerica;
    procedure SetItems(const Value: IBizReferenciaGenerica);
    property Items: IBizReferenciaGenerica read GetItems
            write SetItems;
  end;

  TfEditorListaReferenciaGenerica = class(TfEditorGridBase, IEditorListaReferenciaGenerica)
    frViewListaReferenciaGenerica1: TfrViewListaReferenciaGenerica;
    JvTabBar1: TJvTabBar;
    JvModernTabBarPainter1: TJvModernTabBarPainter;
    actPendiente: TAction;
    Cambiarelestado1: TMenuItem;
    Pendiente1: TMenuItem;
    Enespera1: TMenuItem;
    Resuelto1: TMenuItem;
    TBXSubmenuItem2: TTBXSubmenuItem;
    TBXSeparatorItem16: TTBXSeparatorItem;
    TBXItem35: TTBXItem;
    TBXItem36: TTBXItem;
    TBXItem37: TTBXItem;
    actEspera: TAction;
    actResuelto: TAction;
    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 actEliminarUpdate(Sender: TObject);
    procedure actPrevisualizarUpdate(Sender: TObject);
    procedure actImprimirUpdate(Sender: TObject);
    procedure actPrevisualizarExecute(Sender: TObject);
    procedure actImprimirExecute(Sender: TObject);
    procedure actModificarUpdate(Sender: TObject);
    procedure actRefrescarExecute(Sender: TObject);
    procedure actPendienteExecute(Sender: TObject);
    procedure actPendienteUpdate(Sender: TObject);
    procedure actEsperaExecute(Sender: TObject);
    procedure actEsperaUpdate(Sender: TObject);
    procedure actResueltoUpdate(Sender: TObject);
    procedure actResueltoExecute(Sender: TObject);
    procedure actEliminarExecute(Sender: TObject);
  protected
    FItems : IBizReferenciaGenerica;
    function GetItems: IBizReferenciaGenerica;
    procedure SetItems(const Value: IBizReferenciaGenerica);
    procedure CambiarEstado (const NuevoEstado : TRefGenEstado);
  public
    property Items: IBizReferenciaGenerica read GetItems write SetItems;
    constructor Create(AOwner: TComponent); override;
  end;

implementation

uses uEditorBase, cxGridTableView, cxGridCustomTableView, cxControls,
  uDBSelectionList, uDataModuleReferenciaGenerica, cxGridDBTableView,
  cxGridDBDataDefinitions;


{$R *.dfm}

{ TfEditorListaReferenciaGenerica }

constructor TfEditorListaReferenciaGenerica.Create(AOwner: TComponent);
begin
  inherited;
  ViewGrid := frViewListaReferenciaGenerica1;
end;

function TfEditorListaReferenciaGenerica.GetItems: IBizReferenciaGenerica;
begin
  Result := FItems;
end;

procedure TfEditorListaReferenciaGenerica.SetItems(
  const Value: IBizReferenciaGenerica);
begin
  FItems := Value;
  if Assigned(FItems) then
  begin
    dsDataTable.DataTable := FItems.DataTable;
    frViewListaReferenciaGenerica1.dsDataSource.DataTable := FItems.DataTable;
  end;
end;

procedure TfEditorListaReferenciaGenerica.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;
  Items.VerPendientes;
  with (ViewGrid as IViewListaReferenciaGenerica) do
  begin
    VerPendientes;
    ExpandirTodo;
    GotoFirst;
  end;
end;

procedure TfEditorListaReferenciaGenerica.JvTabBar1TabSelected(
  Sender: TObject; Item: TJvTabBarItem);
begin
  inherited;
  case Item.Index of
    0     : begin
              Items.VerPendientes;
              (ViewGrid as IViewListaReferenciaGenerica).VerPendientes;
            end;

    1     : begin
              Items.VerEnEspera;
              (ViewGrid as IViewListaReferenciaGenerica).VerEnEspera;
            end;

    2     : begin
              Items.VerResueltos;
              (ViewGrid as IViewListaReferenciaGenerica).VerResueltos
            end;
  end;
  ViewGrid.ExpandirTodo;
  ViewGrid.GotoFirst;
end;

procedure TfEditorListaReferenciaGenerica.actModificarExecute(
  Sender: TObject);
var
  AObj : IBizReferenciaGenerica;
  ASelectedRowList : ISelectedRowList;
begin
  inherited;
  if Supports(FItems, ISelectedRowList, ASelectedRowList) then
  begin
    ShowHourglassCursor;
    try
      ViewGrid._FocusedView.BeginUpdate;
      try
        SeleccionarFilasDesdeGrid(ViewGrid._FocusedView, ASelectedRowList);
        AObj := dmReferenciaGenerica.GetSelectedItems(Items);
      finally
        ViewGrid._FocusedView.EndUpdate;
      end;
      AObj.Show;
    finally
      AObj := nil;
      ASelectedRowList := nil;
      actRefrescar.Execute;
      HideHourglassCursor
    end;
  end;
end;

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

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

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

procedure TfEditorListaReferenciaGenerica.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 TfEditorListaReferenciaGenerica.actEliminarUpdate(
  Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (Sender as TAction).Enabled and
    (JvTabBar1.SelectedTab.Index = 2);

end;

procedure TfEditorListaReferenciaGenerica.actPrevisualizarUpdate(
  Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (Sender as TAction).Enabled and
    (JvTabBar1.SelectedTab.Index < 2) and
    (ViewGrid._FocusedView.Controller.FocusedRecord is TcxGridGroupRow)
end;

procedure TfEditorListaReferenciaGenerica.actImprimirUpdate(
  Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (Sender as TAction).Enabled and
    (JvTabBar1.SelectedTab.Index < 2) and
    (ViewGrid._FocusedView.Controller.FocusedRecord is TcxGridGroupRow)
end;

procedure TfEditorListaReferenciaGenerica.actPrevisualizarExecute(
  Sender: TObject);
var
  ACentro : String;
  AVendedor : String;
  AGroupIndexInicial : Integer;
  AEstado : TRefGenEstado;
  AFiltro : String;
begin
  // inherited;
  AGroupIndexInicial := 0;

  while (ViewGrid._FocusedView.Controller.FocusedRecord is TcxGridGroupRow) do
  begin
    (ViewGrid._FocusedView.Controller.FocusedRecord as TcxGridGroupRow).Expand(True);
    ViewGrid._FocusedView.Controller.GoToNext(True);
    AGroupIndexInicial := AGroupIndexInicial + 1;
  end;

  ACentro := ViewGrid._FocusedView.Controller.FocusedRecord.Values[ViewGrid._FocusedView.GetColumnByFieldName('CODCENTRO').Index];
  if AGroupIndexInicial < 2 then
    AVendedor := ViewGrid._FocusedView.Controller.FocusedRecord.Values[ViewGrid._FocusedView.GetColumnByFieldName('CODVENDEDOR').Index];

  case JvTabBar1.SelectedTab.Index of
    0     : AEstado := rgPendiente;
    1     : AEstado := rgEnEspera;
    2     : AEstado := rgResuelto;
    else
      AEstado := rgPendiente;
  end;

  AFiltro := ViewGrid.GetFilterAsSQL;

  Items.Preview(ACentro, AVendedor, AEstado, AFiltro);
end;

procedure TfEditorListaReferenciaGenerica.actImprimirExecute(
  Sender: TObject);
var
  ACentro : String;
  AVendedor : String;
  AGroupIndexInicial : Integer;
  AString : String;
  AEstado : TRefGenEstado;
  AFiltro : String;
begin
  // inherited;
  AGroupIndexInicial := 0;

  while (ViewGrid._FocusedView.Controller.FocusedRecord is TcxGridGroupRow) do
  begin
    (ViewGrid._FocusedView.Controller.FocusedRecord as TcxGridGroupRow).Expand(True);
    ViewGrid._FocusedView.Controller.GoToNext(True);
    AGroupIndexInicial := AGroupIndexInicial + 1; 
  end;

  ACentro := ViewGrid._FocusedView.Controller.FocusedRecord.Values[ViewGrid._FocusedView.GetColumnByFieldName('CODCENTRO').Index];
  if AGroupIndexInicial < 2 then
    AVendedor := ViewGrid._FocusedView.Controller.FocusedRecord.Values[ViewGrid._FocusedView.GetColumnByFieldName('CODVENDEDOR').Index];

  case JvTabBar1.SelectedTab.Index of
    0     : AEstado := rgPendiente;
    1     : AEstado := rgEnEspera;
    2     : AEstado := rgResuelto;
    else
      AEstado := rgPendiente;
  end;

  AFiltro := ViewGrid.GetFilterAsSQL;

  Items.Print(ACentro, AVendedor, AEstado, AFiltro);

  AString := '¿Desea que las ventas del centro ' + ViewGrid._FocusedView.Controller.FocusedRecord.Values[ViewGrid._FocusedView.GetColumnByFieldName('CENTRO').Index];
  if Length(AVendedor) > 0 then
    AString := AString + #10#13 + 'y del vendedor ' + ViewGrid._FocusedView.Controller.FocusedRecord.Values[ViewGrid._FocusedView.GetColumnByFieldName('VENDEDOR').Index];

  AString := AString + #10#13 + 'pasen a espera de la rectificación?';

  if (Application.MessageBox(PAnsiChar(AString), 'Atención', MB_YESNO) = IDYES) then
    CambiarEstado(rgEnEspera);
end;

procedure TfEditorListaReferenciaGenerica.actModificarUpdate(
  Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (Sender as TAction).Enabled and
    (ViewGrid._FocusedView.Controller.SelectedRecordCount > 0)
end;

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

procedure TfEditorListaReferenciaGenerica.actPendienteExecute(
  Sender: TObject);
begin
  CambiarEstado(rgPendiente);
end;

procedure TfEditorListaReferenciaGenerica.actPendienteUpdate(
  Sender: TObject);
begin
  inherited;
  (Sender as TAction).Checked := Assigned(FItems) and
    (FItems.Estado = rgPendiente);
end;

procedure TfEditorListaReferenciaGenerica.actEsperaExecute(
  Sender: TObject);
begin
  CambiarEstado(rgEnEspera);
end;

procedure TfEditorListaReferenciaGenerica.actEsperaUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Checked := Assigned(FItems) and
    (FItems.Estado = rgEnEspera);
end;

procedure TfEditorListaReferenciaGenerica.actResueltoUpdate(
  Sender: TObject);
begin
  inherited;
  (Sender as TAction).Checked := Assigned(FItems) and
    (FItems.Estado = rgResuelto);
end;

procedure TfEditorListaReferenciaGenerica.actResueltoExecute(
  Sender: TObject);
begin
  CambiarEstado(rgResuelto);
end;

procedure TfEditorListaReferenciaGenerica.CambiarEstado(
  const NuevoEstado: TRefGenEstado);
var
  ASelectedRowList : ISelectedRowList;
  i : Integer;
begin
  inherited;
  if Assigned(FItems) and Assigned(ViewGrid) and (ViewGrid.MultiSelect) then
    if Supports(FItems, ISelectedRowList, ASelectedRowList) then
    begin
      ShowHourglassCursor;
      FItems.DataTable.DisableControls;

      try
        SeleccionarFilasDesdeGrid(ViewGrid._FocusedView, ASelectedRowList);
        try
          for i := 0 to ASelectedRowList.SelectedRows.Count - 1 do
          begin
            FItems.First;
            if FItems.Locate('RecID', ASelectedRowList.SelectedRows[i]) then
            begin
              FItems.Edit;
              FItems.FECHACAMBIO := Now;
              FItems.Estado := NuevoEstado;
              FItems.Post;
            end;
          end;
          FItems.DataTable.ApplyUpdates;
          ViewGrid._FocusedView.Controller.ClearSelection;
        except
          FItems.DataTable.CancelUpdates;
        end;
      finally
        FItems.DataTable.EnableControls;
        HideHourglassCursor;
      end;
    end;
end;

procedure TfEditorListaReferenciaGenerica.actEliminarExecute(
  Sender: TObject);
var
  AObj : IBizReferenciaGenerica;
  ASelectedRowList : ISelectedRowList;
  i : integer;
begin
//  inherited;
 if Supports(FItems, ISelectedRowList, ASelectedRowList) then
  begin
    ShowHourglassCursor;
    try
      SeleccionarFilasDesdeGrid(ViewGrid._FocusedView, ASelectedRowList);
      AObj := dmReferenciaGenerica.GetSelectedItems(Items);
      while not AObj.DataTable.EOF do
        AObj.Delete;
      AObj.DataTable.ApplyUpdates;
    finally
      AObj := nil;
      ASelectedRowList := nil;
      HideHourglassCursor;
      actRefrescar.Execute;
    end;
  end;
end;


end.
