Debian Codegraph @

addons/event_sale/models/event_event.py

raw
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from odoo import api, fields, models
5
6
7 class EventEvent(models.Model):
8 _inherit = 'event.event'
9
10 sale_order_lines_ids = fields.One2many(
11 'sale.order.line', 'event_id',
12 groups='sales_team.group_sale_salesman',
13 string='All sale order lines pointing to this event')
14 sale_price_total = fields.Monetary(
15 string='Sales (Tax Included)', compute='_compute_sale_price_total',
16 groups='sales_team.group_sale_salesman')
17
18 @api.depends('company_id.currency_id',
19 'sale_order_lines_ids.price_total', 'sale_order_lines_ids.currency_id',
20 'sale_order_lines_ids.company_id', 'sale_order_lines_ids.order_id.date_order')
21 def _compute_sale_price_total(self):
22 """ Takes only confirmed the sale.order.lines related to this event and converts amounts
23 from the currency of the sale order to the currency of the event company.
24
25 To avoid extra overhead, we use conversion rates as of 'today'.
26 Meaning we have a number that can change over time, but using the conversion rates
27 at the time of the related sale.order would mean thousands of extra requests as we would
28 have to do one conversion per sale.order (and a sale.order is created every time
29 we sell a single event ticket). """
30 date_now = fields.Datetime.now()
31 event_subtotals = self.env['sale.order.line']._read_group(
32 [('event_id', 'in', self.ids), ('price_total', '!=', 0), ('state', '=', 'sale')],
33 ['event_id', 'currency_id'],
34 ['price_total:sum'],
35 )
36 event_subtotals_mapping = dict.fromkeys(self._origin, 0)
37 for event, currency, sum_price_total in event_subtotals:
38 event_subtotals_mapping[event] += event.currency_id._convert(
39 sum_price_total,
40 currency,
41 event.company_id or self.env.company,
42 date_now,
43 )
44
45 for event in self:
46 event.sale_price_total = event_subtotals_mapping.get(event._origin, 0)
47
48 def action_view_linked_orders(self):
49 """ Redirects to only the confirmed orders linked to the current events """
50 sale_order_action = self.env["ir.actions.actions"]._for_xml_id("sale.action_orders")
51 sale_order_action.update({
52 'domain': [('state', '=', 'sale'), ('order_line.event_id', 'in', self.ids)],
53 'context': {'create': 0},
54 })
55 return sale_order_action