def budget_request(app, story): story.append(Paragraph("Budget Request", styles["Heading2"])) categories = [] current_year = {} # XXX HACKS -- make current year categories always come first! for category in LineItemCategory.objects.filter( years__date=app.year).order_by('pk'): category_sum = sum_line_items( LineItem.objects.filter(application=app, category=category)) # XXX HACKS if category.name.startswith("Current"): current_year['name'] = category.name current_year['sum'] = category_sum current_year[ 'end_balance'] = category_sum - app.balance_forward - app.afap_income current_year['object'] = category else: categories.append({ 'name': category.name, 'sum': category_sum, 'object': category }) total_sum = sum([x['sum'] for x in categories]) + current_year['end_balance'] total_sum = total_sum or 0 total_request = max(total_sum, 0) table_data = [ ("Category", "Amount"), ("Balance forward", "$%s" % (app.balance_forward, )), ("AFAP Income", "$%s" % (app.afap_income, )), ("Current Year", "$%s" % (current_year['sum'], )), ("Current year end balance", "$%s" % (current_year['end_balance'], )), ("Projected %s expenses and income" % (app.year.range, ), ), ] for category in categories: table_data.append((category['name'], "$%s" % (category['sum'], ))) print app, total_request table_data.append(( "Sum", "$%s" % (total_sum, ), ), ) table_data.append(( "Request", "$%s" % (total_request, ), )) story.append( Table(table_data, repeatRows=1, style=[('SPAN', (0, 5), (1, 5)), ('GRID', (0, 0), (-1, -1), 1, colors.black)])) if current_year['object'].lineitem_set.filter(application=app).exists(): budget_request_category(app, current_year['object'], story) for category in categories: if category['object'].lineitem_set.filter(application=app).exists(): budget_request_category(app, category['object'], story)
def budget_request_category(app, category, story): story.append(Paragraph(category.name, styles["Heading3"])) table_data = [("Description", "Amount")] lineitems = LineItem.objects.filter(application=app, category=category) for lineitem in lineitems: if lineitem.income: table_data.append((Paragraph("%s (income)" % (lineitem.description,), styles['Normal']), "$%s" % -lineitem.amount)) else: table_data.append((Paragraph(lineitem.description, styles["Normal"]), "$%s" % lineitem.amount)) table_data.append(("Sum", "$%s" % sum_line_items(lineitems))) story.append(Table(table_data, repeatRows=1, style=[('GRID', (0,0), (-1,-1), 1, colors.black)]))
def approve(request, year, hash): try: year = afap.models.Year.objects.get(date=RangedDate(int(year), 1, 1)) approval = afap.models.Approval.objects.get(key=hash) except (ValueError, afap.models.Year.DoesNotExist, afap.models.Approval.DoesNotExist): messages.error(request, "Invalid approval link; please check your email account " + "for an updated link or contact the person in your organization responsible " + "for the AFAP application.") return shortcuts.render_to_response('approval.html', {}, template.RequestContext(request)) if request.method == 'POST': form = afap.forms.ApprovalForm(request.POST, instance=approval) if form.is_valid(): form.save() messages.info(request, "Your changes have been saved.") return shortcuts.redirect(urlresolvers.reverse(afap.views.approve, args=(year.year, hash,))) else: messages.error(request, "An error occurred in saving your changes, " + "please refer to any error messages below.") app = afap.forms.ApplicationForm(instance=approval.application) budget_request_by_cat = {} line_items = afap.models.LineItem.objects.filter(application=approval.application) for line_item in line_items: try: budget_request_by_cat[line_item.category]['line_items'].append(line_item) except (KeyError): budget_request_by_cat[line_item.category] = {'line_items': [line_item,],} budget_request = [] for category in budget_request_by_cat: budget_request.append({ 'category': category, 'line_items': budget_request_by_cat[category]['line_items'], 'sum': sum_line_items(budget_request_by_cat[category]['line_items']), }) total_sum = sum([x['sum'] for x in budget_request]) - ( approval.application.balance_forward + approval.application.afap_income) total_request = max(total_sum, 0) form = afap.forms.ApprovalForm(instance=approval) return shortcuts.render_to_response('approval.html', { 'form': form, 'application': app, 'budget_request': budget_request, 'approval': approval, 'total_sum': total_sum, 'total_request': total_request, 'year': year, 'hash': hash, 'balance_forward': approval.application.balance_forward, 'afap_income': approval.application.afap_income,}, template.RequestContext(request))
def budgetrequestcategory(request, org, year, category): LineItemFormSet = modelformset_factory(afap.models.LineItem, exclude=('application', 'category'), can_delete=True, extra=10) try: year = afap.models.Year.objects.get(date=RangedDate(int(year), 1, 1)) except (ValueError, afap.models.Year.DoesNotExist): return shortcuts.redirect(urlresolvers.reverse(home)) try: app = afap.models.Application.objects.get(organization=org, year=year.date) category = afap.models.LineItemCategory.objects.get(pk=category, years=year) except afap.models.Application.DoesNotExist: messages.error(request, "You do not have an application for year %s" % year.range) return shortcuts.redirect(urlresolvers.reverse(home)) except afap.models.LineItemCategory.DoesNotExist: return shortcuts.redirect(urlresolvers.reverse(budgetrequest, args=(year.year,))) if redirect_to_application(app): return shortcuts.redirect(urlresolvers.reverse(application, args=(year.year,))) if request.method == 'POST': if year.read_only: return shortcuts.redirect(urlresolvers.reverse(budgetrequestcategory, args=[year.year, category.pk])) formset = LineItemFormSet(request.POST) if formset.is_valid(): instances = formset.save(commit=False) for instance in instances: instance.application = app instance.category = category instance.save() messages.info(request, "Your changes have been saved.") return shortcuts.redirect(urlresolvers.reverse(budgetrequestcategory, args=[year.year, category.pk])) else: formset = LineItemFormSet( queryset=afap.models.LineItem.objects.filter(application=app, category=category)) for form in formset.forms: form.fields['description'].widget.attrs['class'] = 'itemdescription' form.fields['amount'].widget.attrs['class'] = 'itemamount' form.fields['income'].widget.attrs['class'] = 'itemincome' form.fields['DELETE'].widget.attrs['class'] = 'itemdelete' category_sum = sum_line_items( afap.models.LineItem.objects.filter(application=app, category=category)) return shortcuts.render_to_response('budgetrequestcategory.html', { 'formset': formset, 'year': year, 'active_year': year, 'category': category, 'category_sum': category_sum, 'read_only': year.read_only,}, template.RequestContext(request))
def budgetrequest(request, org, year): try: year = afap.models.Year.objects.get(date=RangedDate(int(year), 1, 1)) app = afap.models.Application.objects.get(organization=org, year=year.date) except ValueError: return shortcuts.redirect(urlresolvers.reverse(home)) except afap.models.Application.DoesNotExist: messages.error(request, "You do not have an application for year %s" % year.range) return shortcuts.redirect(urlresolvers.reverse(home)) if redirect_to_application(app): return shortcuts.redirect(urlresolvers.reverse(application, args=(year.year,))) if request.method == 'POST': if year.read_only: return shortcuts.redirect(urlresolvers.reverse(budgetrequest, args=(year.year,))) form = afap.forms.BudgetRequestForm(request.POST, instance=app) if form.is_valid(): form.save() messages.info(request, "Your changes have been saved.") return shortcuts.redirect(urlresolvers.reverse(budgetrequest, args=(year.year,))) else: form = afap.forms.BudgetRequestForm(instance=app) categories = [] current_year = {} # XXX HACKS -- make current year categories always come first! for category in afap.models.LineItemCategory.objects.filter(years__date=year.date).order_by('pk'): category_sum = sum_line_items( afap.models.LineItem.objects.filter(application=app, category=category)) # XXX HACKS if category.name.startswith("Current"): current_year['name'] = category.name current_year['sum'] = category_sum current_year['pk'] = category.pk current_year['end_balance'] = category_sum - app.balance_forward - app.afap_income else: categories.append({'name': category.name, 'sum': category_sum, 'pk': category.pk}) total_sum = sum([x['sum'] for x in categories]) + current_year['end_balance'] total_sum = total_sum or 0 total_request = max(total_sum, 0) return shortcuts.render_to_response('budgetrequest.html', { 'categories': categories, 'total_sum': total_sum, 'year': year, 'active_year': year, 'total_request': total_request, 'form': form, 'current_year': current_year, 'read_only': year.read_only}, template.RequestContext(request))
def budget_request_category(app, category, story): story.append(Paragraph(category.name, styles["Heading3"])) table_data = [("Description", "Amount")] lineitems = LineItem.objects.filter(application=app, category=category) for lineitem in lineitems: if lineitem.income: table_data.append( (Paragraph("%s (income)" % (lineitem.description, ), styles['Normal']), "$%s" % -lineitem.amount)) else: table_data.append( (Paragraph(lineitem.description, styles["Normal"]), "$%s" % lineitem.amount)) table_data.append(("Sum", "$%s" % sum_line_items(lineitems))) story.append( Table(table_data, repeatRows=1, style=[('GRID', (0, 0), (-1, -1), 1, colors.black)]))
def budget_request(app, story): story.append(Paragraph("Budget Request", styles["Heading2"])) categories = [] current_year = {} # XXX HACKS -- make current year categories always come first! for category in LineItemCategory.objects.filter(years__date=app.year).order_by('pk'): category_sum = sum_line_items( LineItem.objects.filter(application=app, category=category)) # XXX HACKS if category.name.startswith("Current"): current_year['name'] = category.name current_year['sum'] = category_sum current_year['end_balance'] = category_sum - app.balance_forward - app.afap_income current_year['object'] = category else: categories.append({'name': category.name, 'sum': category_sum, 'object': category}) total_sum = sum([x['sum'] for x in categories]) + current_year['end_balance'] total_sum = total_sum or 0 total_request = max(total_sum, 0) table_data = [ ("Category", "Amount"), ("Balance forward", "$%s" % (app.balance_forward,)), ("AFAP Income", "$%s" % (app.afap_income,)), ("Current Year", "$%s" % (current_year['sum'],)), ("Current year end balance", "$%s" % (current_year['end_balance'],)), ("Projected %s expenses and income" % (app.year.range,),), ] for category in categories: table_data.append((category['name'], "$%s" % (category['sum'],))) print app, total_request table_data.append(("Sum", "$%s" % (total_sum,),),) table_data.append(("Request", "$%s" % (total_request,),)) story.append(Table(table_data, repeatRows=1, style=[ ('SPAN', (0,5), (1,5)), ('GRID', (0,0), (-1,-1), 1, colors.black)])) if current_year['object'].lineitem_set.filter(application=app).exists(): budget_request_category(app, current_year['object'], story) for category in categories: if category['object'].lineitem_set.filter(application=app).exists(): budget_request_category(app, category['object'], story)