Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Add automatically two articles for each Order

By
Selverine
on 2/4/15, 6:13 AM 873 views

Hi,

First, thank you for your time.

 

I would like to add automatically two articles for each Order. 

One: Transport. The second one: Services.

I would like after use the button create new order see directly these two articles on the order.

I try to make an insert at the beginning:

cr.execute('insert into sale_order_line( product_uom, order_id, price_unit, product_uom_qty,name,state,type,delay ) values (4, sale_id,1,1, \'transport\',\'draft\',\'make_to_order\',7)')

But it works only after I save the order. I suppose that before saving the order, we don't have sale_id no?

 

I try to create an function call by default:

    _defaults = {
        'date_order': fields.date.context_today,
        'order_policy': 'manual',
        'order_line' : _get_default_order_line,

 

    def _get_default_order_line(self, cr, uid, ids, context=None):
        context = context or {}
        arning = {}
        result = {}
        domain = {}
        warning_msgs = ''
        result['product_uom']=4
        result['order_id']=59
        result['price_unit']=1
        result['product_uom_qty']=1
        result['name']='transport'
        result['state']='draft'
        result['type']='make_to_order'
        result['delay']=7
        return result

 

But it doesn't work too :-(.

The error is:

Uncaught TypeError: Object #<Object> has no method 'slice'

 

Please, someone has an idea?

 

Thank you a lot,

 

Selverine

2

Cyril Gaspard (GEM)

--Cyril Gaspard (GEM)--
4136
| 5 6 8
Paris, France
--Cyril Gaspard (GEM)--

Forum contributor since 2008

Cyril Gaspard (GEM)
On 2/5/15, 8:27 PM

Hi,

solution 1:

just override ceate method,

def create(self, cr, uid, vals, context=None):

    context = context or {}

    sol = self.pool.get('sale.order.line')

    res = super(sale_order, self).create(cr, uid, vals, context=context)

    # res is the id of the created sale order, and we create 2 lines with product_1 and product_2

    self.write(cr, uid, [res], {'order_line': [[0, 0, {'product_id': id_of_product_1}], [0, 0, {''product_id}: id_of_product_2]]})

    # we must again do the onchange for each line (perhaps after the call of the onchange write the returned result in each line, I have not tested)

    for line in self.browse(cr, uid, res, context=context).order_line:

         sol.onchange_product_id(cr, uid, [line.id], line.product_id.id, ............) # see the complete method in module sale

    return res

solution 2: to display directly when you create a new record (code not tested)

override method default_get :

def default_get(self, cr, uid, fields, context=None):

    context = context or {}

    res = super(sale_order, self).default_get(cr,uid, fields, context=context)

    if 'order_line' in fields:

        res.update({'order_line': [(0, 0, {'product_id': id_of_product_1}), (0, 0, {''product_id: id_of_product_2})]})

    return res

for product_1 and product_2, if they are imported and having xml_id, define id of this products first by using their xml_id (to get the id of record which has an xml_id, use method get_object_reference)

it is better when you override a method, to call in this method a new created method in which you do your treatment.

Bye

 

Thank you! It works! I just didn't put the line sol.onchange_product_id(cr, uid, [line.id], line.product_id.id, ............). But when I am saving the order, I see two new lignes ;-). Just to know, it is not possible to see the two new product just after creation and not after saving the order? Thank you! Selverine

Selverine
on 2/14/15, 10:52 AM

Hi, I had a second solution by overriding method default_get, but I didn't tested it, but should work. Hoping this will work. Bye

Cyril Gaspard (GEM)
on 2/19/15, 7:43 PM
1

zbik

--zbik--
5941
| 6 4 7
Lodz, Poland
--zbik--


zbik
On 2/5/15, 11:05 AM
  1. Module delivery allows you to add delivery methods and cost in sale orders, as sale order line.
  2. If you want to be sure to add both, at one moment, see as it was done in the module delivery, method delivery_set()

 

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 2/4/15, 6:13 AM
Seen: 873 times
Last updated: 3/17/15, 7:32 PM