unit uRptComisiones_Server;

interface

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

type
  TRptComisiones = class(TDataModule)
    DADataCabecera: TDADataSource;
    tbl_Comisiones: TDAMemDataTable;
    frxRichObject1: TfrxRichObject;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxOLEObject1: TfrxOLEObject;
    frxCrossObject1: TfrxCrossObject;
    frxCheckBoxObject1: TfrxCheckBoxObject;
    frxGradientObject1: TfrxGradientObject;
    frxChartObject1: TfrxChartObject;
    frxDBCabecera: TfrxDBDataset;
    frxReport: TfrxReport;
    Bin2DataStreamer: TDABin2DataStreamer;
    frxDBDetalles1: TfrxDBDataset;
    DADataDetalles: TDADataSource;
    tbl_Facturas: TDAMemDataTable;
    schReport: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DataModuleCreate(Sender: TObject);
  private
    FConnection: IDAConnection;
    //Genera cada uno de los albaranes a imprimir
    procedure GenerarComision(const ID: integer); overload;
  public
    function GenerarInforme(const ListaID: TIntegerArray): Binary;
  end;

implementation

{$R *.dfm}

uses
  uSistemaFunc, uDataModuleServer;

const
  rptComision = 'InfComision.fr3';

{ TRptComision }

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

procedure TRptComisiones.GenerarComision(const ID: integer);
var
  AInforme: Variant;

begin
  FConnection.BeginTransaction;
  try
    tbl_Comisiones.Active := False;
    tbl_Facturas.Active := False;

    tbl_Comisiones.ParamByName('ID').AsInteger := ID;
    tbl_Facturas.ParamByName('ID').AsInteger := ID;

    tbl_Comisiones.Active := True;
    tbl_Facturas.Active := True;

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

    frxReport.LoadFromFile(AInforme, True);
    frxReport.ReportOptions.Name := 'Comisiones';
    frxReport.PrepareReport(False);
  finally
    FConnection.RollbackTransaction;
  end;
end;

function TRptComisiones.GenerarInforme(const ListaID: TIntegerArray): Binary;
var
  ID_Comisiones: TStringList;
  i: Integer;

begin
  Result := Binary.Create;
  try
    //Vamos generando todos y cada una de las comisiones recibidas
    for i := 0 to ListaID.Count - 1 do
        GenerarComision(ListaID.Items[i]);

    frxReport.PreviewPages.SaveToStream(Result);
  finally
  end;

end;

end.
