unit uViewInfVentasArticulo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewInformeBase, ActnList, ImgList, PngImageList, uViewGrid,
  uViewInformeBaseGrid, uViewSumarios, TBXDkPanels, StdCtrls,
  uViewAgrupaciones, uCustomView, uViewBase, uViewColumnas, ComCtrls,
  TB2Dock, uViewInformeBaseParametros, uBizInformes, cxStyles,
  cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB,
  cxDBData, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  cxTextEdit, uViewFiltros, uBizInfVentasArticulo,
  uViewPeriodoFechas, uViewInfVentasArticuloGrid, uDADataTable, uViewFiltroBase;

type
  IViewInfVentasArticulo = interface(IViewInformeBase)
    ['{B8B9105B-E833-4E7D-BA36-EB9A7EBDCE69}']
    function GetDatos : IBizInfVentasArticulo;
    procedure SetDatos (const Value : IBizInfVentasArticulo);
    property Datos : IBizInfVentasArticulo read GetDatos write SetDatos;
  end;

  TfrViewInfVentasArticulo = class(TfrViewInformeBase, IViewInfVentasArticulo)
    frViewPeriodoFechas1: TfrViewPeriodoFechas;
    actPeriodoFechas: TAction;
    frViewInfVentasArticuloGrid1: TfrViewInfVentasArticuloGrid;
    procedure actRefrescarExecute(Sender: TObject);
    procedure actPeriodoFechasExecute(Sender: TObject);
  protected
    FDatos : IBizInfVentasArticulo;
    FModificado : Boolean;
    procedure OnViewInformeBaseGridChanged(Sender : TObject);
    procedure OnFilterChanged(Sender : TObject);
    procedure ActivarTarea(const ATarea: Integer); override;
    function GetFocusedView: TcxGridDBTableView; override;
    function GetDatos : IBizInfVentasArticulo;
    procedure SetDatos (const Value : IBizInfVentasArticulo);

  public
    property Datos : IBizInfVentasArticulo read GetDatos write SetDatos;
    constructor Create(AOwner: TComponent); override;
    procedure Refresh; override;
  end;

implementation

const
  PERIODO = 0;

{$R *.dfm}

{ TfrViewInfVentasArticulo }

constructor TfrViewInfVentasArticulo.Create(AOwner: TComponent);
begin
  inherited;
  frViewInfVentasArticuloGrid1.OnViewChanged := OnViewInformeBaseGridChanged;
  frViewInfVentasArticuloGrid1.OnFilterChanged := OnFilterChanged;
  frViewColumnas1.ViewInformeBaseGrid := frViewInfVentasArticuloGrid1;
  frViewAgrupaciones1.ViewInformeBaseGrid := frViewInfVentasArticuloGrid1;
  frViewSumarios1.ViewInformeBaseGrid := frViewInfVentasArticuloGrid1;
  frViewFiltros1.ViewInformeBaseGrid := frViewInfVentasArticuloGrid1;
  pcParametros.ActivePage := TabSheet5;
end;

function TfrViewInfVentasArticulo.GetDatos: IBizInfVentasArticulo;
begin
  Result := FDatos;
end;

function TfrViewInfVentasArticulo.GetFocusedView: TcxGridDBTableView;
begin
  Result := frViewInfVentasArticuloGrid1.cxGridView;
end;

procedure TfrViewInfVentasArticulo.Refresh;
var
  AColumns : TStringList;
  AColumnsGroup : TStringList;
  i: Integer;
begin
  inherited;
  ShowHourglassCursor;
  actRefrescar.Enabled := False;

  frViewInfVentasArticuloGrid1.OnViewChanged := nil;
  frViewInfVentasArticuloGrid1.OnFilterChanged := nil;

  try
    // Calcular las columnas a pedir al servidor
    AColumns := frViewColumnas1.GetCheckedColumns;
    AColumns.sorted := true;
    AColumns.Duplicates := dupIgnore;

    //Aņadimos el nombre de los campos a los que referencia los captions de agrupacion
    AColumnsGroup := frViewAgrupaciones1.GetAgrupaciones;
    for i:= 0 to AColumnsGroup.Count - 1 do
      AColumns.Add((frViewInfVentasArticuloGrid1 as IViewInformeBaseGrid).GetColumnByCaption(AColumnsGroup.Strings[i]).DataBinding.FieldName);

   FDatos.VisibleColumns := AColumns.CommaText;

    with Datos.DataTable do
    begin
      DisableControls;
      try
        Active := False;

        LoadSchema;

        ParamByName('FECHAINI').AsDateTime := frViewPeriodoFechas1.FechaInicial;
        ParamByName('FECHAFIN').AsDateTime := frViewPeriodoFechas1.FechaFinal;

        Active := True;

        frViewColumnas1.Execute;
        frViewAgrupaciones1.Execute;
        frViewSumarios1.Refresh;
        frViewFiltros1.Refresh;
        pnlTareas.Visible := True;

        if Assigned(FOnViewChanged) then
          FOnViewChanged(Self);
      finally
        EnableControls;
        frViewInfVentasArticuloGrid1.Refresh;
        frViewInfVentasArticuloGrid1.ExpandirTodo;
        frViewInfVentasArticuloGrid1.GotoFirst;
      end;
    end;

  finally
    FreeAndNil(AColumns);
//    ViewInfVentasArticuloGrid.OnViewChanged := OnViewInformeBaseGridChanged;
//    ViewInfVentasArticuloGrid.OnFilterChanged := OnFilterChanged;
    actRefrescar.Enabled := True;
    HideHourglassCursor;
  end;
end;

procedure TfrViewInfVentasArticulo.SetDatos(const Value: IBizInfVentasArticulo);
begin
  FDatos := Value;
end;

procedure TfrViewInfVentasArticulo.actRefrescarExecute(Sender: TObject);
begin
  inherited;
  Refresh;
end;

procedure TfrViewInfVentasArticulo.OnViewInformeBaseGridChanged(
  Sender: TObject);
begin
  if Assigned(Self.OnViewChanged) then
    Self.OnViewChanged(Self);
end;

procedure TfrViewInfVentasArticulo.OnFilterChanged(Sender: TObject);
begin
  frViewFiltros1.Refresh;
end;

procedure TfrViewInfVentasArticulo.actPeriodoFechasExecute(
  Sender: TObject);
begin
  inherited;
  ActivarTarea(PERIODO);
end;

procedure TfrViewInfVentasArticulo.ActivarTarea(const ATarea: Integer);
begin
  inherited;
  case ATarea of
    PERIODO    : pcParametros.ActivePage := TabSheet5;
  end;
end;

end.
