InvoiceXml.java

/*
 * *************************************************************************************************************************************************************
 *
 * blueHour: open source accounting
 * http://tidalwave.it/projects/bluehour
 *
 * Copyright (C) 2013 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *************************************************************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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
 * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
 *
 * *************************************************************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/bluehour-src
 * git clone https://github.com/tidalwave-it/bluehour-src
 *
 * *************************************************************************************************************************************************************
 */
package it.tidalwave.accounting.exporter.xml.impl.xml;

import jakarta.annotation.Nonnull;
import java.time.LocalDate;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessorOrder;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import it.tidalwave.accounting.exporter.xml.impl.adapters.IdAdapter;
import it.tidalwave.accounting.exporter.xml.impl.adapters.LocalDateAdapter;
import it.tidalwave.accounting.exporter.xml.impl.adapters.MoneyAdapter;
import it.tidalwave.accounting.model.Accounting;
import it.tidalwave.accounting.model.Invoice;
import it.tidalwave.accounting.model.types.Money;
import it.tidalwave.util.Id;
import lombok.Getter;
import lombok.NoArgsConstructor;
import static javax.xml.bind.annotation.XmlAccessOrder.ALPHABETICAL;
import static javax.xml.bind.annotation.XmlAccessType.FIELD;
import static java.util.stream.Collectors.*;

/***************************************************************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **************************************************************************************************************************************************************/
//@Mutable
@NoArgsConstructor
@XmlRootElement(name = "invoice") @XmlAccessorType(FIELD) @XmlAccessorOrder(ALPHABETICAL)
public class InvoiceXml 
  {
    @Getter
    @XmlAttribute(name = "id")
    @XmlID
    @XmlJavaTypeAdapter(IdAdapter.class)
    private Id id;
    
    @XmlElement(name = "number")
    private String number;
    
    @XmlElement(name = "project")
    @XmlIDREF
    private ProjectXml projectXml;
    
    @XmlElement(name = "date")
    @XmlJavaTypeAdapter(LocalDateAdapter.class)
    private LocalDate date;
    
    @XmlElement(name = "dueDate")
    @XmlJavaTypeAdapter(LocalDateAdapter.class)
    private LocalDate dueDate;
    
    @XmlElement(name = "earnings")
    @XmlJavaTypeAdapter(MoneyAdapter.class)
    private Money earnings;
    
    @XmlElement(name = "tax")
    @XmlJavaTypeAdapter(MoneyAdapter.class)
    private Money tax;
    
    @XmlElementWrapper(name = "events")
    @XmlElement(name = "event")
    @XmlIDREF
    private List<JobEventXml> jobEventsXml; 
    
    public InvoiceXml (@Nonnull final Invoice invoice)
      {
        final var builder = invoice.toBuilder();
        this.id = builder.getId();
        this.number = builder.getNumber();
        this.projectXml = new ProjectXml(builder.getProject());
        this.date = builder.getDate();
        this.dueDate = builder.getDueDate();
        this.earnings = builder.getEarnings();
        this.tax = builder.getTax();
        this.jobEventsXml = builder.getJobEvents().isEmpty() 
                    ? null
                    : builder.getJobEvents().stream().map(JobEventXml::new).collect(toList());
      }

    @Nonnull
    public Invoice.Builder toBuilder (@Nonnull final Accounting accounting)
      {
        final var customer = accounting.getProjectRegistry()
                                       .findProjects()
                                       .withId(projectXml.getId())
                                       .optionalResult()
                .orElseThrow(RuntimeException::new);
        return new Invoice.Builder().withId(id)
                                    .withNumber(number)
                                    .withProject(customer)
                                    .withDate(date)
                                    .withDueDate(dueDate)
                                    .withEarnings(earnings)
                                    .withTax(tax)
                                    .withJobEvents(JobEventXml.toJobEvents(jobEventsXml));
      }
  }