Wonky.Client/Wonky.Client/Pages/OfficeOrderViewPage.razor.cs
2023-10-13 16:51:29 +02:00

163 lines
No EOL
6.2 KiB
C#

// Copyright (C) 2022 FCS Frede's Computer Services.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see [https://www.gnu.org/licenses/agpl-3.0.en.html]
//
using System.Text;
using System.Text.Json;
using Blazored.LocalStorage;
using Blazored.Toast.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using Wonky.Client.Enums;
using Wonky.Client.Helpers;
using Wonky.Client.HttpInterceptors;
using Wonky.Client.HttpRepository;
using Wonky.Client.Local.Services;
using Wonky.Entity.DTO;
using Wonky.Entity.Views;
#pragma warning disable CS8618
namespace Wonky.Client.Pages;
public partial class OfficeOrderViewPage : IDisposable
{
// #############################################################
[Inject] public HttpInterceptorService Interceptor { get; set; }
[Inject] public IAdvisorActivityRepository AdvisorActivityRepo { get; set; }
[Inject] public ISystemSendMailService MailService { get; set; }
[Inject] public ILocalStorageService Storage { get; set; }
[Inject] public ICountryUserInfoRepository UserRepo { get; set; }
[Inject] public ILogger<OfficeOrderViewPage> Logger { get; set; }
[Inject] public IToastService Toast { get; set; }
[Inject] public IUserInfoService UserInfoService { get; set; }
[Inject] public IJSRuntime JsRuntime { get; set; }
[Inject] public IOrderProcessRepository ProcessRepo { get; set; }
// #############################################################
[Parameter] public string CompanyId { get; set; } = "";
[Parameter] public string OrderId { get; set; } = "";
// #############################################################
private ReportItemView ReportItem { get; set; } = new();
private bool IsNotified { get; set; }
private bool Working { get; set; } = true;
private IJSObjectReference JsModule { get; set; }
private readonly JsonSerializerOptions _options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
protected override async Task OnInitializedAsync()
{
Interceptor.RegisterEvent();
Interceptor.RegisterBeforeSendEvent();
// fetch order from backend
ReportItem = await AdvisorActivityRepo.GetReportItem(OrderId);
Logger.LogDebug("ReportItem => \n {}", JsonSerializer.Serialize(ReportItem, _options));
Working = false;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
JsModule = await JsRuntime
.InvokeAsync<IJSObjectReference>("import", "/scripts/print-invoke.js");
}
}
private async Task SetExpressState()
{
// disable doubled actions
if (Working)
return;
Working = true;
Logger.LogDebug("GetExpressState => {}", JsonSerializer.Serialize(ReportItem, _options));
// send request to backend
var responseView = await AdvisorActivityRepo.GetExpressState(ReportItem.ActivityId);
Logger.LogDebug("SetExpressState => responseView <= {} ", JsonSerializer.Serialize(responseView));
// get user info from storage
var user = await UserInfoService.GetUserInfo();
// fetch sales rep from response
var salesRep = await UserRepo.GetUserInfo(responseView.Id);
Logger.LogDebug("SetExpressState => salesRep => {}", JsonSerializer.Serialize(salesRep));
// create email notification body
var body = new StringBuilder();
body.AppendLine($"Kvittering for modtagelse af hasteordre {ReportItem.ESalesNumber}");
body.AppendLine($"Konto : {ReportItem.Company.Account}");
body.AppendLine($"Navn : {ReportItem.Company.Name}");
body.AppendLine(
$"Post By : {salesRep.CountryCode.ToUpper()}-{ReportItem.Company.ZipCode} {ReportItem.Company.City}");
body.AppendLine();
body.AppendLine("Med venlig hilsen");
body.AppendLine($"{user.FirstName} {user.LastName}");
body.AppendLine($"{user.PhoneNumber}");
// create a list of mail addresses
var sendTo = new List<EmailContact>
{
new()
{
Email = salesRep.Email,
Name = $"{salesRep.FirstName} {salesRep.LastName}"
}
};
// create an email
var msg = new EmailMessage
{
Body = body.ToString(),
Subject = $"Haste ordre til {ReportItem.Company.Name} er modtaget.",
To = sendTo,
IsBodyHtml = false
};
Logger.LogDebug("SetExpressState Notification => \n {}", JsonSerializer.Serialize(msg));
// send a system generated email
var sendMail = await MailService.SendMail("System", msg);
// result notification
if (sendMail.IsSuccess)
{
Toast
.ShowSuccess(
$"Status er opdateret og notifikation sendt til {salesRep.FirstName}.");
}
else
{
Toast.ShowWarning($"Notifikation til {salesRep.FirstName} kunne ikke sendes. {sendMail.Message}");
}
Logger.LogDebug("SendMail Result => \n {}", JsonSerializer.Serialize(sendMail));
// disable further notifications
IsNotified = true;
Working = false;
}
private async Task OfficePrint()
{
await ProcessRepo.UpdateOrderStatus(new OrderProcessState
{
OrderId = OrderId,
ProcessStatusEnum = Utils.EnumToString(ProcessStatus.Printed)
});
await JsModule.InvokeVoidAsync("printInvoke");
}
public void Dispose()
{
Interceptor.DisposeEvent();
}
}