InvoiceXml.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * blueHour: open source accounting
  5.  * http://tidalwave.it/projects/bluehour
  6.  *
  7.  * Copyright (C) 2013 - 2025 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. import jakarta.annotation.Nonnull;
  28. import java.time.LocalDate;
  29. import java.util.List;
  30. import javax.xml.bind.annotation.XmlAccessorOrder;
  31. import javax.xml.bind.annotation.XmlAccessorType;
  32. import javax.xml.bind.annotation.XmlAttribute;
  33. import javax.xml.bind.annotation.XmlElement;
  34. import javax.xml.bind.annotation.XmlElementWrapper;
  35. import javax.xml.bind.annotation.XmlID;
  36. import javax.xml.bind.annotation.XmlIDREF;
  37. import javax.xml.bind.annotation.XmlRootElement;
  38. import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  39. import it.tidalwave.accounting.exporter.xml.impl.adapters.IdAdapter;
  40. import it.tidalwave.accounting.exporter.xml.impl.adapters.LocalDateAdapter;
  41. import it.tidalwave.accounting.exporter.xml.impl.adapters.MoneyAdapter;
  42. import it.tidalwave.accounting.model.Accounting;
  43. import it.tidalwave.accounting.model.Invoice;
  44. import it.tidalwave.accounting.model.types.Money;
  45. import it.tidalwave.util.Id;
  46. import lombok.Getter;
  47. import lombok.NoArgsConstructor;
  48. import static javax.xml.bind.annotation.XmlAccessOrder.ALPHABETICAL;
  49. import static javax.xml.bind.annotation.XmlAccessType.FIELD;
  50. import static java.util.stream.Collectors.*;

  51. /***************************************************************************************************************************************************************
  52.  *
  53.  * @author  Fabrizio Giudici
  54.  *
  55.  **************************************************************************************************************************************************************/
  56. //@Mutable
  57. @NoArgsConstructor
  58. @XmlRootElement(name = "invoice") @XmlAccessorType(FIELD) @XmlAccessorOrder(ALPHABETICAL)
  59. public class InvoiceXml
  60.   {
  61.     @Getter
  62.     @XmlAttribute(name = "id")
  63.     @XmlID
  64.     @XmlJavaTypeAdapter(IdAdapter.class)
  65.     private Id id;
  66.    
  67.     @XmlElement(name = "number")
  68.     private String number;
  69.    
  70.     @XmlElement(name = "project")
  71.     @XmlIDREF
  72.     private ProjectXml projectXml;
  73.    
  74.     @XmlElement(name = "date")
  75.     @XmlJavaTypeAdapter(LocalDateAdapter.class)
  76.     private LocalDate date;
  77.    
  78.     @XmlElement(name = "dueDate")
  79.     @XmlJavaTypeAdapter(LocalDateAdapter.class)
  80.     private LocalDate dueDate;
  81.    
  82.     @XmlElement(name = "earnings")
  83.     @XmlJavaTypeAdapter(MoneyAdapter.class)
  84.     private Money earnings;
  85.    
  86.     @XmlElement(name = "tax")
  87.     @XmlJavaTypeAdapter(MoneyAdapter.class)
  88.     private Money tax;
  89.    
  90.     @XmlElementWrapper(name = "events")
  91.     @XmlElement(name = "event")
  92.     @XmlIDREF
  93.     private List<JobEventXml> jobEventsXml;
  94.    
  95.     public InvoiceXml (@Nonnull final Invoice invoice)
  96.       {
  97.         final var builder = invoice.toBuilder();
  98.         this.id = builder.getId();
  99.         this.number = builder.getNumber();
  100.         this.projectXml = new ProjectXml(builder.getProject());
  101.         this.date = builder.getDate();
  102.         this.dueDate = builder.getDueDate();
  103.         this.earnings = builder.getEarnings();
  104.         this.tax = builder.getTax();
  105.         this.jobEventsXml = builder.getJobEvents().isEmpty()
  106.                     ? null
  107.                     : builder.getJobEvents().stream().map(JobEventXml::new).collect(toList());
  108.       }

  109.     @Nonnull
  110.     public Invoice.Builder toBuilder (@Nonnull final Accounting accounting)
  111.       {
  112.         final var customer = accounting.getProjectRegistry()
  113.                                        .findProjects()
  114.                                        .withId(projectXml.getId())
  115.                                        .optionalResult()
  116.                 .orElseThrow(RuntimeException::new);
  117.         return new Invoice.Builder().withId(id)
  118.                                     .withNumber(number)
  119.                                     .withProject(customer)
  120.                                     .withDate(date)
  121.                                     .withDueDate(dueDate)
  122.                                     .withEarnings(earnings)
  123.                                     .withTax(tax)
  124.                                     .withJobEvents(JobEventXml.toJobEvents(jobEventsXml));
  125.       }
  126.   }