-
Notifications
You must be signed in to change notification settings - Fork 0
/
InvoiceMapper.cs
69 lines (58 loc) · 2.44 KB
/
InvoiceMapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.Collections.Immutable;
using coIT.Libraries.Gdi.Accounting.Contracts;
using coIT.Libraries.LexOffice;
using coIT.Libraries.Lexoffice.BusinessRules.Rechnung;
using coIT.Libraries.Toolkit.Datengrundlagen.KundenRelation;
using coIT.Libraries.Toolkit.Datengrundlagen.Mitarbeiter;
using coIT.Libraries.Toolkit.Datengrundlagen.Umsatzkonten;
using CSharpFunctionalExtensions;
using GdiInvoice = coIT.Libraries.Gdi.Accounting.Contracts.Invoice;
using LexofficeInvoice = coIT.Libraries.LexOffice.DataContracts.Invoice.Invoice;
namespace coIT.Toolkit.Lexoffice.GdiExport;
internal class InvoiceMapper
{
private readonly IEnumerable<Umsatzkonto> _accounts;
private readonly IEnumerable<KundeRelation> _customers;
private readonly AlleRechnungsregeln _rechnungsRegeln;
internal InvoiceMapper(
IEnumerable<KundeRelation> customers,
IEnumerable<Umsatzkonto> accounts,
IEnumerable<Mitarbeiter> mitarbeiter
)
{
_customers = customers;
_accounts = accounts;
_rechnungsRegeln = new AlleRechnungsregeln(
customers.ToImmutableList(),
accounts.ToImmutableList(),
mitarbeiter.ToImmutableList()
);
}
internal Result<GdiInvoice> ToGdiInvoice(LexofficeInvoice lexOfficeInvoice)
{
var ergebnis = _rechnungsRegeln.Prüfen(lexOfficeInvoice);
if (ergebnis.IsFailure)
return ergebnis.ConvertFailure<GdiInvoice>();
var exportCustomer = _customers.SingleOrDefault(customer => customer.Id == lexOfficeInvoice.Address.ContactId);
var accountNumberResult = lexOfficeInvoice.KontoErmitteln();
if (accountNumberResult.IsFailure)
return accountNumberResult.ConvertFailure<GdiInvoice>();
var accountNumber = accountNumberResult.Value;
var accountDetails = _accounts.FirstOrDefault(account => account.KontoNummer == accountNumber);
return new GdiInvoice
{
Date = DateTimeOffset.Parse(lexOfficeInvoice.VoucherDate),
Number = lexOfficeInvoice.VoucherNumber,
Type = InvoiceType.Invoice,
NetAmount = lexOfficeInvoice.TotalPrice.TotalNetAmount,
GrossAmount = lexOfficeInvoice.TotalPrice.TotalGrossAmount,
TaxAmount = lexOfficeInvoice.TotalPrice.TotalTaxAmount,
DebitorNumber = exportCustomer?.DebitorenNummer ?? -1,
DebitorName = exportCustomer?.DebitorName ?? "Unbekannt",
RemoteId = lexOfficeInvoice.Id,
DataSource = "lexoffice",
RevenueAccountNumber = accountNumber,
BillingAccountNumber = accountDetails.Steuerschlüssel,
};
}
}