[MOV] event_sale: split sale_order.py

Part-of: odoo/odoo#110833
This commit is contained in:
Victor Feyens
2023-02-03 05:09:05 +01:00
parent b00e91285f
commit 68470b80a8
3 changed files with 129 additions and 125 deletions
+2 -1
View File
@@ -4,6 +4,7 @@ from . import account_move
from . import event_event
from . import event_registration
from . import event_ticket
from . import sale_order
from . import product
from . import sale_order
from . import sale_order_line
from . import sale_order_template_line
+1 -124
View File
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models, _
@@ -57,126 +57,3 @@ class SaleOrder(models.Model):
def unlink(self):
self.order_line._unlink_associated_registrations()
return super(SaleOrder, self).unlink()
class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'
event_id = fields.Many2one(
'event.event', string='Event',
compute="_compute_event_id", store=True, readonly=False, precompute=True,
help="Choose an event and it will automatically create a registration for this event.")
event_ticket_id = fields.Many2one(
'event.event.ticket', string='Event Ticket',
compute="_compute_event_ticket_id", store=True, readonly=False, precompute=True,
help="Choose an event ticket and it will automatically create a registration for this event ticket.")
# TODO in master: remove this field, unused anymore
event_ok = fields.Boolean(compute='_compute_event_ok')
@api.depends('product_id.detailed_type')
def _compute_event_ok(self):
for record in self:
record.event_ok = record.product_id.detailed_type == 'event'
@api.depends('state', 'event_id')
def _compute_product_uom_readonly(self):
event_lines = self.filtered(lambda line: line.event_id)
event_lines.update({'product_uom_readonly': True})
super(SaleOrderLine, self - event_lines)._compute_product_uom_readonly()
def _update_registrations(self, confirm=True, cancel_to_draft=False, registration_data=None, mark_as_paid=False):
""" Create or update registrations linked to a sales order line. A sale
order line has a product_uom_qty attribute that will be the number of
registrations linked to this line. This method update existing registrations
and create new one for missing one. """
RegistrationSudo = self.env['event.registration'].sudo()
registrations = RegistrationSudo.search([('sale_order_line_id', 'in', self.ids)])
registrations_vals = []
for so_line in self:
if not so_line.product_type == 'event':
continue
existing_registrations = registrations.filtered(lambda self: self.sale_order_line_id.id == so_line.id)
if confirm:
existing_registrations.filtered(lambda self: self.state not in ['open', 'cancel']).action_confirm()
if mark_as_paid:
existing_registrations.filtered(lambda self: not self.is_paid)._action_set_paid()
if cancel_to_draft:
existing_registrations.filtered(lambda self: self.state == 'cancel').action_set_draft()
for count in range(int(so_line.product_uom_qty) - len(existing_registrations)):
values = {
'sale_order_line_id': so_line.id,
'sale_order_id': so_line.order_id.id
}
# TDE CHECK: auto confirmation
if registration_data:
values.update(registration_data.pop())
registrations_vals.append(values)
if registrations_vals:
RegistrationSudo.create(registrations_vals)
return True
@api.depends('product_id')
def _compute_event_id(self):
event_lines = self.filtered(lambda line: line.product_id and line.product_id.detailed_type == 'event')
(self - event_lines).event_id = False
for line in event_lines:
if line.product_id not in line.event_id.event_ticket_ids.product_id:
line.event_id = False
@api.depends('event_id')
def _compute_event_ticket_id(self):
event_lines = self.filtered('event_id')
(self - event_lines).event_ticket_id = False
for line in event_lines:
if line.event_id != line.event_ticket_id.event_id:
line.event_ticket_id = False
@api.depends('event_ticket_id')
def _compute_price_unit(self):
super()._compute_price_unit()
@api.depends('event_ticket_id')
def _compute_name(self):
"""Override to add the compute dependency.
The custom name logic can be found below in _get_sale_order_line_multiline_description_sale.
"""
super()._compute_name()
def unlink(self):
self._unlink_associated_registrations()
return super(SaleOrderLine, self).unlink()
def _cancel_associated_registrations(self):
self.env['event.registration'].search([('sale_order_line_id', 'in', self.ids)]).action_cancel()
def _unlink_associated_registrations(self):
self.env['event.registration'].search([('sale_order_line_id', 'in', self.ids)]).unlink()
def _get_sale_order_line_multiline_description_sale(self):
""" We override this method because we decided that:
The default description of a sales order line containing a ticket must be different than the default description when no ticket is present.
So in that case we use the description computed from the ticket, instead of the description computed from the product.
We need this override to be defined here in sales order line (and not in product) because here is the only place where the event_ticket_id is referenced.
"""
if self.event_ticket_id:
return self.event_ticket_id._get_ticket_multiline_description() + self._get_sale_order_line_multiline_description_variants()
else:
return super()._get_sale_order_line_multiline_description_sale()
def _get_display_price(self):
if self.event_ticket_id and self.event_id:
event_ticket = self.event_ticket_id.with_context(
pricelist=self.order_id.pricelist_id.id,
uom=self.product_uom.id
)
company = event_ticket.company_id or self.env.company
pricelist = self.order_id.pricelist_id
if pricelist.discount_policy == "with_discount":
price = event_ticket.price_reduce
else:
price = event_ticket.price
return self._convert_to_sol_currency(price, company.currency_id)
return super()._get_display_price()
+126
View File
@@ -0,0 +1,126 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models, _
class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'
event_id = fields.Many2one(
'event.event', string='Event',
compute="_compute_event_id", store=True, readonly=False, precompute=True,
help="Choose an event and it will automatically create a registration for this event.")
event_ticket_id = fields.Many2one(
'event.event.ticket', string='Event Ticket',
compute="_compute_event_ticket_id", store=True, readonly=False, precompute=True,
help="Choose an event ticket and it will automatically create a registration for this event ticket.")
# TODO in master: remove this field, unused anymore
event_ok = fields.Boolean(compute='_compute_event_ok')
@api.depends('product_id.detailed_type')
def _compute_event_ok(self):
for record in self:
record.event_ok = record.product_id.detailed_type == 'event'
@api.depends('state', 'event_id')
def _compute_product_uom_readonly(self):
event_lines = self.filtered(lambda line: line.event_id)
event_lines.update({'product_uom_readonly': True})
super(SaleOrderLine, self - event_lines)._compute_product_uom_readonly()
def _update_registrations(self, confirm=True, cancel_to_draft=False, registration_data=None, mark_as_paid=False):
""" Create or update registrations linked to a sales order line. A sale
order line has a product_uom_qty attribute that will be the number of
registrations linked to this line. This method update existing registrations
and create new one for missing one. """
RegistrationSudo = self.env['event.registration'].sudo()
registrations = RegistrationSudo.search([('sale_order_line_id', 'in', self.ids)])
registrations_vals = []
for so_line in self:
if not so_line.product_type == 'event':
continue
existing_registrations = registrations.filtered(lambda self: self.sale_order_line_id.id == so_line.id)
if confirm:
existing_registrations.filtered(lambda self: self.state not in ['open', 'cancel']).action_confirm()
if mark_as_paid:
existing_registrations.filtered(lambda self: not self.is_paid)._action_set_paid()
if cancel_to_draft:
existing_registrations.filtered(lambda self: self.state == 'cancel').action_set_draft()
for count in range(int(so_line.product_uom_qty) - len(existing_registrations)):
values = {
'sale_order_line_id': so_line.id,
'sale_order_id': so_line.order_id.id
}
# TDE CHECK: auto confirmation
if registration_data:
values.update(registration_data.pop())
registrations_vals.append(values)
if registrations_vals:
RegistrationSudo.create(registrations_vals)
return True
@api.depends('product_id')
def _compute_event_id(self):
event_lines = self.filtered(lambda line: line.product_id and line.product_id.detailed_type == 'event')
(self - event_lines).event_id = False
for line in event_lines:
if line.product_id not in line.event_id.event_ticket_ids.product_id:
line.event_id = False
@api.depends('event_id')
def _compute_event_ticket_id(self):
event_lines = self.filtered('event_id')
(self - event_lines).event_ticket_id = False
for line in event_lines:
if line.event_id != line.event_ticket_id.event_id:
line.event_ticket_id = False
@api.depends('event_ticket_id')
def _compute_price_unit(self):
super()._compute_price_unit()
@api.depends('event_ticket_id')
def _compute_name(self):
"""Override to add the compute dependency.
The custom name logic can be found below in _get_sale_order_line_multiline_description_sale.
"""
super()._compute_name()
def unlink(self):
self._unlink_associated_registrations()
return super(SaleOrderLine, self).unlink()
def _cancel_associated_registrations(self):
self.env['event.registration'].search([('sale_order_line_id', 'in', self.ids)]).action_cancel()
def _unlink_associated_registrations(self):
self.env['event.registration'].search([('sale_order_line_id', 'in', self.ids)]).unlink()
def _get_sale_order_line_multiline_description_sale(self):
""" We override this method because we decided that:
The default description of a sales order line containing a ticket must be different than the default description when no ticket is present.
So in that case we use the description computed from the ticket, instead of the description computed from the product.
We need this override to be defined here in sales order line (and not in product) because here is the only place where the event_ticket_id is referenced.
"""
if self.event_ticket_id:
return self.event_ticket_id._get_ticket_multiline_description() + self._get_sale_order_line_multiline_description_variants()
else:
return super()._get_sale_order_line_multiline_description_sale()
def _get_display_price(self):
if self.event_ticket_id and self.event_id:
event_ticket = self.event_ticket_id.with_context(
pricelist=self.order_id.pricelist_id.id,
uom=self.product_uom.id
)
company = event_ticket.company_id or self.env.company
pricelist = self.order_id.pricelist_id
if pricelist.discount_policy == "with_discount":
price = event_ticket.price_reduce
else:
price = event_ticket.price
return self._convert_to_sol_currency(price, company.currency_id)
return super()._get_display_price()