Files
hv2-all-in-one/ui/hv2-ui/src/app/tenant-details/tenant-details.component.ts

303 lines
10 KiB
TypeScript

import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { AccountService, CommercialPremiseService, FeeService, FlatService, ParkingService, PremiseService, TenancyFeeMappingService, TenancyService, TenantService } from '../data-object-service';
import { Account, CommercialPremise, Fee, Flat, NULL_Account, NULL_CommercialPremise, NULL_Flat, NULL_Parking, NULL_Tenancy, NULL_Tenant, Parking, Premise, Tenancy, TenancyFeeMapping, Tenant } from '../data-objects';
import { MessageService } from '../message.service';
import { MatButton } from '@angular/material/button';
import { MatTableDataSource } from '@angular/material/table';
import { ExtApiService } from '../ext-data-object-service';
import { MatExpansionPanel } from '@angular/material/expansion';
interface DN_Tenancy {
rawTenancy: Tenancy
flat: string
parking: string
commercialPremise: string
}
interface DN_Flat {
rawFlat: Flat
premise: string
}
interface DN_Parking {
rawParking: Parking
premise: string
}
interface DN_CommercialPremise {
rawCommercialPremise: CommercialPremise
premise: string
}
@Component({
selector: 'app-tenant-details',
templateUrl: './tenant-details.component.html',
styleUrls: ['./tenant-details.component.css']
})
export class TenantDetailsComponent implements OnInit {
tenant: Tenant = NULL_Tenant
tenantId: number
account: Account = NULL_Account
premisesMap: Map<number, Premise>
tenancies: DN_Tenancy[]
tenancyDataSource: MatTableDataSource<DN_Tenancy>
tenancyDisplayColumns: string[] = [ "description", "flat", "parking", "commercial_premise", "startdate", "enddate" ]
collapseTenantDetails: boolean = false
collapseTenancies: boolean = false
collapseTenancyMapping: boolean = false
collapseAccount: boolean = false
selectedTenancy: Tenancy = undefined
mappedFees: Fee[]
mappedFeesDataSource: MatTableDataSource<Fee>
mappedFeesDisplayedColumns: string[] = [ "description", "amount", "fee_type", "startdate", "enddate" ]
allFees: Fee[]
selectedFee: number
newTenancy: Tenancy = NULL_Tenancy
allFlats: DN_Flat[]
allParkings: DN_Parking[]
allCommercialPremises: DN_CommercialPremise[]
@ViewChild('submitButton') submitButton: MatButton
@ViewChild('mapFeeButton') mapFeeButton: MatButton
@ViewChild('panelTenancies') panelTenancies: MatExpansionPanel
@ViewChild('panelAddMapping') panelAddMapping: MatExpansionPanel
constructor(
private tenantService: TenantService,
private accountService: AccountService,
private tenancyService: TenancyService,
private flatService: FlatService,
private parkingService: ParkingService,
private commercialPremiseService: CommercialPremiseService,
private tenancyFeeMappingService: TenancyFeeMappingService,
private feeService: FeeService,
private extApiService: ExtApiService,
private premiseService: PremiseService,
private messageService: MessageService,
private route: ActivatedRoute,
private router: Router
) { }
async getTenant(): Promise<void> {
try {
const id = +this.route.snapshot.paramMap.get('id')
this.messageService.add(`getTenant, id=${id}`)
if (id != 0) {
this.messageService.add("getTenant, not-0-branch")
this.tenantId = id
this.tenant = await this.tenantService.getTenant(id)
this.account = await this.accountService.getAccount(this.tenant.account)
this.getTenancies()
} else {
this.messageService.add("getTenant, 0-branch")
this.tenant = NULL_Tenant
this.account = NULL_Account
this.tenancies = []
}
} catch (err) {
this.messageService.add(JSON.stringify(err, undefined, 4))
}
}
async getTenancies(): Promise<void> {
try {
this.tenancies = []
await this.getPremises()
for (let t of await this.tenancyService.getTenancysByTenant(this.tenant.id)) {
const flat: Flat = (t.flat) ? await this.flatService.getFlat(t.flat) : NULL_Flat
const parking: Parking = (t.parking) ? await this.parkingService.getParking(t.parking) : NULL_Parking
const commercialPremise: CommercialPremise = (t.commercial_premise) ? await this.commercialPremiseService.getCommercialPremise(t.commercial_premise) : NULL_CommercialPremise
this.tenancies.push({
rawTenancy: t,
flat: (flat != NULL_Flat) ? `${flat.description} (${this.premisesMap.get(flat.premise).description})` : '',
parking: (parking != NULL_Parking) ? `${parking.description} (${this.premisesMap.get(parking.premise).description})` : '',
commercialPremise: (commercialPremise != NULL_CommercialPremise) ? `${commercialPremise.description} (${this.premisesMap.get(commercialPremise.premise).description})` : ''
})
}
this.tenancyDataSource = new MatTableDataSource<DN_Tenancy>(this.tenancies)
} catch (err) {
this.messageService.add(JSON.stringify(err, undefined, 4))
}
}
async saveTenant(): Promise<void> {
try {
this.submitButton.disabled = true
this.messageService.add("saveTenant")
this.messageService.add(JSON.stringify(this.tenant, undefined, 4))
if (this.tenant.id == 0) {
this.messageService.add("about to insert new tenant")
this.account = {
"id": 0,
"description": `account_${this.tenant.firstname}_${this.tenant.lastname}`
}
this.account = await this.accountService.postAccount(this.account)
this.tenant.account = this.account.id
this.tenant = await this.tenantService.postTenant(this.tenant)
this.messageService.add(`Successfully added account with id ${this.account.id} and tenant with id ${this.tenant.id}`)
} else {
this.messageService.add("about to update existing tenant")
for (const k in this.tenant) {
this.tenant[k] = (this.tenant[k] == undefined) ? '' : this.tenant[k]
}
this.tenant = await this.tenantService.putTenant(this.tenant)
this.messageService.add(`Successfully changed tenant with id ${this.tenant.id}`)
}
this.router.navigate(['/tenants'])
} finally {
this.submitButton.disabled = false
}
}
async setSelectedTenancy(selectedTenancy: Tenancy): Promise<void> {
this.selectedTenancy = selectedTenancy
this.getMappedFees()
}
async getMappedFees(): Promise<void> {
this.mappedFees = await this.extApiService.getFeeByTenancies(this.selectedTenancy.id)
this.messageService.add(`setSelectedTenancy: mappedFees: ${JSON.stringify(this.mappedFees, undefined, 4)}`)
this.mappedFeesDataSource = new MatTableDataSource<Fee>(this.mappedFees)
this.messageService.add("mappedFeesDataSource set")
}
clearSelectedTenancy(): void {
this.selectedTenancy = undefined
}
async getFees(): Promise<void> {
try {
this.messageService.add("Trying to load fees")
this.allFees = await this.feeService.getFees()
this.messageService.add("fees loaded")
} catch (err) {
this.messageService.add(JSON.stringify(err, undefined, 4))
}
}
async getPremises(): Promise<void> {
if ((! this.premisesMap) || (this.premisesMap.size == 0)) {
try {
this.messageService.add("Trying to load premises")
const premises = await this.premiseService.getPremises()
this.premisesMap = new Map<number, Premise>()
for (let p of premises) {
this.premisesMap.set(p.id, p)
}
this.messageService.add("Premises loaded")
} catch (err) {
this.messageService.add(`Error in getPremises: ${ JSON.stringify(err, undefined, 4) }`)
}
} else {
this.messageService.add("Premises already loaded")
}
}
async addFee(): Promise<void> {
try {
this.mapFeeButton.disabled = true
this.messageService.add(`fee: ${ JSON.stringify(this.selectedFee, undefined, 4) }`)
let newMapping: TenancyFeeMapping = {
'tenancy': this.selectedTenancy.id,
'fee': this.selectedFee,
'id': 0
}
newMapping = await this.tenancyFeeMappingService.postTenancyFeeMapping(newMapping)
this.messageService.add(`New fee tenancy mapping created: ${newMapping.id}`)
this.selectedFee = undefined
this.getMappedFees()
} finally {
this.mapFeeButton.disabled = false
}
}
async openTenancyMapping(): Promise<void> {
this.messageService.add("TenancyMapping opened")
this.collapseTenancyMapping = true
await this.getPremises()
if (! this.allFlats) {
let flats = await this.flatService.getFlats()
this.allFlats = []
for (let p of flats) {
this.allFlats.push({
rawFlat: p,
premise: this.premisesMap.get(p.premise).description
})
}
}
if (! this.allParkings) {
let parkings = await this.parkingService.getParkings()
this.allParkings = []
for (let p of parkings) {
this.allParkings.push({
rawParking: p,
premise: this.premisesMap.get(p.premise).description
})
}
}
if (! this.allCommercialPremises) {
let commercialPremises = await this.commercialPremiseService.getCommercialPremises()
this.allCommercialPremises = []
for (let p of commercialPremises) {
this.allCommercialPremises.push({
rawCommercialPremise: p,
premise: this.premisesMap.get(p.premise).description
})
}
}
}
async closeTenancyMapping(): Promise<void> {
this.messageService.add("TenancyMapping closed")
this.collapseTenancyMapping = false
}
flatSelected(): void {
this.newTenancy.parking = null
this.newTenancy.commercial_premise = null
}
parkingSelected(): void {
this.newTenancy.flat = null
this.newTenancy.commercial_premise = null
}
commercialPremiseSelected(): void {
this.newTenancy.parking = null
this.newTenancy.flat = null
}
async addTenancyToTenant(): Promise<void> {
try {
this.messageService.add(`Going to mapping ${JSON.stringify(this.newTenancy, undefined, 4)}`)
this.newTenancy.tenant = this.tenant.id
this.messageService.add(`new tenancy is ${JSON.stringify(this.newTenancy, undefined, 4)}`)
this.newTenancy = await this.tenancyService.postTenancy(this.newTenancy)
this.messageService.add(`New tenancy added with id ${this.newTenancy.id}`)
this.panelAddMapping.close()
this.panelTenancies.open()
this.getTenancies()
} catch (err) {
this.messageService.add(`Error in addTenancyToTenant: ${ JSON.stringify(err, undefined, 4)}`)
}
}
ngOnInit(): void {
this.getTenant()
this.getFees()
}
}