Skip to content

Commit a323f67

Browse files
author
Kalloor, Eric Abraham
committed
Calculation fixes
1 parent 33bd52f commit a323f67

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

register/api_views.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,6 @@ def get_report_data_api(request, poll_id):
324324
# Fetch all Registers
325325
register_query = Q(tenant, schedule__in=milk_delivered, log_date__gte=twelve_month_ago)
326326
all_register_entry = Register.objects.filter(register_query)
327-
register_data = all_register_entry.annotate(
328-
total=(F('current_price') * F('quantity'))
329-
).values('log_date__month', 'log_date__year', 'paid').annotate(
330-
monthly_total=Sum('total')
331-
)
332327

333328
for i in range(-12, 1):
334329
percent += 3.75
@@ -345,23 +340,22 @@ def get_report_data_api(request, poll_id):
345340
'log_date__year'] == graph_month.year), 0)
346341

347342
# Retrieve Income for the current month
348-
month_income = next((item['income'] for item in income_data if
349-
item['log_date__month'] == graph_month.month and item[
350-
'log_date__year'] == graph_month.year), 0)
343+
month_extra_income = next((item['income'] for item in income_data if
344+
item['log_date__month'] == graph_month.month and item[
345+
'log_date__year'] == graph_month.year), 0)
351346

352-
month_register_sale = next((item['monthly_total'] for item in register_data if
353-
item['log_date__month'] == graph_month.month and item[
354-
'log_date__year'] == graph_month.year and item[
355-
'paid'] in (0, 1)), 0) / 1000
347+
month_register_sale = sum(
348+
[entry.quantity * entry.current_price for entry in all_register_entry if
349+
entry.log_date.month == graph_month.month and entry.log_date.year == graph_month.year]) / 1000
356350

357-
month_income += month_register_sale
351+
month_register_sale += month_extra_income
358352

359-
month_paid = next((item['monthly_total'] for item in register_data if
360-
item['log_date__month'] == graph_month.month and item[
361-
'log_date__year'] == graph_month.year and item['paid']),
362-
0) / 1000
353+
month_paid = sum(
354+
[entry.quantity * entry.current_price for entry in all_register_entry if
355+
entry.log_date.month == graph_month.month and entry.log_date.year == graph_month.year and entry.paid]) / 1000
363356

364-
month_due = month_income - month_paid
357+
month_paid += month_extra_income
358+
month_due = month_register_sale - month_paid
365359

366360
profit = float(
367361
max(month_paid - month_expense, 0)) if month_paid > month_expense else False
@@ -371,7 +365,7 @@ def get_report_data_api(request, poll_id):
371365
current_month = {
372366
"monthName": month_str,
373367
"month": month_abbr,
374-
"income": float(month_income),
368+
"income": float(month_register_sale),
375369
"paid": float(month_paid),
376370
"due": float(month_due),
377371
"expense": float(month_expense),

0 commit comments

Comments
 (0)