Changeset 6ca0e9c in rattail


Ignore:
Timestamp:
03/08/19 14:02:13 (6 months ago)
Author:
Lance Edgar <ledgar@…>
Branches:
master
Children:
2f408c1
Parents:
9afdb741
Message:

Add percentage kwarg to pricing.gross_margin() function

so it can return the "industry standard" value instead

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rattail/pricing.py

    r9afdb741 r6ca0e9c  
    1 # -*- coding: utf-8 -*-
     1# -*- coding: utf-8; -*-
    22################################################################################
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2017 Lance Edgar
     5#  Copyright © 2010-2019 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    2525"""
    2626
    27 from __future__ import unicode_literals
     27from __future__ import unicode_literals, absolute_import
    2828
    2929
     
    3131
    3232
    33 def gross_margin(price, cost):
     33def gross_margin(price, cost, percentage=False):
    3434    """
    3535    Calculate and return a gross margin percentage based on ``price`` and
    3636    ``cost``.
    3737
     38    Please note, that for historical reasons, the default behavior is to return
     39    the margin as a decimal value from 0.0 through 100.0 (or beyond, perhaps).
     40
     41    However the "industry standard" seems to be to use a decimal value between
     42    0.000 and 1.000 instead.  Specify ``percentage=True`` for this behavior.
     43
    3844    If ``price`` is empty (or zero), returns ``None``.
    3945
    40     If ``cost`` is empty (or zero), returns ``100``.
     46    If ``cost`` is empty (or zero), returns ``100`` (or ``1`` if
     47    ``percentage=True``).
    4148    """
    42 
    4349    if not price:
    4450        return None
    4551
    4652    if not cost:
     53        if percentage:
     54            return 1
    4755        return 100
    4856
    49     return 100 * (price - cost) / price
     57    margin = (price - cost) / price
     58    if percentage:
     59        return margin
     60    return 100 * margin
Note: See TracChangeset for help on using the changeset viewer.