unit uCargarFicheros;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls,
  uROClient, uROClientIntf, uRORemoteService, uROBinMessage,
  uROServiceComponent, uROWinInetHttpChannel, Forms,
  VARELA_Intf, uROTypes, uROEventRepository;

type
  TCargarFicherosThread = class(TThread)
  private
    FROMessage: TROBinMessage;
    fROChannel: TROWinInetHTTPChannel;
    fRORemoteService: TRORemoteService;
    FEventReceiver : TROEventReceiver;

    fOnSuccess: TNotifyEvent;
    fOnError: TNotifyEvent;

    fLoadOK: Boolean;
    fTimeStarted: TDateTime;
    fErrorText: string;
    fInfoStr: string;

    FMaxConnectionErrors : Integer;
    FChannelErrorscount: Integer;

    procedure Run;
  protected
    FNombre : String;
    procedure CargarFicheros; virtual; abstract;
    procedure OnChannelFailure(Sender: TROTransportChannel;
      anException: Exception; var Retry: Boolean);
    procedure OnLoadSuccess; virtual;
    procedure OnLoadError(const ErrorMsg: String); virtual;
  public
    property Nombre : string read FNombre;
    property LoadOK: Boolean read floadOK;
    property TimeStarted: TDateTime read fTimeStarted;
    property ErrorText: string read fErrortext;
    property InfoStr: string read fInfoStr;
    destructor Destroy; override;
    procedure Execute; override;
    constructor Create;
  end;


  { TCargarVentasThread }
  TCargarVentasThread = class(TCargarFicherosThread)
  private
    fEDISalesService: IEDI_Sales;

    FFicheroCAB : String;
    FFicheroLUG : String;
    FFicheroART : String;
  protected
    procedure CargarFicheros; override;
  public
    destructor Destroy; override;
    constructor Create(const FicheroCAB: String; const FicheroLUG: String;
        const FicheroART: String; aOnLoadSuccess, aOnLoadError: TNotifyEvent);
  end;


  { TCargarInventarioThread }
  TCargarInventarioThread = class(TCargarFicherosThread)
  private
    fEDIInventaryService: IEDI_Inventary;

    FFicheroCAB : String;
    FFicheroLIN : String;
    FFicheroCANT : String;

  protected
    procedure CargarFicheros; override;
  public
    destructor Destroy; override;
    constructor Create(const FicheroCAB: String; const FicheroLIN: String;
        const FicheroCANT: String; aOnLoadSuccess, aOnLoadError: TNotifyEvent);
  end;


implementation

uses
  uDataModuleBase, cxControls;

{ TROThread }


destructor TCargarFicherosThread.Destroy;
begin
  fROChannel.OnFailure := NIL;

  FreeAndNil(fRORemoteService);
  FreeAndNil(fROChannel);
  FreeAndNil(fROMessage);
  inherited;
end;

procedure TCargarFicherosThread.Run;
begin
  ShowHourglassCursor;
  try
    CargarFicheros;
  finally
    HideHourglassCursor;
  end;
end;

procedure TCargarFicherosThread.Execute;
begin
  try
    Run;
  finally
    Self.Destroy;
  end;
end;

procedure TCargarFicherosThread.OnLoadError(const ErrorMsg: String);
begin
  FErrorText := ErrorMsg;
  if assigned(FOnError) then
    FOnError(Self);
end;

procedure TCargarFicherosThread.OnLoadSuccess;
begin
  fLoadOK := True;
  if assigned(FOnSuccess) then
    FOnSuccess(Self);
end;

procedure TCargarFicherosThread.OnChannelFailure(
  Sender: TROTransportChannel; anException: Exception; var Retry: Boolean);
begin
  if FChannelErrorsCount > FMaxConnectionErrors then
  begin
    FErrorText := anException.Message;
    Self.Terminate;
  end
  else
  begin
    Inc(FChannelErrorsCount);
    Sleep(1000);
    Retry := true;
  end;
end;

constructor TCargarFicherosThread.Create;
begin
  inherited Create(True);
  FMaxConnectionErrors := 5; //try 5 times on channel-error
  fLoadOK := False;

  fROMessage := TROBinMessage.Create(nil);
  fROChannel := TROWinInetHTTPChannel.Create(NIL);
  fROChannel.OnFailure := OnChannelFailure;
  fROChannel.TargetURL := dmBase.Channel.TargetURL;
end;

{ TCargarVentasThread }

procedure TCargarVentasThread.CargarFicheros;
var
  MsgError : String;
begin
  try
    if not fEDISalesService.CargarFicheroEDI(FFicheroCAB, FFicheroLUG,
      FFicheroART, MsgError) then
      OnLoadError(MsgError)
    else
      OnLoadSuccess;
  except
    on e: Exception do
      OnLoadError(e.Message);
  end;
end;

constructor TCargarVentasThread.Create(const FicheroCAB, FicheroLUG,
  FicheroART: String; aOnLoadSuccess, aOnLoadError: TNotifyEvent);
begin
  inherited Create;
  FNombre := 'SLSRPT';
  fRORemoteService := TRORemoteService.Create(nil);
  fRORemoteService.Channel := fROChannel;
  fRORemoteService.Message := fROMessage;
  fRORemoteService.ServiceName := 'EDI_Sales';
  fEDISalesService := fRORemoteService as IEDI_Sales;

  FFicheroCAB := FicheroCAB;
  FFicheroLUG := FicheroLUG;
  FFicheroART := FicheroART;

  if assigned(aOnLoadSuccess) then
    fOnSuccess := aOnLoadSuccess;
  if assigned(aOnLoadError) then
    fOnError := aOnLoadError;
  Resume;
end;

destructor TCargarVentasThread.Destroy;
begin
  FEDISalesService := NIL;
  inherited;
end;

{ TCargarInventarioThread }

procedure TCargarInventarioThread.CargarFicheros;
var
  MsgError : String;
begin
  inherited;
  try
    if not fEDIInventaryService.CargarFicheroEDI(FFicheroCAB, FFicheroLIN,
      FFicheroCANT, MsgError) then
      OnLoadError(MsgError)
    else
      OnLoadSuccess;
  except
    on e: Exception do
      OnLoadError(e.Message);
  end;
end;

constructor TCargarInventarioThread.Create(const FicheroCAB, FicheroLIN,
  FicheroCANT: String; aOnLoadSuccess, aOnLoadError: TNotifyEvent);
begin
  inherited Create;
  FNombre := 'INVRPT';
  fRORemoteService := TRORemoteService.Create(nil);
  fRORemoteService.Channel := fROChannel;
  fRORemoteService.Message := fROMessage;
  fRORemoteService.ServiceName := 'EDI_Inventary';
  fEDIInventaryService := fRORemoteService as IEDI_Inventary;

  FFicheroCAB := FicheroCAB;
  FFicheroLIN := FicheroLIN;
  FFicheroCANT := FicheroCANT;

  if assigned(aOnLoadSuccess) then
    fOnSuccess := aOnLoadSuccess;
  if assigned(aOnLoadError) then
    fOnError := aOnLoadError;
  Resume;
end;

destructor TCargarInventarioThread.Destroy;
begin
  fEDIInventaryService := nil;
  inherited;
end;

end.

