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']]
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.