Skip to content
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

add AbstractOrder.return_order #187

Merged
merged 3 commits into from
Apr 5, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
django-plans changelog
======================

1.1.0 (unreleased)
------------------
* Add `AbstractOrder.return_order()`

1.0.6
-----
* Simplify the query generated by get_initial_number
Expand Down
1 change: 1 addition & 0 deletions demo/example/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
framework.

"""

Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably unnecessary modification of this file.

Copy link
Contributor Author

@radekholy24 radekholy24 Apr 5, 2024

Choose a reason for hiding this comment

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

Yeah, I think so too, but it won't pass Black otherwise... 🤦
image

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK

import os

from django.core.wsgi import get_wsgi_application
Expand Down
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Contributors:
* Victor Safronovich <[email protected]>
* Dominik Kozaczko <http://dominik.kozaczko.info>
* Petr Dlouhý <[email protected]>
* BlenderKit <[email protected]>

Source code:
https://github.com/cypreess/django-plans
Expand Down
10 changes: 9 additions & 1 deletion plans/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ def make_order_completed(modeladmin, request, queryset):
make_order_completed.short_description = _("Make selected orders completed")


def make_order_returned(modeladmin, request, queryset):
for order in queryset:
order.return_order()


make_order_returned.short_description = _("Make selected orders returned")


def make_order_invoice(modeladmin, request, queryset):
for order in queryset:
if (
Expand Down Expand Up @@ -192,7 +200,7 @@ class OrderAdmin(admin.ModelAdmin):
)
readonly_fields = ("created", "updated_at")
list_display_links = list_display
actions = [make_order_completed, make_order_invoice]
actions = [make_order_completed, make_order_returned, make_order_invoice]
inlines = (InvoiceInline,)

def queryset(self, request):
Expand Down
45 changes: 45 additions & 0 deletions plans/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ def get_plan_extended_until(self, plan, pricing):
return self.expire
return self.get_plan_extended_from(plan) + timedelta(days=pricing.period)

def get_plan_reduced_until(self, pricing):
if self.expire is None:
return self.expire
if pricing is None:
return self.expire
return self.expire - timedelta(days=pricing.period)

def plan_autorenew_at(self):
"""
Helper function which calculates when the plan autorenewal will occur
Expand Down Expand Up @@ -430,6 +437,16 @@ def extend_account(self, plan, pricing):

return status

def reduce_account(self, pricing):
"""
Manages reducing account after returning an order
:param pricing: if pricing is None then nothing is changed
:return:
"""
if pricing is not None:
self.expire = self.get_plan_reduced_until(pricing)
self.save()

def expire_account(self):
"""manages account expiration"""

Expand Down Expand Up @@ -831,6 +848,34 @@ def complete_order(self):
else:
return False

def return_order(self):
if self.status != self.STATUS.RETURNED:
if self.status == self.STATUS.COMPLETED:
if self.pricing is not None:
extended_from = self.plan_extended_from
if extended_from is None:
extended_from = self.completed
# Should never happen, but make sure we reduce for the same number of days as we extended.
if (
self.plan_extended_until is None
or extended_from is None
or self.plan_extended_until - extended_from
!= timedelta(days=self.pricing.period)
):
raise ValueError(
f"Invalid order state: completed={self.completed}, "
f"plan_extended_from={self.plan_extended_from}, "
f"plan_extended_until={self.plan_extended_until}, "
f"pricing.period={self.pricing.period}"
)
self.user.userplan.reduce_account(self.pricing)
elif self.status != self.STATUS.NOT_VALID:
raise ValueError(
f"Cannot return order with status other than COMPLETED and NOT_VALID: {self.status}"
)
self.status = self.STATUS.RETURNED
self.save()

def get_invoices_proforma(self):
return AbstractInvoice.get_concrete_model().proforma.filter(order=self)

Expand Down
Loading
Loading