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; function DarListaAnosObjetivosAlbaranesAgente(ID_AGENTE: Integer): TStringList; function DarListaAnosObjetivosFacturasAgente(ID_AGENTE: Integer): TStringList; function BuscarActivos: IBizContacto; 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 BuscarActivos: IBizContacto; function Nuevo : IBizContacto; override; function Guardar(AContacto : IBizContacto): Boolean; override; procedure Ver(AContacto : IBizContacto); override; procedure VerTodos(AContactos: IBizContacto); override; function ElegirContacto(AContactos : IBizContacto; AMensaje: String; AMultiSelect: Boolean): IBizContacto; override; function Eliminar(AAgente: IBizContacto; AllItems: Boolean = false): Boolean; overload; function EsEliminable(AAgente: IBizContacto): Boolean; function DarListaAnosObjetivosAlbaranesAgente(ID_AGENTE: Integer): TStringList; function DarListaAnosObjetivosFacturasAgente(ID_AGENTE: Integer): TStringList; end; implementation uses uDAInterfaces, Windows, SysUtils, Forms, cxControls, Controls, Dialogs, uDataModuleAgentes, uEditorRegistryUtils, uDataTableUtils, uDADataTable, DB, schContactosClient_Intf, uEditorGridBase, uIntegerListUtils, uIEditorElegirAgentes; { TAgenteController } function TAgentesController.Buscar(const ID: Integer): IBizContacto; begin Result := (FDataModule as IDataModuleAgentes).GetItem(ID); FiltrarEmpresa(Result); end; function TAgentesController.BuscarActivos: IBizContacto; var Condicion: TDAWhereExpression; begin ShowHourglassCursor; try Result := BuscarTodos; with Result.DataTable.DynamicWhere do begin // INACTIVOS Condicion := NewBinaryExpression(NewField('', fld_AgentesFECHA_BAJA), NewNull(), dboEqual); if IsEmpty then Expression := Condicion else Expression := NewBinaryExpression(Expression, Condicion, dboAnd); end; finally HideHourglassCursor; end; 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.DarListaAnosObjetivosAlbaranesAgente(ID_AGENTE: Integer): TStringList; begin Result := (FDataModule as IDataModuleAgentes).GetAnosObjetivosAlbaranesItems(ID_AGENTE); end; function TAgentesController.DarListaAnosObjetivosFacturasAgente(ID_AGENTE: Integer): TStringList; begin Result := (FDataModule as IDataModuleAgentes).GetAnosObjetivosAlbaranesItems(ID_AGENTE); end; function TAgentesController.Duplicar(AContacto: IBizContacto): IBizContacto; begin Result := inherited Duplicar(AContacto); // if Assigned((Result as IBizAgente).ObjetivosAlbaranes) then // DuplicarRegistros((AContacto as IBizAgente).ObjetivosAlbaranes.DataTable, (Result as IBizAgente).ObjetivosAlbaranes.DataTable, mdrTodos); // if Assigned((Result as IBizAgente).ObjetivosFacturas) then // DuplicarRegistros((AContacto as IBizAgente).ObjetivosFacturas.DataTable, (Result as IBizAgente).ObjetivosFacturas.DataTable, mdrTodos); end; function TAgentesController.ElegirContacto(AContactos: IBizContacto; AMensaje: String; AMultiSelect: Boolean): IBizContacto; var AEditor : IEditorElegirAgentes; begin Result := NIL; CreateEditor('EditorElegirAgentes', IEditorElegirAgentes, AEditor); 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 AEditor.Release; AEditor := NIL; Application.ProcessMessages; end; end; 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.Guardar(AContacto: IBizContacto): Boolean; begin if inherited Guardar(AContacto) then begin // (AContacto as IBizAgente).ObjetivosAlbaranes.DataTable.Refresh; // (AContacto as IBizAgente).ObjetivosFacturas.DataTable.Refresh; end; 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.