diff --git a/src/app/asset-calculator/asset-calculator.component.ts b/src/app/asset-calculator/asset-calculator.component.ts index e25cab9..b32282e 100644 --- a/src/app/asset-calculator/asset-calculator.component.ts +++ b/src/app/asset-calculator/asset-calculator.component.ts @@ -3,7 +3,7 @@ import {ReactiveFormsModule, FormGroup, Validators, FormControl, FormArray} fro import {CurrencyPipe,DecimalPipe,PercentPipe} from '@angular/common'; -import {Asset, Positions, AssetPlanPosition, TypeDepreciation, YearMonth, AssetLifeChange } from './assets/asset'; +import {Asset, Positions, AssetPlanPosition, TypeDepreciation, YearMonth, AssetLifeChange, YearMonthUtil } from './assets/asset'; import {AssetService} from './assets/service/asset.service' @@ -46,7 +46,7 @@ interface FormValues { const savedAsset = localStorage.getItem('assetForCalculator'); if( savedAsset ) { const asset = JSON.parse( savedAsset ); - this.controlsFromAsset( asset ); + this.assetToControls( asset ); } } @@ -60,9 +60,9 @@ interface FormValues { } - private controlsFromAsset( asset : Asset ) { + private assetToControls( asset : Asset ) { if( asset.life.length > 0){ - const year_month = asset.life[0].when.year +'-'+asset.life[0].when.month; + const year_month = YearMonthUtil.toString( asset.life[0].when ); this.assetsDepreciationFormGroup.patchValue({ year_month : year_month, initialValueAsset : asset.life[0].initial, @@ -74,12 +74,10 @@ interface FormValues { this.addChangeValue(); - const row : FormGroup = this.lifeFormArray.controls[ i - 1 ]; + const row : FormGroup = this.lifeFormArray.controls[ i - 1 ]; const lifeChange : AssetLifeChange = asset.life[i]; row.get( 'initialValueAsset' )?.setValue( lifeChange.initial); - const ym : YearMonth = lifeChange.when; - //const when_txt = ym.toFormValue(); - row.get( 'year_month' )?.setValue( lifeChange.when.year +'-' + lifeChange.when.month ); + row.get( 'year_month' )?.setValue( YearMonthUtil.toString( lifeChange.when ) ); } @@ -98,7 +96,7 @@ interface FormValues { for( let changeControlGroup of this.lifeFormArray.controls ) { const initialValueAsset = changeControlGroup.get('initialValueAsset')?.value; const year_month = changeControlGroup.get('year_month')?.value; - asset.addChange(new AssetLifeChange(new YearMonth(year_month), initialValueAsset, 0, 0)); + asset.addChange(new AssetLifeChange( new YearMonth(year_month), initialValueAsset, 0, 0)); } return asset; } diff --git a/src/app/asset-calculator/assets/asset.ts b/src/app/asset-calculator/assets/asset.ts index 4167b94..c5b9f28 100644 --- a/src/app/asset-calculator/assets/asset.ts +++ b/src/app/asset-calculator/assets/asset.ts @@ -4,27 +4,26 @@ export enum TypeDepreciation{ } export class YearMonth{ + readonly year : number ; readonly month : number ; + constructor( year_month:string ){ + const [year, month] = year_month.split('-').map(Number); + this.year = year; + this.month = month; - constructor( year_month:string | YearMonth ){ - if (typeof year_month === 'string') { - const [year, month] = year_month.split('-').map(Number); - this.year = year; - this.month = month; - } else { - this.year = year_month.year; - this.month = year_month.month; - } - } - - toFormValue() { - return `${this.year}-${this.month.toString().padStart(2, '0')}`; + +} + +export class YearMonthUtil{ + static toString( ym : YearMonth ):string{ + return ym.year + '-' + ym.month ; } } + export class AssetLifeChange{ readonly when : YearMonth; readonly initial : number;