unit uViewDetalleReservas;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, frxClass, frxPreview, cxStyles,
  cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB,
  cxDBData, ActnList, uDADataTable, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, StdCtrls, ExtCtrls, cxLabel, dxPSGlbl, dxPSUtl,
  dxPSEngn, dxPrnPg, dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns, cxIntlPrintSys3, dxPSCore, dxPScxCommon, dxPScxGridLnk, cxImage,
  cxImageComboBox, ImgList, PngImageList, cxTextEdit, Grids, DBGrids, cxDBLookupComboBox,
  cxButtonEdit, cxGridCustomPopupMenu, cxGridPopupMenu, uViewGrid,
  uBizInventario, uBizAlmacenes, cxSpinEdit, uViewFiltroBase, TB2Item, TBX,
  TB2Toolbar, TBXDkPanels, TB2Dock, dxPgsDlg, cxCurrencyEdit, uAlmacenesController;

type
  IViewDetalleReservas = interface(IViewGrid)
  ['{4417F53A-5BB1-4B99-8760-4C0E1D449582}']
    function GetDetalleReservas: IBizDetalleReservas;
    procedure SetDetalleReservas(const Value: IBizDetalleReservas);
    property DetalleReservas: IBizDetalleReservas read GetDetalleReservas write SetDetalleReservas;
    function GetAlmacenes: IBizAlmacen;
    procedure SetAlmacenes(const Value: IBizAlmacen);
    property Almacenes: IBizAlmacen read GetAlmacenes write SetAlmacenes;
  end;

  TfrViewDetalleReservas = class(TfrViewGrid, IViewDetalleReservas)
    cxGridDBTableView1: TcxGridDBTableView;
    PngImageList: TPngImageList;
    cxStyleArticuloSinStock: TcxStyle;
    cxGridViewRecID: TcxGridDBColumn;
    cxGridViewREFERENCIA_ALB: TcxGridDBColumn;
    cxGridViewSITUACION_ALB: TcxGridDBColumn;
    cxGridViewFECHA_PREVISTA_ENVIO_ALB: TcxGridDBColumn;
    cxGridViewID_ALMACEN_ALB: TcxGridDBColumn;
    cxGridViewALMACEN_ALB: TcxGridDBColumn;
    cxGridViewCLIENTE_ALB: TcxGridDBColumn;
    cxGridViewFAMILIA_ART: TcxGridDBColumn;
    cxGridViewREFERENCIA_ART: TcxGridDBColumn;
    cxGridViewREFERENCIA_PROV_ART: TcxGridDBColumn;
    cxGridViewDESCRIPCION_ART: TcxGridDBColumn;
    cxGridViewCANTIDAD_ART: TcxGridDBColumn;

    procedure cxGridActiveTabChanged(Sender: TcxCustomGrid; ALevel: TcxGridLevel);
    procedure cxGridViewStylesGetContentStyle(Sender: TcxCustomGridTableView;
            ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out
            AStyle: TcxStyle);

  private
    FAlmacenesController: IAlmacenesController;
    FAlmacenes: IBizAlmacen;
    function GetAlmacenes: IBizAlmacen;
    procedure SetAlmacenes(const Value: IBizAlmacen);

    //Filtros relativos a la vista
    procedure AnadirFiltroAlmacenes;

  protected
    FDetalleReservas: IBizDetalleReservas;
    function GetDetalleReservas: IBizDetalleReservas;
    procedure SetDetalleReservas(const Value: IBizDetalleReservas);
    procedure AnadirOtrosFiltros; override;

  public
    property Almacenes: IBizAlmacen read GetAlmacenes write SetAlmacenes;
    property DetalleReservas: IBizDetalleReservas read GetDetalleReservas write SetDetalleReservas;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation
{$R *.dfm}

uses uDataModuleInventario, schInventarioClient_Intf;

procedure TfrViewDetalleReservas.AnadirFiltroAlmacenes;
var
  FFiltro : TcxFilterCriteriaItemList;
  CodigoAlmacen: String;
begin

  FFiltro := AddFilterGrid(fboAnd);

  CodigoAlmacen := IntToStr(cxGrid.Levels.Items[cxGrid.ActiveLevel.Index].Tag);
  if CodigoAlmacen <> '-1' then
  begin
     FFiltro.AddItem(cxGridViewID_ALMACEN_ALB, foEqual, CodigoAlmacen, CodigoAlmacen);
     cxGridViewALMACEN_ALB.Visible := False;
  end
  else
     cxGridViewALMACEN_ALB.Visible := True;
end;

procedure TfrViewDetalleReservas.AnadirOtrosFiltros;
begin
  inherited;

  AnadirFiltroAlmacenes;

  //Finalmente activamos el filtro si tenemos algo
  if cxGridView.DataController.Filter.IsEmpty
  then cxGridView.DataController.Filter.Active := False
  else cxGridView.DataController.Filter.Active := True;
  cxGrid.ActiveLevel.GridView := cxGridView;
end;

constructor TfrViewDetalleReservas.Create(AOwner: TComponent);
begin
  inherited;
  FAlmacenesController := TAlmacenesController.Create;
  Almacenes := FAlmacenesController.BuscarTodos;
end;

procedure TfrViewDetalleReservas.cxGridActiveTabChanged(Sender: TcxCustomGrid; ALevel: TcxGridLevel);
begin
  inherited;
  RefrescarFiltro;
end;

procedure TfrViewDetalleReservas.cxGridViewStylesGetContentStyle(Sender:
        TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem:
        TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  IndiceCol: Integer;
  AStock: Variant;
begin
  inherited;
{
  if Assigned(ARecord) then
  begin
    IndiceCol := (Sender as TcxGridDBTableView).GetColumnByFieldName(fld_DetalleReservasSTOCK).Index;
    AStock := ARecord.DisplayTexts[IndiceCol];
    if (AStock < 0) then
      AStyle := cxStyleArticuloSinStock;
  end;
}  
end;

destructor TfrViewDetalleReservas.Destroy;
begin
  FAlmacenesController := Nil;
  FAlmacenes := Nil;
  FDetalleReservas := Nil;
  inherited;
end;

function TfrViewDetalleReservas.GetAlmacenes: IBizAlmacen;
begin
  Result := FAlmacenes;
end;

function TfrViewDetalleReservas.GetDetalleReservas: IBizDetalleReservas;
begin
  Result := FDetalleReservas;
end;

procedure TfrViewDetalleReservas.SetAlmacenes(const Value: IBizAlmacen);
var
  Nivel: TcxGridLevel;
begin
  FAlmacenes := Value;

  if Assigned(FAlmacenes) then
  begin
    if not FAlmacenes.DataTable.Active then
       FAlmacenes.DataTable.Active := True;

    FAlmacenes.First;
    while not FAlmacenes.EOF do
    begin
      Nivel := cxGrid.Levels.Add;
      Nivel.Caption := FAlmacenes.NOMBRE;
      Nivel.Tag := FAlmacenes.ID;
      FAlmacenes.Next;
    end;
  end;
end;

procedure TfrViewDetalleReservas.SetDetalleReservas(const Value: IBizDetalleReservas);
begin
  FDetalleReservas := Value;
  if Assigned(FDetalleReservas) then
     dsDataSource.DataTable := FDetalleReservas.DataTable
  else
     dsDataSource.DataTable := Nil;
end;

end.
