unit uViewControGridlVentaProceso;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewControlGrid, 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;

type
  IViewControlGridVentaProceso = interface(IViewControlGrid)
  ['{537E8507-695E-4345-AAEE-F573C113A2CF}']
  end;

  TfrViewControlGridVentaProceso = class(TfrViewControlGrid, IViewControlGridVentaProceso)
    cxGrid: TcxGrid;
    cxGridView: TcxGridDBTableView;
    cxGridViewFECHA: TcxGridDBColumn;
    cxGridViewDIA: TcxGridDBColumn;
    cxGridViewDIASEMANA: TcxGridDBColumn;
    cxGridViewMES: TcxGridDBColumn;
    cxGridViewANO: TcxGridDBColumn;
    cxGridViewCENTRO: TcxGridDBColumn;
    cxGridViewVENDEDOR: TcxGridDBColumn;
    cxGridViewCODIGO: TcxGridDBColumn;
    cxGridViewPRODUCTO: TcxGridDBColumn;
    cxGridViewGRUPOPRODUCTO: TcxGridDBColumn;
    cxGridViewTIPOPRODUCTO: TcxGridDBColumn;
    cxGridViewCOLECCION: TcxGridDBColumn;
    cxGridViewNUMTALON: TcxGridDBColumn;
    cxGridViewCOLOR: TcxGridDBColumn;
    cxGridViewTALLA: TcxGridDBColumn;
    cxGridViewIMPORTE: TcxGridDBColumn;
    cxGridViewINICIO: TcxGridDBColumn;
    cxGridViewFINAL: TcxGridDBColumn;
    cxGridLevel: TcxGridLevel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyleRed: TcxStyle;
    cxStyleBlue: TcxStyle;
    cxStyleFilterBox: TcxStyle;
    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 cxGridViewCENTROPropertiesInitPopup(Sender: TObject);
    procedure cxGridViewVENDEDORPropertiesInitPopup(Sender: TObject);
    procedure cxGridViewNUMTALONPropertiesInitPopup(Sender: TObject);
    procedure cxGridViewPRODUCTOPropertiesInitPopup(Sender: TObject);
    procedure cxGridViewDataControllerFilterChanged(Sender: TObject);
    procedure cxGridViewDIASEMANAPropertiesInitPopup(Sender: TObject);
    procedure cxGridViewMESPropertiesInitPopup(Sender: TObject);
    procedure cxGridViewGRUPOPRODUCTOPropertiesInitPopup(Sender: TObject);
    procedure cxGridViewTIPOPRODUCTOPropertiesInitPopup(Sender: TObject);
    procedure cxGridViewCOLECCIONPropertiesInitPopup(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, DateUtils, uDataModuleVentasProceso;

{$R *.dfm}

{ TfrViewControlGridVentaProceso }

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

procedure TfrViewControlGridVentaProceso.Refresh;
var
  FocusedRow, TopRow : 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;

    { Esta chapuzilla es para que se rellenen los combobox de los filtros
      por que el cxFilterControl no coge los valores la 1ª vez si los
      combos están vacíos. }
    TcxComboBoxProperties(cxGridViewPRODUCTO.Properties).OnInitPopup(nil);
    TcxComboBoxProperties(cxGridViewCENTRO.Properties).OnInitPopup(nil);
    TcxComboBoxProperties(cxGridViewVENDEDOR.Properties).OnInitPopup(nil);
    TcxComboBoxProperties(cxGridViewNUMTALON.Properties).OnInitPopup(nil);
    TcxComboBoxProperties(cxGridViewDIASEMANA.Properties).OnInitPopup(nil);
    TcxComboBoxProperties(cxGridViewMES.Properties).OnInitPopup(nil);
    TcxComboBoxProperties(cxGridViewGRUPOPRODUCTO.Properties).OnInitPopup(nil);
    TcxComboBoxProperties(cxGridViewTIPOPRODUCTO.Properties).OnInitPopup(nil);
    TcxComboBoxProperties(cxGridViewCOLECCION.Properties).OnInitPopup(nil);
  end;
end;

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

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

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

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

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

procedure TfrViewControlGridVentaProceso.cxGridViewCENTROPropertiesInitPopup(
  Sender: TObject);
begin
  inherited;
  with TcxComboBoxProperties(cxGridViewCENTRO.Properties) do
  begin
    Items.Clear;
    Items.AddStrings(FilterValueList[cxGridViewCENTRO.Index]);
    Items.Delete(1);  // "Personalizado"
    Items.Delete(0);  // "Todos"
  end;
end;

procedure TfrViewControlGridVentaProceso.cxGridViewVENDEDORPropertiesInitPopup(
  Sender: TObject);
begin
  inherited;
  with TcxComboBoxProperties(cxGridViewVENDEDOR.Properties) do
  begin
    Items.Clear;
    Items.AddStrings(FilterValueList[cxGridViewVENDEDOR.Index]);
    Items.Delete(1);  // "Personalizado"
    Items.Delete(0);  // "Todos"
  end;
end;

procedure TfrViewControlGridVentaProceso.cxGridViewNUMTALONPropertiesInitPopup(
  Sender: TObject);
begin
  inherited;
  with TcxComboBoxProperties(cxGridViewNUMTALON.Properties) do
  begin
    Items.Clear;
    Items.AddStrings(FilterValueList[cxGridViewNUMTALON.Index]);
    Items.Delete(1);  // "Personalizado"
    Items.Delete(0);  // "Todos"
  end;
end;

procedure TfrViewControlGridVentaProceso.cxGridViewPRODUCTOPropertiesInitPopup(
  Sender: TObject);
begin
  inherited;
  with TcxComboBoxProperties(cxGridViewPRODUCTO.Properties) do
  begin
    Items.Clear;
    Items.AddStrings(FilterValueList[cxGridViewPRODUCTO.Index]);
    Items.Delete(1);  // "Personalizado"
    Items.Delete(0);  // "Todos"
  end;
end;

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

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

procedure TfrViewControlGridVentaProceso.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 TfrViewControlGridVentaProceso.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;

procedure TfrViewControlGridVentaProceso.cxGridViewDIASEMANAPropertiesInitPopup(
  Sender: TObject);
begin
  inherited;
  with TcxComboBoxProperties(cxGridViewDIASEMANA.Properties) do
  begin
    Items.Clear;
    Items.AddStrings(FilterValueList[cxGridViewDIASEMANA.Index]);
    Items.Delete(1);  // "Personalizado"
    Items.Delete(0);  // "Todos"
  end;
end;

procedure TfrViewControlGridVentaProceso.cxGridViewMESPropertiesInitPopup(
  Sender: TObject);
begin
  inherited;
  with TcxComboBoxProperties(cxGridViewMES.Properties) do
  begin
    Items.Clear;
    Items.AddStrings(FilterValueList[cxGridViewMES.Index]);
    Items.Delete(1);  // "Personalizado"
    Items.Delete(0);  // "Todos"
  end;
end;

procedure TfrViewControlGridVentaProceso.cxGridViewGRUPOPRODUCTOPropertiesInitPopup(
  Sender: TObject);
begin
  inherited;
  with TcxComboBoxProperties(cxGridViewGRUPOPRODUCTO.Properties) do
  begin
    Items.Clear;
    Items.AddStrings(FilterValueList[cxGridViewGRUPOPRODUCTO.Index]);
    Items.Delete(1);  // "Personalizado"
    Items.Delete(0);  // "Todos"
  end;
end;

procedure TfrViewControlGridVentaProceso.cxGridViewTIPOPRODUCTOPropertiesInitPopup(
  Sender: TObject);
begin
  inherited;
  with TcxComboBoxProperties(cxGridViewTIPOPRODUCTO.Properties) do
  begin
    Items.Clear;
    Items.AddStrings(FilterValueList[cxGridViewTIPOPRODUCTO.Index]);
    Items.Delete(1);  // "Personalizado"
    Items.Delete(0);  // "Todos"
  end;
end;

procedure TfrViewControlGridVentaProceso.cxGridViewCOLECCIONPropertiesInitPopup(
  Sender: TObject);
begin
  inherited;
  with TcxComboBoxProperties(cxGridViewCOLECCION.Properties) do
  begin
    Items.Clear;
    Items.AddStrings(FilterValueList[cxGridViewCOLECCION.Index]);
    Items.Delete(1);  // "Personalizado"
    Items.Delete(0);  // "Todos"
  end;
end;

end.
