Skip to content

Commit

Permalink
Ensure stocklines.selectline passes instance in current ORM session
Browse files Browse the repository at this point in the history
  • Loading branch information
sde1000 committed Jan 26, 2024
1 parent f7d6959 commit 6fe46a7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
2 changes: 0 additions & 2 deletions quicktill/recordwaste.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def __init__(self):

def line_selected(self, kb):
self.dismiss()
td.s.add(kb)
stockline_chosen(kb.stockline)

def keypress(self, k):
Expand All @@ -58,7 +57,6 @@ def keypress(self, k):


def stockline_chosen(stockline):
td.s.add(stockline)
if stockline.linetype == "display" \
or stockline.linetype == "continuous":
record_line_waste(stockline)
Expand Down
53 changes: 33 additions & 20 deletions quicktill/stocklines.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ def keypress(self, k):
@user.permission_required('return-stock',
"Return items on display stocklines to stock")
def return_stock(stockline):
td.s.add(stockline)
rsl = stockline.calculate_restock(target=0)
if not rsl:
ui.infopopup(["The till has no record of stock on display for "
Expand Down Expand Up @@ -185,17 +184,19 @@ def validate_location(s, c):


@user.permission_required('create-stockline', "Create a new stock line")
def create(func):
menu = [
("1", ui.lrline(
def create(func, linetypes=["regular", "display", "continuous"]):
menu = []
if "regular" in linetypes:
menu.append(("1", ui.lrline(
"Regular.\n\nThese stock lines can have at most one stock item "
"on sale at any one time. Finishing that stock item and "
"putting another item on sale are done explicitly using "
"the 'Use Stock' button. Typically used where it's obvious "
"to the person serving when a stock item comes to an end: "
"casks/kegs, bottles of spirits, snacks from cards or boxes, "
"and so on.\n"), _create_stockline_popup, ('regular', func)),
("2", ui.lrline(
"and so on.\n"), _create_stockline_popup, ('regular', func)))
if "display" in linetypes:
menu.append(("2", ui.lrline(
"Display.\n\nThese can have several stock items, all of the "
"same type, on sale at once. The number of items 'on display' "
"and 'in stock' are tracked separately; stock is only sold "
Expand All @@ -206,16 +207,16 @@ def create(func):
"fridges. May also be used for snacks where the display "
"space is small and the boxes the snacks are supplied in "
"won't fit. Can't be used where items are not sold in "
"whole numbers.\n"), _create_stockline_popup, ('display', func)),
("3", ui.lrline(
"whole numbers.\n"), _create_stockline_popup, ('display', func)))
if "continuous" in linetypes:
menu.append(("3", ui.lrline(
"Continuous.\n\nThese are used when selling variable sized "
"measures where it's not obvious to the person serving where "
"one stock item ends and another starts. Typically used for "
"wine from bottles and soft drinks from cartons: if a glass "
"is filled from more than one bottle it won't be obvious "
"whether the two bottles were originally in the same case."),
_create_stockline_popup, ('continuous', func)),
]
_create_stockline_popup, ('continuous', func)))
ui.keymenu(menu, blurb="Choose the type of stockline to create:",
title="Create Stock Line", blank_line_between_items=True)

Expand Down Expand Up @@ -568,7 +569,6 @@ class note(user.permission_checked, ui.dismisspopup):
'stockline-note', 'Change the note on a stock line')

def __init__(self, stockline):
td.s.add(stockline)
self.stocklineid = stockline.id
super().__init__(7, 60, title=f"Set the note on {stockline.name}",
colour=ui.colour_input)
Expand Down Expand Up @@ -625,21 +625,27 @@ def keypress(self, k):
class selectline(ui.listpopup):
"""Pop-up menu of stocklines
A pop-up menu of stocklines, sorted by location and name.
Optionally can remove stocklines that have no capacities.
Stocklines with key bindings can be selected through that binding.
A pop-up menu of stocklines, sorted by location and name and
filtered by stockline type. Stocklines with key or barcode
bindings can be selected through that binding if they match the
filter.
Calls func with the StockLine instance as the argument. The
instance is guaranteed to be in the current ORM session.
Optional arguments:
blurb - text for the top of the window
linetypes - list of permissible line types
create_new - allow a new stockline to be created
select_none - a string for a menu item which will result in a call
to func(None)
"""
def __init__(self, func, title="Stock Lines", blurb=None,
linetypes=["regular", "display", "continuous"],
keymap={}, create_new=False, select_none=None):
self.func = func
self.linetypes = tuple(linetypes)
q = td.s.query(StockLine).order_by(StockLine.location,
StockLine.name)
q = q.filter(StockLine.linetype.in_(linetypes))
Expand All @@ -648,7 +654,7 @@ def __init__(self, func, title="Stock Lines", blurb=None,
self.sl = [f(x.name, x.location,
x.department if x.department else "Any",
x.typeinfo,
userdata=x)
userdata=x.id)
for x in stocklines]
self.create_new = create_new
if create_new:
Expand All @@ -661,9 +667,14 @@ def __init__(self, func, title="Stock Lines", blurb=None,
super().__init__(self.sl, title=title, header=hl, keymap=keymap)

def line_selected(self, kb):
self.dismiss()
td.s.add(kb)
self.func(kb.stockline)
# kb is either a KeyboardBinding or a Barcode; both of these
# have stockline attributes

# kb is guaranteed to be in the current ORM session when we
# are called.
if kb.stockline.linetype in self.linetypes:
self.dismiss()
self.func(kb.stockline)

def keypress(self, k):
log.debug("selectline keypress %s", k)
Expand All @@ -678,10 +689,12 @@ def keypress(self, k):
self.dismiss()
line = self.sl[self.s.cursor]
if line.userdata:
self.func(line.userdata)
stockline = td.s.query(StockLine).get(line.userdata)
if stockline:
self.func(stockline)
else:
if self.create_new:
create(self.func)
create(self.func, linetypes=self.linetypes)
else:
self.func(None)
else:
Expand Down
2 changes: 0 additions & 2 deletions quicktill/usestock.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def __init__(self):

def line_chosen(self, kb):
self.dismiss()
td.s.add(kb)
line_chosen(kb.stockline)

def keypress(self, k):
Expand All @@ -57,7 +56,6 @@ def keypress(self, k):


def line_chosen(line):
td.s.add(line)
sl = line.stockonsale
if line.linetype == "regular":
# We sell directly from a single stock item
Expand Down

0 comments on commit 6fe46a7

Please sign in to comment.