New version digresive depreciation

This commit is contained in:
Artur 2024-10-23 15:42:51 +02:00
parent e4a36a7c8f
commit 76e4e4146e
8 changed files with 115 additions and 30 deletions

View File

@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
<version>3.3.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>tk.artikus.angular</groupId>

View File

@ -39,7 +39,7 @@ public class AssetDepreciationAlgorithm {
final boolean isEveryMonthRecalculate;
long valueToBeDepreciated = 0;
long annualDepreciation = 0;
long annualDepreciationFor12Month = 0; // annual depreciation for 12 month
if( depreciationMethod.beDigressive() ) {
valueToBeDepreciated = assetLifeDepreciatied.getValueToBeDepreciatedOn( start.prev() );
long _annualDepreciation = depreciationMethod.calculateAnnualDepreciation( valueToBeDepreciated );
@ -47,7 +47,7 @@ public class AssetDepreciationAlgorithm {
valueToBeDepreciated - consolidatedDepreciation, AssetDepreciationMethod.Factor.INCLUDE );
if( annualDepreciationWithFactor > _annualDepreciation ) {
isEveryMonthRecalculate = false;
annualDepreciation = annualDepreciationWithFactor;
annualDepreciationFor12Month = annualDepreciationWithFactor;
} else {
isEveryMonthRecalculate = true;
}
@ -56,7 +56,15 @@ public class AssetDepreciationAlgorithm {
isEveryMonthRecalculate = true;
}
long monthlyDepreciation = annualDepreciation / 12;
int months = annualPeriod.months();
final long annualDepreciationForThisPeriod;
if( 0 == months ) {
annualDepreciationForThisPeriod = 0;
}else {
annualDepreciationForThisPeriod = annualDepreciationFor12Month * months / 12;
}
long monthlyDepreciation = annualDepreciationFor12Month / 12;
long allocatedAnnualDepreciation = 0;
long sumAnnualValueToBeDepreciated = 0;
@ -67,9 +75,9 @@ public class AssetDepreciationAlgorithm {
if( isEveryMonthRecalculate ) {
valueToBeDepreciated = assetLifeDepreciatied.getValueToBeDepreciatedOn( ym );
annualDepreciation = depreciationMethod.calculateAnnualDepreciation( valueToBeDepreciated );
annualDepreciationFor12Month = depreciationMethod.calculateAnnualDepreciation( valueToBeDepreciated );
sumAnnualValueToBeDepreciated += valueToBeDepreciated;
monthlyDepreciation = annualDepreciation / 12;
monthlyDepreciation = annualDepreciationFor12Month / 12;
}
AssetPlanPosition positionOfPlanItem = assetPlan.getPosition( ym, true );
@ -84,7 +92,7 @@ public class AssetDepreciationAlgorithm {
sumAnnualDepreciation /= 12;
calculatedDepreciation = sumAnnualDepreciation - allocatedAnnualDepreciation;
} else {
calculatedDepreciation = annualDepreciation - allocatedAnnualDepreciation;
calculatedDepreciation = annualDepreciationForThisPeriod - allocatedAnnualDepreciation;
}
}

View File

@ -2,6 +2,7 @@ package tk.artikus.assets.services;
import java.util.Collection;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

View File

@ -1,2 +1,2 @@
spring.application.name=angular-services
server.port=8800
server.port=8801

View File

@ -0,0 +1,62 @@
package tk.artikus.assets;
import junit.framework.TestCase;
import tk.artikus.assets.adapters.AssetAdapterForTest;
import tk.artikus.assets.tools.YearMonth;
public class TestDigresiveAlgorithm extends TestCase{
public void _test_1_years_depreciation( ){
AssetAdapterForTest assetInTest = AssetAdapterForTest.builder( )
.fromYear( 1 ).digressive( ).rate( 50 ).factor( 2, 0 )
.assetValueToEndOfLife( 12 )
.build( );
assetInTest.calculate( );
assetInTest.checkExpectedValues( Values.builder( )
.addHowManyWhat( 12, 1 ).build( ) );
}
public void test_12_month_depreciationB( ){
AssetAdapterForTest assetInTest = AssetAdapterForTest.builder( )
.fromYear( 1 ).digressive( ).rate( 50 ).factor( 2, 0 )
.fromYearMonthAssetChangeValue( 1, 10, 12 )
.build( );
assetInTest.setStartDepreciation( YearMonth.builder().year( 1 ).month( 11 ).build() );
assetInTest.calculate( );
assetInTest.checkExpectedValues( Values.builder( )
.addHowManyWhat( 12, 1 ).build( ) );
}
public void _test_12_month_depreciationA( ){
AssetAdapterForTest assetInTest = AssetAdapterForTest.builder( )
.fromYear( 1 ).digressive( ).rate( 50 ).factor( 2, 0 )
.fromYearMonthAssetChangeValue( 1, 11, 12 )
.build( );
assetInTest.setStartDepreciation( YearMonth.builder().year( 1 ).month( 12 ).build() );
assetInTest.calculate( );
assetInTest.checkExpectedValues( Values.builder( )
.addHowManyWhat( 12, 1 ).build( ) );
}
}

View File

@ -1,13 +1,14 @@
package tk.artikus.assets;
import junit.framework.TestCase;
import tk.artikus.assets.adapters.AssetAdapterForTest;
public class TestEasyCaseAlgorithm extends TestCase{
public void test_2_month_depreciation() {
AssetInTest assetInTest = AssetInTest.builder().fromYear(1).linear().rate(600).assetValueToEndOfLife(120)
AssetAdapterForTest assetInTest = AssetAdapterForTest.builder().fromYear(1).linear().rate(600).assetValueToEndOfLife(120)
.build();
assetInTest.calculate();
@ -18,7 +19,7 @@ public class TestEasyCaseAlgorithm extends TestCase{
public void test_12_months_depreciation( ){
AssetInTest assetInTest = AssetInTest.builder( )
AssetAdapterForTest assetInTest = AssetAdapterForTest.builder( )
.fromYear( 1 ).linear( ).rate( 100 )
.assetValueToEndOfLife( 120 )
.build( );
@ -32,7 +33,7 @@ public class TestEasyCaseAlgorithm extends TestCase{
public void test_2_years_depreciation( ){
AssetInTest assetInTest = AssetInTest.builder( )
AssetAdapterForTest assetInTest = AssetAdapterForTest.builder( )
.fromYear( 1 ).linear( ).rate( 50 )
.assetValueToEndOfLife( 24 )
.build( );

View File

@ -2,6 +2,7 @@ package tk.artikus.assets;
import org.junit.Test;
import tk.artikus.assets.adapters.AssetAdapterForTest;
import tk.artikus.assets.tools.YearMonth;
public class TestMoreComplicatedCaseAlgorithm {
@ -9,7 +10,7 @@ public class TestMoreComplicatedCaseAlgorithm {
@Test
public void test_12_month_depreciation( ) {
AssetInTest assetInTest = AssetInTest.builder().fromYear( 1 ).linear().rate( 100 ).assetValueToEndOfLife( 13 )
AssetAdapterForTest assetInTest = AssetAdapterForTest.builder().fromYear( 1 ).linear().rate( 100 ).assetValueToEndOfLife( 13 )
.build();
assetInTest.calculate();
@ -21,7 +22,7 @@ public class TestMoreComplicatedCaseAlgorithm {
@Test
public void test_2_month_depreciation( ) {
AssetInTest assetInTest = AssetInTest.builder().fromYear( 1 ).digressive().rate( 40 ).factor( 2, 0 )
AssetAdapterForTest assetInTest = AssetAdapterForTest.builder().fromYear( 1 ).digressive().rate( 40 ).factor( 2, 0 )
.assetValueToEndOfLife( 1200 ).build();
assetInTest.calculate();
@ -33,7 +34,7 @@ public class TestMoreComplicatedCaseAlgorithm {
@Test
public void test_12_months_depreciation( ) {
AssetInTest assetInTest = AssetInTest.builder().fromYear( 1 ).linear().rate( 100 ).assetValueToEndOfLife( 1200 )
AssetAdapterForTest assetInTest = AssetAdapterForTest.builder().fromYear( 1 ).linear().rate( 100 ).assetValueToEndOfLife( 1200 )
.build();
assetInTest.calculate();
@ -45,7 +46,7 @@ public class TestMoreComplicatedCaseAlgorithm {
@Test
public void test_12_months_depreciationInMiddle( ) {
AssetInTest assetInTest = AssetInTest.builder()
AssetAdapterForTest assetInTest = AssetAdapterForTest.builder()
.startDepreciation( YearMonth.builder().year( 1 ).month( 11 ).build() ).fromYear( 1 ).linear()
.rate( 100 ).assetValueToEndOfLife( 1200 ).build();
@ -58,7 +59,7 @@ public class TestMoreComplicatedCaseAlgorithm {
@Test
public void test_13_months_depreciation( ) {
AssetInTest assetInTest = AssetInTest.builder().fromYear( 1 ).linear().rate( 600 ).assetValueToEndOfLife( 1201 )
AssetAdapterForTest assetInTest = AssetAdapterForTest.builder().fromYear( 1 ).linear().rate( 600 ).assetValueToEndOfLife( 1201 )
.build();
assetInTest.calculate();
@ -70,7 +71,7 @@ public class TestMoreComplicatedCaseAlgorithm {
@Test
public void test_12_12inMiddle_epreciation100( ) {
AssetInTest assetInTest = AssetInTest.builder().fromYearMonthAssetChangeValue( 0, 12, 1200 )
AssetAdapterForTest assetInTest = AssetAdapterForTest.builder().fromYearMonthAssetChangeValue( 0, 12, 1200 )
.fromYearMonthAssetChangeValue( 1, 3, 1200 ).build();
assetInTest.fromYearDepreciationMethod( 1, AssetDepreciationMethod.builder().linear().rate( 100, 0 ).build() );
@ -85,8 +86,8 @@ public class TestMoreComplicatedCaseAlgorithm {
@Test
public void test_12_12inMiddle_epreciation50( ) {
AssetInTest assetInTest =
AssetInTest.builder()
AssetAdapterForTest assetInTest =
AssetAdapterForTest.builder()
.fromYearMonthAssetChangeValue( 0, 12, 1200 )
.fromYearMonthAssetChangeValue( 1, 3, 1200 ).build();
@ -102,7 +103,7 @@ public class TestMoreComplicatedCaseAlgorithm {
@Test
public void test_is_included_all_changes( ) {
AssetInTest assetInTest = AssetInTest.builder().fromYearMonthAssetChangeValue( 1, 1, 1000 ).build();
AssetAdapterForTest assetInTest = AssetAdapterForTest.builder().fromYearMonthAssetChangeValue( 1, 1, 1000 ).build();
assetInTest.fromYearDepreciationMethod( 1, AssetDepreciationMethod.builder().linear().rate( 600, 0 ).build() );

View File

@ -1,15 +1,23 @@
package tk.artikus.assets;
package tk.artikus.assets.adapters;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.Collection;
import lombok.ToString;
import tk.artikus.assets.Asset;
import tk.artikus.assets.AssetDepreciationAlgorithm;
import tk.artikus.assets.AssetDepreciationMethod;
import tk.artikus.assets.AssetLife;
import tk.artikus.assets.AssetLifeChange;
import tk.artikus.assets.AssetLifeDepreciatied;
import tk.artikus.assets.AssetPlan;
import tk.artikus.assets.AssetPlanPosition;
import tk.artikus.assets.Values;
import tk.artikus.assets.tools.YearMonth;
@ToString
public class AssetInTest {
public class AssetAdapterForTest {
static private final YearMonth FIRST_YEAR = YearMonth.builder().year( 1 ).month( 1 ).build();
@ -21,11 +29,15 @@ public class AssetInTest {
private final AssetPlan assetPlan = new AssetPlan();
public AssetInTest(Asset asset, AssetLifeDepreciatied assetLife) {
public AssetAdapterForTest(Asset asset, AssetLifeDepreciatied assetLife) {
this.assetLifeDepreciatied = assetLife;
this.asset = asset;
}
public void setStartDepreciation( YearMonth start ){
this.asset.setStartOfDepreciation( start );
}
public void fromYearDepreciationMethod( int year, AssetDepreciationMethod adm ) {
asset.setFromYearDepreciationMethod( year, adm );
}
@ -66,11 +78,11 @@ public class AssetInTest {
return new Builder();
}
void calculate( ) {
public void calculate( ) {
new AssetDepreciationAlgorithm( asset, assetLifeDepreciatied, assetPlan ).calculate();
}
static final class Builder {
public static final class Builder {
private Asset asset = new Asset( );
{
@ -119,9 +131,9 @@ public class AssetInTest {
return this;
}
public AssetInTest build( ) {
public AssetAdapterForTest build( ) {
addNewMethod();
return new AssetInTest( asset, assetLife );
return new AssetAdapterForTest( asset, assetLife );
}
public Builder factor( long factor, long factorInHundredths ) {
@ -143,7 +155,7 @@ public class AssetInTest {
}
public Builder assetValueToEndOfLife( long value ) {
AssetLifeChange assetChange = AssetLifeChange.builder().when( AssetInTest.FIRST_YEAR.prev() )
AssetLifeChange assetChange = AssetLifeChange.builder().when( AssetAdapterForTest.FIRST_YEAR.prev() )
.initial( value ).build();
assetLife.registerChange( assetChange );