3156938
3156938

Reputation: 55

Openerpv7: Numbeing in OrderLine

I'm using OpenERP v7 and requesting this below output in Purchase order line window

PO0001      PO0002    PO0003  
  Line      Line      Line
  10          10        10
  20          20        20
  30                    30

My code:

<field name="order_line" context="{'order_id':active_id}">

_columns={
'sequence':fields.integer('Sequence', store=True),
}
_defaults = {
 lambda obj, cr, uid, context: obj._default_sequence(cr, uid, context)
}
 def _default_sequence(self, cr, uid, context=None):
    if context is None:
        context = {}
    max_sequence = 0
    if context.get('order_id'):
        order_line = self.search(cr, uid, [('order_id', '=', context['order_id'])])
        if order_line:
            max_sequence = max([line['sequence'] for line in self.read(cr, uid, order_line, ['sequence'])])
    return max_sequence + 10

But im getting this kind of output . Any advice would be greatful.

PO0001      PO0002    PO0003  
  Line      Line      Line
  10          10        10
  10          10        10
  10                    10

Anyone could help me to achive the required output.

Upvotes: 3

Views: 1338

Answers (3)

OmaL
OmaL

Reputation: 5044

I think you are doing wrong. A record will be created only after you click on any button action(button other than adding the lines) for the first time for the record. Then only you will be able to make the search. I hope default will get the context even if we don't specify context. Still not getting the context, then try by following the answer given by Andrei Boyanov.Also please understand that field names like active, state, sequence etc have some super powers in openerp. For example if a table have a field "sequence", then the table's tree view will have a drag and drop functionality and if we do the drag and drop functionality, then a new sequence for each record in the table will be automatically calculated and saved.

So my opinion is not to use the sequence field. Use any other field name. Then write a on_change method to your one2many field and pass the one2many field name as the attribute to the on_change method. Then in the on_change method check what all values are coming when you add a new line. If the new line's value is in the format (0,0,dictionary_with_fields_as_keys_and_its_values_as_values).Then to this dictionary add the sequence_number with the value.For example:

<field name="order_line" on_change="onchange_line(order_line)">

def onchange_line(self, cr, uid, ids, lines,context=None):
    result = {}
    result['value'] = {}
    #do the proper checking
    count_dict = {}
    count = 10
    had_seq = 0
    for index,line in enumerate(lines):
        if line[0] == 0:
            count_dict[index] = count
            count +=10
        else:
            had_seq +=1
    #seqnece_no is the new sequence field defined
    for k in count_dict:
        if had_seq:
            lines[k][2]['sequence_no'] = had_seq*10 + count_dict[k]
        else:
            lines[k][2]['sequence_no'] = count_dict[k]
    result['value'].update({'order_line':lines})
    return result

Please try it.

Upvotes: 2

Atul Arvind
Atul Arvind

Reputation: 16743

change your Python file by

def _default_sequence(self, cr, uid, context=None):
    if not context: context = {}
    max_sequence = 0
    if context.get('order_id'):
        order_line = self.search(cr, uid, [('order_id', '=', context['order_id'])])
        if order_line:
            max_sequence = max([line['sequence'] for line in self.read(cr, uid, order_line, ['sequence'])])
    return max_sequence + 10

_columns={
    'sequence':fields.integer('Sequence'),
}

_defaults = {
     'sequence': _default_sequence
}

No need to put store=True in the integer field, it will always store value in the database.

Upvotes: 0

Andrei Boyanov
Andrei Boyanov

Reputation: 2393

How do you execute _default_sequence(...) ? I suppose you intend to invoke it trough the _defaults dictionary. In this case correct it as follow:

_defaults = {
     'sequence': lambda obj, cr, uid, context: obj._default_sequence(cr, uid, context)
}

In your code you omitted the key sequence. In fact what you defined is not a dictionary but a set with one element - the lambda function.

Better, you can define it just like this:

_defaults = {
     'sequence': _default_sequence
}

Upvotes: 1

Related Questions