Skip to Content
Menu
This question has been flagged

Hi, Everyone

     i have been able to create a custom in sale order line there are two field 1.totalweight and priceperunit i use a help of @api.onchange so when ever the value that it depends on it will change automatically. below is my .py code

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

 TotalWeight = fields.Float(string='Total Weight',readonly=True,store=True)
 PricePerUnit = fields.Float(string='Price Per Unit',readonly=True,store=True)

@api.onchange('product_uom_qty')
 def on_change_weight(self):
  weight = self.product_uom_qty * self.product_id.weight
  value = {
   'value' : {
    'TotalWeight' : weight
   }
  }
  return value
  
 @api.onchange('price_subtotal','TotalWeight')
 def on_change_PricePerUnit(self):

  try:
   PricePerUnit = self.price_subtotal / self.TotalWeight
  except ZeroDivisionError:
   PricePerUnit = 0
   
  value = {
   'value' : {
    'PricePerUnit' : PricePerUnit
   }
  }
  return value

however,when i press "confirm sale" or "save" or perform any action the value in custom fields are disappear

Secondly, how do i actually pass these values,field to account ?? after the user press click on "create invoice"


Avatar
Discard
Best Answer

Hello Vootipong Limpanitivat,


Try to remove readonly attribute from field.

May be because of readonly attribute, method is not affected. I faced many time in the past.


@api.onchange('product_uom_qty')
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')
def on_change_PricePerUnit(self):
    if self.price_subtotal:
        self.PricePerUnit = self.price_subtotal / self.TotalWeight


For Passing values to account, refer this method under sale module which is def create_invoices


Hope it will helps you.

Thanks,

Avatar
Discard
Author

Sir, it works !! it really works !

So i have to inherited and override those function ?

Yes. For passing values you need to override those function called as def create_invoices. If any sale order line id passes in the invoice line then you will pass it easily. Check that method and override it.

Author

hi , i have found the answer relate to this here is the link

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

the answer were posted by Peter Alabaster

so i manage to change to odoo 10

the out come are like this

def _prepare_invoice(self):

res = super(customSaleField,self)._prepare_invoice(self)

if line.product_id:

if line.TotalWeight:

res['TotalWeight'] = line.TotalWeight

if line.PricePerUnit

res['PricePerUnit'] = line.PricePerUnit

return res

??

Author

i dont know am on the right way or not please she some light please

Best Answer

Hi,

I think store is used to store computed field to db.

If you need to save it from an onchange function try writing it from onchange

example

self.weight = self.product_uom_qty * self.product_id.weight


Avatar
Discard
Author

still not working

@api.onchange('product_uom_qty')

def on_change_weight(self):

self.TotalWeight = self.product_uom_qty * self.product_id.weight

weight= self.product_uom_qty * self.product_id.weight

value = {

'value' : {

'TotalWeight' : weight

}

}

return value

@api.onchange('price_subtotal','TotalWeight')

def on_change_PricePerUnit(self):

try:

self.PricePerUnit = self.price_subtotal / self.TotalWeight

PPU = self.price_subtotal / self.TotalWeight

except ZeroDivisionError:

self.PricePerUnit = 0

PPU = 0

value = {

'value' : {

'PricePerUnit' : PPU

}

}

return value

Related Posts Replies Views Activity
3
Sep 24
11384
2
Sep 18
4787
1
Apr 18
2473
0
Jan 18
3089
0
Sep 17
2434