unit uProveedoresController; interface uses uCustomEditor, uContactosController, uBizContactos, uBizDireccionesContacto, uDireccionesContactoController, uIEditorProveedores, uIEditorProveedor, uIDataModuleContactos, uIDataModuleProveedores; type IProveedoresController = interface(IContactosController) ['{50F10D01-5120-470D-A61D-99FE8A76DF93}'] function BuscarTodosTiendaWeb: IBizProveedor; function EsEliminable(AProveedor: IBizContacto): Boolean; function Eliminar(AProveedor: IBizContacto; AllItems: Boolean = false): Boolean; overload; procedure Preview(AProveedor : IBizProveedor; AllItems: Boolean = false); procedure Print(AProveedor : IBizProveedor; AllItems: Boolean = false); procedure SetTieneSubcuenta(AProveedor: IBizProveedor; AValue: Boolean); procedure SetIgnorarContabilidad(AProveedor: IBizProveedor; AValue: Boolean); end; TProveedoresController = class(TContactosController, IProveedoresController) protected FDireccionesController : IDireccionesContactoController; function ValidarContacto(AContacto: IBizContacto): Boolean; override; public constructor Create; override; destructor Destroy; override; function Buscar(const ID: Integer): IBizContacto; override; function BuscarTodos: IBizContacto; override; function BuscarTodosTiendaWeb: IBizProveedor; 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 EsEliminable(AProveedor: IBizContacto): Boolean; function Eliminar(AProveedor: IBizContacto; AllItems: Boolean = false): Boolean; overload; procedure Preview(AProveedor : IBizProveedor; AllItems: Boolean = false); procedure Print(AProveedor : IBizProveedor; AllItems: Boolean = false); procedure SetTieneSubcuenta(AProveedor: IBizProveedor; AValue: Boolean); procedure SetIgnorarContabilidad(AProveedor: IBizProveedor; AValue: Boolean); end; implementation uses Classes, SysUtils, cxControls, Dialogs, uDataModuleProveedores, uEditorRegistryUtils, uDataTableUtils, uDADataTable, DB, schContactosClient_Intf, uEtiquetasContactosReportController, uIEditorElegirProveedores, Controls, uEditorGridBase, JSDialogs, JSDialog, Windows; { TProveedoresController } function TProveedoresController.Buscar(const ID: Integer): IBizContacto; begin Result := (FDataModule as IDataModuleProveedores).GetItem(ID); FiltrarEmpresa(Result); end; function TProveedoresController.BuscarTodos: IBizContacto; begin Result := (FDataModule as IDataModuleProveedores).GetItems; FiltrarEmpresa(Result); end; function TProveedoresController.BuscarTodosTiendaWeb: IBizProveedor; begin Result := (FDataModule as IDataModuleProveedores).GetItemsTiendaWeb; FiltrarEmpresa(Result); end; constructor TProveedoresController.Create; begin inherited; FDataModule := TDataModuleProveedores.Create(Nil); FDireccionesController := TDireccionesContactoController.Create; end; destructor TProveedoresController.Destroy; begin FDireccionesController := NIL; inherited; end; function TProveedoresController.ElegirContacto(AContactos: IBizContacto; AMensaje: String; AMultiSelect: Boolean): IBizContacto; var AEditor : IEditorElegirProveedores; begin Result := NIL; CreateEditor('EditorElegirProveedores', IEditorElegirProveedores, AEditor); if Assigned(AEditor) then with AEditor do begin try Contactos := AContactos; Controller := Self; MultiSelect := AMultiSelect; Mensaje := AMensaje; if IsPositiveResult(ShowModal) then Result := ContactosSeleccionados; finally Release; AEditor := NIL; end; end; end; function TProveedoresController.Eliminar(AProveedor: IBizContacto; AllItems: Boolean): Boolean; //En el caso de eliminar almenos un elemento del conjunto se devuelve true var bEliminado: Boolean; begin bEliminado := False; if not Assigned(AProveedor) then raise Exception.Create ('Contacto no asignado'); ShowHourglassCursor; try if not AProveedor.DataTable.Active then AProveedor.DataTable.Active := True; if (AProveedor.State in dsEditModes) then AProveedor.Cancel; //Siempre eliminaremos el seleccionado if EsEliminable(AProveedor) then begin AProveedor.Delete; bEliminado := True; end; //En el caso de querer eliminar todos los items del objeto AProveedor if AllItems then begin with AProveedor.DataTable do begin First; while not EOF do begin if EsEliminable(AProveedor) then begin AProveedor.Delete; bEliminado := True end else Next; end; end; end; if bEliminado then begin AProveedor.DataTable.ApplyUpdates; Result := True; end else Result := False; finally HideHourglassCursor; end; end; function TProveedoresController.EsEliminable(AProveedor: IBizContacto): Boolean; begin if not Assigned(AProveedor) then raise Exception.Create ('Contacto no asignado: EsEliminable'); Result := True; end; function TProveedoresController.Guardar(AContacto: IBizContacto): Boolean; begin Result := inherited Guardar(AContacto); (AContacto as IBizProveedor).SubCuentas.DataTable.Refresh; end; function TProveedoresController.Nuevo: IBizContacto; var AContacto : IBizProveedor; begin AContacto := (FDataModule as IDataModuleProveedores).NewItem; FiltrarEmpresa(AContacto); AContacto.DataTable.Active := True; AContacto.Insert; Result := AContacto; end; procedure TProveedoresController.Preview(AProveedor: IBizProveedor; AllItems: Boolean); var AReportController : IEtiquetasContactosReportController; ListaID: TStringList; begin AReportController := TEtiquetasContactosReportController.Create; try ListaID := TStringList.Create; try //Si deseamos previsualizar todos los items del objeto albaran if AllItems then begin with AProveedor.DataTable do begin First; while not EOF do begin ListaID.Add(IntToStr(AProveedor.ID)); Next; end; end; end //Solo previsualizamos el item seleccionado else ListaID.Add(IntToStr(AProveedor.ID)); AReportController.Preview(ListaID.CommaText); finally FreeANDNIL(ListaID); end; finally AReportController := NIL; end; end; procedure TProveedoresController.Print(AProveedor: IBizProveedor; AllItems: Boolean); var AReportController : IEtiquetasContactosReportController; ListaID: TStringList; begin AReportController := TEtiquetasContactosReportController.Create; try ListaID := TStringList.Create; try //Si deseamos previsualizar todos los items del objeto albaran if AllItems then begin with AProveedor.DataTable do begin First; while not EOF do begin ListaID.Add(IntToStr(AProveedor.ID)); Next; end; end; end //Solo previsualizamos el item seleccionado else ListaID.Add(IntToStr(AProveedor.ID)); AReportController.Print(ListaID.CommaText); finally FreeANDNIL(ListaID); end; finally AReportController := NIL; end; end; procedure TProveedoresController.SetIgnorarContabilidad(AProveedor: IBizProveedor; AValue: Boolean); var AEdit: Boolean; begin with AProveedor.DataTable do begin AEdit := Editing; if not AEdit then Edit; if AValue then AProveedor.IGNORAR_CONTABILIDAD := 1 else AProveedor.IGNORAR_CONTABILIDAD := 0; Post; if AEdit then Edit; end; end; procedure TProveedoresController.SetTieneSubcuenta(AProveedor: IBizProveedor; AValue: Boolean); var AEdit: Boolean; begin with AProveedor.DataTable do begin AEdit := Editing; if not AEdit then Edit; if AValue then AProveedor.TIENE_SUBCUENTA := 1 else AProveedor.TIENE_SUBCUENTA := 0; Post; if AEdit then Edit; end; end; function TProveedoresController.ValidarContacto( AContacto: IBizContacto): Boolean; begin Result := inherited ValidarContacto(AContacto); if Result then begin with (AContacto as IBizProveedor) do begin if (SubCuentas.DataTable.State in dsEditModes) then SubCuentas.DataTable.Post; end; end; end; procedure TProveedoresController.Ver(AContacto: IBizContacto); var AEditor : IEditorProveedor; begin AEditor := NIL; CreateEditor('EditorProveedor', IEditorProveedor, AEditor); if Assigned(AEditor) then with AEditor do begin try Contacto := AContacto; Controller := Self; ShowModal; finally Release; AEditor := NIL; end; end; end; procedure TProveedoresController.VerTodos(AContactos: IBizContacto); var AEditor : IEditorProveedores; begin AEditor := NIL; CreateEditor('EditorProveedores', IEditorProveedores, AEditor); if Assigned(AEditor) then with AEditor do begin Contactos := AContactos; Controller := Self; MultiSelect := True; ShowEmbedded; end; end; end.