unit uViewArticulosPorProveedor;

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,  
  uArticulosController, uViewArticulos, uCustomView, uViewBase;

type
  TfrViewArticulosPorProveedor = 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 cxGridView2DataControllerCompare(ADataController: TcxCustomDataController; ARecordIndex1, ARecordIndex2,
      AItemIndex: Integer; const V1, V2: Variant; var Compare: Integer);
    procedure OnFiltroBaseListaPropertiesChange(Sender: TObject);
    procedure CustomViewCreate(Sender: TObject);
    procedure CustomViewDestroy(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);

  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 TfrViewArticulosPorProveedor.GetArticulos: IBizArticulo;
begin
  Result := FArticulos;
end;

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

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


procedure TfrViewArticulosPorProveedor.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 TfrViewArticulosPorProveedor.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 TfrViewArticulosPorProveedor.actFabricanteUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Checked := not (cxGridViewFABRICANTE.GroupIndex < 0);
end;

procedure TfrViewArticulosPorProveedor.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 TfrViewArticulosPorProveedor.actFamiliaUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Checked := not (cxGridViewFAMILIA.GroupIndex < 0);
end;

procedure TfrViewArticulosPorProveedor.actProveedorExecute(Sender: TObject);
begin
  inherited;
  if (cxGrid.ActiveLevel.Tag = 1) then
      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 TfrViewArticulosPorProveedor.actProveedorUpdate(Sender: TObject);
begin
  inherited;
  if (cxGrid.ActiveLevel.Tag = 1) then
  begin
     (Sender as TAction).Enabled := True;
     (Sender as TAction).Checked := not (cxGridViewNOMBRE_PROVEEDOR.GroupIndex < 0);
  end
  else
     (Sender as TAction).Enabled := False;
end;

procedure TfrViewArticulosPorProveedor.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
  //y sobre la lista de articulos detallada por proveedor
  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 TfrViewArticulosPorProveedor.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 TfrViewArticulosPorProveedor.CustomViewCreate(Sender: TObject);
begin
  inherited;
  FListaProveedores := Nil;
  Controller := TArticulosController.Create;
end;

procedure TfrViewArticulosPorProveedor.CustomViewDestroy(Sender: TObject);
begin
  inherited;
  frViewFiltroBase1.eLista.Properties.OnChange := Nil;
  FreeAndNil(FListaProveedores);
end;

procedure TfrViewArticulosPorProveedor.CustomViewShow(Sender: TObject);
begin
  inherited;
  AjustarAncho;
  frViewFiltroBase1.eLista.Properties.OnChange := OnFiltroBaseListaPropertiesChange;
end;

procedure TfrViewArticulosPorProveedor.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 TfrViewArticulosPorProveedor.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 TfrViewArticulosPorProveedor.frViewFiltroBase1actQuitarFiltroExecute(Sender: TObject);
begin
  frViewFiltroBase1.txtFiltroTodo.Clear;
  frViewFiltroBase1.eLista.Clear;
end;

procedure TfrViewArticulosPorProveedor.OnFiltroBaseListaPropertiesChange(Sender: TObject);
begin
  inherited;
  RefrescarFiltro;
end;

end.
