Skip to content

Commit

Permalink
[FIX] website_sale(_loyalty): merge main controller methods
Browse files Browse the repository at this point in the history
Part-of: #110686
  • Loading branch information
vava-odoo authored and Feyensv committed Mar 14, 2023
1 parent d3cddbe commit 323bb4f
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 89 deletions.
2 changes: 1 addition & 1 deletion addons/website_sale/controllers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import backend
from . import delivery_main
from . import delivery
from . import main
from . import variant
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,6 @@
class WebsiteSaleDelivery(WebsiteSale):
_express_checkout_shipping_route = '/shop/express/shipping_address_change'

@http.route()
def shop_payment(self, **post):
order = request.website.sale_get_order()
carrier_id = post.get('carrier_id')
keep_carrier = post.get('keep_carrier', False)
if keep_carrier:
keep_carrier = bool(int(keep_carrier))
if carrier_id:
carrier_id = int(carrier_id)
if order:
order._check_carrier_quotation(force_carrier_id=carrier_id, keep_carrier=keep_carrier)
if carrier_id:
return request.redirect("/shop/payment")

return super(WebsiteSaleDelivery, self).shop_payment(**post)

@http.route(['/shop/update_carrier'], type='json', auth='public', methods=['POST'], website=True)
def update_eshop_carrier(self, **post):
order = request.website.sale_get_order()
Expand Down Expand Up @@ -62,18 +46,6 @@ def cart_carrier_rate_shipment(self, carrier_id, **kw):
res['error_message'] = rate['error_message']
return res

@http.route()
def cart(self, **post):
order = request.website.sale_get_order()
if order and order.carrier_id:
# Express checkout is based on the amout of the sale order. If there is already a
# delivery line, Express Checkout form will display and compute the price of the
# delivery two times (One already computed in the total amount of the SO and one added
# in the form while selecting the delivery carrier)
order._remove_delivery_line()

return super().cart(**post)

@http.route(
_express_checkout_shipping_route, type='json', auth='public', methods=['POST'],
website=True, sitemap=False
Expand Down Expand Up @@ -103,9 +75,7 @@ def express_checkout_process_shipping_address(self, partial_shipping_address):
# Pricelist are recomputed every time the partner is changed. We don't want to recompute
# the price with another pricelist at this state since the customer has already accepted
# the amount and validated the payment.
order_sudo.env.remove_to_compute(
order_sudo._fields['pricelist_id'], order_sudo
)
order_sudo.env.remove_to_compute(order_sudo._fields['pricelist_id'], order_sudo)
elif order_sudo.partner_shipping_id.name.endswith(order_sudo.name):
self._create_or_edit_partner(
partial_shipping_address,
Expand All @@ -123,15 +93,12 @@ def express_checkout_process_shipping_address(self, partial_shipping_address):
child_partner_id = self._find_child_partner(
order_sudo.partner_id.commercial_partner_id.id, partial_shipping_address
)
if child_partner_id:
order_sudo.partner_shipping_id = child_partner_id
else:
order_sudo.partner_shipping_id = self._create_or_edit_partner(
order_sudo.partner_shipping_id = child_partner_id or self._create_or_edit_partner(
partial_shipping_address,
type='delivery',
parent_id=order_sudo.partner_id.id,
name=_('Anonymous express checkout partner for order %s', order_sudo.name),
)
)

# Returns the list of develivery carrier available for the sale order.
return sorted([{
Expand Down Expand Up @@ -220,46 +187,6 @@ def _get_rate(carrier, order, is_express_checkout_flow=False):
rate['price'] = taxes['total_included']
return rate

def order_lines_2_google_api(self, order_lines):
""" Transforms a list of order lines into a dict for google analytics """
order_lines_not_delivery = order_lines.filtered(lambda line: not line.is_delivery)
return super(WebsiteSaleDelivery, self).order_lines_2_google_api(order_lines_not_delivery)

def order_2_return_dict(self, order):
""" Returns the tracking_cart dict of the order for Google analytics """
ret = super(WebsiteSaleDelivery, self).order_2_return_dict(order)
delivery_line = order.order_line.filtered('is_delivery')
if delivery_line:
ret['shipping'] = delivery_line.price_unit
return ret

def _get_express_shop_payment_values(self, order, **kwargs):
values = super(WebsiteSaleDelivery, self)._get_express_shop_payment_values(order, **kwargs)
values['shipping_info_required'] = not order.only_services
values['shipping_address_update_route'] = self._express_checkout_shipping_route
return values

def _get_shop_payment_values(self, order, **kwargs):
values = super(WebsiteSaleDelivery, self)._get_shop_payment_values(order, **kwargs)
has_storable_products = any(line.product_id.type in ['consu', 'product'] for line in order.order_line)

if not order._get_delivery_methods() and has_storable_products:
values['errors'].append(
(_('Sorry, we are unable to ship your order'),
_('No shipping method is available for your current order and shipping address. '
'Please contact us for more information.')))

if has_storable_products:
if order.carrier_id and not order.delivery_rating_success:
order._remove_delivery_line()

delivery_carriers = order._get_delivery_methods()
values['deliveries'] = delivery_carriers.sudo()

values['delivery_has_storable'] = has_storable_products
values['delivery_action_id'] = request.env.ref('delivery.action_delivery_carrier_form').id
return values

def _update_website_sale_delivery_return(self, order, **post):
Monetary = request.env['ir.qweb.field.monetary']
carrier_id = int(post['carrier_id'])
Expand Down
60 changes: 54 additions & 6 deletions addons/website_sale/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def get_current_currency(self, **kwargs):

class WebsiteSale(http.Controller):
_express_checkout_route = '/shop/express_checkout'
_express_checkout_shipping_route = '/shop/express/shipping_address_change'

WRITABLE_PARTNER_FIELDS = [
'name',
Expand Down Expand Up @@ -746,6 +747,12 @@ def cart(self, access_token=None, revive='', **post):
revive: Revival method when abandoned cart. Can be 'merge' or 'squash'
"""
order = request.website.sale_get_order()
if order and order.carrier_id:
# Express checkout is based on the amout of the sale order. If there is already a
# delivery line, Express Checkout form will display and compute the price of the
# delivery two times (One already computed in the total amount of the SO and one added
# in the form while selecting the delivery carrier)
order._remove_delivery_line()
if order and order.state != 'draft':
request.session['sale_order_id'] = None
order = request.website.sale_get_order()
Expand Down Expand Up @@ -1225,9 +1232,7 @@ def process_express_checkout(
order_sudo.partner_id.commercial_partner_id.id, billing_address
)
order_sudo.partner_invoice_id = child_partner_id or self._create_or_edit_partner(
billing_address,
type='invoice',
parent_id=order_sudo.partner_id.id,
billing_address, type='invoice', parent_id=order_sudo.partner_id.id
)

# In a non-express flow, `sale_last_order_id` would be added in the session before the
Expand Down Expand Up @@ -1457,6 +1462,7 @@ def extra_info(self, **post):
# ------------------------------------------------------

def _get_express_shop_payment_values(self, order, **kwargs):
request.session['sale_last_order_id'] = order.id
payment_form_values = sale_portal.CustomerPortal._get_payment_values(
self, order, website_id=request.website.id, is_express_checkout=True
)
Expand All @@ -1472,6 +1478,11 @@ def _get_express_shop_payment_values(self, order, **kwargs):
})
if request.website.is_public_user():
payment_form_values['partner_id'] = -1
if request.website.enabled_delivery:
payment_form_values.update({
'shipping_info_required': not order.only_services,
'shipping_address_update_route': self._express_checkout_shipping_route,
})
return payment_form_values

def _get_shop_payment_values(self, order, **kwargs):
Expand All @@ -1489,7 +1500,29 @@ def _get_shop_payment_values(self, order, **kwargs):
'transaction_route': f'/shop/payment/transaction/{order.id}',
'landing_route': '/shop/payment/validate',
}
return {**portal_page_values, **payment_form_values}
values = {**portal_page_values, **payment_form_values}
if request.website.enabled_delivery:
has_storable_products = any(
line.product_id.type in ['consu', 'product'] for line in order.order_line
)
if not order._get_delivery_methods() and has_storable_products:
values['errors'].append((
_('Sorry, we are unable to ship your order'),
_('No shipping method is available for your current order and shipping address.'
' Please contact us for more information.')
))

if has_storable_products:
if order.carrier_id and not order.delivery_rating_success:
order._remove_delivery_line()
values['deliveries'] = order._get_delivery_methods().sudo()

values['delivery_has_storable'] = has_storable_products
values['delivery_action_id'] = request.env.ref(
'delivery.action_delivery_carrier_form'
).id

return values

@http.route('/shop/payment', type='http', auth='public', website=True, sitemap=False)
def shop_payment(self, **post):
Expand All @@ -1502,7 +1535,18 @@ def shop_payment(self, **post):
did go to a payment.provider website but closed the tab without
paying / canceling
"""
carrier_id = post.get('carrier_id')
keep_carrier = post.get('keep_carrier', False)
if keep_carrier:
keep_carrier = bool(int(keep_carrier))
if carrier_id:
carrier_id = int(carrier_id)
order = request.website.sale_get_order()
if order:
order._check_carrier_quotation(force_carrier_id=carrier_id, keep_carrier=keep_carrier)
if carrier_id:
return request.redirect("/shop/payment")

redirection = self.checkout_redirection(order) or self.checkout_check_address(order)
if redirection:
return redirection
Expand Down Expand Up @@ -1661,7 +1705,7 @@ def _change_website_config(self, **options):
def order_lines_2_google_api(self, order_lines):
""" Transforms a list of order lines into a dict for google analytics """
ret = []
for line in order_lines:
for line in order_lines.filtered(lambda line: not line.is_delivery):
product = line.product_id
ret.append({
'item_id': product.barcode or product.id,
Expand All @@ -1674,14 +1718,18 @@ def order_lines_2_google_api(self, order_lines):

def order_2_return_dict(self, order):
""" Returns the tracking_cart dict of the order for Google analytics basically defined to be inherited """
return {
tracking_cart_dict = {
'transaction_id': order.id,
'affiliation': order.company_id.name,
'value': order.amount_total,
'tax': order.amount_tax,
'currency': order.currency_id.name,
'items': self.order_lines_2_google_api(order.order_line),
}
delivery_line = order.order_line.filtered('is_delivery')
if delivery_line:
tracking_cart_dict['shipping'] = delivery_line.price_unit
return tracking_cart_dict

@http.route(['/shop/country_infos/<model("res.country"):country>'], type='json', auth="public", methods=['POST'], website=True)
def country_infos(self, country, mode, **kw):
Expand Down
7 changes: 7 additions & 0 deletions addons/website_sale/models/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def _get_product_sort_mapping():
default="Not Available For Sale")
contact_us_button_url = fields.Char(string="Contact Us Button URL", translate=True, default="/contactus")
enabled_portal_reorder_button = fields.Boolean(string="Re-order From Portal")
enabled_delivery = fields.Boolean(string="Enable Shipping", compute='_compute_enabled_delivery')

@api.depends('all_pricelist_ids')
def _compute_pricelist_ids(self):
Expand All @@ -127,6 +128,12 @@ def _compute_currency_id(self):
for website in self:
website.currency_id = website.pricelist_id.currency_id or website.company_id.currency_id

def _compute_enabled_delivery(self):
for website in self:
website.enabled_delivery = bool(website.env['delivery.carrier'].sudo().search_count(
[('website_id', 'in', (False, website.id)), ('is_published', '=', True)], limit=1
))

# This method is cached, must not return records! See also #8795
@tools.ormcache(
'country_code', 'show_visible',
Expand Down
1 change: 0 additions & 1 deletion addons/website_sale/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ access_ecom_extra_fields_public,access_ecom_extra_field public,model_website_sal
access_ecom_extra_fields_restricted_editor,access_ecom_extra_field restricted_editor,model_website_sale_extra_field,website.group_website_restricted_editor,1,1,1,1
access_website_base_unit_public,website.base.unit public,model_website_base_unit,,1,0,0,0
access_website_base_unit_sale_manager,website.base.unit sale manager,model_website_base_unit,sales_team.group_sale_manager,1,1,1,1
access_delivery_carrier,delivery.carrier,delivery.model_delivery_carrier,sales_team.group_sale_salesman,1,0,0,0
2 changes: 1 addition & 1 deletion addons/website_sale/tests/test_delivery_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from odoo.exceptions import UserError
from odoo.addons.payment.tests.common import PaymentCommon
from odoo.addons.website_sale.controllers.delivery_main import WebsiteSaleDelivery
from odoo.addons.website_sale.controllers.delivery import WebsiteSaleDelivery
from odoo.addons.website.tools import MockRequest
from odoo.tests import tagged

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from odoo.tests import tagged

from odoo.addons.base.tests.common import HttpCaseWithUserDemo
from odoo.addons.website_sale.controllers.delivery_main import WebsiteSaleDelivery as WebsiteSaleDeliveryController
from odoo.addons.website_sale.controllers.delivery import WebsiteSaleDelivery as WebsiteSaleDeliveryController


@tagged('post_install', '-at_install')
Expand Down
2 changes: 1 addition & 1 deletion addons/website_sale_loyalty/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import delivery_main
from . import delivery
from . import main
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from odoo import http
from odoo.addons.website_sale.controllers.delivery_main import WebsiteSaleDelivery
from odoo.addons.website_sale.controllers.delivery import WebsiteSaleDelivery
from odoo.http import request


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from odoo import http, _
from odoo.addons.website_sale.controllers.main import WebsiteSale, PaymentPortal
from odoo.addons.website_sale.controllers.delivery_main import WebsiteSaleDelivery
from odoo.addons.website_sale.controllers.delivery import WebsiteSaleDelivery

from odoo.exceptions import AccessDenied, ValidationError, UserError
from odoo.http import request
Expand Down

0 comments on commit 323bb4f

Please sign in to comment.