diff --git a/blocnote/apps/financialInputs/old_views.py b/blocnote/apps/financialInputs/old_views.py index d8c232be8350e48c9cf7f34a6e11f67668f16a4b..66542078e409f981af7e45d8decce8ec3f314b65 100644 --- a/blocnote/apps/financialInputs/old_views.py +++ b/blocnote/apps/financialInputs/old_views.py @@ -44,23 +44,6 @@ def get_model_object(model, building_id): return 0 -def change_date_format(dates): - """Change date format. - - The CSV file contains date in MM/DD/YYYY format. The database needs it - to be in YYYY-MM-DD. This function performs this task. - - Args: - date: The date in MM/DD/YYYY format. - - Returns: - new_date: The date in YYYY-MM-DD format. - """ - date_split = dates.split('/') - new_date = date_split[2] + '-' + date_split[0] + '-' + date_split[1] - return new_date - - def get_raw_bill(building_id): """Fetch all the bills in the database. @@ -126,140 +109,6 @@ class Index(View): return render(request, 'financialInputs/index.html', context=context) -class BillsTable(View): - """Create the Energy Bills tables. - - Class to upload energy bills for each utility, store bills and send - the data to the frontend. - """ - - model = Bills - - def put(self, request, building_id): - """Handle HTTP PUT request. - - Obtain bill information from frontend as JSON. The data is in CSV. - The relevant data is stored in the database. Upon successfull upload, - the data is sent to the frontend to create the table. - - Args: - request: HTTP PUT request. - building_id: id of the building. - - Returns: - JsonResponse: JSON of the result which is a list containing - rows to be displayed. - """ - put = json.loads(request.body.decode()) - try: - result = self.handle_file(put, building_id) - except ValueError as err: - return JsonResponse({'error': err.args[0] + 'Please make sure you uploaded the right file.'}, status=400) - return JsonResponse({'result': result}) - - def handle_file(self, data, building_id): - """Handle file input. - - Take file data from frontend and obtain the indices of the relevant - columns. Go through the file and get the relevant data and store in the - database. - - Args: - data: The file data obtained from frontend. - building_id: id of the building. - - Returns: - result: List of rows to be sent to the frontend to display table. - """ - utility_type = data['utility_type'] - - self.model.objects.filter( - building_id=building_id, - utility_type=utility_type - ).delete() - - text = data['text'].split('\n') - date_from_index = "" - date_to_index = "" - usage_index = "" - charge_index = "" - column_headings = text[0].split(',') - for index, heading in enumerate(column_headings): - if heading == "Bill From Date": - date_from_index = index - elif heading == "Bill To Date": - date_to_index = index - elif heading == "Usage": - usage_index = index - elif "Total Charges" in heading: - charge_index = index - - if not date_from_index: - raise ValueError('Could not find "Bill From Date" column.') - if not date_to_index: - raise ValueError('Could not find "Bill To Date" column.') - if not usage_index: - raise ValueError('Could not find "Usage" column.') - if not charge_index: - raise ValueError('Could not find "Total Charge" column.') - - result = [] - for line in range(1, len(text)-1): - row_list = [] - line_array = text[line].split(',') - date_from = change_date_format(line_array[date_from_index]) - row_list.append(date_from) - date_to = change_date_format(line_array[date_to_index]) - row_list.append(date_to) - usage = line_array[usage_index] - row_list.append(usage) - charge = line_array[charge_index] - row_list.append(charge) - self.model.objects.create(building_id=building_id, - date_from=date_from, - date_to=date_to, - utility_type=data['utility_type'], - usage=usage, - charge=charge) - result.append(row_list) - - return result - - def get(self, request, building_id): - """Handle HTTP GET request. - - Fetch stored utility bills data and send to the frontend to display as - table. - - Args: - request: HTTP GET request. - building_id: id of the building. - - Return: - JsonResponse: JSON of the result which is a list of rows to populate - the table. - """ - result = [] - present = False - utility_type = request.GET.get('utility_type') - - model_obj = self.model.objects.filter( - building_id=building_id, - utility_type=utility_type - ) - if model_obj: - present = True - for row in model_obj: - row_list = [] - row_list.append(row.date_from) - row_list.append(row.date_to) - row_list.append(row.usage) - row_list.append(row.charge) - result.append(row_list) - - return JsonResponse({'result': result, 'present': present}) - - def get_if_user_input(building_id): """Check if utility charge input was user or from bills. diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js index 51c74d7167d3ca8d3d8f0ec304ab21e9f0328f9a..c0653d4092a9879502c46b831438f7019896300f 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js @@ -1,13 +1,9 @@ -const utilities = ['electricity', 'gas', 'oil', 'water']; var today = new Date(); const todaysDate = { 'day': today.getDate(), 'month': today.getMonth()+1, 'year': today.getFullYear(), } -for (var utility_index in utilities) { - loadInitialBillsTable(utilities[utility_index]); -} loadBillsOverview(); getIncomeStatementTable(); @@ -37,111 +33,6 @@ function validateDate(startDate, endDate) { return true; } -/** - * Load initial bills table given a utility. Send GET request to obtain - * bill information and call updateTable function to update the bills table - * with the data obtained from backend. - */ -function loadInitialBillsTable(utility) { - const table = document.querySelector('#'+utility); - - request(`bills/?utility_type=${utility}`, { - method: 'GET', - credentials: 'same-origin', - headers: { - 'Content-Type': 'application/json' - }, - }).then(res => { - if (res.payload.present) { - var text = getText(res.payload.result, utility); - updateTable(utility, text); - } - }) -} - -/** - * Update the Bills table with the data. Function queries to get the table - * component and then updates the component with the text. - */ -function updateTable(utility, text) { - table = document.querySelector('#'+utility); - table.innerHTML = text; -} - -/** Generate html text for a given utility and bills data from backend. */ -function getText(result, utility) { - units = {}; - units['electricity'] = `kWh`; - units['gas'] = `mmBTU`; - units['oil'] = `mmBTU`; - units['water'] = `Gallons`; - var text = `
| Date From | -Date To | -Usage (${units[utility]}) | -Bill Charge ($) | -
|---|