Skip to Content
Menu
This question has been flagged
2 Replies
4197 Views
class Product(models.Model):
    _inherit = 'product.product'

    pos_product_order_total = fields.Char(
        string='Product POS Orders', compute='_product_pos_orders')

    def _product_pos_orders(self):
        Order = self.env['pos.order']
        for product in self:
            domain = [('product_id', '=', product.id)]
            for o in Order.search(domain):
                pass 

my goal is to add a field in product form view that will show how many time this product was bought with POS. really stuck with this one and would be nice to have an idea how to do this.

Avatar
Discard

Hello Grf, 
Here is a quick sudo code of how you can achieve this thing. Hope you are using 9 or + version.
This might have syntax bug as i haven't tested it. Thanks.

class Product(models.Model):
    _inherit = 'product.product'
    pos_product_order_total = fields.Char(
        string='Product POS Orders', compute='_product_pos_orders')
    def _product_pos_orders(self):
        Order_line = self.env['pos.order.line']
        total_count = 0
        for product in self:
            domain = [('id', '!=', false)]
            for order_line in Order_line.search(domain):
             if order_line.product_id === product.id:
              total_count = total_count + 1
        return total_count

Best Answer

 To know how many time you sold a product through the POS, you could do something like this

 

PosOrderLineObj = self.env['pos.order.line'] 
retrieve the class you need to use
domain = [('product_id', '=', your_product.id)]
set your domain
lines_with_product = PosOrderLineObj.search(domain)

search the pos.order.line that fits the given domain (ie the lines that contains your product in this example)
list_quantities = lines_with_product.mapped('product_uom_qty')
mapped('product_uom_qty') returns a list that contains all values of the selected field. Here it will return the list of the sold quantities
total_quantity = sum(list_quantities)
you can then simply sum the element in the list


That's obviously a long way to do it and those are long variables names but i tried to break down all 'steps' to show you how it's achieved. This can be done in a one liner:


class Product(models.Model):
    _inherit = 'product.product'

    pos_product_order_total = fields.Float(
        string='Product POS Orders', compute='_product_pos_orders')

    def _product_pos_orders(self):
        Order = self.env['pos.order']
        for product in self:
            product.pos_product_order_total = \
                sum(self.env['pos.order.line'].search(
                    [('product_id', '=', product.id)]).mapped('product_uom_qty'))

 

note that your pos_product_order_total  field should be of type float

You should probably enhance the domain so it only takes the validated pos orders:

[('product_id', '=', product.id), ('order_id.state', 'not in', ['draft', 'cancel']]
Avatar
Discard
Author

Amazing breakdown. Thank you

Related Posts Replies Views Activity
0
Dec 24
43
1
Dec 24
162
0
Nov 24
81
1
Nov 24
151
1
Nov 24
187