Commit 1618928e authored by vindarel's avatar vindarel

Management command: transform a shelf to a deposit.

for #168

[ci skip]
parent 999f1ee1
......@@ -95,3 +95,22 @@ The script will fail early if any error were to occure.
**warning**: presently the script is not indempotent, meaning if you run it twice, it will add twice the quantities.
If you need more features, get in touch.
Other management commands
-------------------------
Transforming a shelf to a place::
./manage.py shelf2place --shelf=<id> [--can_sell true/false]
Use case: we did the inventory, and it turned out that "mezzanine"
should be the stocking place, not a shelf (so we can know what's in
the reserve).
Transform the shelf into a place of the same name with ALL the cards
from the default place. We don't create a movement object.
Consequently the moved cards won't have an associated shelf
anymore. The shelf object will be deleted, and the
inventories made against it too. You might want to save or
export your DB beforehand.
#!/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2014 - 2019 The Abelujo Developers
# See the COPYRIGHT file at the top-level directory of this distribution
# Abelujo is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Abelujo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Abelujo. If not, see <http://www.gnu.org/licenses/>.
"""
Transform a shelf into a place.
Use case: we did the inventory, and it turned out that "mezzanine"
should be the stocking place, not a shelf (so we can know what's in stock).
Usage:
python manage.py shelf2place --shelf <id> [--can_sell true/false]
"""
from django.core.management.base import BaseCommand
from search.models import Preferences, Place, Shelf, Card
import termcolor
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('--shelf',
dest="shelf",
action="store",
required=True,
help="shelf id.")
# parser.add_argument('--place',
# dest="place",
# action="store",
# help="new place name.")
parser.add_argument('--can_sell',
dest="can_sell",
action="store_true",
help="'true' if the new place is a point of sells, 'false' if it is a stocking place.")
def handle(self, *args, **options):
shelf = Shelf.objects.get(id=options.get('shelf'))
self.stdout.write(""" Transform the shelf {} into a place of the same name with ALL the cards from the default place. We don't create a movement object.
Consequently the moved cards won't have an associated shelf anymore.
The shelf object will be deleted, and the inventories made against it too.
You might want to save or export your DB beforehand.""".format(shelf))
self.stdout.write("Nb of cards to move: {}".format(shelf.cards_qty))
self.stdout.write("The new place will be {}.".format("a selling place" if options.get('can_sell') else "a stocking place"))
confirmation = raw_input("Continue ? [Y/n]")
if confirmation == 'n':
exit(0)
default_place = Preferences.get_default_place()
new_place = Place.objects.create(name=shelf.name, can_sell=options.get('can_sell'))
for card in shelf.cards():
default_place.move(new_place, card, card.quantity, create_movement=False)
card.shelf = None
card.save() # that's a second save :/
# Delete the shelf object (and its associated inventories).
shelf.delete()
self.stdout.write("-------------------")
self.stdout.write("All done.")
......@@ -825,8 +825,7 @@ class Card(TimeStampedModel):
quantity_choice=None,
page=None,
page_size=10):
"""
Search a card (by title, authors' names, ean/isbn).
"""Search a card (by title, authors' names, ean/isbn).
SIZE_LIMIT = 100
......@@ -834,7 +833,9 @@ class Card(TimeStampedModel):
- card_type_id: id referencing to CardType
- with_quantity: if False, avoid this calculation (costly).
- with_quantity (deprecated): if False, avoid this
calculation. update: it *was* costly. The quantity field is now
denormalized.
- quantity_choice: string, one of QUANTITY_CHOICES (negative quantity, between 0 and 3, etc).
......@@ -1637,9 +1638,9 @@ class Place (models.Model):
except Exception, e:
log.error(u"--- error while setting the default place: %s" % (e,))
def move(self, dest, card, nb):
def move(self, dest, card, nb, create_movement=True):
"""Move the given card from this place to "dest" and create an history
movement.
movement (unless create_movement is set to False).
- dest: Place object
- nb: quantity (int)
......@@ -1657,8 +1658,15 @@ class Place (models.Model):
dest_copies.save()
dest_copies.nb += nb
dest_copies.save()
mvt = history.InternalMovement(origin=self, dest=dest, card=card, nb=nb)
mvt.save()
# Delete the intermediate object when nb is 0, so a search
# filter by this place doesn't erronously return (see card.search: we
# filter by the place id present in placecopies).
if dest_copies.nb == 0:
dest_copies.delete()
if create_movement:
mvt = history.InternalMovement(origin=self, dest=dest, card=card, nb=nb)
mvt.save()
return True
except Exception as e:
log.error(e)
......
......@@ -91,6 +91,7 @@ div.ng-cloak(ng-controller="sellController")
)
.col-md-9
p.input-group
form(ng-submit="sellCards()")
input.nullable.form-control(
type='text'
typeahead-loading="loadingCards"
......
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