1212from django_registration .backends .one_step .views import RegistrationView
1313from dateutil .rrule import rrule , MONTHLY
1414from django .apps import apps
15+ from decimal import Decimal
1516
1617from . import forms , models , pdf_rendering , micro_timesheet
1718from .temporary_locale import TemporaryLocale
18- from decimal import Decimal
1919
2020
2121class IndexView (TemplateView ):
@@ -77,6 +77,23 @@ def get_context_data(self, **kwargs):
7777
7878 # build up the monthly / quartery / yearly total
7979 totals = dict ()
80+ if invoices :
81+ # fill in all spots between first and last invoice in reverse order
82+ since = invoices .last ().issue_date .replace (day = 1 )
83+ until = invoices .first ().issue_date .replace (day = 1 )
84+ all_months = list (rrule (freq = MONTHLY , dtstart = since , until = until , bymonthday = 1 ))
85+ for every_month in reversed (all_months ):
86+ year = every_month .year
87+ quarter = models .quarter_of (every_month )
88+ month = every_month .month
89+
90+ if year not in totals :
91+ totals [year ] = dict (total = 0 )
92+ if quarter not in totals [year ]:
93+ totals [year ][quarter ] = dict (total = 0 )
94+ if month not in totals [year ][quarter ]:
95+ totals [year ][quarter ][month ] = dict (total = 0 , count = 0 , date = every_month )
96+
8097 for invoice in invoices :
8198 year = invoice .issue_date .year
8299 quarter = models .quarter_of (invoice .issue_date )
@@ -100,22 +117,6 @@ def get_context_data(self, **kwargs):
100117 total_year [quarter ] = total_quarter
101118 totals [year ] = total_year
102119
103- if invoices :
104- # fill in missing spots between first and last invoice
105- since = invoices .last ().issue_date .replace (day = 1 )
106- until = invoices .first ().issue_date .replace (day = 1 )
107- for every_month in rrule (freq = MONTHLY , dtstart = since , until = until , bymonthday = 1 ):
108- year = every_month .year
109- quarter = models .quarter_of (every_month )
110- month = every_month .month
111-
112- if year not in totals :
113- totals [year ] = dict (total = 0 )
114- if quarter not in totals [year ]:
115- totals [year ][quarter ] = dict (total = 0 )
116- if month not in totals [year ][quarter ]:
117- totals [year ][quarter ][month ] = dict (total = 0 , count = 0 , date = every_month )
118-
119120 context ["totals" ] = totals
120121
121122 return context
0 commit comments