unit srvFacturasCliente_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:} DARemoteService_Impl,
  {Used RODLs:} DataAbstract_Intf,
  {Generated:} FactuGES_Intf, uDADataTable, uDACDSDataTable, frxClass, DB,
  frxDBSet, uDAScriptingProvider, uDABusinessProcessor, uDAClasses,
  uDataModuleServer, uDAInterfaces, frxDCtrl, frxDMPExport, frxGradient,
  frxChBox, frxCross, frxRich, frxChart, frxOLE, frxBarcode, uDABINAdapter;

type
  { TsrvFacturasCliente }
  TsrvFacturasCliente = class(TDARemoteService, IsrvFacturasCliente)
    frxDBDetalles: TfrxDBDataset;
    frxDBCabecera: TfrxDBDataset;
    DADataCabecera: TDADataSource;
    DADataDetalles: TDADataSource;
    tbl_FacturaCliente: TDACDSDataTable;
    tbl_DetallesFacturaCliente: TDACDSDataTable;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxOLEObject1: TfrxOLEObject;
    frxChartObject1: TfrxChartObject;
    frxRichObject1: TfrxRichObject;
    frxCrossObject1: TfrxCrossObject;
    frxCheckBoxObject1: TfrxCheckBoxObject;
    frxGradientObject1: TfrxGradientObject;
    frxDotMatrixExport1: TfrxDotMatrixExport;
    frxDialogControls1: TfrxDialogControls;
    DABINAdapter: TDABINAdapter;o
    frxReport1: TfrxReport;
  private
  protected
    { IsrvFacturasCliente methods }
    function GetNextAutoInc: Integer;
    function GenerateReport(const Codigo: Integer): Binary;
    function DarNuevaReferenciaFactura: String;
    function DarNuevaReferenciaFacturaPro: String;
  end;

implementation

{$R *.dfm}
uses
  Dialogs,
  {Generated:} FactuGES_Invk, uRORemoteDataModule,
  fServerForm, Variants, IB, schFacturasClienteClient_Intf;

procedure Create_srvFacturasCliente(out anInstance : IUnknown);
begin
  anInstance := TsrvFacturasCliente.Create(NIL);
end;

{ srvFacturasCliente }
function TsrvFacturasCliente.DarNuevaReferenciaFactura: String;
var
  dsReferencia: IDADataset;
begin
  dsReferencia := schFacturasCliente.NewDataset(Connection, 'DarReferenciaFactura');
  dsReferencia.Active := True;
  Result := dsReferencia.FieldByName('REFERENCIA').AsString;
end;

function TsrvFacturasCliente.DarNuevaReferenciaFacturaPro: String;
var
  dsReferencia: IDADataset;
begin
  dsReferencia := schFacturasCliente.NewDataset(Connection, 'DarReferenciaFacturaProforma');
  dsReferencia.Active := True;
  Result := dsReferencia.FieldByName('REFERENCIA').AsString;
end;

function TsrvFacturasCliente.GenerateReport(const Codigo: Integer): Binary;
var
  dsMaster: IDADataset;
  dsDetail: IDADataset;
  AStream: TMemoryStream;
begin
  AStream := TMemoryStream.Create;
  Result := Binary.Create;
  try
    dsMaster := schFacturasCliente.NewDataset(Connection, 'InformeCabeceraFacturaCliente', ['CODIGO'], [Codigo]);
    dsDetail := schFacturasCliente.NewDataset(Connection, 'InformeDetallesFacturaCliente', ['CODIGOFACTURA'], [Codigo], False);
    dsDetail.Active := True;

    AStream.Clear;
    ServiceAdapter.WriteDataset(AStream, dsMaster, [woRows, woSchema], -1);
    ServiceAdapter.ReadDataset(AStream, tbl_FacturaCliente, TRUE, '', TRUE, TRUE);

    AStream.Clear;
    ServiceAdapter.WriteDataset(AStream, dsDetail, [woRows, woSchema], -1);
    ServiceAdapter.ReadDataset(AStream, tbl_DetallesFacturaCliente, TRUE, '', TRUE, TRUE);

    frxReport1.LoadFromFile(DarRutaInformes + 'InfFacturaCliente.fr3', True);
    frxReport1.PrepareReport;
    frxReport1.PreviewPages.SaveToStream(Result);
  finally
    AStream.Free;
  end;
end;

function TsrvFacturasCliente.GetNextAutoinc: Integer;
begin
  Result := uDataModuleServer.GetNextAutoinc(Connection, 'GEN_FACTURASCLIENTE');
end;

initialization
  TROClassFactory.Create('srvFacturasCliente', Create_srvFacturasCliente, TsrvFacturasCliente_Invoker);

finalization

end.
