Optimization with AI

This commit is contained in:
Artur 2024-11-04 12:41:43 +01:00
parent 71d4a63126
commit d985d87086
1 changed files with 40 additions and 47 deletions

View File

@ -1,4 +1,4 @@
import {Component, effect, OnInit, OnDestroy, signal } from '@angular/core';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ReactiveFormsModule, FormGroup, Validators, FormControl, FormArray } from '@angular/forms';
import { CurrencyPipe,DecimalPipe,PercentPipe } from '@angular/common';
@ -38,25 +38,19 @@ 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()));
}
@ -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
}
position.sumThisYear = sumThisYear
sumThisYear += position.calculatedDepreciation;
}
position.sumThisYear = sumThisYear;
});
}
clazz(pos : AssetPlanPosition ){