Commit 93e008bd authored by vindarel's avatar vindarel

Restocking: actually move the cards

for #168
parent cdfd8b8f
......@@ -1146,7 +1146,7 @@ class Card(TimeStampedModel):
Basket.add_to_auto_command(card)
# Possibly add to the restocking list.
if card.quantity_selling_places() <= 0 and card.quantity_reserve() >= 1:
if card.quantity_to_restock() > 0:
Restocking.add_card(card)
return msgs.status, msgs.msgs
......@@ -1555,12 +1555,13 @@ class Card(TimeStampedModel):
def quantity_to_restock(self):
"""
Return the quantity that ideally we want to move from the stock place to the selling place.
Return the quantity that we can move from the stock
place to the selling place.
"""
res = 1 - self.quantity_selling_places()
# Shall we check that this number is not superior to the
# quantity available in the stock place?
# This should not happen since we re-filter the restocking list.
if self.quantity <= 0:
return 0
ideal_quantity = 1 - self.quantity_selling_places()
res = min(max(0, ideal_quantity), self.quantity_reserve())
return res
@property
......@@ -2398,16 +2399,18 @@ class Restocking(models.Model):
def add_card(card):
try:
restock = Restocking.get_or_create()
copies, created = restock.restockingcopies_set.get_or_create(card=card)
copies.quantity += 1
copies.save()
copy, created = restock.restockingcopies_set.get_or_create(card=card)
qty_to_restock = copy.card.quantity_to_restock()
if qty_to_restock > 0:
copy.quantity += qty_to_restock
copy.save()
except Exception, e:
log.error(u"Error while adding '%s' to the list of restocking" % (card.title))
log.error(e)
return 0
return copies.quantity
return qty_to_restock
@staticmethod
def remove_card(card):
......@@ -2431,7 +2434,7 @@ class Restocking(models.Model):
@staticmethod
def nb_ongoing():
"""
Total quantity of cards in the restocking list.
Total quantity of cards in the restocking list (not the number of copies to move).
Return: int (None on error)
"""
try:
......@@ -2443,7 +2446,7 @@ class Restocking(models.Model):
@staticmethod
def validate(cards=None):
"""
Validate the current list: create a movement.
Validate the current list: move the cards and create a movement.
If a list of cards is given, move only these ones and leave the others.
"""
restock = Restocking.get_or_create()
......@@ -2459,16 +2462,14 @@ class Restocking(models.Model):
for card in cards:
copy = restock.restockingcopies_set.filter(card=card).first()
# filter VS get: when we re-run the script (manual
# testing, it is possible that a card has already been
# removed from the list.
# testing), it is possible that a card has already been
# removed from the list and its movement created.
if copy:
copy.delete()
origin.move(dest, card, copy.quantity)
# So, don't create the movement twice.
# We currently can not edit the moved quantity.
# TODO: but it can differ on the page :S
mvt = history.InternalMovement(origin=origin, dest=dest, card=card, nb=1)
mvt.save()
return True
......
......@@ -1161,16 +1161,28 @@ class TestSells(TestCase):
# We have 1 copy in the shelf, 1 copy in the reserve.
self.reserve.add_copy(self.autobio, nb=1)
# Sell 1:
# Sell 1.
Sell.sell_card(self.autobio)
# When the cards reaches 0 in stock, we should see it in the restocking list.
self.assertEqual(Restocking.quantities_total(), 1)
# If we sell it again, its quantity becomes 0, so
# we shouldn't see it in the restocking list again.
# When the cards reach 0 in the selling place and they are
# available in stock, we should see it in the restocking list.
self.assertEqual(Restocking.nb_ongoing(), 1)
self.assertEqual(restock.restockingcopies_set.first().quantity, 1)
# Sell 2. Sell it again.
# We don't have enough in stock to add 1 quantity in the restocking list.
Sell.sell_card(self.autobio)
self.assertEqual(Restocking.quantities_total(), 1)
# We had 2 sells, the remaining quantity to -1.
# We'd like to move another one, but we don't have enough in stock.
self.assertEqual(restock.restockingcopies_set.first().quantity, 1)
self.assertEqual(self.autobio.quantity_compute(), 0)
# Validate. Move the copies and create a movement.
self.assertEqual(-1, self.place.quantity_of(self.autobio))
self.assertEqual(1, self.reserve.quantity_of(self.autobio))
restock.validate()
self.assertEqual(0, self.place.quantity_of(self.autobio))
self.assertEqual(0, self.reserve.quantity_of(self.autobio))
class TestSellSearch(TestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment