changeset 0b2e50445415 in modules/production:default
details: 
https://hg.tryton.org/modules/production?cmd=changeset;node=0b2e50445415
description:
        Allocate cost based on list price of each product

        issue3209
        review288471002
diffstat:

 CHANGELOG                                   |   2 +
 doc/index.rst                               |   5 +-
 exceptions.py                               |   8 --
 message.xml                                 |   3 -
 production.py                               |  79 +++++++++++-----------
 tests/scenario_production.rst               |   2 +
 tests/scenario_production_by_product.rst    |  99 +++++++++++++++++++++++++++++
 tests/scenario_production_no_list_price.rst |  81 +++++++++++++++++++++++
 tests/test_production.py                    |   9 ++
 9 files changed, 234 insertions(+), 54 deletions(-)

diffs (378 lines):

diff -r 84c44c502032 -r 0b2e50445415 CHANGELOG
--- a/CHANGELOG Wed Dec 04 11:09:36 2019 +0100
+++ b/CHANGELOG Thu Dec 05 22:10:55 2019 +0100
@@ -1,3 +1,5 @@
+* Allocate cost based on list price of each product
+
 Version 5.4.0 - 2019-11-04
 * Bug fixes (see mercurial logs for details)
 
diff -r 84c44c502032 -r 0b2e50445415 doc/index.rst
--- a/doc/index.rst     Wed Dec 04 11:09:36 2019 +0100
+++ b/doc/index.rst     Thu Dec 05 22:10:55 2019 +0100
@@ -58,6 +58,5 @@
   All moves are cancelled.
 
 The cost of the production is computed with the sum of the cost price of all
-incoming products. If a product is selected, it will have the cost as unit
-price. The production verify that all the cost is spread on the unit price of
-output moves.
+incoming products. This cost is allocated to the output products based on the
+list price of each (the product without a list price are considered as waste).
diff -r 84c44c502032 -r 0b2e50445415 exceptions.py
--- a/exceptions.py     Wed Dec 04 11:09:36 2019 +0100
+++ /dev/null   Thu Jan 01 00:00:00 1970 +0000
@@ -1,8 +0,0 @@
-# This file is part of Tryton.  The COPYRIGHT file at the top level of
-# this repository contains the full copyright notices and license terms.
-
-from trytond.model.exceptions import ValidationError
-
-
-class CostError(ValidationError):
-    pass
diff -r 84c44c502032 -r 0b2e50445415 message.xml
--- a/message.xml       Wed Dec 04 11:09:36 2019 +0100
+++ b/message.xml       Thu Dec 05 22:10:55 2019 +0100
@@ -9,8 +9,5 @@
         <record model="ir.message" id="msg_product_bom_unique">
             <field name="text">Products can only appear once on each 
BOM.</field>
         </record>
-        <record model="ir.message" id="msg_uneven_costs">
-            <field name="text">The costs of the outputs "%(outputs)s" of 
production "%(production)s" do not match the cost of the production 
"%(costs)s".</field>
-        </record>
     </data>
 </tryton>
diff -r 84c44c502032 -r 0b2e50445415 production.py
--- a/production.py     Wed Dec 04 11:09:36 2019 +0100
+++ b/production.py     Thu Dec 05 22:10:55 2019 +0100
@@ -4,7 +4,6 @@
 
 from sql import Null
 
-from trytond.i18n import gettext
 from trytond.model import ModelView, ModelSQL, Workflow, fields
 from trytond.wizard import Wizard, StateTransition, StateView, Button
 from trytond.pyson import Eval, Bool, If, Id
@@ -13,10 +12,6 @@
 
 from trytond.modules.product import price_digits
 
-from .exceptions import CostError
-
-__all__ = ['Production', 'AssignFailed', 'Assign']
-
 BOM_CHANGES = ['bom', 'product', 'quantity', 'uom', 'warehouse', 'location',
     'company', 'inputs', 'outputs']
 
@@ -445,54 +440,58 @@
                 move.save()
         self._set_move_planned_date()
 
+    @property
+    def _list_price_context(self):
+        return {
+            'company': self.company.id,
+            }
+
     @classmethod
     def set_cost(cls, productions):
         pool = Pool()
         Uom = pool.get('product.uom')
         Move = pool.get('stock.move')
 
-        digits = Move.unit_price.digits
-        digit = Decimal(str(10 ** -digits[1]))
+        digits = Decimal(str(10 ** -Move.unit_price.digits[1]))
         moves = []
         for production in productions:
-            if not production.quantity or not production.uom:
-                continue
-            if production.company.currency.is_zero(
-                    production.cost - production.output_cost):
-                continue
-            unit_price = production.cost / Decimal(str(production.quantity))
+            sum_ = Decimal(0)
+            prices = {}
             for output in production.outputs:
-                if output.product == production.product:
-                    output.unit_price = Uom.compute_price(
-                        production.uom, unit_price, output.uom).quantize(digit)
+                product = output.product
+                with Transaction().set_context(production._list_price_context):
+                    list_price = product.list_price_used
+                product_price = (Decimal(str(output.quantity))
+                    * Uom.compute_price(
+                        product.default_uom, list_price, output.uom))
+                prices[output] = product_price
+                sum_ += product_price
+
+            if not sum_ and production.product:
+                prices.clear()
+                for output in production.outputs:
+                    if output.product == production.product:
+                        quantity = Uom.compute_qty(
+                            output.uom, output.quantity,
+                            output.product.default_uom, round=False)
+                        quantity = Decimal(str(quantity))
+                        prices[output] = quantity
+                        sum_ += quantity
+
+            for output in production.outputs:
+                if sum_:
+                    ratio = prices.get(output, 0) / sum_
+                else:
+                    ratio = Decimal(1) / len(production.outputs)
+                quantity = Decimal(str(output.quantity))
+                unit_price = (
+                    production.cost * ratio / quantity).quantize(digits)
+                if output.unit_price != unit_price:
+                    output.unit_price = unit_price
                     moves.append(output)
         Move.save(moves)
 
     @classmethod
-    def validate(cls, productions):
-        super(Production, cls).validate(productions)
-        for production in productions:
-            production.check_cost()
-
-    @property
-    def output_cost(self):
-        cost_price = Decimal(0)
-        for output in self.outputs:
-            cost_price += (Decimal(str(output.quantity)) * output.unit_price)
-        return cost_price
-
-    def check_cost(self):
-        if self.state != 'done':
-            return
-        cost_price = self.output_cost
-        if not self.company.currency.is_zero(self.cost - cost_price):
-            raise CostError(gettext(
-                    'production.msg_uneven_costs',
-                    production=self.rec_name,
-                    costs=self.cost,
-                    outputs=cost_price))
-
-    @classmethod
     def view_attributes(cls):
         return [
             ('/tree', 'visual', If(Eval('state') == 'cancel', 'muted', '')),
diff -r 84c44c502032 -r 0b2e50445415 tests/scenario_production.rst
--- a/tests/scenario_production.rst     Wed Dec 04 11:09:36 2019 +0100
+++ b/tests/scenario_production.rst     Thu Dec 05 22:10:55 2019 +0100
@@ -172,6 +172,8 @@
     'done'
     >>> output.effective_date == production.effective_date
     True
+    >>> output.unit_price
+    Decimal('12.5000')
     >>> with config.set_context(locations=[storage.id]):
     ...     Product(product.id).quantity == 2
     True
diff -r 84c44c502032 -r 0b2e50445415 tests/scenario_production_by_product.rst
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/scenario_production_by_product.rst  Thu Dec 05 22:10:55 2019 +0100
@@ -0,0 +1,99 @@
+==========================
+Production with By-product
+==========================
+
+Imports::
+
+    >>> from decimal import Decimal
+    >>> from proteus import Model, Wizard
+    >>> from trytond.tests.tools import activate_modules
+    >>> from trytond.modules.company.tests.tools import create_company, \
+    ...     get_company
+
+Install production Module::
+
+    >>> config = activate_modules('production')
+
+Create company::
+
+    >>> _ = create_company()
+    >>> company = get_company()
+
+Create main product::
+
+    >>> ProductUom = Model.get('product.uom')
+    >>> unit, = ProductUom.find([('name', '=', 'Unit')])
+    >>> ProductTemplate = Model.get('product.template')
+
+    >>> template = ProductTemplate()
+    >>> template.name = 'product'
+    >>> template.default_uom = unit
+    >>> template.type = 'goods'
+    >>> template.producible = True
+    >>> template.list_price = Decimal(30)
+    >>> template.save()
+    >>> product, = template.products
+
+Create by-products 1 marketable and 1 waste::
+
+    >>> template = ProductTemplate()
+    >>> template.name = 'product'
+    >>> template.default_uom = unit
+    >>> template.type = 'goods'
+    >>> template.list_price = Decimal(10)
+    >>> template.save()
+    >>> marketable, = template.products
+
+    >>> template = ProductTemplate()
+    >>> template.name = 'product'
+    >>> template.default_uom = unit
+    >>> template.type = 'goods'
+    >>> template.list_price = Decimal(0)
+    >>> template.save()
+    >>> waste, = template.products
+
+Create component::
+
+    >>> template = ProductTemplate()
+    >>> template.name = 'component'
+    >>> template.default_uom = unit
+    >>> template.type = 'goods'
+    >>> template.save()
+    >>> component, = template.products
+    >>> component.cost_price = Decimal(5)
+    >>> component.save()
+
+Create Bill of Material::
+
+    >>> BOM = Model.get('production.bom')
+    >>> bom = BOM(name='product')
+    >>> input = bom.inputs.new()
+    >>> input.product = component
+    >>> input.quantity = 4
+    >>> output = bom.outputs.new()
+    >>> output.product = product
+    >>> output.quantity = 1
+    >>> output = bom.outputs.new()
+    >>> output.product = marketable
+    >>> output.quantity = 1
+    >>> output = bom.outputs.new()
+    >>> output.product = waste
+    >>> output.quantity = 2
+    >>> bom.save()
+
+Make a production::
+
+    >>> Production = Model.get('production')
+    >>> production = Production()
+    >>> production.product = product
+    >>> production.bom = bom
+    >>> production.quantity = 4
+    >>> production.click('wait')
+    >>> production.click('assign_force')
+    >>> production.click('run')
+    >>> production.click('done')
+
+Check output price::
+
+    >>> sorted([o.unit_price for o in production.outputs])
+    [Decimal('0'), Decimal('5.0000'), Decimal('15.0000')]
diff -r 84c44c502032 -r 0b2e50445415 tests/scenario_production_no_list_price.rst
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/scenario_production_no_list_price.rst       Thu Dec 05 22:10:55 
2019 +0100
@@ -0,0 +1,81 @@
+=============================
+Production without list price
+=============================
+
+Imports::
+
+    >>> from decimal import Decimal
+    >>> from proteus import Model, Wizard
+    >>> from trytond.tests.tools import activate_modules
+    >>> from trytond.modules.company.tests.tools import create_company, \
+    ...     get_company
+
+Install production Module::
+
+    >>> config = activate_modules('production')
+
+Create company::
+
+    >>> _ = create_company()
+    >>> company = get_company()
+
+Create main product::
+
+    >>> ProductUom = Model.get('product.uom')
+    >>> unit, = ProductUom.find([('name', '=', 'Unit')])
+    >>> ProductTemplate = Model.get('product.template')
+
+    >>> template = ProductTemplate()
+    >>> template.name = 'product'
+    >>> template.default_uom = unit
+    >>> template.type = 'goods'
+    >>> template.producible = True
+    >>> template.list_price = Decimal(0)
+    >>> template.save()
+    >>> product, = template.products
+
+Create component::
+
+    >>> template = ProductTemplate()
+    >>> template.name = 'component'
+    >>> template.default_uom = unit
+    >>> template.type = 'goods'
+    >>> template.save()
+    >>> component, = template.products
+    >>> component.cost_price = Decimal(5)
+    >>> component.save()
+
+Create Bill of Material::
+
+    >>> BOM = Model.get('production.bom')
+    >>> bom = BOM(name='product')
+    >>> input = bom.inputs.new()
+    >>> input.product = component
+    >>> input.quantity = 4
+    >>> output = bom.outputs.new()
+    >>> output.product = product
+    >>> output.quantity = 2
+    >>> bom.save()
+
+Make a production::
+
+    >>> Production = Model.get('production')
+    >>> production = Production()
+    >>> production.product = product
+    >>> production.bom = bom
+    >>> production.quantity = 4
+    >>> production.click('wait')
+    >>> production.click('assign_force')
+    >>> production.click('run')
+    >>> output, = production.outputs
+    >>> output.quantity = 2
+    >>> output.save()
+    >>> _ = output.duplicate()
+    >>> production.click('done')
+
+Check output price::
+
+    >>> production.cost
+    Decimal('40.0000')
+    >>> sorted([o.unit_price for o in production.outputs])
+    [Decimal('10.0000'), Decimal('10.0000')]
diff -r 84c44c502032 -r 0b2e50445415 tests/test_production.py
--- a/tests/test_production.py  Wed Dec 04 11:09:36 2019 +0100
+++ b/tests/test_production.py  Thu Dec 05 22:10:55 2019 +0100
@@ -54,4 +54,13 @@
             tearDown=doctest_teardown, encoding='utf-8',
             checker=doctest_checker,
             optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
+    suite.addTests(doctest.DocFileSuite('scenario_production_by_product.rst',
+            tearDown=doctest_teardown, encoding='utf-8',
+            checker=doctest_checker,
+            optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
+    suite.addTests(
+        doctest.DocFileSuite('scenario_production_no_list_price.rst',
+            tearDown=doctest_teardown, encoding='utf-8',
+            checker=doctest_checker,
+            optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
     return suite

Reply via email to