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,6 +1,6 @@
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 { ReactiveFormsModule, FormGroup, Validators, FormControl, FormArray } from '@angular/forms';
import {CurrencyPipe,DecimalPipe,PercentPipe} from '@angular/common'; import { CurrencyPipe,DecimalPipe,PercentPipe } from '@angular/common';
import {Asset, Positions, AssetPlanPosition, TypeDepreciation, YearMonth, AssetLifeChange, YearMonthUtil } from './assets/asset'; import {Asset, Positions, AssetPlanPosition, TypeDepreciation, YearMonth, AssetLifeChange, YearMonthUtil } from './assets/asset';
@ -14,14 +14,14 @@ interface FormValues {
typeDepreciation: TypeDepreciation; typeDepreciation: TypeDepreciation;
factorValue: number; factorValue: number;
} }
@Component({ @Component({
selector: 'app-asset-calculator', selector: 'app-asset-calculator',
standalone: true, standalone: true,
imports: [ CurrencyPipe, DecimalPipe, PercentPipe, ReactiveFormsModule ] , imports: [ CurrencyPipe, DecimalPipe, PercentPipe, ReactiveFormsModule ] ,
templateUrl: "asset-calculator.component.html", templateUrl: "asset-calculator.component.html",
styleUrl: 'asset-calculator.component.css' styleUrl: 'asset-calculator.component.css'
}) })
export class AssetCalculatorComponent implements OnInit, OnDestroy{ export class AssetCalculatorComponent implements OnInit, OnDestroy{
amortizations = new Positions(); amortizations = new Positions();
TypeDepreciation = TypeDepreciation; TypeDepreciation = TypeDepreciation;
@ -38,30 +38,24 @@ interface FormValues {
} ); } );
constructor(private assetService : AssetService ){ constructor(private assetService : AssetService ){
effect( () => { this.calculate(); });
this.assetsDepreciationFormGroup.valueChanges.subscribe( (value) => this.calculate() ); this.assetsDepreciationFormGroup.valueChanges.subscribe( (value) => this.calculate() );
this.lifeFormArray.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{ ngOnInit(): void{
const savedAsset = localStorage.getItem('assetForCalculator');
if (savedAsset) this.assetToControls(JSON.parse(savedAsset));
} }
ngOnDestroy(): void { ngOnDestroy(): void {
// Zapisywanie danych przed zniszczeniem komponentu // Zapisywanie danych przed zniszczeniem komponentu
localStorage.setItem('assetForCalculator', JSON.stringify(this.controlsToAsset()));
} }
private assetToControls( asset : Asset ) { private assetToControls( asset : Asset ) {
if( asset.life.length > 0){ if( asset.life.length > 0 ){
const year_month = YearMonthUtil.toString( asset.life[0].when ); const year_month = YearMonthUtil.toString( asset.life[0].when );
this.assetsDepreciationFormGroup.patchValue({ this.assetsDepreciationFormGroup.patchValue({
year_month : year_month, year_month : year_month,
@ -70,17 +64,14 @@ interface FormValues {
typeDepreciation : asset.type, typeDepreciation : asset.type,
factorValue : asset.factorValue, factorValue : asset.factorValue,
}) })
for( let i = 1; i < asset.life.length; i++ ){ asset.life.slice(1).forEach( lifeChange => {
this.addChangeValue(); this.addChangeValue();
const row = this.lifeFormArray.at( this.lifeFormArray.length - 1 ) as FormGroup;
const row : FormGroup = this.lifeFormArray.controls[ i - 1 ]; row.patchValue( {
const lifeChange : AssetLifeChange = asset.life[i]; initialValueAsset: lifeChange.initial,
row.get( 'initialValueAsset' )?.setValue( lifeChange.initial); year_month: YearMonthUtil.toString( lifeChange.when )
row.get( 'year_month' )?.setValue( YearMonthUtil.toString( lifeChange.when ) ); } );
});
}
} }
} }
@ -93,17 +84,21 @@ interface FormValues {
const creationlifeChange = new AssetLifeChange(when, formValues.initialValueAsset , 0, 0); const creationlifeChange = new AssetLifeChange(when, formValues.initialValueAsset , 0, 0);
asset.addChange( creationlifeChange ); asset.addChange( creationlifeChange );
for( let changeControlGroup of this.lifeFormArray.controls ) { this.lifeFormArray.controls.forEach(control => {
const initialValueAsset = changeControlGroup.get('initialValueAsset')?.value; const initialValue = control.get('initialValueAsset')?.value;
const year_month = changeControlGroup.get('year_month')?.value; const yearMonth = control.get('year_month')?.value;
asset.addChange(new AssetLifeChange( new YearMonth(year_month), initialValueAsset, 0, 0)); asset.addChange(new AssetLifeChange(new YearMonth(yearMonth), initialValue, 0, 0));
} })
return asset; return asset;
} }
calculate(){ calculate(){
const asset = this.controlsToAsset(); 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)); localStorage.setItem('assetForCalculator', JSON.stringify(asset));
} }
@ -117,20 +112,18 @@ interface FormValues {
calculateToValues( positions:Positions ) { calculateToValues( positions:Positions ) {
let sum :number = 0; let sum :number = 0;
let sumThisYear: number = 0; let sumThisYear: number = 0;
for(let position of positions.positions ){ positions.positions.forEach(position => {
position.calculatedDepreciation *= 0.01;
position.calculatedDepreciation = position.calculatedDepreciation *0.01;
sum += position.calculatedDepreciation; sum += position.calculatedDepreciation;
position.sum = sum; position.sum = sum;
if( 1 == position.when.month ){ if (position.when.month === 1) {
sumThisYear = position.calculatedDepreciation; sumThisYear = position.calculatedDepreciation;
}else{ } else {
sumThisYear += position.calculatedDepreciation sumThisYear += position.calculatedDepreciation;
}
position.sumThisYear = sumThisYear
} }
position.sumThisYear = sumThisYear;
});
} }
clazz(pos : AssetPlanPosition ){ clazz(pos : AssetPlanPosition ){