hv2-all-in-one/ui/hv2-ui/src/app/account/account.component.ts

161 lines
6.2 KiB
TypeScript
Raw Normal View History

2021-09-13 16:47:56 +02:00
import { ViewFlags } from '@angular/compiler/src/core';
2021-09-09 18:39:18 +02:00
import { Component, Input, OnInit, OnChanges, ViewChild } from '@angular/core';
2021-09-09 16:39:05 +02:00
import { MatButton } from '@angular/material/button';
2021-09-13 16:47:56 +02:00
import { MatExpansionPanel } from '@angular/material/expansion';
2021-09-09 15:49:49 +02:00
import { MatTableDataSource } from '@angular/material/table';
2021-09-10 11:59:08 +02:00
import { AccountEntryCategoryService, AccountEntryService, AccountService } from '../data-object-service';
import { Account, AccountEntry, AccountEntryCategory, NULL_AccountEntry } from '../data-objects';
2021-11-02 13:06:34 +01:00
import { ErrorDialogService } from '../error-dialog.service';
2021-09-09 18:00:24 +02:00
import { ExtApiService } from '../ext-data-object-service';
2021-11-08 21:04:21 +01:00
import { Saldo, UniqueNumber } from '../ext-data-objects';
2021-09-09 15:49:49 +02:00
import { MessageService } from '../message.service';
2021-09-10 11:59:08 +02:00
interface DN_AccountEntry {
rawAccountEntry: AccountEntry
accountEntryCategory: string
overheadRelevant: boolean
}
2021-09-09 15:49:49 +02:00
@Component({
selector: 'app-account',
templateUrl: './account.component.html',
styleUrls: ['./account.component.css']
})
export class AccountComponent implements OnInit {
@Input() selectedAccountId: number
2021-09-10 11:59:08 +02:00
@Input() shallBeRentPayment: boolean
2021-09-09 16:39:05 +02:00
@ViewChild('addAccountEntryButton') addAccountEntryButton: MatButton
2021-09-09 15:49:49 +02:00
account: Account
2021-09-10 11:59:08 +02:00
accountEntries: DN_AccountEntry[]
accountEntriesDataSource: MatTableDataSource<DN_AccountEntry>
2021-11-08 21:04:21 +01:00
accountEntriesDisplayedColumns: string[] = [ "description", "document_no", "amount", "createdAt", "category", "overhead_relevant" ]
2021-09-09 18:00:24 +02:00
saldo: Saldo
2021-09-10 11:59:08 +02:00
accountEntryCategories: AccountEntryCategory[]
accountEntryCategoriesMap: Map<number, AccountEntryCategory>
accountEntryCategoriesInverseMap: Map<string, AccountEntryCategory>
2021-09-09 15:49:49 +02:00
constructor(
private accountService: AccountService,
private accountEntryService: AccountEntryService,
2021-09-09 18:00:24 +02:00
private extApiService: ExtApiService,
2021-09-10 11:59:08 +02:00
private accountEntryCategoryService: AccountEntryCategoryService,
2021-11-02 13:06:34 +01:00
private messageService: MessageService,
private errorDialogService: ErrorDialogService
2021-09-09 15:49:49 +02:00
) { }
2021-09-13 16:47:56 +02:00
2021-09-09 16:39:05 +02:00
async getAccount(): Promise<void> {
2021-09-09 15:49:49 +02:00
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)}`)
2021-09-09 16:39:05 +02:00
this.getAccountEntries()
2021-09-09 15:49:49 +02:00
}
} catch (err) {
2021-09-09 16:39:05 +02:00
this.messageService.add(`Error in getAccount: ${JSON.stringify(err, undefined, 4)}`)
2021-09-09 15:49:49 +02:00
}
}
2021-09-09 16:39:05 +02:00
async getAccountEntries(): Promise<void> {
try {
2021-09-10 11:59:08 +02:00
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<DN_AccountEntry>(this.accountEntries)
2021-09-09 18:00:24 +02:00
this.saldo = await this.extApiService.getAccountSaldo(this.selectedAccountId)
2021-09-09 16:39:05 +02:00
} catch (err) {
2021-09-10 11:59:08 +02:00
throw err
2021-09-09 16:39:05 +02:00
this.messageService.add(`Error in getAccountEntries: ${JSON.stringify(err, undefined, 4)}`)
}
}
2021-09-09 15:49:49 +02:00
async addAccountEntry(formData: any): Promise<void> {
try {
this.addAccountEntryButton.disabled = true
this.messageService.add(`${JSON.stringify(formData.value, undefined, 4)}`)
2021-11-08 21:04:21 +01:00
let uniquenumber: UniqueNumber = await this.extApiService.getUniqueNumber();
this.messageService.add(`Got unique number as document_no: ${uniquenumber.number}`)
let newAccountEntry: AccountEntry = {
description: formData.value.description,
account: this.account.id,
created_at: formData.value.createdAt,
amount: formData.value.amount,
id: 0,
2021-11-08 21:04:21 +01:00
document_no: uniquenumber.number,
account_entry_category: 0
2021-09-09 16:39:05 +02:00
}
if (this.shallBeRentPayment) {
newAccountEntry.account_entry_category = this.accountEntryCategoriesInverseMap.get('Mietzahlung').id
this.messageService.add(`shall be rentpayment, category is ${newAccountEntry.account_entry_category}`)
} else {
newAccountEntry.account_entry_category = formData.value.category
this.messageService.add(`category is ${newAccountEntry.account_entry_category}`)
}
this.messageService.add(`addAccountEntry: ${ JSON.stringify(newAccountEntry, undefined, 4) }`)
newAccountEntry = await this.accountEntryService.postAccountEntry(newAccountEntry)
this.messageService.add(`New accountEntry created: ${newAccountEntry.id}`)
2021-11-02 13:06:34 +01:00
formData.reset()
this.getAccountEntries()
} catch (err) {
this.messageService.add(`Error in addAccountEntry: ${JSON.stringify(err, undefined, 4)}`)
2021-11-02 13:06:34 +01:00
this.errorDialogService.openDialog('AccountComponent', 'addAccountEntry', JSON.stringify(err, undefined, 4))
} finally {
this.addAccountEntryButton.disabled = false
}
2021-09-09 15:49:49 +02:00
}
2021-09-10 11:59:08 +02:00
async getAccountEntryCategories(): Promise<void> {
try {
this.accountEntryCategories = await this.accountEntryCategoryService.getAccountEntryCategorys()
this.accountEntryCategoriesMap = new Map<number, AccountEntryCategory>()
this.accountEntryCategoriesInverseMap = new Map<string, AccountEntryCategory>()
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<void> {
this.messageService.add(`AccountComponent.init, account: ${this.selectedAccountId}`)
2021-09-09 16:39:05 +02:00
this.getAccount()
2021-09-10 11:59:08 +02:00
await this.getAccountEntryCategories()
}
ngOnInit(): void {
this.init()
2021-09-09 15:49:49 +02:00
}
2021-09-09 18:39:18 +02:00
ngOnChanges(): void {
2021-09-10 11:59:08 +02:00
this.init()
2021-09-09 18:39:18 +02:00
}
2021-10-31 23:06:02 +01:00
2021-09-09 15:49:49 +02:00
}