unit srvFacturasCliente_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:} DARemoteService_Impl,
  {Used RODLs:} DataAbstract_Intf,
  {Generated:} FactuGES_Intf, uDAScriptingProvider, uDABusinessProcessor,
  uDAClasses, uDADataTable, uDABINAdapter,

  uDACDSDataTable, uDAInterfaces, DB, frxClass, frxDBSet;

type
  { TsrvFacturasCliente }
  TsrvFacturasCliente = class(TDARemoteService, IsrvFacturasCliente)
    frxDBDetalles: TfrxDBDataset;
    frxDBCabecera: TfrxDBDataset;
    DADataCabecera: TDADataSource;
    DADataDetalles: TDADataSource;
    tbl_FacturaCliente: TDACDSDataTable;
    tbl_DetallesFacturaCliente: TDACDSDataTable;
    DABINAdapter: TDABINAdapter;
    schFacturasCliente: TDASchema;
    frxReport1: TfrxReport;
    DataDictionary: TDADataDictionary;
    procedure DARemoteServiceBeforeAcquireConnection(Sender: TDARemoteService;
      var ConnectionName: string);
    procedure DARemoteServiceCreate(Sender: TObject);
    procedure DARemoteServiceBeforeGetDatasetData(const Dataset: IDADataset;
      const IncludeSchema: Boolean; const MaxRecords: Integer);
  private
  protected
    { IsrvFacturasCliente methods }
    function GetNextAutoInc(const GeneratorName: String): Integer;
    function GenerateReport(const ID: Integer): Binary;

    function DarNuevaReferencia: String;
  end;

implementation

{$R *.dfm}
uses
  Dialogs,
  {Generated:} FactuGES_Invk, uDataModuleServer, uDatabaseUtils,
  schFacturasClienteClient_Intf, uUsersManager;

  
procedure Create_srvFacturasCliente(out anInstance : IUnknown);
begin
  anInstance := TsrvFacturasCliente.Create(NIL);
end;

{ srvFacturasCliente }

procedure TsrvFacturasCliente.DARemoteServiceBeforeAcquireConnection(
  Sender: TDARemoteService; var ConnectionName: string);
begin
  ConnectionName := dmServer.ConnectionName;
end;

procedure TsrvFacturasCliente.DARemoteServiceBeforeGetDatasetData(
  const Dataset: IDADataset; const IncludeSchema: Boolean;
  const MaxRecords: Integer);
var
  AUserInfo : TUserInfo;
  WhereAnterior : String;
begin
  if DataSet.Name = nme_FacturasCliente then
  begin
    {
      Si el cliente manda alguna cláusula where,
      anteponer AND para unirla al where definido en el esquema.
    }

    if DataSet.Where.NotEmpty then
    begin
      WhereAnterior := DataSet.Where.Clause;
      DataSet.Where.Clear;
      DataSet.Where.AddOperator(opAND);
      DataSet.Where.AddText(WhereAnterior);
    end;

    { Aquí se asegura que el usuario sólo accede a facturas
      de las empresas a las que tiene permiso para acceder }

    AUserInfo := TUserInfo.Create(Session);
    try
      if not AUserInfo.EsAdministrador then
      begin
        if DataSet.Where.NotEmpty then
          DataSet.Where.AddOperator(opAND);
        DataSet.Where.AddCondition(fld_FacturasClienteID_EMPRESA,
          cIn, '(' + AUserInfo.Empresas + ')');
      end;
    finally
      FreeAndNil(AUserInfo);
    end;
  end;
end;

procedure TsrvFacturasCliente.DARemoteServiceCreate(Sender: TObject);
begin
  SessionManager := dmServer.SessionManager;
end;

function TsrvFacturasCliente.DarNuevaReferencia: String;
var
  dsReferencia: IDADataset;
begin
{
  dsReferencia := schFacturasCliente.NewDataset(Connection, 'DarReferenciaFactura');
  dsReferencia.Active := True;
  Result := dsReferencia.FieldByName('REFERENCIA').AsString;
}
end;

function TsrvFacturasCliente.GenerateReport(const ID: Integer): Binary;
var
  dsMaster: IDADataset;
  dsDetail: IDADataset;
  AStream: TMemoryStream;
begin
{  AStream := TMemoryStream.Create;
  Result := Binary.Create;
  try
    dsMaster := schFacturasCliente.NewDataset(Connection, 'InformeCabeceraFacturaCliente', ['ID'], [ID]);
    dsDetail := schFacturasCliente.NewDataset(Connection, 'InformeDetallesFacturaCliente', ['ID_FACTURA'], [ID], False);
    dsDetail.Active := True;

    AStream.Clear;
    ServiceAdapter.WriteDataset(AStream, dsMaster, [woRows, woSchema], -1);
    ServiceAdapter.ReadDataset(AStream, tbl_FacturaCliente, TRUE, '', TRUE, TRUE);

    AStream.Clear;
    ServiceAdapter.WriteDataset(AStream, dsDetail, [woRows, woSchema], -1);
    ServiceAdapter.ReadDataset(AStream, tbl_DetallesFacturaCliente, TRUE, '', TRUE, TRUE);
    frxReport1.Clear;

    frxReport1.LoadFromFile(DarRutaInformes + 'InfFacturaCliente.fr3', True);
//    frxReport1.PrepareReport;
//    frxReport1.PreviewPages.SaveToStream(Result);
//    frxReport1.ShowPreparedReport;
    frxReport1.ShowReport;
    frxReport1.Clear;
//    dsMaster.Close;
//    dsDetail.close;
  finally
    AStream.Free;
  end;}
end;

function TsrvFacturasCliente.GetNextAutoInc(
  const GeneratorName: String): Integer;
begin
  Result := uDatabaseUtils.GetNextAutoInc(GeneratorName)
end;

initialization
  TROClassFactory.Create('srvFacturasCliente', Create_srvFacturasCliente, TsrvFacturasCliente_Invoker);

finalization

end.
