unit uRptListadosContactos_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,
  FactuGES_Intf, frxExportCSV;

type
  TRptListadosContactos = 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;
    Bin2DataStreamer: TDABin2DataStreamer;
    frxDBCabecera2: TfrxDBDataset;
    DADataCabecera2: TDADataSource;
    tbl_lista_datos_contactos: TDAMemDataTable;
    schReport: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DataModuleCreate(Sender: TObject);
  private
    FConnection: IDAConnection;
  public
    function GenerarInformeListaDatosContacto(const ListaID: TIntegerArray): Binary; overload;
    function GenerarInformeEtiquetas(const ListaID: TIntegerArray): Binary; overload;
  end;

implementation

{$R *.dfm}

uses
  uSistemaFunc, uDataModuleServer, schContactosClient_Intf;

const
  rptEtiquetasContacto = 'InfEtiquetasContacto.fr3';
  rptListaDatosContacto = 'InfListaDatosContacto.fr3';

  { Dataset names for schReport }
  ds_InformeEtiquetasContactos = 'InformeEtiquetasContacto';
  ds_InformeListaDatosContacto = 'InformeListaDatosContacto';
  ds_InformeListaDatosContactoPersonal = 'InformeListaDatosContactoPersonal';


{ TRptListadosContactos }


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

function TRptListadosContactos.GenerarInformeEtiquetas(
  const ListaID: TIntegerArray): Binary;
var
  AStream: TMemoryStream;
  dsMaster: IDADataset;
  i : Integer;
  AWhereStr : String;
  AInforme: Variant;

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

  AStream := TMemoryStream.Create;
  try
    dsMaster := schReport.NewDataset(FConnection, ds_InformeEtiquetasContactos, [], [], False); //, ['ID'], ['611, 751']); //AID]);
    AWhereStr := fld_ContactosID + ' in (';
    for i := 0 to ListaID.Count - 1 do
    begin
      if i > 0 then
        AWhereStr := AWhereStr + ', ';
      AWhereStr := AWhereStr + IntToStr(ListaID.Items[i]);
    end;

    AWhereStr := AWhereStr + ')';
    dsMaster.Where.AddText(AWhereStr);

    dsMaster.Open;

    AStream.Clear;
    Bin2DataStreamer.WriteDataset(AStream, dsMaster, [woRows, woSchema], -1);
    Bin2DataStreamer.ReadDataset(AStream, tbl_Contactos, TRUE, '', TRUE, 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;

function TRptListadosContactos.GenerarInformeListaDatosContacto(const ListaID: TIntegerArray): Binary;
var
  AStream: TMemoryStream;
  dsMaster: IDADataset;

  i : Integer;
  AWhereStr : String;
  AInforme: Variant;

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

  AStream := TMemoryStream.Create;
  try
    dsMaster := schReport.NewDataset(FConnection, ds_InformeListaDatosContacto, [], [], False); //, ['ID'], ['611, 751']); //AID]);
    AWhereStr := fld_PersonalContactoID_CONTACTO + ' in (';
    for i := 0 to ListaID.Count - 1 do
    begin
      if i > 0 then
        AWhereStr := AWhereStr + ', ';
      AWhereStr := AWhereStr + IntToStr(ListaID.Items[i]);
    end;

    AWhereStr := AWhereStr + ')';
    dsMaster.Where.AddText(AWhereStr);
    dsMaster.Open;
    AStream.Clear;
    Bin2DataStreamer.WriteDataset(AStream, dsMaster, [woRows, woSchema], -1);
    Bin2DataStreamer.ReadDataset(AStream, tbl_lista_datos_contactos, TRUE, '', TRUE, TRUE);

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

    frxReport.LoadFromFile(AInforme, True);
    frxReport.PrepareReport(True);

    frxReport.PreviewPages.SaveToStream(Result);

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


end;


end.
