From 067e03b7572c6b932591c83ac03320025220fa50 Mon Sep 17 00:00:00 2001 From: Tom Price Date: Mon, 10 Apr 2017 23:14:09 +0100 Subject: [PATCH] Remove Event.purchase_order in favour of a simple EventAuthorisation object. --- RIGS/forms.py | 2 +- .../migrations/0028_migrate_purchase_order.py | 47 +++++++++++++++++++ RIGS/models.py | 1 - RIGS/rigboard.py | 1 - RIGS/templates/RIGS/event_detail.html | 5 -- RIGS/templates/RIGS/event_print_page.xml | 8 ---- RIGS/templates/RIGS/invoice_detail.html | 5 -- RIGS/test_functional.py | 6 +-- 8 files changed, 49 insertions(+), 26 deletions(-) create mode 100644 RIGS/migrations/0028_migrate_purchase_order.py 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 %} -
PO
-
{{ object.purchase_order }}
- {% endif %} -
 
Authorised
diff --git a/RIGS/templates/RIGS/event_print_page.xml b/RIGS/templates/RIGS/event_print_page.xml index c0a2afa1..56f75e68 100644 --- a/RIGS/templates/RIGS/event_print_page.xml +++ b/RIGS/templates/RIGS/event_print_page.xml @@ -34,14 +34,6 @@ {{ invoice.invoice_date|date:"d/m/Y" }} - {% if object.purchase_order %} - - PO Number - - {{ object.purchase_order|default_if_none:"" }} - - - {% endif %} {% elif quote %} diff --git a/RIGS/templates/RIGS/invoice_detail.html b/RIGS/templates/RIGS/invoice_detail.html index d45deeda..d8da9182 100644 --- a/RIGS/templates/RIGS/invoice_detail.html +++ b/RIGS/templates/RIGS/invoice_detail.html @@ -76,11 +76,6 @@
{{ object.checked_in_by.name }}
{% endif %} - {% if object.event.purchase_order %} -
PO
-
{{ object.event.purchase_order }}
- {% endif %} -
 
Authorised
diff --git a/RIGS/test_functional.py b/RIGS/test_functional.py index bda1ffc6..f12bb7c7 100644 --- a/RIGS/test_functional.py +++ b/RIGS/test_functional.py @@ -443,7 +443,7 @@ class EventTest(LiveServerTestCase): def testEventDuplicate(self): testEvent = models.Event.objects.create(name="TE E1", status=models.Event.PROVISIONAL, start_date=date.today() + timedelta(days=6), - description="start future no end", purchase_order="TESTPO") + description="start future no end") item1 = models.EventItem( event=testEvent, @@ -509,8 +509,6 @@ class EventTest(LiveServerTestCase): infoPanel = self.browser.find_element_by_xpath('//div[contains(text(), "Event Info")]/..') self.assertIn("N0000%d" % testEvent.pk, infoPanel.find_element_by_xpath('//dt[text()="Based On"]/following-sibling::dd[1]').text) - # Check the PO hasn't carried through - self.assertNotIn("TESTPO", infoPanel.find_element_by_xpath('//dt[text()="PO"]/following-sibling::dd[1]').text) self.browser.get(self.live_server_url + '/event/' + str(testEvent.pk)) # Go back to the old event @@ -518,8 +516,6 @@ class EventTest(LiveServerTestCase): infoPanel = self.browser.find_element_by_xpath('//div[contains(text(), "Event Info")]/..') self.assertNotIn("N0000%d" % testEvent.pk, infoPanel.find_element_by_xpath('//dt[text()="Based On"]/following-sibling::dd[1]').text) - # Check the PO remains on the old event - self.assertIn("TESTPO", infoPanel.find_element_by_xpath('//dt[text()="PO"]/following-sibling::dd[1]').text) # Check the items are as they were table = self.browser.find_element_by_id('item-table') # ID number is known, see above