Odoo How to insert account.move.line to account.move by code?

Viewed 2981

I have the problem when I insert account.move.line to account.move by code.

It always said 'Cannot create unbalanced journal entry.'.

Are there someway to add account.move.line to account.move by code by code?

ps. debit_line and credit_line have the same amount of credit and debit.

The following is my code:

 # -*- coding: utf-8 -*-

 from odoo import models, fields, api
 import logging

 _logger = logging.getLogger(__name__)

 class JobLine(models.Model):
     _inherit = 'product.job_line'
     value = fields.Float()
     journal_entry = fields.Many2one('account.move')
     cost = fields.Float()

     @api.onchange('is_finish')
     def update_journal(self):
         if self.is_finish:
            revenue_account = self.product.revenue_account
             deferred_receivable_account = self.env['ir.values'].get_default('job.config.settings','deferred_receivable_account')
             journal = self.env['ir.values'].get_default('job.config.settings','journal')
            if not self.journal_entry :
                self.journal_entry = self.env['account.move'].create({
                    'journal_id': journal,
                    'partner_id': self.container.partner.id,
                    'date': fields.Date.context_today(self)
                    })
             # if self.container.order.invoice_count > 0:
             #  _logger.info('Type 1  : ')
             # else:
            #   _logger.info('Type 2  : ')
            debit_line = self.env['account.move.line'].create({
                 'move_id': self.journal_entry.id,
                 'account_id': deferred_receivable_account,
                'partner_id': self.container.partner.id,
                'name': 'Finish '+self.job_name,
                'debit': self.cost
            })
            credit_line = self.env['account.move.line'].create({
                'move_id': self.journal_entry.id,
                'account_id': self.product.revenue_account,
                'partner_id': self.container.partner.id,
                'name': 'Finish '+self.job_name,
                'credit': self.cost
             })
             _logger.info(str(debit_line)+' '+str(credit_line))
            self.journal_entry.line_ids+=debit_line
            self.journal_entry.line_ids+=credit_line
        else:
            self.journal_entry.unlink()
            _logger.info('No finish Yet '+str(self.journal_entry))
1 Answers

Instead of this

credit_line = self.env['account.move.line'].create({
            'move_id': self.journal_entry.id,
            'account_id': self.product.revenue_account,
            'partner_id': self.container.partner.id,
            'name': 'Finish '+self.job_name,
            'credit': self.cost
         })

Try this

credit_line = self.env['account.move.line'].with_context(
            check_move_validity=False).create({
            'move_id': self.journal_entry.id,
            'account_id': self.product.revenue_account,
            'partner_id': self.container.partner.id,
            'name': 'Finish '+self.job_name,
            'credit': self.cost
         })

by passing check_move_validity to False in context you prevent the model to check data consistency after move line creation. link to source code

Hope he will help :-)

Related