diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/sale/models/product_product.py | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/sale/models/product_product.py')
| -rw-r--r-- | addons/sale/models/product_product.py | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/addons/sale/models/product_product.py b/addons/sale/models/product_product.py new file mode 100644 index 00000000..3e20daa9 --- /dev/null +++ b/addons/sale/models/product_product.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from datetime import timedelta, time +from odoo import api, fields, models +from odoo.tools.float_utils import float_round + + +class ProductProduct(models.Model): + _inherit = 'product.product' + + 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 + + def action_view_sales(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_365_days'}, + } + return action + + def _get_invoice_policy(self): + return self.invoice_policy + + def _get_combination_info_variant(self, add_qty=1, pricelist=False, parent_combination=False): + """Return the variant info based on its combination. + See `_get_combination_info` for more information. + """ + self.ensure_one() + return self.product_tmpl_id._get_combination_info(self.product_template_attribute_value_ids, self.id, add_qty, pricelist, parent_combination) + + def _filter_to_unlink(self): + domain = [('product_id', 'in', self.ids)] + lines = self.env['sale.order.line'].read_group(domain, ['product_id'], ['product_id']) + linked_product_ids = [group['product_id'][0] for group in lines] + return super(ProductProduct, self - self.browse(linked_product_ids))._filter_to_unlink() + + +class ProductAttributeCustomValue(models.Model): + _inherit = "product.attribute.custom.value" + + sale_order_line_id = fields.Many2one('sale.order.line', string="Sales Order Line", required=True, ondelete='cascade') + + _sql_constraints = [ + ('sol_custom_value_unique', 'unique(custom_product_template_attribute_value_id, sale_order_line_id)', "Only one Custom Value is allowed per Attribute Value per Sales Order Line.") + ] |
