{*******************************************************}
{                                                       }
{       Administración de puntos de venta               }
{                                                       }
{       Copyright (C) 2006 Rodax Software S.L.          }
{                                                       }
{*******************************************************}

unit uViewListaReferenciaGenerica;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewGrid, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, DBTables, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, uBizReferenciaGenerica, dxPSGlbl,
  dxPSUtl, dxPSEngn, dxPrnPg, dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider,
  dxPSFillPatterns, dxPSEdgePatterns, cxCalendar, cxTextEdit,
  cxCurrencyEdit, dxPSCore, uDADataTable, StdCtrls, uDAInterfaces, TBXDkPanels,
  uCustomView, uViewBase, uViewParametrosControlGrid, uViewParametrosCentro,
  TB2Dock;

type
  IViewListaReferenciaGenerica = interface(IViewGrid)
  ['{2D43B302-DAA6-45FE-BC72-BAD3591CCE67}']
    procedure VerPendientes;
    procedure VerEnEspera;
    procedure VerResueltos;

    function GetItems: IBizReferenciaGenerica;
    procedure SetItems(const Value: IBizReferenciaGenerica);
    property Items: IBizReferenciaGenerica read GetItems write SetItems;
  end;

  TfrViewListaReferenciaGenerica = class(TfrViewGrid, IViewListaReferenciaGenerica)
    cxGridViewFECHA: TcxGridDBColumn;
    cxGridViewCENTRO: TcxGridDBColumn;
    cxGridViewVENDEDOR: TcxGridDBColumn;
    cxGridViewNUMTALON: TcxGridDBColumn;
    cxGridViewCODIGO: TcxGridDBColumn;
    cxGridViewPRODUCTO: TcxGridDBColumn;
    cxGridViewCOLOR: TcxGridDBColumn;
    cxGridViewTALLA: TcxGridDBColumn;
    cxGridViewVTAPROCESO: TcxGridDBColumn;
    cxGridViewVTAFINAL: TcxGridDBColumn;
    cxGridViewVTATERM: TcxGridDBColumn;
    cxGridViewVALTOT: TcxGridDBColumn;
    cxGridViewCODCENTRO: TcxGridDBColumn;
    cxGridViewCODVENDEDOR: TcxGridDBColumn;
    cxGridViewFECHACAMBIO: TcxGridDBColumn;
    cxStyleResuelto: TcxStyle;
    cxStyleEnEspera1semana: TcxStyle;
    cxStyleResueltoRectificado: TcxStyle;
    cxGridViewColumn1: TcxGridDBColumn;
    TBXDockablePanel1: TTBXDockablePanel;
    frViewParametrosCentro1: TfrViewParametrosCentro;
    TBXLabel1: TTBXLabel;
    pnlAgrupaciones: TTBXAlignmentPanel;
    TBXButton3: TTBXButton;
    procedure cxGridViewCellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure cxGridViewStylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridViewMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    FEstado : TRefGenEstado;
  protected
    FItems: IBizReferenciaGenerica;
    function GetItems: IBizReferenciaGenerica;
    procedure SetItems(const Value: IBizReferenciaGenerica);

  public
    property Items: IBizReferenciaGenerica read GetItems write SetItems;

    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure VerPendientes;
    procedure VerEnEspera;
    procedure VerResueltos;
    procedure Refresh;    
  end;


implementation

uses uDataModuleReferenciaGenerica, DateUtils, cxGridDBDataDefinitions;

{$R *.dfm}

constructor TfrViewListaReferenciaGenerica.Create(AOwner: TComponent);
begin
  inherited;
  FItems := Nil;
  frViewParametrosCentro1.Refresh;
end;

procedure TfrViewListaReferenciaGenerica.cxGridViewCellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  if Assigned(FOnDblClick) then
    FOnDblClick(Sender);
end;

procedure TfrViewListaReferenciaGenerica.VerEnEspera;
begin
  cxGridViewFECHACAMBIO.Visible := True;
  cxGridViewFECHACAMBIO.Caption := 'En espera desde';  
  FEstado := rgEnEspera;  
end;

procedure TfrViewListaReferenciaGenerica.VerPendientes;
begin
  cxGridViewFECHACAMBIO.Visible := False;
  FEstado := rgPendiente;
end;

procedure TfrViewListaReferenciaGenerica.VerResueltos;
begin
  cxGridViewFECHACAMBIO.Visible := True;
  cxGridViewFECHACAMBIO.Caption := 'Fecha de rectificación';
  FEstado := rgResuelto;  
end;

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

  if FEstado = rgResuelto then
  begin
    if AItem.Index in [cxGridViewPRODUCTO.Index, cxGridViewCOLOR.Index,
      cxGridViewTALLA.Index] then
      AStyle := cxStyleResueltoRectificado
    else
      AStyle := cxStyleResuelto;
    Exit;
  end;

  if FEstado = rgEnEspera then
  begin
    AColumn := cxGridViewFECHACAMBIO;
    if Assigned(AColumn) and (AColumn.Visible) then
    begin
      try
        AFecha := VarToDateTime(ARecord.Values[AColumn.Index]);
      except
        on Exception do
          AFecha := Now;
      end;

      if DaysBetween(Now, AFecha) > 7 then
        AStyle := cxStyleEnEspera1semana
    end;
  end;
end;

destructor TfrViewListaReferenciaGenerica.Destroy;
begin
  FItems := Nil;
  inherited;
end;

function TfrViewListaReferenciaGenerica.GetItems: IBizReferenciaGenerica;
begin
  Result := FItems;
end;

procedure TfrViewListaReferenciaGenerica.Refresh;
var
  ACentrosList : TStringList;
  AWhereCentro : String;
  AWhere : String;
  i : integer;
begin
  ACentrosList := Nil;

  if Assigned(FItems) then
     Items.DataTable.Active := False;
  ACentrosList := frViewParametrosCentro1.CentrosSeleccionados;

  ShowHourglassCursor;
  try
    if Assigned(ACentrosList) then
    begin
       AWhereCentro := '';
       if ACentrosList.Count = 0 then
       begin
          ShowMessage('Debe seleccionar al menos un centro en los parámetros de la consulta.');
          Exit;
       end
       else begin
          AWhereCentro := AWhereCentro + ' FILIAL in (';
          for i := 0 to ACentrosList.Count - 1 do
          begin
            if i > 0 then
              AWhereCentro := AWhereCentro + ', ';
            AWhereCentro := AWhereCentro + '''' + ACentrosList[i] + '''';
          end;
          AWhereCentro := AWhereCentro + ')';
        end;
    end;

    AWhere := '';
    if AWhereCentro <> '' then
    begin
       AWhere := AWhere + AWhereCentro;
    end;

//    Items.DataTable.Where.Clear;
//    Items.DataTable.Where.AddText(AWhere, False);
    Items.DataTable.Active := True;

  finally
    HideHourglassCursor;
    FreeAndNIL(ACentrosList);
  end;
end;

procedure TfrViewListaReferenciaGenerica.SetItems(const Value: IBizReferenciaGenerica);
begin
  FItems := Value;
  if Assigned(FItems) then
     dsDataSource.DataTable := FItems.DataTable;
end;

procedure TfrViewListaReferenciaGenerica.cxGridViewMouseDown(
  Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
  Y: Integer);
var
  HT: TcxCustomGridHitTest;
  ARecord: TcxCustomGridRecord;
  AGroupIndex, I, AIndex: Integer;
  AList: TList;
  DC: TcxGridDBDataController;
begin
  inherited;
  HT := TcxGridSite(Sender).GridView.ViewInfo.GetHitTest(X, Y);
  if (HT is TcxGridRecordHitTest) and not (HT is TcxGridRecordCellHitTest)
    and not (HT is TcxGridExpandButtonHitTest) then
  begin
    DC := TcxGridDBTableView(TcxGridSite(Sender).GridView).DataController;
    AList := TList.Create;
    ShowHourglassCursor;
    TcxGridDBTableView(TcxGridSite(Sender).GridView).BeginUpdate;
    try
      AGroupIndex := DC.Groups.DataGroupIndexByRowIndex[TcxGridRecordHitTest(HT).GridRecord.Index];
      DC.Groups.LoadRecordIndexes(AList, AGroupIndex);
      for I := 0 to AList.Count - 1 do
      begin
        AIndex := DC.GetRowIndexByRecordIndex(Integer(AList[I]), True);
        DC.SelectRows(AIndex, AIndex);
      end;
    finally
      FreeAndNil(AList);
      TcxGridDBTableView(TcxGridSite(Sender).GridView).EndUpdate;      
      HideHourglassCursor;
    end;
  end;
end;

end.
