import {Component, OnInit } from '@angular/core'; import {FormsModule, ReactiveFormsModule, FormControl, FormGroup, Validators} from '@angular/forms'; import {CurrencyPipe,DecimalPipe,PercentPipe} from '@angular/common'; import {AssetsModule} from './assets/assets.module' import {Asset, Positions, AssetPlanPosition, TypeDepreciation } from './assets/asset'; import {AssetService} from './assets/service/asset.service' @Component({ selector: 'app-asset-calculator', standalone: true, imports: [FormsModule, CurrencyPipe, DecimalPipe, PercentPipe, AssetsModule, ReactiveFormsModule ] , templateUrl: "asset-calculator.component.html", styleUrl: 'asset-calculator.component.css' }) export class AssetCalculatorComponent implements OnInit{ assetsDepreciationForm = new FormGroup( { initialValueSet : new FormControl( '', [Validators.required, this.currencyValidator]), depreciationRate: new FormControl('') , year : new FormControl(''), month : new FormControl(''), typeDepreciation: new FormControl( TypeDepreciation.linear ), factorValue : new FormControl( 2,[Validators.required,Validators.max(2)] ) } ); constructor(private assetService : AssetService ){ } TypeDepreciation = TypeDepreciation; asset = new Asset(); positions = new Positions(); calculate() { this.assetService.calculate(this.asset).subscribe( positions => { this.positions=positions; this.calculateToValues() }); } calculateToValues() { let sum :number = 0; for(let position of this.positions.positions ){ position.calculatedDepreciation = position.calculatedDepreciation *0.01; sum += position.calculatedDepreciation; position.sum = sum; } } clazz(pos : AssetPlanPosition ){ return pos.when.year % 2 === 0 ? "table-light" : "table-dark"; } // Walidator dla kwoty currencyValidator(control: any) { const value = control.value; const regex = /^\d+(\.\d{1,2})?$/; // Akceptuje liczby z maksymalnie dwoma miejscami po przecinku return regex.test(value) ? null : { invalidCurrency: true }; } ngOnInit(): void{ this.calculate(); } }