Luke Plant avatar Luke Plant committed 406e8aa

Added 'RefundPayment' model and functionality, to allow recording of refunds.

Comments (0)

Files changed (5)

cciw/bookings/admin.py

 from django import forms
 
 from cciw.bookings.forms import FixPriceMixin
-from cciw.bookings.models import Price, BookingAccount, Booking, ChequePayment
+from cciw.bookings.models import Price, BookingAccount, Booking, ChequePayment, RefundPayment
 from cciw.cciwmain.common import get_thisyear
 from cciw.utils.views import close_window_response
 
         )
 
 
-class ChequePaymentAdminForm(forms.ModelForm):
+class ChequePaymentAdminFormBase(forms.ModelForm):
 
     account = account_autocomplete_field()
 
-    class Meta:
-        model = ChequePayment
-
     def clean(self):
-        retval = super(ChequePaymentAdminForm, self).clean()
+        retval = super(ChequePaymentAdminFormBase, self).clean()
         if self.instance is not None and self.instance.id is not None:
             raise forms.ValidationError("Cheque payments cannot be changed "
                                         "after being created. If an error was made, "
         return retval
 
 
-class ChequePaymentAdmin(admin.ModelAdmin):
+class ChequePaymentAdminForm(ChequePaymentAdminFormBase):
+
+    class Meta:
+        model = ChequePayment
+
+
+class RefundPaymentAdminForm(ChequePaymentAdminFormBase):
+
+    class Meta:
+        model = RefundPayment
+
+
+class ChequePaymentAdminBase(admin.ModelAdmin):
     list_display = ['account', 'amount', 'created']
     search_fields = ['account__name']
     date_hierarchy = 'created'
-    form = ChequePaymentAdminForm
     fieldsets = [(None,
                   {'fields':
                        ['account', 'amount', 'created']})]
             return []
 
 
+class ChequePaymentAdmin(ChequePaymentAdminBase):
+    form = ChequePaymentAdminForm
+
+
+class RefundPaymentAdmin(ChequePaymentAdminBase):
+    form = RefundPaymentAdminForm
+
+
 admin.site.register(Price, PriceAdmin)
 admin.site.register(BookingAccount, BookingAccountAdmin)
 admin.site.register(Booking, BookingAdmin)
 admin.site.register(ChequePayment, ChequePaymentAdmin)
+admin.site.register(RefundPayment, RefundPaymentAdmin)

cciw/bookings/hooks.py

 
 from .signals import places_confirmed
 from .email import send_unrecognised_payment_email, send_places_confirmed_email
-from .models import BookingAccount, ChequePayment, send_payment
+from .models import BookingAccount, ChequePayment, RefundPayment, send_payment
 
 #### Handlers #####
 
     send_payment(-instance.amount, instance.account, instance)
 
 
+def refund_payment_sent(sender, **kwargs):
+    instance = kwargs['instance']
+    send_payment(-instance.amount, instance.account, instance)
+
+
+def refund_payment_deleted(sender, **kwargs):
+    instance = kwargs['instance']
+    send_payment(instance.amount, instance.account, instance)
+
+
 ### Place confirmation ###
 
 def places_confirmed_handler(sender, **kwargs):
 places_confirmed.connect(places_confirmed_handler)
 post_save.connect(cheque_payment_received, sender=ChequePayment)
 post_delete.connect(cheque_payment_deleted, sender=ChequePayment)
+post_save.connect(refund_payment_sent, sender=RefundPayment)
+post_delete.connect(refund_payment_deleted, sender=RefundPayment)

cciw/bookings/migrations/0008_auto__add_refundpayment.py

+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        
+        # Adding model 'RefundPayment'
+        db.create_table('bookings_refundpayment', (
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+            ('amount', self.gf('django.db.models.fields.DecimalField')(max_digits=10, decimal_places=2)),
+            ('account', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['bookings.BookingAccount'])),
+            ('created', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now)),
+        ))
+        db.send_create_signal('bookings', ['RefundPayment'])
+
+
+    def backwards(self, orm):
+        
+        # Deleting model 'RefundPayment'
+        db.delete_table('bookings_refundpayment')
+
+
+    models = {
+        'auth.group': {
+            'Meta': {'object_name': 'Group'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+        },
+        'auth.permission': {
+            'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+        },
+        'auth.user': {
+            'Meta': {'object_name': 'User'},
+            'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+            'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+        },
+        'bookings.booking': {
+            'Meta': {'ordering': "['-created']", 'object_name': 'Booking'},
+            'account': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'bookings'", 'to': "orm['bookings.BookingAccount']"}),
+            'address': ('django.db.models.fields.TextField', [], {}),
+            'agreement': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'allergies': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'amount_due': ('django.db.models.fields.DecimalField', [], {'max_digits': '10', 'decimal_places': '2'}),
+            'booking_expires': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
+            'camp': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'bookings'", 'to': "orm['cciwmain.Camp']"}),
+            'church': ('django.db.models.fields.CharField', [], {'max_length': '100', 'blank': 'True'}),
+            'contact_name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'contact_phone_number': ('django.db.models.fields.CharField', [], {'max_length': '22'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'date_of_birth': ('django.db.models.fields.DateField', [], {}),
+            'dietary_requirements': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+            'gp_address': ('django.db.models.fields.TextField', [], {}),
+            'gp_name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'gp_phone_number': ('django.db.models.fields.CharField', [], {'max_length': '22'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'illnesses': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'last_tetanus_injection': ('django.db.models.fields.DateField', [], {'null': 'True', 'blank': 'True'}),
+            'learning_difficulties': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'medical_card_number': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'phone_number': ('django.db.models.fields.CharField', [], {'max_length': '22', 'blank': 'True'}),
+            'post_code': ('django.db.models.fields.CharField', [], {'max_length': '10'}),
+            'price_type': ('django.db.models.fields.PositiveSmallIntegerField', [], {}),
+            'regular_medication_required': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'serious_illness': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'sex': ('django.db.models.fields.CharField', [], {'max_length': '1'}),
+            'shelved': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'south_wales_transport': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'state': ('django.db.models.fields.IntegerField', [], {})
+        },
+        'bookings.bookingaccount': {
+            'Meta': {'unique_together': "[('name', 'post_code'), ('name', 'email')]", 'object_name': 'BookingAccount'},
+            'address': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'unique': 'True', 'null': 'True', 'blank': 'True'}),
+            'first_login': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'last_login': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}),
+            'phone_number': ('django.db.models.fields.CharField', [], {'max_length': '22', 'blank': 'True'}),
+            'post_code': ('django.db.models.fields.CharField', [], {'max_length': '10', 'null': 'True', 'blank': 'True'}),
+            'share_phone_number': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'total_received': ('django.db.models.fields.DecimalField', [], {'default': "'0.00'", 'max_digits': '10', 'decimal_places': '2'})
+        },
+        'bookings.chequepayment': {
+            'Meta': {'object_name': 'ChequePayment'},
+            'account': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['bookings.BookingAccount']"}),
+            'amount': ('django.db.models.fields.DecimalField', [], {'max_digits': '10', 'decimal_places': '2'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
+        },
+        'bookings.payment': {
+            'Meta': {'object_name': 'Payment'},
+            'account': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['bookings.BookingAccount']"}),
+            'amount': ('django.db.models.fields.DecimalField', [], {'max_digits': '10', 'decimal_places': '2'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'origin_id': ('django.db.models.fields.PositiveIntegerField', [], {}),
+            'origin_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+            'processed': ('django.db.models.fields.DateTimeField', [], {'null': 'True'})
+        },
+        'bookings.price': {
+            'Meta': {'unique_together': "(['year', 'price_type'],)", 'object_name': 'Price'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'price': ('django.db.models.fields.DecimalField', [], {'max_digits': '10', 'decimal_places': '2'}),
+            'price_type': ('django.db.models.fields.PositiveSmallIntegerField', [], {}),
+            'year': ('django.db.models.fields.PositiveSmallIntegerField', [], {})
+        },
+        'bookings.refundpayment': {
+            'Meta': {'object_name': 'RefundPayment'},
+            'account': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['bookings.BookingAccount']"}),
+            'amount': ('django.db.models.fields.DecimalField', [], {'max_digits': '10', 'decimal_places': '2'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
+        },
+        'cciwmain.camp': {
+            'Meta': {'ordering': "['-year', 'number']", 'unique_together': "(('year', 'number'),)", 'object_name': 'Camp'},
+            'admins': ('django.db.models.fields.related.ManyToManyField', [], {'blank': 'True', 'related_name': "'camps_as_admin'", 'null': 'True', 'symmetrical': 'False', 'to': "orm['auth.User']"}),
+            'chaplain': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'camps_as_chaplain'", 'null': 'True', 'to': "orm['cciwmain.Person']"}),
+            'end_date': ('django.db.models.fields.DateField', [], {}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'leaders': ('django.db.models.fields.related.ManyToManyField', [], {'blank': 'True', 'related_name': "'camps_as_leader'", 'null': 'True', 'symmetrical': 'False', 'to': "orm['cciwmain.Person']"}),
+            'max_campers': ('django.db.models.fields.PositiveSmallIntegerField', [], {'default': '80'}),
+            'max_female_campers': ('django.db.models.fields.PositiveSmallIntegerField', [], {'default': '60'}),
+            'max_male_campers': ('django.db.models.fields.PositiveSmallIntegerField', [], {'default': '60'}),
+            'maximum_age': ('django.db.models.fields.PositiveSmallIntegerField', [], {}),
+            'minimum_age': ('django.db.models.fields.PositiveSmallIntegerField', [], {}),
+            'number': ('django.db.models.fields.PositiveSmallIntegerField', [], {}),
+            'officers': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.User']", 'through': "orm['officers.Invitation']", 'symmetrical': 'False'}),
+            'online_applications': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'previous_camp': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'next_camps'", 'null': 'True', 'to': "orm['cciwmain.Camp']"}),
+            'site': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cciwmain.Site']"}),
+            'start_date': ('django.db.models.fields.DateField', [], {}),
+            'year': ('django.db.models.fields.PositiveSmallIntegerField', [], {})
+        },
+        'cciwmain.person': {
+            'Meta': {'ordering': "('name',)", 'object_name': 'Person'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'info': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '40'}),
+            'users': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.User']", 'symmetrical': 'False', 'blank': 'True'})
+        },
+        'cciwmain.site': {
+            'Meta': {'object_name': 'Site'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'info': ('django.db.models.fields.TextField', [], {}),
+            'long_name': ('django.db.models.fields.CharField', [], {'max_length': "'50'"}),
+            'short_name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': "'25'"}),
+            'slug_name': ('django.db.models.fields.SlugField', [], {'db_index': 'True', 'unique': 'True', 'max_length': "'25'", 'blank': 'True'})
+        },
+        'contenttypes.contenttype': {
+            'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+        },
+        'officers.invitation': {
+            'Meta': {'ordering': "('-camp__year', 'officer__first_name', 'officer__last_name')", 'unique_together': "(('officer', 'camp'),)", 'object_name': 'Invitation'},
+            'camp': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cciwmain.Camp']"}),
+            'date_added': ('django.db.models.fields.DateField', [], {'default': 'datetime.date.today'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'notes': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+            'officer': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
+        }
+    }
+
+    complete_apps = ['bookings']

cciw/bookings/models.py

         return super(ChequePaymentManager, self).get_query_set().select_related('account')
 
 
-class ChequePayment(models.Model):
+class ChequePaymentBase(models.Model):
     amount = models.DecimalField(decimal_places=2, max_digits=10)
     account = models.ForeignKey(BookingAccount)
     created = models.DateTimeField(default=datetime.now)
 
     objects = ChequePaymentManager()
 
+    def save(self, **kwargs):
+        if self.id is not None:
+            raise Exception("%s cannot be edited after it has been saved to DB" %
+                            self.__class__.__name__)
+        else:
+            return super(ChequePaymentBase, self).save(**kwargs)
+
+    class Meta:
+        abstract = True
+
+
+class ChequePayment(ChequePaymentBase):
+
     def __unicode__(self):
         return u"Cheque payment of £%s from %s" % (self.amount, self.account)
 
-    def save(self, **kwargs):
-        if self.id is not None:
-            raise Exception("ChequePayment cannot be edited after it has been saved to DB")
-        else:
-            return super(ChequePayment, self).save(**kwargs)
+
+class RefundPayment(ChequePaymentBase):
+
+    def __unicode__(self):
+        return u"Refund payment of £%s to %s" % (self.amount, self.account)
 
 
 def trigger_payment_processing():

cciw/bookings/tests.py

 from django.utils import simplejson
 
 from cciw.bookings.management.commands.expire_bookings import Command as ExpireBookingsCommand
-from cciw.bookings.models import BookingAccount, Price, Booking, Payment, ChequePayment, book_basket_now
+from cciw.bookings.models import BookingAccount, Price, Booking, Payment, ChequePayment, RefundPayment, book_basket_now
 from cciw.bookings.models import PRICE_FULL, PRICE_2ND_CHILD, PRICE_3RD_CHILD, PRICE_CUSTOM, PRICE_SOUTH_WALES_TRANSPORT, PRICE_DEPOSIT, BOOKING_APPROVED, BOOKING_INFO_COMPLETE, BOOKING_BOOKED, BOOKING_CANCELLED, BOOKING_CANCELLED_FULL_REFUND
 from cciw.cciwmain.common import get_thisyear
 from cciw.cciwmain.models import Camp
         ChequePayment.objects.create(account=acc,
                                      amount=Decimal('100.00'))
         self.assertEqual(Payment.objects.count(), 1)
+        self.assertEqual(Payment.objects.all()[0].amount, Decimal('100.00'))
 
     def test_delete(self):
         # Setup
         self.assertRaises(Exception, cp.save)
 
 
+class TestRefundPayment(TestCase):
+
+    def test_create(self):
+        acc = BookingAccount.objects.create(email='foo@foo.com')
+        self.assertEqual(Payment.objects.count(), 0)
+        RefundPayment.objects.create(account=acc,
+                                     amount=Decimal('100.00'))
+        self.assertEqual(Payment.objects.count(), 1)
+        self.assertEqual(Payment.objects.all()[0].amount, Decimal('-100.00'))
+
+    def test_delete(self):
+        # Setup
+        acc = BookingAccount.objects.create(email='foo@foo.com')
+        cp = RefundPayment.objects.create(account=acc,
+                                          amount=Decimal('100.00'))
+        Payment.objects.all().delete() # reset
+
+        # Test
+        cp.delete()
+        self.assertEqual(Payment.objects.count(), 1)
+        self.assertEqual(Payment.objects.all()[0].amount, cp.amount)
+
+    def test_edit(self):
+        # Setup
+        acc = BookingAccount.objects.create(email='foo@foo.com')
+        cp = RefundPayment.objects.create(account=acc,
+                                          amount=Decimal('100.00'))
+
+        cp.amount=Decimal("101.00")
+        self.assertRaises(Exception, cp.save)
+
+
+
 class TestCancel(CreatePlaceMixin, TestCase):
     """
     Tests covering what happens when a user cancels.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.