-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for VAT rate periodization (#17642)
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
- Loading branch information
Showing
10 changed files
with
943 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
....openhab.transform.vat/src/main/java/org/openhab/transform/vat/internal/RateProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* Copyright (c) 2010-2024 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.transform.vat.internal; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.math.BigDecimal; | ||
import java.time.Instant; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.transform.vat.internal.model.VATCountry; | ||
import org.openhab.transform.vat.internal.model.VATPeriod; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
|
||
/** | ||
* The {@link RateProvider} class provides VAT rates for different | ||
* countries in different periods of time. | ||
* | ||
* @author Jacob Laursen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class RateProvider { | ||
private static final String RESOURCE_NAME = "/vat_rates.yaml"; | ||
|
||
private final Logger logger = LoggerFactory.getLogger(RateProvider.class); | ||
private final Map<String, List<VATPeriod>> rateMap = getMap(); | ||
|
||
public @Nullable BigDecimal getPercentage(String country) { | ||
return getPercentage(country, Instant.now()); | ||
} | ||
|
||
public @Nullable BigDecimal getPercentage(String country, Instant time) { | ||
List<VATPeriod> vatPeriods = rateMap.get(country); | ||
if (vatPeriods == null) { | ||
return null; | ||
} | ||
for (VATPeriod vatPeriod : vatPeriods) { | ||
if (!time.isBefore(vatPeriod.start()) && time.isBefore(vatPeriod.end())) { | ||
return vatPeriod.percentage(); | ||
} | ||
} | ||
|
||
logger.warn("No VAT rate for country {} valid at {}. This is a bug, please report", country, time); | ||
|
||
return null; | ||
} | ||
|
||
private Map<String, List<VATPeriod>> getMap() { | ||
HashMap<String, List<VATPeriod>> rateMap = new HashMap<>(); | ||
Collection<VATCountry> rates = parseResource(); | ||
for (VATCountry rate : rates) { | ||
rateMap.put(rate.country(), rate.vatPeriod()); | ||
} | ||
return rateMap; | ||
} | ||
|
||
private Collection<VATCountry> parseResource() { | ||
try (InputStream inputStream = RateProvider.class.getResourceAsStream(RESOURCE_NAME)) { | ||
if (inputStream == null) { | ||
throw new IllegalStateException("VAT resource not found"); | ||
} | ||
|
||
ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); | ||
mapper.registerModule(new JavaTimeModule()); | ||
|
||
return mapper.readValue(inputStream, | ||
mapper.getTypeFactory().constructCollectionType(List.class, VATCountry.class)); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("VAT resource could not be read and parsed", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...nhab.transform.vat/src/main/java/org/openhab/transform/vat/internal/model/VATCountry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright (c) 2010-2024 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.transform.vat.internal.model; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
/** | ||
* DTO representing a country with VAT rates in different validity periods. | ||
* | ||
* @author Jacob Laursen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public record VATCountry(String country, @JsonProperty("vatPeriod") List<VATPeriod> vatPeriod) { | ||
@Override | ||
public String toString() { | ||
return "CountryVAT{country='" + country + "', period=" + vatPeriod + '}'; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...enhab.transform.vat/src/main/java/org/openhab/transform/vat/internal/model/VATPeriod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Copyright (c) 2010-2024 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.transform.vat.internal.model; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.Instant; | ||
import java.util.Objects; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* DTO representing a VAT rate in a specific validity period. | ||
* | ||
* @author Jacob Laursen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public record VATPeriod(Instant start, Instant end, BigDecimal percentage) { | ||
|
||
@Override | ||
public Instant start() { | ||
return Objects.isNull(start) ? Instant.MIN : start; | ||
} | ||
|
||
@Override | ||
public Instant end() { | ||
return Objects.isNull(end) ? Instant.MAX : end; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "VATPeriod{start='" + start() + "', end='" + end() + "', percentage=" + percentage + '}'; | ||
} | ||
} |
Oops, something went wrong.