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

unit uViewListaRecepcionesAlbaran;

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, uDAInterfaces, TBXDkPanels, uCustomView,
  uViewBase, uViewParametrosControlGrid, uViewParametrosCentro, TB2Dock;

type
  IViewListaRecepcionesAlbaran = interface(IViewGrid)
  ['{F0B4B97B-158B-4FD8-B3F1-8ECAC7AE40D5}']
    function GetItems: IBizRecepcionesAlbaran;
    procedure SetItems(const Value: IBizRecepcionesAlbaran);
    property Items: IBizRecepcionesAlbaran read GetItems write SetItems;
  end;

  TfrViewListaRecepcionesAlbaran = class(TfrViewGrid, IViewListaRecepcionesAlbaran)
    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;
    cxGridViewRecID: TcxGridDBColumn;
    cxGridViewALBARAN: TcxGridDBColumn;
    cxGridViewESTADOALBARAN: TcxGridDBColumn;
    cxGridViewFILIAL: TcxGridDBColumn;
    cxGridViewESTADOPRODUCTO: TcxGridDBColumn;
    cxGridViewCODIGO: TcxGridDBColumn;
    cxGridViewPRODUCTO: TcxGridDBColumn;
    cxGridViewCOLOR: TcxGridDBColumn;
    cxGridViewE38: TcxGridDBColumn;
    cxGridViewE40: TcxGridDBColumn;
    cxGridViewE42: TcxGridDBColumn;
    cxGridViewE44: TcxGridDBColumn;
    cxGridViewE46: TcxGridDBColumn;
    cxGridViewE48: TcxGridDBColumn;
    TBXDockablePanel1: TTBXDockablePanel;
    frViewParametrosCentro1: TfrViewParametrosCentro;
    TBXLabel1: TTBXLabel;
    pnlAgrupaciones: TTBXAlignmentPanel;
    TBXButton3: TTBXButton;
    procedure cxGridViewALBARANGetDataText(Sender: TcxCustomGridTableItem;
      ARecordIndex: Integer; var AText: String);
  protected
    FItems: IBizRecepcionesAlbaran;
    function GetItems: IBizRecepcionesAlbaran;
    procedure SetItems(const Value: IBizRecepcionesAlbaran);

  public
    property Items: IBizRecepcionesAlbaran read GetItems write SetItems;
    
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Preview; override;
    procedure Print; override;
    procedure PrintSetup; override;
    procedure Refresh;
  end;


implementation

uses
  uDataModuleRecepciones, DateUtils, cxGridDBDataDefinitions,
  uViewGridBase, cxGridRows;

{$R *.dfm}
function TfrViewListaRecepcionesAlbaran.GetItems: IBizRecepcionesAlbaran;
begin
  Result := FItems;
end;

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

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

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

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

//No hacemos clear porque en este caso la aprobación o no del albaran no viene dado por la where desde el editor    
//    Items.DataTable.Where.Clear;
    if not Items.DataTable.Where.Empty then
       AWhere  := 'AND ' + AWhere;

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

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

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

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

procedure TfrViewListaRecepcionesAlbaran.cxGridViewALBARANGetDataText(
  Sender: TcxCustomGridTableItem; ARecordIndex: Integer;
  var AText: String);
var
  ARecIndex: Integer;
  Val1: string;
begin
  inherited;

  if Assigned(Sender) then
  begin
    Val1 := cxGridView.DataController.Values[ARecordIndex, cxGridViewALBARAN.Index];
    AText := Val1;
    Val1 := cxGridView.DataController.Values[ARecordIndex, cxGridViewFILIAL.Index];
    AText := AText + ' - Destino: ' + Val1;
  end;
end;

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

end.
