unit srvVentasTerminadas_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:} VARELA_Intf, uDAClasses,
  uDataModuleServer, uDAInterfaces, uDADataTable, uDABINAdapter;

type
  { TsrvVentasTerminadas }
  TsrvVentasTerminadas = class(TDARemoteService, IsrvVentasTerminadas)
    DABINAdapter: TDABINAdapter;
    schVentasTerminadas: TDASchema;
  private
  protected
    { IsrvVentasTerminadas methods }
    function GetData(const DatasetName: String; const MaxRecords: Integer; const Columns: String; const Params: TDADatasetParamArray): Binary;
    function GetSchema(const DatasetName: String; const Columns: String): Binary;
    function GetDataCrossTab(const FechaIni: String; const FechaFin: String; const Coleccion: String): Binary;
    function GetSchemaCrossTab(const FechaIni: String; const FechaFin: String; const Coleccion: String): Binary;
  end;

implementation

{$R *.dfm}
uses
  {Generated:} VARELA_Invk, uSchemaUtilsServer, dialogs;

const
  CAMPOS_SUMA = '#TALLA38#TALLA40#TALLA42#TALLA44#TALLA46#TALLA48#VENTAS#DEVOLUCIONES#TOTAL#VALVEN#VALDEV#VALTOT#VTATERMDEV#VTAFINALRESC#VALTERMDEV#VALFINALRESC#';

procedure Create_srvVentasTerminadas(out anInstance : IUnknown);
begin
  anInstance := TsrvVentasTerminadas.Create(NIL);
end;

{ srvVentasTerminadas }

function TsrvVentasTerminadas.GetData(const DatasetName: String;
  const MaxRecords: Integer; const Columns: String;
  const Params: TDADatasetParamArray): TROBinaryMemoryStream;
var
  ds : IDADataset;
  i : integer;
begin
  result := Binary.Create;
  try
    { Prepares the parameter arrays}

    with TReplicarDataSet.Create do
    begin
      SourceDataSetName := DatasetName;
      Service := Self;
      Columnas := Columns;
      ColumnasQueSuman := CAMPOS_SUMA;
      try
        ds := GetNewDataSet;
      finally
        Free;
      end;
    end;

    for i := 0 to (Params.Count-1) do
      ds.ParamByName(Params[i].Name).Value := Params[i].Value;
    ds.Open;
    ServiceAdapter.WriteDataset(Result, ds, [woRows], MaxRecords);
  except
    FreeAndNIl(result);
    raise;
  end;
end;

function TsrvVentasTerminadas.GetDataCrossTab(const FechaIni, FechaFin: String; const Coleccion: String): Binary;
var
  ds : IDADataset;
  AColeccion : String;
begin
  if (Length(Coleccion) = 0) or (UpperCase(Coleccion) = 'TODAS') then
    AColeccion := '%'
  else
    AColeccion := Coleccion;
    
  try
    Result := Binary.Create;
    ds := schVentasTerminadas.NewDataset(Connection, 'VentasTerminadasCrossTab', ['FECHAINI', 'FECHAFIN', 'COLECCION'], [FechaIni, FechaFin, AColeccion]);//['01/07/2006', '31/07/2006']);
    ds.Open;
    ServiceAdapter.WriteDataset(Result, ds, [woSchema, woRows], -1);
  except
    FreeAndNIl(result);
    raise;
  end;
end;

function TsrvVentasTerminadas.GetSchema(const DatasetName: String; const Columns: String): TROBinaryMemoryStream;
var
  ds : IDADataset;
begin
  result := Binary.Create;
  try
    with TReplicarDataSet.Create do
    begin
      SourceDataSetName := DatasetName;
      Service := Self;
      Columnas := Columns;
      ColumnasQueSuman := CAMPOS_SUMA;
      try
        ds := GetNewDataSet;
      finally
        Free;
      end;
    end;
    ServiceAdapter.WriteDataset(result, ds, [woSchema]);
  except
    FreeAndNIl(result);
    raise;
  end;
end;

function TsrvVentasTerminadas.GetSchemaCrossTab(const FechaIni, FechaFin: String; const Coleccion: String): Binary;
var
  ds : IDADataset;
  AColeccion : String;
begin
  if (Length(Coleccion) = 0) or (UpperCase(Coleccion) = 'TODAS') then
    AColeccion := '%'
  else
    AColeccion := Coleccion;

  try
    Result := Binary.Create;
    ds := schVentasTerminadas.NewDataset(Connection, 'VentasTerminadasCrossTab', ['FECHAINI', 'FECHAFIN', 'COLECCION'], [FechaIni, FechaFin, AColeccion]);//['01/07/2006', '31/07/2006']);
    ds.Open;
    ServiceAdapter.WriteDataset(Result, ds, [woSchema]);
  except
    FreeAndNIl(result);
    raise;
  end;
end;

initialization
  TROClassFactory.Create('srvVentasTerminadas', Create_srvVentasTerminadas, TsrvVentasTerminadas_Invoker);

finalization

end.
