# Compute Product Quantity in sale.order.line

By
Faheem Khan
on 10/11/18, 6:39 AM 101 views

I need to calculate product qty as below.

Product A unit of measure is "Cotton 48" (Cotton 48 = 48 Units)

I want to sell 32'Cotton 48' and 5 Units.

I want to create 2 new fields in sale.order.line (Cotton Qty, Unit Qty).

The user will put 2 in cotton qty and 5 in Unit Qty.

Then product_uom_qty should calculate in unit qty ((cotton qty: 48 x 2 = 96 + Unit Qty: 5) = 101)

Any help on this issue. I want to use same method in pos.order.line too.

Edit: Here is my model.py file for sale.order.line

from odoo import api, fields, models, _
from odoo.exceptions import ValidationError

class SaleOrder(models.Model):
_inherit = "sale.order"

@api.depends('order_line.product_uom_qty')
def _qty_all(self):
vals = {}
for line in self.mapped('order_line').filtered(
lambda l: l.product_uom_qty):
vals[line] = {
'product_uom_qty': line.product_uom_qty,
}
product_uom_qty = line.cotton_qty * line.product_uom.factor_inv + line.pcs_qty
line.update({
'product_uom_qty': product_uom_qty,
})
res = super(SaleOrder, self)._qty_all()
for line in vals.keys():
line.update(vals[line])
return res

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

cotton_qty = fields.Float(
string="Cotton Qty",
digits=dp.get_precision('Qty'),
help="Cotton Quantity.")
pcs_qty = fields.Float(
string="Unit Qty",
digits=dp.get_precision('Qty'),
help="Unit Quantity.")

@api.depends('product_uom_qty')
def _final_qty(self):
vals = {}
for line in self.filtered(lambda l: l.product_uom_qty):
vals[line] = {
'cotton_qty': line.cotton_qty,
'pcs_qty': line.pcs_qty,
}
product_uom_qty = ccotton_qty * product_uom.factor_inv + line.pcs_qty
line.update({
'product_uom_qty': product_uom_qty,
})
res = super(SaleOrderLine, self)._final_qty()
for line in vals.keys():
line.update(vals[line])
return res

Please add piece of code you developer of any compute method or something . It will be more easy to understand your question

Nitin Kantak
on 10/11/18, 8:46 AM

@Nitin Question updated with a code.

Faheem Khan
on 10/11/18, 9:01 AM
Faheem Khan
On 10/12/18, 6:45 AM

The proper code is.

`from odoo.exceptions import ValidationErrorclass ProductTemplateInherit(models.Model):    _inherit = 'product.template'    x_discount = fields.Float(digits=dp.get_precision('Discount'), store=True, readonly=False)class SaleOrderLineDiscount(models.Model):    _inherit = 'sale.order.line'    x_cotton_qty = fields.Float(string='Cotton Qty', digits=dp.get_precision('Qty'), store=True, readonly=False)    x_pcs_qty = fields.Float(string='PCS Qty', digits=dp.get_precision('Qty'), store=True, readonly=False)    @api.depends('x_cotton_qty', 'x_pcs_qty', 'product_id')    def _comput_from_cotton(self):        for line in self:            line.product_uom_qty = line.x_cotton_qty * line.product_id.uom_po_id.factor_inv + line.x_pcs_qty    product_uom_qty = fields.Float(compute='_comput_from_cotton', string='Quantity',                                   digits=dp.get_precision('Product Unit of Measure'), required=True, default=1.0)`

This platform 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.

### Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

1 follower(s)

### Stats

 Asked: 10/11/18, 6:39 AM Seen: 101 times Last updated: 10/12/18, 6:45 AM