-
Notifications
You must be signed in to change notification settings - Fork 52
Reimbursement of latest bought items #424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0bd4e36
9ffb61b
6f996a0
f3324e7
e03c20b
e6c4cd5
c6e3f47
fde8fca
d627e7d
37c032c
f4a9ce3
e90ec5e
9c58a4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| 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() | ||
| 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')), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| ], | ||
| ), | ||
| ] | ||
There was a problem hiding this comment.
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()onproductafter 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.