Skip to content

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 - 2021 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.
 *
 * *********************************************************************************************************************
 *
 * $Id$
 *
 * *********************************************************************************************************************
 * #L%
 */
package it.tidalwave.accounting.importer.ibiz.impl;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
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.Id;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.accounting.model.InvoiceRegistry;
import it.tidalwave.accounting.model.JobEvent;
import it.tidalwave.accounting.model.Project;
import it.tidalwave.accounting.model.ProjectRegistry;
import it.tidalwave.accounting.importer.ibiz.spi.IBizInvoiceImporter;
import it.tidalwave.accounting.model.types.Money;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static java.util.stream.Collectors.toList;

/***********************************************************************************************************************
 *
 * @author Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
<span class="nc" id="L60">@Slf4j</span>
<span class="nc bnc" id="L61" title="All 6 branches missed.">@RequiredArgsConstructor</span>
public class DefaultIBizInvoiceImporter implements IBizInvoiceImporter 
  {
<span class="nc" id="L64">    private static final Set&lt;FileVisitOption&gt; NO_OPTIONS = Collections.&lt;FileVisitOption&gt;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="L84">        log.debug(&quot;importInvoices()&quot;);</span>
<span class="nc" id="L85">        Files.walkFileTree(path.resolve(&quot;Invoices&quot;), NO_OPTIONS, 1, new SimpleFileVisitor&lt;Path&gt;()</span>
<span class="nc" id="L86">          {</span>
            @Override
            public FileVisitResult visitFile (final @Nonnull Path path, final @Nonnull BasicFileAttributes attrs)
              throws IOException
              {
<span class="nc bnc" id="L91" title="All 2 branches missed.">                if (path.getFileName().toString().endsWith(&quot;.invoice&quot;))</span>
                  {
<span class="nc" id="L93">                    importInvoice(path.resolve(&quot;Attributes&quot;));</span>
                  }
                
<span class="nc" id="L96">                return FileVisitResult.CONTINUE;</span>
              }
          });
<span class="nc" id="L99">      }</span>
    
    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
    private void importInvoice (final @Nonnull Path documentFile)
      throws IOException 
      {
        try
          {
<span class="nc" id="L111">            final ConfigurationDecorator configuration = IBizUtils.loadConfiguration(documentFile);</span>
<span class="nc" id="L112">            final Id projectId = configuration.getIds(&quot;projectIDs&quot;).get(0);</span>
<span class="nc" id="L113">            final Project project = projectRegistry.findProjects().withId(projectId).result();</span>
<span class="nc" id="L114">            final Stream&lt;Id&gt; 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="L116">            final List&lt;JobEvent&gt; events = eventIds.flatMap(id -&gt; projectRegistry.findJobEvents().withId(id).stream())</span>
<span class="nc" id="L117">                                                  .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="L119"> final Money tax = configuration.getMoney(&quot;taxes1&quot;);</span> <span class="nc" id="L120"> final Money earnings = configuration.getMoney(&quot;invoiceAmount&quot;);</span> <span class="nc" id="L122"> invoiceRegistry.addInvoice().withId(configuration.getId(&quot;uniqueIdentifier&quot;))</span> <span class="nc" id="L123"> .withNumber(configuration.getString(&quot;invoiceNumber&quot;))</span> <span class="nc" id="L124"> .withDate(configuration.getDate(&quot;date&quot;))</span> <span class="nc" id="L125"> .withDueDate(configuration.getDate(&quot;dueDate&quot;))</span> <span class="nc" id="L126"> .withEarnings(earnings.subtract(tax))</span> <span class="nc" id="L127"> .withTax(tax)</span> <span class="nc" id="L128"> .withProject(project)</span> <span class="nc" id="L129"> .withJobEvents(events)</span> <span class="nc" id="L130"> .create();</span> } <span class="nc" id="L132"> catch (NotFoundException e)</span> { <span class="nc" id="L134"> throw new RuntimeException(e);</span> <span class="nc" id="L135"> }</span> <span class="nc" id="L136"> }</span> } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.6.202009150832</span></div></body></html>