YearMonthUtil because the base class have to be the same like in java

This commit is contained in:
Artur 2024-11-04 10:44:14 +01:00
parent e75a8ee2fb
commit 71d4a63126
2 changed files with 19 additions and 22 deletions

View File

@ -3,7 +3,7 @@ import {ReactiveFormsModule, FormGroup, Validators, FormControl, FormArray} fro
import {CurrencyPipe,DecimalPipe,PercentPipe} from '@angular/common'; 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' import {AssetService} from './assets/service/asset.service'
@ -46,7 +46,7 @@ interface FormValues {
const savedAsset = localStorage.getItem('assetForCalculator'); const savedAsset = localStorage.getItem('assetForCalculator');
if( savedAsset ) { if( savedAsset ) {
const asset = JSON.parse( 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){ 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({ this.assetsDepreciationFormGroup.patchValue({
year_month : year_month, year_month : year_month,
initialValueAsset : asset.life[0].initial, initialValueAsset : asset.life[0].initial,
@ -74,12 +74,10 @@ interface FormValues {
this.addChangeValue(); this.addChangeValue();
const row : FormGroup = this.lifeFormArray.controls[ i - 1 ]; const row : FormGroup = this.lifeFormArray.controls[ i - 1 ];
const lifeChange : AssetLifeChange = asset.life[i]; const lifeChange : AssetLifeChange = asset.life[i];
row.get( 'initialValueAsset' )?.setValue( lifeChange.initial); row.get( 'initialValueAsset' )?.setValue( lifeChange.initial);
const ym : YearMonth = lifeChange.when; row.get( 'year_month' )?.setValue( YearMonthUtil.toString( lifeChange.when ) );
//const when_txt = ym.toFormValue();
row.get( 'year_month' )?.setValue( lifeChange.when.year +'-' + lifeChange.when.month );
} }
@ -98,7 +96,7 @@ interface FormValues {
for( let changeControlGroup of this.lifeFormArray.controls ) { for( let changeControlGroup of this.lifeFormArray.controls ) {
const initialValueAsset = changeControlGroup.get('initialValueAsset')?.value; const initialValueAsset = changeControlGroup.get('initialValueAsset')?.value;
const year_month = changeControlGroup.get('year_month')?.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; return asset;
} }

View File

@ -4,27 +4,26 @@ export enum TypeDepreciation{
} }
export class YearMonth{ export class YearMonth{
readonly year : number ; readonly year : number ;
readonly month : 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{ export class AssetLifeChange{
readonly when : YearMonth; readonly when : YearMonth;
readonly initial : number; readonly initial : number;