unit uViewControGridlVentasTerminadas;

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, cxSpinEdit;

type
  IViewControlGridVentasTerminadas = interface(IViewControlGrid)
    ['{95B6857B-0F06-470B-8099-CDB07017B63F}']
  end;

  TfrViewControlGridVentasTerminadas = class(TfrViewControlGrid, IViewControlGridVentasTerminadas)
    cxGrid: TcxGrid;
    cxGridView: TcxGridDBTableView;
    cxGridLevel: TcxGridLevel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyleRed: TcxStyle;
    cxStyleBlue: TcxStyle;
    cxStyleFilterBox: TcxStyle;
    cxGridViewFECHA: TcxGridDBColumn;
    cxGridViewDIA: TcxGridDBColumn;
    cxGridViewDIASEMANA: TcxGridDBColumn;
    cxGridViewMES: TcxGridDBColumn;
    cxGridViewANO: TcxGridDBColumn;
    cxGridViewCENTRO: TcxGridDBColumn;
    cxGridViewVENDEDOR: TcxGridDBColumn;
    cxGridViewCODIGO: TcxGridDBColumn;
    cxGridViewPRODUCTO: TcxGridDBColumn;
    cxGridViewGRUPOPRODUCTO: TcxGridDBColumn;
    cxGridViewTIPOPRODUCTO: TcxGridDBColumn;
    cxGridViewCOLECCION: TcxGridDBColumn;
    cxGridViewCOLOR: TcxGridDBColumn;
    cxGridViewTALLA: TcxGridDBColumn;
    cxGridViewTALLA38: TcxGridDBColumn;
    cxGridViewTALLA40: TcxGridDBColumn;
    cxGridViewTALLA42: TcxGridDBColumn;
    cxGridViewTALLA44: TcxGridDBColumn;
    cxGridViewTALLA46: TcxGridDBColumn;
    cxGridViewTALLA48: TcxGridDBColumn;
    cxGridViewVENTAS: TcxGridDBColumn;
    cxGridViewDEVOLUCIONES: TcxGridDBColumn;
    cxGridViewTOTAL: TcxGridDBColumn;
    cxGridViewIMPVEN: TcxGridDBColumn;
    cxGridViewIMPDEV: TcxGridDBColumn;
    cxGridViewIMPTOT: TcxGridDBColumn;
    cxGridViewNUMTALON: TcxGridDBColumn;
    cxGridViewVTATERMDEV: TcxGridDBColumn;
    cxGridViewVTAFINALRESC: TcxGridDBColumn;
    cxGridViewVALTERMDEV: TcxGridDBColumn;
    cxGridViewVALFINALRESC: 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 cxGridViewCENTROPropertiesInitPopup(Sender: TObject);
    procedure cxGridViewVENDEDORPropertiesInitPopup(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);
    procedure cxGridViewDataControllerCompare(
      ADataController: TcxCustomDataController; ARecordIndex1,
      ARecordIndex2, AItemIndex: Integer; const V1, V2: Variant;
      var Compare: Integer);
  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}

{ TfrViewControlGridVentasTerminadas }

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

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

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

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

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

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

procedure TfrViewControlGridVentasTerminadas.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 TfrViewControlGridVentasTerminadas.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 TfrViewControlGridVentasTerminadas.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 TfrViewControlGridVentasTerminadas.cxGridViewDataControllerFilterChanged(
  Sender: TObject);
begin
  if Assigned(FOnFilterChanged) then
    FOnFilterChanged(Self);
end;

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

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

procedure TfrViewControlGridVentasTerminadas.cxGridViewDataControllerCompare(
  ADataController: TcxCustomDataController; ARecordIndex1, ARecordIndex2,
  AItemIndex: Integer; const V1, V2: Variant; var Compare: Integer);
var
  AValue1,
  AValue2 : Variant;
begin
  // Por defecto
  Compare := VarCompare(V1, V2);
  try
    if (AItemIndex = cxGridViewDIASEMANA.Index) then
    begin
      AValue1 := ADataController.Values[ARecordIndex1, cxGridViewDIASEMANA.Index];
      AValue2 := ADataController.Values[ARecordIndex2, cxGridViewDIASEMANA.Index];
      if (not VarIsNull(AValue1)) and (not VarIsNull(AValue2)) then
        Compare := VarCompare(NumberDayOfTheWeek(AValue1), NumberDayOfTheWeek(AValue2))
    end
    else begin
      if (AItemIndex = cxGridViewMES.Index) then
      begin
        AValue1 := ADataController.Values[ARecordIndex1, cxGridViewMES.Index];
        AValue2 := ADataController.Values[ARecordIndex2, cxGridViewMES.Index];
        if (not VarIsNull(AValue1)) and (not VarIsNull(AValue2)) then
          Compare := VarCompare(NumberMonthOfTheYear(AValue1), NumberMonthOfTheYear(AValue2))
      end
    end;
  except
    //ShowMessage(VarToStr(ARecordIndex1) + ' ' + VarToStr(ARecordIndex1));
  end;
end;

end.

