Improve tender support; assign user for each batch row added

This commit is contained in:
Lance Edgar 2023-09-27 17:16:02 -05:00
parent 4058f2df67
commit 114c6e4dd6

View file

@ -82,11 +82,12 @@ class POSView(WuttaView):
def set_customer(self, customer, batch=None): def set_customer(self, customer, batch=None):
session = self.app.get_session(customer) session = self.app.get_session(customer)
user = self.get_current_user(session)
if not batch: if not batch:
batch = self.get_current_batch(session) batch = self.get_current_batch(session, user=user)
handler = self.get_batch_handler() handler = self.get_batch_handler()
handler.set_customer(batch, customer) handler.set_customer(batch, customer, user=user)
self.page.session.set('txn_display', handler.get_screen_txn_display(batch)) self.page.session.set('txn_display', handler.get_screen_txn_display(batch))
self.page.session.set('cust_uuid', customer.uuid) self.page.session.set('cust_uuid', customer.uuid)
@ -114,7 +115,8 @@ class POSView(WuttaView):
def attempt_add_product(self, uuid=None, record_badscan=False): def attempt_add_product(self, uuid=None, record_badscan=False):
session = self.app.make_session() session = self.app.make_session()
handler = self.get_batch_handler() handler = self.get_batch_handler()
batch = self.get_current_batch(session) user = self.get_current_user(session)
batch = self.get_current_batch(session, user=user)
entry = self.main_input.value entry = self.main_input.value
quantity = 1 quantity = 1
@ -122,11 +124,15 @@ class POSView(WuttaView):
quantity = self.set_quantity.data quantity = self.set_quantity.data
product = None product = None
item_entry = entry
if uuid: if uuid:
product = session.get(self.model.Product, uuid) product = session.get(self.model.Product, uuid)
assert product assert product
key = self.app.get_product_key_field()
item_entry = str(getattr(product, key) or '') or uuid
row = handler.process_entry(batch, product or entry, quantity=quantity) row = handler.process_entry(batch, product or entry, quantity=quantity,
item_entry=item_entry, user=user)
if row: if row:
session.commit() session.commit()
@ -138,7 +144,7 @@ class POSView(WuttaView):
else: else:
if record_badscan: if record_badscan:
handler.record_badscan(batch, entry, quantity=quantity) handler.record_badscan(batch, entry, quantity=quantity, user=user)
self.page.snack_bar = ft.SnackBar(ft.Text(f"PRODUCT NOT FOUND: {entry}", self.page.snack_bar = ft.SnackBar(ft.Text(f"PRODUCT NOT FOUND: {entry}",
color='black', color='black',
@ -364,9 +370,10 @@ class POSView(WuttaView):
def remove(e): def remove(e):
session = self.app.make_session() session = self.app.make_session()
user = self.get_current_user(session)
handler = self.get_batch_handler() handler = self.get_batch_handler()
batch = self.get_current_batch(session) batch = self.get_current_batch(session, user=user)
handler.set_customer(batch, None) handler.set_customer(batch, None, user=user)
session.commit() session.commit()
session.close() session.close()
@ -462,6 +469,13 @@ class POSView(WuttaView):
def build_controls(self): def build_controls(self):
session = self.app.make_session()
handler = self.get_batch_handler()
self.tender_cash = handler.get_tender(session, 'cash')
self.tender_check = handler.get_tender(session, 'check')
session.expunge_all()
session.close()
self.main_input = ft.TextField(on_submit=self.main_submit, self.main_input = ft.TextField(on_submit=self.main_submit,
text_size=24, text_size=24,
text_style=ft.TextStyle(weight=ft.FontWeight.BOLD), text_style=ft.TextStyle(weight=ft.FontWeight.BOLD),
@ -685,24 +699,37 @@ class POSView(WuttaView):
context_button_width = context_button_height * 2 context_button_width = context_button_height * 2
context_font_size = tenkey_font_size context_font_size = tenkey_font_size
def context_button(text, on_click=None): def context_button(text, on_click=None, data=None):
return ft.Container(content=ft.Text(text, size=context_font_size, return ft.Container(content=ft.Text(text, size=context_font_size,
weight=ft.FontWeight.BOLD), weight=ft.FontWeight.BOLD),
height=context_button_height, height=context_button_height,
width=context_button_width, width=context_button_width,
on_click=on_click, on_click=on_click,
data=data,
alignment=ft.alignment.center, alignment=ft.alignment.center,
border=ft.border.all(1, 'black'), border=ft.border.all(1, 'black'),
border_radius=ft.border_radius.all(5), border_radius=ft.border_radius.all(5),
bgcolor='orange') bgcolor='orange')
cash = {
'tender_code': self.tender_cash.code if self.tender_cash else 'CA',
'tender_name': self.tender_cash.name if self.tender_cash else "Cash",
}
check = {
'tender_code': self.tender_check.code if self.tender_check else 'CK',
'tender_name': self.tender_check.name if self.tender_check else "Check",
}
self.context_menu = ft.Container( self.context_menu = ft.Container(
content=ft.Column( content=ft.Column(
[ [
ft.Row( ft.Row(
[ [
context_button("CASH", on_click=self.tender_click), context_button(cash['tender_name'], data=cash,
context_button("CHECK", on_click=self.tender_click), on_click=self.tender_click),
context_button(check['tender_name'], data=check,
on_click=self.tender_click),
], ],
spacing=0, spacing=0,
), ),
@ -759,11 +786,16 @@ class POSView(WuttaView):
kwargs.setdefault('size', 24) kwargs.setdefault('size', 24)
return ft.Text(*args, **kwargs) return ft.Text(*args, **kwargs)
def get_current_user(self, session):
uuid = self.page.session.get('user_uuid')
if uuid:
return session.get(self.model.User, uuid)
def get_current_batch(self, session, user=None, create=True): def get_current_batch(self, session, user=None, create=True):
handler = self.get_batch_handler() handler = self.get_batch_handler()
if not user: if not user:
user = session.get(self.model.User, self.page.session.get('user_uuid')) user = self.get_current_user(session)
batch, created = handler.get_current_batch(user, create=create, return_created=True) batch, created = handler.get_current_batch(user, create=create, return_created=True)
@ -846,10 +878,9 @@ class POSView(WuttaView):
def confirm(e): def confirm(e):
dlg.open = False dlg.open = False
model = self.model
session = self.app.make_session() session = self.app.make_session()
handler = self.get_batch_handler() handler = self.get_batch_handler()
user = session.get(model.User, self.page.session.get('user_uuid')) user = self.get_current_user(session)
batch = handler.get_current_batch(user, create=True) batch = handler.get_current_batch(user, create=True)
# void current batch # void current batch
@ -918,20 +949,50 @@ class POSView(WuttaView):
self.page.update() self.page.update()
def tender_click(self, e): def tender_click(self, e):
model = self.model
session = self.app.make_session() session = self.app.make_session()
handler = self.get_batch_handler()
user = self.get_current_user(session)
batch = self.get_current_batch(session, user=user, create=False)
if not batch:
session.close()
self.page.snack_bar = ft.SnackBar(ft.Text(f"NO TRANSACTION",
color='black',
size=20,
weight=ft.FontWeight.BOLD),
bgcolor='yellow',
duration=1500)
self.page.snack_bar.open = True
self.reset()
return
if not batch.get_balance():
session.close()
self.page.snack_bar = ft.SnackBar(ft.Text(f"NO SALES",
color='black',
size=20,
weight=ft.FontWeight.BOLD),
bgcolor='yellow',
duration=1500)
self.page.snack_bar.open = True
self.reset()
return
# tender / execute batch # tender / execute batch
tender = e.control.content.value code = e.control.data['tender_code']
tender = handler.get_tender(session, code)
amount = -batch.get_balance()
try: try:
handler = self.get_batch_handler()
user = session.get(model.User, self.page.session.get('user_uuid')) # TODO: should prompt user for amount
batch = handler.get_current_batch(user) # TODO: also should display the tender item somehow,
handler.tender_and_execute(batch, user, tender) # unless that finalized txn in which case..??
row = handler.apply_tender(batch, user, tender or code, amount)
except Exception as error: except Exception as error:
session.rollback() session.rollback()
log.exception("failed to execute batch") log.exception("failed to apply tender '%s' for %s in batch %s",
code, amount, batch.id_str)
self.page.snack_bar = ft.SnackBar(ft.Text(f"ERROR: {error}", self.page.snack_bar = ft.SnackBar(ft.Text(f"ERROR: {error}",
color='black', color='black',
size=20, size=20,
@ -942,10 +1003,13 @@ class POSView(WuttaView):
else: else:
session.commit() session.commit()
self.clear_all()
finally: finally:
session.close() session.close()
# TODO: maybe shouldn't clear screen so immediately?
if batch.executed:
self.clear_all()
self.reset() self.reset()
def clear_all(self): def clear_all(self):