Add basic "pending product" support for new custorder batch

This commit is contained in:
Lance Edgar 2021-12-22 12:06:00 -06:00
parent 408bffb775
commit c0db03bc28
13 changed files with 844 additions and 234 deletions

View file

@ -74,7 +74,6 @@ class CustomerOrderBatchView(BatchMasterView):
]
row_labels = {
'product_upc': "UPC",
'product_brand': "Brand",
'product_description': "Description",
'product_size': "Size",
@ -83,7 +82,7 @@ class CustomerOrderBatchView(BatchMasterView):
row_grid_columns = [
'sequence',
'product_upc',
'_product_key_',
'product_brand',
'product_description',
'product_size',
@ -94,6 +93,38 @@ class CustomerOrderBatchView(BatchMasterView):
'status_code',
]
product_key_fields = {
'upc': 'product_upc',
'item_id': 'product_item_id',
'scancode': 'product_scancode',
}
row_form_fields = [
'sequence',
'item_entry',
'product',
'pending_product',
'_product_key_',
'product_brand',
'product_description',
'product_size',
'product_weighed',
'product_unit_of_measure',
'department_number',
'department_name',
'product_unit_cost',
'case_quantity',
'unit_price',
'price_needs_confirmation',
'order_quantity',
'order_uom',
'discount_percent',
'total_price',
'paid_amount',
# 'payment_transaction_number',
'status_code',
]
def configure_grid(self, g):
super(CustomerOrderBatchView, self).configure_grid(g)
@ -170,6 +201,8 @@ class CustomerOrderBatchView(BatchMasterView):
def row_grid_extra_class(self, row, i):
if row.status_code == row.STATUS_PRODUCT_NOT_FOUND:
return 'warning'
if row.status_code == row.STATUS_PENDING_PRODUCT:
return 'notice'
def configure_row_grid(self, g):
super(CustomerOrderBatchView, self).configure_row_grid(g)
@ -189,6 +222,9 @@ class CustomerOrderBatchView(BatchMasterView):
super(CustomerOrderBatchView, self).configure_row_form(f)
f.set_renderer('product', self.render_product)
f.set_renderer('pending_product', self.render_pending_product)
f.set_renderer('product_upc', self.render_upc)
f.set_type('case_quantity', 'quantity')
f.set_type('cases_ordered', 'quantity')
@ -197,3 +233,4 @@ class CustomerOrderBatchView(BatchMasterView):
f.set_enum('order_uom', self.enum.UNIT_OF_MEASURE)
f.set_type('unit_price', 'currency')
f.set_type('total_price', 'currency')
f.set_type('paid_amount', 'currency')

View file

@ -90,6 +90,7 @@ class CustomerOrderItemView(MasterView):
'sequence',
'person',
'product',
'pending_product',
'product_brand',
'product_description',
'product_size',
@ -178,6 +179,9 @@ class CustomerOrderItemView(MasterView):
# product
f.set_renderer('product', self.render_product)
# pending_product
f.set_renderer('pending_product', self.render_pending_product)
# product uom
f.set_enum('product_unit_of_measure', self.enum.UNIT_OF_MEASURE)

View file

@ -48,6 +48,7 @@ class CustomerOrderView(MasterView):
model_class = model.CustomerOrder
route_prefix = 'custorders'
editable = False
configurable = True
labels = {
'id': "ID",
@ -96,6 +97,10 @@ class CustomerOrderView(MasterView):
'status_code',
]
def __init__(self, request):
super(CustomerOrderView, self).__init__(request)
self.batch_handler = self.get_batch_handler()
def query(self, session):
return session.query(model.CustomerOrder)\
.options(orm.joinedload(model.CustomerOrder.customer))
@ -241,7 +246,8 @@ class CustomerOrderView(MasterView):
submits the order, at which point the batch is converted to a proper
order.
"""
self.handler = self.get_batch_handler()
# TODO: deprecate / remove this
self.handler = self.batch_handler
batch = self.get_current_batch()
if self.request.method == 'POST':
@ -285,16 +291,30 @@ class CustomerOrderView(MasterView):
context.update({
'batch': batch,
'normalized_batch': self.normalize_batch(batch),
'new_order_requires_customer': self.handler.new_order_requires_customer(),
'product_price_may_be_questionable': self.handler.product_price_may_be_questionable(),
'allow_contact_info_choice': self.handler.allow_contact_info_choice(),
'restrict_contact_info': self.handler.should_restrict_contact_info(),
'new_order_requires_customer': self.batch_handler.new_order_requires_customer(),
'product_key_field': self.rattail_config.product_key(),
'product_price_may_be_questionable': self.batch_handler.product_price_may_be_questionable(),
'allow_contact_info_choice': self.batch_handler.allow_contact_info_choice(),
'allow_contact_info_create': not self.batch_handler.allow_contact_info_creation(),
'order_items': items,
'product_key_label': self.rattail_config.product_key_title(),
'allow_unknown_product': self.batch_handler.allow_unknown_product(),
'department_options': self.get_department_options(),
})
return self.render_to_response(template, context)
def get_department_options(self):
model = self.model
departments = self.Session.query(model.Department)\
.order_by(model.Department.name)\
.all()
options = []
for department in departments:
options.append({'label': department.name,
'value': department.uuid})
return options
def get_current_batch(self):
user = self.request.user
if not user:
@ -311,7 +331,7 @@ class CustomerOrderView(MasterView):
except orm.exc.NoResultFound:
# no batch yet for this user, so make one
batch = self.handler.make_batch(
batch = self.batch_handler.make_batch(
self.Session(), created_by=user,
mode=self.enum.CUSTORDER_BATCH_MODE_CREATING)
self.Session.add(batch)
@ -320,16 +340,7 @@ class CustomerOrderView(MasterView):
return batch
def start_over_entirely(self, batch):
# delete pending customer if present
pending = batch.pending_customer
if pending:
batch.pending_customer = None
self.Session.delete(pending)
# just delete current batch outright
# TODO: should use self.handler.do_delete() instead?
self.Session.delete(batch)
self.batch_handler.do_delete(batch)
self.Session.flush()
# send user back to normal "create" page; a new batch will be generated
@ -339,16 +350,7 @@ class CustomerOrderView(MasterView):
return self.redirect(url)
def delete_batch(self, batch):
# delete pending customer if present
pending = batch.pending_customer
if pending:
batch.pending_customer = None
self.Session.delete(pending)
# just delete current batch outright
# TODO: should use self.handler.do_delete() instead?
self.Session.delete(batch)
self.batch_handler.do_delete(batch)
self.Session.flush()
# set flash msg just to be more obvious
@ -363,19 +365,21 @@ class CustomerOrderView(MasterView):
"""
Customer autocomplete logic, which invokes the handler.
"""
self.handler = self.get_batch_handler()
# TODO: deprecate / remove this
self.handler = self.batch_handler
term = self.request.GET['term']
return self.handler.customer_autocomplete(self.Session(), term,
user=self.request.user)
return self.batch_handler.customer_autocomplete(self.Session(), term,
user=self.request.user)
def person_autocomplete(self):
"""
Person autocomplete logic, which invokes the handler.
"""
self.handler = self.get_batch_handler()
# TODO: deprecate / remove this
self.handler = self.batch_handler
term = self.request.GET['term']
return self.handler.person_autocomplete(self.Session(), term,
user=self.request.user)
return self.batch_handler.person_autocomplete(self.Session(), term,
user=self.request.user)
def get_customer_info(self, batch, data):
uuid = data.get('uuid')
@ -391,7 +395,7 @@ class CustomerOrderView(MasterView):
def info_for_customer(self, batch, data, customer):
# most info comes from handler
info = self.handler.get_customer_info(batch)
info = self.batch_handler.get_customer_info(batch)
# maybe add profile URL
if info['person_uuid']:
@ -407,7 +411,7 @@ class CustomerOrderView(MasterView):
# this will either be a Person or Customer UUID
uuid = data['uuid']
if self.handler.new_order_requires_customer():
if self.batch_handler.new_order_requires_customer():
customer = self.Session.query(model.Customer).get(uuid)
if not customer:
@ -423,7 +427,7 @@ class CustomerOrderView(MasterView):
# invoke handler to assign contact
try:
self.handler.assign_contact(batch, **kwargs)
self.batch_handler.assign_contact(batch, **kwargs)
except ValueError as error:
return {'error': six.text_type(error)}
@ -439,9 +443,9 @@ class CustomerOrderView(MasterView):
'phone_number': batch.phone_number,
'contact_display': batch.contact_name,
'email_address': batch.email_address,
'contact_phones': self.handler.get_contact_phones(batch),
'contact_emails': self.handler.get_contact_emails(batch),
'contact_notes': self.handler.get_contact_notes(batch),
'contact_phones': self.batch_handler.get_contact_phones(batch),
'contact_emails': self.batch_handler.get_contact_emails(batch),
'contact_notes': self.batch_handler.get_contact_notes(batch),
'add_phone_number': bool(batch.get_param('add_phone_number')),
'add_email_address': bool(batch.get_param('add_email_address')),
'contact_profile_url': None,
@ -467,7 +471,7 @@ class CustomerOrderView(MasterView):
# we have a pending customer then it's definitely *not* known,
# but if no pending customer yet then we can still "assume" it
# is known, by default, until user specifies otherwise.
contact = self.handler.get_contact(batch)
contact = self.batch_handler.get_contact(batch)
if contact:
context['contact_is_known'] = True
else:
@ -482,7 +486,7 @@ class CustomerOrderView(MasterView):
return context
def unassign_contact(self, batch, data):
self.handler.unassign_contact(batch)
self.batch_handler.unassign_contact(batch)
self.Session.flush()
context = self.get_context_contact(batch)
context['success'] = True
@ -524,7 +528,8 @@ class CustomerOrderView(MasterView):
def update_pending_customer(self, batch, data):
try:
self.handler.update_pending_customer(batch, self.request.user, data)
self.batch_handler.update_pending_customer(batch, self.request.user,
data)
except Exception as error:
return {'error': six.text_type(error)}
@ -562,11 +567,14 @@ class CustomerOrderView(MasterView):
return self.info_for_product(batch, data, product)
def uom_choices_for_product(self, product):
return self.handler.uom_choices_for_product(product)
return self.batch_handler.uom_choices_for_product(product)
def uom_choices_for_row(self, row):
return self.batch_handler.uom_choices_for_row(row)
def info_for_product(self, batch, data, product):
try:
info = self.handler.get_product_info(batch, product)
info = self.batch_handler.get_product_info(batch, product)
except Exception as error:
return {'error': six.text_type(error)}
else:
@ -574,12 +582,12 @@ class CustomerOrderView(MasterView):
return info
def get_past_items(self, batch, data):
past_products = self.handler.get_past_products(batch)
past_products = self.batch_handler.get_past_products(batch)
past_items = []
for product in past_products:
try:
item = self.handler.get_product_info(batch, product)
item = self.batch_handler.get_product_info(batch, product)
except:
# nb. handler may raise error if product is "unsupported"
pass
@ -613,22 +621,20 @@ class CustomerOrderView(MasterView):
def normalize_row(self, row):
app = self.get_rattail_app()
products = app.get_products_handler()
products_handler = app.get_products_handler()
product = row.product
department = product.department if product else None
cost = product.cost if product else None
data = {
'uuid': row.uuid,
'sequence': row.sequence,
'item_entry': row.item_entry,
'product_uuid': row.product_uuid,
'product_upc': six.text_type(row.product_upc or ''),
'product_item_id': row.product_item_id,
'product_scancode': row.product_scancode,
'product_upc_pretty': row.product_upc.pretty() if row.product_upc else None,
'product_brand': row.product_brand,
'product_description': row.product_description,
'product_size': row.product_size,
'product_full_description': product.full_description if product else row.product_description,
'product_weighed': row.product_weighed,
'case_quantity': pretty_quantity(row.case_quantity),
@ -636,38 +642,88 @@ class CustomerOrderView(MasterView):
'units_ordered': pretty_quantity(row.units_ordered),
'order_quantity': pretty_quantity(row.order_quantity),
'order_uom': row.order_uom,
'order_uom_choices': self.uom_choices_for_product(product),
'order_uom_choices': self.uom_choices_for_row(row),
'department_display': department.name if department else None,
'vendor_display': cost.vendor.name if cost else None,
'department_display': row.department_name,
'unit_price': six.text_type(row.unit_price) if row.unit_price is not None else None,
'unit_price': float(row.unit_price) if row.unit_price is not None else None,
'unit_price_display': self.get_unit_price_display(row),
'total_price': six.text_type(row.total_price) if row.total_price is not None else None,
'total_price_display': "${:0.2f}".format(row.total_price) if row.total_price is not None else None,
'total_price': float(row.total_price) if row.total_price is not None else None,
'total_price_display': app.render_currency(row.total_price),
'status_code': row.status_code,
'status_text': row.status_text,
}
case_price = self.handler.get_case_price_for_row(row)
data['case_price'] = six.text_type(case_price) if case_price is not None else None
if row.unit_regular_price:
data['unit_regular_price'] = float(row.unit_regular_price)
data['unit_regular_price_display'] = app.render_currency(row.unit_regular_price)
if row.unit_sale_price:
data['unit_sale_price'] = float(row.unit_sale_price)
data['unit_sale_price_display'] = app.render_currency(row.unit_sale_price)
if row.sale_ends:
sale_ends = app.localtime(row.sale_ends, from_utc=True).date()
data['sale_ends'] = six.text_type(sale_ends)
data['sale_ends_display'] = app.render_date(sale_ends)
if row.unit_sale_price and row.unit_price == row.unit_sale_price:
data['pricing_reflects_sale'] = True
if row.product or row.pending_product:
data['product_full_description'] = products_handler.make_full_description(
row.product or row.pending_product)
if row.product:
cost = row.product.cost
if cost:
data['vendor_display'] = cost.vendor.name
elif row.pending_product:
data['vendor_display'] = row.pending_product.vendor_name
if row.pending_product:
pending = row.pending_product
data['pending_product'] = {
'uuid': pending.uuid,
'upc': six.text_type(pending.upc) if pending.upc is not None else None,
'item_id': pending.item_id,
'scancode': pending.scancode,
'brand_name': pending.brand_name,
'description': pending.description,
'size': pending.size,
'department_uuid': pending.department_uuid,
'regular_price_amount': float(pending.regular_price_amount) if pending.regular_price_amount is not None else None,
'vendor_name': pending.vendor_name,
'vendor_item_code': pending.vendor_item_code,
'unit_cost': float(pending.unit_cost) if pending.unit_cost is not None else None,
'case_size': float(pending.case_size) if pending.case_size is not None else None,
'notes': pending.notes,
}
case_price = self.batch_handler.get_case_price_for_row(row)
data['case_price'] = float(case_price) if case_price is not None else None
data['case_price_display'] = app.render_currency(case_price)
if self.handler.product_price_may_be_questionable():
if self.batch_handler.product_price_may_be_questionable():
data['price_needs_confirmation'] = row.price_needs_confirmation
key = self.rattail_config.product_key()
if key == 'upc':
data['product_key'] = data['product_upc_pretty']
else:
data['product_key'] = getattr(product, key, data['product_upc_pretty'])
elif key == 'item_id':
data['product_key'] = row.product_item_id
elif key == 'scancode':
data['product_key'] = row.product_scancode
else: # TODO: this seems not useful
data['product_key'] = getattr(row.product, key, data['product_upc_pretty'])
if row.product:
data.update({
'product_url': self.request.route_url('products.view', uuid=row.product.uuid),
'product_image_url': products.get_image_url(row.product),
'product_image_url': products_handler.get_image_url(row.product),
})
elif row.product_upc:
data['product_image_url'] = products_handler.get_image_url(upc=row.product_upc)
unit_uom = self.enum.UNIT_OF_MEASURE_POUND if data['product_weighed'] else self.enum.UNIT_OF_MEASURE_EACH
if row.order_uom == self.enum.UNIT_OF_MEASURE_CASE:
@ -695,6 +751,11 @@ class CustomerOrderView(MasterView):
return data
def add_item(self, batch, data):
app = self.get_rattail_app()
order_quantity = decimal.Decimal(data.get('order_quantity') or '0')
order_uom = data.get('order_uom')
if data.get('product_is_known'):
uuid = data.get('product_uuid')
@ -706,18 +767,30 @@ class CustomerOrderView(MasterView):
return {'error': "Product not found"}
kwargs = {}
if self.handler.product_price_may_be_questionable():
if self.batch_handler.product_price_may_be_questionable():
kwargs['price_needs_confirmation'] = data.get('price_needs_confirmation')
row = self.handler.add_product(batch, product,
decimal.Decimal(data.get('order_quantity') or '0'),
data.get('order_uom'),
**kwargs)
self.Session.flush()
row = self.batch_handler.add_product(batch, product,
order_quantity, order_uom,
**kwargs)
else: # product is not known
raise NotImplementedError # TODO
else: # unknown product; add pending
pending_info = dict(data['pending_product'])
if 'upc' in pending_info:
pending_info['upc'] = app.make_gpc(pending_info['upc'])
for field in ('unit_cost', 'regular_price_amount', 'case_size'):
if field in pending_info:
pending_info[field] = decimal.Decimal(pending_info[field])
pending_info['user'] = self.request.user
row = self.batch_handler.add_pending_product(batch,
pending_info,
order_quantity, order_uom)
self.Session.flush()
return {'batch': self.normalize_batch(batch),
'row': self.normalize_row(row)}
@ -733,6 +806,9 @@ class CustomerOrderView(MasterView):
if row not in batch.active_rows():
return {'error': "Row is not active for the batch"}
order_quantity = decimal.Decimal(data.get('order_quantity') or '0')
order_uom = data.get('order_uom')
if data.get('product_is_known'):
uuid = data.get('product_uuid')
@ -745,19 +821,26 @@ class CustomerOrderView(MasterView):
row.item_entry = product.uuid
row.product = product
row.order_quantity = decimal.Decimal(data.get('order_quantity') or '0')
row.order_uom = data.get('order_uom')
row.order_quantity = order_quantity
row.order_uom = order_uom
if self.handler.product_price_may_be_questionable():
if self.batch_handler.product_price_may_be_questionable():
row.price_needs_confirmation = data.get('price_needs_confirmation')
self.handler.refresh_row(row)
self.Session.flush()
self.Session.refresh(row)
self.batch_handler.refresh_row(row)
else: # product is not known
raise NotImplementedError # TODO
# set these first, since row will be refreshed below
row.order_quantity = order_quantity
row.order_uom = order_uom
# nb. this will refresh the row
pending_info = dict(data['pending_product'])
self.batch_handler.update_pending_product(row, pending_info)
self.Session.flush()
self.Session.refresh(row)
return {'batch': self.normalize_batch(batch),
'row': self.normalize_row(row)}
@ -774,7 +857,7 @@ class CustomerOrderView(MasterView):
if row not in batch.active_rows():
return {'error': "Row is not active for this batch"}
self.handler.do_remove_row(row)
self.batch_handler.do_remove_row(row)
return {'ok': True,
'batch': self.normalize_batch(batch)}
@ -794,7 +877,30 @@ class CustomerOrderView(MasterView):
return {'ok': True, 'next_url': next_url}
def execute_new_order_batch(self, batch, data):
return self.handler.do_execute(batch, self.request.user)
return self.batch_handler.do_execute(batch, self.request.user)
def configure_get_simple_settings(self):
return [
# customer handling
{'section': 'rattail.custorders',
'option': 'new_order_requires_customer',
'type': bool},
{'section': 'rattail.custorders',
'option': 'new_orders.allow_contact_info_choice',
'type': bool},
{'section': 'rattail.custorders',
'option': 'new_orders.allow_contact_info_create',
'type': bool},
# product handling
{'section': 'rattail.custorders',
'option': 'allow_unknown_product',
'type': bool},
{'section': 'rattail.custorders',
'option': 'product_price_may_be_questionable',
'type': bool},
]
@classmethod
def defaults(cls, config):