From 7b953c57619a1303dd2deeeb1106d8bd2b92aa63 Mon Sep 17 00:00:00 2001 From: Artur Date: Fri, 25 Oct 2024 00:07:07 +0200 Subject: [PATCH] New version with story life of Asset --- .../assets/services/AssetFromAngular.java | 100 +++++++++++------- .../tk/artikus/assets/tools/YearMonth.java | 6 ++ 2 files changed, 68 insertions(+), 38 deletions(-) diff --git a/src/main/java/tk/artikus/assets/services/AssetFromAngular.java b/src/main/java/tk/artikus/assets/services/AssetFromAngular.java index 2ba4004..e9b43af 100644 --- a/src/main/java/tk/artikus/assets/services/AssetFromAngular.java +++ b/src/main/java/tk/artikus/assets/services/AssetFromAngular.java @@ -1,53 +1,77 @@ package tk.artikus.assets.services; -import lombok.Data; import tk.artikus.assets.Asset; import tk.artikus.assets.AssetDepreciationMethod; import tk.artikus.assets.AssetLife; import tk.artikus.assets.AssetLifeChange; import tk.artikus.assets.tools.YearMonth; -@Data -public class AssetFromAngular { - - String initialValueAsset; - - int year; - int month; - AssetDepreciationMethod.Type type; - String depreciationRate; - String factorValue; - - Asset toAsset() { - - Asset asset = new Asset(); - AssetLife assetLife = new AssetLife(); - - YearMonth yearMonth = YearMonth.builder().year( year ).month( month ).build(); - asset.setStartOfDepreciation( yearMonth ); - - long lDepreciation = txtToLong( depreciationRate ); - long lFactor = txtToLong( factorValue ); - AssetDepreciationMethod assetDepreciationMethod = AssetDepreciationMethod.builder().type( type ) - .factor( 0, lFactor ).rate( 0, lDepreciation ).build(); - - asset.setFromYearDepreciationMethod( yearMonth.getYear(), assetDepreciationMethod ); - - long initialValue = txtToLong( initialValueAsset ); - - AssetLifeChange assetLifeChange = new AssetLifeChange( yearMonth.prev(), initialValue, 0, 0 ); - assetLife.registerChange( assetLifeChange ); - - asset.setLife( assetLife ); - - return asset; - } - - private long txtToLong( String value ) { +class Convert{ + static long txtToLong( String value ) { value = value.replace( ",", "" ); float fInitial = Float.parseFloat( value ); long lValue = (long) (fInitial * 100); return lValue; + } +} + + + +record YearMonthFromAngular(int year, int month) { + YearMonth toYearMonth() { + return YearMonth.builder().year( year ).month( month ).build(); + } +} + +record AssetLifeChangeFromAngular(YearMonthFromAngular when, String initial, String residual, String depreciation) { + AssetLifeChange toAssetLifeChange( boolean isFirst ) { + + YearMonth yearMonth = when.toYearMonth(); + if( isFirst ) { + yearMonth = yearMonth.prev(); + } + return AssetLifeChange.builder().when( yearMonth ) + .initial( Convert.txtToLong(initial) ) + .residual( Convert.txtToLong( residual ) ) + .depreciation( Convert.txtToLong( depreciation ) ) + .build(); + } +} + +public record AssetFromAngular( + YearMonthFromAngular start, + String depreciationRate, + AssetDepreciationMethod.Type type, + String factorValue, + AssetLifeChangeFromAngular[] life) { + + Asset toAsset() { + + Asset asset = new Asset(); + + + YearMonth start = this.start.toYearMonth(); + asset.setStartOfDepreciation( start ); + + long lDepreciation = Convert.txtToLong( depreciationRate ); + long lFactor = Convert.txtToLong( factorValue ); + AssetDepreciationMethod assetDepreciationMethod = AssetDepreciationMethod.builder().type( type ) + .factor( 0, lFactor ).rate( 0, lDepreciation ).build(); + + asset.setFromYearDepreciationMethod( start.getYear(), assetDepreciationMethod ); + + + AssetLife assetLife = new AssetLife(); + + for( AssetLifeChangeFromAngular l : life ) { + AssetLifeChange assetLifeChange = l.toAssetLifeChange(assetLife.noStory() ); + assetLife.registerChange( assetLifeChange ); + + } + asset.setLife( assetLife ); + + return asset; } + } diff --git a/src/main/java/tk/artikus/assets/tools/YearMonth.java b/src/main/java/tk/artikus/assets/tools/YearMonth.java index 1695660..dd6329c 100644 --- a/src/main/java/tk/artikus/assets/tools/YearMonth.java +++ b/src/main/java/tk/artikus/assets/tools/YearMonth.java @@ -174,6 +174,12 @@ public class YearMonth implements Comparable< YearMonth >{ return this; } + public Builder yearMonth( YearMonth yearMonth ){ + this.year = yearMonth.getYear(); + this.month = yearMonth.getMonth(); + return this; + } + public Builder month( int month ){ this.month = month; return this;