397 lines
11 KiB
ObjectPascal
397 lines
11 KiB
ObjectPascal
|
|
unit uEjerciciosController;
|
|||
|
|
|
|||
|
|
interface
|
|||
|
|
|
|||
|
|
uses
|
|||
|
|
Classes, SysUtils, uDADataTable, uControllerBase,
|
|||
|
|
uBizEjercicios, uIDataModuleEjercicios;
|
|||
|
|
|
|||
|
|
type
|
|||
|
|
IEjerciciosController = interface(IControllerBase)
|
|||
|
|
['{94E5F2B6-64C8-4331-B9CB-3ED730478529}']
|
|||
|
|
function BuscarTodos: IBizEjercicio;
|
|||
|
|
function Buscar(ID: Integer): IBizEjercicio;
|
|||
|
|
function BuscarActivo(IDEmpresa: Integer): IBizEjercicio;
|
|||
|
|
procedure VerTodos(AEjercicios: IBizEjercicio);
|
|||
|
|
procedure Ver(AEjercicio: IBizEjercicio);
|
|||
|
|
procedure Anadir(AEjercicio : IBizEjercicio);
|
|||
|
|
function Eliminar(AEjercicio : IBizEjercicio): Boolean;
|
|||
|
|
function Guardar(AEjercicio : IBizEjercicio): Boolean;
|
|||
|
|
function GenerarPCG(IdEjercicioCopia, IdEjercicio: Integer): Boolean;
|
|||
|
|
procedure DescartarCambios(AEjercicio : IBizEjercicio);
|
|||
|
|
function Localizar(AEjercicios: IBizEjercicio; ADescripcion:String): Boolean;
|
|||
|
|
function DarListaEjercicios: TStringList;
|
|||
|
|
function ElegirEjercicio(AEjercicios : IBizEjercicio): IBizEjercicio;
|
|||
|
|
function ExtraerSeleccionados(AEjercicios: IBizEjercicio) : IBizEjercicio;
|
|||
|
|
function CerrarEjercicio(AEjercicio: IBizEjercicio): Boolean;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
TEjerciciosController = class(TControllerBase, IEjerciciosController)
|
|||
|
|
private
|
|||
|
|
procedure FiltrarEmpresa(AEjercicio: IBizEjercicio);
|
|||
|
|
|
|||
|
|
protected
|
|||
|
|
FDataModule : IDataModuleEjercicios;
|
|||
|
|
|
|||
|
|
procedure RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable); override;
|
|||
|
|
function CreateEditor(const AName : String; const IID: TGUID; out Intf): Boolean;
|
|||
|
|
|
|||
|
|
function ValidarEjercicio(AEjercicio: IBizEjercicio): Boolean;
|
|||
|
|
procedure AsignarDataModule;
|
|||
|
|
|
|||
|
|
public
|
|||
|
|
constructor Create; override;
|
|||
|
|
destructor Destroy; override;
|
|||
|
|
|
|||
|
|
function Eliminar(AEjercicio : IBizEjercicio): Boolean;
|
|||
|
|
function Guardar(AEjercicio : IBizEjercicio): Boolean; virtual;
|
|||
|
|
procedure DescartarCambios(AEjercicio : IBizEjercicio); virtual;
|
|||
|
|
procedure Anadir(AEjercicio : IBizEjercicio);
|
|||
|
|
function BuscarTodos: IBizEjercicio;
|
|||
|
|
function Buscar(ID: Integer): IBizEjercicio;
|
|||
|
|
function BuscarActivo(IDEmpresa: Integer): IBizEjercicio;
|
|||
|
|
procedure VerTodos(AEjercicios: IBizEjercicio);
|
|||
|
|
procedure Ver(AEjercicio: IBizEjercicio);
|
|||
|
|
function GenerarPCG(IdEjercicioCopia, IdEjercicio: Integer): Boolean;
|
|||
|
|
function Localizar(AEjercicios: IBizEjercicio; ADescripcion:String): Boolean;
|
|||
|
|
function DarListaEjercicios: TStringList;
|
|||
|
|
function ElegirEjercicio(AEjercicios : IBizEjercicio): IBizEjercicio;
|
|||
|
|
function ExtraerSeleccionados(AEjercicios: IBizEjercicio) : IBizEjercicio;
|
|||
|
|
function CerrarEjercicio(AEjercicio: IBizEjercicio): Boolean;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
implementation
|
|||
|
|
|
|||
|
|
uses
|
|||
|
|
cxControls, DB, uEditorRegistryUtils, schEjerciciosClient_Intf,
|
|||
|
|
uIEditorEjercicios, uIEditorEjercicio, uDataModuleEjercicios,
|
|||
|
|
uDAInterfaces, uDataTableUtils, uDialogUtils, uFactuGES_App,
|
|||
|
|
uDateUtils, uROTypes, DateUtils, Controls, Windows, Dialogs, uIEditorElegirEjercicios;
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
{ TEjerciciosController }
|
|||
|
|
|
|||
|
|
procedure TEjerciciosController.Anadir(AEjercicio: IBizEjercicio);
|
|||
|
|
begin
|
|||
|
|
AEjercicio.Insert;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
procedure TEjerciciosController.AsignarDataModule;
|
|||
|
|
begin
|
|||
|
|
FDataModule := TDataModuleEjercicios.Create(Nil);
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TEjerciciosController.Buscar(ID: Integer): IBizEjercicio;
|
|||
|
|
var
|
|||
|
|
Condicion: TDAWhereExpression;
|
|||
|
|
begin
|
|||
|
|
ShowHourglassCursor;
|
|||
|
|
try
|
|||
|
|
Result := BuscarTodos;
|
|||
|
|
|
|||
|
|
with Result.DataTable.DynamicWhere do
|
|||
|
|
begin
|
|||
|
|
// (ID = :ID)
|
|||
|
|
Condicion := NewBinaryExpression(NewField('', fld_EjerciciosID), NewConstant(ID, datInteger), dboEqual);
|
|||
|
|
|
|||
|
|
if IsEmpty then
|
|||
|
|
Expression := Condicion
|
|||
|
|
else
|
|||
|
|
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
finally
|
|||
|
|
HideHourglassCursor;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TEjerciciosController.BuscarActivo(IDEmpresa: Integer): IBizEjercicio;
|
|||
|
|
var
|
|||
|
|
Condicion: TDAWhereExpression;
|
|||
|
|
begin
|
|||
|
|
ShowHourglassCursor;
|
|||
|
|
try
|
|||
|
|
Result := BuscarTodos;
|
|||
|
|
|
|||
|
|
with Result.DataTable.DynamicWhere do
|
|||
|
|
begin
|
|||
|
|
// (ACTIVO = 1)
|
|||
|
|
Condicion := NewBinaryExpression(NewField('', fld_EjerciciosACTIVO), NewConstant(1, datInteger), dboEqual);
|
|||
|
|
|
|||
|
|
if IsEmpty then
|
|||
|
|
Expression := Condicion
|
|||
|
|
else
|
|||
|
|
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
finally
|
|||
|
|
HideHourglassCursor;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TEjerciciosController.BuscarTodos: IBizEjercicio;
|
|||
|
|
begin
|
|||
|
|
Result := FDataModule.GetItems;
|
|||
|
|
FiltrarEmpresa(Result);
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TEjerciciosController.CerrarEjercicio(AEjercicio: IBizEjercicio): Boolean;
|
|||
|
|
var
|
|||
|
|
IdEjercicioCierre: Integer;
|
|||
|
|
AFechaInicioApertura: TDate;
|
|||
|
|
AFechaFinApertura: TDate;
|
|||
|
|
|
|||
|
|
begin
|
|||
|
|
Result := False;
|
|||
|
|
|
|||
|
|
if Assigned(AEjercicio) then
|
|||
|
|
begin
|
|||
|
|
IdEjercicioCierre := AEjercicio.ID;
|
|||
|
|
AFechaInicioApertura := StartOfAYear((YearOf(AEjercicio.FECHA_INICIO)+1));
|
|||
|
|
AFechaFinApertura := EndOfAYear((YearOf(AEjercicio.FECHA_INICIO)+1));
|
|||
|
|
|
|||
|
|
Anadir(AEjercicio);
|
|||
|
|
AEjercicio.FECHA_INICIO := AFechaInicioApertura;
|
|||
|
|
AEjercicio.FECHA_FIN := AFechaFinApertura;
|
|||
|
|
AEjercicio.NOMBRE := 'Ejercicio ' + IntToStr(YearOf(AFechaInicioApertura));
|
|||
|
|
AEjercicio.ESTADO := CTE_ABIERTO;
|
|||
|
|
Guardar(AEjercicio);
|
|||
|
|
GenerarPCG(IdEjercicioCierre, AEjercicio.ID);
|
|||
|
|
Result := FDataModule.CerrarEjercicio(IdEjercicioCierre, AEjercicio.ID);
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
constructor TEjerciciosController.Create;
|
|||
|
|
begin
|
|||
|
|
inherited;
|
|||
|
|
AsignarDataModule;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TEjerciciosController.CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;
|
|||
|
|
begin
|
|||
|
|
Result := Supports(EditorRegistry.CreateEditor(AName), IID, Intf);
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TEjerciciosController.DarListaEjercicios: TStringList;
|
|||
|
|
var
|
|||
|
|
AEjercicios: IBizEjercicio;
|
|||
|
|
begin
|
|||
|
|
AEjercicios := BuscarTodos;
|
|||
|
|
AEjercicios.DataTable.Active := True;
|
|||
|
|
Result := TStringList.Create;
|
|||
|
|
try
|
|||
|
|
with Result do
|
|||
|
|
begin
|
|||
|
|
AEjercicios.DataTable.First;
|
|||
|
|
while not AEjercicios.DataTable.EOF do
|
|||
|
|
begin
|
|||
|
|
Add(AEjercicios.NOMBRE);
|
|||
|
|
AEjercicios.DataTable.Next;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
finally
|
|||
|
|
AEjercicios := NIL;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
procedure TEjerciciosController.DescartarCambios(AEjercicio: IBizEjercicio);
|
|||
|
|
begin
|
|||
|
|
if not Assigned(AEjercicio) then
|
|||
|
|
raise Exception.Create ('Ejercicio no asignado');
|
|||
|
|
|
|||
|
|
ShowHourglassCursor;
|
|||
|
|
try
|
|||
|
|
if (AEjercicio.State in dsEditModes) then
|
|||
|
|
AEjercicio.Cancel;
|
|||
|
|
|
|||
|
|
AEjercicio.DataTable.CancelUpdates;
|
|||
|
|
finally
|
|||
|
|
HideHourglassCursor;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
destructor TEjerciciosController.Destroy;
|
|||
|
|
begin
|
|||
|
|
FDataModule:= NIL;
|
|||
|
|
inherited;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TEjerciciosController.ValidarEjercicio(AEjercicio: IBizEjercicio): Boolean;
|
|||
|
|
begin
|
|||
|
|
|
|||
|
|
if not Assigned(AEjercicio) then
|
|||
|
|
raise Exception.Create ('Ejercicio no asignado');
|
|||
|
|
|
|||
|
|
if (AEjercicio.DataTable.State in dsEditModes) then
|
|||
|
|
AEjercicio.DataTable.Post;
|
|||
|
|
|
|||
|
|
if (AEjercicio.DataTable.RecordCount = 1)
|
|||
|
|
and (AEjercicio.ACTIVO = 0) then
|
|||
|
|
raise Exception.Create('Debe existir un ejercicio activo.');
|
|||
|
|
|
|||
|
|
if Length(AEjercicio.NOMBRE) = 0 then
|
|||
|
|
raise Exception.Create('Debe indicar un nombre para este ejercicio.');
|
|||
|
|
|
|||
|
|
Result := True;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
procedure TEjerciciosController.Ver(AEjercicio: IBizEjercicio);
|
|||
|
|
var
|
|||
|
|
AEditor : IEditorEjercicio;
|
|||
|
|
begin
|
|||
|
|
AEditor := NIL;
|
|||
|
|
|
|||
|
|
CreateEditor('EditorEjercicio', IEditorEjercicio, AEditor);
|
|||
|
|
if Assigned(AEditor) then
|
|||
|
|
try
|
|||
|
|
AEditor.Controller := Self; //OJO ORDEN MUY IMPORTANTE
|
|||
|
|
AEditor.Ejercicio := AEjercicio;
|
|||
|
|
AEditor.ShowModal;
|
|||
|
|
finally
|
|||
|
|
AEditor.Release;
|
|||
|
|
AEditor := NIL
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
procedure TEjerciciosController.VerTodos(AEjercicios: IBizEjercicio);
|
|||
|
|
var
|
|||
|
|
AEditor : IEditorEjercicios;
|
|||
|
|
begin
|
|||
|
|
AEditor := NIL;
|
|||
|
|
|
|||
|
|
CreateEditor('EditorEjercicios', IEditorEjercicios, AEditor);
|
|||
|
|
if Assigned(AEditor) then
|
|||
|
|
with AEditor do
|
|||
|
|
begin
|
|||
|
|
Controller := Self; //OJO ORDEN MUY IMPORTANTE
|
|||
|
|
Ejercicios := AEjercicios;
|
|||
|
|
ShowEmbedded;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TEjerciciosController.ElegirEjercicio(AEjercicios: IBizEjercicio): IBizEjercicio;
|
|||
|
|
var
|
|||
|
|
AEditor : IEditorElegirEjercicios;
|
|||
|
|
begin
|
|||
|
|
Result := NIL;
|
|||
|
|
|
|||
|
|
CreateEditor('EditorElegirEjercicios', IEditorElegirEjercicios, AEditor);
|
|||
|
|
if Assigned(AEditor) then
|
|||
|
|
try
|
|||
|
|
AEditor.Ejercicios := AEjercicios;
|
|||
|
|
AEditor.Controller := Self;
|
|||
|
|
AEditor.MultiSelect := False;
|
|||
|
|
AEditor.Mensaje := 'Seleccione el ejercicio de la empresa activa que desee:';
|
|||
|
|
if IsPositiveResult(AEditor.ShowModal) then
|
|||
|
|
Result := AEditor.EjerciciosSeleccionados;
|
|||
|
|
finally
|
|||
|
|
AEditor.Release;
|
|||
|
|
AEditor := NIL;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TEjerciciosController.Eliminar(AEjercicio: IBizEjercicio): Boolean;
|
|||
|
|
begin
|
|||
|
|
if not Assigned(AEjercicio) then
|
|||
|
|
raise Exception.Create ('Ejercicio no asignado');
|
|||
|
|
|
|||
|
|
ShowHourglassCursor;
|
|||
|
|
try
|
|||
|
|
if (AEjercicio.State in dsEditModes) then
|
|||
|
|
AEjercicio.Cancel;
|
|||
|
|
|
|||
|
|
//ATENCI<43>N!! Debe haber claves foraneas en las tablas de CONT_EPIGRAFES, CONT_CUENTAS, CONT_SUBCUENTAS
|
|||
|
|
//con el fin de que al borrar un ejercicio se borren todos sus datos correspondientes
|
|||
|
|
AEjercicio.Delete;
|
|||
|
|
AEjercicio.DataTable.ApplyUpdates;
|
|||
|
|
AEjercicio.DataTable.Refresh; //Debemos refrescar en este caso para que se vea el nuevo ejercicio activo en el cliente, como no ser<65>n muchos ejercicios nos da igual
|
|||
|
|
HideHourglassCursor;
|
|||
|
|
Result := True;
|
|||
|
|
finally
|
|||
|
|
HideHourglassCursor;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TEjerciciosController.ExtraerSeleccionados(AEjercicios: IBizEjercicio): IBizEjercicio;
|
|||
|
|
var
|
|||
|
|
ASeleccionados : IBizEjercicio;
|
|||
|
|
begin
|
|||
|
|
ASeleccionados := Self.Buscar(ID_NULO);
|
|||
|
|
CopyDataTableDA5(AEjercicios.DataTable, ASeleccionados.DataTable, True);
|
|||
|
|
Result := ASeleccionados;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
procedure TEjerciciosController.FiltrarEmpresa(AEjercicio: IBizEjercicio);
|
|||
|
|
var
|
|||
|
|
Condicion: TDAWhereExpression;
|
|||
|
|
begin
|
|||
|
|
if AEjercicio.DataTable.Active then
|
|||
|
|
AEjercicio.DataTable.Active := False;
|
|||
|
|
|
|||
|
|
// Filtrar las facturas actuales por empresa
|
|||
|
|
with AEjercicio.DataTable.DynamicWhere do
|
|||
|
|
begin
|
|||
|
|
// (ID_EMPRESA >= ID)
|
|||
|
|
Condicion := NewBinaryExpression(NewField('', fld_EjerciciosID_EMPRESA), NewConstant(AppFactuGES.EmpresaActiva.ID, datInteger), dboEqual);
|
|||
|
|
|
|||
|
|
if IsEmpty then
|
|||
|
|
Expression := Condicion
|
|||
|
|
else
|
|||
|
|
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
procedure TEjerciciosController.RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable);
|
|||
|
|
begin
|
|||
|
|
inherited;
|
|||
|
|
//
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TEjerciciosController.GenerarPCG(IdEjercicioCopia, IdEjercicio: Integer): Boolean;
|
|||
|
|
begin
|
|||
|
|
ShowHourglassCursor;
|
|||
|
|
try
|
|||
|
|
Result := FDataModule.GenerarPGC(IdEjercicioCopia, IdEjercicio);
|
|||
|
|
finally
|
|||
|
|
HideHourglassCursor;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TEjerciciosController.Guardar(AEjercicio: IBizEjercicio): Boolean;
|
|||
|
|
begin
|
|||
|
|
Result := False;
|
|||
|
|
|
|||
|
|
if ValidarEjercicio(AEjercicio) then
|
|||
|
|
begin
|
|||
|
|
ShowHourglassCursor;
|
|||
|
|
try
|
|||
|
|
AEjercicio.DataTable.ApplyUpdates;
|
|||
|
|
AppFactuGES.CambiarEjercicio(AppFactuGES.EmpresaActiva.ID);
|
|||
|
|
Result := True;
|
|||
|
|
finally
|
|||
|
|
HideHourglassCursor;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function TEjerciciosController.Localizar(AEjercicios: IBizEjercicio; ADescripcion: String): Boolean;
|
|||
|
|
begin
|
|||
|
|
Result := True;
|
|||
|
|
ShowHourglassCursor;
|
|||
|
|
try
|
|||
|
|
with AEjercicios.DataTable do
|
|||
|
|
begin
|
|||
|
|
DisableControls;
|
|||
|
|
First;
|
|||
|
|
if not Locate(fld_EjerciciosNOMBRE, ADescripcion, []) then
|
|||
|
|
Result := False;
|
|||
|
|
EnableControls;
|
|||
|
|
end;
|
|||
|
|
finally
|
|||
|
|
HideHourglassCursor;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
end.
|