unit uViewArticulos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewGrid, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, uDADataTable, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, uBizArticulos, ActnList, Menus,
  cxGridBandedTableView, cxGridDBBandedTableView, JvComponent,
  JvFormAutoSize, PngImageList, ImgList, dxPSGlbl, dxPSUtl, dxPSEngn,
  dxPrnPg, dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns,  dxPSCore, dxPScxCommon, dxPScxGrid6Lnk,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxCurrencyEdit, cxSpinEdit, cxCheckBox,
  cxCheckComboBox, cxImageComboBox, TB2Item, SpTBXItem, TB2Toolbar, SpTBXDkPanels,
  TB2Dock, uViewFiltroBase, dxPgsDlg, uDAInterfaces,  
  uArticulosController, uCustomView, uViewBase, cxLookAndFeels,
  cxLookAndFeelPainters, dxPSPDFExportCore, dxPSPDFExport, cxDrawTextUtils,
  dxPSPrVwStd, dxPSPrVwAdv, dxPScxEditorProducers, dxPScxExtEditorProducers,
  dxPScxPageControlProducer;

type
  IViewArticulos = interface(IViewGrid)
  ['{4C09BEC8-C82B-4DE8-8C36-40DB62224E95}']
    function GetArticulos: IBizArticulo;
    procedure SetArticulos(const Value: IBizArticulo);
    property Articulos: IBizArticulo read GetArticulos write SetArticulos;

    function GetController : IArticulosController;
    procedure SetController (const Value : IArticulosController);
    property Controller : IArticulosController read GetController write SetController;
  end;

  TfrViewArticulos = class(TfrViewGrid, IViewArticulos)
    actFamilia: TAction;
    TBXItem3: TSpTBXItem;
    TBXSeparatorItem1: TSpTBXSeparatorItem;
    TBXSeparatorItem2: TSpTBXSeparatorItem;
    TBXItem4: TSpTBXItem;
    actFabricante: TAction;
    cxGridViewRecID: TcxGridDBColumn;
    cxGridViewID: TcxGridDBColumn;
    cxGridViewID_EMPRESA: TcxGridDBColumn;
    cxGridViewREFERENCIA: TcxGridDBColumn;
    cxGridViewDESCRIPCION: TcxGridDBColumn;
    cxGridViewUNIDAD_MEDIDA: TcxGridDBColumn;
    cxGridViewFAMILIA: TcxGridDBColumn;
    cxGridViewFABRICANTE: TcxGridDBColumn;
    cxGridViewREFERENCIA_FABR: TcxGridDBColumn;
    cxGridViewPRECIO: TcxGridDBColumn;
    cxGridViewCODIGO: TcxGridDBColumn;
    cxGridViewDISENO: TcxGridDBColumn;
    cxGridViewCAMPOTECNICO: TcxGridDBColumn;
    procedure actFamiliaExecute(Sender: TObject);
    procedure actFamiliaUpdate(Sender: TObject);
    procedure actFabricanteExecute(Sender: TObject);
    procedure actFabricanteUpdate(Sender: TObject);
    procedure cxGridView2DataControllerCompare(ADataController: TcxCustomDataController; ARecordIndex1, ARecordIndex2,
      AItemIndex: Integer; const V1, V2: Variant; var Compare: Integer);
    procedure frViewFiltroBase1eListaPropertiesChange(Sender: TObject);
    procedure frViewFiltroBase1actQuitarFiltroExecute(Sender: TObject);
    procedure CustomViewShow(Sender: TObject);
    procedure cxGridViewDataControllerCompare(
      ADataController: TcxCustomDataController; ARecordIndex1, ARecordIndex2,
      AItemIndex: Integer; const V1, V2: Variant; var Compare: Integer);

  protected
    FArticulos: IBizArticulo;
    FController: IArticulosController;
    function GetArticulos: IBizArticulo; virtual;
    procedure SetArticulos(const Value: IBizArticulo); virtual;
    function GetController : IArticulosController;
    procedure SetController (const Value : IArticulosController);

  public
    property Articulos: IBizArticulo read GetArticulos write SetArticulos;
    property Controller : IArticulosController read GetController write SetController;
    procedure AnadirOtrosFiltros; override;
  end;

implementation
{$R *.dfm}

uses
  schArticulosClient_Intf, uReferenciasUtils, cxVariants;


{ TfrViewArticulos }

{
******************************* TfrViewArticulos *******************************
}
function TfrViewArticulos.GetArticulos: IBizArticulo;
begin
  Result := FArticulos;
end;

function TfrViewArticulos.GetController: IArticulosController;
begin
  Result := FController;
end;

procedure TfrViewArticulos.SetArticulos(const Value: IBizArticulo);
begin
  FArticulos := Value;
  if Assigned(FArticulos) then
     dsDataSource.DataTable := FArticulos.DataTable;
end;


procedure TfrViewArticulos.SetController(const Value: IArticulosController);
begin
  FController := Value;
end;

procedure TfrViewArticulos.actFabricanteExecute(Sender: TObject);
begin
  if (cxGridViewFABRICANTE.GroupIndex < 0) then
  begin
    cxGridViewFABRICANTE.GroupIndex := cxGridView.GroupedColumnCount;
    cxGridViewFABRICANTE.Visible := False;
  end
  else
  begin
    cxGridViewFABRICANTE.GroupIndex := -1;
    cxGridViewFABRICANTE.Visible := True;
  end;
end;

procedure TfrViewArticulos.actFabricanteUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Checked := not (cxGridViewFABRICANTE.GroupIndex < 0);
end;

procedure TfrViewArticulos.actFamiliaExecute(Sender: TObject);
begin
  if (cxGridViewFAMILIA.GroupIndex < 0) then
  begin
    cxGridViewFAMILIA.GroupIndex := cxGridView.GroupedColumnCount;
    cxGridViewFAMILIA.Visible := False;
  end
  else
  begin
    cxGridViewFAMILIA.GroupIndex := -1;
    cxGridViewFAMILIA.Visible := True;
  end;
end;

procedure TfrViewArticulos.actFamiliaUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Checked := not (cxGridViewFAMILIA.GroupIndex < 0);
end;

procedure TfrViewArticulos.AnadirOtrosFiltros;
begin
  inherited;

  //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 TfrViewArticulos.CustomViewShow(Sender: TObject);
begin
  inherited;
  AjustarAncho;
end;

procedure TfrViewArticulos.cxGridView2DataControllerCompare(
  ADataController: TcxCustomDataController; ARecordIndex1, ARecordIndex2,
  AItemIndex: Integer; const V1, V2: Variant; var Compare: Integer);
begin
  inherited;
  if ((AItemIndex = cxGridViewREFERENCIA.Index)) and
     (VarType(V1) = VarType(V2)) and (VarType(V1) = varString) then
    Compare := CompararReferencias(V1, V2)
  else
    Compare := VarCompare(V1, V2);
end;

procedure TfrViewArticulos.cxGridViewDataControllerCompare(
  ADataController: TcxCustomDataController; ARecordIndex1, ARecordIndex2,
  AItemIndex: Integer; const V1, V2: Variant; var Compare: Integer);
begin
  inherited;
  if (AItemIndex = cxGridViewREFERENCIA.Index) and
     (VarType(V1) = VarType(V2)) and (VarType(V1) = varString) then
    Compare := CompararReferencias(V1, V2)
  else
    Compare := VarCompare(V1, V2);
end;

procedure TfrViewArticulos.frViewFiltroBase1actQuitarFiltroExecute(Sender: TObject);
begin
  frViewFiltroBase1.txtFiltroTodo.Clear;
  frViewFiltroBase1.eLista.Clear;
end;

procedure TfrViewArticulos.frViewFiltroBase1eListaPropertiesChange(Sender: TObject);
begin
  inherited;
  RefrescarFiltro;
end;

end.
