refactoring

This commit is contained in:
2025-06-13 15:18:29 +02:00
parent 45f18244af
commit f5ea7ef92a

View File

@@ -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<byte[]>.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<byte[]>.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<byte[]> 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;
}
}
}