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

unit uViewListaTraspasos;

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

  TfrViewListaTraspasos = class(TfrViewGrid, IViewListaTraspasos)
    cxStyleSubtotal: TcxStyle;
    SmallImages: TPngImageList;
    cxStyleCantidadRectificada: TcxStyle;
    cxStyleNoContabilizar: TcxStyle;
    cxStyleFooter: TcxStyle;
    dxComponentPrinterLink1: TdxGridReportLink;
    cxStyleRepositoryInforme: TcxStyleRepository;
    cxStyleContentInforme: TcxStyle;
    cxStyleFooterInforme: TcxStyle;
    cxStyleGroupInforme: TcxStyle;
    cxStyleHeaderInforme: TcxStyle;
    cxStyleSelectionInforme: TcxStyle;
    cxGridViewTIPO: TcxGridDBColumn;
    cxGridViewFECHA: TcxGridDBColumn;
    cxGridViewCENTRO: TcxGridDBColumn;
    cxGridViewFILIAL: TcxGridDBColumn;
    cxGridViewCENTRO2: TcxGridDBColumn;
    cxGridViewFILIAL2: TcxGridDBColumn;
    cxGridViewAPROBADO: TcxGridDBColumn;
    cxGridViewESTADO_TRASPASO: TcxGridDBColumn;
    cxGridViewCONTABILIZAR: TcxGridDBColumn;
    cxGridViewCODIGO: TcxGridDBColumn;
    cxGridViewPRODUCTO: TcxGridDBColumn;
    cxGridViewCOLOR: TcxGridDBColumn;
    cxGridViewTR38: TcxGridDBColumn;
    cxGridViewTR40: TcxGridDBColumn;
    cxGridViewTR42: TcxGridDBColumn;
    cxGridViewTR44: TcxGridDBColumn;
    cxGridViewTR46: TcxGridDBColumn;
    cxGridViewTR48: TcxGridDBColumn;
    cxGridViewTOTAL_RECT: TcxGridDBColumn;
    cxGridViewT38: TcxGridDBColumn;
    cxGridViewT40: TcxGridDBColumn;
    cxGridViewT42: TcxGridDBColumn;
    cxGridViewT44: TcxGridDBColumn;
    cxGridViewT46: TcxGridDBColumn;
    cxGridViewT48: TcxGridDBColumn;
    cxGridViewTOTAL: TcxGridDBColumn;
    PngImageListTipo: TPngImageList;
    cxStyleCentro2: TcxStyle;
    cxGridViewObservaciones: TcxGridDBColumn;
    cxStyleObservacionesInforme: TcxStyle;
    procedure cxGridViewMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure cxGridBandedViewRR38GetDisplayText(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: String);
    procedure cxGridViewCellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure cxGridBandedViewCODIGOGetDisplayText(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: String);
    procedure cxGridViewTR38GetDisplayText(Sender: TcxCustomGridTableItem;
      ARecord: TcxCustomGridRecord; var AText: String);
    procedure cxGridViewTR40GetDisplayText(Sender: TcxCustomGridTableItem;
      ARecord: TcxCustomGridRecord; var AText: String);
    procedure cxGridViewTR42GetDisplayText(Sender: TcxCustomGridTableItem;
      ARecord: TcxCustomGridRecord; var AText: String);
    procedure cxGridViewTR44GetDisplayText(Sender: TcxCustomGridTableItem;
      ARecord: TcxCustomGridRecord; var AText: String);
    procedure cxGridViewTR46GetDisplayText(Sender: TcxCustomGridTableItem;
      ARecord: TcxCustomGridRecord; var AText: String);
    procedure cxGridViewTR48GetDisplayText(Sender: TcxCustomGridTableItem;
      ARecord: TcxCustomGridRecord; var AText: String);
    procedure cxGridViewTR38StylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridViewTR40StylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridViewTR42StylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridViewTR44StylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridViewTR46StylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridViewTR48StylesGetContentStyle(
      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 cxGridViewTOTAL_RECTGetDataText(
      Sender: TcxCustomGridTableItem; ARecordIndex: Integer;
      var AText: String);
    procedure cxGridViewFECHAStylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    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
  uDataModuleTraspasos, DateUtils, cxGridDBDataDefinitions,
  uViewGridBase, cxGridRows;

{$R *.dfm}

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

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

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

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

  AStr := VarToStrDef(ARecord.Values[cxGridViewT38.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 TfrViewListaTraspasos.cxGridViewCellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  if Assigned(FOnDblClick) then
    FOnDblClick(Sender);
end;

procedure TfrViewListaTraspasos.Preview;
begin
  inherited;
//  dxComponentPrinterLink1.OnInitializeCell := dxComponentPrinterLink1InitializeCell;
  dxComponentPrinterLink1.Preview;
end;

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

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

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

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

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

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

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

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

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

  AStr := VarToStrDef(ARecord.Values[cxGridViewT48.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 TfrViewListaTraspasos.cxGridViewTR38StylesGetContentStyle(
  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[cxGridViewT38.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaTraspasos.cxGridViewTR40StylesGetContentStyle(
  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[cxGridViewT40.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaTraspasos.cxGridViewTR42StylesGetContentStyle(
  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[cxGridViewT42.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaTraspasos.cxGridViewTR44StylesGetContentStyle(
  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[cxGridViewT44.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaTraspasos.cxGridViewTR46StylesGetContentStyle(
  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[cxGridViewT46.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaTraspasos.cxGridViewTR48StylesGetContentStyle(
  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[cxGridViewT48.Index] then
      AStyle := cxStyleCantidadRectificada;
  end;
end;

procedure TfrViewListaTraspasos.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 TfrViewListaTraspasos.cxGridViewCODIGOGetDisplayText(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: String);
begin
  inherited;
  AText := StringReplace(AText, 'Código : ', '', [rfReplaceAll, rfIgnoreCase])
end;

procedure TfrViewListaTraspasos.cxGridViewTOTAL_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, cxGridViewTR38.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridViewTR40.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridViewTR42.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridViewTR44.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridViewTR46.Index], 0);
    Total := Total + StrToFloatDef(DisplayTexts[ARecordIndex, cxGridViewTR48.Index], 0);
  end;
  AText := FloatToStr(Total);
end;

procedure TfrViewListaTraspasos.cxGridViewFECHAStylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  aux : string;
begin
  inherited;
  if Assigned(ARecord) then
  begin
    aux := ARecord.Values[cxGridViewCONTABILIZAR.Index];
    if aux = 'N' then
      AStyle := cxStyleNoContabilizar
  end;
end;

procedure TfrViewListaTraspasos.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.Index];
    if aux = 'N' then
    begin
      AnItem.Font.Style := cxStyleNoContabilizar.Font.Style;
      AnItem.Font.Color := cxStyleNoContabilizar.Font.Color;
    end;
  end;
end;

procedure TfrViewListaTraspasos.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.
