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

Transfer custom field value from Sale Order Line to Invoice Line

By
Santi
on 6/5/14, 9:55 AM 2,955 views

I've created a custom boolean field that appears in Sale Order lines and Invoice lines, and now I'm trying to transfer the values of the field to the Invoice when created from the Sale Order (wether the checkbox is marked or not...TRUE or FALSE), but for some reason my code is not working as expected, I'm not getting any error, but the values aren't being transfered from one module to the other either.

This is my code:

ti_print_report_lines_choice.py:

from openerp.osv import fields, osv, orm
from tools.translate import _

#Create fields in different models

class ti_print_choice_sale_order_line(osv.osv):

  _inherit = 'sale.order.line'
  _columns = {
    'x_printable_choice': fields.boolean(
      "Don't print",
      help='Mark if this product will NOT be printed in reports'
    ),
  }
ti_print_choice_sale_order_line()

class ti_print_choice_account_invoice_line(osv.osv):

  _inherit = "account.invoice.line"
  _columns = {
    'x_printable_choice': fields.boolean(
      "Don't print",
      help='Mark if this product will NOT be printed in reports'
    ),
  }
ti_print_choice_account_invoice_line()


#Copy fields between modules: From Sale Order to Invoice


class sale_order(orm.Model):
    _inherit = 'sale.order'

    def _prepare_order_line_invoice_line(self, cr, uid, line, account_id=False, context=None):

        res = super(sale_order, self)._prepare_invoice(
            cr, uid, line, account_id=False, context=context
        )
        res.update({
            'x_printable_choice': line.x_printable_choice,
            })
        return res


sale_order()

I will be doing the same for Purchase Orders and Stock Moves...but for the sake of simplicity I've excluded it from this question

Any help or insight is appreciated 

 

1
Eric
On 6/5/14, 12:38 PM

The _prepare_invoice() method only returns a dictionary of the invoice values.  You will need to call the _make_invoice() method to create the invoice record.  Once the record is created, then you can update the boolean field on the invoice with the value set on the sales order

0
Santi
On 6/17/14, 5:59 AM

thanks for you reply Eric. But I'm a bit lost in figuring out how to accomplish your suggestion. If you have some spare time, can you provide me some specific details?

This is the _make_invoice() method in sale.py:

 

    def _make_invoice(self, cr, uid, order, lines, context=None):
        inv_obj = self.pool.get('account.invoice')
        obj_invoice_line = self.pool.get('account.invoice.line')
        if context is None:
            context = {}
        invoiced_sale_line_ids = self.pool.get('sale.order.line').search(cr, uid, [('order_id', '=', order.id), ('invoiced', '=', True)], context=context)
        from_line_invoice_ids = []
        for invoiced_sale_line_id in self.pool.get('sale.order.line').browse(cr, uid, invoiced_sale_line_ids, context=context):
            for invoice_line_id in invoiced_sale_line_id.invoice_lines:
                if invoice_li d.invoice_id.id not in from_line_invoice_ids:
                    from_line_invoice_ids.append(invoice_line_id.invoice_id.id)
        for preinv in order.invoice_ids:
            if preinv.state not in ('cancel',) and preinv.id not in from_line_invoice_ids:
                for preline in preinv.invoice_line:
                    inv_line_id = obj_invoice_line.copy(cr, uid, preline.id, {'invoice_id': False, 'price_unit': -preline.price_unit})
                    lines.append(inv_line_id)
        inv = self._prepare_invoice(cr, uid, order, lines, context=context)
        inv_id = inv_obj.create(cr, uid, inv, context=context)
        data = inv_obj.onchange_payment_term_date_invoice(cr, uid, [inv_id], inv['payment_term'], time.strftime(DEFAULT_SERVER_DATE_FORMAT))
        if data.get('value', False):
            inv_obj.write(cr, uid, [inv_id], data['value'], context=context)
        inv_obj.button_compute(cr, uid, [inv_id])
        return inv_id

 

 

Anybody has resolved this?

David
on 11/20/14, 12:34 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

2 follower(s)

Stats

Asked: 6/5/14, 9:55 AM
Seen: 2955 times
Last updated: 8/27/16, 4:11 AM