2014-02-06 18:44:21 +00:00
unit uGenerarFacturasProPreCliUtils;
2010-08-11 10:53:06 +00:00
interface
uses
2014-02-06 18:44:21 +00:00
Windows, SysUtils, Classes, uBizFacturasProforma, uBizPresupuestosCliente, pngimage, JSDialog;
2010-08-11 10:53:06 +00:00
type
2014-02-06 18:44:21 +00:00
TdmGenerarFacturasProPre = class( TDataModule)
2010-08-11 10:53:06 +00:00
JsListaFacturasGeneradas: TJSDialog;
end ;
2014-02-06 18:44:21 +00:00
function GenerarFacturaProPre( const IDPresupuesto : Integer ; const CopiarDetalles: Boolean = True ) : Boolean ; overload ;
function GenerarFacturaProPre( APresupuestos : IBizPresupuestoCliente; const CopiarDetalles: Boolean = True ) : Boolean ; overload ;
function GenerarFacturaProPre : Boolean ; overload ;
2010-08-11 10:53:06 +00:00
2014-02-06 18:44:21 +00:00
function GenerarFacturas( AFacturas : IBizFacturaProforma; AListaPresupuestos : IBizPresupuestoCliente; const CopiarDetalles: Boolean = True ) : Boolean ;
2010-08-11 10:53:06 +00:00
implementation
{$R *.dfm}
uses
uDialogUtils, uDADataTable, uControllerDetallesBase,
uBizDetallesPresupuestoCliente, schPresupuestosClienteClient_Intf,
2014-02-06 18:44:21 +00:00
schFacturasProformaClient_Intf, uDetallesFacturaProformaController,
2010-08-11 10:53:06 +00:00
uPresupuestosClienteController, uDetallesPresupuestoClienteController,
2014-02-06 18:44:21 +00:00
uFacturasProformaController, uBizDetallesFacturaProforma;
2010-08-11 10:53:06 +00:00
var
2014-02-06 18:44:21 +00:00
dmGenerarFacturasProPre: TdmGenerarFacturasProPre;
2010-08-11 10:53:06 +00:00
APresupuestosClienteController : IPresupuestosClienteController;
ADetallesPresupuestosClienteController : IDetallesPresupuestoClienteController;
2014-02-06 18:44:21 +00:00
AFacturasProformaController : IFacturasProformaController;
2010-08-11 10:53:06 +00:00
{ M<> todos auxiliares }
procedure CopiarArticulosPresupuesto( AOrigen: IBizDetallesPresupuestoCliente;
2014-02-06 18:44:21 +00:00
ADestino : IBizDetallesFacturaProforma) ;
2010-08-11 10:53:06 +00:00
var
i : integer ;
2014-02-06 18:44:21 +00:00
ADetallesController : IDetallesFacturaProformaController;
2010-08-11 10:53:06 +00:00
begin
if not Assigned( AOrigen) then
raise Exception. Create ( 'Origen no asignado (CopiarArticulosPresupuesto)' ) ;
if not Assigned( ADestino) then
raise Exception. Create ( 'Destino no asignado (CopiarArticulosPresupuesto)' ) ;
if not AOrigen. DataTable. Active then
AOrigen. DataTable. Active : = True ;
if not ADestino. DataTable. Active then
ADestino. DataTable. Active : = True ;
2014-02-06 18:44:21 +00:00
ADetallesController : = TDetallesFacturaProformaController. Create;
2010-08-11 10:53:06 +00:00
try
//OJO IMPORTANTE
//Siempre que vayamos a trabajar con los detalles debemos hacer un beginupdate de los mismos y un endupdate para
//obligarle siempre a recalcular los detalles una sola vez
ADetallesController. BeginUpdate( ADestino) ;
AOrigen. DataTable. First;
for i : = 0 to AOrigen. DataTable. RecordCount - 1 do
begin
ADetallesController. Add( ADestino, TIPO_DETALLE_CONCEPTO) ;
ADestino. Edit;
ADestino. REFERENCIA : = AOrigen. REFERENCIA;
if AOrigen. ID_ARTICULO > 0 then
ADestino. ID_ARTICULO : = AOrigen. ID_ARTICULO;
ADestino. CONCEPTO : = AOrigen. CONCEPTO;
ADestino. CANTIDAD : = AOrigen. CANTIDAD;
ADestino. IMPORTE_UNIDAD : = AOrigen. IMPORTE_UNIDAD;
ADestino. IMPORTE_TOTAL : = AOrigen. IMPORTE_TOTAL;
ADestino. DESCUENTO : = AOrigen. DESCUENTO;
ADestino. IMPORTE_PORTE : = AOrigen. IMPORTE_PORTE;
ADestino. VISIBLE : = AOrigen. VISIBLE;
ADestino. REFERENCIA_PROVEEDOR : = AOrigen. REFERENCIA_PROVEEDOR;
ADestino. Post;
AOrigen. Next;
end ;
finally
ADetallesController. EndUpdate( ADestino) ;
ADetallesController : = NIL ;
end ;
end ;
procedure Inicializar;
begin
2014-02-06 18:44:21 +00:00
dmGenerarFacturasProPre : = TdmGenerarFacturasProPre. Create( nil ) ;
2010-08-11 10:53:06 +00:00
APresupuestosClienteController : = TPresupuestosClienteController. Create;
ADetallesPresupuestosClienteController : = TDetallesPresupuestoClienteController. Create;
2014-02-06 18:44:21 +00:00
AFacturasProformaController : = TFacturasProformaController. Create;
2010-08-11 10:53:06 +00:00
end ;
procedure Finalizar;
begin
2014-02-06 18:44:21 +00:00
FreeAndNIL( dmGenerarFacturasProPre) ;
2010-08-11 10:53:06 +00:00
APresupuestosClienteController : = nil ;
ADetallesPresupuestosClienteController : = nil ;
2014-02-06 18:44:21 +00:00
AFacturasProformaController : = nil ;
2010-08-11 10:53:06 +00:00
end ;
2014-02-06 18:44:21 +00:00
function GenerarFacturaProPre( const IDPresupuesto : Integer ; const CopiarDetalles: Boolean = True ) : Boolean ; overload ;
2010-08-11 10:53:06 +00:00
var
APresupuesto : IBizPresupuestoCliente;
begin
Result : = False ;
if not Assigned( APresupuestosClienteController) then
Inicializar;
APresupuesto : = APresupuestosClienteController. Buscar( IDPresupuesto) ;
2014-02-06 18:44:21 +00:00
Result : = GenerarFacturaProPre( APresupuesto, CopiarDetalles) ;
2010-08-11 10:53:06 +00:00
if Assigned( APresupuestosClienteController) then
Finalizar;
end ;
2014-02-06 18:44:21 +00:00
function GenerarFacturaProPre( APresupuestos : IBizPresupuestoCliente; const CopiarDetalles: Boolean = True ) : Boolean ; overload ;
2010-08-11 10:53:06 +00:00
var
ARespuesta : Integer ;
2014-02-06 18:44:21 +00:00
AFacturasNuevas : IBizFacturaProforma;
2010-08-11 10:53:06 +00:00
i: Integer ;
begin
Result : = False ;
if not Assigned( APresupuestos) then
2014-02-06 18:44:21 +00:00
raise Exception. Create( 'Presupuesto de cliente no asignado (GenerarFacturaProPre)' ) ;
2010-08-11 10:53:06 +00:00
if not APresupuestos. DataTable. Active then
APresupuestos. DataTable. Active : = True ;
if not Assigned( APresupuestosClienteController) then
Inicializar;
try
2014-02-06 18:44:21 +00:00
AFacturasNuevas : = AFacturasProformaController. Nuevo( False ) ;
2010-08-11 10:53:06 +00:00
if GenerarFacturas( AFacturasNuevas, APresupuestos, CopiarDetalles) then
begin
if AFacturasNuevas. DataTable. RecordCount = 1 then
begin
2014-02-06 18:44:21 +00:00
ShowInfoMessage( 'La factura proforma se ha dado de alta con el c<> digo ' + AFacturasNuevas. REFERENCIA)
2010-08-11 10:53:06 +00:00
end
else begin
2014-02-06 18:44:21 +00:00
with dmGenerarFacturasProPre. JsListaFacturasGeneradas. Content do
2010-08-11 10:53:06 +00:00
begin
Clear;
AFacturasNuevas. DataTable. Last;
for i : = 0 to AFacturasNuevas. DataTable. RecordCount - 1 do
begin
if Length( AFacturasNuevas. REFERENCIA) > 0 then
Add( AFacturasNuevas. REFERENCIA + ': ' + AFacturasNuevas. NOMBRE) ;
AFacturasNuevas. DataTable. Prior;
end ;
end ;
2014-02-06 18:44:21 +00:00
dmGenerarFacturasProPre. JsListaFacturasGeneradas. Execute;
2010-08-11 10:53:06 +00:00
end ;
Result : = True ;
end ;
finally
AFacturasNuevas : = NIL ;
if Assigned( APresupuestosClienteController) then
Finalizar;
end ;
end ;
2014-02-06 18:44:21 +00:00
function GenerarFacturaProPre : Boolean ; overload ;
2010-08-11 10:53:06 +00:00
var
APresupuestos : IBizPresupuestoCliente;
begin
Result : = False ;
try
if not Assigned( APresupuestosClienteController) then
Inicializar;
2014-06-18 15:50:30 +00:00
APresupuestos : = APresupuestosClienteController. ElegirPresupuestos( APresupuestosClienteController. BuscarTodos,
2010-08-11 10:53:06 +00:00
'Elija el presupuesto o presupuestos de proveedor que desea utilizar para dar de alta la factura.'
+ #10 #13 + 'Si elige presupuestos de proveedores diferentes se dar<61> de alta una factura por cada uno de ellos.' , True ) ;
if Assigned( APresupuestos) then
begin
if ( ShowConfirmMessage( 'Generar factura' , Format( '<27> Desea copiar todos los conceptos de/los presupuesto/s seleccionados a sus facturas correspondientes?' , [ ] ) ) = IDYES) then
2014-02-06 18:44:21 +00:00
Result : = GenerarFacturaProPre( APresupuestos)
2010-08-11 10:53:06 +00:00
else
2014-02-06 18:44:21 +00:00
Result : = GenerarFacturaProPre( APresupuestos, False ) ;
2010-08-11 10:53:06 +00:00
end ;
if Assigned( APresupuestosClienteController) then
Finalizar;
finally
APresupuestos : = Nil ;
end ;
end ;
2014-02-06 18:44:21 +00:00
function GenerarFacturas( AFacturas : IBizFacturaProforma; AListaPresupuestos : IBizPresupuestoCliente; const CopiarDetalles: Boolean = True ) : Boolean ;
2010-08-11 10:53:06 +00:00
var
2014-02-06 18:44:21 +00:00
AFacturaActual : IBizFacturaProforma;
2010-08-11 10:53:06 +00:00
APresupuestosController : IPresupuestosClienteController;
I: Integer ;
bEnEdicion : Boolean ;
begin
// ATENCI<43> N!!! AFacturas tiene que estar vacio para no pisar facturas
// ya generadas.
if not Assigned( AFacturas) then
raise Exception. Create ( 'Factura no asignado (Anadir)' ) ;
if not Assigned( AListaPresupuestos) then
raise Exception. Create ( 'Presupuestos no asignados (Anadir)' ) ;
if not AFacturas. DataTable. Active then
AFacturas. DataTable. Active : = True ;
if not AListaPresupuestos. DataTable. Active then
AListaPresupuestos. DataTable. Active : = True ;
APresupuestosController : = TPresupuestosClienteController. Create;
try
// Ordenar por fecha de albaran
AListaPresupuestos. DataTable. Sort( [ fld_PresupuestosClienteFECHA_PRESUPUESTO] , [ uDADataTable. sdAscending] ) ;
AListaPresupuestos. First;
for I : = 0 to AListaPresupuestos. DataTable. RecordCount - 1 do
begin
AListaPresupuestos. _Cliente : = NIL ;
AFacturaActual : = NIL ;
// Busco si hay alguna factura ya hecha de ese Cliente
AFacturas. DataTable. First;
2014-02-06 18:44:21 +00:00
if AFacturas. DataTable. Locate( fld_FacturasProformaID_CLIENTE, AListaPresupuestos. ID_Cliente, [ ] ) then
2010-08-11 10:53:06 +00:00
begin
AFacturaActual : = AFacturas;
2014-02-06 18:44:21 +00:00
AFacturasProformaController. RecuperarCliente( AFacturaActual) ;
2010-08-11 10:53:06 +00:00
AFacturaActual. Cliente. DataTable. Active : = True ;
end
else begin
// No hay factura de ese Cliente. Creo una nueva
2014-02-06 18:44:21 +00:00
AFacturasProformaController. Anadir( AFacturas) ;
2010-08-11 10:53:06 +00:00
APresupuestosController. RecuperarCliente( AListaPresupuestos) ;
AFacturas. Cliente : = AListaPresupuestos. Cliente;
2014-02-06 18:44:21 +00:00
// AFacturas.OBSERVACIONES.Add('Importe de factura por trabajos realizados seg<65> n indicaciones de nuestro presupuesto ' + AListaPresupuestos.REFERENCIA + ' con fecha ' + DateToStr(AListaPresupuestos.FECHA_PRESUPUESTO));
2010-08-11 10:53:06 +00:00
AFacturaActual : = AFacturas;
end ;
// Ya tengo la factura. Le a<> ado los conceptos del presupuesto
AFacturaActual. Detalles. DataTable. Last;
//Se pone la referencia del presupuesto y el total y nos olvidamos de los detalles
if not CopiarDetalles then
begin
2014-02-06 18:44:21 +00:00
AFacturasProformaController. DetallesController. Add( AFacturaActual. Detalles, TIPO_DETALLE_CONCEPTO) ;
2010-08-11 10:53:06 +00:00
with AFacturaActual. Detalles do
begin
Edit;
CONCEPTO : = 'Presupuesto ' + AListaPresupuestos. REFERENCIA;
CANTIDAD : = 1 ;
IMPORTE_UNIDAD : = AListaPresupuestos. IMPORTE_TOTAL; //Tecsitel no utiliza el IVA en los presupuestos por lo tanto el
//importe total es lo que queremos (IMPORTE_NETO+IMPORTE_PORTE)
Post;
end ;
end
//Se copian los detalles del presupuesto a la factura
else
begin
// A<> ado el t<> tulo
2014-02-06 18:44:21 +00:00
AFacturasProformaController. DetallesController. Add( AFacturaActual. Detalles, TIPO_DETALLE_TITULO) ;
2010-08-11 10:53:06 +00:00
with AFacturaActual. Detalles do
begin
Edit;
CONCEPTO : = 'Presupuesto ' + AListaPresupuestos. REFERENCIA + ' del ' + DateToStr( AListaPresupuestos. FECHA_PRESUPUESTO) ;
Post;
end ;
// A<> ado el contenido del presupuesto
CopiarArticulosPresupuesto( AListaPresupuestos. Detalles, AFacturaActual. Detalles) ;
//En el caso de tener porte el presupuesto se a<> ade una fila con el importe
if ( AListaPresupuestos. IMPORTE_PORTE > 0 ) then
begin
2014-02-06 18:44:21 +00:00
AFacturasProformaController. DetallesController. Add( AFacturaActual. Detalles, TIPO_DETALLE_CONCEPTO) ;
2010-08-11 10:53:06 +00:00
with AFacturaActual. Detalles do
begin
Edit;
CONCEPTO : = 'Porte del presupuesto' ;
CANTIDAD : = 1 ;
IMPORTE_UNIDAD : = AListaPresupuestos. IMPORTE_PORTE;
Post;
end ;
end ;
//En el caso de tener descuento (bonificaci<63> n) el presupuesto se a<> ade una fila con el importe
if ( AListaPresupuestos. IMPORTE_DESCUENTO > 0 ) then
begin
2014-02-06 18:44:21 +00:00
AFacturasProformaController. DetallesController. Add( AFacturaActual. Detalles, TIPO_DETALLE_CONCEPTO) ;
2010-08-11 10:53:06 +00:00
with AFacturaActual. Detalles do
begin
Edit;
CONCEPTO : = 'Bonificaci<63> n ' + FloatToStr( AListaPresupuestos. DESCUENTO) + '%' ;
CANTIDAD : = - 1 ;
IMPORTE_UNIDAD : = AListaPresupuestos. IMPORTE_DESCUENTO;
Post;
end ;
end ;
// A<> ado el resumen
2014-02-06 18:44:21 +00:00
AFacturasProformaController. DetallesController. Add( AFacturaActual. Detalles, TIPO_DETALLE_SUBTOTAL) ;
2010-08-11 10:53:06 +00:00
with AFacturaActual. Detalles do
begin
Edit;
CONCEPTO : = 'Total del presupuesto ' + AListaPresupuestos. REFERENCIA;
Post;
end ;
end ;
// A<> ado una l<> nea en blanco
2014-02-06 18:44:21 +00:00
AFacturasProformaController. DetallesController. Add( AFacturaActual. Detalles, TIPO_DETALLE_CONCEPTO) ;
2010-08-11 10:53:06 +00:00
with AFacturaActual. Detalles do
begin
Edit;
CONCEPTO : = '' ;
Post;
end ;
// Guardo la factura que acabo de generar o editar
AFacturaActual. CalcularImporteTotal;
2014-02-06 18:44:21 +00:00
AFacturasProformaController. Guardar( AFacturaActual) ;
2010-08-11 10:53:06 +00:00
// Asocio la factura con el presupuesto
AListaPresupuestos. Edit;
AListaPresupuestos. Post;
APresupuestosController. Guardar( AListaPresupuestos) ;
AListaPresupuestos. Next;
end ;
Result : = True ;
finally
APresupuestosController : = NIL ;
end ;
end ;
end .