From 06496f84423eb291c7c47a44d4644acdecc710fc Mon Sep 17 00:00:00 2001 From: mmrbnjd Date: Mon, 1 Jul 2024 15:39:40 +0330 Subject: [PATCH] CreateReport --- Back/Controllers/InvoiceController.cs | 28 +++- .../Pages/UserPanel/InvoiceItem.razor | 152 +++++++++++------- 2 files changed, 120 insertions(+), 60 deletions(-) diff --git a/Back/Controllers/InvoiceController.cs b/Back/Controllers/InvoiceController.cs index 3c0ecf8..bc54797 100644 --- a/Back/Controllers/InvoiceController.cs +++ b/Back/Controllers/InvoiceController.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; using Shared.DTOs; using Shared.DTOs.Serch; +using System.Diagnostics; namespace Back.Controllers { @@ -441,11 +442,30 @@ namespace Back.Controllers return Ok(await _servTaxPayer.GetPatterns()); } [HttpGet("GetReport/{InvoiceID}")] - public IActionResult GetReport(int InvoiceID) + public async Task> GetReport(int InvoiceID) { - var reportPath = $"Reports\\invoice.mrt"; - var bytes = System.IO.File.ReadAllBytes(reportPath); - return new FileContentResult(bytes, "application/xml"); + string output = ""; + //-----GetUserAndCompany + var claim = HttpContext.User.Claims.First(c => c.Type == "UserID"); + var UserID = claim.Value; + var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID)); + + var CompanyID= user?.RolUsers.First().CompanyID; + + if (await _servInvoice.ExistInvoiceByInvoiceID(CompanyID.Value, InvoiceID)) + return NotFound(); + + // Start the child process. + Process p = new Process(); + // Redirect the output stream of the child process. + p.StartInfo.UseShellExecute = false; + p.StartInfo.RedirectStandardOutput = true; + p.StartInfo.FileName = "C:\\CreateReport\\CreateReport.exe"; + p.StartInfo.Arguments = $"{CompanyID} {InvoiceID}"; + p.Start(); + output = p.StandardOutput.ReadToEnd(); + p.WaitForExit(); + return Ok(output); } } } diff --git a/TaxPayerFull/Pages/UserPanel/InvoiceItem.razor b/TaxPayerFull/Pages/UserPanel/InvoiceItem.razor index 5b37949..dcdb894 100644 --- a/TaxPayerFull/Pages/UserPanel/InvoiceItem.razor +++ b/TaxPayerFull/Pages/UserPanel/InvoiceItem.razor @@ -1,7 +1,7 @@ @using Front.Services @using Shared.DTOs @using Shared - +@inject IJSRuntime JS @inject HttpClientController hc; @layout PanelLayout @@ -22,20 +22,20 @@
}
- @if (InvoiceID.HasValue) - { + @if (InvoiceID.HasValue) + {

سرویس ها / صورتحساب / @invoice?.Title -

- } - else - { + + } + else + {

سرویس ها / صورتحساب / جدید

- } + } + - @* alert *@
- @if (InvoiceID == 0 || InvoiceID == null ? false : true ) + @if (InvoiceID == 0 || InvoiceID == null ? false : true) {

@@ -256,7 +256,7 @@ + } - +
@@ -284,8 +284,8 @@ } - -
+ +

@@ -311,8 +311,8 @@
- - + +

@if (!invoice.IsDeleted) @@ -331,13 +331,13 @@ - @* *@ - + + } } - +
@if (invoice.invoiceType != InvoiceType.Bidding && SendInvoice) { @@ -348,19 +348,19 @@
} - +
- @if (invoice.BillReference.HasValue) - { -
- @{ - string Prefence = "InvoiceDetails/" + invoice.BillReference.Value.ToString(); - } - -
- } + @if (invoice.BillReference.HasValue) + { +
+ @{ + string Prefence = "InvoiceDetails/" + invoice.BillReference.Value.ToString(); + } + +
+ }
@@ -450,7 +450,7 @@ if (rsp.IsSuccessStatusCode) { var resinvoice = await rsp.Content.ReadFromJsonAsync(); - if (resinvoice!=null) + if (resinvoice != null) { invoice = resinvoice; InvoiceID = resinvoice.ID; @@ -470,16 +470,24 @@ { if (InvoiceID.HasValue) { - + hc._nav.NavigateTo($"TaxPayerInvoiceItem/{InvoiceID}"); } } private async Task ShowReport() { - if (InvoiceID.HasValue) + var rsp = await hc.Get($"Invoice/GetReport/{InvoiceID}"); + if (rsp.IsSuccessStatusCode) { - fv.invoice = invoice; - hc._nav.NavigateTo($"InvoiceReport/{InvoiceID}"); + var str = await rsp.Content.ReadFromJsonAsync(); + if (string.IsNullOrEmpty(str)) + ShowDangerAlert("خطایی در ساخت فاکتور"); + else + await DownloadFileFromStream(str, $"{InvoiceID}.pdf"); + } + else + { + ShowDangerAlert("خطایی در ساخت فاکتور"); } } private async Task LoadData() @@ -488,13 +496,13 @@ var rsp = await hc.Get($"Invoice/Get/{InvoiceID}/{true}"); if (rsp.IsSuccessStatusCode) { - invoice = await rsp.Content.ReadFromJsonAsync(); - + invoice = await rsp.Content.ReadFromJsonAsync(); + } - else if(rsp.StatusCode==System.Net.HttpStatusCode.BadRequest) + else if (rsp.StatusCode == System.Net.HttpStatusCode.BadRequest) { - ShowDangerAlert("صورتحساب مرجع یافت نشد"); + ShowDangerAlert("صورتحساب مرجع یافت نشد"); } else { @@ -739,4 +747,36 @@ } } + //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); + } +} + } \ No newline at end of file