using Blazorise; using Gremlin_BlazorServer.Data.EntityClasses; using Gremlin_BlazorServer.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.JSInterop; using System.Globalization; using System.Security.Claims; using System.Text; namespace Gremlin_BlazorServer.Pages.Quotes; public partial class QuoteAdd { [CascadingParameter] private Task? authenticationStateTask { get; set; } [Inject] public IModalService? modalService { get; set; } private IList? contacts; private Quote quote = new(); private Contact? selectedContact; private readonly CultureInfo cultureInfo = new("de-DE"); private bool pdfNotReady = true; private bool isCreatingPdf; private bool texNotReady = true; private bool isCreatingTex; private bool lineItemsNotReady = true; private string? url; private readonly bool debug; private List? suggestedCustomDescriptions; private CustomDescription? newCustomDescription; public Task ShowCustomDescriptionModal() => modalService.Show(builder => { builder.Add(parameter => parameter.customDescription, newCustomDescription); builder.Add(parameter => parameter.suggestedCustomDescriptions, suggestedCustomDescriptions); }); protected override async Task OnParametersSetAsync() { if (authenticationStateTask != null) { ClaimsPrincipal user = (await authenticationStateTask).User; if (user.Identity is { IsAuthenticated: true }) { await ApplicationLoadingIndicatorService.Show(); contacts = await genericController.GetAllAsync(); await ApplicationLoadingIndicatorService.Hide(); quote = await GenerateNewQuote(quote, "Woitschetzki"); } } await base.OnInitializedAsync(); } private async Task GenerateNewQuote(Quote _quote, string salesRepLastName) { if (_quote == null) return new(); _quote.SalesRep = await genericController.GetAsync(c => c.LastName.Equals(salesRepLastName), "Account"); Quote? lastQuote = genericController.GetLast(); _quote.QuoteId = lastQuote != null ? lastQuote.QuoteId + 1 : 1; _quote.QuotationNumber = _quote.SalesRep != null ? _quote.SalesRep.LastName switch { "Woitschetzki" => $"DE-83PE89-{DateTime.Now:My}-{_quote.QuoteId}", "Welsch" => $"DE-83RE32-{DateTime.Now:My}-{_quote.QuoteId}", _ => $"DE-XXYYXX-{DateTime.Now:My}-{_quote.QuoteId}" } : $"DE-XXYYXX-{DateTime.Now:My}-{_quote.QuoteId}"; _quote.Description = "Gerät"; return _quote; } private async Task SelectQuoteOnChanged(FileChangedEventArgs e) { try { foreach (IFileEntry? file in e.Files) { using MemoryStream stream = new(); await file.WriteToStreamAsync(stream); _ = stream.Seek(0, SeekOrigin.Begin); using StreamReader reader = new(stream); string fileContent = await reader.ReadToEndAsync(); quote = QuoteHandling.ReadLineItems(quote, fileContent); if (quote.Recipient?.Account?.AccountName != null) { quote = hostingService.SetPath(quote); } if (quote.LineItems == null) return; FileService.WriteQuoteToTsv(fileContent, quote); //TODO Load all relevant CustomDescriptions upfront for (int i = 0; i < quote.LineItems.Count(); i++) { newCustomDescription = await genericController.GetAsync(newCustomDescription => newCustomDescription.ProductNumber.Equals(quote.LineItems[i].ProductNumber, StringComparison.Ordinal) && newCustomDescription.OptionNumber.Equals(quote.LineItems[i].OptionNumber, StringComparison.Ordinal)); if (newCustomDescription == null) { Console.WriteLine($"Keine CustomDescription für {quote.LineItems[i].ProductNumber}#{quote.LineItems[i].OptionNumber} verfügbar!"); suggestedCustomDescriptions = await SuggestCustomDescriptions(quote.LineItems[i]); //TODO generate new CustomDescription newCustomDescription = new() { ProductNumber = quote.LineItems[i].ProductNumber, OptionNumber = quote.LineItems[i].OptionNumber, Heading = quote.LineItems[i].SapShortDescription, DescriptionText = quote.LineItems[i].SapLongDescription }; await ShowCustomDescriptionModal(); } quote.LineItems[i].CustomDescription = newCustomDescription; } } } catch (Exception exc) { Console.WriteLine(exc.Message); } finally { if (quote.Recipient != null) lineItemsNotReady = false; StateHasChanged(); } } private async Task?> SuggestCustomDescriptions(LineItem lineItem) { //IList? fromProductNumber = await genericController.GetAllAsync(cD => cD.ProductNumber.Equals(lineItem.ProductNumber, StringComparison.Ordinal)); IList? fromOptionNumber = new List(); if (lineItem.OptionNumber != "") fromOptionNumber = await genericController.GetAllAsync(cD => cD.OptionNumber.Equals(lineItem.OptionNumber, StringComparison.Ordinal)); //if (fromOptionNumber == null && fromProductNumber == null) return null; //if (fromOptionNumber == null) return fromProductNumber.ToList(); //if (fromProductNumber == null) return fromOptionNumber.ToList(); //return fromProductNumber.Union(fromOptionNumber).ToList(); return fromOptionNumber.ToList(); } private static void SelectQuoteOnWritten(FileWrittenEventArgs e) => Console.WriteLine($"File: {e.File.Name} Position: {e.Position} Data: {Convert.ToBase64String(e.Data)}"); private static void SelectQuoteOnProgressed(FileProgressedEventArgs e) => Console.WriteLine($"File: {e.File.Name} Progress: {e.Percentage}"); private async Task OnSelectedContactChanged(Contact _selectedContact) { selectedContact = _selectedContact; quote.Recipient = selectedContact; quote.Recipient.Account = await genericController.GetAsync(account => account.AccountId.Equals(quote.Recipient.AccountId)); if (quote.LineItems != null) lineItemsNotReady = false; } private async Task OnSave() { if (await genericController.InsertAsync(quote) > 0) navigationManager.NavigateTo("Quotes/QuoteIndex"); } private async Task OnCreateTex() { isCreatingTex = true; StringBuilder? tex = await QuoteHandling.CreateTexAsync(quote); if (tex == null) return; quote.Tex = tex.ToString(); if (quote.Tex == null) return; await FileService.WriteTexFile(quote); isCreatingTex = false; Console.WriteLine("Tex file succesfull created."); texNotReady = false; } private async Task OnCreatePdf() { isCreatingPdf = true; if (await PdfService.CreatePdf(quote)) { pdfNotReady = false; isCreatingPdf = false; Console.WriteLine("PDF successfull written."); url = HostingService.GetPdfUrl(quote); Console.WriteLine($"URL to PDF is {url}"); } } private void OnOpenPdfApp() => PdfService.OpenPdfWithOkular(quote); private async Task OnOpenPdfNewTab() => await jSRuntime.InvokeVoidAsync("OpenNewTab", $"quotes/{url}"); private void OnDescriptionChanged(string description) => quote.Description = description; private void OnQuotationNumberChanged(string quotationNumber) => quote.QuotationNumber = quotationNumber; private void OnValidForChanged(string validFor) => quote.ValidFor = byte.Parse(validFor); private void OnVATChanged(string vat) => quote.Vat = float.Parse(vat); private void OnIsPriceInformationChanged(bool isPriceInformation) => quote.IsPriceInformation = isPriceInformation; private void OnShowBruttoChanged(bool onShowBrutto) => quote.ShowBrutto = onShowBrutto; private void OnShowDiscountsChanged(bool showDiscount) => quote.ShowDiscounts = showDiscount; private void OnShowSinglePricesChanged(bool showSinglePrices) => quote.ShowSinglePrices = showSinglePrices; private void OnWarrantyChanged(string warranty) => quote.Warranty = int.Parse(warranty); private void OnCancel() => navigationManager.NavigateTo("Quotes/QuoteIndex"); }