Content of file DefaultIBizInvoiceImporter.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>DefaultIBizInvoiceImporter.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">blueHour - Importer - iBiz</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.accounting.importer.ibiz.impl</a> &gt; <span class="el_source">DefaultIBizInvoiceImporter.java</span></div><h1>DefaultIBizInvoiceImporter.java</h1><pre class="source lang-java linenums">/*
 * #%L
 * *********************************************************************************************************************
 *
 * blueHour
 * http://bluehour.tidalwave.it - git clone git@bitbucket.org:tidalwave/bluehour-src.git
 * %%
 * Copyright (C) 2013 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
 * %%
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); 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 &quot;AS IS&quot; 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.
 *
 * *********************************************************************************************************************
 *
 *
 * *********************************************************************************************************************
 * #L%
 */
package it.tidalwave.accounting.importer.ibiz.impl;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.Set;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.accounting.model.InvoiceRegistry;
import it.tidalwave.accounting.model.ProjectRegistry;
import it.tidalwave.accounting.importer.ibiz.spi.IBizInvoiceImporter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static java.util.stream.Collectors.toList;

/***********************************************************************************************************************
 *
 * @author Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L52">@Slf4j</span>
<span class="nc" id="L53">@RequiredArgsConstructor</span>
public class DefaultIBizInvoiceImporter implements IBizInvoiceImporter 
  {
<span class="nc" id="L56">    private static final Set&lt;FileVisitOption&gt; NO_OPTIONS = Collections.emptySet();</span>
    
    @Nonnull
    private final InvoiceRegistry invoiceRegistry;

    @Nonnull
    private final ProjectRegistry projectRegistry;
    
    @Nonnull
    private final Path path;

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void importInvoices()
      throws IOException
      {
<span class="nc" id="L76">        log.debug(&quot;importInvoices()&quot;);</span>
<span class="nc" id="L77">        Files.walkFileTree(path.resolve(&quot;Invoices&quot;), NO_OPTIONS, 1, new SimpleFileVisitor&lt;&gt;()</span>
<span class="nc" id="L78">          {</span>
            @Override
            public FileVisitResult visitFile (@Nonnull final Path path, @Nonnull final BasicFileAttributes attrs)
                    throws IOException
              {
<span class="nc bnc" id="L83" title="All 2 branches missed.">                if (path.getFileName().toString().endsWith(&quot;.invoice&quot;))</span>
                  {
<span class="nc" id="L85">                    importInvoice(path.resolve(&quot;Attributes&quot;));</span>
                  }

<span class="nc" id="L88">                return FileVisitResult.CONTINUE;</span>
              }
          });
<span class="nc" id="L91">      }</span>
    
    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
    private void importInvoice (@Nonnull final Path documentFile)
      throws IOException 
      {
        try
          {
<span class="nc" id="L103">            final var configuration = IBizUtils.loadConfiguration(documentFile);</span>
<span class="nc" id="L104">            final var projectId = configuration.getIds(&quot;projectIDs&quot;).get(0);</span>
<span class="nc" id="L105">            final var project = projectRegistry.findProjects().withId(projectId).result();</span>
<span class="nc" id="L106">            final var eventIds = configuration.getIds(&quot;jobEventIDs&quot;).stream();</span>
            // FIXME: could just search events in project (but needs recursive operations in project.findJobEvents())
<span class="nc" id="L108">            final var events = eventIds.flatMap(id -&gt; projectRegistry.findJobEvents().withId(id).stream())</span>
<span class="nc" id="L109">                                       .collect(toList());</span>
            // FIXME: iBiz duplicates events that are already inside a group - filter them away
iBiz duplicates events that are already inside a group - filter them away
<span class="nc" id="L111"> final var tax = configuration.getMoney(&quot;taxes1&quot;);</span> <span class="nc" id="L112"> final var earnings = configuration.getMoney(&quot;invoiceAmount&quot;);</span> <span class="nc" id="L114"> invoiceRegistry.addInvoice().withId(configuration.getId(&quot;uniqueIdentifier&quot;))</span> <span class="nc" id="L115"> .withNumber(configuration.getString(&quot;invoiceNumber&quot;))</span> <span class="nc" id="L116"> .withDate(configuration.getDate(&quot;date&quot;))</span> <span class="nc" id="L117"> .withDueDate(configuration.getDate(&quot;dueDate&quot;))</span> <span class="nc" id="L118"> .withEarnings(earnings.subtract(tax))</span> <span class="nc" id="L119"> .withTax(tax)</span> <span class="nc" id="L120"> .withProject(project)</span> <span class="nc" id="L121"> .withJobEvents(events)</span> <span class="nc" id="L122"> .create();</span> } <span class="nc" id="L124"> catch (NotFoundException e)</span> { <span class="nc" id="L126"> throw new RuntimeException(e);</span> <span class="nc" id="L127"> }</span> <span class="nc" id="L128"> }</span> } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>