unit srvEjercicios_Impl;

{----------------------------------------------------------------------------}
{ This unit was automatically generated by the RemObjects SDK after reading  }
{ the RODL file associated with this project .                               }
{                                                                            }
{ This is where you are supposed to code the implementation of your objects. }
{----------------------------------------------------------------------------}

interface

uses
  {vcl:} Classes, SysUtils, 
  {RemObjects:} uROClientIntf, uROTypes, uROServer, uROServerIntf, uROSessions,
  {Ancestor Implementation:} DataAbstractService_Impl,
  {Used RODLs:} DataAbstract4_Intf,
  {Generated:} FactuGES_Intf, uDADataTable, uDABINAdapter, uDAClasses,
  uDAScriptingProvider, uDABusinessProcessor, uDADataStreamer,
  uDABin2DataStreamer;

type
  { TsrvEjercicios }
  TsrvEjercicios = class(TDataAbstractService, IsrvEjercicios)
    Diagrams: TDADiagrams;
    Bin2DataStreamer: TDABin2DataStreamer;
    bpEjercicios: TDABusinessProcessor;
    schEjercicios: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DARemoteServiceCreate(Sender: TObject);
    procedure DataAbstractServiceBeforeAcquireConnection(aSender: TObject; var aConnectionName: string);

  protected
    function GenerarPGC(const ID_EJERCICIO_COPIA: Integer; const ID_EJERCICIO: Integer): Boolean;
    function CerrarEjercicio(const ID_EJERCICIO_CIERRE: Integer; const ID_EJERCICIO_APERTURA: Integer): Boolean;
  public  
    function DarEjercicioActivo(const ID_EMPRESA: Integer): Integer;
  end;

implementation

{$R *.dfm}
uses
  {Generated:} FactuGES_Invk, uDAInterfaces, uBizEjerciciosServer, uDataModuleServer, uROClasses,
  uDatabaseUtils;

procedure Create_srvEjercicios(out anInstance : IUnknown);
begin
  anInstance := TsrvEjercicios.Create(NIL);
end;

{ srvEjercicios }
function TsrvEjercicios.CerrarEjercicio(const ID_EJERCICIO_CIERRE, ID_EJERCICIO_APERTURA: Integer): Boolean;
begin
  Result := False;

  Connection.BeginTransaction;
  try
    schEjercicios.NewCommand(Connection, 'CerrarEjercicio', ['ID_EJERCICIO_CIERRE', 'ID_EJERCICIO_APERTURA'], [ID_EJERCICIO_CIERRE, ID_EJERCICIO_APERTURA]);
    Connection.CommitTransaction;
    Result := True;
  except
    Connection.RollbackTransaction;
    RaiseError('Error al cerrar el ejercicio');
  end;
end;

function TsrvEjercicios.DarEjercicioActivo(const ID_EMPRESA: Integer): Integer;
var
  AConn : IDAConnection;
  dsData: IDADataset;

begin
  Result := -1;
  AConn := dmServer.ConnectionManager.NewConnection(dmServer.ConnectionManager.GetDefaultConnectionName);

  try
    try
      dsData := schEjercicios.NewDataset(AConn, 'darEjercicioActivo', ['ID_EMPRESA'], [ID_EMPRESA]);
      dsData.Active := True;
      Result := dsData.FieldValues[0];
      AConn.CommitTransaction;

    except
      RaiseError('Error al buscar el ejercicio activo de la empresa');
      AConn.RollbackTransaction;
    end;
  finally
    dsData := NIL;
  end;
end;

procedure TsrvEjercicios.DARemoteServiceCreate(Sender: TObject);
begin
  //SessionManager := dmServer.SessionManager;
  bpEjercicios.BusinessRulesID := BIZ_SERVER_EJERCICIOS;  
end;

procedure TsrvEjercicios.DataAbstractServiceBeforeAcquireConnection(
  aSender: TObject; var aConnectionName: string);
begin
  ConnectionName := dmServer.ConnectionName;
end;

function TsrvEjercicios.GenerarPGC(const ID_EJERCICIO_COPIA, ID_EJERCICIO: Integer): Boolean;
begin
  Result := False;

  Connection.BeginTransaction;
  try
    schEjercicios.NewCommand(Connection, 'GenerarPGC', ['ID_EJERCICIO_COPIA', 'ID_EJERCICIO'], [ID_EJERCICIO_COPIA, ID_EJERCICIO]);
    Connection.CommitTransaction;
    Result := True;
  except
    Connection.RollbackTransaction;
    RaiseError('Error al generar el plan general contable');
  end;
end;

initialization
  TROClassFactory.Create('srvEjercicios', Create_srvEjercicios, TsrvEjercicios_Invoker);

finalization

end.
