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

unit uViewListaRecepciones;

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, uBizRecepciones, 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
  IViewListaRecepciones = interface(IViewGrid)
  ['{E4401ABB-E95D-43AC-9155-BA57DC2B88C2}']
    procedure VerPendientes;
    procedure VerAprobados;
    procedure VerTodos;
  end;

  TfrViewListaRecepciones = class(TfrViewGrid, IViewListaRecepciones)
    cxGridBandedViewCENTRO: TcxGridDBColumn;
    cxGridBandedViewFILIAL: TcxGridDBColumn;
    cxGridBandedViewCODIGO: TcxGridDBColumn;
    cxGridBandedViewPRODUCTO: TcxGridDBColumn;
    cxGridBandedViewCOLOR: TcxGridDBColumn;
    cxGridBandedViewFECHA: TcxGridDBColumn;
    cxGridBandedViewALBARAN: TcxGridDBColumn;
    cxGridBandedViewE38: TcxGridDBColumn;
    cxGridBandedViewE40: TcxGridDBColumn;
    cxGridBandedViewE42: TcxGridDBColumn;
    cxGridBandedViewE44: TcxGridDBColumn;
    cxGridBandedViewE46: TcxGridDBColumn;
    cxGridBandedViewE48: TcxGridDBColumn;
    cxGridBandedViewENVIADO: TcxGridDBColumn;
    cxGridBandedViewR38: TcxGridDBColumn;
    cxGridBandedViewR40: TcxGridDBColumn;
    cxGridBandedViewR42: TcxGridDBColumn;
    cxGridBandedViewR44: TcxGridDBColumn;
    cxGridBandedViewR46: TcxGridDBColumn;
    cxGridBandedViewR48: TcxGridDBColumn;
    cxGridBandedViewRECIBIDO: TcxGridDBColumn;
    cxGridBandedViewDIFERENCIA: TcxGridDBColumn;
    cxGridBandedViewESTADO_ALBARAN: TcxGridDBColumn;
    cxGridBandedViewCONTABILIZAR_ALBARAN: TcxGridDBColumn;
    cxGridBandedViewRR38: TcxGridDBColumn;
    cxGridBandedViewRR40: TcxGridDBColumn;
    cxGridBandedViewRR42: TcxGridDBColumn;
    cxGridBandedViewRR44: TcxGridDBColumn;
    cxGridBandedViewRR46: TcxGridDBColumn;
    cxGridBandedViewRR48: TcxGridDBColumn;
    cxGridBandedViewRECIBIDO_RECT: TcxGridDBColumn;
    cxGridBandedViewAPROBADO: TcxGridDBColumn;
    cxStyleEnviado: TcxStyle;
    cxStyleRecibido: TcxStyle;
    cxStyleSubtotal: TcxStyle;
    SmallImages: TPngImageList;
    cxStyleCantidadRectificada: TcxStyle;
    cxStyle1: TcxStyle;
    cxStyleFooter: TcxStyle;
    dxComponentPrinterLink1: TdxGridReportLink;
    cxStyleRepositoryInforme: TcxStyleRepository;
    cxStyleContentInforme: TcxStyle;
    cxStyleFooterInforme: TcxStyle;
    cxStyleGroupInforme: TcxStyle;
    cxStyleHeaderInforme: TcxStyle;
    cxStyleSelectionInforme: TcxStyle;
    cxGridViewColumnOBSERVACIONES: TcxGridDBColumn;
    procedure cxGridViewMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure cxGridBandedViewRR38GetDisplayText(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: String);
    procedure cxGridBandedViewALBARANGetDisplayText(
      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 cxGridBandedViewALBARANStylesGetContentStyle(
      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 cxGridBandedViewCODIGOGetDisplayText(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: String);
    procedure cxGridBandedViewRECIBIDO_RECTGetDataText(
      Sender: TcxCustomGridTableItem; ARecordIndex: Integer;
      var AText: String);
    procedure cxGridBandedViewDIFERENCIAGetDataText(
      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
  uDataModuleRecepciones, DateUtils, cxGridDBDataDefinitions,
  uViewGridBase, cxGridRows;

{$R *.dfm}

procedure TfrViewListaRecepciones.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 TfrViewListaRecepciones.VerAprobados;
begin
  FTipo := 'S';
  FreeAndNil(FNivelFiltrado);
  FilterChanged(self);
end;

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

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

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

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

  AStr := VarToStrDef(ARecord.Values[cxGridBandedViewR38.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 TfrViewListaRecepciones.cxGridBandedViewRR40GetDisplayText(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: String);
var
  AStr : String;
begin
  inherited;
  if not Assigned( ARecord ) then
    Exit;

  AStr := VarToStrDef(ARecord.Values[cxGridBandedViewR40.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 TfrViewListaRecepciones.cxGridBandedViewRR42GetDisplayText(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: String);
var
  AStr : String;
begin
  inherited;
  if not Assigned( ARecord ) then
    Exit;

  AStr := VarToStrDef(ARecord.Values[cxGridBandedViewR42.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 TfrViewListaRecepciones.cxGridBandedViewRR44GetDisplayText(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: String);
var
  AStr : String;
begin
  inherited;
  if not Assigned( ARecord ) then
    Exit;

  AStr := VarToStrDef(ARecord.Values[cxGridBandedViewR44.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 TfrViewListaRecepciones.cxGridBandedViewRR46GetDisplayText(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: String);
var
  AStr : String;
begin
  inherited;
  if not Assigned( ARecord ) then
    Exit;

  AStr := VarToStrDef(ARecord.Values[cxGridBandedViewR46.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 TfrViewListaRecepciones.cxGridBandedViewRR48GetDisplayText(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: String);
var
  AStr : String;
begin
  inherited;
  if not Assigned( ARecord ) then
    Exit;

  AStr := VarToStrDef(ARecord.Values[cxGridBandedViewR48.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 TfrViewListaRecepciones.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[cxGridBandedViewR38.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaRecepciones.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[cxGridBandedViewR40.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaRecepciones.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[cxGridBandedViewR42.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaRecepciones.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[cxGridBandedViewR44.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaRecepciones.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[cxGridBandedViewR46.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaRecepciones.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[cxGridBandedViewR48.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

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

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

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

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


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

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

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

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

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

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

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

procedure TfrViewListaRecepciones.cxGridBandedViewRECIBIDO_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, cxGridBandedViewRR38.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridBandedViewRR40.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridBandedViewRR42.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridBandedViewRR44.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridBandedViewRR46.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridBandedViewRR48.Index], 0);
  end;
  AText := FloatToStr(Total);
end;

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

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

procedure TfrViewListaRecepciones.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(cxGridBandedViewAPROBADO, 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.
