946 lines
34 KiB
ObjectPascal
946 lines
34 KiB
ObjectPascal
unit uRptFacturasCliente_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, IBCustomDataSet, IBDatabase, IBQuery, frxIBXComponents,
|
||
frxExportPDF;
|
||
|
||
type
|
||
TRptFacturasCliente = class(TDataModule)
|
||
DADataCabecera: TDADataSource;
|
||
DADataDetalles: TDADataSource;
|
||
tbl_Cabecera: TDAMemDataTable;
|
||
tbl_Detalles: TDAMemDataTable;
|
||
frxRichObject1: TfrxRichObject;
|
||
frxBarCodeObject1: TfrxBarCodeObject;
|
||
frxOLEObject1: TfrxOLEObject;
|
||
frxCrossObject1: TfrxCrossObject;
|
||
frxCheckBoxObject1: TfrxCheckBoxObject;
|
||
frxGradientObject1: TfrxGradientObject;
|
||
frxDBCabecera: TfrxDBDataset;
|
||
frxDBDetalles: TfrxDBDataset;
|
||
DADataVencimientos: TDADataSource;
|
||
tbl_Vencimientos: TDAMemDataTable;
|
||
frxDBDataset1: TfrxDBDataset;
|
||
DataDictionary: TDADataDictionary;
|
||
frxReport: TfrxReport;
|
||
Bin2DataStreamer: TDABin2DataStreamer;
|
||
tbl_InformeListadoFacturas: TDAMemDataTable;
|
||
DADSInformeListadoFacturas: TDADataSource;
|
||
frxDBInformeListadoFacturas: TfrxDBDataset;
|
||
tbl_InformeListadoFacturasResumen: TDAMemDataTable;
|
||
DADSInformeListadoFacturasResumen: TDADataSource;
|
||
frxDBInformeListadoFacturasResumen: TfrxDBDataset;
|
||
frxDBInformeListadoRecibos: TfrxDBDataset;
|
||
DADSInformeListadoRecibos: TDADataSource;
|
||
tbl_InformeListadoRecibos: TDAMemDataTable;
|
||
frxDBInformeListadoFacturasTipoResumen: TfrxDBDataset;
|
||
DADSInformeListadoFacturasTipoResumen: TDADataSource;
|
||
tbl_InformeListadoFacturasTipoResumen: TDAMemDataTable;
|
||
frxDBInforme347FacturasCliente: TfrxDBDataset;
|
||
DADSInforme347FacturasCliente: TDADataSource;
|
||
tbl_Informe347FacturasCliente: TDAMemDataTable;
|
||
frxCrossObject2: TfrxCrossObject;
|
||
IBDatabase1: TIBDatabase;
|
||
IBQuery1: TIBQuery;
|
||
IBQuery1ANO: TSmallintField;
|
||
IBQuery1TRIMESTRE: TSmallintField;
|
||
IBQuery1ID_CLIENTE: TIntegerField;
|
||
IBQuery1NOMBRE: TIBStringField;
|
||
IBQuery1NIF_CIF: TIBStringField;
|
||
IBQuery1BASE_IMPONIBLE_ANO: TIBBCDField;
|
||
IBQuery1BASE_IMPONIBLE_PERIODO: TIBBCDField;
|
||
IBTransaction1: TIBTransaction;
|
||
frxDBInforme347FacturasProveedor: TfrxDBDataset;
|
||
DADSInforme347FacturasProveedor: TDADataSource;
|
||
tbl_Informe347FacturasProveedor: TDAMemDataTable;
|
||
frxPDFExport1: TfrxPDFExport;
|
||
schReport: TDASchema;
|
||
procedure DataModuleCreate(Sender: TObject);
|
||
procedure DataModuleDestroy(Sender: TObject);
|
||
private
|
||
FConnection: IDAConnection;
|
||
FIdEmpresa: Integer;
|
||
FFechaInicio: Variant;
|
||
FFechaFin: Variant;
|
||
FAno: Variant;
|
||
FFechaVenInicio: Variant;
|
||
FFechaVenFin: Variant;
|
||
FListaIDClientes: TIntegerArray;
|
||
FListaNombresClientes : TStringList;
|
||
FImporteMinimo: Currency;
|
||
FImporteFiltro: Currency;
|
||
FDesglosado : Boolean;
|
||
FVerLogotipo: Boolean;
|
||
|
||
//Genera cada una de las facturas a imprimir
|
||
procedure _GenerarFactura(const ID: integer); overload;
|
||
|
||
procedure PrepararTablaInforme(ATabla: TDAMemDataTable);
|
||
procedure PrepararTablaResumenInforme(ATabla: IDADataset);
|
||
function _GenerarInforme(const TipoInforme: String): Binary;
|
||
procedure IniciarParametrosInforme;
|
||
procedure RecuperarNombresClientes;
|
||
|
||
public
|
||
function GenerarFactura(const ListaID : TIntegerArray; const VerLogotipo: Boolean = True): Binary; overload;
|
||
function GenerarFacturaEnPDF(const ListaID : TIntegerArray; const Usuario: AnsiString; const Firmado: Boolean): Binary;
|
||
function GenerarInformeIVA(const IdEmpresa: Integer; const FechaInicio: Variant; const FechaFin: Variant; const ListaIDClientes: TIntegerArray; const Desglosado: Boolean; const ImporteMinimo: Currency): Binary;
|
||
function GenerarInformeListadoFacturas(const IdEmpresa: Integer; const FechaInicio: Variant; const FechaFin: Variant; const ListaIDClientes: TIntegerArray; const Desglosado: Boolean; const ImporteMinimo: Currency): Binary;
|
||
function GenerarInformeListadoFacturasPendientes(const IdEmpresa: Integer; const FechaInicio: Variant; const FechaFin: Variant; const ListaIDClientes: TIntegerArray; const Desglosado: Boolean; const ImporteMinimo: Currency): Binary;
|
||
function GenerarInformeListadoFacturasRecibos(const IdEmpresa: Integer; const ListaIDClientes: TIntegerArray; const Desglosado: Boolean; const FechaInicio, FechaFin: Variant): Binary;
|
||
function GenerarInformeModelo347(const IdEmpresa: Integer; const Desglosado: Boolean; const Ano: Variant; const ImporteFiltro: Currency): Binary;
|
||
end;
|
||
|
||
implementation
|
||
|
||
{$R *.dfm}
|
||
|
||
uses
|
||
uRORemoteDataModule, uSistemaFunc, StrUtils, uROServer, DataAbstract4_Intf, uBizFacturasClienteServer,
|
||
uDataModuleServer, schFacturasClienteClient_Intf, schRecibosClienteClient_Intf, uBizIdiomasServer;
|
||
|
||
const
|
||
rptFacturaCliente = 'InfFacturaCliente.fr3';
|
||
rptFacturaProformaCliente = 'InfFacturaCliente_proforma.fr3';
|
||
rptInformeIVA = 'InformeIVAClientes.fr3';
|
||
rptInformeIVADesglosado = 'InformeIVAClientesDesglosado.fr3';
|
||
rptInformeListadoFacturasCliente = 'InformeListadoFacturasCliente.fr3';
|
||
rptInformeListadoFacturasClienteDesglosado = 'InformeListadoFacturasClienteDesglosado.fr3';
|
||
rptInformeListadoFactuasClientePendiente = 'InformeListadoFacturasClientePendientes.fr3';
|
||
rptInformeListadoFactuasClientePendienteDesglosado = 'InformeListadoFacturasClientePendientesDesglosado.fr3';
|
||
rptInformeListadoFacturasRecibosCliente = 'InformeListadoFacturasRecibosCliente.fr3';
|
||
rptInformeListadoFacturasRecibosClienteDesglosado = 'InformeListadoFacturasRecibosClienteDesglosado.fr3';
|
||
rptInformeModelo347 = 'InformeModelo347.fr3';
|
||
|
||
ds_InformeListadoFacturasResumen = 'ListadoFacturasResumen';
|
||
ds_InformeListadoFacturasTipoResumen = 'ListadoFacturasClienteTipoResumen';
|
||
|
||
{ TRptFacturasCliente }
|
||
|
||
procedure TRptFacturasCliente.DataModuleCreate(Sender: TObject);
|
||
begin
|
||
schReport.ConnectionManager := dmServer.ConnectionManager;
|
||
FConnection := dmServer.DarNuevaConexion;
|
||
frxReport.EngineOptions.NewSilentMode := simReThrow;
|
||
|
||
FListaNombresClientes := TStringList.Create;
|
||
|
||
//Inicializaciones
|
||
FAno := Null;
|
||
FFechaInicio := Null;
|
||
FFechaFin := Null;
|
||
FFechaVenInicio := Null;
|
||
FFechaVenFin := Null;
|
||
FListaIDClientes := Nil;
|
||
FImporteMinimo := 0;
|
||
FImporteFiltro := 0;
|
||
FDesglosado := False;
|
||
end;
|
||
|
||
procedure TRptFacturasCliente.DataModuleDestroy(Sender: TObject);
|
||
begin
|
||
FreeANDNIL(FListaNombresClientes);
|
||
end;
|
||
|
||
function TRptFacturasCliente.GenerarFactura(const ListaID: TIntegerArray; const VerLogotipo: Boolean = True): Binary;
|
||
var
|
||
i: Integer;
|
||
begin
|
||
Result := Binary.Create;
|
||
try
|
||
//Inicializamos parametros
|
||
FVerLogotipo := VerLogotipo;
|
||
|
||
//Vamos generando todos y cada una de las facturas recibidas
|
||
for i := 0 to ListaID.Count - 1 do
|
||
_GenerarFactura(ListaID.Items[i]);
|
||
|
||
frxReport.PreviewPages.SaveToStream(Result);
|
||
finally
|
||
end;
|
||
end;
|
||
|
||
function TRptFacturasCliente.GenerarFacturaEnPDF(const ListaID: TIntegerArray;
|
||
const Usuario: AnsiString; const Firmado: Boolean): Binary;
|
||
var
|
||
i: Integer;
|
||
AStream : Binary;
|
||
ARutaFichero, ANombreFichero, LlamadaBat : String;
|
||
begin
|
||
Result := Binary.Create;
|
||
try
|
||
//Inicializamos parametros
|
||
FVerLogotipo := True;
|
||
|
||
//Vamos generando todos y cada uno de los presupuestos recibidos
|
||
for i := 0 to ListaID.Count - 1 do
|
||
begin
|
||
_GenerarFactura(ListaID.Items[i]);
|
||
ANombreFichero := frxReport.ReportOptions.Name + '_' + FormatDateTime('yyyymmdd_hhnnss_', now) + Usuario;
|
||
if Firmado then
|
||
ANombreFichero := ANombreFichero + 'firmado.pdf'
|
||
else
|
||
ANombreFichero := ANombreFichero + '.pdf';
|
||
|
||
ANombreFichero := DarRutaPDFS + EscapeIllegalChars(ANombreFichero);
|
||
frxPDFExport1.Stream := Result;
|
||
frxReport.Export(frxPDFExport1);
|
||
Result.SaveToFile(ANombreFichero);
|
||
|
||
if Firmado then
|
||
begin
|
||
|
||
ARutaFichero := DarRutaFichero(DarRutaInformes, 'firma.bat ', IntToStr(FIdEmpresa));
|
||
LlamadaBat := ARutaFichero + '"' + ANombreFichero + '"';
|
||
if ExecAndWait(LlamadaBat) then
|
||
Result.LoadFromFile(ANombreFichero);
|
||
end;
|
||
end;
|
||
finally
|
||
end;
|
||
end;
|
||
|
||
function TRptFacturasCliente.GenerarInformeIVA(const IdEmpresa: Integer; const FechaInicio,
|
||
FechaFin: Variant; const ListaIDClientes: TIntegerArray; const Desglosado: Boolean;
|
||
const ImporteMinimo: Currency): Binary;
|
||
var
|
||
ATipoInforme: String;
|
||
AStream: TMemoryStream;
|
||
dsMaster: IDADataset;
|
||
|
||
begin
|
||
FConnection.BeginTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
|
||
|
||
AStream := TMemoryStream.Create;
|
||
try
|
||
//Inicializamos parametros
|
||
FIdEmpresa := IdEmpresa;
|
||
FFechaInicio := FechaInicio;
|
||
FFechaFin := FechaFin;
|
||
FImporteMinimo := ImporteMinimo;
|
||
|
||
if Assigned(FListaIDClientes) then
|
||
FListaIDClientes.Free;
|
||
FListaIDClientes := ListaIDClientes;
|
||
|
||
//Se van a prepara las tablas del informe
|
||
if tbl_InformeListadoFacturas.Active then
|
||
tbl_InformeListadoFacturas.Active := False;
|
||
|
||
PrepararTablaInforme(tbl_InformeListadoFacturas);
|
||
|
||
//Se prepara la tabla del listado resumen del informe
|
||
if tbl_InformeListadoFacturasResumen.Active then
|
||
tbl_InformeListadoFacturasResumen.Active := False;
|
||
dsMaster := schReport.NewDataset(FConnection, ds_InformeListadoFacturasResumen, [], [], False);
|
||
PrepararTablaResumenInforme(dsMaster);
|
||
//Esto se hace para rellenar la tabla del datamodule que usa el informe.
|
||
dsMaster.Open;
|
||
AStream.Clear;
|
||
Bin2DataStreamer.WriteDataset(AStream, dsMaster, [woRows, woSchema], -1);
|
||
Bin2DataStreamer.ReadDataset(AStream, tbl_InformeListadoFacturasResumen, TRUE, '', TRUE, TRUE);
|
||
|
||
if tbl_InformeListadoFacturasTipoResumen.Active then
|
||
tbl_InformeListadoFacturasTipoResumen.Active := False;
|
||
dsMaster := schReport.NewDataset(FConnection, ds_InformeListadoFacturasTipoResumen, [], [], False);
|
||
PrepararTablaResumenInforme(dsMaster);
|
||
//Esto se hace para rellenar la tabla del datamodule que usa el informe.
|
||
dsMaster.Open;
|
||
AStream.Clear;
|
||
Bin2DataStreamer.WriteDataset(AStream, dsMaster, [woRows, woSchema], -1);
|
||
Bin2DataStreamer.ReadDataset(AStream, tbl_InformeListadoFacturasTipoResumen, TRUE, '', TRUE, TRUE);
|
||
|
||
//DESGLOSADO POR CLIENTE EN ESTE INFORME NO SE DESGLOSAR<41> POR CLIENTE
|
||
if Desglosado then
|
||
ATipoInforme := rptInformeIVADesglosado
|
||
else
|
||
ATipoInforme := rptInformeIVA;
|
||
|
||
//Finalmente se abren las tablas del informe
|
||
tbl_InformeListadoFacturas.Active := True;
|
||
tbl_InformeListadoFacturasResumen.Active := True;
|
||
tbl_InformeListadoFacturasTipoResumen.Active := True;
|
||
|
||
Result := _GenerarInforme(ATipoInforme);
|
||
finally
|
||
AStream.Free;
|
||
dsMaster := Nil;
|
||
FConnection.RollbackTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
|
||
end;
|
||
end;
|
||
|
||
function TRptFacturasCliente.GenerarInformeListadoFacturas(const IdEmpresa: Integer;
|
||
const FechaInicio, FechaFin: Variant; const ListaIDClientes: TIntegerArray;
|
||
const Desglosado: Boolean; const ImporteMinimo: Currency): Binary;
|
||
var
|
||
ATipoInforme: String;
|
||
AStream: TMemoryStream;
|
||
dsMaster: IDADataset;
|
||
|
||
begin
|
||
FConnection.BeginTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
|
||
|
||
AStream := TMemoryStream.Create;
|
||
try
|
||
//Inicializamos parametros
|
||
FIdEmpresa := IdEmpresa;
|
||
FFechaInicio := FechaInicio;
|
||
FFechaFin := FechaFin;
|
||
FImporteMinimo := ImporteMinimo;
|
||
|
||
if Assigned(FListaIDClientes) then
|
||
FListaIDClientes.Free;
|
||
FListaIDClientes := ListaIDClientes;
|
||
|
||
//Se prepara la tabla del listado general del informe
|
||
if tbl_InformeListadoFacturas.Active then
|
||
tbl_InformeListadoFacturas.Active := False;
|
||
PrepararTablaInforme(tbl_InformeListadoFacturas);
|
||
|
||
//Se prepara la tabla del listado resumen del informe
|
||
if tbl_InformeListadoFacturasResumen.Active then
|
||
tbl_InformeListadoFacturasResumen.Active := False;
|
||
dsMaster := schReport.NewDataset(FConnection, ds_InformeListadoFacturasResumen, [], [], False);
|
||
PrepararTablaResumenInforme(dsMaster);
|
||
//Esto se hace para rellenar la tabla del datamodule que usa el informe.
|
||
dsMaster.Open;
|
||
AStream.Clear;
|
||
Bin2DataStreamer.WriteDataset(AStream, dsMaster, [woRows, woSchema], -1);
|
||
Bin2DataStreamer.ReadDataset(AStream, tbl_InformeListadoFacturasResumen, TRUE, '', TRUE, TRUE);
|
||
|
||
//DESGLOSADO POR CLIENTE EN ESTE INFORME NO SE DESGLOSAR<41> POR CLIENTE
|
||
FDesglosado := Desglosado;
|
||
if FDesglosado then
|
||
ATipoInforme := rptInformeListadoFacturasClienteDesglosado
|
||
else
|
||
ATipoInforme := rptInformeListadoFacturasCliente;
|
||
|
||
//Finalmente se abren las tablas del informe
|
||
tbl_InformeListadoFacturas.Active := True;
|
||
tbl_InformeListadoFacturasResumen.Active := True;
|
||
|
||
Result := _GenerarInforme(ATipoInforme);
|
||
|
||
finally
|
||
AStream.Free;
|
||
dsMaster := Nil;
|
||
FConnection.RollbackTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
|
||
end;
|
||
end;
|
||
|
||
function TRptFacturasCliente.GenerarInformeListadoFacturasPendientes(
|
||
const IdEmpresa: Integer; const FechaInicio, FechaFin: Variant;
|
||
const ListaIDClientes: TIntegerArray; const Desglosado: Boolean;
|
||
const ImporteMinimo: Currency): Binary;
|
||
var
|
||
Condicion: TDAWhereExpression;
|
||
ATipoInforme: String;
|
||
AStream: TMemoryStream;
|
||
dsMaster: IDADataset;
|
||
|
||
begin
|
||
FConnection.BeginTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
|
||
|
||
AStream := TMemoryStream.Create;
|
||
try
|
||
//Inicializamos parametros
|
||
FIdEmpresa := IdEmpresa;
|
||
FFechaInicio := FechaInicio;
|
||
FFechaFin := FechaFin;
|
||
FImporteMinimo := ImporteMinimo;
|
||
FDesglosado := Desglosado;
|
||
|
||
if Assigned(FListaIDClientes) then
|
||
FListaIDClientes.Free;
|
||
FListaIDClientes := ListaIDClientes;
|
||
|
||
//Se prepara la tabla del listado general del informe
|
||
if tbl_InformeListadoFacturas.Active then
|
||
tbl_InformeListadoFacturas.Active := False;
|
||
PrepararTablaInforme(tbl_InformeListadoFacturas);
|
||
|
||
// Filtrar el informe por situacion
|
||
with tbl_InformeListadoFacturas.DynamicWhere do
|
||
begin
|
||
// (ID_EMPRESA >= ID)
|
||
Condicion := NewBinaryExpression(NewField('', fld_FacturasClienteSITUACION), NewConstant('PAGADA', datString), dboNotEqual);
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
||
end;
|
||
|
||
//Se prepara la tabla del listado resumen del informe
|
||
if tbl_InformeListadoFacturasResumen.Active then
|
||
tbl_InformeListadoFacturasResumen.Active := False;
|
||
|
||
dsMaster := schReport.NewDataset(FConnection, ds_InformeListadoFacturasResumen, [], [], False);
|
||
PrepararTablaResumenInforme(dsMaster);
|
||
|
||
// Filtrar el informe por situacion
|
||
dsMaster.Where.AddText(' AND (SITUACION <> ''PAGADA'')');
|
||
|
||
//Esto se hace para rellenar la tabla del datamodule que usa el informe.
|
||
dsMaster.Open;
|
||
AStream.Clear;
|
||
Bin2DataStreamer.WriteDataset(AStream, dsMaster, [woRows, woSchema], -1);
|
||
Bin2DataStreamer.ReadDataset(AStream, tbl_InformeListadoFacturasResumen, TRUE, '', TRUE, TRUE);
|
||
|
||
|
||
//DESGLOSADO POR CLIENTE EN ESTE INFORME NO SE DESGLOSAR<41> POR CLIENTE
|
||
if FDesglosado then
|
||
ATipoInforme := rptInformeListadoFactuasClientePendienteDesglosado
|
||
else
|
||
ATipoInforme := rptInformeListadoFactuasClientePendiente;
|
||
|
||
//Finalmente se abren las tablas del informe
|
||
tbl_InformeListadoFacturas.Active := True;
|
||
tbl_InformeListadoFacturasResumen.Active := True;
|
||
|
||
Result := _GenerarInforme(ATipoInforme);
|
||
|
||
finally
|
||
AStream.Free;
|
||
dsMaster := Nil;
|
||
FConnection.RollbackTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
|
||
end;
|
||
end;
|
||
|
||
function TRptFacturasCliente.GenerarInformeListadoFacturasRecibos(
|
||
const IdEmpresa: Integer; const ListaIDClientes: TIntegerArray;
|
||
const Desglosado: Boolean; const FechaInicio, FechaFin: Variant): Binary;
|
||
var
|
||
ATipoInforme: String;
|
||
AStream: TMemoryStream;
|
||
dsMaster: IDADataset;
|
||
Condicion: TDAWhereExpression;
|
||
|
||
begin
|
||
FConnection.BeginTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
|
||
|
||
AStream := TMemoryStream.Create;
|
||
try
|
||
//Inicializamos parametros
|
||
FIdEmpresa := IdEmpresa;
|
||
|
||
if Assigned(FListaIDClientes) then
|
||
FListaIDClientes.Free;
|
||
FListaIDClientes := ListaIDClientes;
|
||
|
||
//Se prepara la tabla del listado general del informe
|
||
if tbl_InformeListadoFacturas.Active then
|
||
tbl_InformeListadoFacturas.Active := False;
|
||
PrepararTablaInforme(tbl_InformeListadoFacturas);
|
||
|
||
// Filtrar el informe por fecha
|
||
with tbl_InformeListadoFacturas.DynamicWhere do
|
||
begin
|
||
// (FECHA_INICIO between FECHA_FIN)
|
||
Condicion := NewBinaryExpression(NewField('', fld_FacturasClienteFECHA_FACTURA), NewConstant(FechaInicio, datString), dboGreaterOrEqual);
|
||
Condicion := NewBinaryExpression(NewBinaryExpression(NewField('', fld_FacturasClienteFECHA_FACTURA), NewConstant(FechaFin, datString), dboLessOrEqual), Condicion, dboAnd);
|
||
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Condicion, Expression, dboAnd);
|
||
end;
|
||
|
||
//Se prepara la tabla del listado general del informe
|
||
if tbl_InformeListadoRecibos.Active then
|
||
tbl_InformeListadoRecibos.Active := False;
|
||
PrepararTablaInforme(tbl_InformeListadoRecibos);
|
||
with tbl_InformeListadoRecibos.DynamicWhere do
|
||
begin
|
||
// (FECHA_INICIO between FECHA_FIN)
|
||
Condicion := NewBinaryExpression(NewField('', fld_RecibosClienteFECHA_EMISION), NewConstant(FechaInicio, datDateTime), dboGreaterOrEqual);
|
||
Condicion := NewBinaryExpression(NewBinaryExpression(NewField('', fld_RecibosClienteFECHA_EMISION), NewConstant(FechaFin, datDateTime), dboLessOrEqual), Condicion, dboAnd);
|
||
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Condicion, Expression, dboAnd);
|
||
end;
|
||
|
||
|
||
//DESGLOSADO POR CLIENTE EN ESTE INFORME NO SE DESGLOSAR<41> POR CLIENTE
|
||
FDesglosado := Desglosado;
|
||
if FDesglosado then
|
||
ATipoInforme := rptInformeListadoFacturasRecibosClienteDesglosado
|
||
else
|
||
ATipoInforme := rptInformeListadoFacturasRecibosCliente;
|
||
|
||
//Finalmente se abren las tablas del informe
|
||
tbl_InformeListadoFacturas.Active := True;
|
||
tbl_InformeListadoRecibos.Active := True;
|
||
|
||
Result := _GenerarInforme(ATipoInforme);
|
||
|
||
finally
|
||
AStream.Free;
|
||
dsMaster := Nil;
|
||
FConnection.RollbackTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
|
||
end;
|
||
end;
|
||
|
||
function TRptFacturasCliente.GenerarInformeModelo347(const IdEmpresa: Integer;
|
||
const Desglosado: Boolean; const Ano: Variant; const ImporteFiltro: Currency): Binary;
|
||
var
|
||
ATipoInforme: String;
|
||
AStream: TMemoryStream;
|
||
dsMaster: IDADataset;
|
||
Condicion: TDAWhereExpression;
|
||
|
||
begin
|
||
FConnection.BeginTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
|
||
|
||
AStream := TMemoryStream.Create;
|
||
try
|
||
//Inicializamos parametros
|
||
FIdEmpresa := IdEmpresa;
|
||
FAno := Ano;
|
||
FImporteFiltro := ImporteFiltro;
|
||
FDesglosado := Desglosado;
|
||
{
|
||
if Assigned(FListaIDClientes) then
|
||
FListaIDClientes.Free;
|
||
FListaIDClientes := ListaIDClientes;
|
||
}
|
||
//Se prepara la tabla 347 de facturas de cliente
|
||
if tbl_Informe347FacturasCliente.Active then
|
||
tbl_Informe347FacturasCliente.Active := False;
|
||
PrepararTablaInforme(tbl_Informe347FacturasCliente);
|
||
|
||
//Se prepara la tabla 347 de facturas de proveedor
|
||
if tbl_Informe347FacturasProveedor.Active then
|
||
tbl_Informe347FacturasProveedor.Active := False;
|
||
PrepararTablaInforme(tbl_Informe347FacturasProveedor);
|
||
|
||
|
||
//Se prepara la tabla del listado general del informe
|
||
{ if tbl_Informe347FacturasCliente.Active then
|
||
tbl_InformeListadoRecibos.Active := False;
|
||
PrepararTablaInforme(tbl_InformeListadoRecibos);
|
||
with tbl_InformeListadoRecibos.DynamicWhere do
|
||
begin
|
||
// (FECHA_INICIO between FECHA_FIN)
|
||
Condicion := NewBinaryExpression(NewField('', fld_RecibosClienteFECHA_EMISION), NewConstant(FechaInicio, datDateTime), dboGreaterOrEqual);
|
||
Condicion := NewBinaryExpression(NewBinaryExpression(NewField('', fld_RecibosClienteFECHA_EMISION), NewConstant(FechaFin, datDateTime), dboLessOrEqual), Condicion, dboAnd);
|
||
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Condicion, Expression, dboAnd);
|
||
end;
|
||
}
|
||
|
||
//DESGLOSADO POR CLIENTE EN ESTE INFORME NO SE DESGLOSAR<41> POR CLIENTE
|
||
FDesglosado := Desglosado;
|
||
if FDesglosado then
|
||
ATipoInforme := rptInformeModelo347
|
||
else
|
||
ATipoInforme := rptInformeModelo347;
|
||
|
||
//Finalmente se abren las tablas del informe
|
||
tbl_Informe347FacturasCliente.Active := True;
|
||
tbl_Informe347FacturasProveedor.Active := True;
|
||
|
||
Result := _GenerarInforme(ATipoInforme);
|
||
|
||
finally
|
||
AStream.Free;
|
||
dsMaster := Nil;
|
||
FConnection.RollbackTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
|
||
end;
|
||
end;
|
||
|
||
procedure TRptFacturasCliente.IniciarParametrosInforme;
|
||
var
|
||
ATextos : TStringList;
|
||
ACadena : String;
|
||
AEmpresasService : IsrvEmpresas;
|
||
Intf : IInterface;
|
||
AClientID: TGUID;
|
||
|
||
|
||
begin
|
||
ATextos := TStringList.Create;
|
||
|
||
try
|
||
CreateGUID(AClientID);
|
||
GetClassFactory('srvEmpresas').CreateInstance(AClientID, Intf);
|
||
AEmpresasService := Intf as IsrvEmpresas;
|
||
ATextos.Add(AEmpresasService.DarNombre(FIdEmpresa));
|
||
|
||
//FANO
|
||
if (not VarIsNull(FAno)) then
|
||
ACadena := Format('Ejercicio %s', [VarToStr(FAno)]);
|
||
|
||
//PERIODO
|
||
if (not VarIsNull(FFechaInicio)) and (not VarIsNull(FFechaFin)) then
|
||
ACadena := Format('Fechas de factura desde el %s hasta el %s', [VarToStr(FFechaInicio), VarToStr(FFechaFin)]);
|
||
// else
|
||
// ACadena := 'Sin rango de fechas';
|
||
ATextos.Add(ACadena);
|
||
ACadena := '';
|
||
|
||
// Filtrar el informe por fechas de vencimiento
|
||
if (not VarIsNull(FFechaVenInicio)) and (not VarIsNull(FFechaVenFin)) then
|
||
begin
|
||
ACadena := Format('Vencimientos desde el %s hasta el %s', [VarToStr(FFechaVenInicio), VarToStr(FFechaVenFin)]);
|
||
ATextos.Add(ACadena);
|
||
ACadena := '';
|
||
end;
|
||
|
||
if (FImporteMinimo > 0) then
|
||
begin
|
||
ACadena := Format('Facturas con importe superior a %m', [FImporteMinimo]);
|
||
ATextos.Add(ACadena);
|
||
ACadena := '';
|
||
end;
|
||
|
||
if (FImporteFiltro > 0) then
|
||
begin
|
||
ACadena := Format('Operaciones con importe superior a %m', [FImporteFiltro]);
|
||
ATextos.Add(ACadena);
|
||
ACadena := '';
|
||
end;
|
||
|
||
if Assigned(FListaIDClientes) then
|
||
begin
|
||
if (FListaIDClientes.Count > 0) then
|
||
begin
|
||
RecuperarNombresClientes;
|
||
ACadena := FListaNombresClientes.Text;
|
||
end
|
||
else begin
|
||
ACadena := 'Todos los clientes';
|
||
if FDesglosado then
|
||
ACadena := ACadena + ' (desglosados)'
|
||
end;
|
||
ATextos.Add(ACadena);
|
||
ACadena := '';
|
||
end;
|
||
|
||
frxReport.Variables.Variables['TextoParametros'] := ATextos.Text;
|
||
finally
|
||
FreeAndNil(ATextos);
|
||
end;
|
||
end;
|
||
|
||
procedure TRptFacturasCliente.PrepararTablaInforme(ATabla: TDAMemDataTable);
|
||
var
|
||
Condicion: TDAWhereExpression;
|
||
i: Integer;
|
||
|
||
begin
|
||
|
||
// Filtrar el informe por empresa
|
||
with ATabla.DynamicWhere do
|
||
begin
|
||
// (ID_EMPRESA >= ID)
|
||
Condicion := NewBinaryExpression(NewField('', fld_FacturasClienteID_EMPRESA), NewConstant(FIdEmpresa, datInteger), dboEqual);
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
||
end;
|
||
|
||
// Filtrar A<>o
|
||
if not VarIsNull(FAno) then
|
||
with ATabla.DynamicWhere do
|
||
begin
|
||
// (ANO >= FANO)
|
||
Condicion := NewBinaryExpression(NewField('', fld_ListaAnosRecibosANO), NewConstant(FAno, datInteger), dboEqual);
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
||
end;
|
||
|
||
// Filtrar el informe por fechas
|
||
if not VarIsNull(FFechaInicio)
|
||
and not VarIsNull(FFechaFin) then
|
||
begin
|
||
with ATabla.DynamicWhere do
|
||
begin
|
||
// (FECHA_INICIO between FECHA_FIN)
|
||
Condicion := NewBinaryExpression(NewField('', fld_FacturasClienteFECHA_FACTURA), NewConstant(FFechaInicio, datDateTime), dboGreaterOrEqual);
|
||
Condicion := NewBinaryExpression(NewBinaryExpression(NewField('', fld_FacturasClienteFECHA_FACTURA), NewConstant(FFechaFin, datDateTime), dboLessOrEqual), Condicion, dboAnd);
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
||
end;
|
||
end;
|
||
|
||
// Filtrar el informe por fechas de vencimiento
|
||
if not VarIsNull(FFechaVenInicio)
|
||
and not VarIsNull(FFechaVenFin) then
|
||
begin
|
||
with ATabla.DynamicWhere do
|
||
begin
|
||
// (FECHA_VENCIMIENTO_INICIO between FECHA_VENCIMIENTO_FIN)
|
||
Condicion := NewBinaryExpression(NewField('', fld_FacturasClienteFECHA_VENCIMIENTO), NewConstant(FFechaVenInicio, datDateTime), dboGreaterOrEqual);
|
||
Condicion := NewBinaryExpression(NewBinaryExpression(NewField('', fld_FacturasClienteFECHA_VENCIMIENTO), NewConstant(FFechaVenFin, datDateTime), dboLessOrEqual), Condicion, dboAnd);
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
||
end;
|
||
end;
|
||
|
||
// Filtrar el informe por proveedor
|
||
if Assigned(FListaIDClientes) then
|
||
begin
|
||
with ATabla.DynamicWhere do
|
||
begin
|
||
for i := 0 to FListaIDClientes.Count - 1 do
|
||
begin
|
||
|
||
// (ID_CLIENTE = ID)
|
||
Condicion := NewBinaryExpression(NewField('', fld_FacturasClienteID_CLIENTE), NewConstant(FListaIDClientes.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 (FImporteMinimo > 0) then
|
||
begin
|
||
with ATabla.DynamicWhere do
|
||
begin
|
||
// (IMPORTE_TOTAL > ImporteMinimo)
|
||
Condicion := NewBinaryExpression(NewField('', fld_FacturasCLienteIMPORTE_TOTAL), NewConstant(FImporteMinimo, datCurrency), dboGreaterOrEqual);
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
||
end;
|
||
end;
|
||
|
||
// Filtrar el informe por importe filtro
|
||
if (FImporteFiltro > 0) then
|
||
begin
|
||
with ATabla.DynamicWhere do
|
||
begin
|
||
// (IMPORTE_TOTAL_ANO > ImporteFiltro)
|
||
Condicion := NewBinaryExpression(NewField('', 'IMPORTE_TOTAL_ANO'), NewConstant(FImporteFiltro, datCurrency), dboGreaterOrEqual);
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
||
end;
|
||
end;
|
||
|
||
end;
|
||
|
||
procedure TRptFacturasCliente.PrepararTablaResumenInforme(ATabla: IDADataset);
|
||
var
|
||
i: Integer;
|
||
AWhereStr : String;
|
||
|
||
begin
|
||
// Filtrar el informe por empresa
|
||
AWhereStr := ' (' + fld_FacturasCLienteID_EMPRESA + ' = ' + IntToStr(FIdEmpresa) + ') ';
|
||
|
||
// Filtrar el informe por fechas
|
||
if not VarIsNull(FFechaInicio)
|
||
and not VarIsNull(FFechaFin) then
|
||
begin
|
||
if Length(AWhereStr) > 0 then
|
||
AWhereStr := AWhereStr + 'AND';
|
||
AWhereStr := AWhereStr + ' (' + fld_FacturasCLienteFECHA_FACTURA + ' between ''' + ReplaceStr(VarToStr(FFechaInicio),'/','.') + ''' and ''' + ReplaceStr(VarToStr(FFechaFin),'/','.') + ''') ';
|
||
end;
|
||
|
||
// Filtrar el informe por fechas de vencimiento
|
||
if not VarIsNull(FFechaVenInicio)
|
||
and not VarIsNull(FFechaVenFin) then
|
||
begin
|
||
if Length(AWhereStr) > 0 then
|
||
AWhereStr := AWhereStr + 'AND';
|
||
AWhereStr := AWhereStr + ' (' + fld_FacturasCLienteFECHA_VENCIMIENTO + ' between ''' + ReplaceStr(VarToStr(FFechaVenInicio),'/','.') + ''' and ''' + ReplaceStr(VarToStr(FFechaVenFin),'/','.') + ''') ';
|
||
end;
|
||
|
||
// Filtrar el informe por cliente
|
||
if Assigned(FListaIDClientes) then
|
||
begin
|
||
for i := 0 to FListaIDClientes.Count - 1 do
|
||
begin
|
||
if Length(AWhereStr) > 0 then
|
||
AWhereStr := AWhereStr + 'AND';
|
||
AWhereStr := AWhereStr + ' (' + fld_FacturasCLienteID_CLIENTE + ' = ' + IntToStr(FListaIDClientes.Items[i]) + ') ';
|
||
end;
|
||
end;
|
||
|
||
// Filtrar el informe por importe minimo
|
||
if (FImporteMinimo > 0) then
|
||
begin
|
||
if Length(AWhereStr) > 0 then
|
||
AWhereStr := AWhereStr + 'AND';
|
||
AWhereStr := AWhereStr + ' (' + fld_FacturasCLienteIMPORTE_TOTAL + ' >= ' + CurrToStr(FImporteMinimo) + ') ';
|
||
end;
|
||
|
||
ATabla.Where.AddText(AWhereStr);
|
||
end;
|
||
|
||
procedure TRptFacturasCliente.RecuperarNombresClientes;
|
||
var
|
||
AContactosService : IsrvContactos;
|
||
Intf : IInterface;
|
||
AClientID : TGUID;
|
||
|
||
ATableNameArray: StringArray;
|
||
ATableRequestInfoArray: TableRequestInfoArray;
|
||
ATableRequestInfo: TableRequestInfoV5;
|
||
AStream: TMemoryStream;
|
||
ADataTable: TDAMemDataTable;
|
||
i: Integer;
|
||
AWhereBuilder : TDAWhereBuilder;
|
||
ACondicion : TDAWhereExpression;
|
||
begin
|
||
CreateGUID(AClientID);
|
||
|
||
GetClassFactory('srvContactos').CreateInstance(AClientID, Intf);
|
||
|
||
if Assigned(Intf) then
|
||
begin
|
||
AContactosService := Intf as IsrvContactos;
|
||
|
||
ATableNameArray := StringArray.Create;
|
||
ATableRequestInfoArray := TableRequestInfoArray.Create;
|
||
AWhereBuilder := TDAWhereBuilder.Create;
|
||
try
|
||
ATableNameArray.Add('Clientes');
|
||
ATableRequestInfo := TableRequestInfoV5.Create;
|
||
|
||
with ATableRequestInfo do
|
||
begin
|
||
IncludeSchema := True;
|
||
MaxRecords := -1;
|
||
UserFilter := '';
|
||
|
||
AWhereBuilder.Clear;
|
||
with AWhereBuilder do
|
||
for i := 0 to FListaIDClientes.Count - 1 do
|
||
begin
|
||
ACondicion := NewBinaryExpression(
|
||
NewBinaryExpression(NewField('', 'ID'), NewConstant(FListaIDClientes[i], datInteger), dboEqual),
|
||
NewBinaryExpression(NewField('', 'ID_EMPRESA'), NewConstant(FIdEmpresa, datInteger), dboEqual),
|
||
dboAnd);
|
||
|
||
if not AWhereBuilder.IsEmpty then
|
||
Expression := NewBinaryExpression(Expression, ACondicion, dboOr)
|
||
else
|
||
Expression := ACondicion;
|
||
end;
|
||
|
||
WhereClause := AWhereBuilder.ExpressionToXmlNode(AWhereBuilder.Expression);
|
||
end;
|
||
|
||
try
|
||
ATableRequestInfoArray.Add(ATableRequestInfo);
|
||
AStream := AContactosService.GetData(ATableNameArray, ATableRequestInfoArray);
|
||
if Assigned(AStream) then
|
||
begin
|
||
ADataTable := TDAMemDataTable.Create(nil);
|
||
try
|
||
ADataTable.Name := 'Clientes';
|
||
ADataTable.LocalDataStreamer := Bin2DataStreamer;
|
||
ADataTable.RemoteFetchEnabled := False;
|
||
Bin2DataStreamer.ReadDataset(AStream, ADataTable, True);
|
||
|
||
ADataTable.Open;
|
||
FListaNombresClientes.Clear;
|
||
for i := 0 to ADataTable.RecordCount - 1 do
|
||
begin
|
||
FListaNombresClientes.Add(ADataTable.FieldByName('NOMBRE').AsString);
|
||
ADataTable.Next;
|
||
end;
|
||
|
||
finally
|
||
FreeANDNil(ADataTable);
|
||
end;
|
||
end;
|
||
except
|
||
on e: Exception do
|
||
dmServer.EscribirLog(e.Message);
|
||
end;
|
||
finally
|
||
FreeANDNIL(ATableRequestInfoArray);
|
||
FreeANDNIL(ATableNameArray);
|
||
FreeANDNIL(AWhereBuilder);
|
||
end;
|
||
end;
|
||
end;
|
||
|
||
procedure TRptFacturasCliente._GenerarFactura(const ID: integer);
|
||
var
|
||
AInforme: Variant;
|
||
|
||
begin
|
||
FConnection.BeginTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
|
||
try
|
||
tbl_Cabecera.Active := False;
|
||
tbl_Detalles.Active := False;
|
||
tbl_Vencimientos.Active := False;
|
||
|
||
tbl_Cabecera.ParamByName('ID').AsInteger := ID;
|
||
tbl_Detalles.ParamByName('ID_FACTURA').AsInteger := ID;
|
||
tbl_Vencimientos.ParamByName('ID_FACTURA').AsInteger := ID;
|
||
|
||
tbl_Cabecera.Active := True;
|
||
tbl_Detalles.Active := True;
|
||
tbl_Vencimientos.Active := True;
|
||
|
||
FIdEmpresa := tbl_Cabecera.FieldByName('ID_EMPRESA').AsInteger;
|
||
if (tbl_Cabecera.FieldByName('TIPO').AsString = CTE_TIPO_PROFORMA) then
|
||
AInforme := DarRutaFichero(DarRutaInformes, rptFacturaProformaCliente, tbl_Cabecera.FieldByName('ID_EMPRESA').AsString, tbl_Cabecera.FieldByName('ID_TIENDA').AsString)
|
||
else
|
||
begin
|
||
if (tbl_Cabecera.FieldByName('IDIOMA_ISO').AsString = IDIOMA_EN) then
|
||
AInforme := DarRutaFichero(DarRutaInformes, rptFacturaCliente, tbl_Cabecera.FieldByName('ID_EMPRESA').AsString, tbl_Cabecera.FieldByName('ID_TIENDA').AsString, IDIOMA_EN)
|
||
else
|
||
AInforme := DarRutaFichero(DarRutaInformes, rptFacturaCliente, tbl_Cabecera.FieldByName('ID_EMPRESA').AsString, tbl_Cabecera.FieldByName('ID_TIENDA').AsString);
|
||
end;
|
||
|
||
if VarIsNull(AInforme) then
|
||
raise Exception.Create (('Error Servidor: _GenerarFactura, no encuentra informe ' + rptFacturaCliente));
|
||
|
||
frxReport.LoadFromFile(AInforme, True);
|
||
frxReport.Variables.Variables['VerLogotipo'] := FVerLogotipo;
|
||
frxReport.ReportOptions.Name := tbl_Cabecera.FieldByName('REFERENCIA').AsString;
|
||
frxReport.PrepareReport(False);
|
||
finally
|
||
FConnection.RollbackTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
|
||
end;
|
||
end;
|
||
|
||
function TRptFacturasCliente._GenerarInforme(const TipoInforme: String): Binary;
|
||
var
|
||
AInforme: Variant;
|
||
|
||
begin
|
||
Result := Binary.Create;
|
||
AInforme := DarRutaFichero(DarRutaInformes, TipoInforme, IntToStr(FIdEmpresa));
|
||
if VarIsNull(AInforme) then
|
||
raise Exception.Create (('Error Servidor: _GenerarInforme, no encuentra informe ' + TipoInforme));
|
||
|
||
frxReport.LoadFromFile(AInforme, True);
|
||
IniciarParametrosInforme;
|
||
|
||
frxReport.PrepareReport(False);
|
||
frxReport.PreviewPages.SaveToStream(Result);
|
||
end;
|
||
|
||
end.
|