Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
4550 Widoki
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.

Awatar
Odrzuć

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

Najlepsza odpowiedź

 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']]
Awatar
Odrzuć
Autor

Amazing breakdown. Thank you

Powiązane posty Odpowiedzi Widoki Czynność
1
sie 25
770
1
lip 25
1023
3
lip 25
3471
3
maj 25
1851
1
lip 25
1210