unit srvAlbaranesProveedor_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
  { TsrvAlbaranesProveedor }
  TsrvAlbaranesProveedor = class(TDataAbstractService, IsrvAlbaranesProveedor)
    bpAlbaranesProveedor: TDABusinessProcessor;
    bpAlbaranesProveedor_Detalles: TDABusinessProcessor;
    Bin2DataStreamer: TDABin2DataStreamer;
    schAlbaranesProveedor: TDASchema;
    DADataDictionary: TDADataDictionary;
    procedure DARemoteServiceBeforeGetDatasetData(const Dataset: IDADataset;
      const IncludeSchema: Boolean; const MaxRecords: Integer);
    procedure DARemoteServiceCreate(Sender: TObject);
    procedure DataAbstractServiceBeforeAcquireConnection(aSender: TObject;
      var aConnectionName: string);
  protected
    function GenerarInforme(const ListaID: TIntegerArray;
      const ImprimirPrecio: Boolean; const ImprimirRefProveedor: Boolean): Binary;
    function GenerarInformeEnWord(const ID: Integer;
      const ImprimirPrecio: Boolean; const ImprimirRefProveedor: Boolean): Binary;
    function GenerarInformeEnPDF(const ListaID: TIntegerArray;
      const ImprimirPrecio: Boolean; const ImprimirRefProveedor: Boolean): Binary;
  end;



implementation

{$R *.dfm}
uses
  Dialogs,
  {Generated:} FactuGES_Invk, uDataModuleServer, uDatabaseUtils,
  schAlbaranesProveedorClient_Intf, uRestriccionesUsuarioUtils,
  uBizAlbaranProveedorServer, uRptAlbaranesProveedor_Server,
  uRptWordAlbaranProveedor, uSistemaFunc;

procedure Create_srvAlbaranesProveedor(out anInstance : IUnknown);
begin
  anInstance := TsrvAlbaranesProveedor.Create(NIL);
end;

{ srvAlbaranesProveedor }

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

procedure TsrvAlbaranesProveedor.DARemoteServiceCreate(Sender: TObject);
begin
  //SessionManager := dmServer.SessionManager;
  bpAlbaranesProveedor.BusinessRulesID := BIZ_SERVER_ALBARAN_PROVEEDOR;
end;

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

function TsrvAlbaranesProveedor.GenerarInforme(const ListaID: TIntegerArray;
      const ImprimirPrecio: Boolean; const ImprimirRefProveedor: Boolean): Binary;
var
  AReportGenerator : TRptAlbaranesProveedor;
begin
  AReportGenerator := TRptAlbaranesProveedor.Create(nil);
  try
    Result := AReportGenerator.GenerarAlbaran(ListaID,
      ImprimirPrecio, ImprimirRefProveedor);
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

function TsrvAlbaranesProveedor.GenerarInformeEnPDF(const ListaID: TIntegerArray;
      const ImprimirPrecio: Boolean; const ImprimirRefProveedor: Boolean): Binary;
var
  AReportGenerator : TRptAlbaranesProveedor;
begin
  AReportGenerator := TRptAlbaranesProveedor.Create(nil);
  try
    Result := AReportGenerator.GenerarAlbaranEnPDF(ListaID,
      ImprimirPrecio, ImprimirRefProveedor);
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

function TsrvAlbaranesProveedor.GenerarInformeEnWord(const ID: Integer;
      const ImprimirPrecio: Boolean; const ImprimirRefProveedor: Boolean): Binary;
var
  AReportGenerator : TRptWordAlbaranProveedor;
  AFicheroTMP : TFileName;
begin
  Result := NIL;
  AFicheroTMP := DarFicheroTemporal;
  AReportGenerator := TRptWordAlbaranProveedor.Create(nil);
  try
    AReportGenerator.ImprimirPrecio := ImprimirPrecio;
    AReportGenerator.ImprimirRefProveedor := ImprimirRefProveedor;
    if AReportGenerator.Exportar(IntToStr(ID), AFicheroTMP) then
    begin
      Result := Binary.Create;
      Result.LoadFromFile(AFicheroTMP);
      SysUtils.DeleteFile(AFicheroTMP)
    end;
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

initialization
  TROClassFactory.Create('srvAlbaranesProveedor', Create_srvAlbaranesProveedor, TsrvAlbaranesProveedor_Invoker);

finalization

end.
