Skip to Content
Menu
This question has been flagged
2 Replies
2105 Views

Hi

How can i have a filed like sales_count on product.product that showing sales on last 180 days and 90 days ago instead of 365 days ago ?

thank you in advance

Avatar
Discard
Best Answer

Hi Kamran,

You can start by creating a new smart button in the product, for that, I suggest you extend the xml of the product. 

Here is the standard xml that creates the standard button (365 days) you can use it as a base to create your own button:


model="ir.ui.view" id="product_form_view_sale_order_button">
name="name">product.product.sale.order
name="model">product.product
name="inherit_id" ref="product.product_normal_form_view"/>
name="groups_id" eval="[(4, ref('sales_team.group_sale_salesman'))]"/>
name="arch" type="xml">
name="button_box" position="inside">


name="description" position="after">
string="Warning when Selling this Product" groups="sale.group_warning_sale">
name="sale_line_warn" nolabel="1"/>
name="sale_line_warn_msg" colspan="3" nolabel="1"
attrs="{'required':[('sale_line_warn','!=','no-message')],'readonly':[('sale_line_warn','=','no-message')]}"/>





** of course you must create also your own custom field  sales_count and calculate it using a compute function

Then you just need to create a new action in the product by extending the py of the product.

Here is the standard action to open the sales of the last 365 days:


def action_view_sales(self):
action = self.env.ref('sale.report_all_channels_sales_action').read()[0]
action['domain'] = [('product_id', 'in', self.ids)]
action['context'] = {
'pivot_measures': ['product_uom_qty'],
'active_id'
: self._context.get('active_id'),
'search_default_Sales'
: 1,
'active_model'
: 'sale.report',
'time_ranges'
: {'field': 'date', 'range': 'last_365_days'},
}
return action


Here is the tricky part: if you don't calculate the domain in python you must create a new filter in the search portion of the xml, so then you can call this new filter in the action by passing its name by context.

Hope it helps you to get started.

Avatar
Discard
Author

Hi Alexander
Thank you for your reply unfortunately I’m not that much technical i was wondering if you make it a little more clear

Hi Kamran, I've included more details in the answer, hope that helps in giving you a better direction.

Author

Alexander
Thank you 🙏
Can i have the code for compute custom filed ? And one more thing can i use server action instead of extend py code ? If yes please kindly describe it a little more
Thank you in advance

Hi Kamran,
Here is the code for the field:
sales_count = fields.Float(compute='_compute_sales_count', string='Sold')
def _compute_sales_count(self):
r = {}
self.sales_count = 0
if not self.user_has_groups('sales_team.group_sale_salesman'):
return r
date_from = fields.Datetime.to_string(fields.datetime.combine(fields.datetime.now() - timedelta(days=365),
time.min))

done_states = self.env['sale.report']._get_done_states()

domain = [
('state', 'in', done_states),
('product_id', 'in', self.ids),
('date', '>=', date_from),
]
for group in self.env['sale.report'].read_group(domain, ['product_id', 'product_uom_qty'], ['product_id']):
r[group['product_id'][0]] = group['product_uom_qty']
for product in self:
if not product.id:
product.sales_count = 0.0
continue
product.sales_count = float_round(r.get(product.id, 0), precision_rounding=product.uom_id.rounding)
return r

If you are still having problems modifying this I suggest you get started with the tutorials on the Odoo website.

Best Answer

in product.template

sales_count_custom = fields.Float(compute='_compute_sales_count_custom',string='90 Days Sale')


@api.depends('product_variant_ids.sales_count_custom')    def_compute_sales_count_custom(self):        for product inself:            product.sales_count_custom =float_round(sum([p.sales_count_custom for p in product.with_context(active_test=False).product_variant_ids]),precision_rounding=product.uom_id.rounding)        defaction_view_sales_custom(self):        action =self.env["ir.actions.actions"]._for_xml_id("sale.report_all_channels_sales_action")        action['domain']=[('product_id','in',self.ids)]        action['context']={            'pivot_measures':['product_uom_qty'],            'active_id':self._context.get('active_id'),            'search_default_Sales':1,            'active_model':'sale.report',            'time_ranges':{'field':'date','range':'last_90_days'},        }        return action


in view 


  <xpathexpr="//div[@name='button_box']"position="inside">                    <buttonclass="oe_stat_button"name="action_view_sales_custom"                        type="object"icon="fa-bar-chart-o"groups="sales_team.group_sale_salesman"help="Sold in the last 90 days"attrs="{'invisible': [('sale_ok', '=', False)]}">                        <divclass="o_field_widget o_stat_info">                            <spanclass="o_stat_value">                                <fieldname="sales_count_custom"widget="statinfo"nolabel="1"class="mr4"/>                                <fieldname="uom_name"/>                            span>                            <spanclass="o_stat_text">90 Daysspan>                        div>                    button>                xpath>

Avatar
Discard

it will work for 90 days. cheers . after looking at this , you can make 180 days one aswell.

Related Posts Replies Views Activity
2
Jan 24
1396
0
Nov 24
62
1
Aug 24
260
1
Feb 24
776
1
Aug 24
993