From b86eebbfdce1303dbcfa6464431e1a4be145574e Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Fri, 22 Apr 2022 17:06:13 +0200 Subject: [PATCH] Fix calculation for sell and dividend activities --- ...-or-update-transaction-dialog.component.ts | 108 +++++++++++------- .../create-or-update-transaction-dialog.html | 4 +- 2 files changed, 68 insertions(+), 44 deletions(-) diff --git a/apps/client/src/app/pages/portfolio/transactions/create-or-update-transaction-dialog/create-or-update-transaction-dialog.component.ts b/apps/client/src/app/pages/portfolio/transactions/create-or-update-transaction-dialog/create-or-update-transaction-dialog.component.ts index 134adcdc5..920120900 100644 --- a/apps/client/src/app/pages/portfolio/transactions/create-or-update-transaction-dialog/create-or-update-transaction-dialog.component.ts +++ b/apps/client/src/app/pages/portfolio/transactions/create-or-update-transaction-dialog/create-or-update-transaction-dialog.component.ts @@ -46,6 +46,7 @@ export class CreateOrUpdateTransactionDialog implements OnDestroy { public filteredLookupItemsObservable: Observable; public isLoading = false; public platforms: { id: string; name: string }[]; + public total = 0; public Validators = Validators; private unsubscribeSubject = new Subject(); @@ -89,6 +90,25 @@ export class CreateOrUpdateTransactionDialog implements OnDestroy { unitPrice: [this.data.activity?.unitPrice, Validators.required] }); + this.activityForm.valueChanges + .pipe(takeUntil(this.unsubscribeSubject)) + .subscribe(() => { + if ( + this.activityForm.controls['type'].value === 'BUY' || + this.activityForm.controls['type'].value === 'ITEM' + ) { + this.total = + this.activityForm.controls['quantity'].value * + this.activityForm.controls['unitPrice'].value + + this.activityForm.controls['fee'].value ?? 0; + } else { + this.total = + this.activityForm.controls['quantity'].value * + this.activityForm.controls['unitPrice'].value - + this.activityForm.controls['fee'].value ?? 0; + } + }); + this.filteredLookupItemsObservable = this.activityForm.controls[ 'searchSymbol' ].valueChanges.pipe( @@ -100,9 +120,11 @@ export class CreateOrUpdateTransactionDialog implements OnDestroy { const filteredLookupItemsObservable = this.dataService.fetchSymbols(query); - filteredLookupItemsObservable.subscribe((filteredLookupItems) => { - this.filteredLookupItems = filteredLookupItems; - }); + filteredLookupItemsObservable + .pipe(takeUntil(this.unsubscribeSubject)) + .subscribe((filteredLookupItems) => { + this.filteredLookupItems = filteredLookupItems; + }); return filteredLookupItemsObservable; } @@ -111,45 +133,47 @@ export class CreateOrUpdateTransactionDialog implements OnDestroy { }) ); - this.activityForm.controls['type'].valueChanges.subscribe((type: Type) => { - if (type === 'ITEM') { - this.activityForm.controls['accountId'].removeValidators( - Validators.required - ); - this.activityForm.controls['accountId'].updateValueAndValidity(); - this.activityForm.controls['currency'].setValue( - this.data.user.settings.baseCurrency - ); - this.activityForm.controls['dataSource'].removeValidators( - Validators.required - ); - this.activityForm.controls['dataSource'].updateValueAndValidity(); - this.activityForm.controls['name'].setValidators(Validators.required); - this.activityForm.controls['name'].updateValueAndValidity(); - this.activityForm.controls['quantity'].setValue(1); - this.activityForm.controls['searchSymbol'].removeValidators( - Validators.required - ); - this.activityForm.controls['searchSymbol'].updateValueAndValidity(); - } else { - this.activityForm.controls['accountId'].setValidators( - Validators.required - ); - this.activityForm.controls['accountId'].updateValueAndValidity(); - this.activityForm.controls['dataSource'].setValidators( - Validators.required - ); - this.activityForm.controls['dataSource'].updateValueAndValidity(); - this.activityForm.controls['name'].removeValidators( - Validators.required - ); - this.activityForm.controls['name'].updateValueAndValidity(); - this.activityForm.controls['searchSymbol'].setValidators( - Validators.required - ); - this.activityForm.controls['searchSymbol'].updateValueAndValidity(); - } - }); + this.activityForm.controls['type'].valueChanges + .pipe(takeUntil(this.unsubscribeSubject)) + .subscribe((type: Type) => { + if (type === 'ITEM') { + this.activityForm.controls['accountId'].removeValidators( + Validators.required + ); + this.activityForm.controls['accountId'].updateValueAndValidity(); + this.activityForm.controls['currency'].setValue( + this.data.user.settings.baseCurrency + ); + this.activityForm.controls['dataSource'].removeValidators( + Validators.required + ); + this.activityForm.controls['dataSource'].updateValueAndValidity(); + this.activityForm.controls['name'].setValidators(Validators.required); + this.activityForm.controls['name'].updateValueAndValidity(); + this.activityForm.controls['quantity'].setValue(1); + this.activityForm.controls['searchSymbol'].removeValidators( + Validators.required + ); + this.activityForm.controls['searchSymbol'].updateValueAndValidity(); + } else { + this.activityForm.controls['accountId'].setValidators( + Validators.required + ); + this.activityForm.controls['accountId'].updateValueAndValidity(); + this.activityForm.controls['dataSource'].setValidators( + Validators.required + ); + this.activityForm.controls['dataSource'].updateValueAndValidity(); + this.activityForm.controls['name'].removeValidators( + Validators.required + ); + this.activityForm.controls['name'].updateValueAndValidity(); + this.activityForm.controls['searchSymbol'].setValidators( + Validators.required + ); + this.activityForm.controls['searchSymbol'].updateValueAndValidity(); + } + }); this.activityForm.controls['type'].setValue(this.data.activity?.type); diff --git a/apps/client/src/app/pages/portfolio/transactions/create-or-update-transaction-dialog/create-or-update-transaction-dialog.html b/apps/client/src/app/pages/portfolio/transactions/create-or-update-transaction-dialog/create-or-update-transaction-dialog.html index 212b3c245..3ce720b37 100644 --- a/apps/client/src/app/pages/portfolio/transactions/create-or-update-transaction-dialog/create-or-update-transaction-dialog.html +++ b/apps/client/src/app/pages/portfolio/transactions/create-or-update-transaction-dialog/create-or-update-transaction-dialog.html @@ -138,9 +138,9 @@