116 lines
3.4 KiB
C#
116 lines
3.4 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 GremlinContext gremlinContext;
|
|
|
|
public LineItemService(GremlinContext gC) => gremlinContext = gC;
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
_ = services.AddDbContext<GremlinContext>(ServiceLifetime.Scoped);
|
|
}
|
|
|
|
public async Task<List<LineItem>> GetLineItemsAsync() => await gremlinContext.LineItems.ToListAsync();
|
|
|
|
public async Task<bool> InsertLineItemAsync(LineItem lineItem)
|
|
{
|
|
try
|
|
{
|
|
_ = await gremlinContext.LineItems.AddAsync(lineItem);
|
|
_ = await gremlinContext.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Debug.WriteLine(exception.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<LineItem?> GetLineItemAsync(uint lineItemId)
|
|
{
|
|
return await gremlinContext.LineItems.FirstOrDefaultAsync(LineItem => LineItem.LineItemId.Equals(lineItemId));
|
|
}
|
|
|
|
public async Task<bool> UpdateLineItemAsync(LineItem LineItem)
|
|
{
|
|
_ = gremlinContext.LineItems.Update(LineItem);
|
|
_ = await gremlinContext.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> DeleteLineItemAsync(LineItem LineItem)
|
|
{
|
|
_ = gremlinContext.Remove(LineItem);
|
|
_ = await gremlinContext.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
|
|
public static List<LineItem> ReadLineItemsFromClipboard(string clipboard)
|
|
{
|
|
if (clipboard != "")
|
|
{
|
|
List<LineItem> lineItems = new();
|
|
List<string[]> lineItemStrings = new();
|
|
|
|
//Zeilen aufteilen
|
|
IEnumerable<string> clipboardLines = clipboard.Split(Environment.NewLine.ToCharArray());
|
|
|
|
foreach (string clipboardLine in clipboardLines)
|
|
{
|
|
if (clipboardLine != "")
|
|
{
|
|
//Nach Trennzeichen trennen
|
|
string[] clipboardLineSplit = clipboardLine.Split('\t');
|
|
lineItemStrings.Add(clipboardLineSplit);
|
|
}
|
|
}
|
|
|
|
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 new List<LineItem>();
|
|
}
|
|
}
|
|
}
|