unit uSubCuentasController; interface uses Classes, SysUtils, uDADataTable, uControllerBase, uBizSubCuentas, uBizSubCuentasContacto, uIDataModuleContabilidad; const // (REF_CUENTA = 430 SOLO CUENTAS DE CLIENTES) // (REF_CUENTA = 400 SOLO CUENTAS DE PROVEEDORES) // (REF_CUENTA = 570 SOLO CUENTAS DE CLIENTES) // (REF_CUENTA = 400 SOLO CUENTAS DE PROVEEDORES) CTE_CUENTASCLIENTE = '430'; CTE_CUENTASPROVEEDOR = '400'; type ISubCuentasController = interface(IObservador) ['{94E5F2B6-64C8-4331-B9CB-3ED730478529}'] function BuscarTodos: IBizSubCuenta; function Buscar(ID: Integer): IBizSubCuenta; procedure VerTodos(ASubCuentas: IBizSubCuenta); procedure Ver(ASubCuenta: IBizSubCuenta); procedure Anadir(ASubCuenta : IBizSubCuenta); function Nuevo : IBizSubCuenta; function Eliminar(ASubCuenta : IBizSubCuenta): Boolean; function Guardar(ASubCuenta : IBizSubCuenta): Boolean; procedure DescartarCambios(ASubCuenta : IBizSubCuenta); function Localizar(ASubCuentas: IBizSubCuenta; ADescripcion:String): Boolean; function DarListaSubCuentas: TStringList; function ExtraerSeleccionados(ASubCuentas: IBizSubCuenta) : IBizSubCuenta; function ElegirSubCuenta(ASubCuentas : IBizSubCuenta; AMensaje: String; AMultiSelect: Boolean): IBizSubCuenta; procedure ElegirCuenta(ASubCuenta: IBizSubCuenta); procedure ElegirSubCuentaContacto(ASubCuentaContacto: IBizSubCuentasContacto; TipoCuenta: String); procedure VerSubCuentaContacto(ASubCuentaContacto: IBizSubCuentasContacto); procedure AnadirSubCuentaContacto(ASubCuentaContacto: IBizSubCuentasContacto; TipoCuenta: String); procedure EliminarSubCuentaContacto(ASubCuentaContacto: IBizSubCuentasContacto); end; TSubCuentasController = class(TObservador, ISubCuentasController) private function BuscarCuentas(TipoCuenta: String): IBizSubCuenta; procedure AsignarSubCuentaAContacto(ASubCuenta: IBizSubCuenta; ASubCuentaContacto: IBizSubCuentasContacto); protected FDataModule : IDataModuleContabilidad; procedure RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable); override; function CreateEditor(const AName : String; const IID: TGUID; out Intf): Boolean; function ValidarSubCuenta(ASubCuenta: IBizSubCuenta): Boolean; procedure AsignarDataModule; procedure FiltrarEjercicio(ASubCuenta: IBizSubCuenta); public constructor Create; override; destructor Destroy; override; function Eliminar(ASubCuenta : IBizSubCuenta): Boolean; function Guardar(ASubCuenta : IBizSubCuenta): Boolean; virtual; procedure DescartarCambios(ASubCuenta : IBizSubCuenta); virtual; procedure Anadir(ASubCuenta : IBizSubCuenta); function Nuevo : IBizSubCuenta; function BuscarTodos: IBizSubCuenta; function Buscar(ID: Integer): IBizSubCuenta; procedure VerTodos(ASubCuentas: IBizSubCuenta); procedure Ver(ASubCuenta: IBizSubCuenta); function Localizar(ASubCuentas: IBizSubCuenta; ADescripcion:String): Boolean; function DarListaSubCuentas: TStringList; function ExtraerSeleccionados(ASubCuentas: IBizSubCuenta) : IBizSubCuenta; function ElegirSubCuenta(ASubCuentas : IBizSubCuenta; AMensaje: String; AMultiSelect: Boolean): IBizSubCuenta; procedure ElegirCuenta(ASubCuenta: IBizSubCuenta); procedure ElegirSubCuentaContacto(ASubCuentaContacto: IBizSubCuentasContacto; TipoCuenta: String); procedure VerSubCuentaContacto(ASubCuentaContacto: IBizSubCuentasContacto); procedure AnadirSubCuentaContacto(ASubCuentaContacto: IBizSubCuentasContacto; TipoCuenta: String); procedure EliminarSubCuentaContacto(ASubCuentaContacto: IBizSubCuentasContacto); end; implementation uses cxControls, DB, uEditorRegistryUtils, schContabilidadClient_Intf, uIEditorSubCuentas, uIEditorSubCuenta, uIEditorElegirSubCuentas, uDataModuleContabilidad, uBizCuentas, uCuentasController, Dialogs, uDAInterfaces, uDataTableUtils, uDialogUtils, uFactuGES_App, uDateUtils, uROTypes, DateUtils, Controls, Windows; { TSubCuentasController } procedure TSubCuentasController.Anadir(ASubCuenta: IBizSubCuenta); begin ASubCuenta.Insert; end; procedure TSubCuentasController.AnadirSubCuentaContacto(ASubCuentaContacto: IBizSubCuentasContacto; TipoCuenta: String); var ASubCuenta: IBizSubCuenta; begin ASubCuenta := Nuevo; Ver(ASubCuenta); AsignarSubCuentaAContacto(ASubCuenta, ASubCuentaContacto); end; procedure TSubCuentasController.AsignarDataModule; begin FDataModule := TDataModuleContabilidad.Create(Nil); end; procedure TSubCuentasController.AsignarSubCuentaAContacto(ASubCuenta: IBizSubCuenta; ASubCuentaContacto: IBizSubCuentasContacto); begin if Assigned(ASubCuenta) then begin EliminarSubCuentaContacto(ASubCuentaContacto); ASubCuentaContacto.Insert; ASubCuentaContacto.ID := ASubCuenta.ID; ASubCuentaContacto.REF_SUBCUENTA := ASubCuenta.REF_SUBCUENTA; ASubCuentaContacto.DESCRIPCION := ASubCuenta.DESCRIPCION; ASubCuentaContacto.post; end; end; function TSubCuentasController.Buscar(ID: Integer): IBizSubCuenta; var Condicion: TDAWhereExpression; begin ShowHourglassCursor; try Result := BuscarTodos; with Result.DataTable.DynamicWhere do begin // (ID = :ID) Condicion := NewBinaryExpression(NewField('', fld_SubCuentasID), NewConstant(ID, datInteger), dboEqual); if IsEmpty then Expression := Condicion else Expression := NewBinaryExpression(Expression, Condicion, dboAnd); end; finally HideHourglassCursor; end; end; function TSubCuentasController.BuscarCuentas(TipoCuenta: String): IBizSubCuenta; var Condicion1: TDAWhereExpression; Condicion2: TDAWhereExpression; begin ShowHourglassCursor; try Result := BuscarTodos; with Result.DataTable.DynamicWhere do begin // (REF_CUENTA = 430/400) (SOLO CUENTAS DE CLIENTES, PROVEEDORES QUE ESTEN SIN ASIGNAR) Condicion1 := NewBinaryExpression(NewField('', fld_SubCuentasREF_CUENTA), NewConstant(TipoCuenta, datString), dboEqual); Condicion2 := NewBinaryExpression(NewField('', fld_SubCuentasID_CONTACTO), NewNull(), dboEqual); if IsEmpty then Expression := Condicion1 else Expression := NewBinaryExpression(Expression, Condicion1, dboAnd); Expression := NewBinaryExpression(Expression, Condicion2, dboAnd); end; finally HideHourglassCursor; end; end; function TSubCuentasController.BuscarTodos: IBizSubCuenta; begin Result := FDataModule.GetSubCuentaItems; FiltrarEjercicio(Result); end; constructor TSubCuentasController.Create; begin inherited; AsignarDataModule; end; function TSubCuentasController.CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean; begin Result := Supports(EditorRegistry.CreateEditor(AName), IID, Intf); end; function TSubCuentasController.DarListaSubCuentas: TStringList; var ASubCuentas: IBizSubCuenta; begin ASubCuentas := BuscarTodos; ASubCuentas.DataTable.Active := True; Result := TStringList.Create; try with Result do begin ASubCuentas.DataTable.First; while not ASubCuentas.DataTable.EOF do begin Add(ASubCuentas.DESCRIPCION); ASubCuentas.DataTable.Next; end; end; finally ASubCuentas := NIL; end; end; procedure TSubCuentasController.DescartarCambios(ASubCuenta: IBizSubCuenta); begin if not Assigned(ASubCuenta) then raise Exception.Create ('SubCuenta no asignado'); ShowHourglassCursor; try if (ASubCuenta.State in dsEditModes) then ASubCuenta.Cancel; ASubCuenta.DataTable.CancelUpdates; finally HideHourglassCursor; end; end; destructor TSubCuentasController.Destroy; begin FDataModule:= NIL; inherited; end; function TSubCuentasController.ValidarSubCuenta(ASubCuenta: IBizSubCuenta): Boolean; begin Result := False; if not Assigned(ASubCuenta) then raise Exception.Create ('SubCuenta no asignado'); if (ASubCuenta.DataTable.State in dsEditModes) then ASubCuenta.DataTable.Post; if Length(ASubCuenta.REF_SUBCUENTA) = 0 then raise Exception.Create('Debe indicar una referencia para esta subcuenta.'); if Length(ASubCuenta.DESCRIPCION) = 0 then raise Exception.Create('Debe indicar una descripcion para esta subcuenta.'); Result := True; end; procedure TSubCuentasController.Ver(ASubCuenta: IBizSubCuenta); var AEditor : IEditorSubCuenta; begin AEditor := NIL; ShowHourglassCursor; try CreateEditor('EditorSubCuenta', IEditorSubCuenta, AEditor); if Assigned(AEditor) then with AEditor do begin Controller := Self; //OJO ORDEN MUY IMPORTANTE SubCuenta := ASubCuenta; ShowModal; Release; end; finally AEditor := NIL; HideHourglassCursor; end; end; procedure TSubCuentasController.VerSubCuentaContacto(ASubCuentaContacto: IBizSubCuentasContacto); var ASubCuenta: IBizSubCuenta; begin if Assigned(ASubCuentaContacto) then begin ASubCuenta := Buscar(ASubCuentaContacto.ID); Ver(ASubCuenta); end; if Assigned(ASubCuenta) then begin if Assigned(ASubCuentaContacto) and (ASubCuentaContacto.DataTable.RecordCount > 0) then ASubCuentaContacto.DataTable.Delete; ASubCuentaContacto.Insert; ASubCuentaContacto.ID := ASubCuenta.ID; ASubCuentaContacto.REF_SUBCUENTA := ASubCuenta.REF_SUBCUENTA; ASubCuentaContacto.DESCRIPCION := ASubCuenta.DESCRIPCION; ASubCuentaContacto.post; end; end; procedure TSubCuentasController.VerTodos(ASubCuentas: IBizSubCuenta); var AEditor : IEditorSubCuentas; begin AEditor := NIL; ShowHourglassCursor; try CreateEditor('EditorSubCuentas', IEditorSubCuentas, AEditor); if Assigned(AEditor) then with AEditor do begin Controller := Self; //OJO ORDEN MUY IMPORTANTE SubCuentas := ASubCuentas; ShowEmbedded; end; finally AEditor := NIL; HideHourglassCursor; end; end; procedure TSubCuentasController.ElegirCuenta(ASubCuenta: IBizSubCuenta); var ACuenta : IBizCuenta; AController : ICuentasController; begin inherited; try AController := TCuentasController.Create; ACuenta := (AController.ElegirCuenta(AController.BuscarTodos, 'ddddddd', False) as IBizCuenta); if Assigned(ACuenta) then begin if Assigned(ASubCuenta) then begin if not ASubCuenta.DataTable.Editing then ASubCuenta.DataTable.Edit; ASubCuenta.ID_CUENTA := ACuenta.ID; ASubCuenta.CUENTA := ACuenta.DESCRIPCION; ASubCuenta.Post; end; end; finally AController := Nil; ACuenta := Nil; end; end; function TSubCuentasController.ElegirSubCuenta(ASubCuentas: IBizSubCuenta; AMensaje: String; AMultiSelect: Boolean): IBizSubCuenta; var AEditor : IEditorElegirSubCuentas; begin Result := NIL; CreateEditor('EditorElegirSubCuentas', IEditorElegirSubCuentas, AEditor); try with AEditor do begin SubCuentas := ASubCuentas; Controller := Self; Mensaje := AMensaje; if IsPositiveResult(ShowModal) then Result := SubCuentasSeleccionados; Release; end; finally AEditor := NIL; end; end; procedure TSubCuentasController.ElegirSubCuentaContacto(ASubCuentaContacto: IBizSubCuentasContacto; TipoCuenta: String); var ASubCuenta: IBizSubCuenta; begin ASubCuenta := BuscarCuentas(TipoCuenta); if (TipoCuenta = CTE_CUENTASCLIENTE) then ASubCuenta := ElegirSubCuenta(ASubCuenta, 'El cliente debe tener una subcuenta asociada', False) else ASubCuenta := ElegirSubCuenta(ASubCuenta, 'El proveedor debe tener una subcuenta asociada', False); AsignarSubCuentaAContacto(ASubCuenta, ASubCuentaContacto); end; function TSubCuentasController.Eliminar(ASubCuenta: IBizSubCuenta): Boolean; begin Result := False; if not Assigned(ASubCuenta) then raise Exception.Create ('SubCuenta no asignado'); ShowHourglassCursor; try if (ASubCuenta.State in dsEditModes) then ASubCuenta.Cancel; ASubCuenta.Delete; ASubCuenta.DataTable.ApplyUpdates; HideHourglassCursor; Result := True; finally HideHourglassCursor; end; end; procedure TSubCuentasController.EliminarSubCuentaContacto(ASubCuentaContacto: IBizSubCuentasContacto); begin if Assigned(ASubCuentaContacto) and (not ASubCuentaContacto.DataTable.IsEmpty) then ASubCuentaContacto.DataTable.Delete; end; function TSubCuentasController.ExtraerSeleccionados(ASubCuentas: IBizSubCuenta): IBizSubCuenta; var ASeleccionados : IBizSubCuenta; begin ASeleccionados := Self.Buscar(ID_NULO); CopyDataTableDA3(ASubCuentas.DataTable, ASeleccionados.DataTable, True); Result := ASeleccionados; end; procedure TSubCuentasController.FiltrarEjercicio(ASubCuenta: IBizSubCuenta); var Condicion: TDAWhereExpression; begin if ASubCuenta.DataTable.Active then ASubCuenta.DataTable.Active := False; // Filtrar los SubCuentas actuales por SubCuenta activo with ASubCuenta.DataTable.DynamicWhere do begin // (ID_Ejercicio = ID) Condicion := NewBinaryExpression(NewField('', fld_SubCuentasID_Ejercicio), NewConstant(AppFactuGES.EjercicioActivo.ID, datInteger), dboEqual); if IsEmpty then Expression := Condicion else Expression := NewBinaryExpression(Expression, Condicion, dboAnd); end; end; procedure TSubCuentasController.RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable); begin inherited; // end; function TSubCuentasController.Guardar(ASubCuenta: IBizSubCuenta): Boolean; begin Result := False; if ValidarSubCuenta(ASubCuenta) then begin ShowHourglassCursor; try ASubCuenta.DataTable.ApplyUpdates; Result := True; finally HideHourglassCursor; end; end; end; function TSubCuentasController.Localizar(ASubCuentas: IBizSubCuenta; ADescripcion: String): Boolean; begin Result := True; ShowHourglassCursor; try with ASubCuentas.DataTable do begin DisableControls; First; if not Locate(fld_SubCuentasDESCRIPCION, ADescripcion, []) then Result := False; EnableControls; end; finally HideHourglassCursor; end; end; function TSubCuentasController.Nuevo: IBizSubCuenta; var ASubCuenta : IBizSubCuenta; begin ASubCuenta := FDataModule.NewSubCuentaItem; FiltrarEjercicio(ASubCuenta); ASubCuenta.DataTable.Active := True; ASubCuenta.Insert; Result := ASubCuenta; end; end.