diff --git a/blocnote/apps/financialInputs/forms.py b/blocnote/apps/financialInputs/forms.py deleted file mode 100644 index 64f7084670ac2d08b4983f102bd429aa2592239e..0000000000000000000000000000000000000000 --- a/blocnote/apps/financialInputs/forms.py +++ /dev/null @@ -1,51 +0,0 @@ -import datetime - -from django.forms import ModelForm -from django import forms -from django.forms.extras import SelectDateWidget - -from .models import FinancingOverview - - -class BlocNoteHeaderForm(ModelForm): - """Header Form. - - Form that appears at the top of the page. This class specifies the fields - and sets the form field types for dates, sets the range of dates to display - and adds attributes to some of the fields. - """ - - years_to_display = range(datetime.datetime.now().year - 15, - datetime.datetime.now().year + 50) - anticipated_construction_start_date = forms.DateField( - widget=SelectDateWidget(years=years_to_display) - ) - anticipated_commissioning_date = forms.DateField( - widget=SelectDateWidget(years=years_to_display) - ) - pro_forma_start_date = forms.DateField( - widget=SelectDateWidget(years=years_to_display) - ) - analysis_date = forms.DateField( - widget=SelectDateWidget(years=years_to_display) - ) - - class Meta: - model = FinancingOverview - fields = ('anticipated_construction_start_date', - 'anticipated_commissioning_date', - 'anticipated_construction_period', - 'pro_forma_start_date', - 'pro_forma_duration', - 'analysis_date', - 'fund') - - def __init__(self, *args, **kwargs): - super(BlocNoteHeaderForm, self).__init__(*args, **kwargs) - for field in iter(self.fields): - self.fields[field].widget.attrs.update({ - 'class': 'form-control' - }) - self.fields['fund'].widget.attrs.update({ - 'class': 'custom-select' - }) diff --git a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js index 2b2d615b3870f68ad10ad915e9b3170329c73a01..cfea40d83a14dd0507bfd42cf5e8d5044c991fe1 100644 --- a/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js +++ b/blocnote/apps/financialInputs/static/financialInputs/scripts/app.js @@ -8,6 +8,7 @@ const todaysDate = { for (var utility_index in utilities) { loadInitialBillsTable(utilities[utility_index]); } +getBillProjectionDatesForm(); loadBillsOverview(); getIncomeStatementTable(); getCustomerPreferenceTable(); @@ -15,11 +16,23 @@ getLiabilitiesTable(); getCashBalanceForm(); getLoanOptionsTable(); +/** + * proformaForm will watch the form and if it changes, it will remove the response message is it was displayed. + */ +var proformaForm = document.querySelector('#pro-forma-form'); +proformaForm.onchange = function() { + var resMsg = document.querySelector('#pro-forma-form-save-msg'); + resMsg.innerHTML = ``; +} + /** * The following 2 functions display a warning message saying if fund is changed, it will affect the loan options. * This message is displayed when the mouse is over the fund select box. */ var fund = document.querySelector('#id_fund'); +/** + * This is to watch if fund value is changed. If it did, it would affect loan options. + */ var didFundChange = false; fund.onmouseenter = function() { var errorDiv = document.querySelector('#show-error'); @@ -35,6 +48,37 @@ fund.onchange = function() { didFundChange = true; } +/** + * HTTP GET request to fetch the proforma form information from the database if preent. It would receive the list of + * funds along with. + */ +function getBillProjectionDatesForm() { + request(`finance-overview`, { + method: 'GET', + credentials: 'same-origin', + headers: { + 'Content-Type': 'application/json' + }, + }).then(res => { + fundDropBox = document.querySelector('#id_fund-select'); + var funds = res.payload.result.funds; + for (var fundIndex = 0; fundIndex < funds.length; fundIndex++) { + fundDropBox.innerHTML += ` + + `; + if (res.payload.result.present) { + document.querySelector('#pro-forma-date-input').value = res.payload.result.pro_forma_start_date; + document.querySelector('#pro-forma-duration-input').value = res.payload.result.pro_forma_duration; + document.querySelector('#analysis-date-input').value = res.payload.result.analysis_date; + document.querySelector('#id_fund-select').value = res.payload.result.fund; + document.querySelector('#anticipated-construction-start-date-input').value = res.payload.result.anticipated_construction_start_date; + document.querySelector('#anticipated-commissioning-date-input').value = res.payload.result.anticipated_commissioning_date; + document.querySelector('#anticipated-construction-period-input').value = res.payload.result.anticipated_construction_period; + } + } + }); +} + /** * Handle submition of the header form. Validate commissioning date is greater * than construction start date. Create result dictionary containing the form @@ -43,31 +87,45 @@ fund.onchange = function() { * Bills overview table. */ function billProjectionDatesForm(form) { - const formData = new FormData(form); - var anticipatedConstructionStartYear = formData.get('anticipated_construction_start_date_year'); - var anticipatedConstructionStartMonth = formData.get('anticipated_construction_start_date_month'); - var anticipatedConstructionStartDay = formData.get('anticipated_construction_start_date_day'); - var anticipatedCommissioningStartYear = formData.get('anticipated_commissioning_date_year'); - var anticipatedCommissioningStartMonth = formData.get('anticipated_commissioning_date_month'); - var anticipatedCommissioningStartDay = formData.get('anticipated_commissioning_date_day'); - var anticipatedConstructionStarDate = { + var proFormaStartDate = document.querySelector('#pro-forma-date-input').value; + var proFormaDuration = document.querySelector('#pro-forma-duration-input').value; + var analysisDate = document.querySelector('#analysis-date-input').value; + var fund = document.querySelector('#id_fund-select').value; + var anticipatedConstructionStarDate = document.querySelector('#anticipated-construction-start-date-input').value; + var anticipatedCommissioningDate = document.querySelector('#anticipated-commissioning-date-input').value; + var anticipatedConstructionPeriod = document.querySelector('#anticipated-construction-period-input').value; + startYear = anticipatedConstructionStarDate.split('-') + endDate = anticipatedCommissioningDate.split('-'); + var anticipatedConstructionStartYear = startYear[0]; + var anticipatedConstructionStartMonth = startYear[1]; + var anticipatedConstructionStartDay = startYear[2]; + var anticipatedCommissioningStartYear = endDate[0]; + var anticipatedCommissioningStartMonth = endDate[1]; + var anticipatedCommissioningStartDay = endDate[2]; + var anticipatedConstructionStarDateDict = { 'day': anticipatedConstructionStartDay, 'month': anticipatedConstructionStartMonth, 'year': anticipatedConstructionStartYear, } - var anticipatedCommissioningDate = { + var anticipatedCommissioningDateDict = { 'day': anticipatedCommissioningStartDay, 'month': anticipatedCommissioningStartMonth, 'year': anticipatedCommissioningStartYear, } - var validDate = validateDate(anticipatedConstructionStarDate, anticipatedCommissioningDate); + // Ensure that commissioning date is after start date. + var validDate = validateDate(anticipatedConstructionStarDateDict, anticipatedCommissioningDateDict); if (!validDate) { alert("Anticipated Commissioning date has to be after Anticipated Construction start date"); } else { - const result = {}; - for (const [key, value] of formData.entries()) { - result[key] = value; + result = { + 'pro_forma_start_date': proFormaStartDate, + 'pro_forma_duration': proFormaDuration, + 'analysis_date': analysisDate, + 'fund': fund, + 'anticipated_construction_start_date': anticipatedConstructionStarDate, + 'anticipated_commissioning_date': anticipatedCommissioningDate, + 'anticipated_construction_period': anticipatedConstructionPeriod, } request('finance-overview/', { method: 'PUT', @@ -78,6 +136,13 @@ function billProjectionDatesForm(form) { 'X-CSRFToken': Cookies.get('csrftoken') }) }).then(res => { + // Display response message. Add that loan options changed if fund was changed. Reset didFundChange to false. + responseMessage = res.payload.msg; + if (didFundChange) { + responseMessage += ' Loan Options table is reloded.'; + } + var resMsg = document.querySelector('#pro-forma-form-save-msg'); + resMsg.innerHTML = `${responseMessage}`; /** * delete-loan-options request to the backend to delete loan options for this building id if previosly stored. * This ensures that the loan options table is up to date with the latest fund. Clear the loan options table @@ -94,9 +159,9 @@ function billProjectionDatesForm(form) { }).then(res => { var tableBody = document.querySelector('#loan-options-table tbody'); tableBody.innerHTML = ``; - getLoanOptionsTable(); }); } + getLoanOptionsTable(); }); } return false; diff --git a/blocnote/apps/financialInputs/templates/financialInputs/index.html b/blocnote/apps/financialInputs/templates/financialInputs/index.html index 11d2565f02f74eadccf89eee1e5ee029050a0211..a208ffd677a8cd67f3633d461e4d272e2ac62027 100644 --- a/blocnote/apps/financialInputs/templates/financialInputs/index.html +++ b/blocnote/apps/financialInputs/templates/financialInputs/index.html @@ -13,7 +13,7 @@