Skip to content

Commit 18bf961

Browse files
committed
Merge pull request OCA#24 from grap/7.0_margin_stored
[7.0] 'product_standard_margin' margin stored
2 parents 07dd209 + 2279726 commit 18bf961

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

product_standard_margin/product_std_margin.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
_logger = logging.getLogger(__name__)
2828

2929

30-
class Product(orm.Model):
30+
class ProductProduct(orm.Model):
3131
_inherit = 'product.product'
3232

3333
# TODO : compute the margin with default taxes
@@ -71,19 +71,13 @@ def _compute_margin(self, cr, user, ids, field_name, arg, context=None):
7171
7272
:return dict of dict of the form :
7373
{INT Product ID : {
74-
{'margin_absolute': float,
75-
'margin_relative': float}
74+
{'standard_margin': float,
75+
'standard_margin_rate': float}
7676
}}
7777
7878
"""
79-
80-
if context is None:
81-
context = {}
82-
res = {}
83-
if not ids:
84-
return res
85-
for product in ids:
86-
res[product] = {'margin_absolute': 0, 'margin_relative': 0}
79+
context = context and context or {}
80+
res = {id: {} for id in ids}
8781
for product in self.read(cr, user, ids,
8882
['id', 'cost_price'], context=context):
8983
cost = product['cost_price']
@@ -101,9 +95,32 @@ def _compute_margin(self, cr, user, ids, field_name, arg, context=None):
10195
_res['standard_margin_rate'] = (sale - cost) / sale * 100
10296
return res
10397

98+
def _get_product_margin_change_from_tax(self, cr, uid, ids, context=None):
99+
"""Find the products to trigger when a Tax changes"""
100+
pt_obj = self.pool['product.template']
101+
pp_obj = self.pool['product.product']
102+
pt_ids = pt_obj.search(cr, uid, [
103+
'|', ('taxes_id', 'in', ids),
104+
('supplier_taxes_id', 'in', ids)], context=context)
105+
pp_ids = pp_obj.search(
106+
cr, uid, [('product_tmpl_id', 'in', pt_ids)], context=context)
107+
return pp_ids
108+
109+
_margin_triggers = {
110+
'product.product': (
111+
lambda self, cr, uid, ids, context=None:
112+
ids, None, 10),
113+
'account.tax': (
114+
_get_product_margin_change_from_tax, [
115+
'type', 'price_include', 'amount',
116+
'include_base_amount', 'child_depend'],
117+
10),
118+
}
119+
104120
_columns = {
105121
'standard_margin': fields.function(
106122
_compute_margin,
123+
store=_margin_triggers,
107124
method=True,
108125
string='Theorical Margin',
109126
digits_compute=dp.get_precision('Sale Price'),
@@ -114,6 +131,7 @@ def _compute_margin(self, cr, user, ids, field_name, arg, context=None):
114131
'the margin will be negativ.'),
115132
'standard_margin_rate': fields.function(
116133
_compute_margin,
134+
store=_margin_triggers,
117135
method=True,
118136
string='Theorical Margin (%)',
119137
digits_compute=dp.get_precision('Sale Price'),

0 commit comments

Comments
 (0)