unit uAsistenteCargaFicheros;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JvWizard, JvExControls, JvComponent, StdCtrls, cxControls,
  cxContainer, cxEdit, cxTextEdit, cxMaskEdit, cxSpinEdit,
  cxGraphics, cxDropDownEdit, Mask, JvExMask, JvToolEdit, cxGroupBox,
  ComCtrls, uROClient, SyncObjs, VARELA_Intf,
  uROServiceComponent, uRORemoteService, uDataModuleBase, pngimage,
  ExtCtrls, JvExComCtrls, JvAnimate;

type
  TfAsistenteCargaFicheros = class(TForm)
    JvWizard1: TJvWizard;
    JvWizardWelcomePage1: TJvWizardWelcomePage;
    Label1: TLabel;
    pageRutas: TJvWizardInteriorPage;
    Label3: TLabel;
    pagFinal: TJvWizardWelcomePage;
    lblFinalizar: TLabel;
    cbVentas: TCheckBox;
    cbInventario: TCheckBox;
    Label2: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    edtCABVEN: TJvFilenameEdit;
    edtLUGVEN: TJvFilenameEdit;
    edtARTVEN: TJvFilenameEdit;
    cxGroupBox1: TcxGroupBox;
    cxGroupBox2: TcxGroupBox;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    JvFilenameEdit1: TJvFilenameEdit;
    JvFilenameEdit2: TJvFilenameEdit;
    JvFilenameEdit3: TJvFilenameEdit;
    Label7: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    pbarCABVEN: TProgressBar;
    pbarLUGVEN: TProgressBar;
    pbarARTVEN: TProgressBar;
    RORemoteService: TRORemoteService;
    imgOK1: TImage;
    imgOK2: TImage;
    imgOK3: TImage;
    Bevel1: TBevel;
    imgBAD1: TImage;
    imgBAD2: TImage;
    imgBAD3: TImage;
    imgOK4: TImage;
    imgBAD4: TImage;
    lblError: TLabel;
    JvAnimate1: TJvAnimate;
    bCargar: TButton;
    procedure JvWizard1FinishButtonClick(Sender: TObject);
    procedure cbVentasClick(Sender: TObject);
    procedure edtCABVENChange(Sender: TObject);
    procedure edtLUGVENChange(Sender: TObject);
    procedure edtARTVENChange(Sender: TObject);
    procedure pagFinalEnterPage(Sender: TObject;
      const FromPage: TJvWizardCustomPage);
    procedure bCargarClick(Sender: TObject);
    procedure pagFinalPage(Sender: TObject);
  private
    { Private declarations }
    FCritical: TCriticalSection;
    FFileService: IsrvTransfereciaFicheros;
    procedure ActualizarBotonesNav;
    procedure OnUploadAborted(Sender: TObject);
    procedure OnUploadError(Sender: TObject);
    procedure OnUploadFinished(Sender: TObject);
    procedure OnUploadProgress(Sender: TObject);
    procedure OnUploadStarted(Sender: TObject);
    procedure OnLoadSuccess(Sender: TObject);
    procedure OnLoadError(Sender : TObject);
    procedure CargarFicherosEDI;
    procedure SubirFicheros;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

  procedure VerAsistenteEtiquetasMontajes;

implementation

uses
  uSubirFicheros, uCargarFicheros;

{$R *.dfm}

procedure VerAsistenteEtiquetasMontajes;
var
  fAsistenteCargaFicheros: TfAsistenteCargaFicheros;
begin
  with TfAsistenteCargaFicheros.Create(NIL) do
    try
      ShowModal;
    finally
      Free;
    end;
end;


procedure TfAsistenteCargaFicheros.JvWizard1FinishButtonClick(
  Sender: TObject);
begin
  Close;
end;

procedure TfAsistenteCargaFicheros.cbVentasClick(Sender: TObject);
begin
  edtCABVEN.Enabled := (Sender as TCheckBox).Checked;
  edtLUGVEN.Enabled := (Sender as TCheckBox).Checked;
  edtARTVEN.Enabled := (Sender as TCheckBox).Checked;
  ActualizarBotonesNav;
end;

procedure TfAsistenteCargaFicheros.ActualizarBotonesNav;
var
  bActivarNext : Boolean;
begin
  bActivarNext := True;
  if JvWizard1.ActivePageIndex = 1 then //  Rutas de ficheros
  begin
    bActivarNext := bActivarNext and (cbVentas.Checked or cbInventario.Checked);

    bActivarNext := bActivarNext and
                    ((Length(edtCABVEN.Text) > 0) and
                     (Length(edtLUGVEN.Text) > 0) and
                     (Length(edtARTVEN.Text) > 0));
    pageRutas.EnableButton(bkNext, bActivarNext);
  end;
end;

procedure TfAsistenteCargaFicheros.edtCABVENChange(Sender: TObject);
begin
  ActualizarBotonesNav;
end;

procedure TfAsistenteCargaFicheros.edtLUGVENChange(Sender: TObject);
begin
  ActualizarBotonesNav;
end;

procedure TfAsistenteCargaFicheros.edtARTVENChange(Sender: TObject);
begin
  ActualizarBotonesNav;
end;

procedure TfAsistenteCargaFicheros.OnUploadAborted(Sender: TObject);
begin
  fCritical.Enter;
  with Sender as TSubirFicheroThread do
  try
    if Pos(FileName, edtCABVEN.Text) > 0 then
      imgBAD1.Visible := True;
    if Pos(FileName, edtLUGVEN.Text) > 0 then
      imgBAD2.Visible := True;
    if Pos(FileName, edtARTVEN.Text) > 0 then
      imgBAD3.Visible := True;
  finally
    fCritical.Leave;
  end;
end;

procedure TfAsistenteCargaFicheros.OnUploadError(Sender: TObject);
begin
  fCritical.Enter;
  with Sender as TSubirFicheroThread do
  try
    if Pos(FileName, edtCABVEN.Text) > 0 then
      imgBAD1.Visible := True;
    if Pos(FileName, edtLUGVEN.Text) > 0 then
      imgBAD2.Visible := True;
    if Pos(FileName, edtARTVEN.Text) > 0 then
      imgBAD3.Visible := True;
  finally
    fCritical.Leave;
  end;
end;

procedure TfAsistenteCargaFicheros.OnUploadFinished(Sender: TObject);
begin
  fCritical.Enter;
  with Sender as TSubirFicheroThread do
  try
    if UploadOK then
    begin
      if Pos(FileName, edtCABVEN.Text) > 0 then
        imgOK1.Visible := True;
      if Pos(FileName, edtLUGVEN.Text) > 0 then
        imgOK2.Visible := True;
      if Pos(FileName, edtARTVEN.Text) > 0 then
        imgOK3.Visible := True;
    end;
  finally
    fCritical.Leave;
  end;
  if imgOK1.Visible and imgOK2.Visible and imgOK3.Visible then
    bCargar.Enabled := True;
end;

procedure TfAsistenteCargaFicheros.OnUploadProgress(Sender: TObject);
begin
  FCritical.Enter;
  try
    with Sender as TSubirFicheroThread do
    begin
      if Pos(FileName, edtCABVEN.Text) > 0 then
        pbarCABVEN.Position := CurrentBytePos;
      if Pos(FileName, edtLUGVEN.Text) > 0 then
        pbarLUGVEN.Position := CurrentBytePos;
      if Pos(FileName, edtARTVEN.Text) > 0 then
        pbarARTVEN.Position := CurrentBytePos;
    end;
  finally
    fCritical.Leave;
  end;
end;

procedure TfAsistenteCargaFicheros.OnUploadStarted(Sender: TObject);
begin
  FCritical.Enter;
  with Sender as TSubirFicheroThread do
  try
    if Pos(FileName, edtCABVEN.Text) > 0 then
      pbarCABVEN.Max := FileSize;
    if Pos(FileName, edtLUGVEN.Text) > 0 then
      pbarLUGVEN.Max := FileSize;
    if Pos(FileName, edtARTVEN.Text) > 0 then
      pbarARTVEN.Max := FileSize;
  finally
    FCritical.Leave;
  end;
end;

constructor TfAsistenteCargaFicheros.Create(AOwner: TComponent);
begin
  inherited;
  FCritical := TCriticalSection.create;
  FFileService := RORemoteService as IsrvTransfereciaFicheros;
end;

destructor TfAsistenteCargaFicheros.Destroy;
begin
  FFileService := nil;
  FCritical.Free;
  inherited;
end;

procedure TfAsistenteCargaFicheros.pagFinalEnterPage(
  Sender: TObject; const FromPage: TJvWizardCustomPage);
begin
  SubirFicheros;
end;

procedure TfAsistenteCargaFicheros.CargarFicherosEDI;
begin
  JvAnimate1.Visible := True;
  JvAnimate1.Active := True;
  Label13.Visible := True;
  pagFinal.EnableButton(bkBack, False);
  pagFinal.EnableButton(bkCancel, False);
  imgBAD4.Visible := False;
  imgOK4.Visible := False;
  lblError.Visible := False;

  TCargarVentasThread.Create(ExtractFileName(edtCABVEN.Text), ExtractFileName(edtLUGVEN.Text),
    ExtractFileName(edtARTVEN.Text), OnLoadSuccess, OnLoadError);

{  TCargarInventarioThread.Create(ExtractFileName(edtCABVEN.Text), ExtractFileName(edtLUGVEN.Text),
    ExtractFileName(edtARTVEN.Text), OnLoadSuccess, OnLoadError);}
end;

procedure TfAsistenteCargaFicheros.SubirFicheros;
begin
  TSubirFicheroThread.Create(edtCABVEN.Text, OnUploadStarted, OnUploadProgress,
    OnUploadFinished, OnUploadAborted, OnUploadError);

  TSubirFicheroThread.Create(edtLUGVEN.Text, OnUploadStarted, OnUploadProgress,
    OnUploadFinished, OnUploadAborted, OnUploadError);

  TSubirFicheroThread.Create(edtARTVEN.Text, OnUploadStarted, OnUploadProgress,
    OnUploadFinished, OnUploadAborted, OnUploadError);
end;

procedure TfAsistenteCargaFicheros.OnLoadError(Sender: TObject);
begin
  JvAnimate1.Visible := False;
  JvAnimate1.Active := False;
  imgBAD4.Visible := True;
  lblError.Caption := (Sender as TCargarFicherosThread).ErrorText;
  lblError.Visible := True;
  pagFinal.EnableButton(bkBack, True);
  pagFinal.EnableButton(bkCancel, True);
end;

procedure TfAsistenteCargaFicheros.OnLoadSuccess(Sender: TObject);
begin
  Beep;
  JvAnimate1.Visible := False;
  JvAnimate1.Active := False;
  imgOK4.Visible := True;
  Label13.Caption := 'Ficheros cargados correctamente';
  lblFinalizar.Visible := True;
  pagFinal.EnableButton(bkFinish, True);
end;

procedure TfAsistenteCargaFicheros.bCargarClick(Sender: TObject);
begin
  bCargar.Enabled := False;
  CargarFicherosEDI;
end;

procedure TfAsistenteCargaFicheros.pagFinalPage(Sender: TObject);
begin
  bCargar.Enabled := False;
  imgOK1.Visible := False;
  imgOK2.Visible := False;
  imgOK3.Visible := False;
  imgOK4.Visible := False;
  imgBAD1.Visible := False;
  imgBAD2.Visible := False;
  imgBAD3.Visible := False;
  imgBAD4.Visible := False;
  pbarCABVEN.Position := 0;
  pbarLUGVEN.Position := 0;
  pbarARTVEN.Position := 0;
  JvAnimate1.Visible := False;
  lblFinalizar.Visible := False;
  lblError.Visible := False;
  Label13.Visible := False;
  Label13.Caption := 'Cargando datos desde los ficheros...';
  pagFinal.EnableButton(bkBack, True);
  pagFinal.EnableButton(bkCancel, True);
  pagFinal.EnableButton(bkFinish, False);
end;

end.
