unit uRptRecibosCliente_Server;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, frxClass, frxDBSet, uDAScriptingProvider,
  uDADataTable, uDAMemDataTable, DB, uDAClasses, frxChart, frxGradient,
  frxChBox, frxCross, frxOLE, frxBarcode, frxRich, uDABINAdapter, uROTypes,
  uDAInterfaces, uDADataStreamer, FactuGES_Intf;

type
  TRptRecibosCliente = class(TDataModule)
    DADataCabecera: TDADataSource;
    tbl_Cabecera: TDAMemDataTable;
    DABINAdapter: TDABINAdapter;
    frxRichObject1: TfrxRichObject;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxOLEObject1: TfrxOLEObject;
    frxCrossObject1: TfrxCrossObject;
    frxCheckBoxObject1: TfrxCheckBoxObject;
    frxGradientObject1: TfrxGradientObject;
    frxChartObject1: TfrxChartObject;
    frxDBCabecera: TfrxDBDataset;
    DADataCompensados: TDADataSource;
    frxDBCompensados: TfrxDBDataset;
    tbl_Compensados: TDAMemDataTable;
    frxReport: TfrxReport;
    schReport: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DataModuleCreate(Sender: TObject);
  private
    FConnection: IDAConnection;
    procedure _GenerarRecibo(const ID : Integer);
  public
    function GenerarRecibo(const ListaID : TIntegerArray): Binary; 
  end;

implementation

{$R *.dfm}

uses
  uDataModuleServer;

const
  rptInforme = 'InfReciboCliente.fr3';

  { Dataset names for schReport }
  ds_InformeCabecera = 'Informe_Cabecera';
  ds_InformeCompensados = 'Informe_Compensados';

{ TRptReciboCliente }


procedure TRptRecibosCliente.DataModuleCreate(Sender: TObject);
begin
  schReport.ConnectionManager := dmServer.ConnectionManager;
  FConnection := dmServer.DarNuevaConexion;
  frxReport.EngineOptions.NewSilentMode := simReThrow;
end;

function TRptRecibosCliente.GenerarRecibo(const ListaID: TIntegerArray): Binary;
var
  i: Integer;
begin
  Result := Binary.Create;

  //FConnection.BeginTransaction; <--- Creo que no va a hacer falta.
  try
    //Vamos generando todos y cada uno de los albaranes recibidos
    for i := 0 to ListaID.Count - 1 do
      _GenerarRecibo(ListaID.Items[i]);
    frxReport.PreviewPages.SaveToStream(Result);
  finally
    //FConnection.RollbackTransaction; <--- Creo que no va a hacer falta.
  end;
end;

procedure TRptRecibosCliente._GenerarRecibo(const ID: Integer);
var
  dsMaster: IDADataset;
  dsCompensados : IDADataset;
  AStream: TMemoryStream;

begin
  AStream := TMemoryStream.Create;

  try
    frxDBCabecera.DataSource := DADataCabecera;
    frxDBCompensados.DataSource := DADataCompensados;

    tbl_Cabecera.ParamByName('ID').AsInteger := ID;
    tbl_Compensados.ParamByName('ID_RECIBO').AsInteger := ID;

    tbl_Cabecera.Active := True;
    tbl_Compensados.Active := True;

    frxReport.LoadFromFile(DarRutaInformes + rptInforme, True);
    frxReport.PrepareReport(False);

  finally
    AStream.Free;
    dsMaster := Nil;
    dsCompensados := Nil;
  end;
end;

end.
