diff --git a/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts b/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts index e79c508687..8bd33cf214 100644 --- a/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts +++ b/apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts @@ -267,14 +267,7 @@ export class GfCreateOrUpdateActivityDialogComponent { this.activityForm.get('currencyOfUnitPrice')?.setValue(currency); } - if (!['LIABILITY', 'VALUABLE'].includes(type)) { - if (accountId) { - this.activityForm.get('updateAccountBalance')?.enable(); - } else { - this.activityForm.get('updateAccountBalance')?.disable(); - this.activityForm.get('updateAccountBalance')?.setValue(false); - } - } + this.syncUpdateAccountBalanceControl(); }); this.activityForm @@ -298,15 +291,7 @@ export class GfCreateOrUpdateActivityDialogComponent { }); this.activityForm.get('date')?.valueChanges.subscribe(() => { - if ( - isToday(this.activityForm.get('date')?.value) && - this.activityForm.get('accountId')?.value - ) { - this.activityForm.get('updateAccountBalance')?.enable(); - } else { - this.activityForm.get('updateAccountBalance')?.disable(); - this.activityForm.get('updateAccountBalance')?.setValue(false); - } + this.syncUpdateAccountBalanceControl(); this.changeDetectorRef.markForCheck(); }); @@ -386,8 +371,6 @@ export class GfCreateOrUpdateActivityDialogComponent { .get('searchSymbol') ?.removeValidators(Validators.required); this.activityForm.get('searchSymbol')?.updateValueAndValidity(); - this.activityForm.get('updateAccountBalance')?.disable(); - this.activityForm.get('updateAccountBalance')?.setValue(false); } else if (['FEE', 'INTEREST', 'LIABILITY'].includes(type)) { const currency = this.data.accounts.find(({ id }) => { @@ -423,16 +406,6 @@ export class GfCreateOrUpdateActivityDialogComponent { if (type === 'FEE') { this.activityForm.get('unitPrice')?.setValue(0); } - - if ( - ['FEE', 'INTEREST'].includes(type) && - this.activityForm.get('accountId')?.value - ) { - this.activityForm.get('updateAccountBalance')?.enable(); - } else { - this.activityForm.get('updateAccountBalance')?.disable(); - this.activityForm.get('updateAccountBalance')?.setValue(false); - } } else { this.activityForm .get('dataSource') @@ -444,15 +417,10 @@ export class GfCreateOrUpdateActivityDialogComponent { .get('searchSymbol') ?.setValidators(Validators.required); this.activityForm.get('searchSymbol')?.updateValueAndValidity(); - - if (this.activityForm.get('accountId')?.value) { - this.activityForm.get('updateAccountBalance')?.enable(); - } else { - this.activityForm.get('updateAccountBalance')?.disable(); - this.activityForm.get('updateAccountBalance')?.setValue(false); - } } + this.syncUpdateAccountBalanceControl(); + this.changeDetectorRef.markForCheck(); }); @@ -567,6 +535,27 @@ export class GfCreateOrUpdateActivityDialogComponent { } } + private syncUpdateAccountBalanceControl() { + const accountBalanceControl = this.activityForm.get('updateAccountBalance'); + const accountId = this.activityForm.get('accountId')?.value; + const dataSource = this.activityForm.get('dataSource')?.value; + const date = this.activityForm.get('date')?.value; + const type = this.activityForm.get('type')?.value; + + const isEligible = + !!accountId && + isToday(date) && + !['LIABILITY', 'VALUABLE'].includes(type) && + !(dataSource === 'MANUAL' && type === 'BUY'); + + if (isEligible) { + accountBalanceControl?.enable(); + } else { + accountBalanceControl?.disable(); + accountBalanceControl?.setValue(false); + } + } + private updateAssetProfile() { this.isLoading = true; this.changeDetectorRef.markForCheck();