Skip to content

Commit

Permalink
Merge pull request #398 from nadineproject/outstanding
Browse files Browse the repository at this point in the history
Improve outstanding bill report performance and include 'total applied tax'
  • Loading branch information
jsayles committed Aug 28, 2018
2 parents 460539f + 5d2c6f5 commit 5ba86df
Show file tree
Hide file tree
Showing 28 changed files with 848 additions and 104 deletions.
2 changes: 1 addition & 1 deletion member/templates/member/profile/profile_billing.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ <h3>Recent Bills</h3>
{% for bill in bills %}
<tr class="{% cycle 'row-even' 'row-odd' %}">
<td nowrap>{{ bill.due_date|date:"M d, Y" }}</td>
<td>${{ bill.amount }}</td>
<td>${{ bill.total }}</td>
<td>${{ bill.total_paid }}</td>
<td>{% if bill.is_open %}Open{% endif %}</td>
<td><a href='{{ bill.get_absolute_url}}'>Receipt</a></td>
Expand Down
2 changes: 1 addition & 1 deletion nadine/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@

default_app_config = 'nadine.apps.NadineConfig'
28 changes: 24 additions & 4 deletions nadine/admin/billing.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
from django.contrib import admin

from nadine.admin.core import StyledAdmin
from nadine.models.billing import BillingBatch, UserBill, Payment, BillLineItem
from nadine.models.billing import BillingBatch, UserBill, BillLineItem
from nadine.models.billing import Payment, PaymentMethod
from nadine.models.billing import TaxRate, LineItemTax


class BillLineItemInline(admin.TabularInline):
model = BillLineItem
extra = 0

# class LineItemTaxInline(admin.TabularInline):
# model = LineItemTax
# extra = 0

class PaymentInline(admin.TabularInline):
model = Payment
raw_id_fields = ('user', )
# For now let's just make this read-only
readonly_fields = ('user', 'amount', 'note')
fields = (('user', 'amount'), 'note')
readonly_fields = ('user', 'amount', 'method', 'note')
fields = (('user', 'amount'), 'method', 'note')
extra = 0
# TODO = Hardcode user to be the bill.user

Expand All @@ -29,7 +34,7 @@ def formfield_for_foreignkey(self, db_field, request, **kwargs):

class UserBillAdmin(StyledAdmin):
model = UserBill
list_display = ('id', 'user', 'period_start', 'period_end', 'amount', 'total_paid')
list_display = ('id', 'user', 'period_start', 'period_end', 'amount', 'tax_amount', 'total', 'total_paid')
search_fields = ('user__username', 'user__first_name')
raw_id_fields = ('user', )
readonly_fields = ('id', 'created_ts', )
Expand All @@ -46,6 +51,18 @@ class UserBillAdmin(StyledAdmin):
inlines = [BillLineItemInline, PaymentInline]


class PaymentMethodAdmin(StyledAdmin):
model = PaymentMethod


class LineItemTaxAdmin(StyledAdmin):
model = LineItemTax


class TaxRateAdmin(StyledAdmin):
model = TaxRate


class BillingBatchAdmin(StyledAdmin):
def bills(self):
return self.bills.count()
Expand All @@ -59,3 +76,6 @@ def bills(self):

admin.site.register(BillingBatch, BillingBatchAdmin)
admin.site.register(UserBill, UserBillAdmin)
admin.site.register(PaymentMethod, PaymentMethodAdmin)
admin.site.register(TaxRate, TaxRateAdmin)
admin.site.register(LineItemTax, LineItemTaxAdmin)
11 changes: 11 additions & 0 deletions nadine/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.apps import AppConfig

class NadineConfig(AppConfig):
name = 'nadine'

def ready(self):
# Load and connect signal recievers
import nadine.signals
3 changes: 2 additions & 1 deletion nadine/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ def send_manage_member(user, subject=None):
"text": text_content,
"html": html_content,
}
return mailgun.mailgun_send(mailgun_data, inject_list_id=False)
if hasattr(settings, 'MAILGUN_DOMAIN'):
return mailgun.mailgun_send(mailgun_data, inject_list_id=False)


#####################################################################
Expand Down
4 changes: 3 additions & 1 deletion nadine/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from nadine.models.usage import PAYMENT_CHOICES, CoworkingDay
from nadine.models.resource import Room, Resource
from nadine.models.organization import Organization, OrganizationMember
from nadine.models.billing import UserBill, Payment
from nadine.models.billing import UserBill, Payment, PaymentMethod
from nadine.utils.payment_api import PaymentAPI
from member.models import HelpText, MOTD

Expand Down Expand Up @@ -177,6 +177,7 @@ class PaymentForm(forms.Form):
username = forms.CharField(required=True, widget=forms.HiddenInput)
# created_by = forms.CharField(required=False, widget=forms.HiddenInput)
payment_date = forms.DateField(required=True, widget=forms.DateInput(attrs={'placeholder':'e.g. 12/28/16', 'class':'datepicker'}, format='%m/%d/%Y'))
method = forms.ModelChoiceField(widget=forms.Select(attrs={'class': 'browser-default'}), label="Payment method", queryset=PaymentMethod.objects.all(), required=False)
note = forms.CharField(max_length=256, required=False)
amount = forms.DecimalField(min_value=0, max_value=10000, required=True, max_digits=7, decimal_places=2)

Expand All @@ -192,6 +193,7 @@ def save(self, created_by=None):
payment.created_by = User.objects.get(username=created_by)
payment.note = self.cleaned_data['note']
payment.amount = amount
payment.method = self.cleaned_data['method']
payment.save()
return payment

Expand Down
31 changes: 31 additions & 0 deletions nadine/migrations/0033_payment_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 2.0.2 on 2018-02-12 21:13

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('nadine', '0032_event_billing'),
]

operations = [
migrations.CreateModel(
name='PaymentMethod',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(help_text='e.g., Stripe, Visa, cash, bank transfer', max_length=128)),
],
),
migrations.AddField(
model_name='payment',
name='external_id',
field=models.CharField(blank=True, help_text='ID used by payment service', max_length=128, null=True),
),
migrations.AddField(
model_name='payment',
name='method',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='nadine.PaymentMethod'),
),
]
25 changes: 25 additions & 0 deletions nadine/migrations/0034_stripebillingprofile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 2.0.3 on 2018-04-06 18:37

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('nadine', '0033_payment_method'),
]

operations = [
migrations.CreateModel(
name='StripeBillingProfile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('customer_email', models.EmailField(help_text='Customer email address used with Stripe customer record', max_length=254)),
('customer_id', models.CharField(help_text='Stripe customer ID used for billing via Stripe', max_length=128)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
36 changes: 36 additions & 0 deletions nadine/migrations/0035_auto_20180413_2326.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 2.0.3 on 2018-04-13 23:26

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('nadine', '0034_stripebillingprofile'),
]

operations = [
migrations.CreateModel(
name='LineItemTax',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('amount', models.DecimalField(decimal_places=2, max_digits=7)),
('line_item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='nadine.BillLineItem')),
],
),
migrations.CreateModel(
name='TaxRate',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(help_text='The name of the tax', max_length=256)),
('percentage', models.DecimalField(decimal_places=2, help_text='Tax percentage', max_digits=3)),
('resources', models.ManyToManyField(to='nadine.Resource')),
],
),
migrations.AddField(
model_name='lineitemtax',
name='tax_rate',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='nadine.TaxRate'),
),
]
33 changes: 33 additions & 0 deletions nadine/migrations/0036_auto_20180601_2026.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 2.0.3 on 2018-06-01 20:26

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('nadine', '0035_auto_20180413_2326'),
]

operations = [
migrations.AddField(
model_name='userbill',
name='cached_total_amount',
field=models.DecimalField(decimal_places=2, default=0, max_digits=7),
),
migrations.AddField(
model_name='userbill',
name='cached_total_owed',
field=models.DecimalField(decimal_places=2, default=0, max_digits=7),
),
migrations.AddField(
model_name='userbill',
name='cached_total_paid',
field=models.DecimalField(decimal_places=2, default=0, max_digits=7),
),
migrations.AddField(
model_name='userbill',
name='cached_total_tax_amount',
field=models.DecimalField(decimal_places=2, default=0, max_digits=7),
),
]
3 changes: 2 additions & 1 deletion nadine/models/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ def trigger_new_membership(self, user):
mailing_list.subscribers.add(user)

# Invite them to slack
SlackAPI().invite_user_quiet(user)
if hasattr(settings, 'SLACK_API_TOKEN'):
SlackAPI().invite_user_quiet(user)

def trigger_profile_save(self, profile):
logger.debug("trigger_profile_save: %s" % profile)
Expand Down

0 comments on commit 5ba86df

Please sign in to comment.