unit uViewInfVentasArticuloGrid;

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
  IViewInfVentasArticuloGrid = interface(IViewInformeBaseGrid)
  ['{5B66E9E4-4789-4ECE-99EF-337A52D10460}']
  end;

  TfrViewInfVentasArticuloGrid = class(TfrViewInformeBaseGrid, IViewInfVentasArticuloGrid)
    cxGrid: TcxGrid;
    cxGridView: TcxGridDBTableView;
    cxGridLevel: TcxGridLevel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyleRed: TcxStyle;
    cxStyleBlue: TcxStyle;
    cxStyleFilterBox: TcxStyle;
    cxGridViewRecID: TcxGridDBColumn;
    cxGridViewID_FACTURA: TcxGridDBColumn;
    cxGridViewID_EMPRESA: TcxGridDBColumn;
    cxGridViewREFERENCIA_FACTURA: TcxGridDBColumn;
    cxGridViewFECHA_FACTURA: TcxGridDBColumn;
    cxGridViewIMPORTE_TOTAL_FACTURA: TcxGridDBColumn;
    cxGridViewID_COMISION_FACTURA: TcxGridDBColumn;
    cxGridViewID_ARTICULO: TcxGridDBColumn;
    cxGridViewFAMILIA: TcxGridDBColumn;
    cxGridViewREFERENCIA: TcxGridDBColumn;
    cxGridViewREFERENCIA_PROV: TcxGridDBColumn;
    cxGridViewDESCRIPCION: TcxGridDBColumn;
    cxGridViewCOMISIONABLE: TcxGridDBColumn;
    cxGridViewINVENTARIABLE: TcxGridDBColumn;
    cxGridViewIMPORTE_UNIDAD_COSTE: TcxGridDBColumn;
    cxGridViewDESCUENTO_PROVEEDOR_COSTE: TcxGridDBColumn;
    cxGridViewIMPORTE_NETO_COSTE: TcxGridDBColumn;
    cxGridViewIMPORTE_PORTE_COSTE: TcxGridDBColumn;
    cxGridViewCANTIDAD: TcxGridDBColumn;
    cxGridViewIMPORTE_UNIDAD_VENTA: TcxGridDBColumn;
    cxGridViewDESCUENTO_CLIENTE_VENTA: TcxGridDBColumn;
    cxGridViewIMPORTE_TOTAL_VENTA: TcxGridDBColumn;
    cxGridViewNIF_CIF_PROVEEDOR: TcxGridDBColumn;
    cxGridViewNOMBRE_PROVEEDOR: TcxGridDBColumn;
    cxGridViewNIF_CIF_CLIENTE: TcxGridDBColumn;
    cxGridViewNOMBRE_CLIENTE: TcxGridDBColumn;
    cxGridViewNOMBRE_COMERCIAL_CLIENTE: TcxGridDBColumn;
    cxGridViewNIF_CIF_AGENTE: TcxGridDBColumn;
    cxGridViewNOMBRE_AGENTE: TcxGridDBColumn;
    cxGridViewCOMISION_AGENTE: TcxGridDBColumn;
    cxGridViewIMPORTE_COMISION_AGENTE: TcxGridDBColumn;
    cxGridViewIMPORTE_NETO_VENTA: 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;
  end;

implementation

uses
  cxVariants, uDateUtils;

{$R *.dfm}

{ TfrInfVentasArticuloGrid }

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

procedure TfrViewInfVentasArticuloGrid.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 TfrViewInfVentasArticuloGrid.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 TfrViewInfVentasArticuloGrid.cxGridViewColumnPosChanged(
  Sender: TcxGridTableView; AColumn: TcxGridColumn);
begin
  inherited;
  if Assigned(FOnViewChanged) then
    FOnViewChanged(Self);
end;

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

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

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

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

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

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

procedure TfrViewInfVentasArticuloGrid.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 TfrViewInfVentasArticuloGrid.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.

