Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
26 changes: 26 additions & 0 deletions stregsystem/business/reimburse.py
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missing a .save() on product after updating the quantity.
Unless, the Django ORM does it as part of saving the new reimbursement,
in which case you don't need sale.member.save() on line 21.

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()
23 changes: 23 additions & 0 deletions stregsystem/migrations/0018_reimbursement.py
Original file line number Diff line number Diff line change
@@ -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')),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is a good idea for the reimbursement to be removed when a product is deleted (however unlikely the scenario may be). It gives an incorrect view of the customers history.
SET_NULL seems more appropriate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With that said, it is consistent with how sales are handled, so I guess it is fine.

],
),
]
38 changes: 37 additions & 1 deletion stregsystem/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime
import re
from datetime import timedelta
from collections import Counter
from email.utils import parseaddr

Expand All @@ -22,6 +22,8 @@
)
from stregsystem.mail import send_payment_mail

MAX_REIMBUSEMENT_HOURS = 12


def price_display(value):
return money(value) + " kr."
Expand All @@ -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.


Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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__()
Expand Down Expand Up @@ -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)
15 changes: 13 additions & 2 deletions stregsystem/templates/stregsystem/menu_userinfo.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,25 @@
<tr>
<th align=left>Dato og tidspunkt</th>
<th align=left>Produkt</th>
<th align=left>Pris</th>
<th align=left>Pris</th>
<th align=left>Annuller</th>
</tr>
{% autoescape off %}
{% for sale in last_sale_list %}

<tr>
<td>{{sale.timestamp}}</td>
<td>{{sale.product.name}}</td>
<td align="right">{{sale.price|money}}</td>
<td align="right">{{sale.price|money}}</td>
<td>
{% if sale.is_reimbursable %}
<form action="" method="post">
{% csrf_token %}
<input type="hidden" name="sale_id" value="{{ sale.pk }}"/>
<button name="action" value="reimburse" onclick="return confirm('Er du sikker på at du vil refundere salget på {{sale.product.name}} til en værdi af {{sale.price|money}} stregdollors?')">Refunder</button>
</form>
{% endif %}
</td>
</tr>
{% endfor %}
{% endautoescape %}
Expand Down
10 changes: 9 additions & 1 deletion stregsystem/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
)

from .booze import ballmer_peak
from .business.reimburse import reimburse_sale
from .caffeine import caffeine_mg_to_coffee_cups
from .forms import MobilePayToolForm, QRPaymentForm, PurchaseForm, RankingDateForm

Expand Down Expand Up @@ -231,7 +232,12 @@ def usermenu(request, room, member, bought, from_sale=False):
def menu_userinfo(request, room_id, member_id):
room = Room.objects.get(pk=room_id)
news = __get_news()

if request.method == 'POST' and request.POST.get('action') is not None and request.POST['action'] == 'reimburse':
reimburse_sale(int(request.POST['sale_id']))

member = Member.objects.get(pk=member_id, active=True)

stats = Sale.objects.filter(member_id=member_id).aggregate(
total_amount=Sum('price'), total_purchases=Count('timestamp')
)
Expand All @@ -245,7 +251,9 @@ def menu_userinfo(request, room_id, member_id):
negative_balance = member.balance < 0
stregforbud = member.has_stregforbud()

return render(request, 'stregsystem/menu_userinfo.html', locals())
return render(
request, 'stregsystem/menu_userinfo.html', locals()
) # this is very bad for refactoring. Idk what variables are used in the templates.


def send_userdata(request, room_id, member_id):
Expand Down