unit uRptEtiquetasContactos_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
  TRptEtiquetasContactos = 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;
    frxReport: TfrxReport;
    schReport: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DataModuleCreate(Sender: TObject);
  private
    FConnection: IDAConnection;
  public
    function GenerarEtiquetas(const AID : String): Binary;
  end;

implementation

{$R *.dfm}

uses
  uDataModuleServer, schContactosClient_Intf;

const
  rptInfEtiquetas = 'InfEtiquetasContactos.fr3';

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


{ TRptEtiquetasContactos }


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

function TRptEtiquetasContactos.GenerarEtiquetas(const AID: String): Binary;
var
  dsMaster: IDADataset;
  AStream: TMemoryStream;

begin
  AStream := TMemoryStream.Create;
  Result := Binary.Create;
  FConnection.BeginTransaction;
  try
    dsMaster := schReport.NewDataset(FConnection, ds_InformeCabecera);
    //Aņadimos el where
    dsMaster.Where.AddText('ID in (' + AID + ')');

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

    frxReport.LoadFromFile(DarRutaInformes + rptInfEtiquetas, True);
    frxReport.PrepareReport(False);
    frxReport.PreviewPages.SaveToStream(Result);
  finally
    AStream.Free;
    FConnection.RollbackTransaction;
  end;
end;

end.
