unit uViewInventario;

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;

type
  IViewInventario = interface(IViewGrid)
    ['{4BAE6CBE-4CBB-4103-B85E-80BE5310723A}']
    function GetInventario: IBizInventario;
    procedure SetInventario(const Value: IBizInventario);
    property Inventario: IBizInventario read GetInventario write SetInventario;
    function GetAlmacenes: IBizAlmacen;
    procedure SetAlmacenes(const Value: IBizAlmacen);
    property Almacenes: IBizAlmacen read GetAlmacenes write SetAlmacenes;
  end;

  TfrViewInventario = class(TfrViewGrid, IViewInventario)
    cxGridDBTableView1: TcxGridDBTableView;
    PngImageList: TPngImageList;
    cxStyleArticuloSinStock: TcxStyle;
    cxGridViewSTOCK: TcxGridDBColumn;
    cxGridViewDESCRIPCION: TcxGridDBColumn;
    cxGridViewREFERENCIA: TcxGridDBColumn;
    cxGridViewFAMILIA: TcxGridDBColumn;
    cxGridViewALMACEN: TcxGridDBColumn;
    cxGridViewID_ALMACEN: TcxGridDBColumn;
    cxGridViewRESERVA: TcxGridDBColumn;
    cxGridViewPENDIENTE_RECEPCION: TcxGridDBColumn;
    cxGridViewREFERENCIA_PROVEEDOR: TcxGridDBColumn;
    cxGridViewUNIDADES_ALMACEN: TcxGridDBColumn;
    cxGridViewPRECIO_NETO: TcxGridDBColumn;
    cxGridViewCOSTE_UNIDADES: TcxGridDBColumn;

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

  private
    //Filtros relativos a la vista
    procedure AnadirFiltroAlmacenes;

  protected
    FAlmacenes: IBizAlmacen;
    FInventario: IBizInventario;
    function GetAlmacenes: IBizAlmacen;
    procedure SetAlmacenes(const Value: IBizAlmacen);
    function GetInventario: IBizInventario;
    procedure SetInventario(const Value: IBizInventario);
    procedure AnadirOtrosFiltros; override;

  public
    property Inventario: IBizInventario read GetInventario write SetInventario;
    destructor Destroy; override;
  end;

implementation
{$R *.dfm}

uses uDataModuleInventario, schInventarioClient_Intf;

procedure TfrViewInventario.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, foEqual, CodigoAlmacen, CodigoAlmacen);
     cxGridViewALMACEN.Visible := False;
  end
  else
     cxGridViewALMACEN.Visible := True;
end;

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

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

procedure TfrViewInventario.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_INVENTARIOSTOCK).Index;
    AStock := ARecord.DisplayTexts[IndiceCol];
    if (AStock < 0) then
      AStyle := cxStyleArticuloSinStock;
  end;
end;

destructor TfrViewInventario.Destroy;
begin
  FAlmacenes := Nil;
  FInventario := Nil;
  inherited;
end;

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

function TfrViewInventario.GetInventario: IBizInventario;
begin
  Result := FInventario;
end;

procedure TfrViewInventario.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 TfrViewInventario.SetInventario(const Value: IBizInventario);
begin
  FInventario := Value;
  if Assigned(FInventario) then
     dsDataSource.DataTable := FInventario.DataTable
  else
     dsDataSource.DataTable := Nil;
end;

end.
