unit uRptEtiquetasContacto_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, uDADataStreamer, uDABin2DataStreamer, uDAMemDataTable;

type
  TRptEtiquetasContacto = class(TDataModule)
    DADataCabecera: TDADataSource;
    tbl_Contactos: TDAMemDataTable;
    frxRichObject1: TfrxRichObject;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxOLEObject1: TfrxOLEObject;
    frxCrossObject1: TfrxCrossObject;
    frxCheckBoxObject1: TfrxCheckBoxObject;
    frxGradientObject1: TfrxGradientObject;
    frxChartObject1: TfrxChartObject;
    frxDBCabecera: TfrxDBDataset;
    frxReport: TfrxReport;
    schReport: TDASchema;
    DataDictionary: TDADataDictionary;
    Bin2DataStreamer: TDABin2DataStreamer;
    procedure DataModuleCreate(Sender: TObject);
  private
    FConnection: IDAConnection;
  public
    function GenerarInforme(const AID : String): Binary; overload;
  end;

implementation

{$R *.dfm}

uses
  uSistemaFunc, uDataModuleServer, schContactosClient_Intf;

const
  rptEtiquetasContacto = 'InfEtiquetasContacto.fr3';

  { Dataset names for schReport }
  ds_InformeContactos = 'InformeEtiquetasContacto';


{ TRptEtiquetasContacto }


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

function TRptEtiquetasContacto.GenerarInforme(const AID: String): Binary;
var
  AInforme: Variant;

begin
  Result := Binary.Create;

  FConnection.BeginTransaction;

  try
//    AStream := TMemoryStream.Create;
//    dsMaster := schReport.NewDataset(FConnection, ds_InformeContactos, [], [], False); //, ['ID'], ['611, 751']); //AID]);
//    dsMaster.Where.AddText(fld_ContactosID + ' in (' + AID + ')');
//    dsMaster.Open;

//    AStream.Clear;
//    Bin2DataStreamer.WriteDataset(AStream, dsMaster, [woRows, woSchema], -1);
//    Bin2DataStreamer.ReadDataset(AStream, tbl_Contactos, TRUE, '', TRUE, TRUE);

    tbl_Contactos.Where.AddCondition(fld_ContactosID, cIn, '(' + AID + ')');
    tbl_Contactos.Active := True;

    AInforme := DarRutaFichero(DarRutaInformes, rptEtiquetasContacto);
    if VarIsNull(AInforme) then
       raise Exception.Create (('Error Servidor: GenerarInforme, no encuentra informe' + rptEtiquetasContacto));

    frxReport.LoadFromFile(AInforme, True);
    frxReport.PrepareReport(True);
    frxReport.PreviewPages.SaveToStream(Result);

  finally
//    AStream.Free;
//    dsMaster := Nil;
    FConnection.RollbackTransaction;
  end;

end;

end.
