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.metis.field.MetisFieldSet;
20  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseTaxClass;
21  import io.github.tonywasher.joceanus.moneywise.tax.MoneyWiseTaxBandSet.MoneyWiseTaxBand;
22  import io.github.tonywasher.joceanus.moneywise.tax.MoneyWiseTaxResource;
23  import io.github.tonywasher.joceanus.moneywise.tax.uk.MoneyWiseUKTax.MoneyWiseUKTaxYearCtl;
24  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusMoney;
25  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusRate;
26  
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  /**
32   * Interest Tax Scheme.
33   */
34  public abstract class MoneyWiseUKInterestScheme
35          extends MoneyWiseUKIncomeScheme {
36      /*
37       * Local Report fields.
38       */
39      static {
40          MetisFieldSet.newFieldSet(MoneyWiseUKInterestScheme.class);
41      }
42  
43      /**
44       * Constructor.
45       */
46      protected MoneyWiseUKInterestScheme() {
47      }
48  
49      /**
50       * Obtain theTaxCredit rate for interest.
51       *
52       * @param pTaxYear the taxYear
53       * @return the taxCredit rate
54       */
55      protected abstract OceanusRate getTaxCreditRate(MoneyWiseUKTaxYearCtl pTaxYear);
56  
57      @Override
58      protected OceanusMoney adjustAllowances(final MoneyWiseUKTaxConfig pConfig,
59                                              final OceanusMoney pAmount) {
60          /* Adjust against the basic allowance */
61          final OceanusMoney myRemaining = super.adjustAllowances(pConfig, pAmount);
62  
63          /* If we have any interest left */
64          if (myRemaining.isNonZero()) {
65              /* Adjust the savings allowance noting that it still counts against the taxBand */
66              final OceanusMoney myInterest = adjustForAllowance(pConfig.getSavingsAllowance(), myRemaining);
67  
68              /* Adjust any loSavings band noting that it still counts against the taxBand */
69              adjustForAllowance(pConfig.getLoSavingsBand().getAmount(), myInterest);
70          }
71  
72          /* Return unallocated income */
73          return myRemaining;
74      }
75  
76      @Override
77      protected OceanusMoney getAmountInAllowance(final MoneyWiseUKTaxConfig pConfig,
78                                                  final OceanusMoney pAmount) {
79          /* Obtain the amount covered by the basic allowance */
80          OceanusMoney myAmount = super.getAmountInAllowance(pConfig, pAmount);
81  
82          /* If we have income left over */
83          if (myAmount.compareTo(pAmount) < 0) {
84              /* Calculate remaining amount */
85              final OceanusMoney myRemaining = new OceanusMoney(pAmount);
86              myRemaining.subtractAmount(myAmount);
87  
88              /* Calculate the amount covered by savings allowance */
89              final OceanusMoney myXtra = getAmountInBand(pConfig.getSavingsAllowance(), myRemaining);
90  
91              /* Determine the total amount covered by the allowance */
92              myAmount = new OceanusMoney(myAmount);
93              myAmount.addAmount(myXtra);
94          }
95  
96          /* return the amount */
97          return myAmount;
98      }
99  
100     /**
101      * Obtain the base rate.
102      *
103      * @return the base rate
104      */
105     protected OceanusRate getBaseRate() {
106         return null;
107     }
108 
109     @Override
110     protected Iterator<MoneyWiseTaxBand> taxBandIterator(final MoneyWiseUKTaxConfig pConfig,
111                                                          final MoneyWiseTaxClass pBasis) {
112         /* Obtain the loSavingsBand and the basicRate */
113         final MoneyWiseTaxBand myLoBand = pConfig.getLoSavingsBand();
114         final OceanusMoney myLoAmount = myLoBand.getAmount();
115         OceanusRate myRate = getBaseRate();
116 
117         /* Create a new List */
118         final List<MoneyWiseTaxBand> myList = new ArrayList<>();
119 
120         /* Access underlying iterator and obtain first band */
121         final Iterator<MoneyWiseTaxBand> myIterator = super.taxBandIterator(pConfig, pBasis);
122         MoneyWiseTaxBand myBasicBand = myIterator.next();
123         OceanusMoney myAmount = myBasicBand.getAmount();
124 
125         /* If we are a LoBase instance */
126         if (this instanceof MoneyWiseUKInterestLoBaseRateScheme) {
127             /* Add the first band as is */
128             myList.add(myBasicBand);
129 
130             /* Access the true basic band */
131             myBasicBand = myIterator.next();
132             myAmount = myBasicBand.getAmount();
133 
134             /* Add low band if required */
135         } else if (myLoAmount.isNonZero()) {
136             myList.add(myLoBand);
137             myAmount = new OceanusMoney(myAmount);
138             myAmount.subtract(myLoAmount);
139         }
140 
141         /* Add the basic band */
142         if (myRate == null) {
143             myRate = myBasicBand.getRate();
144         }
145         myList.add(new MoneyWiseTaxBand(myAmount, myRate));
146 
147         /* Loop through remaining tax bands */
148         while (myIterator.hasNext()) {
149             final MoneyWiseTaxBand myBand = myIterator.next();
150             myList.add(myBand);
151         }
152 
153         /* Return the iterator */
154         return myList.iterator();
155     }
156 
157     /**
158      * As Income Scheme.
159      */
160     public static class MoneyWiseUKInterestAsIncomeScheme
161             extends MoneyWiseUKInterestScheme {
162         /**
163          * Local Report fields.
164          */
165         private static final MetisFieldSet<MoneyWiseUKInterestAsIncomeScheme> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseUKInterestAsIncomeScheme.class);
166 
167         /**
168          * Default constructor.
169          */
170         MoneyWiseUKInterestAsIncomeScheme() {
171             /* NoOp */
172         }
173 
174         @Override
175         protected OceanusRate getTaxCreditRate(final MoneyWiseUKTaxYearCtl pTaxYear) {
176             return pTaxYear.getTaxBands().getBasicTaxRate();
177         }
178 
179         @Override
180         public MetisFieldSet<MoneyWiseUKInterestAsIncomeScheme> getDataFieldSet() {
181             return FIELD_DEFS;
182         }
183     }
184 
185     /**
186      * Base Rate Scheme.
187      */
188     public static class MoneyWiseUKInterestBaseRateScheme
189             extends MoneyWiseUKInterestScheme {
190         /**
191          * Local Report fields.
192          */
193         private static final MetisFieldSet<MoneyWiseUKInterestBaseRateScheme> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseUKInterestBaseRateScheme.class);
194 
195         /*
196          * Declare Fields.
197          */
198         static {
199             FIELD_DEFS.declareLocalField(MoneyWiseTaxResource.SCHEME_BASE_RATE, MoneyWiseUKInterestBaseRateScheme::getBaseRate);
200         }
201 
202         /**
203          * The Base Rate.
204          */
205         private final OceanusRate theBaseRate;
206 
207         /**
208          * Constructor.
209          *
210          * @param pRate the base rate
211          */
212         protected MoneyWiseUKInterestBaseRateScheme(final OceanusRate pRate) {
213             theBaseRate = pRate;
214         }
215 
216         @Override
217         protected OceanusRate getBaseRate() {
218             return theBaseRate;
219         }
220 
221         @Override
222         protected OceanusRate getTaxCreditRate(final MoneyWiseUKTaxYearCtl pTaxYear) {
223             return theBaseRate;
224         }
225 
226         @Override
227         public MetisFieldSet<? extends MoneyWiseUKInterestBaseRateScheme> getDataFieldSet() {
228             return FIELD_DEFS;
229         }
230     }
231 
232     /**
233      * LoBase Rate Scheme.
234      */
235     public static class MoneyWiseUKInterestLoBaseRateScheme
236             extends MoneyWiseUKInterestBaseRateScheme {
237         /**
238          * Local Report fields.
239          */
240         private static final MetisFieldSet<MoneyWiseUKInterestLoBaseRateScheme> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseUKInterestLoBaseRateScheme.class);
241 
242         /**
243          * Constructor.
244          *
245          * @param pRate the base rate
246          */
247         protected MoneyWiseUKInterestLoBaseRateScheme(final OceanusRate pRate) {
248             super(pRate);
249         }
250 
251         @Override
252         public MetisFieldSet<MoneyWiseUKInterestLoBaseRateScheme> getDataFieldSet() {
253             return FIELD_DEFS;
254         }
255     }
256 }