]> git.basschouten.com Git - openhab-addons.git/blob
c0db9e30b232043779b2b0eb646f5f11cd6b5029
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.transform.vat.internal;
14
15 import static org.openhab.transform.vat.internal.VATTransformationConstants.*;
16
17 import java.math.BigDecimal;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.library.types.QuantityType;
22 import org.openhab.core.transform.TransformationException;
23 import org.openhab.core.transform.TransformationService;
24 import org.openhab.core.types.UnDefType;
25 import org.osgi.service.component.annotations.Component;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * This {@link TransformationService} adds VAT to the input according to configured country.
31  *
32  * @author Jacob Laursen - Initial contribution
33  */
34 @NonNullByDefault
35 @Component(service = { TransformationService.class }, property = { "openhab.transform=VAT" })
36 public class VATTransformationService implements TransformationService {
37
38     private final Logger logger = LoggerFactory.getLogger(VATTransformationService.class);
39
40     @Override
41     public @Nullable String transform(String valueString, String sourceString) throws TransformationException {
42         QuantityType<?> source;
43         try {
44             source = new QuantityType<>(sourceString);
45         } catch (IllegalArgumentException e) {
46             if (UnDefType.NULL.toString().equals(sourceString) || UnDefType.UNDEF.toString().equals(sourceString)) {
47                 return sourceString;
48             }
49             logger.warn("Input value '{}' could not be converted to a valid number", sourceString);
50             throw new TransformationException("VAT Transformation can only be used with numeric inputs", e);
51         }
52         BigDecimal value;
53         try {
54             value = new BigDecimal(valueString);
55         } catch (NumberFormatException e) {
56             String rate = RATES.get(valueString);
57             if (rate == null) {
58                 logger.warn("Input value '{}' could not be converted to a valid number or country code", valueString);
59                 throw new TransformationException("VAT Transformation can only be used with numeric inputs", e);
60             }
61             value = new BigDecimal(rate);
62         }
63
64         return addVAT(source, value).toString();
65     }
66
67     private QuantityType<?> addVAT(QuantityType<?> source, BigDecimal percentage) {
68         return source.multiply(percentage.divide(new BigDecimal("100")).add(BigDecimal.ONE));
69     }
70 }