diff --git a/src/shop/migrations/0050_auto_20170916_1336.py b/src/shop/migrations/0050_auto_20170916_1336.py new file mode 100644 index 00000000..2959d9ef --- /dev/null +++ b/src/shop/migrations/0050_auto_20170916_1336.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-09-16 11:36 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('shop', '0049_auto_20170914_2034'), + ] + + operations = [ + migrations.AddField( + model_name='creditnote', + name='customer', + field=models.TextField(blank=True, default='', help_text='Customer info if no user is selected'), + ), + migrations.AlterField( + model_name='creditnote', + name='text', + field=models.TextField(help_text='Description of what this credit note covers'), + ), + migrations.AlterField( + model_name='creditnote', + name='user', + field=models.ForeignKey(blank=True, help_text='The user this credit note belongs to, if any.', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='creditnotes', to=settings.AUTH_USER_MODEL, verbose_name='User'), + ), + ] diff --git a/src/shop/models.py b/src/shop/models.py index 0b407cd6..935f3e39 100644 --- a/src/shop/models.py +++ b/src/shop/models.py @@ -380,33 +380,70 @@ class CreditNote(CreatedUpdatedModel): class Meta: ordering = ['-created'] - amount = models.DecimalField(max_digits=10, decimal_places=2) - text = models.TextField() + amount = models.DecimalField( + max_digits=10, + decimal_places=2 + ) + + text = models.TextField( + help_text="Description of what this credit note covers" + ) + pdf = models.FileField( null=True, blank=True, upload_to='creditnotes/' ) + user = models.ForeignKey( 'auth.User', verbose_name=_('User'), - help_text=_('The user this credit note belongs to.'), + help_text=_('The user this credit note belongs to, if any.'), related_name='creditnotes', + null=True, + blank=True ) + + customer = models.TextField( + help_text="Customer info if no user is selected", + blank=True, + default='', + ) + paid = models.BooleanField( verbose_name=_('Paid?'), help_text=_('Whether the amount in this creditnote has been paid back to the customer.'), default=False, ) + sent_to_customer = models.BooleanField(default=False) + def clean(self): + errors = [] + if self.user and self.customer: + msg = "Customer info should be blank if a user is selected." + errors.append(ValidationError({'user', msg})) + errors.append(ValidationError({'customer', msg})) + if not self.user and not self.customer: + msg = "Either pick a user or fill in Customer info" + errors.append(ValidationError({'user', msg})) + errors.append(ValidationError({'customer', msg})) + if errors: + raise ValidationError(errors) + def __str__(self): - return 'creditnote#%s - %s DKK (sent to %s: %s)' % ( - self.id, - self.amount, - self.user.email, - self.sent_to_customer, - ) + if self.user: + return 'creditnoote#%s - %s DKK (customer: user %s)' % ( + self.id, + self.amount, + self.user.email, + ) + else: + return 'creditnoote#%s - %s DKK (customer: user %s)' % ( + self.id, + self.amount, + self.customer, + ) @property def vat(self): diff --git a/src/shop/templates/pdf/creditnote.html b/src/shop/templates/pdf/creditnote.html index 9d15e946..b8e27423 100644 --- a/src/shop/templates/pdf/creditnote.html +++ b/src/shop/templates/pdf/creditnote.html @@ -13,7 +13,7 @@ -