Hi,
I thought id challenge myself by creating an app to pull the previous sales quantities into the product template list view. After many, many hours, unfortunately it seems I cannot work out how to do it.
I have successfully put a float field on the product.template.
Here is my model:
from odoo import api, models, fields
class ProductsSold(models.Model):
_inherit = "product.template" # database table name.
sold = fields.Float(compute='_compute_avail', string="Avail")
def _compute_avail(self):
#sold = self.env['stock.quant'].search_count([])
# records_sum = sum(self.env["stock.quant"].search([]).mapped('inventory_quantity'))
records_sum = sum(self.env["sale.sale_order_line"].search([]).mapped('product_uom_qty'))
self.sold = records_sum
If anyone can offer any hints, I'd be grateful.
Thank you for your response. Awesome!! It works perfectly.
Next challenge: I would then like to set some filters on this field to sum up the sales in the past month - which I think I can probably achieve now.
Although, I am curious about your mention of uom. Is there something here I am overlooking?
If you ANSWER a question, the OP is notified, but nobody else. I can't convert your answer into a comment that @Fazle can see, so if you'd like to ping him, please comment on HIS answer.
Hi wombat, may I know which approach you followed?
Hi Fazle, I when with the first version. I think the word "available" caused some confusion as I was looking for the quantity sold.
Thank you Ray. I am new to forums and their etiquette. I imagine from your response I must have hit answer instead of comment. Not sure what an OP is - or this karma thing that keeps popping up. I'll see what google can tell me.
wombat, I still advise you to go forward with the second approach