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;

type
  TRptComisiones = class(TDataModule)
    DADataCabecera: TDADataSource;
    tbl_Comisiones: TDACDSDataTable;
    DABINAdapter: TDABINAdapter;
    frxRichObject1: TfrxRichObject;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxOLEObject1: TfrxOLEObject;
    frxCrossObject1: TfrxCrossObject;
    frxCheckBoxObject1: TfrxCheckBoxObject;
    frxGradientObject1: TfrxGradientObject;
    frxChartObject1: TfrxChartObject;
    frxDBCabecera: TfrxDBDataset;
    schReport: TDASchema;
    DataDictionary: TDADataDictionary;
    frxReport: TfrxReport;
    procedure DataModuleCreate(Sender: TObject);
  private
    FConnection: IDAConnection;
  public
    function GenerarComision(const AComisionID : Variant; const DesglosadoProv: Boolean; FechaIni: String; FechaFin: String; AAgenteID: Variant): Binary;
  end;

implementation

{$R *.dfm}

uses
  uDataModuleServer, schReportClient_Intf;

const
  rptComision = 'InfComisiones.fr3';
  rptComisionDesglosado = 'InfComisionesDesglosado.fr3';

  { Dataset names for schReport }
  ds_InformeComisiones = 'InformeComisiones';
  ds_InformeComisionesDesglosado = 'InformeComisionesDesglosado';


{ TRptComision }

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

function TRptComisiones.GenerarComision(const AComisionID : Variant; const DesglosadoProv: Boolean; FechaIni: String; FechaFin: String; AAgenteID: Variant): Binary;
var
  dsMaster: IDADataset;
  AStream: TMemoryStream;
begin
  AStream := TMemoryStream.Create;
  Result := Binary.Create;
  FConnection.BeginTransaction;
  try
    if DesglosadoProv then
       dsMaster := schReport.NewDataset(FConnection, ds_InformeComisionesDesglosado,[], [], False)
    else
       dsMaster := schReport.NewDataset(FConnection, ds_InformeComisiones,[], [], False);

    with dsMaster.Where do
    begin
      if not VarIsNull(AComisionID) then
      begin
        AddOperator(opAND);
        OpenBraket;
        AddText('V_AGENTES_COMISIONES_DET_FAC.' + fld_InformeComisionesID_COMISION_LIQUIDADA + ' = ' + VarToStr(AComisionID));
        CloseBraket;
      end;

      if not VarIsNull(AAgenteID) then
      begin
        AddOperator(opAND);
        OpenBraket;
        AddText('V_AGENTES_COMISIONES_DET_FAC.' + fld_InformeComisionesID_AGENTE + ' = ' + VarToStr(AAgenteID));
        CloseBraket;
      end;

      if (Length(FechaIni) > 0)
      and (Length(FechaFin) > 0) then
      begin
        AddOperator(opAND);
        OpenBraket;

        AddText('COMISIONES_LIQUIDADAS.FECHA between ''' + StringReplace(FechaIni, '/', '.', [rfReplaceAll]) + ''' and ''' + StringReplace(FechaFin, '/', '.', [rfReplaceAll]) + '''');
        CloseBraket;
      end;
    end;

    dsMaster.Open;

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

    if DesglosadoProv then
       frxReport.LoadFromFile(DarRutaInformes + rptComisionDesglosado, True)
    else
       frxReport.LoadFromFile(DarRutaInformes + rptComision, True);

    frxReport.Variables['FechaIni'] := QuotedStr(FechaIni);
    frxReport.Variables['FechaFin'] := QuotedStr(FechaFin);
    frxReport.PrepareReport;
    frxReport.PreviewPages.SaveToStream(Result);
  finally
    AStream.Free;
    FConnection.RollbackTransaction;
  end;
end;

end.
