235 lines
6.6 KiB
C#
235 lines
6.6 KiB
C#
using Gremlin_BlazorServer.Data.DBClasses;
|
|
using Gremlin_BlazorServer.Data.EntityClasses;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Diagnostics;
|
|
using System.Text;
|
|
using System.Globalization;
|
|
|
|
namespace Gremlin_BlazorServer.Services
|
|
{
|
|
public class QuoteHandling
|
|
{
|
|
private static GremlinDb gremlinDb = new();
|
|
public QuoteHandling(GremlinDb gC) => gremlinDb = gC;
|
|
|
|
private readonly TexService texService = new();
|
|
|
|
public void ConfigureServices(IServiceCollection services) => services.AddDbContext<GremlinDb>(ServiceLifetime.Scoped);
|
|
|
|
public async Task<List<Quote>> GetAllQuotesAsync() => await gremlinDb.Quotes.ToListAsync();
|
|
|
|
public async Task<List<LineItem>> GetLineItemsAsync(Quote quote) => await gremlinDb.LineItems.Where(lI => lI.QuoteId == quote.QuoteId).ToListAsync();
|
|
|
|
public async Task<bool> InsertQuoteAsync(Quote quote)
|
|
{
|
|
try
|
|
{
|
|
await gremlinDb.Quotes.AddAsync(quote);
|
|
await gremlinDb.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Debug.WriteLine(exception.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<Quote> GetQuoteAsync(uint quoteId) => await gremlinDb.Quotes.FirstOrDefaultAsync(q => q.QuoteId.Equals(quoteId)) ?? new Quote();
|
|
|
|
public async Task<bool> UpdateQuoteAsync(Quote quote)
|
|
{
|
|
gremlinDb.Quotes.Update(quote);
|
|
await gremlinDb.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> DeleteQuoteAsync(Quote quote)
|
|
{
|
|
gremlinDb.Remove(quote);
|
|
await gremlinDb.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
|
|
public StringBuilder CreateTex(Quote quote)
|
|
{
|
|
StringBuilder texString = texService.CreateTex(quote);
|
|
Debug.WriteLine(texString.Length > 0 ? "Creating TexFile succesfully." : "Error during TexFile creation!");
|
|
return texString;
|
|
}
|
|
|
|
//FromWindowsGremlin
|
|
|
|
public Quote ReadLineItems(Quote quote, string clipboard)
|
|
{
|
|
try
|
|
{
|
|
quote = ResetTotals(quote);
|
|
|
|
quote.LineItems = ReadLineItemsFromClipboard(clipboard);
|
|
|
|
quote.QuoteContains3Pp = DoesContains(quote, "3PP");
|
|
quote.QuoteContainsRb = DoesContains(quote, "RB");
|
|
|
|
quote.TotalListprice = GetTotal(quote, "TotalListprice");
|
|
quote.TotalDiscount = GetTotal(quote, "AverageDiscount");
|
|
quote.TotalNet = GetTotal(quote, "TotalNet");
|
|
|
|
//quote.ValidFor = CheckForAcademic(quote.Recipient.Account); //No AccountType in Accounts
|
|
|
|
foreach (LineItem lineItem in quote.LineItems)
|
|
{
|
|
if (lineItem.OptionNumber.StartsWith("8D"))
|
|
quote.Warranty = int.Parse(lineItem.OptionNumber.Last().ToString()) * 12;
|
|
|
|
quote.Warranty = lineItem.OptionNumber switch
|
|
{
|
|
"9EC" => 24,
|
|
"9CC" => 36,
|
|
_ => quote.Warranty
|
|
};
|
|
}
|
|
|
|
quote = CalculateTotals(quote);
|
|
return quote;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.WriteLine(e);
|
|
return new();
|
|
}
|
|
}
|
|
|
|
private static List<LineItem> ReadLineItemsFromClipboard(string clipboard)
|
|
{
|
|
//Zeilen aufteilen
|
|
IEnumerable<string> clipboardLines = clipboard.Split(Environment.NewLine.ToCharArray());
|
|
List<string[]> clipboardList = (from clipboardLine in clipboardLines where clipboardLine != "" select clipboardLine.Split('\t')).ToList();
|
|
return ParseClipboardList(clipboardList);
|
|
}
|
|
|
|
private static List<LineItem> ParseClipboardList(List<string[]> lineItemStrings)
|
|
{
|
|
List<LineItem> lineItems = new();
|
|
CultureInfo cultureInfoUs = new("en-US");
|
|
|
|
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], cultureInfoUs),
|
|
TotalDiscount = decimal.Parse(lineItemString[9], cultureInfoUs),
|
|
NetPrice = decimal.Parse(lineItemString[10], cultureInfoUs),
|
|
Total = decimal.Parse(lineItemString[11], cultureInfoUs),
|
|
SalesDiscount = decimal.Parse(lineItemString[12], cultureInfoUs),
|
|
ContractualDiscount = decimal.Parse(lineItemString[13], cultureInfoUs),
|
|
PromotionalDiscount = decimal.Parse(lineItemString[14], cultureInfoUs),
|
|
DemoDiscount = decimal.Parse(lineItemString[15], cultureInfoUs)
|
|
};
|
|
|
|
lineItems.Add(lineItem);
|
|
}
|
|
else
|
|
{
|
|
Debug.WriteLine("Angebot konnte nicht eingelesen werden!");
|
|
}
|
|
}
|
|
return lineItems;
|
|
}
|
|
|
|
private static byte CheckForAcademic(Account account)
|
|
{
|
|
return (byte)(account.AccountType.AccountTypeCode.StartsWith("N") ? 90 : 60);
|
|
}
|
|
|
|
private static decimal GetFreight(decimal net, decimal freight)
|
|
{
|
|
decimal freightNet = net * freight / 100;
|
|
return freightNet < 3000 ? freightNet : 3000;
|
|
}
|
|
|
|
private static Quote CalculateTotals(Quote quote)
|
|
{
|
|
quote.TotalFreightOnly = GetFreight(quote.TotalNet, quote.Freight);
|
|
quote.TotalFreight = quote.TotalNet + quote.TotalFreightOnly;
|
|
quote.TotalVat = quote.TotalFreight * Convert.ToDecimal(quote.Vat) / 100;
|
|
quote.TotalGross = quote.TotalFreight * (100 + Convert.ToDecimal(quote.Vat)) / 100;
|
|
return quote;
|
|
}
|
|
|
|
private static Quote ResetTotals(Quote quote)
|
|
{
|
|
quote.TotalListprice = 0;
|
|
quote.TotalNet = 0;
|
|
quote.TotalFreightOnly = 0;
|
|
quote.TotalFreight = 0;
|
|
quote.TotalVat = 0;
|
|
quote.TotalGross = 0;
|
|
|
|
return quote;
|
|
}
|
|
|
|
private static decimal GetTotal(Quote quote, string type)
|
|
{
|
|
decimal total = 0;
|
|
|
|
foreach (LineItem lineItem in quote.LineItems)
|
|
{
|
|
switch (type)
|
|
{
|
|
case "TotalListprice":
|
|
total += lineItem.ListPrice;
|
|
break;
|
|
case "TotalNet":
|
|
total += lineItem.Total;
|
|
break;
|
|
case "AverageDiscount":
|
|
total += lineItem.TotalDiscount;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ((type == "AverageDiscount") & (quote.LineItems.Count != 0)) { total /= quote.LineItems.Count; }
|
|
|
|
return total;
|
|
}
|
|
|
|
private static bool DoesContains(Quote quote, string type)
|
|
{
|
|
foreach (LineItem lineItem in quote.LineItems)
|
|
{
|
|
switch (type)
|
|
{
|
|
case "3PP":
|
|
if (lineItem.ProductLine == "3PP")
|
|
{
|
|
Debug.WriteLine($"Quote contains 3PP with ProductNumber {lineItem.ProductNumber}");
|
|
return true;
|
|
}
|
|
break;
|
|
case "RB":
|
|
if ((lineItem.ProductLine == "RB") & (lineItem.ProductNumber != "R2005A"))
|
|
{
|
|
Debug.WriteLine($"Quote contains RB with ProductNumber {lineItem.ProductNumber}");
|
|
return true;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
} |