Files
enterprise-addons/website_appointment_sale/models/sale_order.py
T

107 lines
4.7 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import Command, _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.tools import str2bool
class SaleOrder(models.Model):
_inherit = "sale.order"
calendar_event_count = fields.Integer('Meetings', compute='_compute_meeting_count')
@api.depends('order_line')
def _compute_meeting_count(self):
event_count_per_so = {
sale_order.id: event_count
for sale_order, event_count in self.env['sale.order.line']._read_group(
[('order_id', 'in', self.ids), ('calendar_event_id', '!=', False)],
['order_id'],
['__count']
)
}
for sale_order in self:
sale_order.calendar_event_count = event_count_per_so.get(sale_order.id, 0)
def action_view_calendar_events(self):
action = self.env['ir.actions.act_window']._for_xml_id('calendar.action_calendar_event')
action['domain'] = [('id', 'in', self.order_line.calendar_event_id.ids)]
action['views'] = [(False, 'list'), (False, 'form')]
action['context'] = {'active_test': False}
return action
def _action_cancel(self):
self.order_line.calendar_event_id.action_archive()
return super()._action_cancel()
def _action_confirm(self):
""" Override: as this method is called when successfully paying the SO, or manually in back-end,
we consider that the linked bookings can be transformed to a calendar event """
self.order_line.calendar_event_id.action_unarchive()
self.order_line.calendar_booking_ids._make_event_from_paid_booking()
return super()._action_confirm()
def _check_cart_is_ready_to_be_paid(self):
self.ensure_one()
bookings = self.order_line.calendar_booking_ids
unavailable_bookings = bookings._filter_unavailable_bookings()
if unavailable_bookings:
raise ValidationError(
_(
"The following bookings are not available anymore during the selected period"
" and your cart must be updated. We are sorry for the inconvenience.\n\n%(bookings)s",
bookings=[booking._get_description() for booking in unavailable_bookings],
),
)
return super()._check_cart_is_ready_to_be_paid()
def _cart_find_product_line(self, *args, calendar_booking_id=False, **kwargs):
""" Avoid returning the lines in case of an appointment if the same product exists in
the cart, as one could take many different slots from the same appointment, or even from
different appointments with the same booking fees product. One line per booking, with a
unique description, is meant to be in this case. """
if calendar_booking_id:
return self.env['sale.order.line']
return super()._cart_find_product_line(
*args, calendar_booking_id=calendar_booking_id, **kwargs,
)
def _prepare_order_line_values(self, *args, calendar_booking_id=False, calendar_booking_tz=False, **kwargs):
""" Add calendar booking values to the SOL creation values (if a booking id is provided). """
values = super()._prepare_order_line_values(
*args,
calendar_booking_id=calendar_booking_id,
calendar_booking_tz=calendar_booking_tz,
**kwargs,
)
if not calendar_booking_id:
return values
booking_sudo = self.env['calendar.booking'].sudo().browse(calendar_booking_id)
values['calendar_booking_ids'] = [Command.link(calendar_booking_id)]
# Using order's lang (via _get_lang()), same as usual behavior in _compute_name.
# It is linked to partner created / linked on appointment form submission.
values['name'] = booking_sudo.with_context(tz=calendar_booking_tz, lang=self._get_lang())._get_description()
return values
def unlink(self):
""" Manually unlink in order to unlink answer inputs linked to calendar bookings. """
bookings_to_unlink = self.order_line.calendar_booking_ids
bookings_to_unlink.unlink()
return super().unlink()
def _needs_customer_address(self):
"""Override of `website_sale` to avoid requesting full address details for appointments."""
res = super()._needs_customer_address()
if (
self.order_line
and all(line.calendar_booking_ids for line in self.order_line)
):
return str2bool(
self.env["ir.config_parameter"].sudo().get_param(
"website_appointment_sale.require_billing_details_for_appointments", "False"
)
)
return res