It is currently Sat Feb 11, 2012 12:15 pm

All times are UTC + 2 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Errors modifying sale workflow
PostPosted: Mon Aug 31, 2009 2:41 pm 
Offline

Joined: Tue Aug 04, 2009 5:52 pm
Posts: 172
Hi everyone,

I tried to modify action_ship_end in order to send a request to the sale-responsible salesman with an due date of today + 45 days for products with a 60days warranty.
Code:
    def action_ship_end(self, cr, uid, ids, context={}):
        for order in self.browse(cr, uid, ids):
            val = {'shipped':True}
            if order.state=='shipping_except':
                val['state'] = 'progress'
                if (order.order_policy == 'manual'):
                    for line in order.order_line:
                        if (not line.invoiced) and (line.state not in ('cancel','draft')):
                            val['state'] = 'manual'
                            break
            for line in order.order_line:
                towrite = []
                #
                #si un produit a 3 mois de garantie, alors rajouter un objet de type res.request a ←ch←ance today+45 jours
                #
                for product_id in line:
                    for product_tmpl_id in product_id:
                        if product_tmpl_id.warranty==3:
                            self.pool.get('res.request').create('body':'Attention, 15 jours avant la fin de la garantie pour le client '+name,'name':'Rappel garantie','active':true,'trigger_date':date.today+datetime.delta(days=45),'acto_to':user_id,'acto_from':user_id)

                if line.state=='exception':
                    towrite.append(line.id)
                if towrite:
                    self.pool.get('sale.order.line').write(cr, uid, towrite, {'state':'done'},context=context)
            self.write(cr, uid, [order.id], val)
        return True


But when I launch the server and I try to connect with a gtk client, the client crash with a error message in server log which says that "sales" couldn't have been loaded.

Any tip for this problem ?

_________________
Cordialement, Regards,
Gauthier BEAURIN


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 01, 2009 10:12 am 
Offline

Joined: Mon Dec 03, 2007 8:22 am
Posts: 1472
Location: Belgium
You should first call super method and then write res.request code as you are overriding action_ship_end method.

_________________
OpenERP=Easier, Adaptable, Affordable, Modular


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 01, 2009 10:55 am 
Offline

Joined: Tue Aug 04, 2009 5:52 pm
Posts: 172
Actually, I just forgot few things in my self.pool.get('res.request').create .
I didn't know that every function needed few arguments like cr or uid .
So here is the good function to call.

Code:
self.pool.get('res.request').create(cr, uid, {'body':'Attention, 15 jours avant la fin de la garantie pour le client '+name,'name':'Rappel garantie','active':true,'trigger_date':date.today+datetime.delta(days=45),'act_to':user_id,'act_from':user_id})
[/code]

_________________
Cordialement, Regards,
Gauthier BEAURIN


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 01, 2009 11:45 am 
Offline

Joined: Tue Aug 04, 2009 5:52 pm
Posts: 172
Sorry for the double post.
But I actually manage to create a sale order which go threw my new function, and it returns me this error

Code:
Environment Information :
System : Windows-Vista-6.1.7600
OS Name : nt
Operating System Release : Vista
Operating System Version : 6.1.7600
Operating System Architecture : 32bit
Operating System Locale : fr_FR.cp1252
Python Version : 2.5.2
OpenERP-Client Version : 5.0.3
Last revision No. & ID :Bazaar Package not Found !Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/openerp-server/netsvc.py", line 242, in dispatch
    result = LocalService(service_name)(method, *params)
  File "/usr/lib/python2.5/site-packages/openerp-server/netsvc.py", line 73, in __call__
    return getattr(self, method)(*params)
  File "/usr/lib/python2.5/site-packages/openerp-server/service/web_services.py", line 639, in execute
    return self._execute(db, uid, wiz_id, datas, action, context)
  File "/usr/lib/python2.5/site-packages/openerp-server/service/web_services.py", line 619, in _execute
    return wiz.execute(db, uid, self.wiz_datas[wiz_id], action, context)
  File "/usr/lib/python2.5/site-packages/openerp-server/wizard/__init__.py", line 178, in execute
    res = self.execute_cr(cr, uid, data, state, context)
  File "/usr/lib/python2.5/site-packages/openerp-server/wizard/__init__.py", line 169, in execute_cr
    return self.execute_cr(cr, uid, data, next_state, context)
  File "/usr/lib/python2.5/site-packages/openerp-server/wizard/__init__.py", line 74, in execute_cr
    action_res = action(self, cr, uid, data, context)
  File "/usr/lib/python2.5/site-packages/openerp-server/addons/project/wizard/close_task.py", line 90, in _do_close
    task_obj.do_close(cr, uid, data['ids'], context)
  File "/usr/lib/python2.5/site-packages/openerp-server/addons/project_mrp/project.py", line 38, in do_close
    wf_service.trg_validate(uid, 'mrp.procurement', task.procurement_id.id, 'subflow.done', cr)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/wkf_service.py", line 81, in trg_validate
    res2 = instance.validate(cr, id, ident, signal)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/instance.py", line 50, in validate
    workitem.process(cr, witem, ident, signal, force_running, stack=stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 62, in process
    ok = _split_test(cr, workitem, activity['split_mode'], ident, signal, stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 175, in _split_test
    _join_test(cr, t[0], t[1], ident, stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 183, in _join_test
    create(cr,[activity], inst_id, ident, stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 42, in create
    process(cr, res, ident, stack=stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 62, in process
    ok = _split_test(cr, workitem, activity['split_mode'], ident, signal, stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 175, in _split_test
    _join_test(cr, t[0], t[1], ident, stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 183, in _join_test
    create(cr,[activity], inst_id, ident, stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 42, in create
    process(cr, res, ident, stack=stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 54, in process
    result = _execute(cr, workitem, activity, ident, stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 108, in _execute
    wkf_expr.execute(cr, ident, workitem, activity)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/wkf_expr.py", line 68, in execute
    return _eval_expr(cr, ident, workitem, activity['action'])
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/wkf_expr.py", line 58, in _eval_expr
    ret = eval(line, env)
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.5/site-packages/openerp-server/osv/orm.py", line 168, in <lambda>
    return lambda *args, **argv: getattr(self._table, name)(self._cr, self._uid, [self._id], *args, **argv)
  File "/usr/lib/python2.5/site-packages/openerp-server/addons/mrp/mrp.py", line 1151, in action_done
    wf_service.trg_trigger(uid, 'mrp.procurement', id, cr)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/wkf_service.py", line 57, in trg_trigger
    instance.update(cr, instance_id, ident)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/instance.py", line 59, in update
    workitem.process(cr, witem, ident, stack=stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 62, in process
    ok = _split_test(cr, workitem, activity['split_mode'], ident, signal, stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 175, in _split_test
    _join_test(cr, t[0], t[1], ident, stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 183, in _join_test
    create(cr,[activity], inst_id, ident, stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 42, in create
    process(cr, res, ident, stack=stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 54, in process
    result = _execute(cr, workitem, activity, ident, stack)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/workitem.py", line 108, in _execute
    wkf_expr.execute(cr, ident, workitem, activity)
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/wkf_expr.py", line 68, in execute
    return _eval_expr(cr, ident, workitem, activity['action'])
  File "/usr/lib/python2.5/site-packages/openerp-server/workflow/wkf_expr.py", line 58, in _eval_expr
    ret = eval(line, env)
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.5/site-packages/openerp-server/osv/orm.py", line 168, in <lambda>
    return lambda *args, **argv: getattr(self._table, name)(self._cr, self._uid, [self._id], *args, **argv)
  File "/usr/lib/python2.5/site-packages/openerp-server/addons/sale/sale.py", line 653, in action_ship_end
    for product_id in line:
  File "/usr/lib/python2.5/site-packages/openerp-server/osv/orm.py", line 166, in __getitem__
    elif hasattr(self._table, name):
TypeError: hasattr(): attribute name must be string


It seems that my second "for" loop is not used as it should.

Any tip ?

(french version : http://www.openobject.com/forum/post42462.html#42462 )

_________________
Cordialement, Regards,
Gauthier BEAURIN


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 01, 2009 12:19 pm 
Offline

Joined: Mon Dec 03, 2007 8:22 am
Posts: 1472
Location: Belgium
here is the problem

Code:
for product_id in line:
                    for product_tmpl_id in product_id:
                        if product_tmpl_id.warranty==3:
                            self.pool.get('res.request').create('body':'Attention, 15 jours avant la fin de la garantie pour le client '+name,'name':'Rappel garantie','active':true,'trigger_date':date.today+datetime.delta(days=45),'acto_to':user_id,'acto_from':user_id)

                if line.state=='exception':
                    towrite.append(line.id)
                if towrite:
                    self.pool.get('sale.order.line').write(cr, uid, towrite, {'state':'done'},context=context)
            self.write(cr, uid, [order.id], val)


It should be like this.

Code:
if line.product_id and line.product_id.product_tmpl_id and line.product_id.product_tmpl_id.warranty==3:
    self.pool.get('res.request').create('body':'Attention, 15 jours avant la fin de la garantie pour le client '+name,'name':'Rappel garantie','active':true,'trigger_date':date.today+datetime.delta(days=45),'acto_to':user_id,'acto_from':user_id)

    if line.state=='exception':
        towrite.append(line.id)
        self.pool.get('sale.order.line').write(cr, uid, towrite, {'state':'done'},context=context)
        self.write(cr, uid, [order.id], val)


You should run for loop on one2many and many2many relations only.
for order and order line has one2many relationship so it will work fien.
but each line has only one product so you can access it directly like line.product_id.

_________________
OpenERP=Easier, Adaptable, Affordable, Modular


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 01, 2009 4:14 pm 
Offline

Joined: Tue Aug 04, 2009 5:52 pm
Posts: 172
Code:
    def action_ship_end(self, cr, uid, ids, context={}):
        for order in self.browse(cr, uid, ids):
            val = {'shipped':True}
            if order.state=='shipping_except':
                val['state'] = 'progress'
                if (order.order_policy == 'manual'):
                    for line in order.order_line:
                        if (not line.invoiced) and (line.state not in ('cancel','draft')):
                            val['state'] = 'manual'
                            break
            for line in order.order_line:
                towrite = []
                if line.product_id and line.product_id.product_tmpl_id and line.product_id.product_tmpl_id.warranty==3:
                    request_new = {
                        'body': 'Attention, 15 jours avant la fin de la garantie pour le client '+order.partner_id.name,
                        'name': 'Rappel garantie',
                        'active': True,
                        'trigger_date': datetime.date.today()+datetime.timedelta(days=45),
                        'act_to': order.user_id.id,
                        'act_from': order.user_id.id,
                    }
                    self.pool.get('res.request').create(cr, uid, request_new)
                if line.state=='exception':
                    towrite.append(line.id)
                if towrite:
                    self.pool.get('sale.order.line').write(cr, uid, towrite, {'state':'done'},context=context)
            self.write(cr, uid, [order.id], val)
        return True[quote]

This code finally works. Enjoy it as I enjoyed it.[/quote]

_________________
Cordialement, Regards,
Gauthier BEAURIN


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 15, 2009 10:23 am 
Offline

Joined: Tue Sep 15, 2009 9:37 am
Posts: 3
After seeing your code I felt that you should use switch case rather than if.. else statements. Because your code is looking too clumsy and cumbersome. So you should go for shorthands.


Top
 Profile  
 
 Post subject: Re:
PostPosted: Mon Nov 15, 2010 12:30 pm 
Offline

Joined: Sat Nov 17, 2007 12:35 am
Posts: 265
Location: Madeira, PT
Quote:
After seeing your code I felt that you should use switch case rather than if.. else statements. Because your code is looking too clumsy and cumbersome. So you should go for shorthands.


what are you talking about? Is there a 'switch .. case' statement in python?
In which version?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC + 2 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:

Protected by Anti-Spam ACP