Skip to content

Commit

Permalink
Allow to cancel order when deleting last line
Browse files Browse the repository at this point in the history
  • Loading branch information
artursmet committed Feb 17, 2016
1 parent 6d3182a commit 7e04479
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
35 changes: 7 additions & 28 deletions saleor/dashboard/order/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,34 +128,12 @@ def __init__(self, *args, **kwargs):
self.item = kwargs.pop('item')
super(CancelItemsForm, self).__init__(*args, **kwargs)

def clean(self):
data = super(CancelItemsForm, self).clean()
# Check if this line is not the only line in this order
order = self.item.delivery_group.order
other_group_items = self.item.delivery_group.items.exclude(
pk=self.item.pk)
other_groups = order.groups.exclude(pk=self.item.delivery_group.pk)
if not other_group_items and not other_groups:
# There is only one group in this order and user wants
# to cancel this one line
# He should cancel whole order instead.
raise forms.ValidationError(
_('This is the only line in Order. '
'You should cancel whole order instead.'))
return data

def cancel_item(self):
delivery_group = self.item.delivery_group
Stock.objects.deallocate_stock(self.item.stock, self.item.quantity)
self.item.delete()
delivery_group = DeliveryGroup.objects.get(pk=delivery_group.pk)
order = delivery_group.order
if not delivery_group.items.exists():
# Should we cancel order after dropping this group?
if order.groups.exclude(pk=delivery_group.pk).exists():
# we have another delivery groups as well
delivery_group.delete()
Order.objects.recalculate_order(order)
if self.item.stock:
Stock.objects.deallocate_stock(self.item.stock, self.item.quantity)
order = self.item.delivery_group.order
OrderedItem.objects.remove_empty_groups(self.item, force=True)
Order.objects.recalculate_order(order)


class ChangeQuantityForm(forms.ModelForm):
Expand Down Expand Up @@ -227,7 +205,8 @@ def __init__(self, *args, **kwargs):

def cancel_group(self):
for line in self.delivery_group:
Stock.objects.deallocate_stock(line.stock, line.quantity)
if line.stock:
Stock.objects.deallocate_stock(line.stock, line.quantity)
self.delivery_group.status = Status.CANCELLED
self.delivery_group.save()
other_groups = self.delivery_group.order.groups.all()
Expand Down
22 changes: 16 additions & 6 deletions saleor/order/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ def recalculate_order(self, order):
total_net = sum(p.net for p in prices)
total_gross = sum(p.gross for p in prices)
shipping = [group.shipping_price for group in order]
total_shipping = sum(shipping[1:], shipping[0])
if shipping:
total_shipping = sum(shipping[1:], shipping[0])
else:
total_shipping = Price(0, currency=settings.DEFAULT_CURRENCY)
total = Price(net=total_net, gross=total_gross,
currency=settings.DEFAULT_CURRENCY)
total += total_shipping
Expand Down Expand Up @@ -180,11 +183,15 @@ def total(self, price):
self.total_tax = Price(price.tax, currency=price.currency)

def get_subtotal_without_voucher(self):
return super(Order, self).get_total()
if self.get_items():
return super(Order, self).get_total()
return Price(net=0, currency=settings.DEFAULT_CURRENCY)

def get_total_shipping(self):
costs = [group.shipping_price for group in self]
return sum(costs[1:], costs[0])
if costs:
return sum(costs[1:], costs[0])
return Price(net=0, currency=settings.DEFAULT_CURRENCY)

def can_cancel(self):
return self.status not in {Status.CANCELLED, Status.SHIPPED}
Expand Down Expand Up @@ -263,8 +270,6 @@ def can_cancel(self):
class OrderedItemManager(models.Manager):

def move_to_group(self, item, target_group, quantity):
source_group = item.delivery_group
order = target_group.order
try:
target_item = target_group.items.get(
product=item.product, product_name=item.product_name,
Expand All @@ -280,11 +285,16 @@ def move_to_group(self, item, target_group, quantity):
target_item.quantity += quantity
target_item.save()
item.quantity -= quantity
self.remove_empty_groups(item)

def remove_empty_groups(self, item, force=False):
source_group = item.delivery_group
order = source_group.order
if item.quantity:
item.save()
else:
item.delete()
if not source_group.get_total_quantity():
if not source_group.get_total_quantity() or force:
source_group.delete()
if not order.get_items():
order.change_status('cancelled')
Expand Down

0 comments on commit 7e04479

Please sign in to comment.