106 lines
5.4 KiB
Python
106 lines
5.4 KiB
Python
from db import dbGetMany, dbGetOne
|
|
from loguru import logger
|
|
from decimal import Decimal
|
|
|
|
|
|
def perform(dbh, params):
|
|
createdAt = params['created_at']
|
|
|
|
tenants = dbGetMany(dbh, { "statement": "SELECT * FROM tenant_t", "params": () })
|
|
for tenant in tenants:
|
|
logger.info(f"Tenant: {tenant['firstname']} {tenant['lastname']}")
|
|
|
|
# check tenancies
|
|
tenancies = dbGetMany(dbh, {
|
|
"statement": "SELECT * FROM tenancy_t WHERE tenant = %s AND startdate < now() AND (enddate > now() or enddate is null)",
|
|
"params": (tenant['id'], )
|
|
}
|
|
)
|
|
requests = []
|
|
for tenancy in tenancies:
|
|
fee = dbGetOne(dbh, {
|
|
"statement": """
|
|
SELECT f.amount, f.fee_type
|
|
FROM fee_t f, tenancy_fee_mapping_t t
|
|
WHERE t.tenancy = %s AND
|
|
f.id = t.fee AND
|
|
f.startdate < now() AND
|
|
(f.enddate > now() OR f.enddate is null)
|
|
""",
|
|
"params": (tenancy['id'], )
|
|
}
|
|
)
|
|
if (tenancy['flat']):
|
|
logger.debug(f" Flat tenancy: {tenancy['id']}, Fee: {fee['amount']}, Fee_Type: {fee['fee_type']}")
|
|
flat = dbGetOne(dbh, { "statement": "SELECT area FROM flat_t WHERE id = %s", "params": (tenancy['flat'], ) })
|
|
logger.debug(f" Area: {flat['area']}")
|
|
if (fee['fee_type'] == 'per_area'):
|
|
feeRequest = flat['area'] * fee['amount']
|
|
else:
|
|
feeRequest = fee['amount']
|
|
feeRequest = feeRequest.quantize(Decimal('1.00'))
|
|
requests.append({
|
|
'description': f"Miete {tenancy['description']}",
|
|
'account': tenant['account'],
|
|
'created_at': createdAt,
|
|
'amount': feeRequest,
|
|
'category': 'Mietforderung'
|
|
})
|
|
overheadAdvance = dbGetOne(dbh, {
|
|
"statement": """
|
|
SELECT o.amount
|
|
FROM overhead_advance_t o, overhead_advance_flat_mapping_t m
|
|
WHERE m.flat = %s AND
|
|
o.id = m.overhead_advance AND
|
|
o.startdate < now() AND
|
|
(o.enddate > now() OR o.enddate is null)
|
|
""",
|
|
"params": (tenancy['flat'], )
|
|
}
|
|
)
|
|
overheadAdvanceRequest = flat['area'] * overheadAdvance['amount']
|
|
overheadAdvanceRequest = overheadAdvanceRequest.quantize(Decimal('1.00'))
|
|
requests.append({
|
|
'description': f"Betriebskosten {tenancy['description']}",
|
|
'account': tenant['account'],
|
|
'created_at': createdAt,
|
|
'amount': overheadAdvanceRequest,
|
|
'category': 'Betriebskostenforderung'
|
|
})
|
|
if (tenancy['parking']):
|
|
logger.debug(f" Garage tenancy: {tenancy['id']}, Fee: {fee['amount']}, Fee_Type: {fee['fee_type']}")
|
|
feeRequest = fee['amount']
|
|
feeRequest = feeRequest.quantize(Decimal('1.00'))
|
|
requests.append({
|
|
'description': f"Miete {tenancy['description']}",
|
|
'account': tenant['account'],
|
|
'created_at': createdAt,
|
|
'amount': feeRequest,
|
|
'category': 'Mietforderung'
|
|
})
|
|
if (tenancy['commercial_premise']):
|
|
logger.debug(f" Commercial premise tenancy: {tenancy['id']}, Fee: {fee['amount']}, Fee_Type: {fee['fee_type']}")
|
|
feeRequest = fee['amount']
|
|
feeRequest = feeRequest.quantize(Decimal('1.00'))
|
|
requests.append({
|
|
'description': f"Miete {tenancy['description']}",
|
|
'account': tenant['account'],
|
|
'created_at': createdAt,
|
|
'amount': feeRequest,
|
|
'category': 'Mietforderung'
|
|
})
|
|
|
|
for request in requests:
|
|
request['amount'] = Decimal('-1.0') * request['amount']
|
|
logger.info(f" {request['description']}, {request['account']}, {request['created_at']}, {request['amount']}, {request['category']}")
|
|
accountEntry = dbGetOne(dbh, {
|
|
"statement": """
|
|
INSERT INTO account_entry_t
|
|
(description, account, created_at, amount, account_entry_category)
|
|
VALUES (%s, %s, %s, %s, (SELECT id FROM account_entry_category_t WHERE description = %s))
|
|
RETURNING id
|
|
""",
|
|
"params": (request['description'], request['account'], request['created_at'], request['amount'], request['category'])
|
|
}
|
|
)
|
|
logger.info(f" account entry entered with id {accountEntry['id']}") |