Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odpovědi
4551 Zobrazení
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
Zrušit

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

Nejlepší odpověď

 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
Zrušit
Autor

Amazing breakdown. Thank you

Related Posts Odpovědi Zobrazení Aktivita
1
srp 25
771
1
čvc 25
1024
3
čvc 25
3477
3
kvě 25
1856
1
čvc 25
1210