Browse Source

[WIP] sale_exception_by_domain

pull/1557/head
Mourad 6 years ago
committed by Iván Todorovich
parent
commit
4f9eec5363
  1. 50
      base_exception/models/base_exception.py
  2. 3
      base_exception/views/base_exception_view.xml

50
base_exception/models/base_exception.py

@ -41,6 +41,13 @@ class ExceptionRule(models.Model):
required=True, required=True,
) )
model = fields.Selection(selection=[], string='Apply on', required=True) model = fields.Selection(selection=[], string='Apply on', required=True)
type = fields.Selection(
selection=[('by_domain', 'By domain'),
('by_py_code', 'By Python Code')],
string='Exception Type', required=True)
domain = fields.Char('Domain')
active = fields.Boolean('Active') active = fields.Boolean('Active')
next_state = fields.Char( next_state = fields.Char(
'Next state', 'Next state',
@ -76,6 +83,19 @@ class ExceptionRule(models.Model):
select_vals select_vals
)) ))
@api.multi
def _get_domain(self):
""" override me to customize domains according exceptions cases """
self.ensure_one()
return safe_eval(self.domain)
@api.onchange('type',)
def onchange_type(self):
if self.type == 'by_domain':
self.code = False
elif self.type == 'by_py_code':
self.domain = False
class BaseException(models.AbstractModel): class BaseException(models.AbstractModel):
_name = 'base.exception' _name = 'base.exception'
@ -146,6 +166,7 @@ class BaseException(models.AbstractModel):
def detect_exceptions(self): def detect_exceptions(self):
"""returns the list of exception_ids for all the considered base.exceptions """returns the list of exception_ids for all the considered base.exceptions
""" """
import pdb; pdb.set_trace()
if not self: if not self:
return [] return []
exception_obj = self.env['exception.rule'] exception_obj = self.env['exception.rule']
@ -195,15 +216,25 @@ class BaseException(models.AbstractModel):
@api.multi @api.multi
def _detect_exceptions(self, model_exceptions, sub_exceptions): def _detect_exceptions(self, model_exceptions, sub_exceptions):
self.ensure_one() self.ensure_one()
import pdb; pdb.set_trace()
exception_ids = [] exception_ids = []
next_state_rule = False next_state_rule = False
for rule in model_exceptions: for rule in model_exceptions:
if self._rule_eval(rule, self.rule_group, self):
if rule.type == 'by_py_code' and self._rule_eval(
rule, self.rule_group, self):
exception_ids.append(rule.id) exception_ids.append(rule.id)
if rule.next_state:
if not next_state_rule or \
rule.sequence < next_state_rule.sequence:
next_state_rule = rule
elif rule.type == 'by_domain' and rule.domain:
domain = rule._get_domain()
domain.append(('id', '=', self.id))
if self.search(domain):
exception_ids.append(rule.id)
if rule.next_state:
if not next_state_rule or \
rule.sequence < next_state_rule.sequence:
next_state_rule = rule
if sub_exceptions: if sub_exceptions:
for obj_line in self._get_lines(): for obj_line in self._get_lines():
for rule in sub_exceptions: for rule in sub_exceptions:
@ -213,8 +244,15 @@ class BaseException(models.AbstractModel):
# (ex sale order line if obj is sale order) # (ex sale order line if obj is sale order)
continue continue
group_line = self.rule_group + '_line' group_line = self.rule_group + '_line'
if self._rule_eval(rule, group_line, obj_line):
if rule.type == 'by_py_code' and self._rule_eval(
rule, group_line, obj_line):
exception_ids.append(rule.id) exception_ids.append(rule.id)
elif rule.type == 'by_domain' and rule.domain:
domain = rule._get_domain()
domain.append(('id', '=', obj_line.id))
if obj_line.search(domain):
exception_ids.append(rule.id)
# set object to next state # set object to next state
if next_state_rule: if next_state_rule:
self.state = next_state_rule.next_state self.state = next_state_rule.next_state

3
base_exception/views/base_exception_view.xml

@ -40,9 +40,10 @@
<field name="rule_group"/> <field name="rule_group"/>
<field name="model"/> <field name="model"/>
<field name="next_state"/> <field name="next_state"/>
<field name="domain" attrs="{'invisible': [('type','!=','by_domain')]}"/>
</group> </group>
</group> </group>
<notebook>
<notebook attrs="{'invisible': [('type','!=','by_py_code')]}">
<page name="code" string="Python Code"> <page name="code" string="Python Code">
<field name="code" widget="ace" options="{'mode': 'python'}" placeholder="Enter Python code here. Help about Python expression is available in the help tab of this document."/> <field name="code" widget="ace" options="{'mode': 'python'}" placeholder="Enter Python code here. Help about Python expression is available in the help tab of this document."/>
</page> </page>

Loading…
Cancel
Save