DefaultIBizInvoiceImporter.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * blueHour: open source accounting
  5.  * http://tidalwave.it/projects/bluehour
  6.  *
  7.  * Copyright (C) 2013 - 2024 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.importer.ibiz.impl;

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

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

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

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

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

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