unit srvAlmacenes_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;

type
  { TsrvAlmacenes }
  TsrvAlmacenes = class(TDataAbstractService, IsrvAlmacenes)
    Diagrams: TDADiagrams;
    Bin2DataStreamer: TDABin2DataStreamer;
    bpAlmacenes: TDABusinessProcessor;
    schAlmacenes: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DARemoteServiceAfterGetDatasetData(const Dataset: IDADataset;
      const IncludeSchema: Boolean; const MaxRecords: Integer);
    procedure DARemoteServiceCreate(Sender: TObject);
    procedure DataAbstractServiceBeforeAcquireConnection(aSender: TObject;
      var aConnectionName: string);
  protected
      function DarListaAlmacenes(const IDEmpresa: Integer): Binary;
  end;

implementation

{$R *.dfm}
uses
  {Generated:} FactuGES_Invk, uDataModuleServer, uROClasses,
  uDatabaseUtils, schAlmacenesClient_Intf, uRestriccionesUsuarioUtils;

procedure Create_srvAlmacenes(out anInstance : IUnknown);
begin
  anInstance := TsrvAlmacenes.Create(NIL);
end;

{ srvAlmacenes }
procedure TsrvAlmacenes.DARemoteServiceAfterGetDatasetData(
  const Dataset: IDADataset; const IncludeSchema: Boolean;
  const MaxRecords: Integer);
begin
  if DataSet.Name = nme_Almacenes then
  begin
    { Aquí se asegura que el usuario sólo accede a los almacenes
      de las empresas a las que tiene permiso para acceder
      filtrando DataSet por ID_EMPRESA. }
    FiltrarAccesoUsuario(Session, Connection, schAlmacenes, DataSet, fld_AlmacenesID_EMPRESA);
  end;
end;

procedure TsrvAlmacenes.DARemoteServiceCreate(Sender: TObject);
begin
  SessionManager := dmServer.SessionManager;
end;

function TsrvAlmacenes.DarListaAlmacenes(const IDEmpresa: Integer): Binary;
var
  ASchema : TDASchema;
  AConn : IDAConnection;
  dsData: IDADataset;
  ALista : TStringList;
begin
  Result := Binary.Create;

  ASchema := schAlmacenes;
  AConn := dmServer.ConnectionManager.NewConnection(dmServer.ConnectionManager.GetDefaultConnectionName);

  try
//Los almacenes serán comunes para todas las empresas.
    dsData := ASchema.NewDataset(AConn, 'ListaAlmacenes'); //, ['ID_EMPRESA'], [IntToStr(IDEmpresa)]);
  except
    RaiseError('No existe la tabla ALMACENES');
  end;

  ALista := TStringList.Create;
  try
    dsData.Active := True;
//    ALista.Sorted := True;
    while not dsData.EOF do
    begin
      ALista.Add(Format('%s=%d', [dsData.Fields[1].AsString, dsData.Fields[0].AsInteger]));
      dsData.Next;
    end;

    ALista.SaveToStream(Result);
  finally
    FreeANDNIL(ALista);
    dsData := NIL;
    AConn := NIL;
  end;
end;

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

initialization
  TROClassFactory.Create('srvAlmacenes', Create_srvAlmacenes, TsrvAlmacenes_Invoker);

finalization

end.
