Skip to content

Commit

Permalink
Light reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
edelgm6 committed Jul 25, 2024
1 parent 5c7d512 commit 5f79b04
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 deletions api/views/journal_entry_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,55 @@ def get_paystubs_table_html(self):
paystubs_template = 'api/tables/paystubs-table.html'
return render_to_string(paystubs_template, {'paystubs': paystubs})

def get_combined_formset_errors(
self, debit_formset, credit_formset
):
form_errors = []
debit_total = debit_formset.get_entry_total()
credit_total = credit_formset.get_entry_total()
if debit_total != credit_total:
form_errors.append('Debits ($' + str(debit_total) + ') and Credits ($' + str(credit_total) + ') must balance.')

print(form_errors)
return form_errors

def check_for_errors(self, request, debit_formset, credit_formset, metadata_form, transaction):
has_errors = False
form_errors = []
# Check if formsets have errors on their own, then check if they have errors
# in the aggregate (e.g., don't have balanced credits/debits)
if debit_formset.is_valid() and credit_formset.is_valid() and metadata_form.is_valid():
form_errors = self.get_combined_formset_errors(
debit_formset=debit_formset,
credit_formset=credit_formset
)
has_errors = bool(form_errors)
else:
print(debit_formset.errors)
print(credit_formset.errors)
print(metadata_form.errors)
has_errors = True

if not has_errors:
return False, None

context = {
'debit_formset': debit_formset,
'credit_formset': credit_formset,
'transaction_id': transaction.id,
'autofocus_debit': True,
'form_errors': form_errors,
'prefilled_total': debit_formset.get_entry_total(),
'debit_prefilled_total': debit_formset.get_entry_total(),
'credit_prefilled_total': credit_formset.get_entry_total(),
'metadata_form': metadata_form
}

html = render_to_string(self.entry_form_template, context)
response = HttpResponse(html)
response.headers['HX-Retarget'] = '#form-div'
return True, response

def get_journal_entry_form_html(
self, transaction, index=0, debit_formset=None,
credit_formset=None, is_debit=True, form_errors=None,
Expand Down Expand Up @@ -245,57 +294,9 @@ def get(self, request):
html = render_to_string(self.view_template, context)
return HttpResponse(html)

def _get_combined_formset_errors(
self, debit_formset, credit_formset
):
form_errors = []
debit_total = debit_formset.get_entry_total()
credit_total = credit_formset.get_entry_total()
if debit_total != credit_total:
form_errors.append('Debits ($' + str(debit_total) + ') and Credits ($' + str(credit_total) + ') must balance.')

print(form_errors)
return form_errors

def _check_for_errors(self, request, debit_formset, credit_formset, metadata_form, transaction):
has_errors = False
form_errors = []
# Check if formsets have errors on their own, then check if they have errors
# in the aggregate (e.g., don't have balanced credits/debits)
if debit_formset.is_valid() and credit_formset.is_valid() and metadata_form.is_valid():
form_errors = self._get_combined_formset_errors(
debit_formset=debit_formset,
credit_formset=credit_formset
)
has_errors = bool(form_errors)
else:
print(debit_formset.errors)
print(credit_formset.errors)
print(metadata_form.errors)
has_errors = True

if not has_errors:
return False, None

context = {
'debit_formset': debit_formset,
'credit_formset': credit_formset,
'transaction_id': transaction.id,
'autofocus_debit': True,
'form_errors': form_errors,
'prefilled_total': debit_formset.get_entry_total(),
'debit_prefilled_total': debit_formset.get_entry_total(),
'credit_prefilled_total': credit_formset.get_entry_total(),
'metadata_form': metadata_form
}

html = render_to_string(self.entry_form_template, context)
response = HttpResponse(html)
response.headers['HX-Retarget'] = '#form-div'
return True, response

def post(self, request, transaction_id):
# Build formsets for the credit and debit side of the JE and get transaction
# and metadata form
JournalEntryItemFormset = modelformset_factory(
JournalEntryItem,
formset=BaseJournalEntryItemFormset,
Expand All @@ -309,8 +310,8 @@ def post(self, request, transaction_id):
metadata_form = JournalEntryMetadataForm(request.POST)
transaction = get_object_or_404(Transaction, pk=transaction_id)

# First check if the forms are valid and create JEIs if so
has_errors, response = self._check_for_errors(
# First check if the forms are valid and return errors if not
has_errors, response = self.check_for_errors(
debit_formset=debit_formset,
credit_formset=credit_formset,
request=request,
Expand Down Expand Up @@ -339,12 +340,11 @@ def post(self, request, transaction_id):
except ValueError:
pass

# Build the transactions table — use the filter settings if valid,
# Build the transactions table — use the existing filter settings if valid,
# else return all transactions
filter_form = TransactionFilterForm(request.POST, prefix='filter')
if filter_form.is_valid():
transactions = filter_form.get_transactions()
# Default to 0 if 'index' is not provided
index = metadata_form.cleaned_data['index']
else:
_, transactions = self.get_filter_form_html_and_objects(
Expand All @@ -357,7 +357,7 @@ def post(self, request, transaction_id):
index = 0

if len(transactions) == 0:
entry_form_html = None
entry_form_html = ''
else:
# Need to check an index error in case
# user chose the last entry
Expand Down Expand Up @@ -385,4 +385,4 @@ def post(self, request, transaction_id):
'paystubs_table': paystubs_table_html
}
html = render_to_string(self.view_template, context)
return HttpResponse(html)
return HttpResponse(html)

0 comments on commit 5f79b04

Please sign in to comment.