114 lines
3.2 KiB
C#
114 lines
3.2 KiB
C#
using Gremlin_BlazorServer.Data.DBClasses;
|
|
using Gremlin_BlazorServer.Data.EntityClasses;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Diagnostics;
|
|
|
|
namespace Gremlin_BlazorServer.Services
|
|
{
|
|
public class LineItemService
|
|
{
|
|
private readonly GremlinDb gremlinDb;
|
|
|
|
public LineItemService(GremlinDb gC)
|
|
{
|
|
gremlinDb = gC;
|
|
}
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
_ = services.AddDbContext<GremlinDb>(ServiceLifetime.Scoped);
|
|
}
|
|
|
|
public async Task<List<LineItem>> GetLineItemsAsync()
|
|
{
|
|
return await gremlinDb.LineItems.ToListAsync();
|
|
}
|
|
|
|
public async Task<bool> InsertLineItemAsync(LineItem lineItem)
|
|
{
|
|
try
|
|
{
|
|
_ = await gremlinDb.LineItems.AddAsync(lineItem);
|
|
_ = await gremlinDb.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Debug.WriteLine(exception.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<LineItem?> GetLineItemAsync(uint lineItemId)
|
|
{
|
|
return await gremlinDb.LineItems.FirstOrDefaultAsync(lineItem => lineItem.LineItemId.Equals(lineItemId));
|
|
}
|
|
|
|
public async Task<bool> UpdateLineItemAsync(LineItem lineItem)
|
|
{
|
|
_ = gremlinDb.LineItems.Update(lineItem);
|
|
_ = await gremlinDb.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> DeleteLineItemAsync(LineItem lineItem)
|
|
{
|
|
_ = gremlinDb.Remove(lineItem);
|
|
_ = await gremlinDb.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
|
|
public static List<LineItem> ReadLineItemsFromClipboard(string clipboard)
|
|
{
|
|
if (clipboard != "")
|
|
{
|
|
List<LineItem> lineItems = new();
|
|
|
|
//Zeilen aufteilen
|
|
IEnumerable<string> clipboardLines = clipboard.Split(Environment.NewLine.ToCharArray());
|
|
|
|
List<string[]> lineItemStrings = (from clipboardLine in clipboardLines where clipboardLine != "" select clipboardLine.Split('\t')).ToList();
|
|
|
|
foreach (string[] lineItemString in lineItemStrings)
|
|
{
|
|
//Anzahl an Spalten entspricht Clipboard
|
|
if (lineItemString.Length == 19)
|
|
{
|
|
//Header ignorieren
|
|
if (lineItemString[0] == "#") { continue; }
|
|
|
|
//Dateiinhalt in Klasse schreiben
|
|
LineItem lineItem = new()
|
|
{
|
|
Position = ushort.Parse(lineItemString[0]),
|
|
ProductNumber = lineItemString[1],
|
|
OptionNumber = lineItemString[2],
|
|
ProductLine = lineItemString[3],
|
|
SapShortDescription = lineItemString[4],
|
|
Amount = ushort.Parse(lineItemString[5]),
|
|
ListPrice = decimal.Parse(lineItemString[6]),
|
|
TotalDiscount = decimal.Parse(lineItemString[9]),
|
|
NetPrice = decimal.Parse(lineItemString[10]),
|
|
Total = decimal.Parse(lineItemString[11]),
|
|
SalesDiscount = decimal.Parse(lineItemString[12]),
|
|
ContractualDiscount = decimal.Parse(lineItemString[13]),
|
|
PromotionalDiscount = decimal.Parse(lineItemString[14]),
|
|
DemoDiscount = decimal.Parse(lineItemString[15])
|
|
};
|
|
|
|
//Preise selbst berechnen
|
|
//lineItem.CalcNetPrice = lineItem.ListPrice * (100 - lineItem.TotalDiscount) / 100;
|
|
//lineItem.CalcTotalNet = lineItem.CalcNetPrice * lineItem.Amount;
|
|
|
|
//lineItem.CustomDescriptionVM = CustomDescriptionViewModel.GetCustomDescription(lineItem);
|
|
|
|
lineItems.Add(lineItem);
|
|
}
|
|
}
|
|
return lineItems;
|
|
}
|
|
return new();
|
|
}
|
|
}
|
|
}
|