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, cxIntlPrintSys3, dxPSCore, dxPScxCommon, dxPScxGrid6Lnk,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxCurrencyEdit, cxSpinEdit, cxCheckBox,
  cxCheckComboBox, cxImageComboBox, TB2Item, TBX, TB2Toolbar, TBXDkPanels,
  TB2Dock, uViewFiltroBase, dxPgsDlg, uDAInterfaces, dxSkinsCore, dxSkinBlack,
  dxSkinBlue, dxSkinCaramel, dxSkinCoffee, dxSkinGlassOceans, dxSkiniMaginary,
  dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin,
  dxSkinMoneyTwins, dxSkinOffice2007Black, dxSkinOffice2007Blue,
  dxSkinOffice2007Green, dxSkinOffice2007Pink, dxSkinOffice2007Silver,
  dxSkinSilver, dxSkinStardust, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinXmas2008Blue, dxSkinscxPCPainter, uArticulosController;

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)
    cxGridViewREFERENCIA: TcxGridDBColumn;
    cxGridViewDESCRIPCION: TcxGridDBColumn;
    cxGridViewFAMILIA: TcxGridDBColumn;
    cxGridViewNOMBRE_PROVEEDOR: TcxGridDBColumn;
    actFamilia: TAction;
    actProveedor: TAction;
    TBXItem2: TTBXItem;
    TBXItem3: TTBXItem;
    TBXSeparatorItem1: TTBXSeparatorItem;
    TBXSeparatorItem2: TTBXSeparatorItem;
    cxGridViewINVENTARIABLE: TcxGridDBColumn;
    cxGridViewUNIDAD_MEDIDA: TcxGridDBColumn;
    cxGridViewFABRICANTE: TcxGridDBColumn;
    TBXItem4: TTBXItem;
    TBXSeparatorItem3: TTBXSeparatorItem;
    actFabricante: TAction;
    cxGridViewREFERENCIA_FABR: TcxGridDBColumn;
    cxGridViewPRECIO_PVP_VENTA: TcxGridDBColumn;
    cxGridViewID_PROVEEDOR: TcxGridDBColumn;
    cxGridViewMARGEN: TcxGridDBColumn;
    cxGridViewPRECIO_PVP_TOTAL: TcxGridDBColumn;
    cxGridViewTIEMPO: TcxGridDBColumn;
    cxGridViewPARAM_TIEMPO: TcxGridDBColumn;
    cxGridViewMANO_OBRA: TcxGridDBColumn;
    procedure actProveedorExecute(Sender: TObject);
    procedure actProveedorUpdate(Sender: TObject);
    procedure actFamiliaExecute(Sender: TObject);
    procedure actFamiliaUpdate(Sender: TObject);
    procedure actFabricanteExecute(Sender: TObject);
    procedure actFabricanteUpdate(Sender: TObject);
    procedure cxGridViewDataControllerCompare(
      ADataController: TcxCustomDataController; ARecordIndex1, ARecordIndex2,
      AItemIndex: Integer; const V1, V2: Variant; var Compare: Integer);
    procedure frViewFiltroBase1eListaPropertiesChange(Sender: TObject);
    procedure CustomViewCreate(Sender: TObject);
    procedure CustomViewDestroy(Sender: TObject);
    procedure frViewFiltroBase1actQuitarFiltroExecute(Sender: TObject);

  private
    FListaProveedores: TStringList;
    //Filtros relativos a la vista
    procedure AnadirFiltroProveedores;

  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);
var
  i: Integer;

begin
  FController := Value;

  //Rellenamos la lista de proveedores con articulos
  if Assigned(FController) then
  begin
    FListaProveedores := FController.DarListaProveedoresConArticulos;
    with frViewFiltroBase1.eLista.Properties.Items do
    begin
      BeginUpdate;
      try
        Clear;
        Add('Todos');
        for i := 0 to FListaProveedores.Count - 1 do
            Add(FListaProveedores.Names[i]);

        frViewFiltroBase1.eLista.ItemIndex := 0;
      finally
        EndUpdate;
      end;
    end;
  end;
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.actProveedorExecute(Sender: TObject);
begin
  inherited;
  if (cxGridViewNOMBRE_PROVEEDOR.GroupIndex < 0) then
  begin
    cxGridViewNOMBRE_PROVEEDOR.GroupIndex := cxGridView.GroupedColumnCount;
    cxGridViewNOMBRE_PROVEEDOR.Visible := False;
  end
  else
  begin
    cxGridViewNOMBRE_PROVEEDOR.GroupIndex := -1;
    cxGridViewNOMBRE_PROVEEDOR.Visible := True;
  end;
end;

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

procedure TfrViewArticulos.AnadirFiltroProveedores;
var
  FFiltro : TcxFilterCriteriaItemList;
  CodigoProveedor: Variant;
  Index: Integer;
begin
  //Solo se aplica este filtro en el caso de tener activo el panel de detalle de filtro
  if frViewFiltroBase1.Visible then
  begin
    if VarIsNull(frViewFiltroBase1.eLista.EditValue) then
       CodigoProveedor := frViewFiltroBase1.eLista.EditValue
    else
       CodigoProveedor := FListaProveedores.Values[frViewFiltroBase1.eLista.EditValue];

    if not VarIsNull(CodigoProveedor)
    and (VarToStr(CodigoProveedor) <> '') then
    begin
       FFiltro := AddFilterGrid(fboAnd);
       FFiltro.AddItem(cxGridViewID_PROVEEDOR, foEqual, CodigoProveedor, CodigoProveedor);
       cxGridViewNOMBRE_PROVEEDOR.Visible := False;
    end
    else
       cxGridViewNOMBRE_PROVEEDOR.Visible := True;
  end;
end;

procedure TfrViewArticulos.AnadirOtrosFiltros;
begin
  inherited;

  AnadirFiltroProveedores;

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

procedure TfrViewArticulos.CustomViewDestroy(Sender: TObject);
begin
  inherited;
  FreeAndNil(FListaProveedores);
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.
