unit uEmpleadosController; interface uses uCustomEditor, Classes, uContactosController, uBizContactos, uIEditorEmpleados, uIEditorEmpleado, uIDataModuleContactos, uIDataModuleEmpleados; type IEmpleadosController = interface(IContactosController) ['{A3841871-7EF6-4847-9758-EA2B1C521D4A}'] function EsEliminable(AEmpleado: IBizContacto): Boolean; function Eliminar(AEmpleado: IBizContacto; AllItems: Boolean = false): Boolean; overload; procedure Preview(AEmpleado : IBizEmpleado; AllItems: Boolean = false); procedure Print(AEmpleado : IBizEmpleado; AllItems: Boolean = false); function DarListaContratosEmpleado: TStringList; function DarListaEmpleadosConMovil: TStringList; end; TEmpleadosController = class(TContactosController, IEmpleadosController) 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(AEmpleado: IBizContacto; AllItems: Boolean = false): Boolean; overload; function EsEliminable(AEmpleado: IBizContacto): Boolean; function DarListaContratosEmpleado: TStringList; function DarListaEmpleadosConMovil: TStringList; procedure Preview(AEmpleado : IBizEmpleado; AllItems: Boolean = false); procedure Print(AEmpleado : IBizEmpleado; AllItems: Boolean = false); end; implementation uses Windows, SysUtils, cxControls, Dialogs, uDataModuleEmpleados, uEditorRegistryUtils, uDataTableUtils, uDADataTable, DB, schContactosClient_Intf, uFichasEmpleadoReportController, uEditorGridBase, uIntegerListUtils; { TEmpleadoController } function TEmpleadosController.Buscar(const ID: Integer): IBizContacto; begin Result := (FDataModule as IDataModuleEmpleados).GetItem(ID); FiltrarEmpresa(Result); end; function TEmpleadosController.BuscarTodos: IBizContacto; begin Result := (FDataModule as IDataModuleEmpleados).GetItems; FiltrarEmpresa(Result); end; constructor TEmpleadosController.Create; begin inherited; FDataModule := TDataModuleEmpleados.Create(Nil); end; function TEmpleadosController.DarListaContratosEmpleado: TStringList; begin Result := (FDataModule as IDataModuleEmpleados).DarListaContratosEmpleado; end; function TEmpleadosController.DarListaEmpleadosConMovil: TStringList; var AEmpleados: IBizEmpleado; AStr : String; begin Result := TStringList.Create; AEmpleados := IBizEmpleado(BuscarTodos); ShowHourglassCursor; try AEmpleados.DataTable.Active := True; with Result do begin AEmpleados.DataTable.First; while not AEmpleados.DataTable.EOF do begin AStr := AEmpleados.NOMBRE; if not AEmpleados.MOVIL_1IsNull then AStr := AStr + '. Tlf: ' + AEmpleados.MOVIL_1; Add(AStr); AEmpleados.DataTable.Next; end; end; finally AEmpleados := NIL; HideHourglassCursor; end; end; function TEmpleadosController.Duplicar(AContacto: IBizContacto): IBizContacto; begin Result := inherited Duplicar(AContacto); end; function TEmpleadosController.Eliminar(AEmpleado: 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(AEmpleado) then raise Exception.Create ('Contacto no asignado'); ShowHourglassCursor; try if not AEmpleado.DataTable.Active then AEmpleado.DataTable.Active := True; if (AEmpleado.State in dsEditModes) then AEmpleado.Cancel; //Siempre eliminaremos el seleccionado if EsEliminable(AEmpleado) then begin AEmpleado.Delete; bEliminado := True; end; //En el caso de querer eliminar todos los items del objeto AEmpleado if AllItems then begin with AEmpleado.DataTable do begin First; while not EOF do begin if EsEliminable(AEmpleado) then begin AEmpleado.Delete; bEliminado := True end else Next; end; end; end; if bEliminado then begin AEmpleado.DataTable.ApplyUpdates; Result := True; end else Result := False; finally HideHourglassCursor; end; end; function TEmpleadosController.EsEliminable(AEmpleado: IBizContacto): Boolean; begin if not Assigned(AEmpleado) then raise Exception.Create ('Contacto no asignado: EsEliminable'); Result := True; end; function TEmpleadosController.Nuevo: IBizContacto; var AContacto : IBizEmpleado; begin AContacto := (FDataModule as IDataModuleEmpleados).NewItem; FiltrarEmpresa(AContacto); AContacto.DataTable.Active := True; AContacto.Insert; Result := AContacto; end; procedure TEmpleadosController.Preview(AEmpleado: IBizEmpleado; AllItems: Boolean); var AReportController : IFichasEmpleadoReportController; ID_Fichas: TIntegerList; begin AReportController := TFichasEmpleadoReportController.Create; ID_Fichas := TIntegerList.Create; try //Si deseamos previsualizar todos los items del objeto albaran if AllItems then begin with AEmpleado.DataTable do begin First; while not EOF do begin ID_Fichas.Add(AEmpleado.ID); Next; end; end; end //Solo previsualizamos el item seleccionado else ID_Fichas.Add(AEmpleado.ID); AReportController.Preview(ID_Fichas); finally AReportController := NIL; FreeANDNil(ID_Fichas); end; end; procedure TEmpleadosController.Print(AEmpleado: IBizEmpleado; AllItems: Boolean); var AReportController : IFichasEmpleadoReportController; ID_Fichas: TIntegerList; begin AReportController := TFichasEmpleadoReportController.Create; ID_Fichas := TIntegerList.Create; try //Si deseamos previsualizar todos los items del objeto albaran if AllItems then begin with AEmpleado.DataTable do begin First; while not EOF do begin ID_Fichas.Add(AEmpleado.ID); Next; end; end; end //Solo previsualizamos el item seleccionado else ID_Fichas.Add(AEmpleado.ID); AReportController.Print(ID_Fichas); finally AReportController := NIL; FreeANDNIL(ID_Fichas); end; end; function TEmpleadosController.ValidarContacto(AContacto: IBizContacto): Boolean; begin Result := inherited ValidarContacto(AContacto); if Result then begin if not (AContacto as IBizEmpleado).FECHA_BAJAIsNull and (Length((AContacto as IBizEmpleado).CAUSA_BAJA) = 0) then raise Exception.Create('Debe indicar la causa de la baja del empleado.'); end; end; procedure TEmpleadosController.Ver(AContacto: IBizContacto); var AEditor : IEditorEmpleado; begin AEditor := NIL; CreateEditor('EditorEmpleado', IEditorEmpleado, AEditor); if Assigned(AEditor) then try AEditor.Contacto := AContacto; AEditor.Controller := Self; AEditor.ShowModal; finally AEditor.Release; AEditor := NIL; end; end; procedure TEmpleadosController.VerTodos(AContactos: IBizContacto); var AEditor : IEditorEmpleados; begin AEditor := NIL; CreateEditor('EditorEmpleados', IEditorEmpleados, AEditor); if Assigned(AEditor) then with AEditor do begin Contactos := AContactos; Controller := Self; MultiSelect := True; ShowEmbedded; end; end; end.