diff --git a/stregsystem/business/__init__.py b/stregsystem/business/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/stregsystem/business/reimburse.py b/stregsystem/business/reimburse.py new file mode 100644 index 00000000..1e250aa5 --- /dev/null +++ b/stregsystem/business/reimburse.py @@ -0,0 +1,26 @@ +from django.db import transaction + +from stregsystem.models import Reimbursement, Sale, SaleNotFoundError, ReimbursementTransaction + + +@transaction.atomic +def reimburse_sale(sale_id): + """ + 1. Create payment to pay back the member + 2. Create reimbursement object referencing the payment and the product + 3. Adjust the inventory + 4. delete the sale + """ + sale: Sale = Sale.objects.get(id=sale_id) + if not sale: + raise SaleNotFoundError() + product = sale.product + product.quantity = product.quantity + 1 + product.save() + sale.member.fulfill(ReimbursementTransaction(amount=sale.price)) + sale.member.save() + sale.save() + + Sale.delete(sale) + reimbursement = Reimbursement(product=product, amount=sale.price, member=sale.member) + reimbursement.save() diff --git a/stregsystem/migrations/0018_reimbursement.py b/stregsystem/migrations/0018_reimbursement.py new file mode 100644 index 00000000..78e75246 --- /dev/null +++ b/stregsystem/migrations/0018_reimbursement.py @@ -0,0 +1,23 @@ +# Generated by Django 2.2.28 on 2024-04-14 13:32 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('stregsystem', '0017_auto_20220511_1738'), + ] + + operations = [ + migrations.CreateModel( + name='Reimbursement', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('amount', models.IntegerField()), + ('member', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='stregsystem.Member')), + ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='stregsystem.Product')), + ], + ), + ] diff --git a/stregsystem/models.py b/stregsystem/models.py index afc9d428..6e8f419b 100644 --- a/stregsystem/models.py +++ b/stregsystem/models.py @@ -1,5 +1,5 @@ import datetime -import re +from datetime import timedelta from collections import Counter from email.utils import parseaddr @@ -22,6 +22,8 @@ ) from stregsystem.mail import send_payment_mail +MAX_REIMBUSEMENT_HOURS = 12 + def price_display(value): return money(value) + " kr." @@ -43,6 +45,19 @@ class NoMoreInventoryError(Exception): pass +class SaleNotFoundError(Exception): + pass + + +class MoneyTransactionError(Exception): + pass + + +class ReimbursementError(Exception): + def __init__(self, message): + super({'message': message}) + + # Create your models here. @@ -141,6 +156,17 @@ def execute(self): self.member.save() +class ReimbursementTransaction(MoneyTransaction): + def change(self): + """ + Returns the change to the users account + caused by fulfilling this transaction. + """ + if self.amount <= 0: + raise ReimbursementError("Cannot perform negative reimbursement") + return self.amount + + class GetTransaction(MoneyTransaction): # The change to the users account def change(self): @@ -638,6 +664,9 @@ def price_display(self): # XXX - django bug - kan ikke vaelge mellem desc og asc i admin, som ved normalt felt price_display.admin_order_field = 'price' + def is_reimbursable(self): + return timedelta(hours=MAX_REIMBUSEMENT_HOURS) >= timezone.now() - self.timestamp + @deprecated def __unicode__(self): return self.__str__() @@ -673,3 +702,10 @@ def __unicode__(self): def __str__(self): return self.title + " -- " + str(self.pub_date) + + +class Reimbursement(models.Model): + product = models.ForeignKey(Product, on_delete=models.CASCADE) + member = models.ForeignKey(Member, on_delete=models.CASCADE) + amount = models.IntegerField() + models.DateTimeField(auto_now_add=True) diff --git a/stregsystem/templates/stregsystem/menu_userinfo.html b/stregsystem/templates/stregsystem/menu_userinfo.html index 44ac98bc..c50cfbf9 100644 --- a/stregsystem/templates/stregsystem/menu_userinfo.html +++ b/stregsystem/templates/stregsystem/menu_userinfo.html @@ -38,14 +38,25 @@