import { ViewFlags } from '@angular/compiler/src/core'; import { Component, Input, OnInit, OnChanges, ViewChild } from '@angular/core'; import { MatButton } from '@angular/material/button'; import { MatExpansionPanel } from '@angular/material/expansion'; import { MatTableDataSource } from '@angular/material/table'; import { AccountEntryCategoryService, AccountEntryService, AccountService } from '../data-object-service'; import { Account, AccountEntry, AccountEntryCategory, NULL_AccountEntry } from '../data-objects'; import { ExtApiService } from '../ext-data-object-service'; import { Saldo } from '../ext-data-objects'; import { MessageService } from '../message.service'; interface DN_AccountEntry { rawAccountEntry: AccountEntry accountEntryCategory: string overheadRelevant: boolean } @Component({ selector: 'app-account', templateUrl: './account.component.html', styleUrls: ['./account.component.css'] }) export class AccountComponent implements OnInit { @Input() selectedAccountId: number @Input() shallBeRentPayment: boolean @ViewChild('addAccountEntryButton') addAccountEntryButton: MatButton account: Account accountEntries: DN_AccountEntry[] accountEntriesDataSource: MatTableDataSource accountEntriesDisplayedColumns: string[] = [ "description", "amount", "createdAt", "category", "overhead_relevant" ] saldo: Saldo accountEntryCategories: AccountEntryCategory[] accountEntryCategoriesMap: Map accountEntryCategoriesInverseMap: Map newAccountEntry: AccountEntry = NULL_AccountEntry constructor( private accountService: AccountService, private accountEntryService: AccountEntryService, private extApiService: ExtApiService, private accountEntryCategoryService: AccountEntryCategoryService, private messageService: MessageService ) { } async getAccount(): Promise { try { if (this.selectedAccountId) { this.messageService.add(`Trying to load account ${this.selectedAccountId} and entries`) this.account = await this.accountService.getAccount(this.selectedAccountId) this.messageService.add(`Account: ${JSON.stringify(this.account, undefined, 4)}`) this.getAccountEntries() } } catch (err) { this.messageService.add(`Error in getAccount: ${JSON.stringify(err, undefined, 4)}`) } } async getAccountEntries(): Promise { try { const rawAccountEntries = await this.accountEntryService.getAccountEntrysByAccount(this.selectedAccountId) rawAccountEntries.reverse() this.messageService.add(`AccountEntries: ${JSON.stringify(rawAccountEntries, undefined, 4)}`) this.accountEntries = [] for (let f of rawAccountEntries) { this.accountEntries.push({ rawAccountEntry: f, accountEntryCategory: this.accountEntryCategoriesMap.get(f.account_entry_category).description, overheadRelevant: this.accountEntryCategoriesMap.get(f.account_entry_category).overhead_relevant }) } this.accountEntriesDataSource = new MatTableDataSource(this.accountEntries) this.saldo = await this.extApiService.getAccountSaldo(this.selectedAccountId) } catch (err) { throw err this.messageService.add(`Error in getAccountEntries: ${JSON.stringify(err, undefined, 4)}`) } } async addAccountEntry(): Promise { try { this.addAccountEntryButton.disabled = true this.newAccountEntry.account = this.account.id this.messageService.add(`addAccountEntry: ${ JSON.stringify(this.newAccountEntry, undefined, 4) }`) this.newAccountEntry = await this.accountEntryService.postAccountEntry(this.newAccountEntry) this.messageService.add(`New accountEntry created: ${this.newAccountEntry.id}`) this.newAccountEntry = { 'account': this.account.id, 'amount': undefined, 'created_at': '', 'description': '', 'id': 0, 'account_entry_category': 0 } this.getAccountEntries() } catch (err) { this.messageService.add(`Error in addAccountEntry: ${JSON.stringify(err, undefined, 4)}`) } finally { this.addAccountEntryButton.disabled = false } } async getAccountEntryCategories(): Promise { try { this.accountEntryCategories = await this.accountEntryCategoryService.getAccountEntryCategorys() this.accountEntryCategoriesMap = new Map() this.accountEntryCategoriesInverseMap = new Map() for (let p of this.accountEntryCategories) { this.accountEntryCategoriesMap.set(p.id, p) this.accountEntryCategoriesInverseMap.set(p.description, p) } this.messageService.add(`getAccountEntryCategories: ${JSON.stringify(this.accountEntryCategories, undefined, 4)}`) } catch (err) { this.messageService.add(`Error in getAccountEntryCategories: ${JSON.stringify(err, undefined, 4)}`) } } private async init(): Promise { this.messageService.add(`AccountComponent.init, account: ${this.selectedAccountId}`) this.newAccountEntry = NULL_AccountEntry this.getAccount() await this.getAccountEntryCategories() if (this.shallBeRentPayment) { this.messageService.add('shall be rentpayment') this.newAccountEntry.account_entry_category = this.accountEntryCategoriesInverseMap.get('Mietzahlung').id } } ngOnInit(): void { this.init() } ngOnChanges(): void { this.init() } }