unit uDataModuleEDI_Inventary;

interface

uses
  SysUtils, Classes, JvComponentBase, JvLogFile, uDAClasses,
  uDABusinessProcessor, uDAScriptingProvider, uDADataTable, uDACDSDataTable,
  uDataModuleServer, uBizEDIInventary, EDI_Reader, EDI_Inventary_Impl,
  EDI_INVRPT, uDAInterfaces;

type
  TdmEDIInventary = class(TDataModule)
    schEDIInventary: TDASchema;
  private
    function InsertarCabecera(const ACabData : TEDIFileData; var NumRegNuevos : integer) : Boolean;
    function InsertarLineas(const ALinData : TEDIFileData; var NumRegNuevos : integer) : Boolean;
    function InsertarCantidades(const ACantData : TEDIFileData; var NumRegNuevos : integer) : Boolean;
    procedure InsertarLog(OK : Boolean; Error_msg : String = '');
  public
    function CargarFicheros(const FicheroCAB: String; const FicheroLIN: String;
      const FicheroCANT: String; var NumRegNuevos: Integer ): Boolean;
  end;

var
  dmEDIInventary: TdmEDIInventary;

implementation

uses
  Controls, Forms, uDataTableUtils, DB,
  Dialogs, Variants, schEDIInventaryClient_Intf, EDI_Utils, EDI_Log_Impl,
  VARELA_Intf;

{$R *.dfm}

{ TdmEDIInventary }

function TdmEDIInventary.CargarFicheros(const FicheroCAB, FicheroLIN,
  FicheroCANT: String; var NumRegNuevos: Integer ): Boolean;
var
  Cab_Reader : TCABINV_Reader;
  Lin_Reader : TLININV_Reader;
  Cant_Reader : TCANTINV_Reader;

  NumRegNuevosCab : Integer;
  NumRegNuevosLin : Integer;
  NumRegNuevosCant : Integer;
begin
  Result := False;
  Cab_Reader := TCABINV_Reader.Create;
  Lin_Reader := TLININV_Reader.Create;
  Cant_Reader := TCANTINV_Reader.Create;

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

  try
    Lin_Reader.Filename := FicheroLIN;
    Lin_Reader.ReadFile;
    InsertarLineas (Lin_Reader.FileData, NumRegNuevosLin);
  finally
    FreeAndNIL(Lin_Reader);
  end;

  try
    Cant_Reader.Filename := FicheroCANT;
    Cant_Reader.ReadFile;
    InsertarCantidades(Cant_Reader.FileData, NumRegNuevosCant);
  finally
    Cant_Reader.Free;
  end;

  NumRegNuevos := NumRegNuevosCant;
  Result := True;
end;

function TdmEDIInventary.InsertarCabecera(const ACabData: TEDIFileData;
  var NumRegNuevos : integer) : Boolean;
var
  i : integer;
  Registro : PCABINV;
  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 := schEDIInventary.NewCommand(AConnection, 'Insert_RDX_CABINV');
    with ACommand do
    begin
      ParamByName('NUMDOC').AsString := Trim(Registro.NUMDOC);
      ParamByName('TIPODOC').AsString := Trim(Registro.TIPODOC);
      ParamByName('TIPOLIQ').AsString := Trim(Registro.TIPOLIQ);
      ParamByName('FECHA').AsDateTime := EDIDateToDateF(Trim(Registro.FECHA));
      ParamByName('FECHALIQ').AsDateTime := EDIDateToDateF(Trim(Registro.FECHALIQ));
      ParamByName('EMISOR').AsString := Trim(Registro.EMISOR);
      ParamByName('RECEPTOR').AsString := Trim(Registro.RECEPTOR);
      ParamByName('DPTO').AsString := Trim(Registro.DPTO);
      ParamByName('CENTRO').AsString := Trim(Registro.CENTRO);
    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 + ' (InsertarCabecera INV)');
      end
    end;
  end; //for
end;

function TdmEDIInventary.InsertarCantidades(const ACantData: TEDIFileData;
  var NumRegNuevos : integer) : Boolean;
var
  i : integer;
  Registro : PCANTINV;
  ACommand: IDASQLCommand;
  AConnection : IDAConnection;
begin
  Result := False;
  NumRegNuevos := 0;
  AConnection := dmServer.ConnectionManager.NewConnection(dmServer.ConnectionName);

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

    if not Registro.REGISTRO_OK then
      Continue;

    ACommand := schEDIInventary.NewCommand(AConnection, 'Insert_RDX_CANTINV');
    with ACommand do
    begin
      ParamByName('NUMDOC').AsString := Trim(Registro.NUMDOC);
      ParamByName('NUMLIN').AsInteger := StrToInt(Trim(Registro.NUMLIN));
      ParamByName('NUMCANT').AsInteger := StrToInt(Trim(Registro.NUMCANT));
      ParamByName('TIPOCANT').AsString := Trim(Registro.TIPOCANT);
      ParamByName('CANTIDAD').AsFloat := StrToFloat(Trim(Registro.CANTIDAD));
      ParamByName('CENTRO').AsString := Trim(Registro.CENTRO);
      ParamByName('TMOV').AsString := Trim(Registro.TMOV);
      ParamByName('FACT').AsString := Trim(Registro.FACT);
      ParamByName('DEPTO').AsString := Trim(Registro.DEPTO);
      ParamByName('IDVEN').AsString := Trim(Registro.IDVEN);
    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 + ' (InsertarCantidades INV)');
      end
    end;
  end; //for
end;

function TdmEDIInventary.InsertarLineas(const ALinData: TEDIFileData;
  var NumRegNuevos : integer) : Boolean;
var
  i : integer;
  Registro : PLININV;
  ACommand: IDASQLCommand;
  AConnection : IDAConnection;
begin
  Result := False;
  NumRegNuevos := 0;
  AConnection := dmServer.ConnectionManager.NewConnection(dmServer.ConnectionName);

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

    if not Registro.REGISTRO_OK then
      Continue;

    ACommand := schEDIInventary.NewCommand(AConnection, 'Insert_RDX_LININV');
    with ACommand do
    begin
      ParamByName('NUMDOC').AsString := Trim(Registro.NUMDOC);
      ParamByName('NUMLIN').AsInteger := StrToInt(Trim(Registro.NUMLIN));
      ParamByName('CODART').AsString := Trim(Registro.CODART);
      ParamByName('FAMILIA').AsString := Trim(Registro.FAMILIA);
      ParamByName('BARRA').AsString := Trim(Registro.BARRA);
      ParamByName('TALLA').AsString := Trim(Registro.TALLA);
      ParamByName('MARCA').AsString := Trim(Registro.MARCA);
      ParamByName('TAMANO').AsString := Trim(Registro.TAMANO);
      ParamByName('SERIE').AsString := Trim(Registro.SERIE);
      ParamByName('COLOR').AsString := Trim(Registro.COLOR);
      ParamByName('MODELO').AsString := Trim(Registro.MODELO);
      ParamByName('DIBUJO').AsString := Trim(Registro.DIBUJO);
      ParamByName('DESCRIP').AsString := Trim(Registro.DESCRIP);
    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 + ' (InsertarLineas INV)');
      end
    end;
  end; //for
  Result := True;
end;


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

initialization
  dmEDIInventary := TdmEDIInventary.Create(nil);

finalization
  FreeAndNil(dmEDIInventary);

end.
