Gremlin/Gremlin_BlazorServer/Pages/Quotes/QuoteAdd.razor.cs

207 lines
8.9 KiB
C#

using System.Globalization;
using System.Security.Claims;
using System.Text;
using Blazorise;
using Blazorise.DataGrid;
using Gremlin_BlazorServer.Data.EntityClasses;
using Gremlin_BlazorServer.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.JSInterop;
using NuGet.Packaging;
namespace Gremlin_BlazorServer.Pages.Quotes;
public partial class QuoteAdd {
private const uint salesRepId = 15; //Sascha
private const bool debug = true;
private readonly IList<Contact> contacts = new List<Contact>();
private readonly CultureInfo cultureInfo = new("de-DE");
private bool isCreatingPdf;
private bool isCreatingTex;
private bool lineItemsNotReady = true;
private bool pdfNotReady = true;
private Quote quote = new();
private Contact recipient = new();
private SalesRep salesRep = new();
private LineItem? selectedLineItem;
// private List<CustomDescription>? suggestedCustomDescriptions;
private bool texNotReady = true;
private string url = string.Empty;
[CascadingParameter] private Task<AuthenticationState>? AuthenticationStateTask { get; set; }
// [Inject] public static IModalService ModalService { get; set; }
protected override async Task OnParametersSetAsync() {
if (AuthenticationStateTask is not null) {
ClaimsPrincipal user = (await AuthenticationStateTask).User;
if (user.Identity is { IsAuthenticated: true }) {
await ApplicationLoadingIndicatorService.Show();
contacts.AddRange(await GenericController.GetAllAsync<Contact>());
recipient = contacts.First();
await OnSelectedContactChanged(recipient);
salesRep = await GenericController.Get<SalesRep>(sR => sR.SalesRepId.Equals(salesRepId), "Account");
quote = await GenerateNewQuote(quote);
await ApplicationLoadingIndicatorService.Hide();
}
}
await base.OnInitializedAsync();
}
public Task<ModalInstance> ShowCustomDescriptionModal(CustomDescription customDescription, List<CustomDescription> suggestions) {
return ModalService.Show<CustomDescriptionModal>(builder => {
builder.Add(parameter => parameter.CustomDescription, customDescription);
builder.Add(parameter => parameter.Suggestions, suggestions);
});
}
private async Task<Quote> GenerateNewQuote(Quote newQuote) {
quote.SalesRepId = salesRep.SalesRepId;
Quote lastQuote = await GenericController.GetLastAsync<Quote>();
newQuote.QuoteId = lastQuote.QuoteId + 1;
newQuote.QuotationNumber = $"DE-{salesRep.TerritoryId}-{DateTime.Now:My}-{newQuote.QuoteId}";
newQuote.Description = "Gerät";
return newQuote;
}
private async Task OnPriceSurferFileChanged(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 = await QuoteHandling.GenerateQuoteFromString(quote, recipient, fileContent);
}
}
catch (Exception exception) {
Console.WriteLine(exception.InnerException);
}
finally {
lineItemsNotReady = false;
StateHasChanged();
}
}
private static void OnPriceSurferFileWritten(FileWrittenEventArgs e) {
Console.WriteLine($"File: {e.File.Name} Position: {e.Position} Data: {Convert.ToBase64String(e.Data)}");
}
private static void OnPriceSurferFileProgressed(FileProgressedEventArgs e) {
Console.WriteLine($"File: {e.File.Name} Progress: {e.Percentage}");
}
private async Task OnSelectedContactChanged(Contact selectedContact) {
recipient = selectedContact;
quote.RecipientId = recipient.ContactId;
recipient = await GenericController.Get<Contact>(c => c.ContactId.Equals(selectedContact.ContactId), "Account");
if (quote is { LineItems: not null, Recipient.Account: not null }) lineItemsNotReady = false;
}
private async Task OnSave() {
if (await GenericController.InsertAsync(quote) > 0) {
Console.WriteLine($"Succesfully added quote {quote.QuotationNumber} to db with {quote.LineItems.Count} lineitems and a total net of {quote.TotalNet} euros.");
NavigationManager.NavigateTo("Quotes/QuoteIndex");
}
}
private async Task OnCreateTex() {
isCreatingTex = true;
StringBuilder? tex = await QuoteHandling.CreateTexAsync(quote);
if (tex is null) return;
quote.Tex = tex.ToString();
if (quote.Tex == null) return;
await FileService.WriteTexFile(quote);
isCreatingTex = false;
Console.WriteLine("Tex file succesfully created.");
texNotReady = false;
}
private async Task OnCreatePdf() {
isCreatingPdf = true;
if (await PdfService.CreatePdf(quote)) {
pdfNotReady = false;
isCreatingPdf = false;
Console.WriteLine("PDF successfully written.");
url = HostingService.GetPdfUrl(quote, recipient) ?? "URL not found";
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 async Task OnSelectedLineItemChanged(LineItem newSelectedLineItem) {
selectedLineItem = newSelectedLineItem;
selectedCustomDescriptions = await GenericController.GetAllAsync<CustomDescription>(cD => cD.CustomDescriptionId == selectedLineItem.CustomDescriptionId);
}
private void OnWarrantyChanged(string warranty) => quote.Warranty = int.Parse(warranty);
private void OnCancel() => NavigationManager.NavigateTo("Quotes/QuoteIndex");
private async Task<string> GetClipboardTextAsync() => await JsRuntime.InvokeAsync<string>("navigator.clipboard.readText");
private bool isReloadingLineItems;
private IList<CustomDescription>? selectedCustomDescriptions;
private async Task OnReloadLineItems() {
isReloadingLineItems = true;
quote = await QuoteHandling.GetCustomDescriptionsForLineItems(quote);
isReloadingLineItems = false;
}
private static async Task OnRowInsertedAsync(SavedRowItem<CustomDescription, Dictionary<string, object>> customDescription) {
CustomDescription nCd = ResolveCustomDescription(customDescription.Item);
int count = await GenericController.InsertAsync(nCd);
Console.WriteLine($"Inserted {count} properties for new custom description {nCd.ProductNumber}#{nCd.OptionNumber}: {nCd.Heading}");
}
private static async Task OnRowUpdatedAsync(SavedRowItem<CustomDescription, Dictionary<string, object>> customDescription) {
CustomDescription nCd = ResolveCustomDescription(customDescription.Item);
int count = await GenericController.UpdateAsync(customDescription.Item);
Console.WriteLine($"Updated {count} properties for custom description {nCd.ProductNumber}#{nCd.OptionNumber}: {nCd.Heading}");
}
private static async Task OnRowRemovedAsync(CustomDescription customDescription) {
int count = await GenericController.RemoveAsync(customDescription);
Console.WriteLine($"Removed {count} properties and custom description {customDescription.ProductNumber}#{customDescription.OptionNumber}: {customDescription.Heading}");
}
private static CustomDescription ResolveCustomDescription(CustomDescription customDescription) {
customDescription.DataModificationByUser = "Gremlin Blazor Server GUI";
customDescription.DataVersionNumber++;
return customDescription;
}
private CustomDescription? selectedCustomDescription;
private void OnSelectedCustomDescriptionChanged(CustomDescription newSelectedCustomDescription) => selectedCustomDescription = newSelectedCustomDescription;
}