changeset a7e81f1cf006 in modules/project_revenue:default
details: 
https://hg.tryton.org/modules/project_revenue?cmd=changeset;node=a7e81f1cf006
description:
        Allow not time based product on work

        They are considered as fixed prices.

        issue7853
        review289011002
diffstat:

 CHANGELOG                          |   2 ++
 doc/index.rst                      |   5 +++++
 tests/scenario_project_revenue.rst |  20 ++++++++++++++++----
 work.py                            |  33 ++++++++++++++++++++++++---------
 4 files changed, 47 insertions(+), 13 deletions(-)

diffs (155 lines):

diff -r 35630f3f3f18 -r a7e81f1cf006 CHANGELOG
--- a/CHANGELOG Thu Mar 19 19:08:03 2020 +0100
+++ b/CHANGELOG Sat Mar 28 13:06:21 2020 +0100
@@ -1,3 +1,5 @@
+* Allow non-time based products on work
+
 Version 5.4.0 - 2019-11-04
 * Bug fixes (see mercurial logs for details)
 
diff -r 35630f3f3f18 -r a7e81f1cf006 doc/index.rst
--- a/doc/index.rst     Thu Mar 19 19:08:03 2020 +0100
+++ b/doc/index.rst     Sat Mar 28 13:06:21 2020 +0100
@@ -2,3 +2,8 @@
 ######################
 
 The project revenue module computes revenue and cost per task and project.
+The revenue uses the list price of the product. If the product's unit of
+measure is time based, the revenue is computed as the product of the price and
+the hours of effort otherwise the price is considered as fixed.
+The cost is computed by summing the cost of all the linked time sheets and the
+linked purchase lines.
diff -r 35630f3f3f18 -r a7e81f1cf006 tests/scenario_project_revenue.rst
--- a/tests/scenario_project_revenue.rst        Thu Mar 19 19:08:03 2020 +0100
+++ b/tests/scenario_project_revenue.rst        Sat Mar 28 13:06:21 2020 +0100
@@ -43,6 +43,7 @@
 
     >>> ProductUom = Model.get('product.uom')
     >>> hour, = ProductUom.find([('name', '=', 'Hour')])
+    >>> unit, = ProductUom.find([('name', '=', 'Unit')])
     >>> ProductTemplate = Model.get('product.template')
 
     >>> template = ProductTemplate()
@@ -53,6 +54,14 @@
     >>> template.save()
     >>> product, = template.products
 
+    >>> template = ProductTemplate()
+    >>> template.name = 'Service'
+    >>> template.default_uom = unit
+    >>> template.type = 'service'
+    >>> template.list_price = Decimal('80')
+    >>> template.save()
+    >>> package, = template.products
+
 Create a Project::
 
     >>> Project = Model.get('project.work')
@@ -77,17 +86,20 @@
     >>> task_no_effort.name = "Task 2"
     >>> task_no_effort.type = 'task'
     >>> task_no_effort.effort_duration = None
+    >>> task_no_effort.product = package
+    >>> task_no_effort.list_price
+    Decimal('80.0000')
     >>> project.save()
     >>> task, task_no_effort = project.children
 
 Check project revenue and cost::
 
     >>> project.revenue
-    Decimal('120.00')
+    Decimal('200.00')
     >>> task.revenue
     Decimal('100.00')
     >>> task_no_effort.revenue
-    Decimal('0')
+    Decimal('80.00')
     >>> project.cost
     Decimal('0.00')
     >>> task.cost
@@ -114,11 +126,11 @@
     >>> project.reload()
     >>> task, task_no_effort = project.children
     >>> project.revenue
-    Decimal('120.00')
+    Decimal('200.00')
     >>> task.revenue
     Decimal('100.00')
     >>> task_no_effort.revenue
-    Decimal('0')
+    Decimal('80.00')
     >>> project.cost
     Decimal('50.00')
     >>> task.cost
diff -r 35630f3f3f18 -r a7e81f1cf006 work.py
--- a/work.py   Thu Mar 19 19:08:03 2020 +0100
+++ b/work.py   Sat Mar 28 13:06:21 2020 +0100
@@ -7,7 +7,7 @@
 from sql.operators import Concat
 
 from trytond.model import fields
-from trytond.pyson import Eval, Id
+from trytond.pyson import Eval
 from trytond.transaction import Transaction
 from trytond.pool import Pool, PoolMeta
 from trytond.tools import reduce_ids, grouped_slice
@@ -20,7 +20,6 @@
     product = fields.Many2One('product.product', 'Product',
         domain=[
             ('type', '=', 'service'),
-            ('default_uom_category', '=', Id('product', 'uom_cat_time')),
             ])
     list_price = fields.Numeric('List Price', digits=price_digits)
     revenue = fields.Function(fields.Numeric('Revenue',
@@ -153,13 +152,16 @@
 
     @classmethod
     def _get_revenue(cls, works):
-        revenues = {}
+        revenues = dict.fromkeys(map(int, works), Decimal(0))
         for work in works:
-            if work.list_price:
-                revenues[work.id] = work.company.currency.round(
+            if not work.list_price:
+                continue
+            if work.price_list_hour:
+                revenue = work.company.currency.round(
                     work.list_price * Decimal(str(work.effort_hours)))
             else:
-                revenues[work.id] = Decimal(0)
+                revenue = work.list_price
+            revenues[work.id] = work.company.currency.round(revenue)
         return revenues
 
     @fields.depends('company')
@@ -188,9 +190,12 @@
         if not self.product:
             return
 
-        hour_uom = Uom(ModelData.get_id('product', 'uom_hour'))
-        self.list_price = Uom.compute_price(self.product.default_uom,
-            self.product.list_price, hour_uom)
+        if self.price_list_hour:
+            hour_uom = Uom(ModelData.get_id('product', 'uom_hour'))
+            self.list_price = Uom.compute_price(
+                self.product.default_uom, self.product.list_price, hour_uom)
+        else:
+            self.list_price = self.product.list_price
 
         if self.company:
             user = User(Transaction().user)
@@ -202,3 +207,13 @@
         digits = self.__class__.list_price.digits
         self.list_price = self.list_price.quantize(
             Decimal(str(10.0 ** -digits[1])))
+
+    @property
+    def price_list_hour(self):
+        pool = Pool()
+        ModelData = pool.get('ir.model.data')
+        Category = pool.get('product.uom.category')
+        if not self.product:
+            return
+        time = Category(ModelData.get_id('product', 'uom_cat_time'))
+        return self.product.default_uom_category == time

Reply via email to