[docs]classNewOrderBatchHandler(base.NewOrderBatchHandler):""" Custom :term:`handler` for :term:`new order batches <new order batch>` which can use CORE-POS as external data source for customers and products. See parent class :class:`~sideshow:sideshow.batch.neworder.NewOrderBatchHandler` for more info. """defautocomplete_customers_external(self,session,term,user=None):""" """corepos=self.app.get_corepos_handler()op_model=corepos.get_model_office_op()op_session=corepos.make_session_office_op()# base queryquery=op_session.query(op_model.CustomerClassic)\
.join(op_model.MemberInfo,op_model.MemberInfo.card_number==op_model.CustomerClassic.card_number)# filter querycriteria=[]forwordinterm.split():criteria.append(sa.or_(op_model.CustomerClassic.first_name.ilike(f'%{word}%'),op_model.CustomerClassic.last_name.ilike(f'%{word}%')))query=query.filter(sa.and_(*criteria))# sort queryquery=query.order_by(op_model.CustomerClassic.first_name,op_model.CustomerClassic.last_name)# get data# TODO: need max_results optioncustomers=query.all()# get resultsdefresult(customer):return{'value':str(customer.card_number),'label':str(customer)}results=[result(c)forcincustomers]op_session.close()returnresultsdefrefresh_batch_from_external_customer(self,batch):""" """corepos=self.app.get_corepos_handler()op_model=corepos.get_model_office_op()op_session=corepos.make_session_office_op()ifnotbatch.customer_id.isdigit():raiseValueError(f"invalid CORE-POS customer card number: {batch.customer_id}")try:customer=op_session.query(op_model.CustomerClassic)\
.join(op_model.MemberInfo,op_model.MemberInfo.card_number==op_model.CustomerClassic.card_number)\
.filter(op_model.CustomerClassic.card_number==int(batch.customer_id))\
.filter(op_model.CustomerClassic.person_number==1)\
.options(orm.joinedload(op_model.CustomerClassic.member_info))\
.one()exceptorm.exc.NoResultFound:raiseValueError(f"CORE-POS Customer not found: {batch.customer_id}")batch.customer_name=str(customer)batch.phone_number=customer.member_info.phonebatch.email_address=customer.member_info.emailop_session.close()defautocomplete_products_external(self,session,term,user=None):""" """corepos=self.app.get_corepos_handler()op_model=corepos.get_model_office_op()op_session=corepos.make_session_office_op()# base queryquery=op_session.query(op_model.Product)# filter querycriteria=[]forwordinterm.split():criteria.append(sa.or_(op_model.Product.brand.ilike(f'%{word}%'),op_model.Product.description.ilike(f'%{word}%')))query=query.filter(sa.and_(*criteria))# sort queryquery=query.order_by(op_model.Product.brand,op_model.Product.description)# get data# TODO: need max_results optionproducts=query.all()# get resultsdefresult(product):return{'value':product.upc,'label':self.app.make_full_name(product.brand,product.description,product.size)}results=[result(c)forcinproducts]op_session.close()returnresultsdefget_product_info_external(self,session,product_id,user=None):""" """corepos=self.app.get_corepos_handler()op_model=corepos.get_model_office_op()op_session=corepos.make_session_office_op()try:product=op_session.query(op_model.Product)\
.filter(op_model.Product.upc==product_id)\
.one()exceptorm.exc.NoResultFound:raiseValueError(f"CORE-POS Product not found: {product_id}")data={'product_id':product.upc,'scancode':product.upc,'brand_name':product.brand,'description':product.description,'size':product.size,'full_description':self.app.make_full_name(product.brand,product.description,product.size),'weighed':product.scale,'special_order':False,'department_id':product.department_number,'department_name':product.department.nameifproduct.departmentelseNone,'case_size':self.get_case_size_for_external_product(product),'unit_price_reg':self.get_unit_price_reg_for_external_product(product),# TODO# 'vendor_name': product.vendor_name,# 'vendor_item_code': product.vendor_item_code,}op_session.close()returndatadefrefresh_row_from_external_product(self,row):""" """corepos=self.app.get_corepos_handler()op_model=corepos.get_model_office_op()op_session=corepos.make_session_office_op()try:product=op_session.query(op_model.Product)\
.filter(op_model.Product.upc==row.product_id)\
.one()exceptorm.exc.NoResultFound:raiseValueError(f"CORE-POS Product not found: {row.product_id}")row.product_scancode=product.upcrow.product_brand=product.brandrow.product_description=product.descriptionrow.product_size=product.sizerow.product_weighed=product.scalerow.department_id=product.department_numberrow.department_name=product.department.nameifproduct.departmentelseNonerow.special_order=Falserow.case_size=self.get_case_size_for_external_product(product)row.unit_cost=product.costrow.unit_price_reg=self.get_unit_price_reg_for_external_product(product)op_session.close()defget_case_size_for_external_product(self,product):""" """ifproduct.vendor_items:item=product.vendor_items[0]ifitem.unitsisnotNone:returndecimal.Decimal(f'{item.units:0.4f}')defget_unit_price_reg_for_external_product(self,product):""" """ifproduct.normal_priceisnotNone:returndecimal.Decimal(f'{product.normal_price:0.3f}')