2007-09-12 14:11:44 +00:00
unit uClientesController;
interface
uses
2008-12-17 19:07:42 +00:00
uCustomEditor, Classes,
2007-09-12 14:11:44 +00:00
uContactosController, uBizContactos, uBizDireccionesContacto,
uIEditorClientes, uIEditorCliente, uIDataModuleContactos, uIDataModuleClientes,
2008-09-02 16:21:43 +00:00
uDireccionesContactoController, uBizContactosPersonal;
2007-09-12 14:11:44 +00:00
type
IClientesController = interface( IContactosController)
[ '{AAC3C51A-37F7-4961-B39F-FBC6B6A2B0F1}' ]
function BuscarTodosTiendaWeb: IBizCliente;
function TieneDatosBancarios( ACliente: IBizCliente) : Boolean ;
2008-09-03 09:21:08 +00:00
function ElegirDireccionEntrega( ACliente: IBizCliente; AMensaje: String ) : IBizDireccionesContacto;
2007-09-12 14:11:44 +00:00
function EsEliminable( ACliente: IBizContacto) : Boolean ;
function Eliminar( ACliente: IBizContacto; AllItems: Boolean = false ) : Boolean ; overload ;
procedure Preview( ACliente : IBizCliente; AllItems: Boolean = false ) ;
procedure Print( ACliente : IBizCliente; AllItems: Boolean = false ) ;
2008-11-10 11:29:57 +00:00
procedure VerPresupuestosDeCliente( ACliente : IBizCliente) ;
procedure VerAlbaranesDeCliente( ACliente : IBizCliente) ;
procedure VerFacturasDeCliente( ACliente : IBizCliente) ;
procedure VerRecibosDeCliente( ACliente : IBizCliente) ;
2008-12-17 19:07:42 +00:00
function DarListaCuentasBancarias( const IdCliente: Integer ) : TStringList;
2007-09-12 14:11:44 +00:00
end ;
TClientesController = class( TContactosController, IClientesController)
protected
FDireccionesController : IDireccionesContactoController;
function ValidarContacto( AContacto: IBizContacto) : Boolean ; override ;
public
constructor Create; override ;
destructor Destroy; override ;
function Duplicar( AContacto: IBizContacto) : IBizContacto; override ;
function Buscar( const ID: Integer ) : IBizContacto; override ;
function BuscarTodos: IBizContacto; override ;
function BuscarTodosTiendaWeb: IBizCliente;
function Nuevo : IBizContacto; override ;
procedure Ver( AContacto : IBizContacto) ; override ;
procedure VerTodos( AContactos: IBizContacto) ; override ;
function ElegirContacto( AContactos : IBizContacto;
AMensaje: String ; AMultiSelect: Boolean ) : IBizContacto; override ;
function TieneDatosBancarios( ACliente: IBizCliente) : Boolean ;
2008-09-03 09:21:08 +00:00
function ElegirDireccionEntrega( ACliente: IBizCliente;
2007-09-12 14:11:44 +00:00
AMensaje: String ) : IBizDireccionesContacto;
function EsEliminable( ACliente: IBizContacto) : Boolean ;
function Eliminar( ACliente: IBizContacto; AllItems: Boolean = false ) : Boolean ; overload ;
procedure Preview( ACliente : IBizCliente; AllItems: Boolean = false ) ;
procedure Print( ACliente : IBizCliente; AllItems: Boolean = false ) ;
2008-11-10 11:29:57 +00:00
procedure VerPresupuestosDeCliente( ACliente : IBizCliente) ;
procedure VerAlbaranesDeCliente( ACliente : IBizCliente) ;
procedure VerFacturasDeCliente( ACliente : IBizCliente) ;
procedure VerRecibosDeCliente( ACliente : IBizCliente) ;
2008-12-17 19:07:42 +00:00
function DarListaCuentasBancarias( const IdCliente: Integer ) : TStringList;
2007-09-12 14:11:44 +00:00
end ;
implementation
uses
2008-12-17 19:07:42 +00:00
Forms, Windows, SysUtils, Controls, cxControls, uDialogUtils, uDataModuleClientes, uEditorRegistryUtils,
2007-09-12 14:11:44 +00:00
uDataTableUtils, uDADataTable, DB, schContactosClient_Intf, uEtiquetasContactosReportController,
2008-11-10 11:29:57 +00:00
uIEditorElegirClientes, uIEditorElegirDireccionEntrega, uEditorGridBase, uModuleController,
2008-11-27 16:14:51 +00:00
Dialogs, uIntegerListUtils, uFactuGES_App,
2008-11-10 11:29:57 +00:00
uPlugins_Intf;
2007-09-12 14:11:44 +00:00
{ TClientesController }
function TClientesController. Buscar( const ID: Integer ) : IBizContacto;
begin
Result : = ( FDataModule as IDataModuleClientes) . GetItem( ID) ;
FiltrarEmpresa( Result ) ;
end ;
function TClientesController. BuscarTodos: IBizContacto;
begin
Result : = ( FDataModule as IDataModuleClientes) . GetItems;
FiltrarEmpresa( Result ) ;
end ;
function TClientesController. BuscarTodosTiendaWeb: IBizCliente;
begin
Result : = ( FDataModule as IDataModuleClientes) . GetItemsTiendaWeb;
FiltrarEmpresa( Result ) ;
end ;
constructor TClientesController. Create;
begin
inherited ;
FDataModule : = TDataModuleClientes. Create( Nil ) ;
FDireccionesController : = TDireccionesContactoController. Create;
end ;
2008-12-17 19:07:42 +00:00
function TClientesController. DarListaCuentasBancarias( const IdCliente: Integer ) : TStringList;
var
ACliente: IBizCliente;
Aux : String ;
begin
ACliente : = ( Buscar( IdCliente) as IBizCliente) ;
ACliente. DataTable. Active : = True ;
Result : = TStringList. Create;
ShowHourglassCursor;
try
with Result do
begin
ACliente. DatosBancarios. DataTable. First;
while not ACliente. DatosBancarios. DataTable. EOF do
begin
Aux : = Format( 'N<> de cuenta: %s %s %s %s - Titular: %s ' , [ ACliente. DatosBancarios. ENTIDAD,
ACliente. DatosBancarios. SUCURSAL, ACliente. DatosBancarios. DC, ACliente. DatosBancarios. CUENTA, ACliente. DatosBancarios. TITULAR] ) ;
Add( Format( '%s=%d' , [ Aux, ACliente. DatosBancarios. ID] ) ) ;
ACliente. DatosBancarios. DataTable. Next;
end ;
end ;
finally
ACliente : = NIL ;
HideHourglassCursor;
end ;
end ;
2007-09-12 14:11:44 +00:00
destructor TClientesController. Destroy;
begin
FDireccionesController : = NIL ;
inherited ;
end ;
function TClientesController. Duplicar( AContacto: IBizContacto) : IBizContacto;
begin
Result : = inherited Duplicar( AContacto) ;
if Assigned( ( Result as IBizCliente) . Descuentos) then
DuplicarRegistros( ( AContacto as IBizCliente) . Descuentos. DataTable, ( Result as IBizCliente) . Descuentos. DataTable, mdrTodos) ;
end ;
function TClientesController. ElegirContacto( AContactos : IBizContacto;
AMensaje: String ; AMultiSelect: Boolean ) : IBizContacto;
var
AEditor : IEditorElegirClientes;
begin
Result : = NIL ;
CreateEditor( 'EditorElegirClientes' , IEditorElegirClientes, AEditor) ;
2008-09-19 10:11:52 +00:00
if Assigned( AEditor) then
begin
try
AEditor. Contactos : = AContactos;
AEditor. Controller : = Self;
AEditor. MultiSelect : = AMultiSelect;
AEditor. Mensaje : = AMensaje;
if IsPositiveResult( AEditor. ShowModal) then
Result : = AEditor. ContactosSeleccionados;
finally
2008-11-13 10:33:06 +00:00
AEditor. Release;
2008-09-19 10:11:52 +00:00
AEditor : = NIL ;
Application. ProcessMessages;
2007-09-12 14:11:44 +00:00
end ;
end ;
end ;
2008-09-03 09:21:08 +00:00
function TClientesController. ElegirDireccionEntrega( ACliente: IBizCliente;
2007-09-12 14:11:44 +00:00
AMensaje: String ) : IBizDireccionesContacto;
var
AEditor : IEditorElegirDireccionEntrega;
begin
Result : = NIL ;
CreateEditor( 'EditorElegirDireccionEntrega' , IEditorElegirDireccionEntrega, AEditor) ;
2008-09-02 16:21:43 +00:00
if Assigned( AEditor) then
begin
try
2008-09-03 09:21:08 +00:00
AEditor. Contacto : = ACliente;
2008-09-02 16:21:43 +00:00
AEditor. Mensaje : = AMensaje;
if IsPositiveResult( AEditor. ShowModal) then
Result : = AEditor. DireccionSeleccionada;
finally
2008-11-13 13:10:32 +00:00
AEditor. Release;
2008-09-02 16:21:43 +00:00
AEditor : = NIL ;
2008-09-19 10:11:52 +00:00
Application. ProcessMessages;
2008-09-02 16:21:43 +00:00
end ;
end ;
end ;
2007-09-12 14:11:44 +00:00
function TClientesController. Eliminar( ACliente: IBizContacto; AllItems: Boolean ) : Boolean ;
//En el caso de eliminar almenos un elemento del conjunto se devuelve true
var
bEliminado: Boolean ;
begin
2008-05-20 18:50:02 +00:00
bEliminado : = False ;
2007-09-12 14:11:44 +00:00
if not Assigned( ACliente) then
raise Exception. Create ( 'Contacto no asignado' ) ;
ShowHourglassCursor;
try
if not ACliente. DataTable. Active then
ACliente. DataTable. Active : = True ;
if ( ACliente. State in dsEditModes) then
ACliente. Cancel;
//Siempre eliminaremos el seleccionado
if EsEliminable( ACliente) then
begin
ACliente. Delete;
bEliminado : = True ;
end ;
//En el caso de querer eliminar todos los items del objeto ACliente
if AllItems then
begin
with ACliente. DataTable do
begin
First;
while not EOF do
begin
if EsEliminable( ACliente) then
begin
ACliente. Delete;
bEliminado : = True
end
else Next;
end ;
end ;
end ;
if bEliminado then
begin
ACliente. DataTable. ApplyUpdates;
Result : = True ;
end
else
Result : = False ;
HideHourglassCursor;
2009-03-31 17:32:20 +00:00
except
on E: Exception do
begin
ACliente. DataTable. CancelUpdates;
HideHourglassCursor;
if ( Pos( 'FOREIGN KEY' , E. Message ) > 0 ) then
MessageBox( 0 , 'No se pueden borrar el/los cliente/s seleccionado/s porque, alguno/s de ellos tiene documentos dados de alta en la aplicaci<63> n (presupuestos, albaranes, facturas, etc)' , 'Atenci<63> n' , MB_ICONWARNING or MB_OK) ;
end ;
2007-09-12 14:11:44 +00:00
end ;
end ;
function TClientesController. EsEliminable( ACliente: IBizContacto) : Boolean ;
begin
if not Assigned( ACliente) then
raise Exception. Create ( 'Contacto no asignado: EsEliminable' ) ;
Result : = True ;
end ;
function TClientesController. Nuevo: IBizContacto;
var
AContacto : IBizCliente;
begin
AContacto : = ( FDataModule as IDataModuleClientes) . NewItem;
FiltrarEmpresa( AContacto) ;
AContacto. DataTable. Active : = True ;
AContacto. Insert;
Result : = AContacto;
end ;
procedure TClientesController. Preview( ACliente: IBizCliente; AllItems: Boolean ) ;
var
AReportController : IEtiquetasContactosReportController;
2008-08-22 14:52:35 +00:00
ListaID: TIntegerList;
2007-09-12 14:11:44 +00:00
begin
AReportController : = TEtiquetasContactosReportController. Create;
2008-08-22 14:52:35 +00:00
ListaID : = TIntegerList. Create;
2007-09-12 14:11:44 +00:00
2008-05-20 18:50:02 +00:00
try
2007-09-12 14:11:44 +00:00
//Si deseamos previsualizar todos los items del objeto albaran
if AllItems then
begin
with ACliente. DataTable do
begin
First;
while not EOF do
begin
2008-08-22 14:52:35 +00:00
ListaID. Add( ACliente. ID) ;
2007-09-12 14:11:44 +00:00
Next;
end ;
end ;
end
//Solo previsualizamos el item seleccionado
else
2008-08-22 14:52:35 +00:00
ListaID. Add( ACliente. ID) ;
2007-09-12 14:11:44 +00:00
2008-08-22 14:52:35 +00:00
AReportController. Preview( ListaID) ;
2007-09-12 14:11:44 +00:00
finally
AReportController : = NIL ;
2008-08-22 14:52:35 +00:00
FreeANDNIL( ListaID) ;
2007-09-12 14:11:44 +00:00
end ;
end ;
procedure TClientesController. Print( ACliente: IBizCliente; AllItems: Boolean ) ;
var
AReportController : IEtiquetasContactosReportController;
2008-08-22 14:52:35 +00:00
ListaID: TIntegerList;
2007-09-12 14:11:44 +00:00
begin
AReportController : = TEtiquetasContactosReportController. Create;
2008-08-22 14:52:35 +00:00
ListaID : = TIntegerList. Create;
2007-09-12 14:11:44 +00:00
2008-05-20 18:50:02 +00:00
try
2007-09-12 14:11:44 +00:00
//Si deseamos previsualizar todos los items del objeto albaran
if AllItems then
begin
with ACliente. DataTable do
begin
First;
while not EOF do
begin
2008-08-22 14:52:35 +00:00
ListaID. Add( ACliente. ID) ;
2007-09-12 14:11:44 +00:00
Next;
end ;
end ;
end
//Solo previsualizamos el item seleccionado
else
2008-08-22 14:52:35 +00:00
ListaID. Add( ACliente. ID) ;
2007-09-12 14:11:44 +00:00
2008-08-22 14:52:35 +00:00
AReportController. Print( ListaID) ;
2007-09-12 14:11:44 +00:00
finally
AReportController : = NIL ;
2008-08-22 14:52:35 +00:00
FreeANDNil( ListaID) ;
2007-09-12 14:11:44 +00:00
end ;
end ;
function TClientesController. TieneDatosBancarios(
ACliente: IBizCliente) : Boolean ;
begin
if not Assigned( ACliente) then
raise Exception. Create ( 'Cliente no asignado (TieneDatosBancarios)' ) ;
if ACliente. DataTable. Active then
ACliente. DataTable. Active : = True ;
with ACliente. DatosBancarios do
begin
Result : = ( DataTable. RecordCount > 0 ) and
(
( Length( ENTIDAD) > 0 ) and
( Length( SUCURSAL) > 0 ) and
( Length( DC) > 0 ) and
( Length( CUENTA) > 0 )
) ;
end ;
end ;
function TClientesController. ValidarContacto( AContacto: IBizContacto) : Boolean ;
begin
Result : = inherited ValidarContacto( AContacto) ;
if Result then
begin
with ( AContacto as IBizCliente) do
begin
if ( BLOQUEADO = 0 ) then
begin
Edit;
MOTIVO_BLOQUEO : = '' ;
Post;
end ;
if ( TIENDA_WEB = 1 ) and ( Length( EMAIL_1) = 0 ) then
begin
{ ShowWarningMessage( 'Acceso a la tienda web' ,
'Para que el cliente pueda tener acceso a la tienda web es necesario indicar una direcci<63> n de e-mail en el campo ' 'Correo de trabajo' '' +
#10 #13 + #10 #13 +
'Por favor, indique una direcci<63> n o desactive el acceso a la tienda.' ) ;
Result : = False ; }
raise Exception. Create( 'Para que el cliente pueda tener acceso a la tienda web es necesario indicar una direcci<63> n de e-mail en el campo ' 'Correo de trabajo' '' +
#10 #13 + #10 #13 +
'Por favor, indique una direcci<63> n o desactive el acceso a la tienda.' ) ;
end ;
end ;
2007-12-16 21:37:26 +00:00
{
// Por ahora no existe el concepto de direcciones de entrega asi que esto est<73> desactivado
2007-09-12 14:11:44 +00:00
if Result and ( AContacto. Direcciones. RecordCount = 0 ) then
if ( ShowConfirmMessage( 'El cliente no tiene direcciones asociadas' ,
AContacto. NOMBRE + ' no tiene ninguna direcci<63> n de env<6E> o dada de alta, ' + #10 #13 +
'<27> Desea utilizar el domicilio fiscal para dar de alta una direcci<63> n de entrega?' ) = IDYES) then
begin
FDireccionesController. CopiarDireccionFiscal( AContacto, AContacto. Direcciones) ;
FDireccionesController. Ver( AContacto. Direcciones) ;
2007-12-16 21:37:26 +00:00
end ; }
2007-09-12 14:11:44 +00:00
end ;
end ;
procedure TClientesController. Ver( AContacto: IBizContacto) ;
var
AEditor : IEditorCliente;
begin
2007-11-18 18:42:04 +00:00
AEditor : = NIL ;
2008-11-11 16:22:18 +00:00
CreateEditor( 'EditorCliente' , IEditorCliente, AEditor) ;
if Assigned( AEditor) then
2008-11-13 13:10:32 +00:00
try
AEditor. Contacto : = AContacto;
AEditor. Controller : = Self;
AEditor. ShowModal;
finally
AEditor. Release;
AEditor : = NIL ;
end ;
2007-09-12 14:11:44 +00:00
end ;
2008-11-10 11:29:57 +00:00
procedure TClientesController. VerAlbaranesDeCliente( ACliente: IBizCliente) ;
var
AModule : TModuleController;
APlugin : IMCAlbaranesCliente;
begin
if not Assigned( ACliente) then
raise Exception. Create ( 'Cliente no asignado (VerAlbaranesDeCliente)' ) ;
if ACliente. DataTable. Active then
ACliente. DataTable. Active : = True ;
AModule : = AppFactuGES. GetModule( MODULENAME_ALBARANES_CLIENTE) ;
if Assigned( AModule) then
try
if Supports( AModule, IMCAlbaranesCliente, APlugin) then
APlugin. VerAlbaranes( ACliente. ID, ACliente. NOMBRE) ;
finally
APlugin : = NIL ;
end ;
end ;
procedure TClientesController. VerFacturasDeCliente( ACliente: IBizCliente) ;
var
AModule : TModuleController;
APlugin : IMCFacturasCliente;
begin
if not Assigned( ACliente) then
raise Exception. Create ( 'Cliente no asignado (VerFacturasDeCliente)' ) ;
if ACliente. DataTable. Active then
ACliente. DataTable. Active : = True ;
AModule : = AppFactuGES. GetModule( MODULENAME_FACTURAS_CLIENTE) ;
if Assigned( AModule) then
try
if Supports( AModule, IMCFacturasCliente, APlugin) then
APlugin. VerFacturas( ACliente. ID, ACliente. NOMBRE) ;
finally
APlugin : = NIL ;
end ;
end ;
procedure TClientesController. VerPresupuestosDeCliente( ACliente : IBizCliente) ;
var
AModule : TModuleController;
APlugin : IMCPresupuestosCliente;
begin
if not Assigned( ACliente) then
raise Exception. Create ( 'Cliente no asignado (VerPresupuestosDeCliente)' ) ;
if ACliente. DataTable. Active then
ACliente. DataTable. Active : = True ;
AModule : = AppFactuGES. GetModule( MODULENAME_PRESUPUESTOS_CLIENTE) ;
if Assigned( AModule) then
try
if Supports( AModule, IMCPresupuestosCliente, APlugin) then
APlugin. VerPresupuestos( ACliente. ID, ACliente. NOMBRE) ;
finally
APlugin : = NIL ;
end ;
end ;
procedure TClientesController. VerRecibosDeCliente( ACliente: IBizCliente) ;
var
AModule : TModuleController;
APlugin : IMCRecibosCliente;
begin
if not Assigned( ACliente) then
raise Exception. Create ( 'Cliente no asignado (VerFacturasDeCliente)' ) ;
if ACliente. DataTable. Active then
ACliente. DataTable. Active : = True ;
AModule : = AppFactuGES. GetModule( MODULENAME_RECIBOS_CLIENTE) ;
if Assigned( AModule) then
try
if Supports( AModule, IMCRecibosCliente, APlugin) then
APlugin. VerRecibos( ACliente. ID, ACliente. NOMBRE) ;
finally
APlugin : = NIL ;
end ;
end ;
2007-09-12 14:11:44 +00:00
procedure TClientesController. VerTodos( AContactos: IBizContacto) ;
var
AEditor : IEditorClientes;
begin
2007-11-18 18:42:04 +00:00
AEditor : = NIL ;
2008-11-11 16:22:18 +00:00
CreateEditor( 'EditorClientes' , IEditorClientes, AEditor) ;
if Assigned( AEditor) then
with AEditor do
begin
2008-11-11 18:56:14 +00:00
Contactos : = AContactos;
Controller : = Self;
MultiSelect : = True ;
ShowEmbedded;
2008-11-11 16:22:18 +00:00
end ;
2007-09-12 14:11:44 +00:00
end ;
end .