unit srvArticulos_Impl;

{----------------------------------------------------------------------------}
{ This unit was automatically generated by the RemObjects SDK after reading  }
{ the RODL file associated with this project .                               }
{                                                                            }
{ This is where you are supposed to code the implementation of your objects. }
{----------------------------------------------------------------------------}

interface

uses
  {vcl:} Classes, SysUtils,
  {RemObjects:} uROClientIntf, uROTypes, uROServer, uROServerIntf, uROSessions,
  {Ancestor Implementation:} DataAbstractService_Impl,
  {Used RODLs:} DataAbstract4_Intf,
  {Generated:}   uDABusinessProcessor, uDABin2DataStreamer, uDADataStreamer,
  uDAScriptingProvider, uDAClasses,
  FactuGES_Intf, uDAInterfaces, uROClient;

type
  { TsrvArticulos }
  TsrvArticulos = class(TDataAbstractService, IsrvArticulos)
    bpArticulos: TDABusinessProcessor;
    Bin2DataStreamer: TDABin2DataStreamer;
    bpArticulos_Proveedores: TDABusinessProcessor;
    schArticulos: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DARemoteServiceBeforeGetDatasetData(const Dataset: IDADataset;
      const IncludeSchema: Boolean; const MaxRecords: Integer);
    procedure DARemoteServiceCreate(Sender: TObject);
    procedure DataAbstractServiceBeforeAcquireConnection(aSender: TObject;
      var aConnectionName: string);

    function DarListaProveedoresConArticulos(Const IDEmpresa: Integer): Binary;
  end;

implementation

{$R *.dfm}
uses
  {Generated:} FactuGES_Invk, uDataModuleServer, Variants, uStringsUtils,
  uDatabaseUtils, schArticulosClient_Intf, uRestriccionesUsuarioUtils, dialogs,
  uBizArticulosServer;

procedure Create_srvArticulos(out anInstance : IUnknown);
begin
  anInstance := TsrvArticulos.Create(NIL);
end;

{ srvArticulos }
procedure TsrvArticulos.DARemoteServiceBeforeGetDatasetData(
  const Dataset: IDADataset; const IncludeSchema: Boolean;
  const MaxRecords: Integer);
begin
  if (DataSet.Name = nme_Articulos) then
  begin
    { Aquí se asegura que el usuario sólo accede a los Articulos
      de las empresas a las que tiene permiso para acceder
      filtrando DataSet por ID_EMPRESA. }
    FiltrarAccesoUsuario(Session, Connection, schArticulos, DataSet, fld_ArticulosID_EMPRESA);
  end;
end;

procedure TsrvArticulos.DARemoteServiceCreate(Sender: TObject);
begin
  //SessionManager := dmServer.SessionManager;
  bpArticulos.BusinessRulesID := BIZ_SERVER_ARTICULOS;
end;

function TsrvArticulos.DarListaProveedoresConArticulos(Const IDEmpresa: Integer): Binary;
var
  dsProveedores : IDADataset;
  Aux : TStringList;
begin
  Result := Binary.Create;

  Aux := TStringList.Create;
  dsProveedores := schArticulos.NewDataset(Connection, 'ProveedoresConArticulos', ['ID_EMPRESA'], [IDEmpresa], True);
  try
    while not dsProveedores.EOF do
    begin
      if not VarIsNull(dsProveedores.Fields[0].AsVariant) then
         Aux.Add(Format('%s=%d', [dsProveedores.Fields[1].AsString, dsProveedores.Fields[0].AsInteger]));
      dsProveedores.Next;
    end;

    Aux.SaveToStream(Result);

  finally
    dsProveedores.Close;
    dsProveedores := NIL;
    FreeAndNil(Aux);    
  end;
end;

procedure TsrvArticulos.DataAbstractServiceBeforeAcquireConnection(
  aSender: TObject; var aConnectionName: string);
begin
  ConnectionName := dmServer.ConnectionName;
end;

initialization
  TROClassFactory.Create('srvArticulos', Create_srvArticulos, TsrvArticulos_Invoker);

finalization

end.
