unit uViewInfMargenArticuloGrid;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewInformeBaseGrid, DB, uDADataTable, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData,
  cxTextEdit, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid,
  cxCurrencyEdit, cxCalendar, dxPSGlbl, dxPSUtl, dxPrnPg, dxBkgnd, dxWrap,
  dxPrnDev, dxPSEngn, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns, dxPSCore, dxPScxCommon, dxPScxGridLnk, dxPgsDlg,
  cxDropDownEdit, StdCtrls, cxSpinEdit;

type
  IViewInfMargenArticuloGrid = interface(IViewInformeBaseGrid)
  ['{19EAFDBF-857D-4A6B-851A-DDB7A3CB281B}']
  end;

  TfrViewInfMargenArticuloGrid = class(TfrViewInformeBaseGrid, IViewInfMargenArticuloGrid)
    cxGrid: TcxGrid;
    cxGridView: TcxGridDBTableView;
    cxGridLevel: TcxGridLevel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyleRed: TcxStyle;
    cxStyleBlue: TcxStyle;
    cxStyleFilterBox: TcxStyle;
    cxGridViewRecID: TcxGridDBColumn;
    cxGridViewID_EMPRESA: TcxGridDBColumn;
    cxGridViewID_CLIENTE: TcxGridDBColumn;
    cxGridViewID_ARTICULO: TcxGridDBColumn;
    cxGridViewFAMILIA: TcxGridDBColumn;
    cxGridViewREFERENCIA: TcxGridDBColumn;
    cxGridViewREFERENCIA_PROV: TcxGridDBColumn;
    cxGridViewDESCRIPCION: TcxGridDBColumn;
    cxGridViewCOMISIONABLE: TcxGridDBColumn;
    cxGridViewINVENTARIABLE: TcxGridDBColumn;
    cxGridViewIMP_UNIDAD_COMPRA: TcxGridDBColumn;
    cxGridViewIMP_NETO_COMPRA: TcxGridDBColumn;
    cxGridViewIMP_PORTE_COMPRA: TcxGridDBColumn;
    cxGridViewIMP_MARGEN_ULT_VENT_COMPRA: TcxGridDBColumn;
    cxGridViewPOR_MARGEN_ULT_VENT_COMPRA: TcxGridDBColumn;
    cxGridViewPOR_MARGEN_ULT_VENT_ULT_COMP: TcxGridDBColumn;
    cxGridViewNIF_CIF_CLIENTE: TcxGridDBColumn;
    cxGridViewNOMBRE_CLIENTE: TcxGridDBColumn;
    cxGridViewNOMBRE_COMERCIAL_CLIENTE: TcxGridDBColumn;
    cxGridViewNIF_CIF_AGENTE: TcxGridDBColumn;
    cxGridViewNOMBRE_AGENTE: TcxGridDBColumn;
    cxGridViewIMP_MARGEN_PRO_VENT_PRO_COMP: TcxGridDBColumn;
    cxGridViewIMP_MARGEN_ULT_VENT_ULT_COMP: TcxGridDBColumn;
    cxGridViewIMP_UNIDAD_ULT_COMP: TcxGridDBColumn;
    cxGridViewIMP_NETO_ULT_COMP: TcxGridDBColumn;
    cxGridViewIMP_PORTE_ULT_COMP: TcxGridDBColumn;
    cxGridViewIMP_UNIDAD_PRO_COMP: TcxGridDBColumn;
    cxGridViewIMP_NETO_PRO_COMP: TcxGridDBColumn;
    cxGridViewIMP_PORTE_PRO_COMP: TcxGridDBColumn;
    cxGridViewIMP_MARGEN_ULT_VENT_PRO_COMP: TcxGridDBColumn;
    cxGridViewPOR_MARGEN_ULT_VENT_PRO_COMP: TcxGridDBColumn;
    cxGridViewIMP_MARGEN_PRO_VENT_COMPRA: TcxGridDBColumn;
    cxGridViewPOR_MARGEN_PRO_VENT_COMPRA: TcxGridDBColumn;
    cxGridViewIMP_MARGEN_PRO_VENT_ULT_COMP: TcxGridDBColumn;
    cxGridViewPOR_MARGEN_PRO_VENT_ULT_COMP: TcxGridDBColumn;
    cxGridViewIMP_UNIDAD_ULT_VENT: TcxGridDBColumn;
    cxGridViewIMP_NETO_ULT_VENT: TcxGridDBColumn;
    cxGridViewIMP_PORTE_ULT_VENT: TcxGridDBColumn;
    cxGridViewIMP_UNIDAD_PRO_VENT: TcxGridDBColumn;
    cxGridViewIMP_NETO_PRO_VENT: TcxGridDBColumn;
    cxGridViewIMP_PORTE_PRO_VENT: TcxGridDBColumn;
    cxGridViewPOR_MARGEN_PRO_VENT_PRO_COMP: TcxGridDBColumn;
    procedure cxGridViewStylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridViewColumnPosChanged(Sender: TcxGridTableView;
      AColumn: TcxGridColumn);
    procedure cxGridViewColumnSizeChanged(Sender: TcxGridTableView;
      AColumn: TcxGridColumn);
    procedure cxGridViewDataControllerGroupingChanged(Sender: TObject);
    procedure cxGridViewDataControllerSortingChanged(Sender: TObject);
    procedure cxGridViewDataControllerSummaryAfterSummary(ASender: TcxDataSummary);
    procedure cxGridViewDataControllerFilterChanged(Sender: TObject);
  protected
    procedure ActivarEventos; override;
    procedure DesactivarEventos; override;
    function GetFocusedView : TcxGridDBTableView; override;
  public
    procedure Refresh; override;
    constructor Create(AOwner: TComponent); override;
    function HayColumnasDePromedio: boolean;
  end;

implementation

uses
  cxVariants, uDateUtils;

{$R *.dfm}

{ TfrInfMargenArticuloGrid }

function TfrViewInfMargenArticuloGrid.GetFocusedView: TcxGridDBTableView;
begin
  Result := cxGridView;
end;

function TfrViewInfMargenArticuloGrid.HayColumnasDePromedio: boolean;
var
  i: Integer;
begin
  Result := False;

  //Para que el usuario pueda ocultar y desocultar columnas sin afectar al informe
  for i := 0 to FocusedView.ColumnCount - 1 do
  begin
    if  (FocusedView.Columns[i].Visible)
    and (Pos('_PRO_', FocusedView.Columns[i].DataBinding.FieldName) <> 0) then
    begin
       Result := true;
       break;
    end;
  end;
end;

procedure TfrViewInfMargenArticuloGrid.Refresh;
var
  FocusedRow, TopRow, i : Integer;
begin
  inherited;
  { Refrescar las filas del grid a partir del dataset y colocar la fila
    seleccionada donde estaba }
  if FocusedView.DataController.DataSource.DataSet.State = dsBrowse then
  begin
    TopRow := FocusedView.Controller.TopRowIndex;
    FocusedRow := FocusedView.DataController.FocusedRowIndex;

    FocusedView.DataController.UpdateItems(False);  // <- Refresco

    FocusedView.DataController.FocusedRowIndex := FocusedRow;
    FocusedView.Controller.TopRowIndex := TopRow;

    //Para que el usuario pueda ocultar y desocultar columnas sin afectar al informe
    for i := 0 to FocusedView.ColumnCount - 1 do
        FocusedView.Columns[i].Hidden := not FocusedView.Columns[i].Visible;
  end;
end;

procedure TfrViewInfMargenArticuloGrid.cxGridViewStylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  AColumn : TcxGridDBColumn;
  AFecha : TDateTime;
begin
  inherited;

  // La fila es la cabecera de un grupo
{  if ARecord.Expandable then
    Exit;

  AColumn := FocusedView.GetColumnByFieldName('FECHA');
  if Assigned(AColumn) and (AColumn.Visible) then
  begin
    try
      AFecha := VarToDateTime(ARecord.Values[AColumn.Index]);
    except
      on Exception do
        ShowMessage(VarToStr(ARecord.Values[AColumn.Index]));
    end;

    if DaysBetween(Now, AFecha) > 14 then
      AStyle := cxStyleRed
    else
      if DaysBetween(Now, AFecha) > 7 then
        AStyle := cxStyleBlue
  end; }
end;

procedure TfrViewInfMargenArticuloGrid.cxGridViewColumnPosChanged(
  Sender: TcxGridTableView; AColumn: TcxGridColumn);
begin
  inherited;
  if Assigned(FOnViewChanged) then
    FOnViewChanged(Self);
end;

procedure TfrViewInfMargenArticuloGrid.cxGridViewColumnSizeChanged(
  Sender: TcxGridTableView; AColumn: TcxGridColumn);
begin
  inherited;
  if Assigned(FOnViewChanged) then
    FOnViewChanged(Self);
end;

procedure TfrViewInfMargenArticuloGrid.cxGridViewDataControllerGroupingChanged(
  Sender: TObject);
begin
  inherited;
  if Assigned(FOnViewChanged) then
    FOnViewChanged(Self);
end;

procedure TfrViewInfMargenArticuloGrid.cxGridViewDataControllerSortingChanged(
  Sender: TObject);
begin
  inherited;
  if Assigned(FOnViewChanged) then
    FOnViewChanged(Self);
end;

procedure TfrViewInfMargenArticuloGrid.cxGridViewDataControllerSummaryAfterSummary(
  ASender: TcxDataSummary);
begin
  inherited;
  if Assigned(FOnViewChanged) then
    FOnViewChanged(Self);
end;

procedure TfrViewInfMargenArticuloGrid.cxGridViewDataControllerFilterChanged(
  Sender: TObject);
begin
  if Assigned(FOnFilterChanged) then
    FOnFilterChanged(Self);
end;

constructor TfrViewInfMargenArticuloGrid.Create(AOwner: TComponent);
begin
  inherited;
end;

procedure TfrViewInfMargenArticuloGrid.ActivarEventos;
begin
  inherited;
  with cxGridView do
  begin
    DataController.Filter.OnChanged := cxGridViewDataControllerFilterChanged;
    DataController.Summary.OnAfterSummary := cxGridViewDataControllerSummaryAfterSummary;
    DataController.OnGroupingChanged := cxGridViewDataControllerGroupingChanged;
    DataController.OnSortingChanged := cxGridViewDataControllerSortingChanged;
    OnColumnPosChanged := cxGridViewColumnPosChanged;
    OnColumnSizeChanged := cxGridViewColumnSizeChanged;
  end;
end;

procedure TfrViewInfMargenArticuloGrid.DesactivarEventos;
begin
  inherited;
  with cxGridView do
  begin
    DataController.Filter.OnChanged := nil;
    DataController.Summary.OnAfterSummary := nil;
    DataController.OnGroupingChanged := nil;
    DataController.OnSortingChanged := nil;
    OnColumnPosChanged := nil;
    OnColumnSizeChanged := nil;
  end;
end;

end.

