Skip to content

Commit

Permalink
Merge branch 'master' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronk committed Feb 12, 2025
2 parents 068b0fb + 76b955e commit 0f387ee
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
39 changes: 39 additions & 0 deletions coldfront/plugins/ifx/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
'''
import logging
import re
import requests
import json
from collections import defaultdict, OrderedDict
from decimal import Decimal
from django.core.exceptions import MultipleObjectsReturned
Expand Down Expand Up @@ -852,3 +854,40 @@ def remove_billing_records(self, user, account_data):

for br in billing_records:
br.delete()

def recalculate_billing_records(self, user, account_data):
'''
Recalculate the billing records for the given facility, user, year, and month
'''
# Recreate the billing records by calling the application calculate-billing-month url with invoice_prefix, year, and month
# url = getIfxUrl(f'{self.facility.application_username.upper()}_CALCULATE_BILLING_MONTH')

# This needs to be http://localhost because of some networky funk that I don't understand
url = 'http://localhost/ifx/api/billing/calculate-billing-month/'
url = f'{url}{self.facility.invoice_prefix}/{self.year}/{self.month}/'
headers = {
'Authorization': self.auth_token_str,
'Content-Type': 'application/json',
}
data = self.get_recalculate_body(user, account_data)
response = requests.post(url, headers=headers, json=data, timeout=None)
response_data = None
try:
response_data = response.json()
logger.error(f'Recalculate billing records response_data: {response_data}')
except json.JSONDecodeError:
raise Exception(f'Unable to decode response from {url}: {response.text}')

if response.status_code != 200:
if response_data:
error_message = ','.join(str(k) for k in response_data.values())
else:
error_message = response.text
logger.error(f'Recalculate billing records error response: {response_data}')
raise Exception(f'Error recalculating billing records for {user.full_name} for {self.month}/{self.year}: {error_message}')

if response_data != 'OK' and response_data.get('errors', None):
error_message = ','.join(set(response_data['errors']))
raise Exception(f'Error recalculating billing records for {user.full_name} for {self.month}/{self.year}: {error_message}')

logger.error(f'Recalculate billing records response: {response_data}')
12 changes: 8 additions & 4 deletions coldfront/plugins/ifx/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,16 @@ def rebalance(request):
return Response(data={ 'error': 'invoice_prefix is required' }, status=status.HTTP_400_BAD_REQUEST)
if not ifxid:
return Response(data={ 'error': 'ifxid is required' }, status=status.HTTP_400_BAD_REQUEST)
if not year:
return Response(data={ 'error': 'year is required' }, status=status.HTTP_400_BAD_REQUEST)
if not month:
return Response(data={ 'error': 'month is required' }, status=status.HTTP_400_BAD_REQUEST)
if not requestor_ifxid:
return Response(data={ 'error': 'requestor_ifxid is required' }, status=status.HTTP_400_BAD_REQUEST)
try:
year = int(year)
except ValueError:
return Response(data={ 'error': 'year must be an integer' }, status=status.HTTP_400_BAD_REQUEST)
try:
month = int(month)
except ValueError:
return Response(data={ 'error': 'month must be an integer' }, status=status.HTTP_400_BAD_REQUEST)


try:
Expand Down
2 changes: 1 addition & 1 deletion ifxbilling

0 comments on commit 0f387ee

Please sign in to comment.