unit uRptRemesasCliente_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, uROTypes,
  uDAInterfaces, uDADataStreamer, FactuGES_Intf, uDABin2DataStreamer;


type
  TRptRemesasCliente = class(TDataModule)
    DADataCabecera: TDADataSource;
    DADataDetalles: TDADataSource;
    tbl_Cabecera: TDAMemDataTable;
    tbl_Detalles: TDAMemDataTable;
    frxRichObject1: TfrxRichObject;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxOLEObject1: TfrxOLEObject;
    frxCrossObject1: TfrxCrossObject;
    frxCheckBoxObject1: TfrxCheckBoxObject;
    frxGradientObject1: TfrxGradientObject;
    frxChartObject1: TfrxChartObject;
    frxDBCabecera: TfrxDBDataset;
    frxDBDetalles: TfrxDBDataset;
    frxReport: TfrxReport;
    schReport: TDASchema;
    DataDictionary: TDADataDictionary;
    Bin2DataStreamer: TDABin2DataStreamer;
    procedure DataModuleCreate(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
  private
    FConnection: IDAConnection;
    procedure _GenerarRemesa(const ID : Integer); overload;
  public
    function GenerarRemesa(const ListaID : TIntegerArray): Binary;
  end;

implementation

{$R *.dfm}

uses
  uDataModuleServer, uSistemaFunc;

const
  rptInforme = 'InfRemesaCliente.fr3';

  { Dataset names for schReport }
  ds_InformeCabecera = 'Informe_Cabecera';
  ds_InformeDetalles = 'Informe_Detalles';


{ TRptRemesaCliente }


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

  frxDBCabecera.DataSource := DADataCabecera;
  frxDBCabecera.CloseDataSource := False;

  frxDBDetalles.DataSource := DADataDetalles;
  frxDBDetalles.CloseDataSource := False;
end;

function TRptRemesasCliente.GenerarRemesa(const ListaID : TIntegerArray): Binary;
var
  i: Integer;
begin
  Result := Binary.Create;
  try
    //Vamos generando todos y cada uno de los albaranes recibidos
    for i := 0 to ListaID.Count - 1 do
      _GenerarRemesa(ListaID.Items[i]);
    frxReport.PreviewPages.SaveToStream(Result);
  finally
  end;
end;

procedure TRptRemesasCliente.DataModuleDestroy(Sender: TObject);
begin
  tbl_Cabecera.Active := False;
  tbl_Detalles.Active := False;
end;

procedure TRptRemesasCliente._GenerarRemesa(const ID: Integer);
var
  AInforme: Variant;

begin
  FConnection.BeginTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
  try
    tbl_Cabecera.Active := False;
    tbl_Detalles.Active := False;

    tbl_Cabecera.ParamByName('ID').AsInteger := ID;
    tbl_Detalles.ParamByName('ID_REMESA').AsInteger := ID;

    tbl_Cabecera.Active := True;
    tbl_Detalles.Active := True;

    AInforme := DarRutaFichero(DarRutaInformes, rptInforme, tbl_Cabecera.FieldByName('ID_EMPRESA').AsString);
    if VarIsNull(AInforme) then
       raise Exception.Create (('Error Servidor: _GenerarRemesa, no encuentra informe ' + rptInforme));

    frxReport.LoadFromFile(AInforme, True);
    frxReport.PrepareReport(False);
  finally
    FConnection.RollbackTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
  end;
end;

end.
