Skip to content

Commit

Permalink
[IMP] website_sale_cart_with_unconfirmed_budget: Remove fork
Browse files Browse the repository at this point in the history
  • Loading branch information
unaiberis committed Oct 21, 2024
1 parent a321ca6 commit c4feba7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
import logging

from odoo import fields, http
from odoo.http import request

from odoo.addons.website_sale.controllers.main import WebsiteSale

_logger = logging.getLogger(__name__)


class WebsiteSale(WebsiteSale):

@http.route(["/shop/cart"], type="http", auth="public", website=True, sitemap=False)
def cart(self, access_token=None, revive="", **post):
_logger.info("Accessing cart...")
response = super().cart(access_token=access_token, revive=revive, **post)
order = request.website.sale_get_order()

if order and order.state != "draft":
_logger.info("Current order state is not 'draft', resetting sale_order_id.")
request.session["sale_order_id"] = None
order = request.website.sale_get_order()

request.session["website_sale_cart_quantity"] = order.cart_quantity
if not order:
_logger.info(
"No current order found, searching for the last order in 'sent' state."
)
last_order = request.env["sale.order"].search(
[
("partner_id", "=", request.env.user.partner_id.id),
("state", "=", "sent"),
],
order="date_order desc",
limit=1,
)
if last_order:
_logger.info("Found last order in 'sent' state.")
order = last_order
else:
_logger.info("No last order found in 'sent' state.")

request.session["website_sale_cart_quantity"] = (
order.cart_quantity if order else 0
)
_logger.info(
f"Cart quantity set to: {request.session['website_sale_cart_quantity']}"
)

values = {
"website_sale_order": order,
Expand All @@ -22,14 +53,18 @@ def cart(self, access_token=None, revive="", **post):
}

if order:
_logger.info("Updating values with order extra values.")
values.update(order._get_website_sale_extra_values())
_logger.info("Removing inactive product lines from the order.")
order.order_line.filtered(
lambda line: line.product_id and not line.product_id.active
).unlink()
values["suggested_products"] = order._cart_accessories()
values.update(self._get_express_shop_payment_values(order))

return request.render("website_sale.cart", values)
response.qcontext.update(values)
_logger.info("Rendering cart with updated values.")
return response

@http.route(
["/shop/cart/update"],
Expand All @@ -46,23 +81,29 @@ def cart_update(
product_custom_attribute_values=None,
no_variant_attribute_values=None,
express=False,
**kwargs
**kwargs,
):
_logger.info(
f"Updating cart with product_id: {product_id}, add_qty: {add_qty}, set_qty: {set_qty}."
)
sale_order = super().cart_update(
product_id=product_id,
add_qty=add_qty,
set_qty=set_qty,
product_custom_attribute_values=product_custom_attribute_values,
no_variant_attribute_values=no_variant_attribute_values,
express=express,
**kwargs
**kwargs,
)

request.session["website_sale_cart_quantity"] = sale_order.cart_quantity
_logger.info(f"Cart quantity updated to: {sale_order.cart_quantity}.")

if express:
_logger.info("Express checkout initiated, redirecting to checkout.")
return request.redirect("/shop/checkout?express=1")

_logger.info("Redirecting to cart after update.")
return request.redirect("/shop/cart")

@http.route(
Expand All @@ -82,8 +123,11 @@ def cart_update_json(
display=True,
product_custom_attribute_values=None,
no_variant_attribute_values=None,
**kw
**kw,
):
_logger.info(
f"Updating cart via JSON with product_id: {product_id}, line_id: {line_id}."
)
values = super().cart_update_json(
product_id=product_id,
line_id=line_id,
Expand All @@ -92,9 +136,12 @@ def cart_update_json(
display=display,
product_custom_attribute_values=product_custom_attribute_values,
no_variant_attribute_values=no_variant_attribute_values,
**kw
**kw,
)

request.session["website_sale_cart_quantity"] = values.get("cart_quantity", 0)
_logger.info(
f"Cart quantity from JSON update set to: {request.session['website_sale_cart_quantity']}."
)

return values
49 changes: 29 additions & 20 deletions website_sale_cart_with_unconfirmed_budget/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,34 @@ class SaleOrder(models.Model):
_inherit = "sale.order"

@api.model
def create(self, vals):
unconfirmed_budget = self.env["sale.order"].search(
[
("state", "=", "draft"),
("partner_id", "=", vals.get("partner_id")),
],
limit=1,
)
def create(self, vals_list):
if isinstance(vals_list, dict):
vals_list = [vals_list]

if unconfirmed_budget:
unconfirmed_budget.order_line.unlink()
for line in vals.get("order_line", []):
unconfirmed_budget.order_line.create(
{
"order_id": unconfirmed_budget.id,
"product_id": line[2]["product_id"],
"product_uom_qty": line[2]["product_uom_qty"],
}
)
return unconfirmed_budget
created_orders = self.browse()

return super().create(vals)
for vals in vals_list:
unconfirmed_budget = self.env["sale.order"].search(
[
("state", "=", "draft"),
("partner_id", "=", vals.get("partner_id")),
],
limit=1,
)

if unconfirmed_budget:
unconfirmed_budget.order_line.unlink()
for line in vals.get("order_line", []):
unconfirmed_budget.order_line.create(
{
"order_id": unconfirmed_budget.id,
"product_id": line[2]["product_id"],
"product_uom_qty": line[2]["product_uom_qty"],
}
)
created_orders += unconfirmed_budget
else:
created_order = super().create(vals)
created_orders += created_order

return created_orders

0 comments on commit c4feba7

Please sign in to comment.