119 lines
3.3 KiB
C#
119 lines
3.3 KiB
C#
using BeWo.Data.Access;
|
|
using BeWo.Data.Entities;
|
|
using BeWo.Service.Core;
|
|
using BeWo.Service.ServiceImplementations;
|
|
using BeWo.Service.ServiceProxy;
|
|
using BeWo.Service.ServiceUtils;
|
|
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;
|
|
|
|
namespace BeWo.Service.Plugins
|
|
{
|
|
public class XRechnungService : AbstractIDSpecificDefaultClass<XRechnungService>
|
|
{
|
|
public ApiResponse<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);
|
|
|
|
// Falls Lokal vorhanden, gebe lokale Version zurück
|
|
if(invoice_base.XRechnungStoredFile?.FileName is string lFileName && tryGetXRechnungLocal(lStorage, lFileName, out var bytes))
|
|
return ApiResponse<byte[]>.SuccessResponse(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 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 ApiResponse<byte[]> requestXRechnung(LocalTenantFileStorageDAO pStorage, InvoiceBase invoice_base, object obj, string token_url)
|
|
{
|
|
// Request
|
|
var response = ServiceFacade.XRechnung.Create(obj, token_url);
|
|
|
|
var lFileName = pStorage.GetXRechnungFileName(invoice_base.Oid.Value);
|
|
|
|
if (response.Success)
|
|
{
|
|
try
|
|
{
|
|
var bytes = response.Data;
|
|
using (MemoryStream ms = new MemoryStream(bytes))
|
|
{
|
|
var file = pStorage.SaveHybridFile(BWPFeature.XRechnung, lFileName, "application/pdf", ms, TableID.InvoiceBase, invoice_base.Oid.Value);
|
|
|
|
invoice_base.XRechnungStoredFile = file;
|
|
|
|
DAOFactory.GenericDAO.Update(invoice_base);
|
|
}
|
|
}
|
|
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 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;
|
|
}
|
|
}
|
|
}
|