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

181 lines
6.5 KiB
C#

using System.Globalization;
using System.Security.Claims;
using System.Text;
using Blazorise;
using Gremlin_BlazorServer.Data.EntityClasses;
using Gremlin_BlazorServer.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.JSInterop;
namespace Gremlin_BlazorServer.Pages.Quotes;
public partial class QuoteAdd {
private readonly CultureInfo cultureInfo = new("de-DE");
private readonly bool debug;
private IList<Contact>? contacts;
private bool isCreatingPdf;
private bool isCreatingTex;
private bool lineItemsNotReady = true;
private static CustomDescription newCustomDescription;
private bool pdfNotReady = true;
private Quote quote = new();
private Contact? selectedContact;
private LineItem? selectedLineItem;
// private List<CustomDescription>? suggestedCustomDescriptions;
private bool texNotReady = true;
private string? url;
[CascadingParameter] private Task<AuthenticationState>? AuthenticationStateTask { get; set; }
[Inject] public static IModalService ModalService { get; set; }
public static Task ShowCustomDescriptionModal(List<CustomDescription> suggestedCustomDescriptions) {
return ModalService.Show<CustomDescriptionModal>(builder => {
builder.Add(parameter => parameter.CustomDescription, newCustomDescription);
builder.Add(parameter => parameter.SuggestedCustomDescriptions, suggestedCustomDescriptions);
});
}
protected override async Task OnParametersSetAsync() {
if (AuthenticationStateTask is not null) {
ClaimsPrincipal user = (await AuthenticationStateTask).User;
if (user.Identity is { IsAuthenticated: true }) {
contacts = await GenericController.GetAllAsync<Contact>();
selectedContact = contacts?.FirstOrDefault();
if (selectedContact is not null)
await SelectedContactChanged(selectedContact);
SalesRep newSalesRep = new() {
LastName = "Woitschetzki",
FirstName = "Sascha",
TerritoryId = "83PE89",
PhoneNumber = "+49 176 22285334",
EMail = "sascha.woitschetzki@non.agilent.com",
AccountId = 2262,
Gender = 1
};
quote = await GenerateNewQuote(quote, newSalesRep);
}
}
await base.OnInitializedAsync();
}
private async Task<Quote> GenerateNewQuote(Quote newQuote, SalesRep newSalesRep) {
newQuote.SalesRep = newSalesRep; //await genericController.GetAsync<SalesRep>(sR => sR.LastName.Equals(newSalesRep.LastName));
newQuote.SalesRep.Account = await GenericController.GetAsync<Account>(a => a.AccountId.Equals(newQuote.SalesRep.AccountId));
Quote? lastQuote = GenericController.GetLast<Quote>();
newQuote.QuoteId = lastQuote is not null ? lastQuote.QuoteId + 1 : 1;
newQuote.QuotationNumber = $"DE-{newQuote.SalesRep?.TerritoryId}-{DateTime.Now:My}-{newQuote.QuoteId}";
newQuote.Description = "Gerät";
return newQuote;
}
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 = await QuoteHandling.GenerateQuoteFromString(quote, fileContent);
}
}
catch (Exception exc) {
Console.WriteLine(exc.Message);
}
finally {
if (quote.Recipient != null) lineItemsNotReady = false;
StateHasChanged();
}
}
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 SelectedContactChanged(Contact newSelectedContact) {
quote.Recipient = await GenericController.GetAsync<Contact>(c => c.ContactId.Equals(newSelectedContact.ContactId));
if (quote.Recipient is not null)
//Read account seperatly to avoid new generation
quote.Recipient.Account = await GenericController.GetAsync<Account>(a => a.AccountId.Equals(quote.Recipient.AccountId));
if (quote is { LineItems: not null, Recipient.Account: not null }) lineItemsNotReady = false;
selectedContact = newSelectedContact;
}
private async Task OnSave() {
//HACK Try to avoid new generation of FKs
// quote.Recipient = new() { Account = new() };
// quote.SalesRep = new() { Account = new() };
// quote.LineItems = null;
if (await GenericController.UpdateAsync(quote) > 0)
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 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 OnSelectedLineItemChanged(LineItem newSelectedLineItem) => selectedLineItem = newSelectedLineItem;
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");
}