unit uViewInfMargenArticulo;

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, uBizInfMargenArticulo,
  uViewPeriodoFechas, uViewInfMargenArticuloGrid, cxContainer, uViewFiltroBase,
  uDADataTable;

type
  IViewInfMargenArticulo = interface(IViewInformeBase)
  ['{B9426535-792E-47C3-825E-1EB378FD4AB7}']
    function GetDatos : IBizInfMargenArticulo;
    procedure SetDatos (const Value : IBizInfMargenArticulo);
    property Datos : IBizInfMargenArticulo read GetDatos write SetDatos;
  end;

  TfrViewInfMargenArticulo = class(TfrViewInformeBase, IViewInfMargenArticulo)
    actPeriodoFechas: TAction;
    frViewInfMargenArticuloGrid1: TfrViewInfMargenArticuloGrid;
    frViewPeriodoFechas1: TfrViewPeriodoFechas;
    procedure actRefrescarExecute(Sender: TObject);
    procedure actPeriodoFechasExecute(Sender: TObject);
  protected
    FDatos : IBizInfMargenArticulo;
    FModificado : Boolean;
    procedure OnViewInformeBaseGridChanged(Sender : TObject);
    procedure OnFilterChanged(Sender : TObject);
    procedure ActivarTarea(const ATarea: Integer); override;
    function GetFocusedView: TcxGridDBTableView; override;
    function GetDatos : IBizInfMargenArticulo;
    procedure SetDatos (const Value : IBizInfMargenArticulo);

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

implementation

const
  PERIODO = 0;

{$R *.dfm}

{ TfrViewInfMargenArticulo }

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

function TfrViewInfMargenArticulo.GetDatos: IBizInfMargenArticulo;
begin
  Result := FDatos;
end;

function TfrViewInfMargenArticulo.GetFocusedView: TcxGridDBTableView;
begin
  Result := frViewInfMargenArticuloGrid1.cxGridView;
end;

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

  frViewInfMargenArticuloGrid1.OnViewChanged := nil;
  frViewInfMargenArticuloGrid1.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((frViewInfMargenArticuloGrid1 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;
        frViewInfMargenArticuloGrid1.Refresh;
        frViewInfMargenArticuloGrid1.ExpandirTodo;
        frViewInfMargenArticuloGrid1.GotoFirst;
      end;
    end;

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

procedure TfrViewInfMargenArticulo.SetDatos(const Value: IBizInfMargenArticulo);
begin
  FDatos := Value;
end;

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

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

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

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

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

end.

