Files
moadiran/TaxPayerFull/Pages/InvoiceReport.razor

91 lines
3.0 KiB
Plaintext
Raw Normal View History

2024-07-03 16:05:28 +03:30
<PageTitle>دریافت صورتحساب</PageTitle>
@inject IJSRuntime JS
2024-05-30 15:08:36 +03:30
@inject HttpClientController hc;
2024-07-03 16:05:28 +03:30
<Preload LoadingText="در حال بارگذاری..." />
<Toasts class="p-3" Messages="messages" Placement="ToastsPlacement.MiddleCenter" />
2024-07-04 17:18:59 +03:30
@layout EmptyLayout
2024-07-06 16:18:23 +03:30
@page "/InvoiceReport/{InvoiceID:int}"
2024-07-03 16:05:28 +03:30
@using Front.Services
2024-05-30 15:08:36 +03:30
@code {
2024-07-03 16:05:28 +03:30
[Inject] protected PreloadService PreloadService { get; set; } = default!;
List<ToastMessage> messages = new List<ToastMessage>();
2024-07-06 16:18:23 +03:30
[Parameter] public int? InvoiceID { get; set; }
2024-07-03 16:05:28 +03:30
protected async override Task OnParametersSetAsync()
{
2024-07-06 16:18:23 +03:30
await ShowReport();
2024-07-03 16:05:28 +03:30
await base.OnParametersSetAsync();
}
2024-06-30 19:57:32 +03:30
2024-05-30 15:08:36 +03:30
}
2024-07-03 16:05:28 +03:30
@functions {
private void ShowMessage(ToastType toastType, string msg) => messages.Add(CreateToastMessage(toastType, msg));
private ToastMessage CreateToastMessage(ToastType toastType, string msg)
=> new ToastMessage
{
Type = toastType,
Message = msg,
};
private async Task ShowReport()
{
2024-07-06 16:18:23 +03:30
if (InvoiceID != null && InvoiceID > 0)
2024-07-03 16:05:28 +03:30
{
2024-07-06 16:18:23 +03:30
PreloadService.Show(SpinnerColor.Dark);
var rsp = await hc.Get($"Invoice/GetReport/{InvoiceID}");
if (rsp.IsSuccessStatusCode)
{
var str = await rsp.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(str))
ShowMessage(ToastType.Warning, "مشکلی در ساخت فایل رخ داده لطفا مجدد تلاش کنید");
else
await DownloadFileFromStream(str, $"{InvoiceID}.pdf");
}
else if (rsp.StatusCode == System.Net.HttpStatusCode.NotFound)
ShowMessage(ToastType.Warning, "فاکتوری یافت نشد");
else ShowMessage(ToastType.Warning, "خطایی در چاپ فاکتور");
PreloadService.Hide();
}
else hc._nav.NavigateTo("Invoice");
2024-07-03 16:05:28 +03:30
2024-07-06 16:18:23 +03:30
2024-07-03 16:05:28 +03:30
}
//for download
private Stream GetFileStream(byte[] bytes)
{
var fileStream = new MemoryStream(bytes);
return fileStream;
}
private async Task DownloadFileFromStream(string Base64, string FileName)
{
byte[] bytes = System.Convert.FromBase64String(Base64);
var fileStream = GetFileStream(bytes);
// var fileName = "log.bin";
using var streamRef = new DotNetStreamReference(stream: fileStream);
await JS.InvokeVoidAsync("downloadFileFromStream", FileName, streamRef);
}
2024-06-14 22:37:22 +03:30
}
2024-07-03 16:05:28 +03:30
<script>
window.downloadFileFromStream = async (fileName, contentStreamReference) => {
const arrayBuffer = await contentStreamReference.arrayBuffer();
const blob = new Blob([arrayBuffer]);
const url = URL.createObjectURL(blob);
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
URL.revokeObjectURL(url);
}
</script>