From 95f95cac7f80bce89fe3f7cdecbf2f7a71652d3b Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Thu, 20 Apr 2017 10:36:31 -0400 Subject: [PATCH 01/14] Create view for cash balance. --- blocnote/apps/financialInputs/views.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/blocnote/apps/financialInputs/views.py b/blocnote/apps/financialInputs/views.py index 37ca0fd..b94257b 100644 --- a/blocnote/apps/financialInputs/views.py +++ b/blocnote/apps/financialInputs/views.py @@ -517,3 +517,19 @@ class CustomerPreferenceView(View): instance['status'] = False return JsonResponse({'instance': instance}) + +class CashBalanceView(View): + model = CashBalance + + def get(self, request, building_id): + obj = get_model_object(self.model, building_id) + instance = {} + if obj: + instance['status'] = True + instance['date'] = obj.date + instance['is_from_balance_sheet'] = obj.is_from_balance_sheet + instance['balance_amount'] = obj.balance_amount + else: + instance['status'] = False + + return JsonResponse({'instance': instance}) -- GitLab From cf5d3f7637dade181eb8d3166f4706f7d9f13f36 Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Thu, 20 Apr 2017 10:38:05 -0400 Subject: [PATCH 02/14] Create model cash balance. --- blocnote/apps/financialInputs/models.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/blocnote/apps/financialInputs/models.py b/blocnote/apps/financialInputs/models.py index 139bfb3..e6c7f57 100644 --- a/blocnote/apps/financialInputs/models.py +++ b/blocnote/apps/financialInputs/models.py @@ -65,3 +65,10 @@ class CustomerPreference(models.Model): downpayment = models.DecimalField(max_digits=10, decimal_places=2) expected_payback = models.DecimalField(max_digits=3, decimal_places=0) expected_net_noi_dscr = models.DecimalField(max_digits=5, decimal_places=2) + + +class CashBalance(models.Model): + building_id = models.IntegerField() + date = models.DateField() + is_from_balance_sheet = models.BooleanField(default=False) + balance_amount = models.DecimalField(max_digits=10, decimal_places=2) -- GitLab From 8f8108d23b8c31d0e6956d264a19bcc609a8da5f Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Thu, 20 Apr 2017 10:43:40 -0400 Subject: [PATCH 03/14] Create cash balance table UI. Implement add row button. Save button present but doesn't do anything. --- .../static/financialInputs/scripts/app.js | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js index cfc0187..ed18bab 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js @@ -4,6 +4,7 @@ for (var utility_index in utilities) { } loadBillsOverview(); getCustomerPreferenceTable(); +getCashBalanceForm(); /** * Handle submition of the header form. Validate commissioning date is greater @@ -354,3 +355,67 @@ function getCustomerPreferenceTable() { createCustomerPreferenceTable(res.payload.instance); }); } + +function loadCashBalanceTable(instance) { + form = document.querySelector('#cash-balance-table'); + if(!instance.status) { + var date = ''; + var isFromBalanceSheet = false; + var balanceAmount = 0; + + var text = ` + + Balance Amount + Date + Balance Sheet + + + + + + + + + + ` + } + form.innerHTML = text; +} + +function getCashBalanceForm() { + request(`cash-balance/`, { + method: 'get', + credentials: 'same-origin', + headers: { + 'Content-Type': 'application/json' + }, + }).then(res => { + loadCashBalanceTable(res.payload.instance); + }); +} + +function addRow() { + form = document.querySelector('#cash-balance-table'); + text = ` + + + + + + `; + form.innerHTML += text; + return false; +} + +function submitCashBalanceForm(form) { + formData = new FormData(form); + return false; +} -- GitLab From b18108dfa8f67235803e01c353a3905b1856fa90 Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Fri, 21 Apr 2017 09:24:34 -0400 Subject: [PATCH 04/14] Add cash balance table component. Implement add row button. Implement submit. GET fetches the correct data but is-from-balance-sheet checkboxes are not set to match the value from backend. --- .../migrations/0013_cashbalance.py | 25 ++++ .../migrations/0014_auto_20170421_0004.py | 20 +++ blocnote/apps/financialInputs/models.py | 2 +- .../static/financialInputs/scripts/app.js | 139 +++++++++++++----- .../financialInputs/cashBalance.html | 23 +++ .../templates/financialInputs/index.html | 3 + blocnote/apps/financialInputs/urls.py | 1 + blocnote/apps/financialInputs/views.py | 60 +++++++- 8 files changed, 226 insertions(+), 47 deletions(-) create mode 100644 blocnote/apps/financialInputs/migrations/0013_cashbalance.py create mode 100644 blocnote/apps/financialInputs/migrations/0014_auto_20170421_0004.py create mode 100644 blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html diff --git a/blocnote/apps/financialInputs/migrations/0013_cashbalance.py b/blocnote/apps/financialInputs/migrations/0013_cashbalance.py new file mode 100644 index 0000000..7fe2952 --- /dev/null +++ b/blocnote/apps/financialInputs/migrations/0013_cashbalance.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.6 on 2017-04-20 15:12 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('financialInputs', '0012_customerpreference'), + ] + + operations = [ + migrations.CreateModel( + name='CashBalance', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('building_id', models.IntegerField()), + ('date', models.DateField()), + ('is_from_balance_sheet', models.BooleanField(default=False)), + ('balance_amount', models.DecimalField(decimal_places=2, max_digits=10)), + ], + ), + ] diff --git a/blocnote/apps/financialInputs/migrations/0014_auto_20170421_0004.py b/blocnote/apps/financialInputs/migrations/0014_auto_20170421_0004.py new file mode 100644 index 0000000..b737b51 --- /dev/null +++ b/blocnote/apps/financialInputs/migrations/0014_auto_20170421_0004.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.6 on 2017-04-21 00:04 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('financialInputs', '0013_cashbalance'), + ] + + operations = [ + migrations.RenameField( + model_name='cashbalance', + old_name='date', + new_name='start_date', + ), + ] diff --git a/blocnote/apps/financialInputs/models.py b/blocnote/apps/financialInputs/models.py index e6c7f57..65d77a5 100644 --- a/blocnote/apps/financialInputs/models.py +++ b/blocnote/apps/financialInputs/models.py @@ -69,6 +69,6 @@ class CustomerPreference(models.Model): class CashBalance(models.Model): building_id = models.IntegerField() - date = models.DateField() + start_date = models.DateField() is_from_balance_sheet = models.BooleanField(default=False) balance_amount = models.DecimalField(max_digits=10, decimal_places=2) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js index ed18bab..eb2580d 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js @@ -357,65 +357,126 @@ function getCustomerPreferenceTable() { } function loadCashBalanceTable(instance) { - form = document.querySelector('#cash-balance-table'); - if(!instance.status) { - var date = ''; - var isFromBalanceSheet = false; - var balanceAmount = 0; - - var text = ` - - Balance Amount - Date - Balance Sheet - - - - - - - - - - ` + table = document.querySelector('#cash-balance-table'); + if(instance.status) { + for(var i=0; i < instance.result.length; i++) { + date = instance.result[i].date.split('-'); + console.log(date); + addRow(Number(instance.result[i].balance_amount), Number(date[1]), Number(date[2]), Number(date[0])) + } + } + else { + addRow(0, 1, 1, 1980); } - form.innerHTML = text; } function getCashBalanceForm() { request(`cash-balance/`, { - method: 'get', + method: 'GET', credentials: 'same-origin', headers: { 'Content-Type': 'application/json' }, }).then(res => { + console.log(res); loadCashBalanceTable(res.payload.instance); }); } -function addRow() { - form = document.querySelector('#cash-balance-table'); - text = ` - - - - `; + return text; +} + +function addRow(balance, month, day, year) { + table = document.querySelector('#cash-balance-table'); + var rowCount = table.rows.length; + var row = table.insertRow(rowCount); + var cell = row.insertCell(0); + cell.innerHTML += `${rowCount}`; + cell = row.insertCell(1); + cell.innerHTML += ``; + cell = row.insertCell(2); + cell.innerHTML += `${createDateComponent(rowCount, month, day, year)}`; + cell = row.insertCell(3); + cell.innerHTML += getCheckBox(rowCount); return false; } function submitCashBalanceForm(form) { + console.log(form); formData = new FormData(form); + table = document.querySelector('#cash-balance-table'); + var rowCount = table.rows.length; + result = {}; + for (const [key, value] of formData.entries()) { + result[key] = value; + } + console.log(result); + result['no-of-rows'] = rowCount; + request('cash-balance/', { + method: 'PUT', + credentials: 'same-origin', + body: JSON.stringify(result), + headers: new Headers({ + 'Content-Type': 'application/json', + 'X-CSRFToken': Cookies.get('csrftoken') + }) + }).then(res => { + console.log(res); + }) return false; } + +function createDateComponent(num, mon, day, yr) { + months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; + selectMonth = ``; + selectDay = ``; + selectYear = ``; + var text = ` +
+ + + +
`; + + return text; +} diff --git a/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html b/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html new file mode 100644 index 0000000..b2799f6 --- /dev/null +++ b/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html @@ -0,0 +1,23 @@ +

+ Cash Balance +

+
+
+ +
+ + + + + + + + + +
#Balance AmountDateBalance Sheet
+
+
+
+ +
+
diff --git a/blocnote/apps/financialInputs/templates/financialInputs/index.html b/blocnote/apps/financialInputs/templates/financialInputs/index.html index 25a35b7..6a4a298 100644 --- a/blocnote/apps/financialInputs/templates/financialInputs/index.html +++ b/blocnote/apps/financialInputs/templates/financialInputs/index.html @@ -22,6 +22,9 @@
{% include "financialInputs/billsOverview.html" %}
+
+ {% include "financialInputs/cashBalance.html" %} +
{% include "financialInputs/customerPreference.html" %}
diff --git a/blocnote/apps/financialInputs/urls.py b/blocnote/apps/financialInputs/urls.py index 72b7fbd..e84aa1c 100644 --- a/blocnote/apps/financialInputs/urls.py +++ b/blocnote/apps/financialInputs/urls.py @@ -8,4 +8,5 @@ urlpatterns = [ url(r'^bills/$', views.BillsTable.as_view(), name='bills'), url(r'^bills-overview/$', views.BillsOverviewView.as_view(), name='bills_overview'), url(r'^customer-preference/$', views.CustomerPreferenceView.as_view(), name='customer_preference'), + url(r'^cash-balance/$', views.CashBalanceView.as_view(), name='cash_balance'), ] diff --git a/blocnote/apps/financialInputs/views.py b/blocnote/apps/financialInputs/views.py index b94257b..48a541d 100644 --- a/blocnote/apps/financialInputs/views.py +++ b/blocnote/apps/financialInputs/views.py @@ -4,7 +4,9 @@ from django.http import JsonResponse from django.db import connections from django.views import View -from .models import FinancingOverview, Bills, BillsOverview, CustomerPreference +from datetime import date + +from .models import FinancingOverview, Bills, BillsOverview, CustomerPreference, CashBalance from .forms import BlocNoteHeaderForm @@ -522,14 +524,58 @@ class CashBalanceView(View): model = CashBalance def get(self, request, building_id): - obj = get_model_object(self.model, building_id) + objs = self.model.objects.filter(building_id=building_id) + print(objs) instance = {} - if obj: + instance['result'] = [] + if objs: instance['status'] = True - instance['date'] = obj.date - instance['is_from_balance_sheet'] = obj.is_from_balance_sheet - instance['balance_amount'] = obj.balance_amount + for obj in objs: + print(obj) + temp = {} + temp['date'] = obj.start_date + temp['is_from_balance_sheet'] = obj.is_from_balance_sheet + temp['balance_amount'] = obj.balance_amount + instance['result'].append(temp) else: instance['status'] = False - + print(instance) return JsonResponse({'instance': instance}) + + def get_record_list(self, put): + result = [] + for i in range(1, put['no-of-rows']): + temp = {} + balance = 'balance-amount-'+str(i) + date_month = 'date-month-'+str(i) + date_day = 'date-day-'+str(i) + date_year = 'date-year-'+str(i) + is_from_balance_sheet = 'is-from-balance-sheet-'+str(i) + if is_from_balance_sheet in put: + temp['is_from_balance_sheet'] = True + else: + temp['is_from_balance_sheet'] = False + temp['balance'] = put[balance] + temp['month'] = int(put[date_month]) + temp['day'] = int(put[date_day]) + temp['year'] = int(put[date_year]) + result.append(temp) + return result + + def put(self, request, building_id): + put = json.loads(request.body.decode()) + print(put) + result = self.get_record_list(put) + self.model.objects.filter( + building_id=building_id, + ).delete() + for i in result: + self.model.objects.create( + building_id=building_id, + start_date=date(i['year'], i['month'], i['day']), + is_from_balance_sheet=i['is_from_balance_sheet'], + balance_amount=i['balance'] + ) + print(result) + return JsonResponse({'instance': result}) + -- GitLab From f5ed60d4ddbc075d4162acd5905f29bc41611e3a Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Tue, 25 Apr 2017 09:01:49 -0400 Subject: [PATCH 05/14] Set checkbox value if user already previously set. Default is false. --- .../static/financialInputs/scripts/app.js | 24 +++++++++++-------- .../financialInputs/cashBalance.html | 2 +- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js index eb2580d..8432597 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js @@ -362,11 +362,11 @@ function loadCashBalanceTable(instance) { for(var i=0; i < instance.result.length; i++) { date = instance.result[i].date.split('-'); console.log(date); - addRow(Number(instance.result[i].balance_amount), Number(date[1]), Number(date[2]), Number(date[0])) + addRow(Number(instance.result[i].balance_amount), Number(date[1]), Number(date[2]), Number(date[0]), instance.result[i]['is_from_balance_sheet']) } } else { - addRow(0, 1, 1, 1980); + addRow(0, 1, 1, 1980, false); } } @@ -383,17 +383,21 @@ function getCashBalanceForm() { }); } -function getCheckBox(rowCount) { +function getCheckBox(rowCount, isFromBalanceSheet) { + var check = ""; + if (isFromBalanceSheet) { + check = "checked"; + console.log(check); + } var text = ` - `; + + `; return text; } -function addRow(balance, month, day, year) { +function addRow(balance, month, day, year, isFromBalanceSheet) { table = document.querySelector('#cash-balance-table'); var rowCount = table.rows.length; var row = table.insertRow(rowCount); @@ -404,7 +408,7 @@ function addRow(balance, month, day, year) { cell = row.insertCell(2); cell.innerHTML += `${createDateComponent(rowCount, month, day, year)}`; cell = row.insertCell(3); - cell.innerHTML += getCheckBox(rowCount); + cell.innerHTML += getCheckBox(rowCount, isFromBalanceSheet); return false; } diff --git a/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html b/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html index b2799f6..1b6df22 100644 --- a/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html +++ b/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html @@ -17,7 +17,7 @@
-
+
-- GitLab From 888228a137eb55b68e921345e4d279eb9768bf27 Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Tue, 25 Apr 2017 09:05:54 -0400 Subject: [PATCH 06/14] Remove fetch lines. --- blocnote/apps/financialInputs/views.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/blocnote/apps/financialInputs/views.py b/blocnote/apps/financialInputs/views.py index 7b7c15b..aa8fa5e 100644 --- a/blocnote/apps/financialInputs/views.py +++ b/blocnote/apps/financialInputs/views.py @@ -4,15 +4,10 @@ from django.http import JsonResponse from django.db import connections from django.views import View -<<<<<<< HEAD from datetime import date - from .models import FinancingOverview, Bills, BillsOverview, CustomerPreference, CashBalance -======= from bpfin.utilbills.bill_backend_call import bill_prior_proj_rough_annual - from .models import FinancingOverview, Bills, BillsOverview, CustomerPreference, EstimationAlgorithm ->>>>>>> f7268d6d6b7a20d23cf34ca22c61c0033d5f90cf from .forms import BlocNoteHeaderForm -- GitLab From 73a2aaf8867eac7cff32ae8eacea16f90d2c9356 Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Tue, 25 Apr 2017 17:50:09 -0400 Subject: [PATCH 07/14] Create cash balance table. Load with values from database if available. If not, display empty table. Implement button to add new row. Implement delete row. Store records in database. --- .../static/financialInputs/scripts/app.js | 177 +++++++++++------- .../financialInputs/cashBalance.html | 3 +- blocnote/apps/financialInputs/views.py | 85 ++++++--- 3 files changed, 172 insertions(+), 93 deletions(-) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js index 038da3f..e4137ad 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js @@ -5,6 +5,7 @@ for (var utility_index in utilities) { loadBillsOverview(); getCustomerPreferenceTable(); getCashBalanceForm(); +var rowCounter = 0; /** * Handle submition of the header form. Validate commissioning date is greater @@ -325,11 +326,11 @@ function loadBillsOverview() { 'Content-Type': 'application/json' }, }).then(res => { - if(!res.err) { + if (!res.err) { const table = document.querySelector('#Energy-Bills-Overview'); var text = ``; text += createEstimateModelForm(); - if(res.payload.instance.present) { + if (res.payload.instance.present) { text += createBillsOverviewTable(res.payload.instance); } table.innerHTML = text; @@ -357,7 +358,7 @@ function billsOverviewFormCalculate(data) { 'X-CSRFToken': Cookies.get('csrftoken') }) }).then(res => { - if(res.payload.instance['err']) { + if (res.payload.instance['err']) { alert(res.payload.instance['msg']); return false; } @@ -425,12 +426,12 @@ function createCustomerPreferenceTable(instance) { var expectedNetNOIDSCR = instance['expected_net_noi_dscr'] } var text = ` - - - - - - + + + + + + @@ -450,9 +451,7 @@ function createCustomerPreferenceTable(instance) { customerPreferenceForm.innerHTML = text; } -/** - * Make HTTP GET request to obtain customer preference table data if present. - */ +/**Make HTTP GET request to obtain customer preference table data if present. */ function getCustomerPreferenceTable() { request(`customer-preference/`, { method: 'GET', @@ -465,20 +464,7 @@ function getCustomerPreferenceTable() { }); } -function loadCashBalanceTable(instance) { - table = document.querySelector('#cash-balance-table'); - if(instance.status) { - for(var i=0; i < instance.result.length; i++) { - date = instance.result[i].date.split('-'); - console.log(date); - addRow(Number(instance.result[i].balance_amount), Number(date[1]), Number(date[2]), Number(date[0]), instance.result[i]['is_from_balance_sheet']) - } - } - else { - addRow(0, 1, 1, 1980, false); - } -} - +/**Send HTTP GET request to obtain all cash balance records. */ function getCashBalanceForm() { request(`cash-balance/`, { method: 'GET', @@ -487,109 +473,174 @@ function getCashBalanceForm() { 'Content-Type': 'application/json' }, }).then(res => { - console.log(res); loadCashBalanceTable(res.payload.instance); }); } -function getCheckBox(rowCount, isFromBalanceSheet) { +/**Load cash balance table with initial values. Takes response from GET request to construct the table. */ +function loadCashBalanceTable(instance) { + table = document.querySelector('#cash-balance-table'); + if (instance.status) { + for (var record=0; record < instance.result.length; record++) { + balanceAmount = Number(instance.result[record].balance_amount); + date = instance.result[record].date.split('-'); + month = Number(date[1]); + day = Number(date[2]); + year = Number(date[0]); + isFromBalanceSheet = instance.result[record]['is_from_balance_sheet'] + addCashBalanceRow(balanceAmount, month, day, year, isFromBalanceSheet); + } + } + else { + addCashBalanceRow(0, 1, 1, 1980, false); + } +} + +/** + * Return check box to indicate if a record is from balance sheet or not. Takes a boolean as argument which + * is true if the record is from balance, else not. This function returns a checked checkbox if it is from balance + * sheet, else an unchecked checkbox. + */ +function getCashBalanceCheckBox(isFromBalanceSheet) { var check = ""; if (isFromBalanceSheet) { check = "checked"; - console.log(check); } var text = ` `; return text; } -function addRow(balance, month, day, year, isFromBalanceSheet) { +/**Delete a given row in the cash balance table. Takes in the row index as argument. */ +function deleteCashBalanceRow(row) { + table = document.querySelector('#cash-balance-table'); + table.deleteRow(row); + for (rowInd = 1; rowInd < table.rows.length; rowInd++) { + row = table.rows.item(rowInd).cells; + row.item(0).innerHTML = rowInd; + } + return false; +} + +/** + * Add a row to the cash balance table. Take balance amount, date and boolean that represents if a record is from + * balance sheet or not. + */ +function addCashBalanceRow(balance, month, day, year, isFromBalanceSheet) { + rowCounter += 1; table = document.querySelector('#cash-balance-table'); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var cell = row.insertCell(0); cell.innerHTML += `${rowCount}`; cell = row.insertCell(1); - cell.innerHTML += ``; + cell.innerHTML += ` + + `; cell = row.insertCell(2); - cell.innerHTML += ``; + cell.innerHTML += ` + ${createDateComponent(month, day, year)} + `; cell = row.insertCell(3); - cell.innerHTML += getCheckBox(rowCount, isFromBalanceSheet); + cell.innerHTML += getCashBalanceCheckBox(isFromBalanceSheet); + cell = row.insertCell(4) + cell.innerHTML += ` + + `; return false; } +/**Make PUT request with data from the cash balance table. */ function submitCashBalanceForm(form) { - console.log(form); - formData = new FormData(form); + var myResult = {} table = document.querySelector('#cash-balance-table'); var rowCount = table.rows.length; - result = {}; - for (const [key, value] of formData.entries()) { - result[key] = value; + for (rowInd = 1; rowInd < rowCount; rowInd++) { + balance = table.rows[rowInd].cells[1].children[0].value; + dateMonth = table.rows[rowInd].cells[2].children[0].value; + dateDay = table.rows[rowInd].cells[2].children[1].value; + dateYear = table.rows[rowInd].cells[2].children[2].value; + checkBox = table.rows[rowInd].querySelectorAll('input[type="checkbox"]:checked'); + myResult['balance-amount-' + String(rowInd)] = balance; + myResult['date-month-' + String(rowInd)] = dateMonth; + myResult['date-day-' + String(rowInd)] = dateDay; + myResult['date-year-' + String(rowInd)] = dateYear; + if (checkBox.length > 0) { + myResult['is-from-balance-sheet-' + String(rowInd)] = 'on'; + } } - console.log(result); - result['no-of-rows'] = rowCount; + myResult['no-of-rows'] = rowInd; request('cash-balance/', { method: 'PUT', credentials: 'same-origin', - body: JSON.stringify(result), + body: JSON.stringify(myResult), headers: new Headers({ 'Content-Type': 'application/json', 'X-CSRFToken': Cookies.get('csrftoken') }) - }).then(res => { - console.log(res); - }) + }); return false; } -function createDateComponent(num, mon, day, yr) { +/**Create the select date component.*/ +function createDateComponent(mon, day, yr) { months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; selectMonth = ``; selectDay = ``; selectYear = ``; var text = ` -
- + `; + for (var month=0; month<12; month++) { + if (mon-1 === month){ selectMonth = `selected` } else { selectMonth = ``; } - text += ``; + text += ` + + `; } - text += ` - + - + -
`; + text += ` + + `; return text; } diff --git a/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html b/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html index 1b6df22..c6975d9 100644 --- a/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html +++ b/blocnote/apps/financialInputs/templates/financialInputs/cashBalance.html @@ -12,12 +12,13 @@ +
PreferenceValue
PreferenceValue
Affordable Downpayment${createDateComponent(rowCount, month, day, year)}Balance Amount Date Balance SheetOption
-
+
diff --git a/blocnote/apps/financialInputs/views.py b/blocnote/apps/financialInputs/views.py index aa8fa5e..452012b 100644 --- a/blocnote/apps/financialInputs/views.py +++ b/blocnote/apps/financialInputs/views.py @@ -5,9 +5,10 @@ from django.db import connections from django.views import View from datetime import date -from .models import FinancingOverview, Bills, BillsOverview, CustomerPreference, CashBalance from bpfin.utilbills.bill_backend_call import bill_prior_proj_rough_annual -from .models import FinancingOverview, Bills, BillsOverview, CustomerPreference, EstimationAlgorithm +from bpfin.financials.cash_balance import cash_balance + +from .models import FinancingOverview, Bills, BillsOverview, CustomerPreference, EstimationAlgorithm, CashBalance from .forms import BlocNoteHeaderForm @@ -605,17 +606,25 @@ class CustomerPreferenceView(View): class CashBalanceView(View): + """Display, store and use cash balance data.""" + model = CashBalance def get(self, request, building_id): + """Handle HTTP GET request. + + Fetch cash balance data from database if present. If not, return false status. + + Args: + request: HTTP GET request. + building_id: id of the building. + """ objs = self.model.objects.filter(building_id=building_id) - print(objs) instance = {} instance['result'] = [] if objs: instance['status'] = True for obj in objs: - print(obj) temp = {} temp['date'] = obj.start_date temp['is_from_balance_sheet'] = obj.is_from_balance_sheet @@ -623,43 +632,61 @@ class CashBalanceView(View): instance['result'].append(temp) else: instance['status'] = False - print(instance) return JsonResponse({'instance': instance}) def get_record_list(self, put): - result = [] - for i in range(1, put['no-of-rows']): - temp = {} - balance = 'balance-amount-'+str(i) - date_month = 'date-month-'+str(i) - date_day = 'date-day-'+str(i) - date_year = 'date-year-'+str(i) - is_from_balance_sheet = 'is-from-balance-sheet-'+str(i) + """Retrieve records from PUT request. + + Retrieve all cash balance records from PUT request and return a list of records which is a dictionary here. + + Args: + put: Decoded put request body which is a dictionary. + + Returns: + result: List of dictionaries which are the cash balance records. + """ + record_list = [] + for row in range(1, put['no-of-rows']): + record = {} + balance = 'balance-amount-'+str(row) + date_month = 'date-month-'+str(row) + date_day = 'date-day-'+str(row) + date_year = 'date-year-'+str(row) + is_from_balance_sheet = 'is-from-balance-sheet-'+str(row) if is_from_balance_sheet in put: - temp['is_from_balance_sheet'] = True + record['is_from_balance_sheet'] = True else: - temp['is_from_balance_sheet'] = False - temp['balance'] = put[balance] - temp['month'] = int(put[date_month]) - temp['day'] = int(put[date_day]) - temp['year'] = int(put[date_year]) - result.append(temp) - return result + record['is_from_balance_sheet'] = False + record['balance'] = put[balance] + record['month'] = int(put[date_month]) + record['day'] = int(put[date_day]) + record['year'] = int(put[date_year]) + record_list.append(record) + return record_list def put(self, request, building_id): + """Handle HTTP PUT request. + + Retrieve the list of records from the request. Delete existing records and create new records. + + Args: + request: HTTP PUT request. + building_id: id of the building. + + Returns: + JsonResponse: An instance with OK. + """ put = json.loads(request.body.decode()) - print(put) - result = self.get_record_list(put) + record_list = self.get_record_list(put) self.model.objects.filter( building_id=building_id, ).delete() - for i in result: + for record in record_list: self.model.objects.create( building_id=building_id, - start_date=date(i['year'], i['month'], i['day']), - is_from_balance_sheet=i['is_from_balance_sheet'], - balance_amount=i['balance'] + start_date=date(record['year'], record['month'], record['day']), + is_from_balance_sheet=record['is_from_balance_sheet'], + balance_amount=record['balance'] ) - print(result) - return JsonResponse({'instance': result}) + return JsonResponse({'instance': 'OK'}) -- GitLab From a8d6e05cdebac268a283d0311a4fc397d2ae806f Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Tue, 25 Apr 2017 19:37:15 -0400 Subject: [PATCH 08/14] Update javascript and views to implement PUT in a better way. --- .../static/financialInputs/scripts/app.js | 19 ++++++++----------- blocnote/apps/financialInputs/views.py | 17 ++++++----------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js index e4137ad..72bffdf 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js @@ -555,24 +555,21 @@ function addCashBalanceRow(balance, month, day, year, isFromBalanceSheet) { /**Make PUT request with data from the cash balance table. */ function submitCashBalanceForm(form) { - var myResult = {} + var myResult = []; table = document.querySelector('#cash-balance-table'); var rowCount = table.rows.length; for (rowInd = 1; rowInd < rowCount; rowInd++) { - balance = table.rows[rowInd].cells[1].children[0].value; - dateMonth = table.rows[rowInd].cells[2].children[0].value; - dateDay = table.rows[rowInd].cells[2].children[1].value; - dateYear = table.rows[rowInd].cells[2].children[2].value; + record = {}; + record['balance'] = table.rows[rowInd].cells[1].children[0].value; + record['date-month'] = table.rows[rowInd].cells[2].children[0].value; + record['date-day'] = table.rows[rowInd].cells[2].children[1].value; + record['date-year'] = table.rows[rowInd].cells[2].children[2].value; checkBox = table.rows[rowInd].querySelectorAll('input[type="checkbox"]:checked'); - myResult['balance-amount-' + String(rowInd)] = balance; - myResult['date-month-' + String(rowInd)] = dateMonth; - myResult['date-day-' + String(rowInd)] = dateDay; - myResult['date-year-' + String(rowInd)] = dateYear; if (checkBox.length > 0) { - myResult['is-from-balance-sheet-' + String(rowInd)] = 'on'; + record['is-from-balance-sheet'] = 'on'; } + myResult.push(record); } - myResult['no-of-rows'] = rowInd; request('cash-balance/', { method: 'PUT', credentials: 'same-origin', diff --git a/blocnote/apps/financialInputs/views.py b/blocnote/apps/financialInputs/views.py index 452012b..9ac3403 100644 --- a/blocnote/apps/financialInputs/views.py +++ b/blocnote/apps/financialInputs/views.py @@ -646,21 +646,16 @@ class CashBalanceView(View): result: List of dictionaries which are the cash balance records. """ record_list = [] - for row in range(1, put['no-of-rows']): + for row in put: record = {} - balance = 'balance-amount-'+str(row) - date_month = 'date-month-'+str(row) - date_day = 'date-day-'+str(row) - date_year = 'date-year-'+str(row) - is_from_balance_sheet = 'is-from-balance-sheet-'+str(row) - if is_from_balance_sheet in put: + if 'is-from-balance-sheet' in row: record['is_from_balance_sheet'] = True else: record['is_from_balance_sheet'] = False - record['balance'] = put[balance] - record['month'] = int(put[date_month]) - record['day'] = int(put[date_day]) - record['year'] = int(put[date_year]) + record['balance'] = row['balance'] + record['month'] = int(row['date-month']) + record['day'] = int(row['date-day']) + record['year'] = int(row['date-year']) record_list.append(record) return record_list -- GitLab From 6730d9211f8365859828041b6b4dae85f28a7f3b Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Wed, 26 Apr 2017 13:56:15 -0400 Subject: [PATCH 09/14] Make indent changes in app. Add missing trailing comma. --- .../static/financialInputs/scripts/app.js | 34 +++++++++---------- blocnote/apps/financialInputs/views.py | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js index 72bffdf..c439327 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js @@ -558,13 +558,13 @@ function submitCashBalanceForm(form) { var myResult = []; table = document.querySelector('#cash-balance-table'); var rowCount = table.rows.length; - for (rowInd = 1; rowInd < rowCount; rowInd++) { + for (rowIndex = 1; rowIndex < rowCount; rowIndex++) { record = {}; - record['balance'] = table.rows[rowInd].cells[1].children[0].value; - record['date-month'] = table.rows[rowInd].cells[2].children[0].value; - record['date-day'] = table.rows[rowInd].cells[2].children[1].value; - record['date-year'] = table.rows[rowInd].cells[2].children[2].value; - checkBox = table.rows[rowInd].querySelectorAll('input[type="checkbox"]:checked'); + record['balance'] = table.rows[rowIndex].cells[1].children[0].value; + record['date-month'] = table.rows[rowIndex].cells[2].children[0].value; + record['date-day'] = table.rows[rowIndex].cells[2].children[1].value; + record['date-year'] = table.rows[rowIndex].cells[2].children[2].value; + checkBox = table.rows[rowIndex].querySelectorAll('input[type="checkbox"]:checked'); if (checkBox.length > 0) { record['is-from-balance-sheet'] = 'on'; } @@ -589,9 +589,9 @@ function createDateComponent(mon, day, yr) { selectDay = ``; selectYear = ``; var text = ` - + `; + for (var month = 0; month < 12; month++) { if (mon-1 === month){ selectMonth = `selected` } @@ -599,13 +599,13 @@ function createDateComponent(mon, day, yr) { selectMonth = ``; } text += ` - + `; } text += ` - - + - + + `; return text; diff --git a/blocnote/apps/financialInputs/views.py b/blocnote/apps/financialInputs/views.py index 9ac3403..cb55f7b 100644 --- a/blocnote/apps/financialInputs/views.py +++ b/blocnote/apps/financialInputs/views.py @@ -681,7 +681,7 @@ class CashBalanceView(View): building_id=building_id, start_date=date(record['year'], record['month'], record['day']), is_from_balance_sheet=record['is_from_balance_sheet'], - balance_amount=record['balance'] + balance_amount=record['balance'], ) return JsonResponse({'instance': 'OK'}) -- GitLab From 33f705bc9482726f67831a00877f752c0757e2ea Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Thu, 27 Apr 2017 12:22:54 -0400 Subject: [PATCH 10/14] Add comment to cash balance model. --- blocnote/apps/financialInputs/models.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/blocnote/apps/financialInputs/models.py b/blocnote/apps/financialInputs/models.py index 2881652..29a9de4 100644 --- a/blocnote/apps/financialInputs/models.py +++ b/blocnote/apps/financialInputs/models.py @@ -74,6 +74,11 @@ class CustomerPreference(models.Model): class CashBalance(models.Model): + """Store Balance amount information. + + Store balance amount information. Flag for amount coming from balance sheet or bank statement. + """ + building_id = models.IntegerField() start_date = models.DateField() is_from_balance_sheet = models.BooleanField(default=False) -- GitLab From d08a6a87f7aba5ba970bf08f71caf79f8eaddea6 Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Thu, 27 Apr 2017 12:26:26 -0400 Subject: [PATCH 11/14] Add comment for variable rowCounter. --- .../financialInputs/static/financialInputs/scripts/app.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js index c439327..10970e5 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js @@ -5,6 +5,11 @@ for (var utility_index in utilities) { loadBillsOverview(); getCustomerPreferenceTable(); getCashBalanceForm(); +/**rowCounter is used to uniquely identify the row of the cash balance table. This does NOT represent the + * actual row number. It represents the total number of rows added. This does NOT get decremented on deleting + * a row. This is so that each new row has a new number in its id so that information can be obtained and sent + * to backend. + */ var rowCounter = 0; /** -- GitLab From 324adccbae81c3480c16145dad6e3774dd034991 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Thu, 27 Apr 2017 14:32:54 -0400 Subject: [PATCH 12/14] Fix migration by merging two leaf nodes in financialInputs migrations. --- .../migrations/0016_merge_20170427_1611.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 blocnote/apps/financialInputs/migrations/0016_merge_20170427_1611.py diff --git a/blocnote/apps/financialInputs/migrations/0016_merge_20170427_1611.py b/blocnote/apps/financialInputs/migrations/0016_merge_20170427_1611.py new file mode 100644 index 0000000..ef9d476 --- /dev/null +++ b/blocnote/apps/financialInputs/migrations/0016_merge_20170427_1611.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.6 on 2017-04-27 16:11 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('financialInputs', '0015_auto_20170424_2250'), + ('financialInputs', '0014_auto_20170421_0004'), + ] + + operations = [ + ] -- GitLab From da4c6d35b306ebbe3eb8c17ccf47b0ff6dc691ea Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Thu, 27 Apr 2017 14:54:36 -0400 Subject: [PATCH 13/14] Change row counter naming to identify its functionality and increment counter at end of adding a row. --- .../static/financialInputs/scripts/app.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js index 10970e5..594149f 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js @@ -5,12 +5,12 @@ for (var utility_index in utilities) { loadBillsOverview(); getCustomerPreferenceTable(); getCashBalanceForm(); -/**rowCounter is used to uniquely identify the row of the cash balance table. This does NOT represent the +/**nextCashBalanceRowID is used to uniquely identify the row of the cash balance table. This does NOT represent the * actual row number. It represents the total number of rows added. This does NOT get decremented on deleting * a row. This is so that each new row has a new number in its id so that information can be obtained and sent * to backend. */ -var rowCounter = 0; +var nextCashBalanceRowID = 0; /** * Handle submition of the header form. Validate commissioning date is greater @@ -513,7 +513,7 @@ function getCashBalanceCheckBox(isFromBalanceSheet) { } var text = ` `; return text; @@ -535,7 +535,6 @@ function deleteCashBalanceRow(row) { * balance sheet or not. */ function addCashBalanceRow(balance, month, day, year, isFromBalanceSheet) { - rowCounter += 1; table = document.querySelector('#cash-balance-table'); var rowCount = table.rows.length; var row = table.insertRow(rowCount); @@ -543,7 +542,7 @@ function addCashBalanceRow(balance, month, day, year, isFromBalanceSheet) { cell.innerHTML += `${rowCount}`; cell = row.insertCell(1); cell.innerHTML += ` - + `; cell = row.insertCell(2); cell.innerHTML += ` @@ -555,6 +554,7 @@ function addCashBalanceRow(balance, month, day, year, isFromBalanceSheet) { cell.innerHTML += ` `; + nextCashBalanceRowID += 1; return false; } @@ -594,7 +594,7 @@ function createDateComponent(mon, day, yr) { selectDay = ``; selectYear = ``; var text = ` - `; for (var month = 0; month < 12; month++) { if (mon-1 === month){ @@ -610,7 +610,7 @@ function createDateComponent(mon, day, yr) { text += ` - `; for (var d=0; d<31; d++) { @@ -626,7 +626,7 @@ function createDateComponent(mon, day, yr) { } text += ` - `; for (var year=1980; year <= 2080; year++) { -- GitLab From 53cb885b103b4eec8cdcad5172647698039df408 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Thu, 27 Apr 2017 15:01:52 -0400 Subject: [PATCH 14/14] Change balance sheet field name and add migration. --- .../migrations/0017_auto_20170427_1900.py | 20 +++++++++++++++++++ blocnote/apps/financialInputs/models.py | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 blocnote/apps/financialInputs/migrations/0017_auto_20170427_1900.py diff --git a/blocnote/apps/financialInputs/migrations/0017_auto_20170427_1900.py b/blocnote/apps/financialInputs/migrations/0017_auto_20170427_1900.py new file mode 100644 index 0000000..a014e1d --- /dev/null +++ b/blocnote/apps/financialInputs/migrations/0017_auto_20170427_1900.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.6 on 2017-04-27 19:00 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('financialInputs', '0016_merge_20170427_1611'), + ] + + operations = [ + migrations.RenameField( + model_name='cashbalance', + old_name='start_date', + new_name='statement_date', + ), + ] diff --git a/blocnote/apps/financialInputs/models.py b/blocnote/apps/financialInputs/models.py index 29a9de4..8de7ff4 100644 --- a/blocnote/apps/financialInputs/models.py +++ b/blocnote/apps/financialInputs/models.py @@ -80,6 +80,6 @@ class CashBalance(models.Model): """ building_id = models.IntegerField() - start_date = models.DateField() + statement_date = models.DateField() is_from_balance_sheet = models.BooleanField(default=False) balance_amount = models.DecimalField(max_digits=10, decimal_places=2) -- GitLab