DefaultIBizInvoiceImporter.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * blueHour
  6.  * http://bluehour.tidalwave.it - git clone git@bitbucket.org:tidalwave/bluehour-src.git
  7.  * %%
  8.  * Copyright (C) 2013 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  * *********************************************************************************************************************
  11.  *
  12.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  13.  * the License. You may obtain a copy of the License at
  14.  *
  15.  *     http://www.apache.org/licenses/LICENSE-2.0
  16.  *
  17.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  18.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations under the License.
  20.  *
  21.  * *********************************************************************************************************************
  22.  *
  23.  *
  24.  * *********************************************************************************************************************
  25.  * #L%
  26.  */
  27. package it.tidalwave.accounting.importer.ibiz.impl;

  28. import javax.annotation.Nonnull;
  29. import java.util.Collections;
  30. import java.util.Set;
  31. import java.io.IOException;
  32. import java.nio.file.FileVisitOption;
  33. import java.nio.file.FileVisitResult;
  34. import java.nio.file.Files;
  35. import java.nio.file.Path;
  36. import java.nio.file.SimpleFileVisitor;
  37. import java.nio.file.attribute.BasicFileAttributes;
  38. import it.tidalwave.util.NotFoundException;
  39. import it.tidalwave.accounting.model.InvoiceRegistry;
  40. import it.tidalwave.accounting.model.ProjectRegistry;
  41. import it.tidalwave.accounting.importer.ibiz.spi.IBizInvoiceImporter;
  42. import lombok.RequiredArgsConstructor;
  43. import lombok.extern.slf4j.Slf4j;
  44. import static java.util.stream.Collectors.toList;

  45. /***********************************************************************************************************************
  46.  *
  47.  * @author Fabrizio Giudici
  48.  *
  49.  **********************************************************************************************************************/
  50. @Slf4j
  51. @RequiredArgsConstructor
  52. public class DefaultIBizInvoiceImporter implements IBizInvoiceImporter
  53.   {
  54.     private static final Set<FileVisitOption> NO_OPTIONS = Collections.emptySet();
  55.    
  56.     @Nonnull
  57.     private final InvoiceRegistry invoiceRegistry;

  58.     @Nonnull
  59.     private final ProjectRegistry projectRegistry;
  60.    
  61.     @Nonnull
  62.     private final Path path;

  63.     /*******************************************************************************************************************
  64.      *
  65.      * {@inheritDoc}
  66.      *
  67.      ******************************************************************************************************************/
  68.     @Override
  69.     public void importInvoices()
  70.       throws IOException
  71.       {
  72.         log.debug("importInvoices()");
  73.         Files.walkFileTree(path.resolve("Invoices"), NO_OPTIONS, 1, new SimpleFileVisitor<>()
  74.           {
  75.             @Override
  76.             public FileVisitResult visitFile (@Nonnull final Path path, @Nonnull final BasicFileAttributes attrs)
  77.                     throws IOException
  78.               {
  79.                 if (path.getFileName().toString().endsWith(".invoice"))
  80.                   {
  81.                     importInvoice(path.resolve("Attributes"));
  82.                   }

  83.                 return FileVisitResult.CONTINUE;
  84.               }
  85.           });
  86.       }
  87.    
  88.     /*******************************************************************************************************************
  89.      *
  90.      *
  91.      *
  92.      ******************************************************************************************************************/
  93.     private void importInvoice (@Nonnull final Path documentFile)
  94.       throws IOException
  95.       {
  96.         try
  97.           {
  98.             final var configuration = IBizUtils.loadConfiguration(documentFile);
  99.             final var projectId = configuration.getIds("projectIDs").get(0);
  100.             final var project = projectRegistry.findProjects().withId(projectId).result();
  101.             final var eventIds = configuration.getIds("jobEventIDs").stream();
  102.             // FIXME: could just search events in project (but needs recursive operations in project.findJobEvents())
  103.             final var events = eventIds.flatMap(id -> projectRegistry.findJobEvents().withId(id).stream())
  104.                                        .collect(toList());
  105.             // FIXME: iBiz duplicates events that are already inside a group - filter them away
  106.             final var tax = configuration.getMoney("taxes1");
  107.             final var earnings = configuration.getMoney("invoiceAmount");

  108.             invoiceRegistry.addInvoice().withId(configuration.getId("uniqueIdentifier"))
  109.                                         .withNumber(configuration.getString("invoiceNumber"))
  110.                                         .withDate(configuration.getDate("date"))
  111.                                         .withDueDate(configuration.getDate("dueDate"))
  112.                                         .withEarnings(earnings.subtract(tax))
  113.                                         .withTax(tax)
  114.                                         .withProject(project)
  115.                                         .withJobEvents(events)
  116.                                         .create();
  117.           }
  118.         catch (NotFoundException e)
  119.           {
  120.             throw new RuntimeException(e);
  121.           }
  122.       }
  123.   }