Files
BeWoPlaner/Service/Plugins/XRechnungService.cs
2025-11-28 14:54:24 +01:00

214 lines
7.0 KiB
C#

using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Data.Models.XRechnung;
using BeWo.Service.Core;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.ServiceImplementations;
using BeWo.Service.ServiceProxy;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.Interface;
using DevExpress.Export;
using DevExpress.Utils.Filtering.Internal;
using Newtonsoft.Json;
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>
{
private readonly LocalTenantFileStorageDAO _storage;
private readonly GenericDAO _genericDAO;
private readonly FileLogger _logger;
public XRechnungService()
{
// Hole Storage
var lStoragePath = MergedConfig.GetSetting(WebConfigSetting.StoredTenantFilesFolderPath);
_storage = DAOFactory.GetLocalTenantFileStorage(lStoragePath);
_genericDAO = DAOFactory.GenericDAO;
_logger = ServiceHelper.CoreLogger;
}
public ApiResponse<Tuple<StoredFileDC, byte[]>> GetXRechnung(long invoice_base_oid, int report_type, string report_url)
{
var invoice_base = _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(_storage, 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();
calculate(invoice_base);
// Erstelle Url ohne weitere Authentifizierung
var token_url = GetTokenUrl(report_url);
return requestXRechnung(_storage, 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;
_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
{
_logger.LogError(ex, nameof(GetXRechnung));
}
catch (Exception ex2)
{
response.ErrorStrings.Add("AccountingEnhancedServiceImp - CoreLogger: " + 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";
return new DownloadServiceImp().CreateTemporaryUrl(download_url, false);
}
private bool tryGetXRechnungLocal(LocalTenantFileStorageDAO pStorage, string pFileName, out byte[] bytes)
{
bytes = null;
// Hole StoredFile aus DB
var lFile = pStorage.GetStoredFile(BWPFeature.XRechnung, pFileName);
if (lFile is null)
return false;
using (Stream file_stream = pStorage.GetFileContent(lFile))
{
if (file_stream is null) return false;
using (MemoryStream ms = new MemoryStream())
{
file_stream.CopyTo(ms);
bytes = ms.ToArray();
}
}
return true;
}
private string calculate(InvoiceBase invoice)
{
var undefinded = "??undefinded??";
var undefinded_num = -1;
var undefinded_num2 = 1001;
var undefinded_num3 = 58;
XRechnung xRechnung = new XRechnung();
xRechnung.Invoice.Type = (invoice.InvoiceTypeText?.StartsWith("storno", StringComparison.OrdinalIgnoreCase) ?? false) ? "384" : "380";
xRechnung.Invoice.Number = invoice.InvoiceNumber;
xRechnung.Invoice.Date = invoice.InvoiceDate?.ToString("yyyyMMdd") ?? undefinded;
xRechnung.Invoice.Currency = "EUR";
xRechnung.Invoice.Notice = undefinded;
xRechnung.Invoice.Period.Start = invoice.AccountingPeriodStart?.ToString("yyyyMMdd") ?? undefinded;
xRechnung.Invoice.Period.End = invoice.AccountingPeriodEnd?.ToString("yyyyMMdd") ?? undefinded;
xRechnung.Invoice.Period.Description = undefinded;
xRechnung.Invoice.Payment.Type = undefinded_num;
xRechnung.Invoice.Payment.Type = undefinded_num3;
xRechnung.Invoice.Payment.Term = undefinded;
xRechnung.Invoice.Payment.DueDate = undefinded;
xRechnung.Seller.Name = undefinded;
xRechnung.Seller.ID = undefinded;
xRechnung.Seller.TaxID = undefinded;
xRechnung.Seller.VatID = undefinded;
xRechnung.Seller.Street = undefinded;
xRechnung.Seller.ZipCode = undefinded;
xRechnung.Seller.City = undefinded;
xRechnung.Seller.EMail = undefinded;
xRechnung.Seller.CountryCode = "DE";
xRechnung.Seller.Contact.Name = undefinded;
xRechnung.Seller.Contact.Department = undefinded;
xRechnung.Seller.Contact.Phone = undefinded;
xRechnung.Seller.Contact.EMail = undefinded;
xRechnung.Seller.BankAccount.IBAN = undefinded;
xRechnung.Seller.BankAccount.BIC = undefinded;
xRechnung.Seller.BankAccount.Owner = undefinded;
xRechnung.Seller.BankAccount.Reference = undefinded;
xRechnung.Seller.BankAccount.ReferenceID = undefinded;
xRechnung.Buyer.Name = undefinded;
xRechnung.Buyer.ID = undefinded;
xRechnung.Buyer.Street = undefinded;
xRechnung.Buyer.ZipCode = undefinded;
xRechnung.Buyer.City = undefinded;
xRechnung.Buyer.EMail = undefinded;
xRechnung.Buyer.CountryCode = undefinded;
xRechnung.Buyer.Reference = undefinded;
xRechnung.Buyer.BankAccount.IBAN = undefinded;
xRechnung.Buyer.BankAccount.BIC = undefinded;
xRechnung.Buyer.BankAccount.Owner = undefinded;
xRechnung.Buyer.BankAccount.MandateID = undefinded;
foreach (var item in invoice.GetInvoiceItems())
{
var position = new XRPosition();
position.Quantity = undefinded_num;
position.QuantityType = undefinded;
position.Text = undefinded;
position.Net = undefinded_num;
position.NetTotal = undefinded_num;
position.Tax = undefinded_num2;
position.TaxSum = undefinded_num2;
position.TaxType = undefinded;
xRechnung.Position.Add(position);
}
return JsonConvert.SerializeObject(xRechnung);
}
}
}