diff --git a/wuttapos/views/pos.py b/wuttapos/views/pos.py index bddd00a..21932ff 100644 --- a/wuttapos/views/pos.py +++ b/wuttapos/views/pos.py @@ -82,11 +82,12 @@ class POSView(WuttaView): def set_customer(self, customer, batch=None): session = self.app.get_session(customer) + user = self.get_current_user(session) if not batch: - batch = self.get_current_batch(session) + batch = self.get_current_batch(session, user=user) 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('cust_uuid', customer.uuid) @@ -114,7 +115,8 @@ class POSView(WuttaView): def attempt_add_product(self, uuid=None, record_badscan=False): session = self.app.make_session() 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 quantity = 1 @@ -122,11 +124,15 @@ class POSView(WuttaView): quantity = self.set_quantity.data product = None + item_entry = entry if uuid: product = session.get(self.model.Product, uuid) 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: session.commit() @@ -138,7 +144,7 @@ class POSView(WuttaView): else: 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}", color='black', @@ -364,9 +370,10 @@ class POSView(WuttaView): def remove(e): session = self.app.make_session() + user = self.get_current_user(session) handler = self.get_batch_handler() - batch = self.get_current_batch(session) - handler.set_customer(batch, None) + batch = self.get_current_batch(session, user=user) + handler.set_customer(batch, None, user=user) session.commit() session.close() @@ -462,6 +469,13 @@ class POSView(WuttaView): 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, text_size=24, text_style=ft.TextStyle(weight=ft.FontWeight.BOLD), @@ -685,24 +699,37 @@ class POSView(WuttaView): context_button_width = context_button_height * 2 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, weight=ft.FontWeight.BOLD), height=context_button_height, width=context_button_width, on_click=on_click, + data=data, alignment=ft.alignment.center, border=ft.border.all(1, 'black'), border_radius=ft.border_radius.all(5), 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( content=ft.Column( [ ft.Row( [ - context_button("CASH", on_click=self.tender_click), - context_button("CHECK", on_click=self.tender_click), + context_button(cash['tender_name'], data=cash, + on_click=self.tender_click), + context_button(check['tender_name'], data=check, + on_click=self.tender_click), ], spacing=0, ), @@ -759,11 +786,16 @@ class POSView(WuttaView): kwargs.setdefault('size', 24) 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): handler = self.get_batch_handler() 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) @@ -846,10 +878,9 @@ class POSView(WuttaView): def confirm(e): dlg.open = False - model = self.model session = self.app.make_session() 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) # void current batch @@ -918,20 +949,50 @@ class POSView(WuttaView): self.page.update() def tender_click(self, e): - model = self.model 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 = e.control.content.value + code = e.control.data['tender_code'] + tender = handler.get_tender(session, code) + amount = -batch.get_balance() try: - handler = self.get_batch_handler() - user = session.get(model.User, self.page.session.get('user_uuid')) - batch = handler.get_current_batch(user) - handler.tender_and_execute(batch, user, tender) + + # TODO: should prompt user for amount + # TODO: also should display the tender item somehow, + # unless that finalized txn in which case..?? + row = handler.apply_tender(batch, user, tender or code, amount) except Exception as error: 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}", color='black', size=20, @@ -942,10 +1003,13 @@ class POSView(WuttaView): else: session.commit() - self.clear_all() finally: session.close() + # TODO: maybe shouldn't clear screen so immediately? + if batch.executed: + self.clear_all() + self.reset() def clear_all(self):