2007-11-05 18:00:22 +00:00
unit uFacturasClienteController;
interface
uses
Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleFacturasCliente,
uClientesController, uDetallesFacturaClienteController, uBizAlbaranesCliente,
uBizFacturasCliente;
type
2008-08-22 14:52:35 +00:00
IFacturasClienteController = interface( IControllerBase)
2007-11-05 18:00:22 +00:00
[ '{CAD20B4E-6D0B-4A1C-9306-B195824B6CAD}' ]
function GetClienteController: IClientesController;
procedure SetClienteController( const Value: IClientesController) ;
property ClienteController: IClientesController read GetClienteController write SetClienteController;
function GetDetallesController: IDetallesFacturaClienteController;
procedure SetDetallesController( const Value: IDetallesFacturaClienteController) ;
property DetallesController: IDetallesFacturaClienteController read GetDetallesController write SetDetallesController;
function Buscar( const ID: Integer ) : IBizFacturaCliente;
2008-11-10 11:29:57 +00:00
function BuscarTodos: IBizFacturaCliente; overload ;
function BuscarTodos( const AID_Cliente: Integer ) : IBizFacturaCliente; overload ;
2007-11-05 18:00:22 +00:00
function BuscarTodasPendientesComision( IdAgente: Integer ; IdComision: Integer ; IdFacturasAsociadas: String ) : IBizFacturaCliente;
2015-02-05 17:51:58 +00:00
function BuscarDePresupuesto( const AID_Presupuesto: Integer ) : IBizFacturaCliente;
2007-11-05 18:00:22 +00:00
procedure Ver( AFactura : IBizFacturaCliente) ;
2008-11-10 11:29:57 +00:00
procedure VerTodos( AFacturas: IBizFacturaCliente;
const AVerModal : Boolean = False ; const AWindowCaption: String = '' ;
const AHeaderText: String = '' ) ;
2008-01-15 10:31:41 +00:00
function Nuevo ( withInsert: Boolean = True ) : IBizFacturaCliente;
2007-11-05 18:00:22 +00:00
function Anadir( AFactura : IBizFacturaCliente) : Boolean ; overload ;
function AnadirAbono( AFactura : IBizFacturaCliente) : Boolean ;
2008-06-02 11:59:36 +00:00
// function Anadir(AFacturas : IBizFacturaCliente; AListaAlbaranes : IBizAlbaranCliente): Boolean; overload;
2007-11-05 18:00:22 +00:00
// function Anadir(AFactura : IBizFacturaCliente; const IDPedido : Integer): Boolean; overload;
function Eliminar( const ID : Integer ) : Boolean ; overload ;
function Eliminar( AFactura : IBizFacturaCliente; AllItems: Boolean = false ) : Boolean ; overload ;
function Guardar( AFactura : IBizFacturaCliente) : Boolean ;
procedure DescartarCambios( AFactura : IBizFacturaCliente) ;
function Existe( const ID: Integer ) : Boolean ;
procedure RecuperarCliente( AFactura : IBizFacturaCliente) ;
function Duplicar( AFactura: IBizFacturaCliente) : IBizFacturaCliente;
function GenerarAbono( AFactura: IBizFacturaCliente) : IBizFacturaCliente;
2008-11-13 10:33:06 +00:00
procedure Preview( AFactura : IBizFacturaCliente; AllItems: Boolean = false ;
2009-04-17 09:34:26 +00:00
const VerSello: Boolean = True ; const VerCopia: Boolean = True ) ;
2008-11-13 10:33:06 +00:00
procedure Print( AFactura : IBizFacturaCliente; AllItems: Boolean = false ;
2009-04-17 09:34:26 +00:00
const VerSello: Boolean = True ; const VerCopia: Boolean = True ) ;
2007-11-05 18:00:22 +00:00
procedure RecalcularImportes( AFactura: IBizFacturaCliente) ;
function EsModificable( AFactura: IBizFacturaCliente) : Boolean ;
function EsEliminable( AFactura: IBizFacturaCliente) : Boolean ;
function ElegirFacturas( AFacturas : IBizFacturaCliente; AMensaje: String ; AMultiSelect: Boolean ) : IBizFacturaCliente;
function ExtraerSeleccionados( ARecibosCliente: IBizFacturaCliente) : IBizFacturaCliente;
2010-09-23 10:49:10 +00:00
function EnviarFacturaPorEMail( AFactura : IBizFacturaCliente;
const AEnviarDirectamente: Boolean = True ;
const ADireccionEMail: String = '' ; const AAsuntoEMail: String = '' ;
const ATextoEMail: String = '' ) : Boolean ;
function EnviarEmailFacturas( AFacturas : IBizFacturaCliente) : Boolean ;
function GenerarEmailFactura( AFactura : IBizFacturaCliente) : Boolean ;
2009-02-27 10:44:29 +00:00
function DarListaAnosFacturas: TStringList;
procedure FiltrarAno( AFactura: IBizFacturaCliente; ADynWhereDataTable: WideString ; const Ano: String ) ;
2007-11-05 18:00:22 +00:00
end ;
2008-08-22 14:52:35 +00:00
TFacturasClienteController = class( TControllerBase, IFacturasClienteController)
2007-11-05 18:00:22 +00:00
private
FDataModule : IDataModuleFacturasCliente;
FClienteController : IClientesController;
FDetallesController : IDetallesFacturaClienteController;
function GetClienteController: IClientesController;
procedure SetClienteController( const Value: IClientesController) ;
function GetDetallesController: IDetallesFacturaClienteController;
procedure SetDetallesController( const Value: IDetallesFacturaClienteController) ;
function CreateEditor( const AName : String ; const IID: TGUID; out Intf) : Boolean ;
function _Vacio : IBizFacturaCliente;
procedure FiltrarEmpresa( AFactura: IBizFacturaCliente) ;
function ValidarFactura( AFactura: IBizFacturaCliente) : Boolean ;
procedure GenerarRecibos( AFactura: IBizFacturaCliente) ;
2008-09-25 17:28:31 +00:00
2007-11-05 18:00:22 +00:00
protected
procedure RecibirAviso( ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable) ; override ;
2008-09-25 17:28:31 +00:00
2007-11-05 18:00:22 +00:00
public
property ClienteController: IClientesController read GetClienteController write SetClienteController;
property DetallesController: IDetallesFacturaClienteController read GetDetallesController write SetDetallesController;
2007-11-22 19:09:07 +00:00
constructor Create; override ;
2007-11-05 18:00:22 +00:00
destructor Destroy; override ;
function Eliminar( const ID : Integer ) : Boolean ; overload ;
function Eliminar( AFactura : IBizFacturaCliente; AllItems: Boolean = false ) : Boolean ; overload ;
function Guardar( AFactura : IBizFacturaCliente) : Boolean ;
procedure DescartarCambios( AFactura : IBizFacturaCliente) ; virtual ;
function Existe( const ID: Integer ) : Boolean ; virtual ;
function Anadir( AFactura : IBizFacturaCliente) : Boolean ; overload ;
function AnadirAbono( AFactura : IBizFacturaCliente) : Boolean ;
2008-06-02 11:59:36 +00:00
// function Anadir(AFacturas : IBizFacturaCliente; AListaAlbaranes : IBizAlbaranCliente): Boolean; overload;
2007-11-05 18:00:22 +00:00
// function Anadir(AFactura : IBizFacturaCliente; const IDPedido : Integer): Boolean; overload;
function Buscar( const ID: Integer ) : IBizFacturaCliente;
2008-11-10 11:29:57 +00:00
function BuscarTodos: IBizFacturaCliente; overload ;
function BuscarTodos( const AID_Cliente: Integer ) : IBizFacturaCliente; overload ;
2007-11-05 18:00:22 +00:00
function BuscarTodasPendientesComision( IdAgente: Integer ; IdComision: Integer ; IdFacturasAsociadas: String ) : IBizFacturaCliente;
2015-02-05 17:51:58 +00:00
function BuscarDePresupuesto( const AID_Presupuesto: Integer ) : IBizFacturaCliente;
2008-01-15 10:31:41 +00:00
function Nuevo ( withInsert: Boolean = True ) : IBizFacturaCliente;
2007-11-05 18:00:22 +00:00
procedure Ver( AFactura : IBizFacturaCliente) ;
2008-11-10 11:29:57 +00:00
procedure VerTodos( AFacturas: IBizFacturaCliente;
const AVerModal : Boolean = False ; const AWindowCaption: String = '' ;
const AHeaderText: String = '' ) ;
2007-11-05 18:00:22 +00:00
function Duplicar( AFactura: IBizFacturaCliente) : IBizFacturaCliente;
function GenerarAbono( AFactura: IBizFacturaCliente) : IBizFacturaCliente;
procedure RecuperarCliente( AFactura : IBizFacturaCliente) ;
2008-11-13 10:33:06 +00:00
procedure Preview( AFactura : IBizFacturaCliente; AllItems: Boolean = false ;
2009-04-17 09:34:26 +00:00
const VerSello: Boolean = True ; const VerCopia: Boolean = True ) ;
2008-11-13 10:33:06 +00:00
procedure Print( AFactura : IBizFacturaCliente; AllItems: Boolean = false ;
2009-04-17 09:34:26 +00:00
const VerSello: Boolean = True ; const VerCopia: Boolean = True ) ;
2007-11-05 18:00:22 +00:00
procedure RecalcularImportes( AFactura: IBizFacturaCliente) ;
function EsModificable( AFactura: IBizFacturaCliente) : Boolean ;
function EsEliminable( AFactura: IBizFacturaCliente) : Boolean ;
function ElegirFacturas( AFacturas : IBizFacturaCliente; AMensaje: String ; AMultiSelect: Boolean ) : IBizFacturaCliente;
function ExtraerSeleccionados( AFacturasCliente: IBizFacturaCliente) : IBizFacturaCliente;
2010-09-23 10:49:10 +00:00
function EnviarFacturaPorEMail( AFactura : IBizFacturaCliente;
const AEnviarDirectamente: Boolean = True ;
const ADireccionEMail: String = '' ; const AAsuntoEMail: String = '' ;
const ATextoEMail: String = '' ) : Boolean ;
function EnviarEmailFacturas( AFacturas : IBizFacturaCliente) : Boolean ;
function GenerarEmailFactura( AFactura : IBizFacturaCliente) : Boolean ;
2009-02-27 10:44:29 +00:00
function DarListaAnosFacturas: TStringList;
procedure FiltrarAno( AFactura: IBizFacturaCliente; ADynWhereDataTable: WideString ; const Ano: String ) ;
2007-11-05 18:00:22 +00:00
end ;
implementation
uses
2010-09-23 10:49:10 +00:00
Controls, cxControls, DB, uEditorRegistryUtils, schFacturasClienteClient_Intf,
2007-11-18 15:07:51 +00:00
uBizContactos, uIEditorFacturasCliente, uIEditorFacturaCliente, uFactuGES_App,
2007-11-05 18:00:22 +00:00
uDataModuleFacturasCliente, uBizDetallesFacturaCliente, uControllerDetallesBase,
2008-06-04 16:12:28 +00:00
uDataModuleUsuarios, uDAInterfaces, uDataTableUtils, uDateUtils, uNumUtils,
2007-11-05 18:00:22 +00:00
uAlbaranesClienteController, schAlbaranesClienteClient_Intf, uROTypes, uDetallesAlbaranClienteController,
uBizDetallesAlbaranCliente, uFacturasClienteReportController,
2008-03-03 12:27:26 +00:00
DateUtils, Forms, uFormasPagoController, uBizFormasPago, uIEditorElegirFacturasCliente,
2010-09-23 10:49:10 +00:00
uRecibosClienteController, uBizRecibosCliente, uIntegerListUtils, uIDialogListaFacturasClienteEnvioEMail,
2009-02-27 10:44:29 +00:00
uSistemaFunc, uDialogElegirEMail, uEMailUtils, Dialogs, uStringsUtils,
uROXMLIntf;
2007-11-05 18:00:22 +00:00
{ TFacturasClienteController }
function TFacturasClienteController. Anadir( AFactura: IBizFacturaCliente) : Boolean ;
begin
if not Assigned( AFactura) then
raise Exception. Create ( 'Factura no asignada (Anadir)' ) ;
AFactura. Insert;
Result : = True ;
end ;
function TFacturasClienteController. AnadirAbono( AFactura: IBizFacturaCliente) : Boolean ;
begin
Anadir( AFactura) ;
AFactura. TIPO : = CTE_TIPO_ABONO;
Result : = True ;
end ;
2008-06-02 11:59:36 +00:00
{
2007-11-05 18:00:22 +00:00
function TFacturasClienteController. Anadir( AFacturas: IBizFacturaCliente;
AListaAlbaranes: IBizAlbaranCliente) : Boolean ;
var
AFacturaActual : IBizFacturaCliente;
AAlbaranesController : IAlbaranesClienteController;
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 asignada (Anadir)' ) ;
if not Assigned( AListaAlbaranes) then
raise Exception. Create ( 'Albaranes no asignados (Anadir)' ) ;
if not AFacturas. DataTable. Active then
AFacturas. DataTable. Active : = True ;
if not AListaAlbaranes. DataTable. Active then
AListaAlbaranes. DataTable. Active : = True ;
ShowHourglassCursor;
Application. ProcessMessages;
AAlbaranesController : = TAlbaranesClienteController. Create;
try
// Ordenar por fecha de albaran
AListaAlbaranes. DataTable. Sort( [ fld_AlbaranesClienteFECHA_ALBARAN] , [ uDADataTable. sdAscending] ) ;
AListaAlbaranes. First;
for I : = 0 to AListaAlbaranes. DataTable. RecordCount - 1 do
begin
AListaAlbaranes. _Cliente : = NIL ;
AFacturaActual : = NIL ;
// Busco si hay alguna factura ya hecha de ese cliente
AFacturas. DataTable. First;
if AFacturas. DataTable. Locate( fld_FacturasClienteID_CLIENTE, AListaAlbaranes. ID_CLIENTE, [ ] ) then
begin
AFacturaActual : = AFacturas;
RecuperarCliente( AFacturaActual) ;
AFacturaActual. Cliente. DataTable. Active : = True ;
end
else begin
// No hay factura de ese cliente. Creo una nueva
Anadir( AFacturas) ;
AAlbaranesController. RecuperarCliente( AListaAlbaranes) ;
AListaAlbaranes. Cliente. DataTable. Active : = True ;
AFacturas. Cliente : = AListaAlbaranes. Cliente;
//Si el albaran es de tipo devoluci<63> n hacemos la factura de tipo abono
if ( AListaAlbaranes. TIPO = CTE_TIPO_ALBARAN_DEV) then
begin
bEnEdicion : = ( AFacturas. DataTable. State in dsEditModes) ;
if not bEnEdicion then
AFacturas. Edit;
AFacturas. TIPO : = CTE_TIPO_ABONO;
AFacturas. Post;
if bEnEdicion then
AFacturas. Edit;
end ;
AFacturaActual : = AFacturas;
end ;
// Ya tengo la factura. Le a<> ado los conceptos del albar<61> n
AFacturaActual. Detalles. DataTable. Last;
// A<> ado el t<> tulo
Self. DetallesController. Add( AFacturaActual. Detalles, TIPO_DETALLE_TITULO) ;
with AFacturaActual. Detalles do
begin
Edit;
CONCEPTO : = 'Albar<61> n ' + AListaAlbaranes. REFERENCIA + ' del ' + DateToStr( AListaAlbaranes. FECHA_ALBARAN) ;
Post;
end ;
// A<> ado el contenido del albar<61> n
CopiarArticulosAlbaran( AListaAlbaranes. Detalles, AFacturaActual. Detalles) ;
Self. DetallesController. Add( AFacturaActual. Detalles, TIPO_DETALLE_CONCEPTO) ;
with AFacturaActual. Detalles do
begin
Edit;
CONCEPTO : = 'Porte del albar<61> n' ;
CANTIDAD : = 1 ;
IMPORTE_UNIDAD : = AListaAlbaranes. IMPORTE_PORTE;
Post;
end ;
// A<> ado el resumen
Self. DetallesController. Add( AFacturaActual. Detalles, TIPO_DETALLE_SUBTOTAL) ;
with AFacturaActual. Detalles do
begin
Edit;
CONCEPTO : = 'Total del albar<61> n ' + AListaAlbaranes. REFERENCIA;
Post;
end ;
// A<> ado una l<> nea en blanco
Self. DetallesController. Add( AFacturaActual. Detalles, TIPO_DETALLE_CONCEPTO) ;
with AFacturaActual. Detalles do
begin
Edit;
CONCEPTO : = '' ;
Post;
end ;
// Guardo la factura que acabo de generar o editar
AFacturaActual. CalcularImporteTotal;
Self. Guardar( AFacturaActual) ;
// Asocio la factura con el albar<61> n
AListaAlbaranes. Edit;
AListaAlbaranes. ID_FACTURA : = AFacturaActual. ID;
AListaAlbaranes. Post;
AAlbaranesController. Guardar( AListaAlbaranes) ;
AListaAlbaranes. Next;
end ;
Result : = True ;
finally
AAlbaranesController : = NIL ;
HideHourglassCursor
end ;
end ;
2008-06-02 11:59:36 +00:00
}
2007-11-05 18:00:22 +00:00
{ function TFacturasClienteController. Anadir( AFactura: IBizFacturaCliente;
const IDPedido: Integer ) : Boolean ;
var
APedidosController : IPedidosClienteController;
APedido : IBizPedidoCliente;
begin
Result : = False ;
if not Assigned( AFactura) then
raise Exception. Create ( 'Factura no asignada (Anadir)' ) ;
if ( IDPedido < 0 ) or ( IDPedido = 0 ) then
raise Exception. Create ( Format( 'ID de pedido (%d) incorrecto (Anadir)' , [ IDPedido] ) ) ;
if not AFactura. DataTable. Active then
AFactura. DataTable. Active : = True ;
ShowHourglassCursor;
Application. ProcessMessages;
APedido : = NIL ;
APedidosController : = TPedidosClienteController. Create;
try
APedido : = APedidosController. Buscar( IDPedido) ;
if not Assigned( APedido) then
raise Exception. Create ( Format( 'No se ha encontrado un pedido de cliente con ID %d (Anadir)' , [ IDPedido] ) ) ;
APedido. DataTable. Active : = True ;
APedidosController. RecuperarCliente( APedido) ;
Self. Anadir( AFactura) ;
AFactura. Cliente : = APedido. Cliente;
CopiarArticulosPedido( APedido. Detalles, AFactura. Detalles) ;
// Guardo la factura que acabo de generar
AFactura. CalcularImporteTotal;
Self. Guardar( AFactura) ;
Result : = True ;
finally
APedido : = NIL ;
APedidosController : = NIL ;
HideHourglassCursor;
Application. ProcessMessages;
end ;
end ; }
function TFacturasClienteController. Buscar( const ID: Integer ) : IBizFacturaCliente;
begin
Result : = ( FDataModule as IDataModuleFacturasCliente) . GetItem( ID) ;
2009-02-27 10:44:29 +00:00
FiltrarEmpresa( Result ) ;
2007-11-05 18:00:22 +00:00
end ;
2015-02-05 17:51:58 +00:00
function TFacturasClienteController. BuscarDePresupuesto( const AID_Presupuesto: Integer ) : IBizFacturaCliente;
var
Condicion: TDAWhereExpression;
begin
ShowHourglassCursor;
try
Result : = BuscarTodos;
with Result . DataTable. DynamicWhere do
begin
// ID_CLIENTE
Condicion : = NewBinaryExpression( NewField( '' , fld_FacturasClienteID_PRESUPUESTO) , NewConstant( AID_Presupuesto, datInteger) , dboEqual) ;
if IsEmpty then
Expression : = Condicion
else
Expression : = NewBinaryExpression( Expression, Condicion, dboAnd) ;
end ;
finally
HideHourglassCursor;
end ;
end ;
2007-11-05 18:00:22 +00:00
function TFacturasClienteController. BuscarTodasPendientesComision( IdAgente: Integer ; IdComision: Integer ; IdFacturasAsociadas: String ) : IBizFacturaCliente;
2007-11-18 17:23:37 +00:00
var
Condicion: TDAWhereExpression;
2007-11-05 18:00:22 +00:00
begin
ShowHourglassCursor;
try
Result : = BuscarTodos;
2007-11-18 17:23:37 +00:00
with Result . DataTable. DynamicWhere do
2007-11-05 18:00:22 +00:00
begin
//Todas las facturas de un agente determinado
2007-11-18 17:23:37 +00:00
Condicion : = NewBinaryExpression( NewField( '' , fld_FacturasClienteID_AGENTE) , NewConstant( IdAgente, datInteger) , dboEqual) ;
if IsEmpty then
Expression : = Condicion
else
Expression : = NewBinaryExpression( Expression, Condicion, dboAnd) ;
end ;
2007-11-05 18:00:22 +00:00
2007-11-18 17:23:37 +00:00
with Result . DataTable. DynamicWhere do
begin
2007-11-05 18:00:22 +00:00
//Todas aquellas que no esten asociadas a ninguna comisi<73> n o asociadas a la comisi<73> n,
//pero no asociadas en el editor de la comision, esto es porque se puede agregar y quitar
//facturas y todos los cambios estan el cache y por lo tanto al pedir las facturas a seleccionar
//debemos tener en cuenta dichso cambios en cache
2007-11-18 17:23:37 +00:00
Condicion : = NewBinaryExpression( NewField( '' , fld_FacturasClienteID_COMISION_LIQUIDADA) , NewNull( ) , dboEqual) ;
if IsEmpty then
Expression : = Condicion
else
Expression : = NewBinaryExpression( Expression, Condicion, dboAnd) ;
end ;
{ REPASARRRRRRR
//Quitamos aquellas que ya est<73> n asociadas
if length( IdFacturasAsociadas) > 0 then
with Result . DataTable. DynamicWhere do
begin
Condicion : = NewBinaryExpression( NewField( '' , fld_FacturasClienteID) , ' not in (' + IdFacturasAsociadas + ')' ) ; , dboEqual) ;
if IsEmpty then
Expression : = Condicion
else
Expression : = NewBinaryExpression( Expression, Condicion, dboAnd) ;
end ;
2007-11-05 18:00:22 +00:00
2007-11-18 17:23:37 +00:00
with Result . DataTable. Where do
begin
CLOSEBraket;
2007-11-05 18:00:22 +00:00
AddOperator( opOR) ;
OpenBraket;
//En caso de ser facturas ya asociadas lo limitamos a la comision actual
//ya que no debemos poder elegir facturas asociadas a otras comisiones
OpenBraket;
AddText( fld_FacturasClienteID_COMISION_LIQUIDADA + ' = ' + IntToStr( IdComision) ) ;
CloseBraket;
//En el caso de que halla facturas asociadas
if length( IdFacturasAsociadas) > 0 then
begin
AddOperator( opAND) ;
OpenBraket;
AddText( fld_FacturasClienteID + ' not in (' + IdFacturasAsociadas + ')' ) ;
CloseBraket;
end ;
CloseBraket;
CloseBraket;
end ;
2007-11-18 17:23:37 +00:00
}
2007-11-05 18:00:22 +00:00
finally
HideHourglassCursor;
end ;
end ;
2008-11-10 11:29:57 +00:00
function TFacturasClienteController. BuscarTodos(
const AID_Cliente: Integer ) : IBizFacturaCliente;
var
Condicion: TDAWhereExpression;
begin
ShowHourglassCursor;
try
Result : = BuscarTodos;
with Result . DataTable. DynamicWhere do
begin
// ID_CLIENTE
Condicion : = NewBinaryExpression( NewField( '' , fld_FacturasClienteID_CLIENTE) , NewConstant( AID_Cliente, datInteger) , dboEqual) ;
if IsEmpty then
Expression : = Condicion
else
Expression : = NewBinaryExpression( Expression, Condicion, dboAnd) ;
end ;
finally
HideHourglassCursor;
end ;
end ;
2007-11-05 18:00:22 +00:00
function TFacturasClienteController. BuscarTodos: IBizFacturaCliente;
begin
Result : = FDataModule. GetItems;
FiltrarEmpresa( Result ) ;
end ;
constructor TFacturasClienteController. Create;
begin
2007-11-22 19:09:07 +00:00
inherited ;
2007-11-05 18:00:22 +00:00
FDataModule : = TDataModuleFacturasCliente. Create( Nil ) ;
FClienteController : = TClientesController. Create;
FDetallesController : = TDetallesFacturaClienteController. Create;
FDetallesController. addObservador( Self) ;
end ;
function TFacturasClienteController. CreateEditor( const AName: String ;
const IID: TGUID; out Intf) : Boolean ;
begin
Result : = Supports( EditorRegistry. CreateEditor( AName) , IID, Intf) ;
end ;
2009-02-27 10:44:29 +00:00
function TFacturasClienteController. DarListaAnosFacturas: TStringList;
begin
Result : = FDataModule. GetAnosItems;
end ;
2007-11-05 18:00:22 +00:00
procedure TFacturasClienteController. DescartarCambios( AFactura: IBizFacturaCliente) ;
begin
if not Assigned( AFactura) then
raise Exception. Create ( 'Factura no asignada' ) ;
ShowHourglassCursor;
try
if ( AFactura. State in dsEditModes) then
AFactura. Cancel;
AFactura. DataTable. CancelUpdates;
finally
HideHourglassCursor;
end ;
end ;
destructor TFacturasClienteController. Destroy;
begin
FDataModule : = Nil ;
FClienteController : = Nil ;
FDetallesController : = Nil ;
inherited ;
end ;
function TFacturasClienteController. Duplicar( AFactura: IBizFacturaCliente) : IBizFacturaCliente;
begin
Result : = Self. _Vacio;
ShowHourglassCursor;
try
DuplicarRegistros( AFactura. DataTable, Result . DataTable, mdrActual) ;
DuplicarRegistros( AFactura. Detalles. DataTable, Result . Detalles. DataTable, mdrTodos) ;
// Hay que dejar algunos campos como si fuera una factura nueva
Result . Edit;
with Result do
begin
2007-11-18 15:07:51 +00:00
ID_EMPRESA : = AppFactuGES. EmpresaActiva. ID;
USUARIO : = AppFactuGES. UsuarioActivo. UserName;
2007-11-05 18:00:22 +00:00
REFERENCIA : = '' ; //Para que se asigne una nueva
SITUACION : = CTE_PENDIENTE; //Al ser una nueva debe de estar pendiente
FECHA_FACTURA : = DateOf( Now) ;
ID_COMISION_LIQUIDADA : = 0 ;
end ;
Result . Post;
finally
HideHourglassCursor;
end ;
end ;
function TFacturasClienteController. ValidarFactura( AFactura: IBizFacturaCliente) : Boolean ;
2008-12-17 19:07:42 +00:00
var
AFormaPago: IBizFormaPago;
2007-11-05 18:00:22 +00:00
begin
if not Assigned( AFactura) then
raise Exception. Create ( 'Factura no asignada' ) ;
if ( AFactura. DataTable. State in dsEditModes) then
AFactura. DataTable. Post;
//Tambien hacemos post de sus tablas hija
if ( AFactura. Detalles. DataTable. State in dsEditModes) then
AFactura. Detalles. DataTable. Post;
if ( AFactura. ID_CLIENTE < 0 ) or ( AFactura. ID_CLIENTE = 0 ) then
// No comprobar el objeto Cliente por que puede fallar la validaci<63> n
// cuando se generan facturas autom<6F> ticamente.
{ ( not Assigned( AFactura. Cliente) ) or
( AFactura. Cliente. IsEmpty) then }
raise Exception. Create( 'Debe indicar el cliente de esta factura' ) ;
if ( EsFechaVacia( AFactura. FECHA_FACTURA) ) then
raise Exception. Create( 'Debe indicar la fecha de esta factura' ) ;
2009-01-16 20:07:01 +00:00
//En el caso de asignar una retenci<63> n a la factura ser<65> obligatorio meter la fecha de retenci<63> n.
if ( AFactura. IMPORTE_RETENCION > 0 ) then
if ( EsFechaVacia( AFactura. FECHA_RETENCION) ) then
raise Exception. Create( 'Debe indicar la fecha de vencimiento de la retenci<63> n, para poder asociarla al recibo que contendr<64> el importe de la retenci<63> n.' ) ;
2007-11-05 18:00:22 +00:00
if ( AFactura. Detalles. DataTable. RecordCount = 0 ) then
raise Exception. Create( 'La factura debe tener al menos un concepto en su contenido' ) ;
{ Esta validaci<EFBFBD> n puede saltar cuando se generan facturas autom<EFBFBD> ticamente
por albaranes o pedidos y el cliente no tiene Tipo de IVA puesto. }
{ if ( AFactura. ID_TIPO_IVA = 0 ) then
raise Exception. Create( 'Debe indicar un tipo de IVA para esta factura' ) ; }
2008-12-17 19:07:42 +00:00
//De esta forma obligaremos siempre a tener un recibo asociado a la factura,
//porque si la forma de pago no tiene plazos es obligatorio la fecha de vencimiento
with TFormasPagoController. Create do
begin
try
AFormaPago : = Buscar( AFactura. ID_FORMA_PAGO) ;
AFormaPago. DataTable. Active : = True ;
if ( AFormaPago. Plazos. RecordCount = 0 )
and ( EsFechaVacia( AFactura. FECHA_VENCIMIENTO) ) then
raise Exception. Create( 'Debe indicar una fecha de vencimiento para esta factura' ) ;
finally
AFormaPago : = NIL ;
Free;
end ;
end ;
2007-11-05 18:00:22 +00:00
//En caso de ser un Abono no podra tener un importe total positivo
if ( AFactura. TIPO = CTE_TIPO_ABONO) then
2008-08-05 14:25:25 +00:00
if ( AFactura. IMPORTE_TOTAL > 0 ) then
2008-05-26 14:57:35 +00:00
raise Exception. Create( 'Un abono nunca no puede tener un importe positivo, asegurese que las cantidades sean negativas' ) ;
2007-11-05 18:00:22 +00:00
{ Asegurarse de valores en campos "autom<6F> ticos" tanto
en MODIFICACI<EFBFBD> N como en INSERCI<EFBFBD> N. }
AFactura. Edit;
try
2007-11-18 15:07:51 +00:00
AFactura. USUARIO : = AppFactuGES. UsuarioActivo. UserName;
2007-11-05 18:00:22 +00:00
if Assigned( AFactura. Cliente)
and ( AFactura. ID_CLIENTE < > AFactura. Cliente. ID) then
AFactura. ID_CLIENTE : = AFactura. Cliente. ID;
Result : = True ;
finally
AFactura. Post;
end ;
end ;
procedure TFacturasClienteController. Ver( AFactura: IBizFacturaCliente) ;
var
AEditor : IEditorFacturaCliente;
begin
AEditor : = NIL ;
2008-11-11 16:22:18 +00:00
RecuperarCliente( AFactura) ;
CreateEditor( 'EditorFacturaCliente' , IEditorFacturaCliente, AEditor) ;
2007-11-05 18:00:22 +00:00
2008-11-11 16:22:18 +00:00
if Assigned( AEditor) then
2008-11-13 13:10:32 +00:00
try
AEditor. Controller : = Self; //OJO ORDEN MUY IMPORTANTE
AEditor. Factura : = AFactura;
//MODO CONSULTAR
if not EsModificable( AFactura) then
begin
SetDataTableReadOnly( AFactura. DataTable, True ) ;
AEditor. ReadOnly : = True ;
2007-11-05 18:00:22 +00:00
end ;
2008-11-13 13:10:32 +00:00
AEditor. ShowModal;
//MODO CONSULTAR (Se deja la tabla como estaba)
if AEditor. ReadOnly then
SetDataTableReadOnly( AFactura. DataTable, False ) ;
finally
AEditor. Release;
AEditor : = NIL ;
2008-11-11 16:22:18 +00:00
end ;
2007-11-05 18:00:22 +00:00
end ;
2008-11-10 11:29:57 +00:00
procedure TFacturasClienteController. VerTodos( AFacturas: IBizFacturaCliente;
const AVerModal : Boolean = False ; const AWindowCaption: String = '' ;
const AHeaderText: String = '' ) ;
2007-11-05 18:00:22 +00:00
var
AEditor : IEditorFacturasCliente;
begin
AEditor : = NIL ;
2008-11-11 16:22:18 +00:00
CreateEditor( 'EditorFacturasCliente' , IEditorFacturasCliente, AEditor) ;
if Assigned( AEditor) then
2008-11-13 13:10:32 +00:00
try
if not EsCadenaVacia( AWindowCaption) then
AEditor. WindowCaption : = AWindowCaption;
if not EsCadenaVacia( AHeaderText) then
AEditor. HeaderText : = AHeaderText;
AEditor. Controller : = Self; //OJO ORDEN MUY IMPORTANTE
AEditor. Facturas : = AFacturas;
AEditor. MultiSelect : = True ;
if AVerModal then
AEditor. ShowModal
else
AEditor. ShowEmbedded;
finally
if AVerModal then
AEditor. Release;
AEditor : = Nil ;
2008-11-11 16:22:18 +00:00
end ;
2007-11-05 18:00:22 +00:00
end ;
function TFacturasClienteController. _Vacio: IBizFacturaCliente;
begin
Result : = Buscar( ID_NULO) ;
end ;
function TFacturasClienteController. Eliminar( const ID: Integer ) : Boolean ;
var
AFactura : IBizFacturaCliente;
begin
AFactura : = Buscar( ID) ;
if not Assigned( AFactura) then
raise Exception. Create( Format( 'No se ha encontrado la factura con ID = %d' , [ ID] ) ) ;
Result : = Eliminar( AFactura) ;
AFactura : = NIL ;
end ;
function TFacturasClienteController. ElegirFacturas( AFacturas: IBizFacturaCliente; AMensaje: String ;
AMultiSelect: Boolean ) : IBizFacturaCliente;
var
AEditor : IEditorElegirFacturasCliente;
begin
Result : = NIL ;
2008-11-11 16:22:18 +00:00
CreateEditor( 'EditorElegirFacturasCliente' , IEditorElegirFacturasCliente, AEditor) ;
if Assigned( AEditor) then
2008-11-13 13:10:32 +00:00
try
AEditor. Controller : = Self;
AEditor. Facturas : = AFacturas;
AEditor. MultiSelect : = AMultiSelect;
AEditor. Mensaje : = AMensaje;
if IsPositiveResult( AEditor. ShowModal) then
Result : = AEditor. FacturasClienteSeleccionados;
finally
AEditor. Release;
AEditor : = NIL ;
2008-11-11 16:22:18 +00:00
end ;
2007-11-05 18:00:22 +00:00
end ;
function TFacturasClienteController. Eliminar( AFactura: IBizFacturaCliente; AllItems: Boolean = false ) : Boolean ;
//En el caso de eliminar almenos un elemento del conjunto se devuelve true
var
bEliminado: Boolean ;
begin
bEliminado : = False ;
if not Assigned( AFactura) then
raise Exception. Create ( 'Factura no asignada' ) ;
ShowHourglassCursor;
try
if not AFactura. DataTable. Active then
AFactura. DataTable. Active : = True ;
if ( AFactura. State in dsEditModes) then
AFactura. Cancel;
//Siempre eliminaremos el seleccionado
if EsEliminable( AFactura) then
begin
AFactura. Delete;
bEliminado : = True ;
end ;
//En el caso de querer eliminar todos los items del objeto AAlbaran
if AllItems then
begin
with AFactura. DataTable do
begin
First;
while not EOF do
begin
if EsEliminable( AFactura) then
begin
AFactura. Delete;
bEliminado : = True
end
else Next;
end ;
end ;
end ;
if bEliminado then
begin
2008-09-19 09:23:41 +00:00
try
AFactura. DataTable. ApplyUpdates;
Result : = True
except
//En el caso de una factura que tiene recibos con devoluciones hechas no se puede borrar aunque la factura este en situacion de pendiente
AFactura. DataTable. CancelUpdates;
Result : = False ;
end ;
2007-11-05 18:00:22 +00:00
end
else
Result : = False ;
finally
HideHourglassCursor;
end ;
end ;
2010-09-23 10:49:10 +00:00
function TFacturasClienteController. EnviarEmailFacturas( AFacturas: IBizFacturaCliente) : Boolean ;
var
ADialog : IDialogListaFacturasClienteEnvioEMail;
ARespuesta : Integer ;
begin
ADialog : = NIL ;
if not Assigned( AFacturas) then
raise Exception. Create ( 'Facturas no asignadas (EnviarFacturasPorEMail)' ) ;
if AFacturas. DataTable. Active then
AFacturas. DataTable. Active : = True ;
ShowHourglassCursor;
try
CreateEditor( 'DialogListaFacturasClienteEnvioEMail' , IDialogListaFacturasClienteEnvioEMail, ADialog) ;
if Assigned( ADialog) then
begin
try
ADialog. Facturas : = AFacturas;
ARespuesta : = ADialog. ShowModal;
Result : = ( ARespuesta = mrOK)
finally
ADialog. Release;
end ;
end ;
finally
ADialog : = NIL ;
HideHourglassCursor;
end ;
end ;
function TFacturasClienteController. EnviarFacturaPorEMail( AFactura : IBizFacturaCliente;
const AEnviarDirectamente: Boolean = True ;
const ADireccionEMail: String = '' ; const AAsuntoEMail: String = '' ;
const ATextoEMail: String = '' ) : Boolean ;
2008-08-25 11:08:44 +00:00
var
AReportController : IFacturasClienteReportController;
AFicheroTMP : TFileName;
AEMail : String ;
AListaEmail : TStringList;
begin
if not Assigned( AFactura) then
2010-09-23 10:49:10 +00:00
raise Exception. Create ( 'Presupuesto no asignado (EnviarPresupuestoPorEMail)' ) ;
2008-08-25 11:08:44 +00:00
if AFactura. DataTable. Active then
AFactura. DataTable. Active : = True ;
2010-09-23 10:49:10 +00:00
RecuperarCliente( AFactura) ;
AFactura. Cliente. DataTable. Active : = True ;
2008-08-25 11:08:44 +00:00
2010-09-23 10:49:10 +00:00
AFicheroTMP : = DarFicheroPDFTemporal( EscapeIllegalChars( AFactura. REFERENCIA) ) ;
2008-08-25 11:08:44 +00:00
2010-09-23 10:49:10 +00:00
if not EsCadenaVacia( ADireccionEMail) then
AEMail : = ADireccionEMail
else begin
AListaEmail : = TStringList. Create;
try
if not AFactura. Cliente. EMAIL_1IsNull then
AListaEmail. Add( AFactura. Cliente. EMAIL_1) ;
if not AFactura. Cliente. EMAIL_2IsNull then
AListaEmail. Add( AFactura. Cliente. EMAIL_2) ;
2008-08-25 11:08:44 +00:00
2010-09-23 10:49:10 +00:00
if not ElegirEMail( AListaEmail, AEMail) then
Exit;
finally
FreeANDNIL( AListaEmail) ;
end ;
2008-08-25 11:08:44 +00:00
end ;
ShowHourglassCursor;
AReportController : = TFacturasClienteReportController. Create;
try
AReportController. ExportToPDF( AFactura. ID, AFicheroTMP) ;
2010-09-23 10:49:10 +00:00
Result : = EnviarEMailMAPI( 'Factura ' + AFactura. REFERENCIA, '' , AFicheroTMP, '' , '' , AFactura. Cliente. NOMBRE, AEMail, AEnviarDirectamente) ;
2008-08-25 11:08:44 +00:00
finally
2010-09-23 10:49:10 +00:00
DeleteFile( AFicheroTMP) ;
2008-08-25 11:08:44 +00:00
AReportController : = NIL ;
HideHourglassCursor;
end ;
end ;
2007-11-05 18:00:22 +00:00
function TFacturasClienteController. EsEliminable( AFactura: IBizFacturaCliente) : Boolean ;
begin
Result : = EsModificable( AFactura) ;
end ;
function TFacturasClienteController. EsModificable( AFactura: IBizFacturaCliente) : Boolean ;
begin
if not Assigned( AFactura) then
raise Exception. Create ( 'Factura no asignado: EsModificable' ) ;
Result : = ( AFactura. SITUACION = CTE_PENDIENTE) ;
end ;
procedure TFacturasClienteController. RecalcularImportes(
AFactura: IBizFacturaCliente) ;
var
bEnEdicion : Boolean ;
2007-12-20 20:36:56 +00:00
ADetallePosAct : Integer ;
2007-11-05 18:00:22 +00:00
begin
if not Assigned( AFactura) then
raise Exception. Create ( 'Factura no asignado (RecalcularImportes)' ) ;
if AFactura. DataTable. Active then
AFactura. DataTable. Active : = True ;
2007-12-20 21:07:01 +00:00
{ Hay que guardar la posici<EFBFBD> n en la que estamos en los detalles por que
la asignaci<EFBFBD> n de valores a los campos IMPORTE_NETO e IMPORTE_PORTE
( ver m<EFBFBD> s adelante) colocan el puntero en la tabla detalle al principio.
No he encontrado la raz<EFBFBD> n por la que mueve el puntero. }
2007-12-20 20:36:56 +00:00
ADetallePosAct : = AFactura. Detalles. POSICION;
2007-11-05 18:00:22 +00:00
bEnEdicion : = ( AFactura. DataTable. State in dsEditModes) ;
if not bEnEdicion then
AFactura. Edit;
ShowHourglassCursor;
try
AFactura. IMPORTE_NETO : = FDetallesController. DarTotalImporteTotal( AFactura. Detalles) ;
AFactura. IMPORTE_PORTE : = FDetallesController. DarTotalPorteTotal( AFactura. Detalles) ;
2007-12-20 21:07:01 +00:00
2007-11-05 18:00:22 +00:00
if not bEnEdicion then
AFactura. Post;
finally
HideHourglassCursor;
2007-12-20 21:07:01 +00:00
// Restaurar la posici<63> n que ten<65> amos en los detalles.
FDetallesController. LocalizarPosicion( AFactura. Detalles, ADetallePosAct) ;
2007-11-05 18:00:22 +00:00
end ;
end ;
procedure TFacturasClienteController. RecibirAviso( ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable) ;
var
AFactura : IBizFacturaCliente;
ADetalles : IBizDetallesFacturaCliente;
begin
inherited ;
if Supports( ADataTable, IBizDetallesFacturaCliente, ADetalles) and
Supports( ADetalles. DataTable. MasterSource. DataTable, IBizFacturaCliente, AFactura) then
begin
RecalcularImportes( AFactura) ;
end ;
end ;
procedure TFacturasClienteController. RecuperarCliente(
AFactura: IBizFacturaCliente) ;
begin
AFactura. _Cliente : = ( FClienteController. Buscar( AFactura. ID_CLIENTE) as IBizCliente) ;
end ;
function TFacturasClienteController. Existe( const ID: Integer ) : Boolean ;
var
AFactura : IBizFacturaCliente;
begin
try
AFactura : = Buscar( ID) ;
Result : = Assigned( AFactura) and ( AFactura. ID = ID) ;
finally
AFactura : = NIL ;
end ;
end ;
function TFacturasClienteController. ExtraerSeleccionados( AFacturasCliente: IBizFacturaCliente) : IBizFacturaCliente;
var
ASeleccionados : IBizFacturaCliente;
begin
ASeleccionados : = ( Self. Buscar( ID_NULO) as IBizFacturaCliente) ;
2008-05-21 15:15:41 +00:00
CopyDataTableDA5( AFacturasCliente. DataTable, ASeleccionados. DataTable, True ) ;
2007-11-05 18:00:22 +00:00
Result : = ASeleccionados;
end ;
2009-02-27 10:44:29 +00:00
procedure TFacturasClienteController. FiltrarAno( AFactura: IBizFacturaCliente; ADynWhereDataTable: WideString ; const Ano: String ) ;
var
Condicion: TDAWhereExpression;
FechaIni: String ;
FechaFin: String ;
begin
AFactura. DataTable. DynamicWhere. Clear;
AFactura. DataTable. DynamicWhere. Xml : = ADynWhereDataTable;
if ( Ano < > 'Todos' ) then
begin
// Filtrar las facturas actuales por empresa
FechaIni : = '01/01/' + Ano;
FechaFin : = '31/12/' + Ano;
with AFactura. DataTable. DynamicWhere do
begin
// (FECHA_INICIO between FECHA_FIN)
Condicion : = NewBinaryExpression( NewField( '' , fld_FacturasClienteFECHA_FACTURA) , NewConstant( FechaIni, 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 ;
end ;
end ;
2007-11-05 18:00:22 +00:00
procedure TFacturasClienteController. FiltrarEmpresa( AFactura: IBizFacturaCliente) ;
2007-11-18 17:23:37 +00:00
var
Condicion: TDAWhereExpression;
2007-11-05 18:00:22 +00:00
begin
if AFactura. DataTable. Active then
AFactura. DataTable. Active : = False ;
2007-11-18 17:23:37 +00:00
2007-11-05 18:00:22 +00:00
// Filtrar las facturas actuales por empresa
2007-11-18 17:23:37 +00:00
with AFactura. DataTable. DynamicWhere do
2007-11-05 18:00:22 +00:00
begin
2007-11-18 17:23:37 +00:00
// (ID_EMPRESA >= ID)
Condicion : = NewBinaryExpression( NewField( '' , fld_FacturasClienteID_EMPRESA) , NewConstant( AppFactuGES. EmpresaActiva. ID, datInteger) , dboEqual) ;
if IsEmpty then
Expression : = Condicion
else
Expression : = NewBinaryExpression( Expression, Condicion, dboAnd) ;
2007-11-05 18:00:22 +00:00
end ;
end ;
procedure TFacturasClienteController. SetClienteController( const Value: IClientesController) ;
begin
FClienteController : = Value;
end ;
procedure TFacturasClienteController. SetDetallesController( const Value: IDetallesFacturaClienteController) ;
begin
FDetallesController : = Value;
end ;
function TFacturasClienteController. Guardar( AFactura: IBizFacturaCliente) : Boolean ;
begin
Result : = False ;
if not Assigned( AFactura) then
raise Exception. Create ( 'Factura no asignada' ) ;
if not Assigned( FDetallesController) then
raise Exception. Create ( 'Controller detalles no asignado' ) ;
if ValidarFactura( AFactura) then
begin
ShowHourglassCursor;
// Asegurarnos de que todos los importes est<73> n bien.
RecalcularImportes( AFactura) ;
try
AFactura. DataTable. ApplyUpdates;
//Se generan los recibos autom<6F> ticamente a partir de la forma de pago
2007-11-14 19:41:42 +00:00
GenerarRecibos( AFactura) ;
2007-11-05 18:00:22 +00:00
Result : = True ;
finally
HideHourglassCursor;
end ;
end ;
end ;
2008-01-15 10:31:41 +00:00
function TFacturasClienteController. Nuevo( withInsert: Boolean = True ) : IBizFacturaCliente;
2007-11-05 18:00:22 +00:00
var
AFactura : IBizFacturaCliente;
begin
AFactura : = FDataModule. NewItem;
FiltrarEmpresa( AFactura) ;
AFactura. DataTable. Active : = True ;
2008-01-15 10:31:41 +00:00
if WithInsert then
AFactura. Insert;
2007-11-05 18:00:22 +00:00
Result : = AFactura;
end ;
2008-11-13 10:33:06 +00:00
procedure TFacturasClienteController. Preview( AFactura: IBizFacturaCliente; AllItems: Boolean = false ;
2009-04-17 09:34:26 +00:00
const VerSello: Boolean = True ; const VerCopia: Boolean = True ) ;
2007-11-05 18:00:22 +00:00
var
AReportController : IFacturasClienteReportController;
2008-08-22 14:52:35 +00:00
ID_Facturas: TIntegerList;
2007-11-05 18:00:22 +00:00
begin
AReportController : = TFacturasClienteReportController. Create;
2008-08-22 14:52:35 +00:00
ID_Facturas : = TIntegerList. Create;
2007-11-05 18:00:22 +00:00
2008-05-20 18:50:02 +00:00
try
2007-11-05 18:00:22 +00:00
//Si deseamos previsualizar todos los items del objeto albaran
if AllItems then
begin
with AFactura. DataTable do
begin
First;
while not EOF do
begin
2008-08-22 14:52:35 +00:00
ID_Facturas. Add( AFactura. ID) ;
2007-11-05 18:00:22 +00:00
Next;
end ;
end ;
end
//Solo previsualizamos el item seleccionado
else
2008-08-22 14:52:35 +00:00
ID_Facturas. Add( AFactura. ID) ;
2007-11-05 18:00:22 +00:00
2009-04-17 09:34:26 +00:00
AReportController. Preview( ID_Facturas, VerSello, VerCopia) ;
2007-11-05 18:00:22 +00:00
finally
AReportController : = NIL ;
2008-08-22 14:52:35 +00:00
FreeANDNIL( ID_Facturas) ;
2007-11-05 18:00:22 +00:00
end ;
end ;
2008-11-13 10:33:06 +00:00
procedure TFacturasClienteController. Print( AFactura: IBizFacturaCliente; AllItems: Boolean = false ;
2009-04-17 09:34:26 +00:00
const VerSello: Boolean = True ; const VerCopia: Boolean = True ) ;
2007-11-05 18:00:22 +00:00
var
AReportController : IFacturasClienteReportController;
2008-08-22 14:52:35 +00:00
ID_Facturas: TIntegerList;
2007-11-05 18:00:22 +00:00
begin
AReportController : = TFacturasClienteReportController. Create;
2008-08-22 14:52:35 +00:00
ID_Facturas : = TIntegerList. Create;
2007-11-05 18:00:22 +00:00
2008-05-20 18:50:02 +00:00
try
2007-11-05 18:00:22 +00:00
//Si deseamos previsualizar todos los items del objeto albaran
if AllItems then
begin
with AFactura. DataTable do
begin
First;
while not EOF do
begin
2008-08-22 14:52:35 +00:00
ID_Facturas. Add( AFactura. ID) ;
2007-11-05 18:00:22 +00:00
Next;
end ;
end ;
end
//Solo previsualizamos el item seleccionado
else
2008-08-22 14:52:35 +00:00
ID_Facturas. Add( AFactura. ID) ;
2007-11-05 18:00:22 +00:00
2009-04-17 09:34:26 +00:00
AReportController. Print( ID_Facturas, VerSello, VerCopia) ;
2007-11-05 18:00:22 +00:00
finally
AReportController : = NIL ;
2008-08-22 14:52:35 +00:00
FreeANDNIL( ID_Facturas) ;
2007-11-05 18:00:22 +00:00
end ;
end ;
function TFacturasClienteController. GenerarAbono( AFactura: IBizFacturaCliente) : IBizFacturaCliente;
begin
ShowHourglassCursor;
try
Result : = Duplicar( AFactura) ;
//A<> ade un concepto con los datos de la factura asociada al abono
FDetallesController. AnadirDetalleFacturaAsociadaAbono( Result . Detalles, AFactura. REFERENCIA, DateToStr( AFactura. FECHA_FACTURA) ) ;
//Convierte todos los articulos de la factura a negativos por se un abono
FDetallesController. CambiarSignoDetalles( Result . Detalles) ;
// Hay que dejar algunos campos como si fuera una factura nueva
Result . Edit;
Result . TIPO : = CTE_TIPO_ABONO;
Result . Post;
2008-09-25 17:28:31 +00:00
2007-11-05 18:00:22 +00:00
finally
2010-09-23 10:49:10 +00:00
HideHourglassCursor;
end ;
end ;
function TFacturasClienteController. GenerarEmailFactura( AFactura: IBizFacturaCliente) : Boolean ;
begin
if not Assigned( AFactura) then
raise Exception. Create ( 'Factura no asignadas (GenerarEmailFactura)' ) ;
if AFactura. DataTable. Active then
AFactura. DataTable. Active : = True ;
ShowHourglassCursor;
try
RecuperarCliente( AFactura) ;
EnviarFacturaPorEMail( AFactura, False ) ;
finally
2007-11-05 18:00:22 +00:00
HideHourglassCursor;
end ;
end ;
procedure TFacturasClienteController. GenerarRecibos( AFactura: IBizFacturaCliente) ;
var
AFormasPagoController : IFormasPagoController;
AFormaPago: IBizFormaPago;
ARecibosClienteController: IRecibosClienteController;
ARecibos: IBizRecibosCliente;
AFechaVencimiento: TDateTime;
i: Integer ;
2008-09-25 17:28:31 +00:00
ADiaVencimiento: Integer ;
ADiasMas: Integer ;
2009-01-12 18:40:25 +00:00
BSemaforo: Boolean ;
2009-01-15 13:05:03 +00:00
AImporteTotal: Double ;
2008-09-25 17:28:31 +00:00
2007-11-05 18:00:22 +00:00
begin
if not Assigned( AFactura) then
Exit;
AFormasPagoController : = TFormasPagoController. Create;
AFormaPago : = AFormasPagoController. Buscar( AFactura. ID_FORMA_PAGO) ;
AFormaPago. DataTable. Active : = True ;
2015-06-11 08:37:46 +00:00
// if AFormaPago.DataTable.RecordCount <> 1 then
// Exit; //No hay forma de pago en la factura y por lo tanto no se generan recibos
2007-11-05 18:00:22 +00:00
//raise Exception.Create('No existe la forma de pago de la factura');
ARecibosClienteController : = TRecibosClienteController. Create;
//Eliminamos todos los recibos que tuviera la factura porque sabemos que todos
//estar<61> n pendientes (solo permitiremos modificar y eliminar facturas pendientes,
//parcialmente pagadas o pagadas no
ARecibos : = ARecibosClienteController. BuscarRecibosFactura( AFactura. ID) ;
ARecibosClienteController. EliminarTodo( ARecibos) ;
2009-01-15 13:05:03 +00:00
//Si hay retenci<63> n, se generar<61> un recibo por ese importe, y el resto se generar<61> n recibos
//seg<65> n forma de pago
AImporteTotal : = AFactura. IMPORTE_TOTAL - AFactura. IMPORTE_RETENCION;
2007-11-05 18:00:22 +00:00
//Vamos a generar todos los recibos necesarios para la factura
With AFormaPago. Plazos. DataTable do
begin
i : = 1 ;
First;
2008-12-17 19:07:42 +00:00
repeat
2007-11-05 18:00:22 +00:00
ARecibos : = ARecibosClienteController. Nuevo;
ARecibos. Edit;
ARecibos. ID_FACTURA : = AFactura. ID;
ARecibos. REFERENCIA : = AFactura. REFERENCIA + ' - ' + IntToStr( i) ;
2008-09-25 17:28:31 +00:00
ARecibos. FECHA_EMISION : = AFactura. FECHA_FACTURA;
2007-11-05 18:00:22 +00:00
2008-12-17 19:07:42 +00:00
if AFormaPago. Plazos. RecordCount < 1 then
begin
ARecibos. FECHA_VENCIMIENTO : = AFactura. FECHA_VENCIMIENTO;
2009-01-15 13:05:03 +00:00
ARecibos. IMPORTE : = AImporteTotal;
2008-12-17 19:07:42 +00:00
end
else
2007-11-05 18:00:22 +00:00
begin
2008-12-17 19:07:42 +00:00
AFechaVencimiento : = AFactura. FECHA_FACTURA + AFormaPago. Plazos. NUM_DIAS;
ADiasMas : = 0 ;
2009-01-12 18:40:25 +00:00
BSemaforo : = False ;
2008-12-17 19:07:42 +00:00
if ( AFactura. Cliente. VENCIMIENTO_FACTURAS_1 < > 0 )
or ( AFactura. Cliente. VENCIMIENTO_FACTURAS_2 < > 0 )
or ( AFactura. Cliente. VENCIMIENTO_FACTURAS_3 < > 0 ) then
2008-09-25 17:28:31 +00:00
begin
2008-12-17 19:07:42 +00:00
ADiaVencimiento : = DayOf( AFechaVencimiento) ;
while ( ADiaVencimiento < > AFactura. Cliente. VENCIMIENTO_FACTURAS_1)
and ( ADiaVencimiento < > AFactura. Cliente. VENCIMIENTO_FACTURAS_2)
and ( ADiaVencimiento < > AFactura. Cliente. VENCIMIENTO_FACTURAS_3) do
begin
if ADiaVencimiento = DaysInMonth( AFechaVencimiento) then
2009-01-12 18:40:25 +00:00
begin
ADiaVencimiento : = 1 ;
//Controlamos con una variable semaforo que no nos quedemos en un bucle infinito
//intentando encontrar el dia de pago establecido para el cliente (es el caso de tener
//asignado el dia de pago 30 y llegar febrero en el que no se encuentra dicho dia, o
//tener como dia de pago los d<> as 31 y en el caso de meses de 30 dias no encontrarlo.
if BSemaforo then
begin
ADiasMas : = 0 ;
Break
end
else
BSemaforo : = True ;
end
2008-12-17 19:07:42 +00:00
else
Inc( ADiaVencimiento) ;
Inc( ADiasMas) ;
end ;
2008-09-25 17:28:31 +00:00
end ;
2008-12-17 19:07:42 +00:00
AFechaVencimiento : = IncDay( AFechaVencimiento, ADiasMas) ;
ARecibos. FECHA_VENCIMIENTO : = AFechaVencimiento;
2009-01-15 13:05:03 +00:00
ARecibos. IMPORTE : = AImporteTotal * ( AFormaPago. Plazos. PORCENTAJE / 1 0 0 ) ;
2007-11-05 18:00:22 +00:00
end ;
2008-06-04 16:12:28 +00:00
2008-06-04 17:32:05 +00:00
ARecibos. DESCRIPCION : = 'Pago de factura ' + AFactura. REFERENCIA + ': son ' + CifraToLetras( ARecibos. IMPORTE) ;
2007-11-05 18:00:22 +00:00
ARecibosClienteController. Guardar( ARecibos) ;
Inc( i) ;
Next;
2008-12-17 19:07:42 +00:00
until ( eof) ;
2009-01-15 13:05:03 +00:00
//Si hay retenci<63> n generaremos un recibo para dicha retenci<63> n
if AFactura. IMPORTE_RETENCION > 0 then
begin
ARecibos : = ARecibosClienteController. Nuevo;
ARecibos. Edit;
ARecibos. ID_FACTURA : = AFactura. ID;
ARecibos. REFERENCIA : = AFactura. REFERENCIA + ' - ' + IntToStr( i) ;
ARecibos. FECHA_EMISION : = AFactura. FECHA_FACTURA;
ARecibos. FECHA_VENCIMIENTO : = AFactura. FECHA_RETENCION;
ARecibos. IMPORTE : = AFactura. IMPORTE_RETENCION;
ARecibos. DESCRIPCION : = 'Pago de factura de retenci<63> n ' + AFactura. REFERENCIA + ': son ' + CifraToLetras( ARecibos. IMPORTE) ;
ARecibosClienteController. Guardar( ARecibos) ;
end ;
2007-11-05 18:00:22 +00:00
end ;
//Liberamos
AFormasPagoController : = Nil ;
AFormaPago : = Nil ;
ARecibosClienteController : = Nil ;
ARecibos : = Nil ;
end ;
function TFacturasClienteController. GetClienteController: IClientesController;
begin
Result : = FClienteController;
end ;
function TFacturasClienteController. GetDetallesController: IDetallesFacturaClienteController;
begin
Result : = FDetallesController;
end ;
end .