unit uRptRecibosCliente_Server;

interface

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

type
  TRptRecibosCliente = class(TDataModule)
    DADataCabecera: TDADataSource;
    tbl_Cabecera: TDACDSDataTable;
    DABINAdapter: TDABINAdapter;
    frxRichObject1: TfrxRichObject;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxOLEObject1: TfrxOLEObject;
    frxCrossObject1: TfrxCrossObject;
    frxCheckBoxObject1: TfrxCheckBoxObject;
    frxGradientObject1: TfrxGradientObject;
    frxChartObject1: TfrxChartObject;
    frxDBCabecera: TfrxDBDataset;
    DADataCompensados: TDADataSource;
    frxDBCompensados: TfrxDBDataset;
    tbl_Compensados: TDACDSDataTable;
    schReport: TDASchema;
    DataDictionary: TDADataDictionary;
    frxReport: TfrxReport;
    procedure DataModuleCreate(Sender: TObject);
  private
    FConnection: IDAConnection;
    procedure GenerarRecibo(const ID : Integer); overload;
  public
    function GenerarRecibo(const ID : String): Binary; overload;
  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 ID: String): Binary;
var
  ID_Recibos: TStringList;
  i: Integer;

begin
  Result := Binary.Create;
  FConnection.BeginTransaction;

  try
    ID_Recibos := TStringList.Create;
    ID_Recibos.CommaText := ID;

    //Vamos generando todos y cada uno de los albaranes recibidos
    for i := 0 to ID_Recibos.Count - 1 do
        GenerarRecibo(StrToInt(ID_Recibos.Strings[i]));

    frxReport.PreviewPages.SaveToStream(Result);

  finally
    FConnection.RollbackTransaction;
  end;
end;

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

begin
  AStream := TMemoryStream.Create;

  try
    dsMaster := schReport.NewDataset(FConnection, ds_InformeCabecera, ['ID'], [ID]);
    dsCompensados := schReport.NewDataset(FConnection, ds_InformeCompensados, ['ID_RECIBO'], [ID], False);

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

    AStream.Clear;
    DABINAdapter.WriteDataset(AStream, dsCompensados, [woRows, woSchema], -1);
    DABINAdapter.ReadDataset(AStream, tbl_Compensados, TRUE, '', TRUE, TRUE);

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

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

end.
