unit uRptRecibosCliente_Server; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, frxClass, frxDBSet, uDAScriptingProvider, uDADataTable, uDAMemDataTable, DB, uDAClasses, frxChart, frxGradient, frxChBox, frxCross, frxOLE, frxBarcode, frxRich, uDABINAdapter, uROTypes, uDAInterfaces, uDADataStreamer, FactuGES_Intf; type TRptRecibosCliente = class(TDataModule) DADataCabecera: TDADataSource; tbl_Cabecera: TDAMemDataTable; DABINAdapter: TDABINAdapter; frxRichObject1: TfrxRichObject; frxBarCodeObject1: TfrxBarCodeObject; frxOLEObject1: TfrxOLEObject; frxCrossObject1: TfrxCrossObject; frxCheckBoxObject1: TfrxCheckBoxObject; frxGradientObject1: TfrxGradientObject; frxChartObject1: TfrxChartObject; frxDBCabecera: TfrxDBDataset; DADataCompensados: TDADataSource; frxDBCompensados: TfrxDBDataset; tbl_Compensados: TDAMemDataTable; frxReport: TfrxReport; tbl_InformeListadoRecibos: TDAMemDataTable; DADSInformeListadoRecibos: TDADataSource; frxDBInformeListadoRecibos: TfrxDBDataset; schReport: TDASchema; DataDictionary: TDADataDictionary; procedure DataModuleCreate(Sender: TObject); private FConnection: IDAConnection; procedure _GenerarRecibo(const ID : Integer); function _GenerarInforme(const TipoInforme: String; const IdEmpresa: Integer; const FechaInicio: DateTime; const FechaFin: DateTime; const ListaIDClientes: TIntegerArray; const ImporteMinimo: Currency): Binary; public function GenerarRecibo(const ListaID : TIntegerArray): Binary; function GenerarInformeListadoRecibos(const IdEmpresa: Integer; const FechaInicio: DateTime; const FechaFin: DateTime; const ListaIDClientes: TIntegerArray; const Desglosado: Boolean; const ImporteMinimo: Currency): Binary; function GenerarInformeListadoRecibosPendientes(const IdEmpresa: Integer; const FechaInicio: DateTime; const FechaFin: DateTime; const ListaIDClientes: TIntegerArray; const Desglosado: Boolean; const ImporteMinimo: Currency): Binary; end; implementation {$R *.dfm} uses uDataModuleServer, schRecibosClienteClient_Intf; const rptInforme = 'InfReciboCliente.fr3'; rptInformeListadoRecibosClienteDesglosado = 'InformeListadoRecibosClienteDesglosado.fr3'; rptInformeListadoRecibosCliente = 'InformeListadoRecibosCliente.fr3'; rptInformeListadoRecibosCliPendientes = 'InformeListadoRecibosCliPendientes.fr3'; rptInformeListadoRecibosCliPendientesDesglosado = 'InformeListadoRecibosCliPendientesDesglosado.fr3'; { Dataset names for schReport } ds_InformeCabecera = 'Informe_Cabecera'; ds_InformeCompensados = 'Informe_Compensados'; { TRptReciboCliente } procedure TRptRecibosCliente.DataModuleCreate(Sender: TObject); begin schReport.ConnectionManager := dmServer.ConnectionManager; FConnection := dmServer.DarNuevaConexion; frxReport.EngineOptions.NewSilentMode := simReThrow; frxDBCabecera.DataSource := DADataCabecera; frxDBCompensados.DataSource := DADataCompensados; end; function TRptRecibosCliente.GenerarRecibo(const ListaID: TIntegerArray): Binary; var i: Integer; begin Result := Binary.Create; try //Vamos generando todos y cada uno de los albaranes recibidos for i := 0 to ListaID.Count - 1 do _GenerarRecibo(ListaID.Items[i]); frxReport.PreviewPages.SaveToStream(Result); finally end; end; function TRptRecibosCliente.GenerarInformeListadoRecibos( const IdEmpresa: Integer; const FechaInicio, FechaFin: DateTime; const ListaIDClientes: TIntegerArray; const Desglosado: Boolean; const ImporteMinimo: Currency): Binary; var ATipoInforme: String; begin //DESGLOSADO POR CLIENTE EN ESTE INFORME NO SE DESGLOSARÁ POR CLIENTE if Desglosado then ATipoInforme := rptInformeListadoRecibosClienteDesglosado else ATipoInforme := rptInformeListadoRecibosCliente; Result := _GenerarInforme(ATipoInforme, IdEmpresa, FechaInicio, FechaFin, ListaIDClientes, ImporteMinimo); end; function TRptRecibosCliente.GenerarInformeListadoRecibosPendientes( const IdEmpresa: Integer; const FechaInicio, FechaFin: DateTime; const ListaIDClientes: TIntegerArray; const Desglosado: Boolean; const ImporteMinimo: Currency): Binary; var Condicion: TDAWhereExpression; ATipoInforme: String; begin if tbl_InformeListadoRecibos.Active then tbl_InformeListadoRecibos.Active := False; // Filtrar el informe por situacion with tbl_InformeListadoRecibos.DynamicWhere do begin // (ID_EMPRESA >= ID) Condicion := NewBinaryExpression(NewField('', fld_RecibosClienteSITUACION), NewConstant('COBRADO', datString), dboNotEqual); if IsEmpty then Expression := Condicion else Expression := NewBinaryExpression(Expression, Condicion, dboAnd); end; //DESGLOSADO POR CLIENTE EN ESTE INFORME NO SE DESGLOSARÁ POR CLIENTE if Desglosado then ATipoInforme := rptInformeListadoRecibosCliPendientesDesglosado else ATipoInforme := rptInformeListadoRecibosCliPendientes; Result := _GenerarInforme(ATipoInforme, IdEmpresa, FechaInicio, FechaFin, ListaIDClientes, ImporteMinimo); end; function TRptRecibosCliente._GenerarInforme(const TipoInforme: String; const IdEmpresa: Integer; const FechaInicio, FechaFin: DateTime; const ListaIDClientes: TIntegerArray; const ImporteMinimo: Currency): Binary; var Condicion: TDAWhereExpression; i: Integer; begin Result := Binary.Create; FConnection.BeginTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO" try if tbl_InformeListadoRecibos.Active then tbl_InformeListadoRecibos.Active := False; // Filtrar el informe por empresa with tbl_InformeListadoRecibos.DynamicWhere do begin // (ID_EMPRESA >= ID) Condicion := NewBinaryExpression(NewField('', fld_RecibosClienteID_EMPRESA), NewConstant(IdEmpresa, datInteger), dboEqual); if IsEmpty then Expression := Condicion else Expression := NewBinaryExpression(Expression, Condicion, dboAnd); end; // Filtrar el informe por fechas if not VarIsNull(FechaInicio) and not VarIsNull(FechaFin) then begin with tbl_InformeListadoRecibos.DynamicWhere do begin // (FECHA_INICIO between FECHA_FIN) Condicion := NewBinaryExpression(NewField('', fld_RecibosClienteFECHA_VENCIMIENTO), NewConstant(FechaInicio, datDateTime), dboGreaterOrEqual); Condicion := NewBinaryExpression(NewBinaryExpression(NewField('', fld_RecibosClienteFECHA_VENCIMIENTO), NewConstant(FechaFin, datDateTime), dboLessOrEqual), Condicion, dboAnd); if IsEmpty then Expression := Condicion else Expression := NewBinaryExpression(Expression, Condicion, dboAnd); end; end; // Filtrar el informe por cliente if Assigned(ListaIDClientes) then begin with tbl_InformeListadoRecibos.DynamicWhere do begin for i := 0 to ListaIDClientes.Count - 1 do begin // (ID_CLIENTE = ID) Condicion := NewBinaryExpression(NewField('', fld_RecibosClienteID_CLIENTE), NewConstant(ListaIDClientes.Items[i], datInteger), dboEqual); if IsEmpty then Expression := Condicion else Expression := NewBinaryExpression(Expression, Condicion, dboAnd); end; end; end; // Filtrar el informe por importe minimo if (ImporteMinimo > 0) then begin with tbl_InformeListadoRecibos.DynamicWhere do begin // (IMPORTE_TOTAL > ImporteMinimo) Condicion := NewBinaryExpression(NewField('', fld_RecibosClienteIMPORTE_TOTAL), NewConstant(ImporteMinimo, datCurrency), dboGreaterOrEqual); if IsEmpty then Expression := Condicion else Expression := NewBinaryExpression(Expression, Condicion, dboAnd); end; end; tbl_InformeListadoRecibos.Active := True; frxReport.LoadFromFile(DarRutaInformes + IntToStr(IdEmpresa) + '\' + TipoInforme, True); frxReport.Variables.Variables['FechaInicio'] := FechaInicio; frxReport.Variables.Variables['FechaFin'] := FechaFin; frxReport.PrepareReport(False); frxReport.PreviewPages.SaveToStream(Result); finally FConnection.RollbackTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO" end; end; procedure TRptRecibosCliente._GenerarRecibo(const ID: Integer); begin FConnection.BeginTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO" try tbl_Cabecera.Active := False; tbl_Compensados.Active := False; tbl_Cabecera.ParamByName('ID').AsInteger := ID; tbl_Compensados.ParamByName('ID_RECIBO').AsInteger := ID; tbl_Cabecera.Active := True; tbl_Compensados.Active := True; frxReport.LoadFromFile(DarRutaInformes + rptInforme, True); frxReport.PrepareReport(False); finally FConnection.RollbackTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO" end; end; end.