Skip to contentMethod: toBuilder(Accounting)
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * blueHour: open source accounting
5: * http://tidalwave.it/projects/bluehour
6: *
7: * Copyright (C) 2013 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/bluehour-src
22: * git clone https://github.com/tidalwave-it/bluehour-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.accounting.exporter.xml.impl.xml;
27:
28: import javax.annotation.Nonnull;
29: import java.time.LocalDate;
30: import java.util.List;
31: import javax.xml.bind.annotation.XmlAccessorOrder;
32: import javax.xml.bind.annotation.XmlAccessorType;
33: import javax.xml.bind.annotation.XmlAttribute;
34: import javax.xml.bind.annotation.XmlElement;
35: import javax.xml.bind.annotation.XmlElementWrapper;
36: import javax.xml.bind.annotation.XmlID;
37: import javax.xml.bind.annotation.XmlIDREF;
38: import javax.xml.bind.annotation.XmlRootElement;
39: import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
40: import it.tidalwave.accounting.exporter.xml.impl.adapters.IdAdapter;
41: import it.tidalwave.accounting.exporter.xml.impl.adapters.LocalDateAdapter;
42: import it.tidalwave.accounting.exporter.xml.impl.adapters.MoneyAdapter;
43: import it.tidalwave.accounting.model.Accounting;
44: import it.tidalwave.accounting.model.Invoice;
45: import it.tidalwave.accounting.model.types.Money;
46: import it.tidalwave.util.Id;
47: import lombok.Getter;
48: import lombok.NoArgsConstructor;
49: import static javax.xml.bind.annotation.XmlAccessOrder.ALPHABETICAL;
50: import static javax.xml.bind.annotation.XmlAccessType.FIELD;
51: import static java.util.stream.Collectors.*;
52:
53: /***************************************************************************************************************************************************************
54: *
55: * @author Fabrizio Giudici
56: *
57: **************************************************************************************************************************************************************/
58: //@Mutable
59: @NoArgsConstructor
60: @XmlRootElement(name = "invoice") @XmlAccessorType(FIELD) @XmlAccessorOrder(ALPHABETICAL)
61: public class InvoiceXml
62: {
63: @Getter
64: @XmlAttribute(name = "id")
65: @XmlID
66: @XmlJavaTypeAdapter(IdAdapter.class)
67: private Id id;
68:
69: @XmlElement(name = "number")
70: private String number;
71:
72: @XmlElement(name = "project")
73: @XmlIDREF
74: private ProjectXml projectXml;
75:
76: @XmlElement(name = "date")
77: @XmlJavaTypeAdapter(LocalDateAdapter.class)
78: private LocalDate date;
79:
80: @XmlElement(name = "dueDate")
81: @XmlJavaTypeAdapter(LocalDateAdapter.class)
82: private LocalDate dueDate;
83:
84: @XmlElement(name = "earnings")
85: @XmlJavaTypeAdapter(MoneyAdapter.class)
86: private Money earnings;
87:
88: @XmlElement(name = "tax")
89: @XmlJavaTypeAdapter(MoneyAdapter.class)
90: private Money tax;
91:
92: @XmlElementWrapper(name = "events")
93: @XmlElement(name = "event")
94: @XmlIDREF
95: private List<JobEventXml> jobEventsXml;
96:
97: public InvoiceXml (@Nonnull final Invoice invoice)
98: {
99: final var builder = invoice.toBuilder();
100: this.id = builder.getId();
101: this.number = builder.getNumber();
102: this.projectXml = new ProjectXml(builder.getProject());
103: this.date = builder.getDate();
104: this.dueDate = builder.getDueDate();
105: this.earnings = builder.getEarnings();
106: this.tax = builder.getTax();
107: this.jobEventsXml = builder.getJobEvents().isEmpty()
108: ? null
109: : builder.getJobEvents().stream().map(JobEventXml::new).collect(toList());
110: }
111:
112: @Nonnull
113: public Invoice.Builder toBuilder (@Nonnull final Accounting accounting)
114: {
115: final var customer = accounting.getProjectRegistry()
116: .findProjects()
117: .withId(projectXml.getId())
118: .optionalResult()
119: .orElseThrow(RuntimeException::new);
120: return new Invoice.Builder().withId(id)
121: .withNumber(number)
122: .withProject(customer)
123: .withDate(date)
124: .withDueDate(dueDate)
125: .withEarnings(earnings)
126: .withTax(tax)
127: .withJobEvents(JobEventXml.toJobEvents(jobEventsXml));
128: }
129: }