Skip to content

Commit

Permalink
[MIG] website_sale_secondary_unit: Migration to version 17.0
Browse files Browse the repository at this point in the history
- Replaced the popover implementation with notifications, following the update in odoo/odoo#133990.
- Removed the Unit from the displayed price. Previously, Odoo showed the price per unit, making it appropriate to include the UoM. However, as it now displays the total, the UoM is unnecessary.
  • Loading branch information
carlos-lopez-tecnativa committed Jan 10, 2025
1 parent 16499ce commit a7b529a
Show file tree
Hide file tree
Showing 15 changed files with 377 additions and 298 deletions.
1 change: 1 addition & 0 deletions website_sale_secondary_unit/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Contributors
- Sergio Teruel
- Carlos Roca
- Pilar Vargas
- Carlos Lopez

Maintainers
-----------
Expand Down
6 changes: 3 additions & 3 deletions website_sale_secondary_unit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Website Sale Secondary Unit",
"summary": "Allow manage secondary units in website shop",
"version": "15.0.1.2.1",
"version": "17.0.1.0.0",
"development_status": "Beta",
"category": "Website",
"website": "https://github.com/OCA/e-commerce",
Expand All @@ -23,11 +23,11 @@
"post_init_hook": "post_init_hook",
"assets": {
"web.assets_frontend": [
"/website_sale_secondary_unit/static/src/js/website_sale_secondary_unit.js",
"/website_sale_secondary_unit/static/src/js/**/*.esm.js",
"/website_sale_secondary_unit/static/src/scss/website_sale_secondary_unit.scss",
],
"web.assets_tests": [
"/website_sale_secondary_unit/static/src/js/website_sale_secondary_unit_tour.js"
"/website_sale_secondary_unit/static/tests/tours/website_sale_secondary_unit_tour.esm.js"
],
},
}
21 changes: 21 additions & 0 deletions website_sale_secondary_unit/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,24 @@ def _prepare_product_values(self, product, category, search, **kwargs):
lambda su: su.active and su.is_published
)
return res

def _get_cart_notification_information(self, order, line_ids):
res = super()._get_cart_notification_information(order, line_ids)
for line in res["lines"]:
sale_line = request.env["sale.order.line"].browse(line["id"])
line["secondary_uom_name"] = ""
line["secondary_uom_qty"] = sale_line.secondary_uom_qty
secondary_uom = sale_line.secondary_uom_id
if not secondary_uom:
continue
factor = (
int(secondary_uom.factor) == secondary_uom.factor
and int(secondary_uom.factor)
or secondary_uom.factor
)
uom_name = secondary_uom.product_tmpl_id.sudo().uom_id.name
secondary_uom_name = f"{secondary_uom.name} {factor}"
if uom_name != secondary_uom.name:
secondary_uom_name += f" {uom_name}"
line["secondary_uom_name"] = secondary_uom_name
return res
4 changes: 2 additions & 2 deletions website_sale_secondary_unit/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


def post_init_hook(cr, registry):
def post_init_hook(env):
"""
At installation time, set allow_uom_sell field as true for all products
that have already been created.
"""
cr.execute(
env.cr.execute(
"""
UPDATE product_template
SET allow_uom_sell=true
Expand Down
18 changes: 18 additions & 0 deletions website_sale_secondary_unit/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,21 @@ class ProductTemplate(models.Model):
string="Allow to sell in unit of measure",
default=True,
)

def _get_combination_info(
self,
combination=False,
product_id=False,
add_qty=1,
parent_combination=False,
only_template=False,
):
combination_info = super()._get_combination_info(
combination=combination,
product_id=product_id,
add_qty=add_qty,
parent_combination=parent_combination,
only_template=only_template,
)
combination_info.update({"has_secondary_uom": bool(self.secondary_uom_ids)})
return combination_info
35 changes: 29 additions & 6 deletions website_sale_secondary_unit/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,36 @@ def _cart_find_product_line(self, product_id=None, line_id=None, **kwargs):
)
return so_lines

def _website_product_id_change(self, order_id, product_id, qty=0, **kwargs):
res = super()._website_product_id_change(
order_id, product_id, qty=qty, **kwargs
def _prepare_order_line_values(
self,
product_id,
quantity,
linked_line_id=False,
no_variant_attribute_values=None,
product_custom_attribute_values=None,
**kwargs,
):
values = super()._prepare_order_line_values(
product_id,
quantity,
linked_line_id=linked_line_id,
no_variant_attribute_values=no_variant_attribute_values,
product_custom_attribute_values=product_custom_attribute_values,
**kwargs,
)
secondary_uom_id = self.env.context.get("secondary_uom_id", False)
res["secondary_uom_id"] = secondary_uom_id
return res
values["secondary_uom_id"] = self.env.context.get("secondary_uom_id")
return values

def _prepare_order_line_update_values(
self, order_line, quantity, linked_line_id=False, **kwargs
):
values = super()._prepare_order_line_update_values(
order_line, quantity, linked_line_id=linked_line_id, **kwargs
)
secondary_uom_id = self.env.context.get("secondary_uom_id")
if secondary_uom_id != order_line.secondary_uom_id.id:
values["secondary_uom_id"] = secondary_uom_id
return values

def _cart_update(
self,
Expand Down
1 change: 1 addition & 0 deletions website_sale_secondary_unit/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
- Sergio Teruel
- Carlos Roca
- Pilar Vargas
- Carlos Lopez
1 change: 1 addition & 0 deletions website_sale_secondary_unit/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<li>Sergio Teruel</li>
<li>Carlos Roca</li>
<li>Pilar Vargas</li>
<li>Carlos Lopez</li>
</ul>
</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/** @odoo-module **/
/* Copyright 2025 Carlos Lopez - Tecnativa
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */

import {AddToCartNotification} from "@website_sale/js/notification/add_to_cart_notification/add_to_cart_notification";
import {patch} from "@web/core/utils/patch";

patch(AddToCartNotification.prototype, {
/**
* Return the product summary based on the line information.
*
* If the line has a secondary unit of measure,
* the product summary is computed based on the secondary unit of measure quantity and name,
*
* @param {Object} line - The line element for which to return the product summary.
* @returns {String} - The product summary.
*/
getProductSummary(line) {
if (line.secondary_uom_name) {
return (
line.secondary_uom_qty +
" x " +
line.secondary_uom_name +
" " +
line.name
);
}
return super.getProductSummary(...arguments);
},
});

const extendedShape = {
...AddToCartNotification.props.lines.element.shape,
secondary_uom_name: String,
secondary_uom_qty: {type: Number, optional: true},
};

AddToCartNotification.props = {
...AddToCartNotification.props,
lines: {
...AddToCartNotification.props.lines,
element: {
...AddToCartNotification.props.lines.element,
shape: extendedShape,
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/** @odoo-module **/
/* Copyright 2025 Carlos Lopez - Tecnativa
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */

import {CartNotification} from "@website_sale/js/notification/cart_notification/cart_notification";

const extendedShape = {
...CartNotification.props.lines.element.shape,
secondary_uom_name: String,
secondary_uom_qty: {type: Number, optional: true},
};

CartNotification.props = {
...CartNotification.props,
lines: {
...CartNotification.props.lines,
element: {
...CartNotification.props.lines.element,
shape: extendedShape,
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/** @odoo-module **/
/* Copyright 2019 Sergio Teruel
* Copyright 2025 Carlos Lopez - Tecnativa
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */

import "@website_sale/js/website_sale";
import VariantMixin from "@website_sale/js/sale_variant_mixin";
import publicWidget from "@web/legacy/js/public/public_widget";

publicWidget.registry.sale_secondary_unit = publicWidget.Widget.extend(VariantMixin, {
selector: ".secondary-unit",
// eslint-disable-next-line no-unused-vars
init: function (parent, editableMode) {
this._super.apply(this, arguments);
this.$secondary_uom = null;
this.$secondary_uom_qty = null;
this.$product_qty = null;
this.secondary_uom_qty = null;
this.secondary_uom_factor = null;
this.product_uom_factor = null;
this.product_qty = null;
},
start: function () {
const _this = this;
this.$secondary_uom = $("#secondary_uom");
this.$secondary_uom_qty = $(".secondary-quantity");
this.$product_qty = $(".quantity");
this._setValues();
this.$target.on(
"change",
".secondary-quantity",
this._onChangeSecondaryUom.bind(this)
);
this.$target.on(
"change",
"#secondary_uom",
this._onChangeSecondaryUom.bind(this)
);
this.$product_qty.on("change", null, this._onChangeProductQty.bind(this));
return this._super.apply(this, arguments).then(function () {
_this._onChangeSecondaryUom();
});
},
_setValues: function () {
this.secondary_uom_qty = Number(this.$target.find(".secondary-quantity").val());
this.secondary_uom_factor = Number(
$("option:selected", this.$secondary_uom).data("secondary-uom-factor")
);
this.product_uom_factor = Number(
$("option:selected", this.$secondary_uom).data("product-uom-factor")
);
this.product_qty = Number($(".quantity").val());
},

_onChangeSecondaryUom: function (ev) {
if (!ev) {
// HACK: Create a fake event to locate the form on "onChangeAddQuantity"
// odoo method
ev = jQuery.Event("fakeEvent");
ev.currentTarget = $(".form-control.quantity");
}
this._setValues();
const factor = this.secondary_uom_factor * this.product_uom_factor;
this.$product_qty.val(this.secondary_uom_qty * factor);
this.onChangeAddQuantity(ev);
},
_onChangeProductQty: function () {
this._setValues();
const factor = this.secondary_uom_factor * this.product_uom_factor;
this.$secondary_uom_qty.val(this.product_qty / factor);
},
});

publicWidget.registry.sale_secondary_unit_cart = publicWidget.Widget.extend({
selector: ".oe_cart",
// eslint-disable-next-line no-unused-vars
init: function (parent, editableMode) {
this._super.apply(this, arguments);
this.$product_qty = null;
this.secondary_uom_qty = null;
this.secondary_uom_factor = null;
this.product_uom_factor = null;
this.product_qty = null;
},
start: function () {
var _this = this;
this.$target.on(
"change",
"input.js_secondary_quantity[data-line-id]",
function () {
_this._onChangeSecondaryUom(this);
}
);
},
_setValues: function (order_line) {
this.$product_qty = this.$target.find(
".quantity[data-line-id=" + order_line.dataset.lineId + "]"
);
this.secondary_uom_qty = Number(order_line.value);
this.secondary_uom_factor = Number(order_line.dataset.secondaryUomFactor);
this.product_uom_factor = Number(order_line.dataset.productUomFactor);
},
_onChangeSecondaryUom: function (order_line) {
this._setValues(order_line);
const factor = this.secondary_uom_factor * this.product_uom_factor;
this.$product_qty.val(this.secondary_uom_qty * factor);
this.$product_qty.trigger("change");
},
});

publicWidget.registry.WebsiteSale.include({
_onChangeCombination: function (ev, $parent, combination) {
const quantity = $parent.find(".css_quantity:not(.secondary_qty)");
const res = this._super(...arguments);
if (combination.has_secondary_uom) {
quantity.removeClass("d-inline-flex").addClass("d-none");
} else {
quantity.removeClass("d-none").addClass("d-inline-flex");
}
return res;
},
_submitForm: function () {
if (
!("secondary_uom_id" in this.rootProduct) &&
$(this.$target).find("#secondary_uom").length
) {
this.rootProduct.secondary_uom_id = $(this.$target)
.find("#secondary_uom")
.val();
this.rootProduct.secondary_uom_qty = $(this.$target)
.find(".secondary-quantity")
.val();
}

this._super.apply(this, arguments);
},
});
Loading

0 comments on commit a7b529a

Please sign in to comment.