Skip to content

Package: DefaultIBizInvoiceImporter

DefaultIBizInvoiceImporter

nameinstructionbranchcomplexitylinemethod
importInvoice(Path)
M: 80 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 22 C: 0
0%
M: 1 C: 0
0%
importInvoices()
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
lambda$importInvoice$0(Id)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

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:
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.accounting.importer.ibiz.spi.IBizInvoiceImporter;
39: import it.tidalwave.accounting.model.InvoiceRegistry;
40: import it.tidalwave.accounting.model.ProjectRegistry;
41: import it.tidalwave.util.NotFoundException;
42: import lombok.RequiredArgsConstructor;
43: import lombok.extern.slf4j.Slf4j;
44: import static java.util.stream.Collectors.*;
45:
46: /***************************************************************************************************************************************************************
47: *
48: * @author Fabrizio Giudici
49: *
50: **************************************************************************************************************************************************************/
51: @Slf4j
52: @RequiredArgsConstructor
53: public class DefaultIBizInvoiceImporter implements IBizInvoiceImporter
54: {
55: private static final Set<FileVisitOption> NO_OPTIONS = Collections.emptySet();
56:
57: @Nonnull
58: private final InvoiceRegistry invoiceRegistry;
59:
60: @Nonnull
61: private final ProjectRegistry projectRegistry;
62:
63: @Nonnull
64: private final Path path;
65:
66: /***********************************************************************************************************************************************************
67: * {@inheritDoc}
68: **********************************************************************************************************************************************************/
69: @Override
70: public void importInvoices()
71: throws IOException
72: {
73: log.debug("importInvoices()");
74: Files.walkFileTree(path.resolve("Invoices"), NO_OPTIONS, 1, new SimpleFileVisitor<>()
75: {
76: @Override
77: public FileVisitResult visitFile (@Nonnull final Path path, @Nonnull final BasicFileAttributes attrs)
78: throws IOException
79: {
80: if (path.getFileName().toString().endsWith(".invoice"))
81: {
82: importInvoice(path.resolve("Attributes"));
83: }
84:
85: return FileVisitResult.CONTINUE;
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 =
101: projectRegistry.findProjects().withId(projectId).optionalResult().orElseThrow(NotFoundException::new);
102: final var eventIds = configuration.getIds("jobEventIDs").stream();
103: // FIXME: could just search events in project (but needs recursive operations in project.findJobEvents())
104: final var events = eventIds.flatMap(id -> projectRegistry.findJobEvents().withId(id).stream())
105: .collect(toList());
106: // FIXME: iBiz duplicates events that are already inside a group - filter them away
107: final var tax = configuration.getMoney("taxes1");
108: final var earnings = configuration.getMoney("invoiceAmount");
109:
110: invoiceRegistry.addInvoice().withId(configuration.getId("uniqueIdentifier"))
111: .withNumber(configuration.getString("invoiceNumber"))
112: .withDate(configuration.getDate("date"))
113: .withDueDate(configuration.getDate("dueDate"))
114: .withEarnings(earnings.subtract(tax))
115: .withTax(tax)
116: .withProject(project)
117: .withJobEvents(events)
118: .create();
119: }
120: catch (NotFoundException e)
121: {
122: throw new RuntimeException(e);
123: }
124: }
125: }