unit uComercialesController; interface uses Classes, uCustomEditor, uContactosController, uBizContactos, uIEditorComerciales, uIEditorComercial, uIDataModuleContactos, uIDataModuleComerciales; const CTE_COMERCIAL = 'Comercial'; type IComercialesController = interface(IContactosController) ['{855E9797-7DE1-4138-84CE-7AD0E8AAA330}'] function EsEliminable(AComercial: IBizContacto): Boolean; function Eliminar(AComercial: IBizContacto; AllItems: Boolean = false): Boolean; overload; function DarListaComerciales : TStringList; function BuscarTodos: IBizContacto; overload; function BuscarTodos(const CadenaIDs: String): IBizContacto; overload; // function DarIDComercialDeUsuario(const AUsuario: Integer): Integer; end; TComercialesController = class(TContactosController, IComercialesController) public constructor Create; override; function Duplicar(AContacto: IBizContacto): IBizContacto; override; function Buscar(const ID: Integer): IBizContacto; override; function BuscarTodos: IBizContacto; overload; override; function BuscarTodos(const CadenaIDs: String): IBizContacto; overload; 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 DarListaComerciales : TStringList; function Eliminar(AComercial: IBizContacto; AllItems: Boolean = false): Boolean; overload; function EsEliminable(AComercial: IBizContacto): Boolean; // function DarIDComercialDeUsuario(const AUsuario: Integer): Integer; end; implementation uses Windows, SysUtils, Controls, cxControls, Dialogs, uDataModuleComerciales, uEditorRegistryUtils, uDataTableUtils, uDADataTable, DB, schContactosClient_Intf, uEditorGridBase, uDAInterfaces, uIEditorElegirComerciales; { TComercialController } function TComercialesController.Buscar(const ID: Integer): IBizContacto; begin Result := (FDataModule as IDataModuleComerciales).GetItem(ID); FiltrarEmpresa(Result); end; function TComercialesController.BuscarTodos: IBizContacto; begin Result := (FDataModule as IDataModuleComerciales).GetItems; FiltrarEmpresa(Result); end; function TComercialesController.BuscarTodos(const CadenaIDs: String): IBizContacto; var Cadena : TStringList; Condicion: TDAWhereExpression; i: Integer; begin ShowHourglassCursor; try Result := BuscarTodos; if (Length(CadenaIDs) > 0) then begin Cadena := TStringList.Create; Cadena.CommaText := CadenaIDs; //Vamos generando todas las where de cada uno de los ID recibidos for i := 0 to Cadena.Count - 1 do with Result.DataTable.DynamicWhere do begin //Todas aquellos Comerciales que no esten asociados a la liquidación actual Condicion := NewBinaryExpression(NewField('', fld_ComercialesID), NewConstant(StrToInt(Cadena.Strings[i]), datInteger), dboNotEqual); if IsEmpty then Expression := Condicion else Expression := NewBinaryExpression(Expression, Condicion, dboAnd); end; Cadena.Free; end; finally HideHourglassCursor; end; end; constructor TComercialesController.Create; begin inherited; FDataModule := TDataModuleComerciales.Create(Nil); end; {function TComercialesController.DarIDComercialDeUsuario( const AUsuario: Integer): Integer; var AComerciales: IBizComercial; begin AComerciales := (BuscarTodos as IBizComercial); AComerciales.DataTable.Active := True; with AComerciales.DataTable do begin First; while not EOF do begin if (AComerciales.ID_USUARIO = AUsuario) then begin Result := AComerciales.ID; Break; end else Next; end; end; end; } function TComercialesController.DarListaComerciales: TStringList; var AComerciales: IBizContacto; begin AComerciales := BuscarTodos; AComerciales.DataTable.Active := True; Result := TStringList.Create; try with Result do begin AComerciales.DataTable.First; while not AComerciales.DataTable.EOF do begin Add(Format('%s=%d', [AComerciales.NOMBRE, AComerciales.ID])); AComerciales.DataTable.Next; end; end; finally AComerciales := NIL; end; end; function TComercialesController.Duplicar(AContacto: IBizContacto): IBizContacto; begin Result := inherited Duplicar(AContacto); end; function TComercialesController.ElegirContacto(AContactos: IBizContacto; AMensaje: String; AMultiSelect: Boolean): IBizContacto; var AEditor : IEditorElegirComerciales; begin Result := NIL; CreateEditor('EditorElegirComerciales', IEditorElegirComerciales, AEditor); if Assigned(AEditor) then 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; end; end; function TComercialesController.Eliminar(AComercial: 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(AComercial) then raise Exception.Create ('Contacto no asignado'); ShowHourglassCursor; try if not AComercial.DataTable.Active then AComercial.DataTable.Active := True; if (AComercial.State in dsEditModes) then AComercial.Cancel; //Siempre eliminaremos el seleccionado if EsEliminable(AComercial) then begin AComercial.Delete; bEliminado := True; end; //En el caso de querer eliminar todos los items del objeto AComercial if AllItems then begin with AComercial.DataTable do begin First; while not EOF do begin if EsEliminable(AComercial) then begin AComercial.Delete; bEliminado := True end else Next; end; end; end; if bEliminado then begin AComercial.DataTable.ApplyUpdates; Result := True; end else Result := False; finally HideHourglassCursor; end; end; function TComercialesController.EsEliminable(AComercial: IBizContacto): Boolean; begin if not Assigned(AComercial) then raise Exception.Create ('Contacto no asignado: EsEliminable'); Result := True; end; function TComercialesController.Nuevo: IBizContacto; var AContacto : IBizComercial; begin AContacto := (FDataModule as IDataModuleComerciales).NewItem; FiltrarEmpresa(AContacto); AContacto.DataTable.Active := True; AContacto.Insert; Result := AContacto; end; procedure TComercialesController.Ver(AContacto: IBizContacto); var AEditor : IEditorComercial; begin AEditor := NIL; CreateEditor('EditorComercial', IEditorComercial, AEditor); if Assigned(AEditor) then try AEditor.Contacto := AContacto; AEditor.Controller := Self; AEditor.ShowModal; finally AEditor.Release; AEditor := NIL; end; end; procedure TComercialesController.VerTodos(AContactos: IBizContacto); var AEditor : IEditorComerciales; begin AEditor := NIL; CreateEditor('EditorComerciales', IEditorComerciales, AEditor); if Assigned(AEditor) then with AEditor do begin Contactos := AContactos; Controller := Self; MultiSelect := True; ShowEmbedded; end; end; end.