Changeset - dc0c65ea7b05
[Not reviewed]
0 1 0
Lance Edgar (lance) - 3 years ago 2021-11-06 17:35:51
lance@edbob.org
Add some "case price" logic for custorder batch
1 file changed with 23 insertions and 1 deletions:
0 comments (0 inline, 0 general)
rattail/batch/custorder.py
Show inline comments
 
@@ -18,24 +18,25 @@
 
#
 
#  You should have received a copy of the GNU General Public License along with
 
#  Rattail.  If not, see <http://www.gnu.org/licenses/>.
 
#
 
################################################################################
 
"""
 
Handler for "customer order" batches
 
"""
 

	
 
from __future__ import unicode_literals, absolute_import, division
 

	
 
import re
 
import decimal
 

	
 
import six
 
import sqlalchemy as sa
 
from sqlalchemy import orm
 

	
 
from rattail.db import model
 
from rattail.batch import BatchHandler
 

	
 

	
 
class CustomerOrderBatchHandler(BatchHandler):
 
    """
 
    Handler for all "customer order" batches, regardless of "mode".  The
 
@@ -326,24 +327,35 @@ class CustomerOrderBatchHandler(BatchHandler):
 
        batch.contact_name = pending.display_name
 
        batch.phone_number = pending.phone_number
 
        batch.email_address = pending.email_address
 

	
 
    def get_case_size_for_product(self, product):
 
        if product.case_size:
 
            return product.case_size
 

	
 
        cost = product.cost
 
        if cost:
 
            return cost.case_size
 

	
 
    def get_case_price_for_row(self, row):
 
        """
 
        Calculate and return the per-case price for the given row.
 

	
 
        NB. we do not store case price, only unit price.  maybe that
 
        should change some day..
 
        """
 
        if row.unit_price is not None:
 
            case_price = row.unit_price * (row.case_quantity or 1)
 
            return case_price.quantize(decimal.Decimal('0.01'))
 

	
 
    # TODO: this method should maybe not exist?  and caller just
 
    # invokes the handler directly instead?
 
    def customer_autocomplete(self, session, term, **kwargs):
 
        """
 
        Override the Customer autocomplete, to search by phone number
 
        as well as name.
 
        """
 
        autocompleter = self.app.get_autocompleter('customers.neworder')
 
        return autocompleter.autocomplete(session, term, **kwargs)
 

	
 
    # TODO: this method should maybe not exist?  and caller just
 
    # invokes the handler directly instead?
 
@@ -399,30 +411,40 @@ class CustomerOrderBatchHandler(BatchHandler):
 
                                  "method.".format(__class__.__name__))
 

	
 
    def get_product_info(self, batch, product, **kwargs):
 
        """
 
        Return a data dict containing misc. info pertaining to the
 
        given product, for the order batch.
 
        """
 
        products = self.app.get_products_handler()
 
        info = {
 
            'uuid': product.uuid,
 
            'upc': six.text_type(product.upc),
 
            'upc_pretty': product.upc.pretty(),
 
            'unit_price_display': products.render_price(product.regular_price),
 
            'full_description': product.full_description,
 
            'size': product.size,
 
            'case_quantity': self.app.render_quantity(self.get_case_size_for_product(product)),
 
            'unit_price_display': products.render_price(product.regular_price),
 
            'image_url': products.get_image_url(product),
 
            'uom_choices': self.uom_choices_for_product(product),
 
        }
 

	
 
        case_price = None
 
        if product.regular_price and product.regular_price is not None:
 
            case_size = self.get_case_size_for_product(product)
 
            case_price = case_size * product.regular_price.price
 
            case_price = case_price.quantize(decimal.Decimal('0.01'))
 
        info['case_price'] = six.text_type(case_price) if case_price is not None else None
 
        info['case_price_display'] = self.app.render_currency(case_price)
 

	
 
        key = self.config.product_key()
 
        if key == 'upc':
 
            info['key'] = info['upc_pretty']
 
        else:
 
            info['key'] = getattr(product, key, info['upc_pretty'])
 

	
 
        return info
 

	
 
    def uom_choices_for_product(self, product):
 
        """
 
        Return a list of UOM choices for the given product.
 
        """
0 comments (0 inline, 0 general)