343 lines
10 KiB
ObjectPascal
343 lines
10 KiB
ObjectPascal
|
|
unit uContactosController;
|
|||
|
|
|
|||
|
|
interface
|
|||
|
|
|
|||
|
|
|
|||
|
|
uses
|
|||
|
|
Windows, Forms, Classes, Controls, Contnrs, SysUtils, uDADataTable,
|
|||
|
|
uBizContactos, uBizContactosDatosBancarios, uIDataModuleContactos,
|
|||
|
|
uControllerBase, uBizContactosPersonal;
|
|||
|
|
|
|||
|
|
type
|
|||
|
|
IContactosController = interface(IControllerBase)
|
|||
|
|
['{43F9A4ED-6563-4F95-9A56-330CF9C66731}']
|
|||
|
|
function Buscar(const ID: Integer): IBizContacto;
|
|||
|
|
function BuscarTodos: IBizContacto;
|
|||
|
|
procedure Ver(AContacto : IBizContacto);
|
|||
|
|
procedure VerTodos(AContactos: IBizContacto);
|
|||
|
|
function Nuevo : IBizContacto;
|
|||
|
|
procedure Anadir(AContacto : IBizContacto);
|
|||
|
|
procedure Eliminar(const ID : Integer); overload;
|
|||
|
|
function Eliminar(AContacto : IBizContacto): Boolean; overload;
|
|||
|
|
function Guardar(AContacto : IBizContacto): Boolean;
|
|||
|
|
procedure DescartarCambios(AContacto : IBizContacto);
|
|||
|
|
function Existe(const ID: Integer) : Boolean;
|
|||
|
|
function Duplicar(AContacto : IBizContacto) : IBizContacto;
|
|||
|
|
function Localizar(AContactos: IBizContacto; const ID : Integer): Boolean;
|
|||
|
|
function ElegirContacto(AContactos : IBizContacto;
|
|||
|
|
AMensaje: String; AMultiSelect: Boolean): IBizContacto;
|
|||
|
|
function ElegirPersonaContacto(APersonal: IBizContactoPersonal;
|
|||
|
|
AMensaje: String): IBizContactoPersonal;
|
|||
|
|
|
|||
|
|
function ExtraerSeleccionados(AContactos: IBizContacto) : IBizContacto;
|
|||
|
|
function DarListaPersonalConMovil(const IDContacto : Integer): TStringList;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
TContactosController = class(TControllerBase, IContactosController)
|
|||
|
|
protected
|
|||
|
|
FDataModule : IDataModuleContactos;
|
|||
|
|
procedure FiltrarEmpresa(AContacto : IBizContacto); virtual;
|
|||
|
|
function _Vacio : IBizContacto; virtual;
|
|||
|
|
function ValidarContacto(AContacto : IBizContacto): Boolean; virtual;
|
|||
|
|
public
|
|||
|
|
constructor Create; override;
|
|||
|
|
destructor Destroy; override;
|
|||
|
|
|
|||
|
|
procedure Eliminar(const ID : Integer); overload; virtual;
|
|||
|
|
function Eliminar(AContacto : IBizContacto): Boolean; overload; virtual;
|
|||
|
|
function Guardar(AContacto : IBizContacto): Boolean;
|
|||
|
|
procedure DescartarCambios(AContacto : IBizContacto); virtual;
|
|||
|
|
function Existe(const ID: Integer) : Boolean; virtual;
|
|||
|
|
procedure Anadir(AContacto : IBizContacto); virtual;
|
|||
|
|
function Duplicar(AContacto: IBizContacto): IBizContacto; virtual;
|
|||
|
|
function ExtraerSeleccionados(AContactos: IBizContacto) : IBizContacto; virtual;
|
|||
|
|
function Localizar(AContactos: IBizContacto; const ID : Integer): Boolean;
|
|||
|
|
|
|||
|
|
// Se implementa en los controladores hijos
|
|||
|
|
function Buscar(const ID: Integer): IBizContacto; virtual; abstract;
|
|||
|
|
function BuscarTodos: IBizContacto; virtual; abstract;
|
|||
|
|
function Nuevo : IBizContacto; virtual; abstract;
|
|||
|
|
procedure Ver(AContacto : IBizContacto); virtual; abstract;
|
|||
|
|
procedure VerTodos(AContactos: IBizContacto); virtual; abstract;
|
|||
|
|
function ElegirContacto(AContactos : IBizContacto;
|
|||
|
|
AMensaje: String; AMultiSelect: Boolean): IBizContacto; virtual; abstract;
|
|||
|
|
function ElegirPersonaContacto(APersonal: IBizContactoPersonal;
|
|||
|
|
AMensaje: String): IBizContactoPersonal;
|
|||
|
|
function DarListaPersonalConMovil(const IDContacto : Integer): TStringList;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
implementation
|
|||
|
|
|
|||
|
|
uses
|
|||
|
|
Dialogs, uEditorRegistryUtils, cxControls, DB, uDAInterfaces, uDataTableUtils,
|
|||
|
|
schContactosClient_Intf, uFactuGES_App, uIEditorElegirPersonaContacto;
|
|||
|
|
|
|||
|
|
{ TContactosController }
|
|||
|
|
|
|||
|
|
procedure TContactosController.Anadir(AContacto: IBizContacto);
|
|||
|
|
begin
|
|||
|
|
AContacto.Insert;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TContactosController.Duplicar(AContacto: IBizContacto): IBizContacto;
|
|||
|
|
begin
|
|||
|
|
Result := Self._Vacio;
|
|||
|
|
ShowHourglassCursor;
|
|||
|
|
try
|
|||
|
|
DuplicarRegistros(AContacto.DataTable, Result.DataTable, mdrActual);
|
|||
|
|
DuplicarRegistros(AContacto.DatosBancarios.DataTable, Result.DatosBancarios.DataTable, mdrTodos);
|
|||
|
|
if Assigned(AContacto.Direcciones) then
|
|||
|
|
DuplicarRegistros(AContacto.Direcciones.DataTable, Result.Direcciones.DataTable, mdrTodos);
|
|||
|
|
|
|||
|
|
// Hay que dejar algunos campos como si fuera un contacto nuevo
|
|||
|
|
Result.Edit;
|
|||
|
|
with Result do
|
|||
|
|
begin
|
|||
|
|
ID_EMPRESA := AppFactuGES.EmpresaActiva.ID;
|
|||
|
|
USUARIO := AppFactuGES.UsuarioActivo.UserName;
|
|||
|
|
REFERENCIA := '';
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
Result.Post;
|
|||
|
|
finally
|
|||
|
|
HideHourglassCursor;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
constructor TContactosController.Create;
|
|||
|
|
begin
|
|||
|
|
inherited;
|
|||
|
|
FDataModule := NIL;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TContactosController.DarListaPersonalConMovil(const IDContacto : Integer): TStringList;
|
|||
|
|
var
|
|||
|
|
AContacto: IBizContacto;
|
|||
|
|
AStr : String;
|
|||
|
|
begin
|
|||
|
|
AContacto := IBizContacto(Buscar(IDContacto));
|
|||
|
|
AContacto.DataTable.Active := True;
|
|||
|
|
Result := TStringList.Create;
|
|||
|
|
try
|
|||
|
|
with Result do
|
|||
|
|
begin
|
|||
|
|
AContacto.Personal.DataTable.First;
|
|||
|
|
while not AContacto.Personal.DataTable.EOF do
|
|||
|
|
begin
|
|||
|
|
AStr := AContacto.Personal.NOMBRE;
|
|||
|
|
if not AContacto.Personal.MOVILIsNull then
|
|||
|
|
AStr := AStr + '. Tlf: ' + AContacto.Personal.MOVIL;
|
|||
|
|
Add(AStr);
|
|||
|
|
AContacto.Personal.DataTable.Next;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
finally
|
|||
|
|
AContacto := NIL;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
procedure TContactosController.DescartarCambios(AContacto: IBizContacto);
|
|||
|
|
begin
|
|||
|
|
if not Assigned(AContacto) then
|
|||
|
|
raise Exception.Create ('Contacto no asignado');
|
|||
|
|
|
|||
|
|
ShowHourglassCursor;
|
|||
|
|
try
|
|||
|
|
if (AContacto.State in dsEditModes) then
|
|||
|
|
AContacto.Cancel;
|
|||
|
|
|
|||
|
|
AContacto.DataTable.CancelUpdates;
|
|||
|
|
finally
|
|||
|
|
HideHourglassCursor;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
destructor TContactosController.Destroy;
|
|||
|
|
begin
|
|||
|
|
FDataModule := NIL;
|
|||
|
|
inherited;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TContactosController.ElegirPersonaContacto(
|
|||
|
|
APersonal: IBizContactoPersonal; AMensaje: String): IBizContactoPersonal;
|
|||
|
|
var
|
|||
|
|
AEditor : IEditorElegirPersonaContacto;
|
|||
|
|
begin
|
|||
|
|
Result := NIL;
|
|||
|
|
|
|||
|
|
CreateEditor('EditorElegirPersonaContacto', IEditorElegirPersonaContacto, AEditor);
|
|||
|
|
if Assigned(AEditor) then
|
|||
|
|
begin
|
|||
|
|
try
|
|||
|
|
AEditor.Personas := APersonal;
|
|||
|
|
AEditor.Mensaje := AMensaje;
|
|||
|
|
if IsPositiveResult(AEditor.ShowModal) then
|
|||
|
|
Result := AEditor.PersonaSeleccionada;
|
|||
|
|
finally
|
|||
|
|
AEditor.Release;
|
|||
|
|
AEditor := NIL;
|
|||
|
|
Application.ProcessMessages;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TContactosController.Eliminar(AContacto: IBizContacto): Boolean;
|
|||
|
|
begin
|
|||
|
|
Result := False;
|
|||
|
|
|
|||
|
|
if not Assigned(AContacto) then
|
|||
|
|
raise Exception.Create ('Contacto no asignado');
|
|||
|
|
|
|||
|
|
ShowHourglassCursor;
|
|||
|
|
try
|
|||
|
|
if (AContacto.State in dsEditModes) then
|
|||
|
|
AContacto.Cancel;
|
|||
|
|
|
|||
|
|
AContacto.Delete;
|
|||
|
|
AContacto.DataTable.ApplyUpdates;
|
|||
|
|
HideHourglassCursor;
|
|||
|
|
Result := True;
|
|||
|
|
except
|
|||
|
|
on E: Exception do
|
|||
|
|
begin
|
|||
|
|
AContacto.DataTable.CancelUpdates;
|
|||
|
|
HideHourglassCursor;
|
|||
|
|
if (Pos('FOREIGN KEY', E.Message) > 0) then
|
|||
|
|
MessageBox(0, 'No se puede borrar este contacto porque tiene documentos dados de alta (como presupuestos, albaranes, facturas, etc)', 'Atenci<63>n', MB_ICONWARNING or MB_OK);
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
procedure TContactosController.Eliminar(const ID: Integer);
|
|||
|
|
var
|
|||
|
|
AContacto : IBizContacto;
|
|||
|
|
begin
|
|||
|
|
AContacto := Buscar(ID);
|
|||
|
|
|
|||
|
|
if not Assigned(AContacto) then
|
|||
|
|
raise Exception.Create(Format('No se ha encontrado el contacto con ID = %d', [ID]));
|
|||
|
|
|
|||
|
|
Eliminar(AContacto);
|
|||
|
|
AContacto := NIL;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TContactosController.Existe(const ID: Integer): Boolean;
|
|||
|
|
var
|
|||
|
|
AContacto : IBizContacto;
|
|||
|
|
begin
|
|||
|
|
try
|
|||
|
|
AContacto := Buscar(ID);
|
|||
|
|
Result := Assigned(AContacto) and (AContacto.ID = ID);
|
|||
|
|
finally
|
|||
|
|
AContacto := NIL;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TContactosController.ExtraerSeleccionados(AContactos: IBizContacto): IBizContacto;
|
|||
|
|
var
|
|||
|
|
ASeleccionados : IBizContacto;
|
|||
|
|
begin
|
|||
|
|
ASeleccionados := Self.Buscar(ID_NULO);
|
|||
|
|
CopyDataTableDA5(AContactos.DataTable, ASeleccionados.DataTable, True);
|
|||
|
|
Result := ASeleccionados;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TContactosController.Guardar(AContacto: IBizContacto): Boolean;
|
|||
|
|
var
|
|||
|
|
dtDetails : TList;
|
|||
|
|
i : integer;
|
|||
|
|
begin
|
|||
|
|
Result := False;
|
|||
|
|
|
|||
|
|
if ValidarContacto(AContacto) then
|
|||
|
|
begin
|
|||
|
|
ShowHourglassCursor;
|
|||
|
|
|
|||
|
|
if AContacto.EsNuevo then
|
|||
|
|
begin
|
|||
|
|
AContacto.Edit;
|
|||
|
|
AContacto.ID_EMPRESA := AppFactuGES.EmpresaActiva.ID;
|
|||
|
|
AContacto.Post;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
dtDetails := AContacto.DataTable.GetDetailDataTables;
|
|||
|
|
for i := 0 to dtDetails.Count - 1 do
|
|||
|
|
begin
|
|||
|
|
if (TDADataTable(dtDetails.Items[i])).State in dsEditModes then
|
|||
|
|
(TDADataTable(dtDetails.Items[i])).Post;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
AContacto.DataTable.ApplyUpdates;
|
|||
|
|
Result := True;
|
|||
|
|
finally
|
|||
|
|
HideHourglassCursor;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TContactosController.Localizar(AContactos: IBizContacto;
|
|||
|
|
const ID: Integer): Boolean;
|
|||
|
|
begin
|
|||
|
|
Result := True;
|
|||
|
|
ShowHourglassCursor;
|
|||
|
|
try
|
|||
|
|
with AContactos.DataTable do
|
|||
|
|
begin
|
|||
|
|
DisableControls;
|
|||
|
|
First;
|
|||
|
|
if not Locate(fld_ContactosID, ID, []) then
|
|||
|
|
Result := False;
|
|||
|
|
EnableControls;
|
|||
|
|
end;
|
|||
|
|
finally
|
|||
|
|
HideHourglassCursor;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TContactosController.ValidarContacto(AContacto: IBizContacto): Boolean;
|
|||
|
|
begin
|
|||
|
|
if not Assigned(AContacto) then
|
|||
|
|
raise Exception.Create ('Contacto no asignado');
|
|||
|
|
|
|||
|
|
if (AContacto.DataTable.State in dsEditModes) then
|
|||
|
|
AContacto.DataTable.Post;
|
|||
|
|
|
|||
|
|
if Length(AContacto.NOMBRE) = 0 then
|
|||
|
|
raise Exception.Create('Debe indicar al menos el nombre de este contacto.');
|
|||
|
|
|
|||
|
|
// Asegurarse de valores en campos "autom<6F>ticos"
|
|||
|
|
AContacto.Edit;
|
|||
|
|
AContacto.USUARIO := AppFactuGES.UsuarioActivo.UserName;
|
|||
|
|
AContacto.Post;
|
|||
|
|
|
|||
|
|
Result := True;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TContactosController._Vacio: IBizContacto;
|
|||
|
|
begin
|
|||
|
|
Result := Buscar(ID_NULO);
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
procedure TContactosController.FiltrarEmpresa(AContacto: IBizContacto);
|
|||
|
|
var
|
|||
|
|
Condicion: TDAWhereExpression;
|
|||
|
|
begin
|
|||
|
|
if AContacto.DataTable.Active then
|
|||
|
|
AContacto.DataTable.Active := False;
|
|||
|
|
|
|||
|
|
// Filtrar los presupuestos actuales por empresa
|
|||
|
|
with AContacto.DataTable.DynamicWhere do
|
|||
|
|
begin
|
|||
|
|
// (ID_EMPRESA >= ID)
|
|||
|
|
Condicion := NewBinaryExpression(NewField('', fld_ContactosID_EMPRESA), NewConstant(AppFactuGES.EmpresaActiva.ID, datInteger), dboEqual);
|
|||
|
|
|
|||
|
|
if IsEmpty then
|
|||
|
|
Expression := Condicion
|
|||
|
|
else
|
|||
|
|
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
end.
|