Files
BeWoPlaner/Service/Plugins/XRechnungService.cs
Rene Evertz bb812665db XRechung StoredFileVM + StoredFileVM.Data
XRechnung wird nach dem Runterladen im Arbeitsspeicher gespiechert
2025-06-25 11:49:36 +02:00

133 lines
4.0 KiB
C#

using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.Core;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.ServiceImplementations;
using BeWo.Service.ServiceProxy;
using BeWo.Service.ServiceUtils;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.MikePHPContracts;
using BS.Shared.Interface;
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;
namespace BeWo.Service.Plugins
{
public class XRechnungService : AbstractIDSpecificDefaultClass<XRechnungService>
{
public ApiResponse<Tuple<StoredFileDC, byte[]>> GetXRechnung(long invoice_base_oid, int report_type, string report_url)
{
// Hole Parameter
var lStoragePath = MergedConfig.GetSetting(WebConfigSetting.StoredTenantFilesFolderPath);
var lStorage = DAOFactory.GetLocalTenantFileStorage(lStoragePath);
var invoice_base = DAOFactory.GenericDAO.LoadByID<InvoiceBase>(invoice_base_oid);
var stored_file = invoice_base.XRechnungStoredFile;
// Falls Lokal vorhanden, gebe lokale Version zurück
if (stored_file?.FileName is string lFileName && tryGetXRechnungLocal(lStorage, lFileName, out var bytes))
{
var stored_file_dc = MapperFactory.StoredFile.MapToNewDC(stored_file);
return ApiResponse<Tuple<StoredFileDC, byte[]>>.SuccessResponse(new Tuple<StoredFileDC, byte[]>(stored_file_dc, bytes));
}
// Lokal nicht vorhanden, berechne XRechnung
var str = ServiceConfigReader.GetInvoiceTestJsonString();
// Erstelle Url ohne weitere Authentifizierung
var token_url = GetTokenUrl(report_url);
return requestXRechnung(lStorage, invoice_base, str, token_url);
}
private ApiResponse<Tuple<StoredFileDC, byte[]>> requestXRechnung(LocalTenantFileStorageDAO pStorage, InvoiceBase invoice_base, object obj, string token_url)
{
var response = ServiceFacade.XRechnung.Create(obj, token_url);
var final_response = new ApiResponse<Tuple<StoredFileDC, byte[]>>();
ApiResponse<byte[]>.CopyTo(response, final_response);
var lFileName = pStorage.GetXRechnungFileName(invoice_base.Oid.Value);
if (response.Success)
{
try
{
var bytes = response.Data;
StoredFile stored_file = null;
using (MemoryStream ms = new MemoryStream(bytes))
{
stored_file = pStorage.SaveHybridFile(BWPFeature.XRechnung, lFileName, "application/pdf", ms, TableID.InvoiceBase, invoice_base.Oid.Value);
invoice_base.XRechnungStoredFile = stored_file;
DAOFactory.GenericDAO.Update(invoice_base);
}
var stored_file_dc = MapperFactory.StoredFile.MapToNewDC(stored_file);
final_response.Data = new Tuple<StoredFileDC, byte[]>(stored_file_dc, bytes);
}
catch (Exception ex)
{
try
{
ServiceHelper.CoreLogger.LogError(ex, nameof(GetXRechnung));
}
catch (Exception ex2)
{
response.ErrorStrings.Add("AccountingEnhancedServiceImp: " + ex2.ToString());
}
response.ErrorStrings.Add("AccountingEnhancedServiceImp: " + ex.ToString());
}
}
return final_response;
}
private string GetTokenUrl(string report_url)
{
string download_url;
if (report_url.IndexOf("localhost") < 0)
download_url = report_url + "&mode=pdf";
else
download_url = "https://app5.bewoplaner.de/service/ApiStreamService.svc/GetPdf";
var token_url = new DownloadServiceImp().CreateTemporaryUrl(download_url, false);
return token_url;
}
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;
}
}
}