Changeset - b6803ce301fa
[Not reviewed]
0 1 0
Lance Edgar (lance) - 2 years ago 2022-10-28 19:30:50
lance@edbob.org
Update logic for calculating markup from margin

to assume margin value is between 0 - 100 by default, instead of
assuming it's between 0.0 - 1.0

part of a broader effort to standardize how we handle percentage values
1 file changed with 29 insertions and 4 deletions:
0 comments (0 inline, 0 general)
rattail/pricing.py
Show inline comments
 
@@ -27,6 +27,7 @@ Pricing Utilities
 
from __future__ import unicode_literals, absolute_import
 

	
 
import decimal
 
import warnings
 

	
 

	
 
def gross_margin(price, cost, percentage=False):
 
@@ -59,12 +60,19 @@ def gross_margin(price, cost, percentage=False):
 
    return 100 * margin
 

	
 

	
 
def calculate_markup(margin):
 
def calculate_markup_from_margin(margin, from_decimal=False):
 
    """
 
    Calculate the "markup" value corresponding to the given margin.
 

	
 
    :param margin: Profit margin as decimal percentage (e.g. between
 
       0.0 and 1.0).
 
    This assumes the ``margin`` value is user-friendly, e.g. ``37.5``
 
    instead of ``0.375`` to represent 37.5%, unless ``from_decimal``
 
    is true in which case the decimal format is assumed.
 

	
 
    :param margin: Profit margin percentage.
 

	
 
    :param from_decimal: If false (the default), then ``margin``
 
       should (normally) be between 0 - 100.  But if true, then
 
       ``margin`` is assumed to be between 0.0 and 1.0 instead.
 

	
 
    :returns: Equivalent cost markup as decimal value (e.g. 1.4).
 
    """
 
@@ -73,7 +81,24 @@ def calculate_markup(margin):
 
    if margin == 0:
 
        return 1
 

	
 
    return 1 / (1 - margin)
 
    if from_decimal:
 
        margin *= 100
 

	
 
    return 100 / (100 - margin)
 

	
 

	
 
def calculate_markup_from_margin_decimal(margin):
 
    warnings.warn("calculate_markup_from_margin_decimal() is deprecated; "
 
                  "please use calculate_markup_from_margin() instead",
 
                  DeprecationWarning, stacklevel=2)
 
    return calculate_markup_from_margin(margin, from_decimal=True)
 

	
 

	
 
def calculate_markup(margin):
 
    warnings.warn("calculate_markup() is deprecated; please use "
 
                  "calculate_markup_from_margin() instead",
 
                  DeprecationWarning, stacklevel=2)
 
    return calculate_markup_from_margin(margin, from_decimal=True)
 

	
 

	
 
def calculate_variance(oldvalue, newvalue):
0 comments (0 inline, 0 general)