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

unit uViewListaDevoluciones;

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, uBizDevoluciones, dxPSGlbl,
  dxPSUtl, dxPSEngn, dxPrnPg, dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider,
  dxPSFillPatterns, dxPSEdgePatterns, cxCalendar, cxTextEdit,
  cxCurrencyEdit, dxPSCore, uDADataTable, StdCtrls, cxGridBandedTableView,
  cxGridDBBandedTableView, cxImageComboBox, ImgList, PngImageList,
  dxPScxCommon, dxPScxGridLnk, cxMemo;

type
  IViewListaDevoluciones = interface(IViewGrid)
  ['{2179BA33-B039-4CC9-A423-F50FD2C7B880}']
    procedure VerPendientes;
    procedure VerAprobados;
    procedure VerTodos;
  end;

  TfrViewListaDevoluciones = class(TfrViewGrid, IViewListaDevoluciones)
    cxStyleEnviado: TcxStyle;
    cxStyleRecibido: TcxStyle;
    cxStyleSubtotal: TcxStyle;
    SmallImages: TPngImageList;
    cxStyleCantidadRectificada: TcxStyle;
    cxStyle1: TcxStyle;
    cxGridViewAPROBADO: TcxGridDBColumn;
    cxGridViewCENTRO: TcxGridDBColumn;
    cxGridViewFILIAL: TcxGridDBColumn;
    cxGridViewCODIGO: TcxGridDBColumn;
    cxGridViewPRODUCTO: TcxGridDBColumn;
    cxGridViewCOLOR: TcxGridDBColumn;
    cxGridViewFECHA: TcxGridDBColumn;
    cxGridViewNUMHOJA: TcxGridDBColumn;
    cxGridViewESTADO_HOJA: TcxGridDBColumn;
    cxGridViewOBSERVACIONES_HOJA: TcxGridDBColumn;
    cxGridViewCONTABILIZAR_HOJA: TcxGridDBColumn;
    cxGridViewS38: TcxGridDBColumn;
    cxGridViewS40: TcxGridDBColumn;
    cxGridViewS42: TcxGridDBColumn;
    cxGridViewS44: TcxGridDBColumn;
    cxGridViewS46: TcxGridDBColumn;
    cxGridViewS48: TcxGridDBColumn;
    cxGridViewSOLICITADO: TcxGridDBColumn;
    cxGridViewE38: TcxGridDBColumn;
    cxGridViewE40: TcxGridDBColumn;
    cxGridViewE42: TcxGridDBColumn;
    cxGridViewE44: TcxGridDBColumn;
    cxGridViewE46: TcxGridDBColumn;
    cxGridViewE48: TcxGridDBColumn;
    cxGridViewENVIADO: TcxGridDBColumn;
    cxGridViewER38: TcxGridDBColumn;
    cxGridViewER40: TcxGridDBColumn;
    cxGridViewER42: TcxGridDBColumn;
    cxGridViewER44: TcxGridDBColumn;
    cxGridViewER46: TcxGridDBColumn;
    cxGridViewER48: TcxGridDBColumn;
    cxGridViewENVIADO_RECT: TcxGridDBColumn;
    cxGridViewDIFERENCIA: TcxGridDBColumn;
    dxComponentPrinterLink1: TdxGridReportLink;
    cxStyleRepositoryInforme: TcxStyleRepository;
    cxStyleContentInforme: TcxStyle;
    cxStyleFooterInforme: TcxStyle;
    cxStyleGroupInforme: TcxStyle;
    cxStyleHeaderInforme: TcxStyle;
    cxStyleSelectionInforme: TcxStyle;
    procedure cxGridViewMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure cxGridBandedViewRR38GetDisplayText(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: String);
    procedure cxGridBandedViewRR40GetDisplayText(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: String);
    procedure cxGridBandedViewRR42GetDisplayText(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: String);
    procedure cxGridBandedViewRR44GetDisplayText(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: String);
    procedure cxGridBandedViewRR46GetDisplayText(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: String);
    procedure cxGridBandedViewRR48GetDisplayText(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: String);
    procedure cxGridBandedViewRR38StylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridBandedViewRR40StylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridBandedViewRR42StylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridBandedViewRR44StylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridBandedViewRR46StylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridBandedViewRR48StylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridViewCustomDrawGroupCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableCellViewInfo;
      var ADone: Boolean);
    procedure cxGridViewCellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure cxGridBandedViewCODIGOGetDataText(
      Sender: TcxCustomGridTableItem; ARecordIndex: Integer;
      var AText: String);
    procedure cxGridViewNUMHOJAGetDisplayText(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: String);
    procedure cxGridViewNUMHOJAStylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridViewCODIGOGetDataText(Sender: TcxCustomGridTableItem;
      ARecordIndex: Integer; var AText: String);
    procedure cxGridViewCODIGOGetDisplayText(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: String);
    procedure cxGridViewENVIADO_RECTGetDataText(
      Sender: TcxCustomGridTableItem; ARecordIndex: Integer;
      var AText: String);
    procedure cxGridViewDIFERENCIAGetDataText(
      Sender: TcxCustomGridTableItem; ARecordIndex: Integer;
      var AText: String);
    procedure dxComponentPrinterLink1InitializeCell(
      Sender: TdxGridReportLink; AView: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AColumn: TcxGridColumn;
      AnItem: TAbstractdxReportCellData);
  protected
    FTipo : String;
    FNivelFiltrado : TcxFilterCriteriaItemList;
    procedure FilterChanged(Sender: TObject); override;
  public
    procedure Preview; override;
    procedure Print; override;
    procedure PrintSetup; override;
    procedure VerPendientes;
    procedure VerAprobados;
    procedure VerTodos;
  end;


implementation

uses
  uDataModuleDevoluciones, DateUtils, cxGridDBDataDefinitions,
  uViewGridBase, cxGridRows;

{$R *.dfm}

procedure TfrViewListaDevoluciones.cxGridViewMouseDown(
  Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
  Y: Integer);
var
  HT: TcxCustomGridHitTest;
  ALevelIndex, 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;
    ALevelIndex := TcxGridRecordHitTest(HT).GridRecord.Level;
    if ALevelIndex > 0 then
    begin
      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;

procedure TfrViewListaDevoluciones.VerAprobados;
begin
  FTipo := 'S';
  FreeAndNil(FNivelFiltrado);
  FilterChanged(self);
end;

procedure TfrViewListaDevoluciones.VerPendientes;
begin
  FTipo := 'N';
  FreeAndNil(FNivelFiltrado);
  FilterChanged(self);
end;

procedure TfrViewListaDevoluciones.VerTodos;
begin
  FTipo := '';
  FreeAndNil(FNivelFiltrado);
  FilterChanged(self);
  cxGridViewAPROBADO.Visible := True;
end;

procedure TfrViewListaDevoluciones.cxGridBandedViewRR38GetDisplayText(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: String);
var
  AStr : String;
begin
  inherited;
  if not Assigned( ARecord ) then
    Exit;

  AStr := VarToStrDef(ARecord.Values[cxGridViewE38.Index], '');
  if not ((AStr = '') and (AText = '0')) and (AStr <> AText) then
  begin
    if Length(AStr) = 0 then
      AStr := '0';
    AText := AText + ' (' + AStr + ')';
  end;
end;

procedure TfrViewListaDevoluciones.cxGridBandedViewRR40GetDisplayText(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: String);
var
  AStr : String;
begin
  inherited;
  if not Assigned( ARecord ) then
    Exit;

  AStr := VarToStrDef(ARecord.Values[cxGridViewE40.Index], '');
  if not ((AStr = '') and (AText = '0')) and (AStr <> AText) then
  begin
    if Length(AStr) = 0 then
      AStr := '0';
    AText := AText + ' (' + AStr + ')';
  end;
end;

procedure TfrViewListaDevoluciones.cxGridBandedViewRR42GetDisplayText(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: String);
var
  AStr : String;
begin
  inherited;
  if not Assigned( ARecord ) then
    Exit;

  AStr := VarToStrDef(ARecord.Values[cxGridViewE42.Index], '');
  if not ((AStr = '') and (AText = '0')) and (AStr <> AText) then
  begin
    if Length(AStr) = 0 then
      AStr := '0';
    AText := AText + ' (' + AStr + ')';
  end;
end;

procedure TfrViewListaDevoluciones.cxGridBandedViewRR44GetDisplayText(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: String);
var
  AStr : String;
begin
  inherited;
  if not Assigned( ARecord ) then
    Exit;

  AStr := VarToStrDef(ARecord.Values[cxGridViewE44.Index], '');
  if not ((AStr = '') and (AText = '0')) and (AStr <> AText) then
  begin
    if Length(AStr) = 0 then
      AStr := '0';
    AText := AText + ' (' + AStr + ')';
  end;
end;

procedure TfrViewListaDevoluciones.cxGridBandedViewRR46GetDisplayText(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: String);
var
  AStr : String;
begin
  inherited;
  if not Assigned( ARecord ) then
    Exit;

  AStr := VarToStrDef(ARecord.Values[cxGridViewE46.Index], '');
  if not ((AStr = '') and (AText = '0')) and (AStr <> AText) then
  begin
    if Length(AStr) = 0 then
      AStr := '0';
    AText := AText + ' (' + AStr + ')';
  end;
end;

procedure TfrViewListaDevoluciones.cxGridBandedViewRR48GetDisplayText(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: String);
var
  AStr : String;
begin
  inherited;
  if not Assigned( ARecord ) then
    Exit;

  AStr := VarToStrDef(ARecord.Values[cxGridViewE48.Index], '');
  if not ((AStr = '') and (AText = '0')) and (AStr <> AText) then
  begin
    if Length(AStr) = 0 then
      AStr := '0';
    AText := AText + ' (' + AStr + ')';
  end;
end;

procedure TfrViewListaDevoluciones.cxGridBandedViewRR38StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
  inherited;
  if Assigned(ARecord) and Assigned(AItem) then
  begin
    if ARecord.Values[AItem.Index] <> ARecord.Values[cxGridViewE38.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaDevoluciones.cxGridBandedViewRR40StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
  inherited;
  if Assigned(ARecord) and Assigned(AItem) then
  begin
    if ARecord.Values[AItem.Index] <> ARecord.Values[cxGridViewE40.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaDevoluciones.cxGridBandedViewRR42StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
  inherited;
  if Assigned(ARecord) and Assigned(AItem) then
  begin
    if ARecord.Values[AItem.Index] <> ARecord.Values[cxGridViewE42.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaDevoluciones.cxGridBandedViewRR44StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
  inherited;
  if Assigned(ARecord) and Assigned(AItem) then
  begin
    if ARecord.Values[AItem.Index] <> ARecord.Values[cxGridViewE44.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaDevoluciones.cxGridBandedViewRR46StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
  inherited;
  if Assigned(ARecord) and Assigned(AItem) then
  begin
    if ARecord.Values[AItem.Index] <> ARecord.Values[cxGridViewE46.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaDevoluciones.cxGridBandedViewRR48StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
  inherited;
  if Assigned(ARecord) and Assigned(AItem) then
  begin
    if ARecord.Values[AItem.Index] <> ARecord.Values[cxGridViewE48.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaDevoluciones.cxGridViewCustomDrawGroupCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableCellViewInfo; var ADone: Boolean);
var
  ARecIndex: Integer;
  Val1: string;
begin
  inherited;

  if TcxGridGroupCellViewInfo(AViewInfo).GridRecord.GroupedColumn = cxGridViewFILIAL then
  begin
    ARecIndex := AViewInfo.GridRecord.RecordIndex;
    Val1 := Sender.DataController.DisplayTexts[ARecIndex, cxGridViewFILIAL.Index];
    AViewInfo.Text := 'Centro: ' + Val1;
  end;
end;


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

procedure TfrViewListaDevoluciones.cxGridBandedViewCODIGOGetDataText(
  Sender: TcxCustomGridTableItem; ARecordIndex: Integer;
  var AText: String);
var
  Val1: string;
begin
  inherited;
  Val1 := Sender.GridView.DataController.DisplayTexts[ARecordIndex, cxGridViewCOLOR.Index];
  AText := AText + '-' + Val1;

end;

procedure TfrViewListaDevoluciones.cxGridViewNUMHOJAGetDisplayText(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: String);
begin
  inherited;
  if Length(VarToStr(ARecord.Values[cxGridViewESTADO_HOJA.Index])) > 0 then
    AText := AText + ' (' + VarToStr(ARecord.Values[cxGridViewESTADO_HOJA.Index]) + ')';
end;

procedure TfrViewListaDevoluciones.cxGridViewNUMHOJAStylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
  inherited;

  if VarToStr(ARecord.Values[cxGridViewCONTABILIZAR_HOJA.Index]) = 'N' then
    AStyle := cxStyle1;
end;

procedure TfrViewListaDevoluciones.Preview;
begin
  inherited;
  dxComponentPrinterLink1.Preview;
end;

procedure TfrViewListaDevoluciones.Print;
begin
  inherited;
  dxComponentPrinterLink1.Print(True, nil);
end;

procedure TfrViewListaDevoluciones.PrintSetup;
begin
  inherited;
  dxComponentPrinterLink1.PageSetup
end;

procedure TfrViewListaDevoluciones.cxGridViewCODIGOGetDataText(
  Sender: TcxCustomGridTableItem; ARecordIndex: Integer;
  var AText: String);
var
  Val1, Val2: string;
begin
  inherited;

  Val1 := Sender.GridView.DataController.DisplayTexts[ARecordIndex, cxGridViewPRODUCTO.Index];
  Val2 := Sender.GridView.DataController.DisplayTexts[ARecordIndex, cxGridViewCOLOR.Index];
  AText := 'Producto: ' + Val1 + ' - Color: ' + Val2;
end;

procedure TfrViewListaDevoluciones.cxGridViewCODIGOGetDisplayText(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: String);
begin
  inherited;
  AText := StringReplace(AText, 'CODIGO : ', '', [rfReplaceAll, rfIgnoreCase])
end;

procedure TfrViewListaDevoluciones.cxGridViewENVIADO_RECTGetDataText(
  Sender: TcxCustomGridTableItem; ARecordIndex: Integer;
  var AText: String);
var
  Total : Double;
begin
  inherited;

  Total := 0;
  with Sender.GridView.DataController do
  begin
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridViewER38.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridViewER40.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridViewER42.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridViewER44.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridViewER46.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridViewER48.Index], 0);
  end;
  AText := FloatToStr(Total);
end;

procedure TfrViewListaDevoluciones.cxGridViewDIFERENCIAGetDataText(
  Sender: TcxCustomGridTableItem; ARecordIndex: Integer;
  var AText: String);
var
  Total : Double;
begin
  inherited;
  with Sender.GridView.DataController do
  begin
    Total := StrToFloatDef(DisplayTexts[ARecordIndex, cxGridViewENVIADO_RECT.Index], 0);
    Total := Total - StrToFloatDef(DisplayTexts[ARecordIndex, cxGridViewSOLICITADO.Index], 0);
  end;
  AText := FloatToStr(Total);
end;

procedure TfrViewListaDevoluciones.dxComponentPrinterLink1InitializeCell(
  Sender: TdxGridReportLink; AView: TcxCustomGridTableView;
  ARecord: TcxCustomGridRecord; AColumn: TcxGridColumn;
  AnItem: TAbstractdxReportCellData);
var
  aux : string;
begin
  inherited;
  if Assigned(ARecord) then
  begin
    aux := ARecord.Values[cxGridViewCONTABILIZAR_HOJA.Index];
    if aux = 'N' then
    begin
      AnItem.Font.Style := cxStyle1.Font.Style;
      AnItem.Font.Color := cxStyle1.Font.Color;
    end;
  end;
end;

procedure TfrViewListaDevoluciones.FilterChanged(Sender: TObject);
var
  AItemList: TcxFilterCriteriaItemList;
begin
  inherited;

  cxGridView.DataController.Filter.BeginUpdate;
  AItemList := cxGridView.DataController.Filter.Root;

  if (Length(FTIPO) > 0) then
  begin
     FNivelFiltrado := AItemList.AddItemList(fboAnd);
     FNivelFiltrado.AddItem(cxGridViewAPROBADO, foEqual, FTIPO, FTIPO);
  end;

  if cxGridView.DataController.Filter.IsEmpty
  then cxGridView.DataController.Filter.Active := False
  else cxGridView.DataController.Filter.Active := True;

  cxGridView.DataController.Filter.EndUpdate;
end;

end.
