diff --git a/RIGS/forms.py b/RIGS/forms.py index 14df5772..77e504eb 100644 --- a/RIGS/forms.py +++ b/RIGS/forms.py @@ -140,7 +140,7 @@ class EventForm(forms.ModelForm): fields = ['is_rig', 'name', 'venue', 'start_time', 'end_date', 'start_date', 'end_time', 'meet_at', 'access_at', 'description', 'notes', 'mic', 'person', 'organisation', 'dry_hire', 'checked_in_by', 'status', - 'collector', 'purchase_order'] + 'collector'] class BaseClientEventAuthorisationForm(forms.ModelForm): diff --git a/RIGS/migrations/0028_migrate_purchase_order.py b/RIGS/migrations/0028_migrate_purchase_order.py new file mode 100644 index 00000000..05275d03 --- /dev/null +++ b/RIGS/migrations/0028_migrate_purchase_order.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +from django.db.models import F, Sum, DecimalField + + +def POs_forward(apps, schema_editor): + VatRate = apps.get_model('RIGS', 'VatRate') + Event = apps.get_model('RIGS', 'Event') + EventItem = apps.get_model('RIGS', 'EventItem') + EventAuthorisation = apps.get_model('RIGS', 'EventAuthorisation') + db_alias = schema_editor.connection.alias + for event in Event.objects.using(db_alias).filter(purchase_order__isnull=False): + sum_total = EventItem.objects.filter(event=event).aggregate( + sum_total=Sum(models.F('cost') * F('quantity'), + output_field=DecimalField( + max_digits=10, + decimal_places=2) + ) + )['sum_total'] + + vat = VatRate.objects.using(db_alias).filter(start_at__lte=event.start_date).latest() + total = sum_total + sum_total * vat.rate + + EventAuthorisation.objects.using(db_alias).create(event=event, name='LEGACY', + email='treasurer@nottinghamtec.co.uk', + amount=total) + + +def POs_reverse(apps, schema_editor): + EventAuthorisation = apps.get_model('RIGS', 'EventAuthorisation') + db_alias = schema_editor.connection.alias + for auth in EventAuthorisation.objects.using(db_alias).filter(po__isnull=False): + auth.event.purchase_order = auth.po + auth.delete() + + +class Migration(migrations.Migration): + dependencies = [ + ('RIGS', '0027_eventauthorisation_event_singular'), + ] + + operations = [ + migrations.RunPython(POs_forward, POs_reverse), + migrations.RemoveField(model_name='event', name='purchase_order') + ] diff --git a/RIGS/models.py b/RIGS/models.py index d1f20446..9b73a1ab 100644 --- a/RIGS/models.py +++ b/RIGS/models.py @@ -332,7 +332,6 @@ class Event(models.Model, RevisionMixin): # Monies payment_method = models.CharField(max_length=255, blank=True, null=True) payment_received = models.CharField(max_length=255, blank=True, null=True) - purchase_order = models.CharField(max_length=255, blank=True, null=True, verbose_name='PO') collector = models.CharField(max_length=255, blank=True, null=True, verbose_name='collected by') # Calculated values diff --git a/RIGS/rigboard.py b/RIGS/rigboard.py index 1c6d6b5e..f6565b3f 100644 --- a/RIGS/rigboard.py +++ b/RIGS/rigboard.py @@ -125,7 +125,6 @@ class EventDuplicate(EventUpdate): old = super(EventDuplicate, self).get_object(queryset) # Get the object (the event you're duplicating) new = copy.copy(old) # Make a copy of the object in memory new.based_on = old # Make the new event based on the old event - new.purchase_order = None if self.request.method in ( 'POST', 'PUT'): # This only happens on save (otherwise items won't display in editor) diff --git a/RIGS/templates/RIGS/event_detail.html b/RIGS/templates/RIGS/event_detail.html index 22f8aa4a..3493a421 100644 --- a/RIGS/templates/RIGS/event_detail.html +++ b/RIGS/templates/RIGS/event_detail.html @@ -148,11 +148,6 @@ {% endif %} {% if event.is_rig %} - {% if object.purchase_order %} -