unit srvEmpresas_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:} FactuGES_Intf, uDAScriptingProvider, uDABusinessProcessor,
  uDABin2DataStreamer, uDADataStreamer, uDAClasses, uDAInterfaces,
  uDADataTable;
  
type
  { TsrvEmpresas }
  TsrvEmpresas = class(TDataAbstractService, IsrvEmpresas)
    Diagrams: TDADiagrams;
    DABin2DataStreamer: TDABin2DataStreamer;
    bpEmpresas: TDABusinessProcessor;
    schEmpresas: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DARemoteServiceCreate(Sender: TObject);
    procedure DataAbstractServiceBeforeAcquireConnection(aSender: TObject;
      var aConnectionName: string);
    procedure DataAbstractServiceBeforeGetDatasetData(aSender: TObject;
      const aDataset: IDADataset; const aIncludeSchema: Boolean;
      const aMaxRecords: Integer);
  protected
    function DarDatosEmpresa(const EmpresaID: Integer; AEmpresaDataSet: IDADataset): Boolean;
    function DarListaFormasPago: Binary;
    function DarListaTiposIVA: Binary;
  end;

function RecuperarEmpresa(const AID: Integer; AEmpresaDataSet: IDADataset): Boolean;

implementation

{$R *.dfm}
uses
  {Generated:} FactuGES_Invk, uDataModuleServer, uRORemoteDataModule,
  uDatabaseUtils, Dialogs, Variants, uROStreamSerializer, uROBinaryHelpers,
  uSesionesUtils, schEmpresasClient_Intf, uUsersManager,
  uRestriccionesUsuarioUtils, uROClasses, uBizEmpresasServer,
  srvFormasPago_Impl, srvTiposIva_Impl;

procedure Create_srvEmpresas(out anInstance : IUnknown);
begin
  anInstance := TsrvEmpresas.Create(NIL);
end;

function RecuperarEmpresa(const AID: Integer; AEmpresaDataSet: IDADataset): Boolean;
begin
  with TsrvEmpresas.Create(NIL) do
    try
      Result := DarDatosEmpresa(AID, AEmpresaDataSet);
    finally
      Free;
    end;
end;


{ srvEmpresas }
function TsrvEmpresas.DarDatosEmpresa(const EmpresaID: Integer; AEmpresaDataSet: IDADataset): Boolean;
var
  ASchema : TDASchema;
  AConn : IDAConnection;
  dsData: IDADataset;

//  AWhere : TDAWhereExpression;
  ABinary : Binary;
begin
  {
    Construir la expresión del Where a partir de los parámetros
    que se reciban.
  }
  { ************ NO SÉ POR QUÉ NO FUNCIONA EL DYNAMICWHERE AQUI
  with TDAWhereBuilder.Create do
    try
      AWhere := NewBinaryExpression(NewField('EMPRESAS', 'ID'), NewConstant(EmpresaID, datInteger), dboEqual);
    finally
      Free;
    end;
  ************** }

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

  try
    try
      dsData := ASchema.NewDataset(AConn, 'Empresas', '', False);
      dsData.Where.AddText('EMPRESAS.ID = ' + IntToStr(EmpresaID));
    except
      RaiseError('No existe la tabla EMPRESAS');
    end;

    dsData.Active := True;

    ABinary := Binary.Create;
    try
      ServiceDataStreamer.WriteDataset(ABinary, dsData, [woSchema, woRows]);
      ServiceDataStreamer.Initialize(ABinary, aiReadFromBeginning);
      ServiceDataStreamer.ReadDataset('Empresas', AEmpresaDataSet, True, True);
      Result := True;
    finally
      FreeAndNIL(ABinary);
    end;
  finally
    dsData := NIL;
    AConn := NIL;
  end;
end;

procedure TsrvEmpresas.DARemoteServiceCreate(Sender: TObject);
begin
  SessionManager := dmServer.SessionManager;
  bpEmpresas.BusinessRulesID := BIZ_SERVER_EMPRESAS;  
end;

function TsrvEmpresas.DarListaFormasPago: Binary;
var
  AFormasPagoServer : TsrvFormasPago;
begin
  AFormasPagoServer := TsrvFormasPago.Create(nil);
  try
    Result := AFormasPagoServer.DarListaFormasPago;
  finally
    FreeAndNIL(AFormasPagoServer);
  end;
end;

function TsrvEmpresas.DarListaTiposIVA: Binary;
var
  ATiposIVAServer : TsrvTiposIVA;
begin
  ATiposIVAServer := TsrvTiposIVA.Create(nil);
  try
    Result := ATiposIVAServer.DarListaTiposIVA;
  finally
    FreeAndNIL(ATiposIVAServer);
  end;
end;

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

procedure TsrvEmpresas.DataAbstractServiceBeforeGetDatasetData(aSender: TObject;
  const aDataset: IDADataset; const aIncludeSchema: Boolean;
  const aMaxRecords: Integer);
begin
  Exit;
  if (aDataset.Name <> nme_EmpresasDatosBanco) then
  begin
    { Aquí se asegura que el usuario sólo accede a las empresas a
      las que tiene permiso para acceder filtrando DataSet por ID_EMPRESA. }
    FiltrarAccesoUsuario(Session, Connection, ServiceSchema, aDataset, fld_EmpresasID);
  end;
end;

initialization
  TROClassFactory.Create('srvEmpresas', Create_srvEmpresas, TsrvEmpresas_Invoker);

finalization

end.
