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

Accounting Module v7 : Account move -- Create Method Issue

By
Yassine TEIMI
on 12/24/14, 12:09 PM 1,493 views

                                                              SHORT SITUATION EXPLAINATION  :

I am creating a method, trying to create a record on account.move object,

the account.move object had a one2many field into account.move.line, so as to fill this field while creating the account.move record, I used a list of dictionnaries like this : [{'name' : name, 'debit' : debit} , {'name' : name2 , 'debit' : debit}], so as every dictionnary of this list is corresponding to a record of the account.move.line.

                                                                 CODE AND ERROR TRACEBACK :

Here is the interesting part of my method : 

        line = [{}]
        line = [{
            'name' : 'Credit Client',
            'account_id' : acc_id,
            'debit' : debit,
        }]

        line.append({
            'name' : 'equilibrage',
            'account_id' : 726,
            'credit' : debit,
        })

        period = period_obj.find(cr, uid, date, context=None)
        period_res = period[0]
        move = {
            'ref' : num_bl,
            'line_id' : line,
            'journal_id' : 2,
            'date' : date,
            'company_id' : company_id,
            'period_id' : period_res,
        }

        move_id = move_obj.create(cr, uid, move, context=None)
        move_obj.post(cr, uid, [move_id], context=None)
        res = super(StockPickingOut, self).force_assign(cr, uid, ids, *args)

        return res

Here is the create method of account.move overrided : 

    def create(self, cr, uid, vals, context=None):
        if context is None:
            context = {}
        if 'line_id' in vals and context.get('copy'):
            for l in vals['line_id']:
                if not l[0]:
                    l[2].update({
                        'reconcile_id':False,
                        'reconcile_partial_id':False,
                        'analytic_lines':False,
                        'invoice':False,
                        'ref':False,
                        'balance':False,
                        'account_tax_id':False,
                        'statement_id': False,
                    })

            if 'journal_id' in vals and vals.get('journal_id', False):
                for l in vals['line_id']:
                    if not l[0]:
                        l[2]['journal_id'] = vals['journal_id']
                context['journal_id'] = vals['journal_id']
            if 'period_id' in vals:
                for l in vals['line_id']:
                    if not l[0]:
                        l[2]['period_id'] = vals['period_id']
                context['period_id'] = vals['period_id']
            else:
                default_period = self._get_period(cr, uid, context)
                for l in vals['line_id']:
                    if not l[0]:
                        l[2]['period_id'] = default_period
                context['period_id'] = default_period

        if vals.get('line_id', False):
            c = context.copy()
            c['novalidate'] = True
            c['period_id'] = vals['period_id'] if 'period_id' in vals else self._get_period(cr, uid, context)
            c['journal_id'] = vals['journal_id']
            if 'date' in vals: c['date'] = vals['date']

            result = super(account_move, self).create(cr, uid, vals, c)
            tmp = self.validate(cr, uid, [result], context)
            journal = self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context)
            if journal.entry_posted and tmp:
                self.button_validate(cr,uid, [result], context)
        else:
            result = super(account_move, self).create(cr, uid, vals, context)
        return result

Here is the server traceback :

File "/opt/openerp/server/openerp/addons/add_bl_valcr/stock.py", line 64, in force_assign move_id = move_obj.create(cr, uid, move, context=None) File "/opt/openerp/server/openerp/addons/account/account.py", line 1393, in create result = super(account_move, self).create(cr, uid, vals, c) File "/opt/openerp/server/openerp/osv/orm.py", line 4550, in create result += self._columns[field].set(cr, self, id_new, field, vals[field], user, rel_context) or [] File "/opt/openerp/server/openerp/osv/fields.py", line 557, in set if act[0] == 0: KeyError: 0

 

I think there is something wrong with the key 0 on the line 5 of create method, caused by the "line" type, it tried to access the object via the key 0, but it's not valid, I'm searching a simple and valid way to save these two records in account.move.line.

Should I override the create method too ? because it's used for another purpose, and because I am using it in different conditions.

Suggestions, solutions please ?

1
Mohamed Essakali
On 12/25/14, 4:54 AM

To create one2many records you must use tuples like this : (0,0,{'field1':value1,'field2':value2...}), other tuples that can be used :

(0, 0, { values }) link to a new record that needs to be created with the given values dictionary 

(1, ID, { values }) update the linked record with id = ID (write *values* on it) 

(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well) (3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself) 

(4, ID) link to existing record with id = ID (adds a relationship) 

(5) unlink all (like using (3,ID) for all linked records) 

(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)

For more details see https://doc.odoo.com/v6.0/developer/2_5_Objects_Fields_Methods/methods.html/

Good luck!

Thank you very much Ssi Mohamed, very interesting informations.

Yassine TEIMI
on 12/26/14, 5:10 AM

You're welcome Ssi Yassine ;)

Mohamed Essakali
on 12/26/14, 4:50 PM

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: 12/24/14, 12:09 PM
Seen: 1493 times
Last updated: 3/16/15, 8:10 AM