跳至内容
菜单
此问题已终结
2 回复
4576 查看
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.

形象
丢弃

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

最佳答案

 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']]
形象
丢弃
编写者

Amazing breakdown. Thank you

相关帖文 回复 查看 活动
1
8月 25
889
1
7月 25
1128
3
7月 25
3905
3
5月 25
2028
1
7月 25
1310