Skip to Content
Menu
This question has been flagged
1 Reply
1306 Views

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.




Avatar
Discard
Author

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?

Author

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

Best Answer

Hi Wombat, this should find the qty sold for a product. Also you have to add it to the view. Again I think you should also consider uom. Another thing is there is already a field in the product template form to show the sale qty. The field is called 'sales_count'. So this funtion or a new field should be redundent. 

from odoo import api, models, fields

class ProductsSold(models.Model):
_inherit = "product.product" # database table name.

def _compute_avail(self):
records_sum = sum(self.env["sale.order.line"].search([('product_id', '=', self.id), ('state', '=', 'sale')]).mapped('product_uom_qty'))
self.sold = records_sum sold = fields.Float(compute='_compute_avail', string="Avail")

instead this below should do want you want to achive. and no need to create any field or method. in words you need to inherit the tree view and just add the sales_count field. **Code section in comment does not let me include closing tags**


product_template_inherit_treefield>
product.templatefield>





xpath>

field>
record>


Avatar
Discard
Related Posts Replies Views Activity
3
Jun 23
1181
1
Sep 22
1114
0
Apr 22
2225
1
Jan 22
3098
2
Oct 16
2190