This question has been flagged
1 Reply
11715 Views

I'd like that every time, I add an item in a sale order, the sequence field is automatically incremented by 10.

What I did:

class sale_order_line(osv.osv):

    _inherit = 'sale.order.line'

    _columns = {
       'sequence': fields.char('Sequence', help="Gives the sequence order when displaying a list of sales order lines."),
               }

    _defaults = {
       'sequence':lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'sale.order.line'),    
            }

sale_order_line()

 <record id="seq_type_sale_order_line" model="ir.sequence.type">
     <field name="name">Sale Order Line</field>
     <field name="code">sale.order.line</field>
</record>
<record id="seq_sale_order_line" model="ir.sequence">
     <field name="name">Sale Order Line</field>
     <field name="code">sale.order.line</field>            
     <field name="padding">3</field>
     <field name="number_increment">10</field>
     <field name="number_next">10</field>
 </record> 

The sequence is incremented, but when I create a new sale order, it doesn't reset to 10. If I have SO1 with 3 sale lines, I have sequences 010, 020 and 030. But in SO2, I have 040 and 050, instead of 010 and 020.

How can I modify my code?

Avatar
Discard

It doesn't reset automatically. It will continuously increases everytime you fetch the record. Why do you need to start from '10' everytime?

You can use functional field in sales order line for creating a sequence as '010' instead using ir.sequence. Else you can reset sequence every time when a sales order created.

Author

Thanks for your answers. I need to start from '10' everytime because it's clearer, if I have many sales orders with many lines increasing everytime, I would attend numbers like 653240, and it would be difficult to distinguish which line has the lowest sequence. I tried using an onchange method, but I cannot call it in xml file because I should call it in sale.order model () but the onchange method is defined in sale.order model.

Best Answer

Hello Wided,

Here you don't need to create ir.sequence record ,because it will always incremented.

better solution is to override default_get method of sale order line.

I have already given answer for this. Have a look on this answer it will help you.

https://www.odoo.com/forum/help-1/question/how-to-generate-automatic-sequence-number-in-one2many-field-67834#answer-67880

Regards,

Anil.

Avatar
Discard
Author

Thanks Anil Kesariya, I've tried your code, but I always have 10 in my field "sequence", after debugging, I found that the line: if len(context.get('order_line')) > 0 is not executed, because context.get('order_line')=[], so len ( context.get('order_line')) is always 0. I don't understand context in openerp very well, I don't know how to change the code to work.

you need to pass your one2many field on context. whatever fields name is for eg. if sale order line than pass . you need to inherit the sale view and add context on sale order line field than it will work. so our default get method will get the line value. if any line added in your current sale order record.

Author

I did that: But I still get always 10 in sequence field.

Author

I've found that the function is working when I use "add an item", but it's not working when I use "save and create". I'm trying to make it work when using "save and create".