diff --git a/src/app/asset-calculator/asset-calculator.component.html b/src/app/asset-calculator/asset-calculator.component.html index 0fcb3d1..46e9ce0 100644 --- a/src/app/asset-calculator/asset-calculator.component.html +++ b/src/app/asset-calculator/asset-calculator.component.html @@ -24,13 +24,13 @@
- +
- +
- +
@@ -60,10 +60,10 @@ @if( TypeDepreciation.digressive === assetsDepreciationFormGroup.get( 'typeDepreciation' )?.value ) {
- +
- +
} diff --git a/src/app/asset-calculator/asset-calculator.component.ts b/src/app/asset-calculator/asset-calculator.component.ts index ebf3f60..d7cf8de 100644 --- a/src/app/asset-calculator/asset-calculator.component.ts +++ b/src/app/asset-calculator/asset-calculator.component.ts @@ -3,16 +3,16 @@ import { ReactiveFormsModule, FormGroup, Validators, FormControl, FormArray } f import { CurrencyPipe,DecimalPipe,PercentPipe } from '@angular/common'; -import {Asset, Positions, AssetPlanPosition, TypeDepreciation, YearMonth, AssetLifeChange, YearMonthUtil } from './assets/asset'; -import {AssetService} from './assets/service/asset.service' +import {Asset, Positions, AssetPlanPosition, TypeDepreciation, YearMonth, AssetLifeChange, AssetDepreciationMethod, YearMonthUtil } from '../assets/asset'; +import {AssetService} from '../assets/service/asset.service' interface FormValues { initialValueAsset: number; - depreciationRate: number; + rate: number; year_month: string; typeDepreciation: TypeDepreciation; - factorValue: number; + factor: number; } @Component({ selector: 'app-asset-calculator', @@ -22,18 +22,19 @@ templateUrl: "asset-calculator.component.html", styleUrl: 'asset-calculator.component.css' }) export class AssetCalculatorComponent implements OnInit, OnDestroy{ - + + TypeDepreciation = TypeDepreciation; amortizations = new Positions(); - TypeDepreciation = TypeDepreciation; + lifeFormArray = new FormArray([]); assetsDepreciationFormGroup = new FormGroup( { - initialValueAsset : new FormControl( 3000, [ Validators.required, this.currencyValidator ]), - depreciationRate : new FormControl( 20 ) , + initialValueAsset : new FormControl( 5000, [ Validators.required, this.currencyValidator ]), + rate : new FormControl( 20 ) , year_month : new FormControl( "2024-10" ), typeDepreciation : new FormControl( TypeDepreciation.linear ), - factorValue : new FormControl( 2, [Validators.required,Validators.max(2)] ), + factor : new FormControl( 2/*, [Validators.required,Validators.max(2)]*/ ), } ); @@ -43,33 +44,32 @@ export class AssetCalculatorComponent implements OnInit, OnDestroy{ } ngOnInit(): void{ - const savedAsset = localStorage.getItem('assetForCalculator'); - if (savedAsset) this.assetToControls(JSON.parse(savedAsset)); + 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 ){ - const year_month = YearMonthUtil.toString( asset.life[0].when ); + const year_month = YearMonthUtil.toTxt( asset.life[0].when ); this.assetsDepreciationFormGroup.patchValue({ year_month : year_month, initialValueAsset : asset.life[0].initial, - depreciationRate : asset.depreciationRate, - typeDepreciation : asset.type, - factorValue : asset.factorValue, + rate : asset.depreciationMethods[0].rate, + typeDepreciation : asset.depreciationMethods[0].type, + factor : asset.depreciationMethods[0].factor, }) asset.life.slice(1).forEach( lifeChange => { this.addChangeValue(); const row = this.lifeFormArray.at( this.lifeFormArray.length - 1 ) as FormGroup; row.patchValue( { initialValueAsset: lifeChange.initial, - year_month: YearMonthUtil.toString( lifeChange.when ) + year_month: YearMonthUtil.toTxt( lifeChange.when ) } ); }); } @@ -79,15 +79,20 @@ export class AssetCalculatorComponent implements OnInit, OnDestroy{ private controlsToAsset() : Asset { const formValues = this.assetsDepreciationFormGroup.value as FormValues; - const when = new YearMonth( formValues.year_month ); - const asset = new Asset( formValues.depreciationRate, when, formValues.typeDepreciation, formValues.factorValue ); - const creationlifeChange = new AssetLifeChange(when, formValues.initialValueAsset , 0, 0); + + const when = new YearMonth( formValues.year_month ); + const asset = new Asset( when ); + + const method = new AssetDepreciationMethod( when.year, formValues.rate, formValues.typeDepreciation, formValues.factor ); + asset.addMethod( method ); + + const creationlifeChange = new AssetLifeChange( when, formValues.initialValueAsset, 0, 0 ); asset.addChange( creationlifeChange ); 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)); + const yearMonth = control.get('year_month')?.value; + asset.addChange( new AssetLifeChange( new YearMonth(yearMonth), initialValue, 0, 0 ) ); }) return asset; diff --git a/src/app/asset-calculator/assets/asset.spec.ts b/src/app/assets/asset.spec.ts similarity index 100% rename from src/app/asset-calculator/assets/asset.spec.ts rename to src/app/assets/asset.spec.ts diff --git a/src/app/asset-calculator/assets/asset.ts b/src/app/assets/asset.ts similarity index 65% rename from src/app/asset-calculator/assets/asset.ts rename to src/app/assets/asset.ts index c5b9f28..b9acdec 100644 --- a/src/app/asset-calculator/assets/asset.ts +++ b/src/app/assets/asset.ts @@ -17,14 +17,20 @@ export class YearMonth{ } export class YearMonthUtil{ - static toString( ym : YearMonth ):string{ + + static toTxt( ym : YearMonth ):string{ return ym.year + '-' + ym.month ; } + + static toToday( ):YearMonth{ + const today = new Date(); + return new YearMonth( today.getFullYear() + '-' +today.getMonth()+1 ); + } + } - - export class AssetLifeChange{ + readonly when : YearMonth; readonly initial : number; readonly residual : number; @@ -34,37 +40,51 @@ export class AssetLifeChange{ initial : number, residual : number, depreciation: number ){ + this.when = when; this.initial = initial; this.residual = residual; this.depreciation = depreciation; + } } +export class AssetDepreciationMethod{ + readonly year : number; + readonly type : TypeDepreciation; + readonly rate : number ; + readonly factor : number; + constructor( year : number, + rate : number, + type : TypeDepreciation, + factor : number ){ + + this.year = year; + this.rate = rate; + this.type = type; + this.factor = factor; + } +} export class Asset { start : YearMonth ; - depreciationRate : number ; - type : TypeDepreciation; - factorValue : number; - life : AssetLifeChange[]=[]; + life : AssetLifeChange[]=[]; + depreciationMethods : AssetDepreciationMethod[]=[]; addChange( change : AssetLifeChange ){ this.life.push( change ); } - constructor( depreciationRate : number, - start : YearMonth, - type : TypeDepreciation, - factorValue : number ){ - this.depreciationRate = depreciationRate; + addMethod( method : AssetDepreciationMethod ){ + this.depreciationMethods.push( method ); + } + + constructor( start : YearMonth ){ this.start = start; - this.type = type; - this.factorValue = factorValue; } } @@ -73,10 +93,6 @@ export class AssetsContainer{ assets: Map = new Map(); - constructor(){ - } - - delete( nrInv: string ){ this.assets.delete(nrInv); } diff --git a/src/app/asset-calculator/assets/assets.module.ts b/src/app/assets/assets.module.ts similarity index 100% rename from src/app/asset-calculator/assets/assets.module.ts rename to src/app/assets/assets.module.ts diff --git a/src/app/asset-calculator/assets/service/asset.service.spec.ts b/src/app/assets/service/asset.service.spec.ts similarity index 100% rename from src/app/asset-calculator/assets/service/asset.service.spec.ts rename to src/app/assets/service/asset.service.spec.ts diff --git a/src/app/asset-calculator/assets/service/asset.service.ts b/src/app/assets/service/asset.service.ts similarity index 87% rename from src/app/asset-calculator/assets/service/asset.service.ts rename to src/app/assets/service/asset.service.ts index 308bb24..9cec5e9 100644 --- a/src/app/asset-calculator/assets/service/asset.service.ts +++ b/src/app/assets/service/asset.service.ts @@ -12,7 +12,7 @@ import { Observable } from 'rxjs'; }) export class AssetService { - // private assetUrl: string ='http://localhost:8800/rest-api/assets/calculate'; + // private assetUrl: string ='http://localhost:8800/rest-api/assets/calculate'; //private assetUrl: string ='http://localhost:5001/rest-api/assets/calculate'; diff --git a/src/app/fixed-asset/fixed-asset.component.ts b/src/app/fixed-asset/fixed-asset.component.ts index bb9620a..2eba9a6 100644 --- a/src/app/fixed-asset/fixed-asset.component.ts +++ b/src/app/fixed-asset/fixed-asset.component.ts @@ -1,6 +1,7 @@ -import { Component } from '@angular/core'; -import { AssetsContainer, Asset, TypeDepreciation, YearMonth } from '../asset-calculator/assets/asset'; -import { ReactiveFormsModule, FormBuilder, FormGroup, Validators, FormControl, FormArray} from '@angular/forms'; +import { Component, OnInit, OnDestroy } from '@angular/core'; +import { Asset, TypeDepreciation, YearMonthUtil, AssetLifeChange } from '../assets/asset'; +import { ReactiveFormsModule, FormBuilder, FormGroup, FormArray, ValidatorFn, ValidationErrors, AbstractControl } from '@angular/forms'; + @Component({ @@ -19,8 +20,8 @@ import { ReactiveFormsModule, FormBuilder, FormGroup, Validators, FormControl, F Numer inwentarzowy Wartość Metoda amortyzacji - Stawka - + @@ -28,15 +29,16 @@ import { ReactiveFormsModule, FormBuilder, FormGroup, Validators, FormControl, F @for( assetFormGroup of assetFormArray.controls; track $index ) { - + - - @@ -50,55 +52,92 @@ import { ReactiveFormsModule, FormBuilder, FormGroup, Validators, FormControl, F + `, styleUrl: './fixed-asset.component.css' }) export class FixedAssetComponent { - + static STORAGE_KEY = "fixed_assets"; + static indexNr = 1; TypeDepreciation = TypeDepreciation; - assetsContainer : AssetsContainer; - - assetFormArray : FormArray; - constructor( private fb: FormBuilder ){ - - this.assetsContainer = new AssetsContainer (); this.assetFormArray = this.fb.array([]) + } + + ngOnInit(): void { + const savedAssets = localStorage.getItem( FixedAssetComponent.STORAGE_KEY) ; + if( savedAssets ) { + this.assetsToControls( JSON.parse( savedAssets ) ); + } + } + + ngOnDestroy(): void { + localStorage.setItem( FixedAssetComponent.STORAGE_KEY, JSON.stringify( this.controlsToAssets() ) ); + } + + private assetsToControls( assetsArray: [ string, Asset ][]) { + const assetsMap = new Map( assetsArray ); + assetsMap.forEach( (asset, key) => { + this.addRow( key, asset.life[0].initial ); + }); + } + + private controlsToAssets(): [string, Asset][] { + + const assets = new Map(); + this.assetFormArray.controls.forEach( control => { + + const nrInv = control.get('nrInv')?.value; + const initialValue = control.get('initialValue')?.value; + const yearMonth = YearMonthUtil.toToday(); + const asset = new Asset( yearMonth ); + const assetLifeChange = new AssetLifeChange( yearMonth, initialValue, 0, 0 ); + asset.addChange( assetLifeChange ); + assets.set( nrInv, asset ); + + }); + + return Array.from( assets.entries() ); } delete( index : number ){ - this.assetFormArray.removeAt( index ); + this.assetFormArray.removeAt( index ); } addNew(){ - this.addNr("NrInv" + FixedAssetComponent.indexNr++ ); + this.addRow( "NrInv" + FixedAssetComponent.indexNr++, 1000 ); } - addNr( nrInv:string ):void{ + addRow( nrInv_:string, initial_: number ):void{ + + const newRow = this.fb.group({ + nrInv : [ nrInv_ ], + initialValue : [ initial_ ], + }); - const today = new Date(); - const year = today.getFullYear(); - const month = today.getMonth() + 1; - const year_month = year +'-'+month; - const yearMonth = new YearMonth( year_month ) ; - const asset = new Asset( 20, yearMonth, TypeDepreciation.linear, 10 ); - - const newRow= this.fb.group({ - nr : [ nrInv ], - initialValue : [ 10000 ], - depreciationRate : [ 10 ], - typeDepreciation : [ TypeDepreciation.linear ], - year_month : [ year_month ] - }) + // typeDepreciation : [ TypeDepreciation.linear ], + // depreciationRate : [ 10 ], + // year_month : [ year_month ] this.assetFormArray.push( newRow ); + } + + // uniqueColumnValidator(): ValidatorFn { + // return (control: AbstractControl): ValidationErrors | null => { + + // const values = this.assetFormArray.map(field => field.value) || []; + // const uniqueValues = new Set(values); + // return uniqueValues.size !== values.length ? { uniqueColumn: true } : null; + // }; + // } + } diff --git a/src/app/validators/unique-column.validator.ts b/src/app/validators/unique-column.validator.ts new file mode 100644 index 0000000..4f0442e --- /dev/null +++ b/src/app/validators/unique-column.validator.ts @@ -0,0 +1,9 @@ +import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms'; + +export function uniqueColumnValidator(columnName: string): ValidatorFn { + return (control: AbstractControl): ValidationErrors | null => { + const formArray = control.get(columnName) as AbstractControl[]; + const values = formArray?.map(field => field.value) || []; + const uniqueValues = new Set(values); + return uniqueValues.size !== values.length ? { uniqueColumn: true } : null; + };