From d985d870863a576ec60688f6dcd07cb97d2196a1 Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 4 Nov 2024 12:41:43 +0100 Subject: [PATCH] Optimization with AI --- .../asset-calculator.component.ts | 87 +++++++++---------- 1 file changed, 40 insertions(+), 47 deletions(-) diff --git a/src/app/asset-calculator/asset-calculator.component.ts b/src/app/asset-calculator/asset-calculator.component.ts index b32282e..ebf3f60 100644 --- a/src/app/asset-calculator/asset-calculator.component.ts +++ b/src/app/asset-calculator/asset-calculator.component.ts @@ -1,6 +1,6 @@ -import {Component, effect, OnInit, OnDestroy, signal } from '@angular/core'; -import {ReactiveFormsModule, FormGroup, Validators, FormControl, FormArray} from '@angular/forms'; -import {CurrencyPipe,DecimalPipe,PercentPipe} from '@angular/common'; +import { Component, OnInit, OnDestroy } from '@angular/core'; +import { ReactiveFormsModule, FormGroup, Validators, FormControl, FormArray } from '@angular/forms'; +import { CurrencyPipe,DecimalPipe,PercentPipe } from '@angular/common'; import {Asset, Positions, AssetPlanPosition, TypeDepreciation, YearMonth, AssetLifeChange, YearMonthUtil } from './assets/asset'; @@ -14,14 +14,14 @@ interface FormValues { typeDepreciation: TypeDepreciation; factorValue: number; } - @Component({ - selector: 'app-asset-calculator', - standalone: true, - imports: [ CurrencyPipe, DecimalPipe, PercentPipe, ReactiveFormsModule ] , - templateUrl: "asset-calculator.component.html", - styleUrl: 'asset-calculator.component.css' - }) - export class AssetCalculatorComponent implements OnInit, OnDestroy{ +@Component({ +selector: 'app-asset-calculator', +standalone: true, +imports: [ CurrencyPipe, DecimalPipe, PercentPipe, ReactiveFormsModule ] , +templateUrl: "asset-calculator.component.html", +styleUrl: 'asset-calculator.component.css' +}) +export class AssetCalculatorComponent implements OnInit, OnDestroy{ amortizations = new Positions(); TypeDepreciation = TypeDepreciation; @@ -38,30 +38,24 @@ interface FormValues { } ); constructor(private assetService : AssetService ){ - - effect( () => { this.calculate(); }); this.assetsDepreciationFormGroup.valueChanges.subscribe( (value) => this.calculate() ); this.lifeFormArray.valueChanges.subscribe( (value) => this.calculate() ); - - const savedAsset = localStorage.getItem('assetForCalculator'); - if( savedAsset ) { - const asset = JSON.parse( savedAsset ); - this.assetToControls( asset ); - } } ngOnInit(): void{ - + const savedAsset = localStorage.getItem('assetForCalculator'); + if (savedAsset) this.assetToControls(JSON.parse(savedAsset)); } ngOnDestroy(): void { // Zapisywanie danych przed zniszczeniem komponentu - + localStorage.setItem('assetForCalculator', JSON.stringify(this.controlsToAsset())); + } private assetToControls( asset : Asset ) { - if( asset.life.length > 0){ + if( asset.life.length > 0 ){ const year_month = YearMonthUtil.toString( asset.life[0].when ); this.assetsDepreciationFormGroup.patchValue({ year_month : year_month, @@ -70,17 +64,14 @@ interface FormValues { typeDepreciation : asset.type, factorValue : asset.factorValue, }) - for( let i = 1; i < asset.life.length; i++ ){ - + asset.life.slice(1).forEach( lifeChange => { this.addChangeValue(); - - const row : FormGroup = this.lifeFormArray.controls[ i - 1 ]; - const lifeChange : AssetLifeChange = asset.life[i]; - row.get( 'initialValueAsset' )?.setValue( lifeChange.initial); - row.get( 'year_month' )?.setValue( YearMonthUtil.toString( lifeChange.when ) ); - - - } + const row = this.lifeFormArray.at( this.lifeFormArray.length - 1 ) as FormGroup; + row.patchValue( { + initialValueAsset: lifeChange.initial, + year_month: YearMonthUtil.toString( lifeChange.when ) + } ); + }); } } @@ -93,17 +84,21 @@ interface FormValues { const creationlifeChange = new AssetLifeChange(when, formValues.initialValueAsset , 0, 0); asset.addChange( creationlifeChange ); - 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)); - } + this.lifeFormArray.controls.forEach(control => { + const initialValue = control.get('initialValueAsset')?.value; + const yearMonth = control.get('year_month')?.value; + asset.addChange(new AssetLifeChange(new YearMonth(yearMonth), initialValue, 0, 0)); + }) + return asset; } calculate(){ const asset = this.controlsToAsset(); - this.calculateForAsset( asset ); + this.assetService.calculate(asset).subscribe(positions => { + this.amortizations = positions; + this.calculateToValues(positions); + }); localStorage.setItem('assetForCalculator', JSON.stringify(asset)); } @@ -117,20 +112,18 @@ interface FormValues { calculateToValues( positions:Positions ) { let sum :number = 0; let sumThisYear: number = 0; - for(let position of positions.positions ){ - - position.calculatedDepreciation = position.calculatedDepreciation *0.01; - + positions.positions.forEach(position => { + position.calculatedDepreciation *= 0.01; sum += position.calculatedDepreciation; position.sum = sum; - if( 1 == position.when.month ){ + if (position.when.month === 1) { sumThisYear = position.calculatedDepreciation; - }else{ - sumThisYear += position.calculatedDepreciation + } else { + sumThisYear += position.calculatedDepreciation; } - position.sumThisYear = sumThisYear - } + position.sumThisYear = sumThisYear; + }); } clazz(pos : AssetPlanPosition ){