unit uAgentesController; interface uses uCustomEditor, Classes, uContactosController, uBizContactos, uIEditorAgentes, uIEditorAgente, uIDataModuleContactos, uIDataModuleAgentes; type IAgentesController = interface(IContactosController) ['{73A25A90-020F-4403-B7A1-55C06CE648D9}'] function EsEliminable(AAgente: IBizContacto): Boolean; function Eliminar(AAgente: IBizContacto; AllItems: Boolean = false): Boolean; overload; end; TAgentesController = class(TContactosController, IAgentesController) protected function ValidarContacto(AContacto : IBizContacto): Boolean; override; public constructor Create; override; function Duplicar(AContacto: IBizContacto): IBizContacto; override; function Buscar(const ID: Integer): IBizContacto; override; function BuscarTodos: IBizContacto; override; function Nuevo : IBizContacto; override; procedure Ver(AContacto : IBizContacto); override; procedure VerTodos(AContactos: IBizContacto); override; function Eliminar(AAgente: IBizContacto; AllItems: Boolean = false): Boolean; overload; function EsEliminable(AAgente: IBizContacto): Boolean; end; implementation uses Windows, SysUtils, cxControls, Dialogs, uDataModuleAgentes, uEditorRegistryUtils, uDataTableUtils, uDADataTable, DB, schContactosClient_Intf, uEditorGridBase, uIntegerListUtils; { TAgenteController } function TAgentesController.Buscar(const ID: Integer): IBizContacto; begin Result := (FDataModule as IDataModuleAgentes).GetItem(ID); FiltrarEmpresa(Result); end; function TAgentesController.BuscarTodos: IBizContacto; begin Result := (FDataModule as IDataModuleAgentes).GetItems; FiltrarEmpresa(Result); end; constructor TAgentesController.Create; begin inherited; FDataModule := TDataModuleAgentes.Create(Nil); end; function TAgentesController.Duplicar(AContacto: IBizContacto): IBizContacto; begin Result := inherited Duplicar(AContacto); end; function TAgentesController.Eliminar(AAgente: IBizContacto; 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(AAgente) then raise Exception.Create ('Contacto no asignado'); ShowHourglassCursor; try if not AAgente.DataTable.Active then AAgente.DataTable.Active := True; if (AAgente.State in dsEditModes) then AAgente.Cancel; //Siempre eliminaremos el seleccionado if EsEliminable(AAgente) then begin AAgente.Delete; bEliminado := True; end; //En el caso de querer eliminar todos los items del objeto AAgente if AllItems then begin with AAgente.DataTable do begin First; while not EOF do begin if EsEliminable(AAgente) then begin AAgente.Delete; bEliminado := True end else Next; end; end; end; if bEliminado then begin AAgente.DataTable.ApplyUpdates; Result := True; end else Result := False; finally HideHourglassCursor; end; end; function TAgentesController.EsEliminable(AAgente: IBizContacto): Boolean; begin if not Assigned(AAgente) then raise Exception.Create ('Contacto no asignado: EsEliminable'); Result := True; end; function TAgentesController.Nuevo: IBizContacto; var AContacto : IBizAgente; begin AContacto := (FDataModule as IDataModuleAgentes).NewItem; FiltrarEmpresa(AContacto); AContacto.DataTable.Active := True; AContacto.Insert; Result := AContacto; end; function TAgentesController.ValidarContacto(AContacto: IBizContacto): Boolean; begin Result := inherited ValidarContacto(AContacto); { if Result then begin if not (AContacto as IBizAgente).FECHA_BAJAIsNull and (Length((AContacto as IBizAgente).CAUSA_BAJA) = 0) then raise Exception.Create('Debe indicar la causa de la baja del agente.'); end; } end; procedure TAgentesController.Ver(AContacto: IBizContacto); var AEditor : IEditorAgente; begin AEditor := NIL; CreateEditor('EditorAgente', IEditorAgente, AEditor); if Assigned(AEditor) then try AEditor.Contacto := AContacto; AEditor.Controller := Self; AEditor.ShowModal; finally AEditor.Release; AEditor := NIL; end; end; procedure TAgentesController.VerTodos(AContactos: IBizContacto); var AEditor : IEditorAgentes; begin AEditor := NIL; CreateEditor('EditorAgentes', IEditorAgentes, AEditor); if Assigned(AEditor) then with AEditor do begin Contactos := AContactos; Controller := Self; MultiSelect := True; ShowEmbedded; end; end; end.