unit uViewElegirArticulosFacturasProveedor;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewArticulos, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxImageComboBox, cxCurrencyEdit,
  cxSpinEdit, dxPSGlbl, dxPSUtl, dxPSEngn, dxPrnPg, dxBkgnd, dxWrap, dxPrnDev,
  dxPSCompsProvider, dxPSFillPatterns, dxPSEdgePatterns, ImgList, PngImageList,
  cxGridCustomPopupMenu, cxGridPopupMenu, dxPSCore, dxPScxCommon, dxPScxGrid6Lnk,
  uDADataTable, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, dxPgsDlg,
  ActnList, TB2Item, SpTBXItem, TB2Toolbar, SpTBXDkPanels, TB2Dock, uViewFiltroBase,
  uBizContactos, uDAInterfaces, uViewArticulosPorProveedor;

type
  IViewElegirArticulosFacturasProveedor = interface(IViewArticulos)
  ['{8F2E6451-0A4E-4DF1-908A-62BB5785A150}']
    function GetProveedor: IBizProveedor;
    procedure SetProveedor(const Value: IBizProveedor);
    property Proveedor: IBizProveedor read GetProveedor write SetProveedor;
  end;

  TfrViewElegirArticulosFacturasProveedor = class(TfrViewArticulosPorProveedor, IViewElegirArticulosFacturasProveedor)
    cxGridLevel1: TcxGridLevel;
    procedure cxGridActiveTabChanged(Sender: TcxCustomGrid; ALevel: TcxGridLevel);
    procedure CustomViewDestroy(Sender: TObject);

  private
    FProveedor: IBizProveedor;
    function GetProveedor: IBizProveedor;
    procedure SetProveedor(const Value: IBizProveedor);

    //Filtros relativos a la vista
    procedure AnadirFiltroProveedor;

  public
    property Proveedor: IBizProveedor read GetProveedor write SetProveedor;
    procedure AnadirOtrosFiltros; override;

  end;

implementation
{$R *.dfm}

procedure TfrViewElegirArticulosFacturasProveedor.AnadirFiltroProveedor;
var
  FFiltro : TcxFilterCriteriaItemList;
begin
  FFiltro := AddFilterGrid(fboAnd);

  case cxGrid.ActiveLevel.Index of
    0   : if Assigned(FProveedor) then
             FFiltro.AddItem(cxGridViewID_PROVEEDOR, foEqual, FProveedor.ID, FProveedor.NOMBRE);
    1   : if Assigned(FProveedor) then
             FFiltro.AddItem(cxGridViewID_PROVEEDOR, foEqual, Null, 'ARTICULOS SIN PROVEEDOR');
  end;
end;

procedure TfrViewElegirArticulosFacturasProveedor.AnadirOtrosFiltros;
begin
  inherited;

  AnadirFiltroProveedor;

  //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 TfrViewElegirArticulosFacturasProveedor.CustomViewDestroy(Sender: TObject);
begin
  FProveedor := Nil;
  inherited;
end;

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

function TfrViewElegirArticulosFacturasProveedor.GetProveedor: IBizProveedor;
begin
  Result := FProveedor;
end;

procedure TfrViewElegirArticulosFacturasProveedor.SetProveedor(
  const Value: IBizProveedor);
begin
  FProveedor := Value;
  if Assigned(FProveedor) then
  begin
     cxGridLevel.Caption := FProveedor.NOMBRE;
     RefrescarFiltro;
  end;
end;

end.
