Skip to Content
Menu
This question has been flagged

Hi,i have been put maximum effort on these i know these have been ask a lot and i have followed almost every post and i haven't achieve what i want

I have been trying to override _prepare_invoice function

so this is my sale.order.line (inherited) .py

class customSaleField(models.Model):
 _inherit = 'sale.order.line'

 TotalWeight = fields.Float(string='Total Weight',store=True)
 PricePerUnit = fields.Float(string='Price Per Unit',store=True)
 @api.onchange('product_uom_qty','product_id')
 def on_change_weight(self):
  if self.product_uom_qty:
   self.TotalWeight = self.product_uom_qty * self.product_id.weight
  
 @api.onchange('price_subtotal','TotalWeight','product_id','price_unit')
 def on_change_PricePerUnit(self):
  if self.price_subtotal:
   try:
    self.PricePerUnit = self.price_subtotal / self.TotalWeight
   except ZeroDivisionError:
    self.PricePerUnit = 0
 def _prepare_invoice(self):
  res = super(customSaleField,self)._prepare_invoice(self)
  if self.product_id:
   if self.TotalWeight:
    res['TotalWeight'] = self.TotalWeight
   if self.PricePerUnit
    res['PricePerUnit'] = self.PricePerUnit
  return res

I have followed this post and change the code structure to odoo10 it doesnt work 

it doesnt pass value up to account.invoice the value in the field are 0

https://www.odoo.com/zh_CN/forum/help-1/question/how-to-transfer-data-from-sale-order-to-invoice-when-clicking-on-create-invoice-85740

this is my account.invoice.line (inherited) .py

class customAccountField(models.Model):
 _inherit = 'account.invoice.line'

 TotalWeight = fields.Float(string='Total Weight',store=True)
 PricePerUnit = fields.Float(string='Price Per Unit',store=True)
 @api.onchange('quantity','product_id','invoice_line_ids')
 def on_change_weight(self):
  if self.quantity:
   self.TotalWeight = self.quantity * self.product_id.weight
 @api.onchange('price_subtotal','TotalWeight','product_id','price_unit','invoice_line_ids')
 def on_change_PricePerUnit(self):
  if self.price_subtotal:
   try:
    self.PricePerUnit = self.price_subtotal / self.TotalWeight
   except ZeroDivisionError:
    self.PricePerUnit = 0

in sale.order xml file

<odoo>
  <data>
<record model="ir.ui.view" id="sale_order_view_inherit"> 
    <field name="name">sale.order.view.inherit</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml"> 
        <xpath expr="/form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='product_uom_qty']" position="after">
            <field name="TotalWeight"/>
   <field name="PricePerUnit"/>
        </xpath> 
    </field> 
 </record>
</data>
</odoo>

in account.invoice xml file

<odoo>
  <data>
<record model="ir.ui.view" id="account_view_inherit"> 
    <field name="name">account.view.inherit</field>
    <field name="model">account.invoice</field>
    <field name="inherit_id" ref="account.invoice_form"/>
    <field name="arch" type="xml"> 
        <xpath expr="/form/sheet/notebook/page/field[@name='invoice_line_ids']/tree/field[@name='quantity']" position="after">
            <field name="TotalWeight"/>
   <field name="PricePerUnit"/>
        </xpath> 
    </field> 
 </record>
</data>
</odoo>


Avatar
Discard

Try overriding _create_invoice method in sale.advance.payment.inv model.

That method is creating invoice when you click create invoice button

Author

can you give some example on it if possible please thank you

Best Answer

You want to pass the fields value of sale.order.line to account.invoice.line object. For that you need to override `_prepare_invoice_line` method of sale.order.line object.

Like this:

class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    @api.multi
    def _prepare_invoice_line(self, qty):
        res = super(SaleOrderLine, self)._prepare_invoice_line(qty)
        res.update({'PricePerUnit': self.PricePerUnit, 'TotalWeight': self.TotalWeight})
        return res

Hope this will help you.

Sudhir Arya
ERP Harbor Consulting Services
skype: 
sudhir@erpharbor.com  website: http://www.erpharbor.com
Avatar
Discard
Related Posts Replies Views Activity
3
Jun 17
6285
2
Sep 18
5887
1
Apr 18
3358
0
Jan 18
4024
0
Sep 17
3199