unit uRptComisiones_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
  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;
    DADataDetalles1: TDADataSource;
    tbl_Facturas: TDAMemDataTable;
    frxDBDetalles2: TfrxDBDataset;
    DADataDetalles2: TDADataSource;
    tbl_Vendedores: 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 GenerarComision(const AComisionID : String): Binary; overload;
  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.ParamByName('ID').AsInteger := ID;
    tbl_Facturas.ParamByName('ID').AsInteger := ID;
    tbl_Vendedores.ParamByName('ID').AsInteger := ID;
    tbl_Comisiones.Active := True;
    tbl_Facturas.Active := True;
    tbl_Vendedores.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.PrepareReport(False);
  finally
    FConnection.RollbackTransaction;
  end;
end;

function TRptComisiones.GenerarComision(const AComisionID: String): Binary;
var
  ID_Comisiones: TStringList;
  i: Integer;

begin
  Result := Binary.Create;

  ID_Comisiones := TStringList.Create;
  ID_Comisiones.CommaText := AComisionID;

  //Vamos generando todos y cada uno de los albaranes recibidos
  for i := 0 to ID_Comisiones.Count - 1 do
      GenerarComision(StrToInt(ID_Comisiones.Strings[i]));

  frxReport.PreviewPages.SaveToStream(Result);
end;

end.
