From f5ea7ef92aa83e0571a528d2c66b8cb459031cbd Mon Sep 17 00:00:00 2001 From: Rene Evertz Date: Fri, 13 Jun 2025 15:18:29 +0200 Subject: [PATCH] refactoring --- Service/Plugins/XRechnungService.cs | 57 +++++++++++++++++++---------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/Service/Plugins/XRechnungService.cs b/Service/Plugins/XRechnungService.cs index 068442e15..57fe9ab08 100644 --- a/Service/Plugins/XRechnungService.cs +++ b/Service/Plugins/XRechnungService.cs @@ -7,10 +7,12 @@ using BS.Shared; using BS.Shared.Core; using BS.Shared.DataContracts.MikePHPContracts; using DevExpress.Export; +using DevExpress.Utils.Filtering.Internal; using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; @@ -25,30 +27,25 @@ namespace BeWo.Service.Plugins var lStorage = DAOFactory.GetLocalTenantFileStorage(lStoragePath); var lFileName = lStorage.GetXRechnungFileName(invoice_base_oid); - var lFile = lStorage.GetStoredFile(BWPFeature.XRechnung, lFileName); + + // Falls Lokal vorhanden, gebe lokale Version zurück + if(tryGetXRechnungLocal(lStorage, lFileName, out var bytes)) + return ApiResponse.SuccessResponse(bytes); - if (lFile is object) - { - using (Stream file_stream = lStorage.GetFileContent(lFile)) - { - using (MemoryStream ms = new MemoryStream()) - { - file_stream.CopyTo(ms); - var bytes = ms.ToArray(); - return ApiResponse.SuccessResponse(bytes); - } - } - } - - // Berechne XRechnung + // Lokal nicht vorhanden, berechne XRechnung var str = ServiceConfigReader.GetInvoiceTestJsonString(); // Erstelle Url ohne weitere Authentifizierung var download_url = report_url + "&mode=pdf"; var token_url = new DownloadServiceImp().CreateTemporaryUrl(download_url, false); - // Response - var response = ServiceFacade.XRechnung.Create(str, token_url); + return requestXRechnung(lStorage, lFileName, invoice_base_oid, str, token_url); + } + + private ApiResponse requestXRechnung(LocalTenantFileStorageDAO pStorage, string pFileName, long invoice_base_oid, object obj, string token_url) + { + // Request + var response = ServiceFacade.XRechnung.Create(obj, token_url); if (response.Success) { @@ -57,8 +54,9 @@ namespace BeWo.Service.Plugins var bytes = response.Data; using (MemoryStream ms = new MemoryStream(bytes)) { - var file = lStorage.SaveHybridFile(BWPFeature.XRechnung, lFileName, "application/pdf", ms, TableID.InvoiceBase, invoice_base_oid); - ServiceHelper.CoreLogger.Log(LogLevel.Info, $"Tenant: {lStorage.Tenant}, FilePath: {file.FilePath}", $"{nameof(LocalFileStorageDAO)}.{nameof(lStorage.SaveHybridFile)}"); + var file = pStorage.SaveHybridFile(BWPFeature.XRechnung, pFileName, "application/pdf", ms, TableID.InvoiceBase, invoice_base_oid); + //var msg = $"Tenant: {lStorage.Tenant}, FilePath: {file.FilePath}"; + //ServiceHelper.CoreLogger.Log(LogLevel.Info, msg, $"{nameof(LocalFileStorageDAO)}.{nameof(lStorage.SaveHybridFile)}"); } } catch (Exception ex) @@ -77,5 +75,26 @@ namespace BeWo.Service.Plugins return response; } + + private bool tryGetXRechnungLocal(LocalTenantFileStorageDAO pStorage, string pFileName, out byte[] bytes) + { + bytes = null; + + var lFile = pStorage.GetStoredFile(BWPFeature.XRechnung, pFileName); + + if (lFile is null) + return false; + + using (Stream file_stream = pStorage.GetFileContent(lFile)) + { + using (MemoryStream ms = new MemoryStream()) + { + file_stream.CopyTo(ms); + bytes = ms.ToArray(); + } + } + + return true; + } } }