unit srvReferenciaGenerica_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, uDAScriptingProvider,
  uDABusinessProcessor, uDADataTable, uDACDSDataTable, frxClass, DB,
  frxDBSet, ADODB, uDAInterfaces, uROClient, uROEventRepository,
  uDABINAdapter;

type
  { TsrvReferenciaGenerica }
  TsrvReferenciaGenerica = class(TDARemoteService, IsrvReferenciaGenerica)
    bpReferenciaGenerica: TDABusinessProcessor;
    frxDBCabecera: TfrxDBDataset;
    dsReferenciasGenericas: TDADataSource;
    tbl_ReferenciasGenericas: TDACDSDataTable;
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    DataSource1: TDataSource;
    frxReport1: TfrxReport;
    DABINAdapter: TDABINAdapter;
    schReferenciaGenerica: TDASchema;
    procedure DARemoteServiceCreate(Sender: TObject);
  private
  protected
    function GetProducto(const Codigo: String; const Talla: String;
      const Color: String; out CodigoBarras: String; out Descripcion: String): Boolean;
    function GenerateReport(const Centro: String; const Vendedor: String;
      const Estado: String; const FiltroUsuario: String): Binary;
    procedure PasarProductosAEspera(const Centro: String; const Vendedor: String);
  end;

implementation

{$R *.dfm}
uses
  {Generated:} VARELA_Invk, uDataModuleServer,
  schReferenciaGenericaClient_Intf, uBizReferenciaGenericaServer, Dialogs;

procedure Create_srvReferenciaGenerica(out anInstance : IUnknown);
begin
  anInstance := TsrvReferenciaGenerica.Create(NIL);
end;

{ srvReferenciaGenerica }
{ TsrvReferenciaGenerica }

function TsrvReferenciaGenerica.GenerateReport(const Centro,
  Vendedor: String; const Estado: String; const FiltroUsuario: String): Binary;
var
  ds: IDADataset;
  AStream: TMemoryStream;
  AWhere : String;
begin
  AStream := TMemoryStream.Create;
  Result := Binary.Create;
  AWhere := '';
  try
    ds := schReferenciaGenerica.NewDataset(Connection, 'InformeRefGenerica');

    if Length(Centro) > 0 then
    begin
      AWhere := 'CODCENTRO = ''' + Centro + '''';
    end;

    if Length(Vendedor) > 0 then
    begin
      if Length(AWhere) > 0 then
        AWhere := AWhere + ' AND ';
      AWhere := AWhere + 'CODVENDEDOR = ''' + Vendedor + '''';
    end;

    if Length(Estado) > 0 then
    begin
      if Length(AWhere) > 0 then
        AWhere := AWhere + ' AND ';
      AWhere := AWhere + '((ESTADO = ''' + Estado + ''') ';
      if Estado = 'P' then
        AWhere := AWhere + 'OR (ESTADO is null)';
      AWhere := AWhere + ')';
    end;

    if Length(FiltroUsuario) > 0 then
    begin
      if Length(AWhere) > 0 then
        AWhere := AWhere + ' AND ';
      AWhere := AWhere + '(' + FiltroUsuario + ')';
    end;

    ds.Where.AddText(AWhere);

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

    frxReport1.LoadFromFile(DarRutaInformes + 'InformeReferenciaGenerica.fr3', True);
    frxReport1.PrepareReport;
    frxReport1.PreviewPages.SaveToStream(Result);
  finally
    AStream.Free;
  end;
end;


function TsrvReferenciaGenerica.GetProducto(const Codigo: String; const Talla: String;
  const Color: String; out CodigoBarras: String; out Descripcion: String): Boolean;
var
  ds : IDADataset;
  conn : IDAConnection;
begin
  conn := dmServer.ConnectionManager.NewConnection(ConnectionName);
  ds := schReferenciaGenerica.NewDataset(conn, nme_GetProducto);

  ds.ParamByName('CODIGO').AsString := Codigo;
  ds.ParamByName('TALLA').AsString := Talla;
  ds.ParamByName('COLOR').AsString := Color;
  try
    ds.Open;
    Result := not ds.IsEmpty;
    if Result then
    begin
      Descripcion := ds.FieldValues[idx_GetProductoDESC_PRODUTO];
      CodigoBarras := ds.FieldValues[idx_GetProductoCODIGO_BARRA];
    end;
  finally
    ds.Close;
    ds := nil;
    conn := nil;
  end;
end;

procedure TsrvReferenciaGenerica.PasarProductosAEspera(const Centro,
  Vendedor: String);
var
  cmd : IDASQLCommand;
  conn : IDAConnection;
begin
  conn := dmServer.ConnectionManager.NewConnection(ConnectionName);
  conn.BeginTransaction;

  cmd := schReferenciaGenerica.NewCommand(conn, 'CambiarEstado');
  try
    if Length(Centro) > 0 then
    begin
      cmd.Where.AddText('V.CODCENTRO = ''' + Centro + '''');
      if Length(Vendedor) > 0 then
        cmd.Where.AddText('AND V.CODVENDEDOR = ''' + Vendedor + '''');
    end;
    cmd.Execute;
    conn.CommitTransaction;
  except
    conn.RollbackTransaction;
  end;
end;

procedure TsrvReferenciaGenerica.DARemoteServiceCreate(Sender: TObject);
begin
 bpReferenciaGenerica.BusinessRulesID := BIZ_SERVER_REFERENCIA_GENERICA;
end;

initialization
  TROClassFactory.Create('srvReferenciaGenerica', Create_srvReferenciaGenerica, TsrvReferenciaGenerica_Invoker);

finalization

end.
