From 3053a9bfb2328de6947ddfeab101f393266cc9fa Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Thu, 5 Oct 2017 13:06:28 -0700 Subject: [PATCH] Adds donation calculator. --- pinaxcon/templatetags/__init__.py | 0 pinaxcon/templatetags/nbpy_tags.py | 44 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 pinaxcon/templatetags/__init__.py create mode 100644 pinaxcon/templatetags/nbpy_tags.py diff --git a/pinaxcon/templatetags/__init__.py b/pinaxcon/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pinaxcon/templatetags/nbpy_tags.py b/pinaxcon/templatetags/nbpy_tags.py new file mode 100644 index 0000000..16af756 --- /dev/null +++ b/pinaxcon/templatetags/nbpy_tags.py @@ -0,0 +1,44 @@ +from registrasion.models import commerce +from registrasion.controllers.category import CategoryController +from registrasion.controllers.item import ItemController + +from decimal import Decimal +from django import template +from django.conf import settings +from django.db.models import Sum +from urllib import urlencode # TODO: s/urllib/six.moves.urllib/ + +register = template.Library() + + +@register.simple_tag(takes_context=True) +def donation_income(context, invoice): + ''' Calculates the donation income for a given invoice. + + Returns: + the donation income. + + ''' + + # 15% (FSA) goes to Conservancy; 85% is real goods + + fsa_rate = Decimal("0.85") + rbi_full_ticket = Decimal("68.00") + rbi_early_bird_discount = Decimal("-21.35") + rbi = [] + + for line in invoice.lineitem_set.all(): + if line.product.category.name == "Ticket": + if line.product.name.startswith("Unaffiliated Individual"): + # Includes full price & discounts + rbi.append(line.total_price * fsa_rate) + else: + if line.total_price > 0: + rbi.append(rbi_full_ticket) + elif line.total_price < 0: + rbi.append(rbi_early_bird_discount) + elif line.product.category.name == "T-Shirt": + rbi.append(line.total_price * fsa_rate) + + donation = (invoice.value - sum(rbi)) + return donation.quantize(Decimal('.01'))