unit uDataModuleEDI_Sales;

interface

uses
  SysUtils, Classes, uDAScriptingProvider, uDADataTable, uDACDSDataTable,
  uBizEDISales, uDAEngine, uDAADODriver, uDABINAdapter,
  uDABusinessProcessor, uDAClasses, EDI_Reader, JvComponentBase;

type
  TdmEDISales = class(TDataModule)
    schEDISales: TDASchema;
  private
    function InsertarCabecera(const ACabData : TEDIFileData; var NumRegNuevos : integer) : Boolean;
    function InsertarLugares(const ALugData : TEDIFileData; var NumRegNuevos : integer) : Boolean;
    function InsertarArticulos(const AArtData : TEDIFileData; var NumRegNuevos : integer) : Boolean;
    procedure InsertarLog(OK : Boolean; Error_msg : String = '');    
  public
    function CargarFicheros(const FicheroCAB: String; const FicheroLUG: String;
      const FicheroART: String; var NumRegNuevos: Integer ): Boolean;
  end;

var
  dmEDISales: TdmEDISales;

implementation

uses
  EDI_Sales_Impl, Controls, Forms, uDataTableUtils, EDI_SLSRPT, uDAInterfaces, 
  Dialogs, schEDISalesClient_Intf, uDataModuleServer, Variants, EDI_Utils,
  VARELA_Intf, EDI_Log_Impl, DB;

{$R *.dfm}

{ TdmEDISales }

function TdmEDISales.CargarFicheros(const FicheroCAB: String; const FicheroLUG: String;
  const FicheroART: String; var NumRegNuevos: Integer ): Boolean;
var
  Cab_Reader : TCABVEN_Reader;
  Lug_Reader : TLUGVEN_Reader;
  Art_Reader : TARTVEN_Reader;

  NumRegNuevosCab : Integer;
  NumRegNuevosLug : Integer;
  NumRegNuevosArt : Integer;
begin
  Result := False;
  Cab_Reader := TCABVEN_Reader.Create;
  Lug_Reader := TLUGVEN_Reader.Create;
  Art_Reader := TARTVEN_Reader.Create;

  try
    Cab_Reader.Filename := FicheroCAB;
    Cab_Reader.ReadFile;
    InsertarCabecera (Cab_Reader.FileData, NumRegNuevosCab);
  finally
    FreeAndNIL(Cab_Reader);
  end;

  try
    Lug_Reader.Filename := FicheroLUG;
    Lug_Reader.ReadFile;
    InsertarLugares (Lug_Reader.FileData, NumRegNuevosLug);
  finally
    FreeAndNIL(Lug_Reader);
  end;

  try
    Art_Reader.Filename := FicheroART;
    Art_Reader.ReadFile;
    InsertarArticulos(Art_Reader.FileData, NumRegNuevosArt);
  finally
    FreeAndNil(Art_Reader);
  end;
  NumRegNuevos := NumRegNuevosArt;
  Result := True;
end;

function TdmEDISales.InsertarArticulos(const AArtData: TEDIFileData;
  var NumRegNuevos : integer): Boolean;
var
  i : integer;
  Registro : PARTVEN;
  AuxStr : String;
  AuxInt : Integer;
  AuxNum : Double;
  ACommand: IDASQLCommand;
  AConnection : IDAConnection;
begin
  Result := False;
  NumRegNuevos := 0;
  AConnection := dmServer.ConnectionManager.NewConnection(dmServer.ConnectionName);

  for i := 0 to AArtData.Count - 1 do
  begin
    Registro := AArtData[i];

    if not Registro.REGISTRO_OK then
      Continue;

    ACommand := schEDISales.NewCommand(AConnection, 'Insert_RDX_ARTVEN');
    with ACommand do
    begin
      ParamByName('CLAVE1').AsFloat := StrToFloat(Trim(Registro.CLAVE1));
      ParamByName('CLAVE2').AsFloat := StrToInt(Trim(Registro.CLAVE2));
      ParamByName('CLAVE3').AsFloat := StrToInt(Trim(Registro.CLAVE3));

      ParamByName('EANART').AsString := Trim(Registro.EANART);
      ParamByName('CODCOM').AsString := Trim(Registro.CODCOM);
      ParamByName('NUMVAR').AsString := Trim(Registro.NUMVAR);
      ParamByName('CODPRO').AsString := Trim(Registro.CODPRO);
      ParamByName('DESARTCO').AsString := Trim(Registro.DESARTCO);
      ParamByName('DESART').AsString := Trim(Registro.DESART);
      ParamByName('NUMCON').AsString := Trim(Registro.NUMCON);
      ParamByName('NUMLIS').AsString := Trim(Registro.NUMLIS);
      ParamByName('CONESP1').AsString := Trim(Registro.CONESP1);
      ParamByName('CONESP2').AsString := Trim(Registro.CONESP2);
      ParamByName('CONESP3').AsString := Trim(Registro.CONESP3);

      ParamByName('VALVEN').AsVariant := Null;
      AuxStr := Trim(Registro.VALVEN);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('VALVEN').AsFloat := AuxNum/1000;  //N(15,3)

      ParamByName('PRENETCT').AsVariant := Null;
      AuxStr := Trim(Registro.PRENETCT);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('PRENETCT').AsFloat := AuxNum/1000;  //N(15,3)

      ParamByName('PREBRUCT').AsVariant := Null;
      AuxStr := Trim(Registro.PREBRUCT);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('PREBRUCT').AsFloat := AuxNum/1000;  //N(15,3)

      ParamByName('PREINFET').AsVariant := Null;
      AuxStr := Trim(Registro.PREINFET);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('PREINFET').AsFloat := AuxNum/1000;  //N(15,3)

      ParamByName('PREINFFT').AsVariant := Null;
      AuxStr := Trim(Registro.PREINFFT);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('PREINFFT').AsFloat := AuxNum/1000;  //N(15,3)

      ParamByName('PRECALCT').AsVariant := Null;
      AuxStr := Trim(Registro.PRECALCT);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('PRECALCT').AsFloat := AuxNum/1000;  //N(15,3)

      ParamByName('PRENETCA').AsVariant := Null;
      AuxStr := Trim(Registro.PRENETCA);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('PRENETCA').AsFloat := AuxNum/1000;  //N(15,3)

      ParamByName('PREBRUCA').AsVariant := Null;
      AuxStr := Trim(Registro.PREBRUCA);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('PREBRUCA').AsFloat := AuxNum/1000;  //N(15,3)

      ParamByName('PREINFEA').AsVariant := Null;
      AuxStr := Trim(Registro.PREINFEA);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('PREINFEA').AsFloat := AuxNum/1000;  //N(15,3)

      ParamByName('PREINFFA').AsVariant := Null;
      AuxStr := Trim(Registro.PREINFFA);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('PREINFFA').AsFloat := AuxNum/1000;  //N(15,3)

      ParamByName('PRECALCA').AsVariant := Null;
      AuxStr := Trim(Registro.PRECALCA);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('PRECALCA').AsFloat := AuxNum/1000;  //N(15,3)


      ParamByName('BAPRUN1').AsVariant := Null;
      AuxStr := Trim(Registro.BAPRUN1);
      if TryStrToInt (AuxStr, AuxInt) then
        ParamByName('BAPRUN1').AsInteger := AuxInt;
      ParamByName('UNIMED1').AsString := Trim(Registro.UNIMED1);

      ParamByName('BAPRUN2').AsVariant := Null;
      AuxStr := Trim(Registro.BAPRUN2);
      if TryStrToInt (AuxStr, AuxInt) then
        ParamByName('BAPRUN2').AsInteger := AuxInt;
      ParamByName('UNIMED2').AsString := Trim(Registro.UNIMED2);

      ParamByName('BAPRUN3').AsVariant := Null;
      AuxStr := Trim(Registro.BAPRUN3);
      if TryStrToInt (AuxStr, AuxInt) then
        ParamByName('BAPRUN3').AsInteger := AuxInt;
      ParamByName('UNIMED3').AsString := Trim(Registro.UNIMED3);

      ParamByName('BAPRUN4').AsVariant := Null;
      AuxStr := Trim(Registro.BAPRUN4);
      if TryStrToInt (AuxStr, AuxInt) then
        ParamByName('BAPRUN4').AsInteger := AuxInt;
      ParamByName('UNIMED4').AsString := Trim(Registro.UNIMED4);

      ParamByName('BAPRUN5').AsVariant := Null;
      AuxStr := Trim(Registro.BAPRUN5);
      if TryStrToInt (AuxStr, AuxInt) then
        ParamByName('BAPRUN5').AsInteger := AuxInt;
      ParamByName('UNIMED5').AsString := Trim(Registro.UNIMED5);

      ParamByName('CANVEN').AsVariant := Null;
      AuxStr := Trim(Registro.CANVEN);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('CANVEN').AsFloat := AuxNum/1000;  // N(15,3)

      ParamByName('EXIPRO').AsVariant := Null;
      AuxStr := Trim(Registro.EXIPRO);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('EXIPRO').AsFloat := AuxNum/1000;  // N(15,3)

      ParamByName('DESEXI').AsVariant := Null;
      AuxStr := Trim(Registro.DESEXI);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('DESEXI').AsFloat := AuxNum/1000;  // N(15,3)

      ParamByName('ESPMEDCV').AsString := Trim(Registro.ESPMEDCV);
      ParamByName('ESPMEDEP').AsString := Trim(Registro.ESPMEDEP);
      ParamByName('ESPMEDDE').AsString := Trim(Registro.ESPMEDDE);

      ParamByName('CANDEV').AsVariant := Null;
      AuxStr := Trim(Registro.CANDEV);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('CANDEV').AsFloat := AuxNum/1000;  // N(15,3)

      ParamByName('EANVEN').AsString := Trim(Registro.EANVEN);
      ParamByName('NOMVEN').AsString := Trim(Registro.NOMVEN);
      ParamByName('NUMTALON').AsString := Trim(Registro.NUMTALON);

      ParamByName('CANVENNET').AsVariant := Null;
      AuxStr := Trim(Registro.CANVENNET);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('CANVENNET').AsFloat := AuxNum/1000;  // N(15,3)
      ParamByName('UMEDVENNET').AsString := Trim(Registro.UMEDVENNET);

      ParamByName('CANESP').AsVariant := Null;
      AuxStr := Trim(Registro.CANESP);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('CANESP').AsFloat := AuxNum/1000;  // N(15,3)
      ParamByName('UMEDESP').AsString := Trim(Registro.UMEDESP);
    end;

    AConnection.BeginTransaction;
    try
      ACommand.Execute;
      AConnection.CommitTransaction;
      NumRegNuevos := NumRegNuevos + 1;      
      Result := True;
    except
      on E : Exception do
      begin
        AConnection.RollbackTransaction;
        if Pos('PRIMARY KEY', E.Message) = 0 then
          InsertarLog(False, e.Message + ' (InsertarArticulos SLS)');
      end
    end;
  end; //for
end;

function TdmEDISales.InsertarCabecera(const ACabData: TEDIFileData;
  var NumRegNuevos : integer): Boolean;
var
  i : integer;
  Registro : PCABVEN;
  AuxStr : String;
  AuxNum : Double;
  ACommand: IDASQLCommand;
  AConnection : IDAConnection;  
begin
  Result := False;
  NumRegNuevos := 0;
  AConnection := dmServer.ConnectionManager.NewConnection(dmServer.ConnectionName);

  for i := 0 to ACabData.Count - 1 do
  begin
    Registro := ACabData[i];

    if not Registro.REGISTRO_OK then
      Continue;

    ACommand := schEDISales.NewCommand(AConnection, 'Insert_RDX_CABVEN');
    with ACommand do
    begin
      ParamByName('CLAVE1').AsFloat := StrToFloat(Trim(Registro.CLAVE1));
      ParamByName('NUMINF').AsString := Trim(Registro.NUMINF);
      ParamByName('FUNCION').AsString := Trim(Registro.FUNCION);

      ParamByName('FECINI').AsDateTime := EDIDateToDateF(Trim(Registro.FECINI));
      ParamByName('FECFIN').AsDateTime := EDIDateToDateF(Trim(Registro.FECFIN));
      ParamByName('FECINF').AsDateTime := EDIDateToDateF(Trim(Registro.FECINF));

      ParamByName('PERVENINI').AsVariant := Null;
      ParamByName('PERVENFIN').AsVariant := Null;
      if Length(Trim(Registro.PERVEN)) = 16 then
      begin
        ParamByName('PERVENINI').AsDateTime := EDIDateToDateF(Copy(Trim(Registro.PERVEN), 0, 8));
        ParamByName('PERVENFIN').AsDateTime := EDIDateToDateF(Copy(Trim(Registro.PERVEN), 9, 8));
      end;

      ParamByName('EANOFI').AsString := Trim(Registro.EANOFI);
      ParamByName('EANEMI').AsString := Trim(Registro.EANEMI);
      ParamByName('EANREC').AsString := Trim(Registro.EANREC);
      ParamByName('EANVEN').AsString := Trim(Registro.EANVEN);
      ParamByName('EANALM').AsString := Trim(Registro.EANALM);
      ParamByName('EANREP').AsString := Trim(Registro.EANREP);
      ParamByName('EANPRO').AsString := Trim(Registro.EANPRO);
      ParamByName('EANADMSA').AsString := Trim(Registro.EANADMSA);
      ParamByName('EANREPSR').AsString := Trim(Registro.EANREPSR);
      ParamByName('ADMVENSA').AsString := Trim(Registro.ADMVENSA);
      ParamByName('REPDEPSR').AsString := Trim(Registro.REPDEPSR);

      ParamByName('EMAILSA').AsString := Trim(Registro.EMAILSA);
      ParamByName('NUMFAXSA').AsString := Trim(Registro.NUMFAXSA);
      ParamByName('NUMTELSA').AsString := Trim(Registro.NUMTELSA);
      ParamByName('NUMTLXSA').AsString := Trim(Registro.NUMTLXSA);
      ParamByName('X400SA').AsString := Trim(Registro.X400SA);

      ParamByName('EMAILSR').AsString := Trim(Registro.EMAILSR);
      ParamByName('NUMFAXSR').AsString := Trim(Registro.NUMFAXSR);
      ParamByName('NUMTELSR').AsString := Trim(Registro.NUMTELSR);
      ParamByName('NUMTLXSR').AsString := Trim(Registro.NUMTLXSR);
      ParamByName('X400SR').AsString := Trim(Registro.X400SR);

      ParamByName('NUMPRE').AsString := Trim(Registro.NUMPRE);
      ParamByName('NUMINFSC').AsString := Trim(Registro.NUMINFSC);
      ParamByName('NUMCON').AsString := Trim(Registro.NUMCON);

      ParamByName('FECPRE').AsDateTime := EDIDateToDateF(Trim(Registro.FECPRE));
      ParamByName('FECINFSC').AsDateTime := EDIDateToDateF(Trim(Registro.FECINFSC));
      ParamByName('FECCON').AsDateTime := EDIDateToDateF(Trim(Registro.FECCON));

      ParamByName('DIVPED').AsString := Trim(Registro.DIVPED);
      ParamByName('DIVPRE').AsString := Trim(Registro.DIVPRE);
      ParamByName('DIVPAG').AsString := Trim(Registro.DIVPAG);
      ParamByName('DIVPEDF').AsString := Trim(Registro.DIVPEDF);
      ParamByName('DIVPREF').AsString := Trim(Registro.DIVPREF);
      ParamByName('DIVPAGF').AsString := Trim(Registro.DIVPAGF);

      ParamByName('TASCAM').AsVariant := Null;
      AuxStr := Trim(Registro.TASCAM);
      if TryStrToFloat (AuxStr, AuxNum) then
        ParamByName('TASCAM').AsFloat := AuxNum/1000;  // N(15,3)

      ParamByName('FECTAS').AsDateTime := EDIDateToDateF(Trim(Registro.FECTAS));
    end;  // with

    AConnection.BeginTransaction;
    try
      ACommand.Execute;
      AConnection.CommitTransaction;
      NumRegNuevos := NumRegNuevos + 1;
      Result := True;
    except
      on E : Exception do
      begin
        AConnection.RollbackTransaction;
        if Pos('PRIMARY KEY', E.Message) = 0 then
          InsertarLog(False, e.Message + ' (InsertarCabecera SLS)');
      end
    end;

  end; //for
end;


procedure TdmEDISales.InsertarLog(OK: Boolean; Error_msg: String);
var
  LogService : IEDI_Log;
begin
  LogService := EDI_Log_Impl.TEDI_Log.Create(Self);
  try
    LogService.InsertarLog(Now, 'SLSRPT', OK, Error_msg);
  finally
    LogService := nil;
  end;
end;

function TdmEDISales.InsertarLugares(const ALugData: TEDIFileData;
  var NumRegNuevos : integer): Boolean;
var
  i : integer;
  Registro : PLUGVEN;
  ACommand: IDASQLCommand;
  AConnection : IDAConnection;
begin
  Result := False;
  NumRegNuevos := 0;
  AConnection := dmServer.ConnectionManager.NewConnection(dmServer.ConnectionName);

  for i := 0 to ALugData.Count - 1 do
  begin
    Registro := ALugData[i];

    if not Registro.REGISTRO_OK then
      Continue;

    ACommand := schEDISales.NewCommand(AConnection, 'Insert_RDX_LUGVEN');
    with ACommand do
    begin
      ParamByName('CLAVE1').AsFloat := StrToFloat(Trim(Registro.CLAVE1));
      ParamByName('CLAVE2').AsInteger := StrToInt(Trim(Registro.CLAVE2));
      ParamByName('EANLUG').AsString := Trim(Registro.EANLUG);
      ParamByName('LUGVEN').AsString := Trim(Registro.LUGVEN);

      ParamByName('FECINI').AsDateTime := EDIDateToDateF(Trim(Registro.FECINI));
      ParamByName('FECFIN').AsDateTime := EDIDateToDateF(Trim(Registro.FECFIN));

      ParamByName('PERVENINI').AsVariant := Null;
      ParamByName('PERVENFIN').AsVariant := Null;
      if Length(Trim(Registro.PERVEN)) >= 8 then
      begin
        ParamByName('PERVENINI').AsDateTime := EDIDateToDateF(Copy(Trim(Registro.PERVEN), 0, 8));
        if Length(Trim(Registro.PERVEN)) = 16 then
          ParamByName('PERVENFIN').AsDateTime := EDIDateToDateF(Copy(Trim(Registro.PERVEN), 9, 8))
      end
    end;  // with

    AConnection.BeginTransaction;
    try
      ACommand.Execute;
      AConnection.CommitTransaction;
      NumRegNuevos := NumRegNuevos + 1;      
      Result := True;
    except
      on E : Exception do
      begin
        AConnection.RollbackTransaction;
        if Pos('PRIMARY KEY', E.Message) = 0 then
          InsertarLog(False, e.Message + '(InsertarLugares SLS)');
      end
    end;
  end; //for
end;


initialization
  dmEDISales := TdmEDISales.Create(nil);

finalization
  FreeAndNil(dmEDISales);

end.

