diff --git a/addons/base_action_rule/base_action_rule.py b/addons/base_action_rule/base_action_rule.py index 856d2626f69..349721db8c4 100644 --- a/addons/base_action_rule/base_action_rule.py +++ b/addons/base_action_rule/base_action_rule.py @@ -109,7 +109,7 @@ trigger date, like sending a reminder 15 minutes before a meeting."), _order = 'sequence' - def post_action(self, cr, uid, ids, model, context=None): + def post_action(self, cr, uid, ids, model, old_records_states=None, context=None): # Searching for action rules cr.execute("SELECT model.model, rule.id FROM base_action_rule rule \ LEFT JOIN ir_model model on (model.id = rule.model_id) \ @@ -117,11 +117,11 @@ trigger date, like sending a reminder 15 minutes before a meeting."), res = cr.fetchall() # Check if any rule matching with current object for obj_name, rule_id in res: - obj = self.pool.get(obj_name) + model_pool = self.pool.get(obj_name) # If the rule doesn't involve a time condition, run it immediately # Otherwise we let the scheduler run the action if self.browse(cr, uid, rule_id, context=context).trg_date_type == 'none': - self._action(cr, uid, [rule_id], obj.browse(cr, uid, ids, context=context), context=context) + self._action(cr, uid, [rule_id], model_pool.browse(cr, uid, ids, context=context), old_records_states=old_records_states, context=context) return True def _create(self, old_create, model, context=None): @@ -144,13 +144,23 @@ trigger date, like sending a reminder 15 minutes before a meeting."), `post_action`, in that order. """ def wrapper(cr, uid, ids, vals, context=context): + old_records_states = {} if context is None: context = {} if isinstance(ids, (str, int, long)): ids = [ids] + model_pool = self.pool.get(model) + # get the old records (before the write) + if model and ids: + for record in model_pool.browse(cr,uid,ids,context=context): + old_records_states[record.id] = record.state + #old_records = model_pool.browse(cr,uid,ids,context=context) + #print old_records[0] + #print old_records[0].state old_write(cr, uid, ids, vals, context=context) + #print old_records[0].state if not context.get('action'): - self.post_action(cr, uid, ids, model, context=context) + self.post_action(cr, uid, ids, model, old_records_states=old_records_states, context=context) return True return wrapper @@ -242,7 +252,7 @@ trigger date, like sending a reminder 15 minutes before a meeting."), - def do_check(self, cr, uid, action, obj, context=None): + def do_check(self, cr, uid, action, obj, old_records_states=None, context=None): """ check Action """ if context is None: context = {} @@ -263,14 +273,21 @@ trigger date, like sending a reminder 15 minutes before a meeting."), (action.trg_partner_categ_id.id in map(lambda x: x.id, obj.partner_id.category_id or [])) ) ) - state_to = context.get('state_to', False) - state = getattr(obj, 'state', False) - if state: - ok = ok and (not action.trg_state_from or action.trg_state_from==state) - if state_to: - ok = ok and (not action.trg_state_to or action.trg_state_to==state_to) - elif action.trg_state_to: - ok = False + #state_to = the state after a write or a create + state_to = getattr(obj, 'state', False) + #state_from = nothing or the state of old_records + state_from = "" + if old_records_states != None: + if old_records_states[obj.id]: + state_from = old_records_states[obj.id] + #if we have an action that check the status + if action.trg_state_from: + print action.trg_state_from, " and ", state_to + ok = ok and action.trg_state_from==state_to + if action.trg_state_to: + print action.trg_state_to, " for ", state_from, " and ", state_to + ok = state_from!=state_to + ok = ok and action.trg_state_to==state_to reg_name = action.regex_name result_name = True if reg_name: @@ -312,19 +329,16 @@ trigger date, like sending a reminder 15 minutes before a meeting."), model_obj.message_subscribe(cr, uid, [obj.id], new_followers, context=context) return True - def _action(self, cr, uid, ids, objects, scrit=None, context=None): + def _action(self, cr, uid, ids, objects, scrit=None, old_records_states=None, context=None): """ Do Action """ if context is None: context = {} - context.update({'action': True}) - if not scrit: - scrit = [] if not isinstance(objects, list): objects = [objects] for action in self.browse(cr, uid, ids, context=context): for obj in objects: - if self.do_check(cr, uid, action, obj, context=context): + if self.do_check(cr, uid, action, obj, old_records_states=old_records_states, context=context): self.do_action(cr, uid, action, obj, context=context) context.update({'action': False}) diff --git a/addons/base_status/base_stage.py b/addons/base_status/base_stage.py index 86edff46920..ba4f62a181a 100644 --- a/addons/base_status/base_stage.py +++ b/addons/base_status/base_stage.py @@ -19,6 +19,7 @@ # ############################################################################## +import pdb from osv import fields, osv from tools.translate import _ @@ -280,15 +281,10 @@ class base_stage(object): # 2. update values if values_to_update: self.write(cr, uid, ids, values_to_update, context=context) - # 3. call _action for base action rule - if new_state_name: - self._action(cr, uid, cases, new_state_name, context=context) - elif not (new_stage_id is None): - new_state_name = self.read(cr, uid, ids, ['state'], context=context)[0]['state'] - self._action(cr, uid, cases, new_state_name, context=context) return True def _action(self, cr, uid, cases, state_to, scrit=None, context=None): + print "--- Base_Stage (_action) ---" if context is None: context = {} context['state_to'] = state_to diff --git a/addons/base_status/base_state.py b/addons/base_status/base_state.py index cebf5537485..aad9129406e 100644 --- a/addons/base_status/base_state.py +++ b/addons/base_status/base_state.py @@ -157,13 +157,14 @@ class base_state(object): :params: update_values: values that will be added with the state update when writing values to the record. """ + print "-------------------------BASE STATE--------------------------------" cases = self.browse(cr, uid, ids, context=context) cases[0].state # fill browse record cache, for _action having old and new values if update_values is None: update_values = {} update_values['state'] = state_name self.write(cr, uid, ids, update_values, context=context) - self._action(cr, uid, cases, state_name, context=context) + #self._action(cr, uid, cases, state_name, context=context) def _action(self, cr, uid, cases, state_to, scrit=None, context=None): if context is None: diff --git a/addons/crm/crm_action_rule.py b/addons/crm/crm_action_rule.py index 4a8ee98fbae..d7caafd7d1d 100644 --- a/addons/crm/crm_action_rule.py +++ b/addons/crm/crm_action_rule.py @@ -43,8 +43,8 @@ class base_action_rule(osv.osv): 'act_categ_id': fields.many2one('crm.case.categ', 'Set Category to'), } - def do_check(self, cr, uid, action, obj, context=None): - ok = super(base_action_rule, self).do_check(cr, uid, action, obj, context=context) + def do_check(self, cr, uid, action, obj, old_records_states=None, context=None): + ok = super(base_action_rule, self).do_check(cr, uid, action, obj, old_records_states=old_records_states, context=context) if hasattr(obj, 'section_id'): ok = ok and (not action.trg_section_id or action.trg_section_id.id == obj.section_id.id) diff --git a/addons/crm/crm_lead.py b/addons/crm/crm_lead.py index 0a824aef83e..b9d92639e63 100644 --- a/addons/crm/crm_lead.py +++ b/addons/crm/crm_lead.py @@ -29,6 +29,7 @@ from tools.translate import _ from tools import html2plaintext from base.res.res_partner import format_address +import pdb CRM_LEAD_PENDING_STATES = ( crm.AVAILABLE_STATES[2][0], # Cancelled @@ -380,8 +381,10 @@ class crm_lead(base_stage, format_address, osv.osv): """ Mark the case as lost: state=cancel and probability=0 """ for lead in self.browse(cr, uid, ids): stage_id = self.stage_find(cr, uid, [lead], lead.section_id.id or False, [('probability', '=', 0.0)], context=context) - if stage_id: - self.case_set(cr, uid, [lead.id], values_to_update={'probability': 0.0}, new_stage_id=stage_id, context=context) + if not stage_id: + return False + else: + self.write(cr, uid, ids, {'probability' : 0.0, 'stage_id' : stage_id}, context=context) self.case_mark_lost_send_note(cr, uid, ids, context=context) return True @@ -389,8 +392,10 @@ class crm_lead(base_stage, format_address, osv.osv): """ Mark the case as lost: state=done and probability=100 """ for lead in self.browse(cr, uid, ids): stage_id = self.stage_find(cr, uid, [lead], lead.section_id.id or False, [('probability', '=', 100.0)], context=context) - if stage_id: - self.case_set(cr, uid, [lead.id], values_to_update={'probability': 100.0}, new_stage_id=stage_id, context=context) + if not stage_id: + return False + else: + self.write(cr, uid, ids, {'probability' : 100.0, 'stage_id' : stage_id}, context=context) self.case_mark_won_send_note(cr, uid, ids, context=context) return True diff --git a/addons/crm_claim/crm_claim.py b/addons/crm_claim/crm_claim.py index 70dcc18baf7..5c141c81080 100644 --- a/addons/crm_claim/crm_claim.py +++ b/addons/crm_claim/crm_claim.py @@ -167,8 +167,10 @@ class crm_claim(base_stage, osv.osv): """ Mark the case as refused: state=done and case_refused=True """ for lead in self.browse(cr, uid, ids): stage_id = self.stage_find(cr, uid, [lead], lead.section_id.id or False, ['&', ('state', '=', 'done'), ('case_refused', '=', True)], context=context) - if stage_id: - self.case_set(cr, uid, [lead.id], values_to_update={}, new_stage_id=stage_id, context=context) + if not stage_id: + return False + else: + self.write(cr, uid, ids, {'stage_id' : stage_id}, context=context) return self.case_refuse_send_note(cr, uid, ids, context=context) def onchange_partner_id(self, cr, uid, ids, part, email=False):