event_event.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. class Event(models.Model):
  5. _inherit = 'event.event'
  6. sale_order_lines_ids = fields.One2many(
  7. 'sale.order.line', 'event_id',
  8. groups='sales_team.group_sale_salesman',
  9. string='All sale order lines pointing to this event')
  10. sale_price_subtotal = fields.Monetary(
  11. string='Sales (Tax Excluded)', compute='_compute_sale_price_subtotal',
  12. groups='sales_team.group_sale_salesman')
  13. currency_id = fields.Many2one(
  14. 'res.currency', string='Currency',
  15. related='company_id.currency_id', readonly=True)
  16. @api.depends('company_id.currency_id',
  17. 'sale_order_lines_ids.price_subtotal', 'sale_order_lines_ids.currency_id',
  18. 'sale_order_lines_ids.company_id', 'sale_order_lines_ids.order_id.date_order')
  19. def _compute_sale_price_subtotal(self):
  20. """ Takes all the sale.order.lines related to this event and converts amounts
  21. from the currency of the sale order to the currency of the event company.
  22. To avoid extra overhead, we use conversion rates as of 'today'.
  23. Meaning we have a number that can change over time, but using the conversion rates
  24. at the time of the related sale.order would mean thousands of extra requests as we would
  25. have to do one conversion per sale.order (and a sale.order is created every time
  26. we sell a single event ticket). """
  27. date_now = fields.Datetime.now()
  28. sale_price_by_event = {}
  29. if self.ids:
  30. event_subtotals = self.env['sale.order.line']._read_group(
  31. [('event_id', 'in', self.ids),
  32. ('price_subtotal', '!=', 0)],
  33. ['event_id', 'currency_id', 'price_subtotal:sum'],
  34. ['event_id', 'currency_id'],
  35. lazy=False
  36. )
  37. company_by_event = {
  38. event._origin.id or event.id: event.company_id
  39. for event in self
  40. }
  41. currency_by_event = {
  42. event._origin.id or event.id: event.currency_id
  43. for event in self
  44. }
  45. currency_by_id = {
  46. currency.id: currency
  47. for currency in self.env['res.currency'].browse(
  48. [event_subtotal['currency_id'][0] for event_subtotal in event_subtotals]
  49. )
  50. }
  51. for event_subtotal in event_subtotals:
  52. price_subtotal = event_subtotal['price_subtotal']
  53. event_id = event_subtotal['event_id'][0]
  54. currency_id = event_subtotal['currency_id'][0]
  55. sale_price = currency_by_event[event_id]._convert(
  56. price_subtotal,
  57. currency_by_id[currency_id],
  58. company_by_event[event_id] or self.env.company,
  59. date_now)
  60. if event_id in sale_price_by_event:
  61. sale_price_by_event[event_id] += sale_price
  62. else:
  63. sale_price_by_event[event_id] = sale_price
  64. for event in self:
  65. event.sale_price_subtotal = sale_price_by_event.get(event._origin.id or event.id, 0)
  66. def action_view_linked_orders(self):
  67. """ Redirects to the orders linked to the current events """
  68. sale_order_action = self.env["ir.actions.actions"]._for_xml_id("sale.action_orders")
  69. sale_order_action.update({
  70. 'domain': [('state', '!=', 'cancel'), ('order_line.event_id', 'in', self.ids)],
  71. 'context': {'create': 0},
  72. })
  73. return sale_order_action