View Javadoc
1   /*
2    * MoneyWise: Finance Application
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.moneywise.tax.uk;
18  
19  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusMoney;
20  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusRate;
21  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
22  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
23  import io.github.tonywasher.joceanus.moneywise.tax.MoneyWiseMarginalReduction;
24  import io.github.tonywasher.joceanus.moneywise.tax.MoneyWiseTaxBandSet.MoneyWiseTaxBand;
25  import io.github.tonywasher.joceanus.moneywise.tax.MoneyWiseTaxResource;
26  
27  /**
28   * Basic UK Tax Allowance.
29   */
30  public abstract class MoneyWiseUKBasicAllowance
31          implements MetisFieldItem {
32      /**
33       * Local Report fields.
34       */
35      private static final MetisFieldSet<MoneyWiseUKBasicAllowance> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseUKBasicAllowance.class);
36  
37      /*
38       * Declare Fields.
39       */
40      static {
41          FIELD_DEFS.declareLocalField(MoneyWiseTaxResource.ALLOWANCE_BASIC, MoneyWiseUKBasicAllowance::getAllowance);
42          FIELD_DEFS.declareLocalField(MoneyWiseTaxResource.ALLOWANCE_RENTAL, MoneyWiseUKBasicAllowance::getRentalAllowance);
43          FIELD_DEFS.declareLocalField(MoneyWiseTaxResource.ALLOWANCE_CAPITAL, MoneyWiseUKBasicAllowance::getCapitalAllowance);
44          FIELD_DEFS.declareLocalField(MoneyWiseTaxResource.MARGINAL_REDUCTION, MoneyWiseUKBasicAllowance::getMarginalReduction);
45      }
46  
47      /**
48       * Allowance.
49       */
50      private final OceanusMoney theAllowance;
51  
52      /**
53       * Rental Allowance.
54       */
55      private final OceanusMoney theRentalAllowance;
56  
57      /**
58       * Capital Allowance.
59       */
60      private final OceanusMoney theCapitalAllowance;
61  
62      /**
63       * Marginal Reduction Type.
64       */
65      private final MoneyWiseMarginalReduction theMarginalReduction;
66  
67      /**
68       * Constructor.
69       *
70       * @param pAllowance        the allowance
71       * @param pRentalAllowance  the rental allowance
72       * @param pCapitalAllowance the capital allowance
73       * @param pReduction        the marginal reduction
74       */
75      protected MoneyWiseUKBasicAllowance(final OceanusMoney pAllowance,
76                                          final OceanusMoney pRentalAllowance,
77                                          final OceanusMoney pCapitalAllowance,
78                                          final MoneyWiseMarginalReduction pReduction) {
79          theAllowance = pAllowance;
80          theRentalAllowance = pRentalAllowance;
81          theCapitalAllowance = pCapitalAllowance;
82          theMarginalReduction = pReduction;
83      }
84  
85      /**
86       * Constructor.
87       *
88       * @param pAllowance        the allowance
89       * @param pRentalAllowance  the rental allowance
90       * @param pCapitalAllowance the capital allowance
91       */
92      protected MoneyWiseUKBasicAllowance(final OceanusMoney pAllowance,
93                                          final OceanusMoney pRentalAllowance,
94                                          final OceanusMoney pCapitalAllowance) {
95          this(pAllowance, pRentalAllowance, pCapitalAllowance, MoneyWiseMarginalReduction.TWOINTHREE);
96      }
97  
98      /**
99       * Obtain the allowance.
100      *
101      * @return the Allowance
102      */
103     protected OceanusMoney getAllowance() {
104         return theAllowance;
105     }
106 
107     /**
108      * Obtain the rental allowance.
109      *
110      * @return the Allowance
111      */
112     protected OceanusMoney getRentalAllowance() {
113         return theRentalAllowance;
114     }
115 
116     /**
117      * Obtain the capital allowance.
118      *
119      * @return the Allowance
120      */
121     protected OceanusMoney getCapitalAllowance() {
122         return theCapitalAllowance;
123     }
124 
125     /**
126      * Obtain the marginal reduction.
127      *
128      * @return the Reduction
129      */
130     protected MoneyWiseMarginalReduction getMarginalReduction() {
131         return theMarginalReduction;
132     }
133 
134     /**
135      * Calculate the allowance.
136      *
137      * @param pConfig the tax configuration
138      * @return the calculated allowance
139      */
140     protected OceanusMoney calculateBasicAllowance(final MoneyWiseUKTaxConfig pConfig) {
141         return getAllowance();
142     }
143 
144     /**
145      * Calculate the savings allowance.
146      *
147      * @param pConfig the tax configuration
148      * @return the savings allowance
149      */
150     protected OceanusMoney calculateSavingsAllowance(final MoneyWiseUKTaxConfig pConfig) {
151         return getZeroAmount();
152     }
153 
154     /**
155      * Calculate the dividend allowance.
156      *
157      * @return the dividend allowance
158      */
159     protected OceanusMoney calculateDividendAllowance() {
160         return getZeroAmount();
161     }
162 
163     /**
164      * Calculate the loSavings band.
165      *
166      * @param pConfig    the tax configuration
167      * @param pLoSavings the low savings band
168      * @return the loSavings band
169      */
170     protected MoneyWiseTaxBand calculateLoSavingsBand(final MoneyWiseUKTaxConfig pConfig,
171                                                       final MoneyWiseTaxBand pLoSavings) {
172         /* Obtain the loSavings band */
173         if (pLoSavings == null) {
174             return new MoneyWiseTaxBand(getZeroAmount(), OceanusRate.getWholePercentage(0));
175         }
176         final MoneyWiseTaxBand myBand = new MoneyWiseTaxBand(pLoSavings);
177         final OceanusMoney myAmount = myBand.getAmount();
178 
179         /* Obtain the preSavings income remaining after the allowance */
180         final OceanusMoney myPreSavings = new OceanusMoney(pConfig.getGrossPreSavings());
181         myPreSavings.subtractAmount(pConfig.getAllowance());
182 
183         /* If we have allowance left over, return the full loSavings band */
184         if (!myPreSavings.isPositive()) {
185             return myBand;
186         }
187 
188         /* Subtract remaining income from the loSavings band */
189         myAmount.subtractAmount(myPreSavings);
190 
191         /* If we are negative, reset to zero */
192         if (!myAmount.isPositive()) {
193             myAmount.setZero();
194         }
195 
196         /* return the amount */
197         return myBand;
198     }
199 
200     /**
201      * Obtain zero amount.
202      *
203      * @return the zero amount
204      */
205     protected OceanusMoney getZeroAmount() {
206         /* Return a zero allowance */
207         final OceanusMoney myAllowance = new OceanusMoney(theAllowance);
208         myAllowance.setZero();
209         return myAllowance;
210     }
211 
212     /**
213      * Obtain the data fields.
214      *
215      * @return the data fields
216      */
217     protected static MetisFieldSet<? extends MoneyWiseUKBasicAllowance> getBaseFieldSet() {
218         return FIELD_DEFS;
219     }
220 }