unit uVendedoresController; interface uses Classes, uCustomEditor, uContactosController, uBizContactos, uIEditorVendedores, uIEditorVendedor, uIDataModuleContactos, uIDataModuleVendedores; const CTE_VENDEDOR = 'Vendedor'; type IVendedoresController = interface(IContactosController) ['{A3841871-7EF6-4847-9758-EA2B1C521D4A}'] function EsEliminable(AVendedor: IBizContacto): Boolean; function Eliminar(AVendedor: IBizContacto; AllItems: Boolean = false): Boolean; overload; function DarListaVendedores : TStringList; function BuscarTodos: IBizContacto; overload; function BuscarTodos(const CadenaIDs: String): IBizContacto; overload; function DarIDVendedorDeUsuario(const AUsuario: Integer): Integer; end; TVendedoresController = class(TContactosController, IVendedoresController) 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 DarListaVendedores : TStringList; function Eliminar(AVendedor: IBizContacto; AllItems: Boolean = false): Boolean; overload; function EsEliminable(AVendedor: IBizContacto): Boolean; function DarIDVendedorDeUsuario(const AUsuario: Integer): Integer; end; implementation uses Windows, SysUtils, Controls, cxControls, Dialogs, uDataModuleVendedores, uEditorRegistryUtils, uDataTableUtils, uDADataTable, DB, schContactosClient_Intf, uEditorGridBase, uDAInterfaces, uIEditorElegirVendedores; { TVendedorController } function TVendedoresController.Buscar(const ID: Integer): IBizContacto; begin Result := (FDataModule as IDataModuleVendedores).GetItem(ID); FiltrarEmpresa(Result); end; function TVendedoresController.BuscarTodos: IBizContacto; begin Result := (FDataModule as IDataModuleVendedores).GetItems; FiltrarEmpresa(Result); end; function TVendedoresController.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 vendedores que no esten asociados a la liquidación actual Condicion := NewBinaryExpression(NewField('', fld_VendedoresID), 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 TVendedoresController.Create; begin inherited; FDataModule := TDataModuleVendedores.Create(Nil); end; function TVendedoresController.DarIDVendedorDeUsuario( const AUsuario: Integer): Integer; var AVendedores: IBizVendedor; begin AVendedores := (BuscarTodos as IBizVendedor); AVendedores.DataTable.Active := True; with AVendedores.DataTable do begin First; while not EOF do begin if (AVendedores.ID_USUARIO = AUsuario) then begin Result := AVendedores.ID; Break; end else Next; end; end; end; function TVendedoresController.DarListaVendedores: TStringList; var AVendedores: IBizContacto; begin AVendedores := BuscarTodos; AVendedores.DataTable.Active := True; Result := TStringList.Create; try with Result do begin AVendedores.DataTable.First; while not AVendedores.DataTable.EOF do begin Add(Format('%s=%d', [AVendedores.NOMBRE, AVendedores.ID])); AVendedores.DataTable.Next; end; end; finally AVendedores := NIL; end; end; function TVendedoresController.Duplicar(AContacto: IBizContacto): IBizContacto; begin Result := inherited Duplicar(AContacto); end; function TVendedoresController.ElegirContacto(AContactos: IBizContacto; AMensaje: String; AMultiSelect: Boolean): IBizContacto; var AEditor : IEditorElegirVendedores; begin Result := NIL; CreateEditor('EditorElegirVendedores', IEditorElegirVendedores, 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 TVendedoresController.Eliminar(AVendedor: 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(AVendedor) then raise Exception.Create ('Contacto no asignado'); ShowHourglassCursor; try if not AVendedor.DataTable.Active then AVendedor.DataTable.Active := True; if (AVendedor.State in dsEditModes) then AVendedor.Cancel; //Siempre eliminaremos el seleccionado if EsEliminable(AVendedor) then begin AVendedor.Delete; bEliminado := True; end; //En el caso de querer eliminar todos los items del objeto AVendedor if AllItems then begin with AVendedor.DataTable do begin First; while not EOF do begin if EsEliminable(AVendedor) then begin AVendedor.Delete; bEliminado := True end else Next; end; end; end; if bEliminado then begin AVendedor.DataTable.ApplyUpdates; Result := True; end else Result := False; finally HideHourglassCursor; end; end; function TVendedoresController.EsEliminable(AVendedor: IBizContacto): Boolean; begin if not Assigned(AVendedor) then raise Exception.Create ('Contacto no asignado: EsEliminable'); Result := True; end; function TVendedoresController.Nuevo: IBizContacto; var AContacto : IBizVendedor; begin AContacto := (FDataModule as IDataModuleVendedores).NewItem; FiltrarEmpresa(AContacto); AContacto.DataTable.Active := True; AContacto.Insert; Result := AContacto; end; procedure TVendedoresController.Ver(AContacto: IBizContacto); var AEditor : IEditorVendedor; begin AEditor := NIL; CreateEditor('EditorVendedor', IEditorVendedor, AEditor); if Assigned(AEditor) then try AEditor.Contacto := AContacto; AEditor.Controller := Self; AEditor.ShowModal; finally AEditor.Release; AEditor := NIL; end; end; procedure TVendedoresController.VerTodos(AContactos: IBizContacto); var AEditor : IEditorVendedores; begin AEditor := NIL; CreateEditor('EditorVendedores', IEditorVendedores, AEditor); if Assigned(AEditor) then with AEditor do begin Contactos := AContactos; Controller := Self; MultiSelect := True; ShowEmbedded; end; end; end.