Skip to Content
Menu
This question has been flagged

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 

 

Avatar
Discard
Best Answer

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

Avatar
Discard
Author Best Answer

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

 

 

Avatar
Discard

Anybody has resolved this?

Related Posts Replies Views Activity
2
Feb 16
4312
2
Jan 24
3675
0
Jun 23
942
1
Dec 22
1962
0
Apr 22
1404