Införande av repository method
This commit is contained in:
169
Accounting.BLR/ReadingIn.cs
Normal file
169
Accounting.BLR/ReadingIn.cs
Normal file
@ -0,0 +1,169 @@
|
||||
using Accounting.BLI;
|
||||
using Accounting.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Accounting.BLR
|
||||
{
|
||||
public class ReadingIn : IReadingIn, IDisposable
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<ReadingIn> _logger;
|
||||
private readonly IAccountRecords _accountRecords;
|
||||
private readonly IExcellent _excellent;
|
||||
|
||||
public ReadingIn(
|
||||
IConfiguration configuration,
|
||||
ILogger<ReadingIn> logger,
|
||||
IAccountRecords accountRecords,
|
||||
IExcellent excellent)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
_accountRecords = accountRecords;
|
||||
_excellent = excellent;
|
||||
}
|
||||
|
||||
public IEnumerable<AccountRecord> readXLS(string FilePath)
|
||||
{
|
||||
List<AccountRecord> records = new List<AccountRecord>();
|
||||
AccountRecord? record = null;
|
||||
FileInfo existingFile = new FileInfo(FilePath);
|
||||
var fieldNr = 0;
|
||||
|
||||
_excellent.ExcellentStart(existingFile.FullName, 1); ;
|
||||
|
||||
//get the first worksheet in the workbook
|
||||
int colCount = _excellent.Columns;
|
||||
int rowCount = _excellent.Rows; //get row count
|
||||
bool prt = false;
|
||||
for (int row = 0; row <= rowCount; row++)
|
||||
{
|
||||
if (prt)
|
||||
{
|
||||
//Console.WriteLine();
|
||||
record.Id = row;
|
||||
_logger.LogInformation(record.ToString());
|
||||
record.Id = 0;
|
||||
records.Add(record);
|
||||
}
|
||||
prt = false;
|
||||
for (int col = 0; col <= colCount; col++)
|
||||
{
|
||||
var x = _excellent.ReadCell(row, col);
|
||||
|
||||
if (_excellent.ReadCell(row, col) == null)
|
||||
{
|
||||
// Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + "null");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (col == 0 && _excellent.DataType == "DateTime")
|
||||
{
|
||||
prt = true;
|
||||
record = new AccountRecord();
|
||||
fieldNr = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (col == 0)
|
||||
{
|
||||
col = colCount;
|
||||
}
|
||||
}
|
||||
//Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim());
|
||||
if (prt)
|
||||
{
|
||||
if (x == "") { }
|
||||
else
|
||||
{
|
||||
fieldNr++;
|
||||
|
||||
// _logger.LogInformation($"{exObject.ReadCell(row, col).ToString().Trim()}/t");
|
||||
switch (fieldNr)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
record.BetalDatum = DateTime.FromOADate(double.Parse(_excellent.ReadCell(row, col)));
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
record.Mottagare = _excellent.ReadCell(row, col).ToString().Trim();
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
record.Konto = _excellent.ReadCell(row, col).ToString().Trim();
|
||||
if (record.Konto.ToLower().StartsWith("bg")
|
||||
|| record.Konto.ToLower().StartsWith("pg")
|
||||
|| record.Konto.ToLower().StartsWith("hs")) { }
|
||||
else
|
||||
{
|
||||
prt = false;
|
||||
col = colCount;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 4:
|
||||
{
|
||||
record.Belopp = double.Parse(_excellent.ReadCell(row, col).ToString().Trim());
|
||||
break;
|
||||
}
|
||||
case 5:
|
||||
{
|
||||
record.Avisering = _excellent.ReadCell(row, col).ToString().Trim();
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return records;
|
||||
}
|
||||
public bool ReadAndSaveInvoices(string fullFileName)
|
||||
{
|
||||
var result = true;
|
||||
var restab = readXLS(fullFileName);
|
||||
if (restab != null)
|
||||
{
|
||||
//restab.ToList().ForEach(x =>
|
||||
//{
|
||||
// _logger.LogInformation(x.ToString());
|
||||
//});
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
restab.ToList().ForEach(x =>
|
||||
{
|
||||
if (_accountRecords.GetAccountByDateBelKonto(x.BetalDatum, x.Belopp, x.Konto) == null)
|
||||
_accountRecords.AddAccountRecord(x);
|
||||
else { };
|
||||
});
|
||||
|
||||
// restab.ToList().ForEach(x => { _accountRecordRepository.AddAccountRecord(x); });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("MassUppdatering misslyckat: -->{iMessage}", ex.Message);
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_excellent.Dispose();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user