Skip to Content
Menu
This question has been flagged

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, _
import odoo.addons.decimal_precision as dp
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

Avatar
Discard

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

Author

@Nitin Question updated with a code.

Author Best Answer

I had solved the issue.

The proper code is.

from odoo.exceptions import ValidationError


class 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)
Avatar
Discard
Related Posts Replies Views Activity
0
Oct 18
1802
0
Sep 24
126
3
Aug 24
529
0
Feb 24
265
0
Feb 24
159