Skip to content

Commit

Permalink
WIP FIX behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
StephaneMangin committed Jan 10, 2025
1 parent 99fc1f1 commit 8a0cf88
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,16 @@ odoo.define("website_sale_secondary_unit.animation", function (require) {
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",
Expand All @@ -39,16 +31,14 @@ odoo.define("website_sale_secondary_unit.animation", function (require) {
});
},
_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());
this.uom_factor = this.secondary_uom_factor * this.product_uom_factor
},

_onChangeSecondaryUom: function (ev) {
Expand All @@ -59,14 +49,30 @@ odoo.define("website_sale_secondary_unit.animation", function (require) {
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.$product_qty.val(this.uom_factor);
this.onChangeAddQuantity(ev);
},
_onChangeProductQty: function () {
// This method is called when the product quantity is changed
// It will adjust the quantity to be a multiple of the uom factor
// Constraint: Quantity cannot be less than 0
this._setValues();
const factor = this.secondary_uom_factor * this.product_uom_factor;
this.$secondary_uom_qty.val(this.product_qty / factor);
const product_qty = this.$product_qty.val();
var qty_ratio = parseFloat(product_qty / this.uom_factor);
if (qty_ratio < 1) {
qty_ratio = 1;
}
// By using round, we get the closest ratio telling us if the value
// is decreased (1.75 -> 2) or increased (2.25 -> 2)
var nearest_ratio = Math.round(qty_ratio);
if (nearest_ratio !== qty_ratio) {
if (nearest_ratio < qty_ratio) { // increased
qty_ratio = Math.ceil(qty_ratio);
} else { // decreased
qty_ratio = Math.floor(qty_ratio);
}
}
this.$product_qty.val(qty_ratio * this.uom_factor);
},
});

Expand Down Expand Up @@ -123,9 +129,6 @@ odoo.define("website_sale_secondary_unit.website_sale", function (require) {
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);
Expand Down
55 changes: 0 additions & 55 deletions website_sale_secondary_unit/views/templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,6 @@
<!-- Copyright 2019 Tecnativa - Sergio Teruel
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>
<template id="secondary_qty">
<div
class="css_quantity input-group oe_website_spinner"
contenteditable="false"
>
<div class="input-group-prepend">
<a
t-attf-href="#"
class="btn btn-secondary js_add_cart_json"
aria-label="Remove one"
title="Remove one"
>
<i class="fa fa-minus" />
</a>
</div>
<input
type="text"
class="form-control secondary-quantity"
data-min="1"
name="add_secondary_qty"
value="1"
/>
<div class="input-group-append">
<a
t-attf-href="#"
class="btn btn-secondary float_left js_add_cart_json"
aria-label="Add one"
title="Add one"
>
<i class="fa fa-plus" />
</a>
</div>
</div>
</template>
<template id="second_qty_description">
<t
t-set="factor"
Expand All @@ -59,7 +25,6 @@
>
<t t-if="secondary_uom_ids">
<div class="mb8 secondary-unit">
<t t-call="website_sale_secondary_unit.secondary_qty" />
<select
class="form-control mt4"
id="secondary_uom"
Expand Down Expand Up @@ -101,13 +66,6 @@
</t>
</xpath>
</template>
<template id="product_quantity" inherit_id="website_sale.product_quantity">
<xpath expr="//input[@name='add_qty']/.." position="attributes">
<attribute
name="t-attf-class"
>css_quantity input-group oe_website_spinner #{'d-none' if secondary_uom_ids else None}</attribute>
</xpath>
</template>
<template id="cart_lines" inherit_id="website_sale.cart_lines">
<xpath expr="//td[hasclass('td-qty')]/div" position="before">
<t t-if="line.secondary_uom_id">
Expand Down Expand Up @@ -152,14 +110,6 @@
</div>
</t>
</xpath>
<xpath
expr="//td[hasclass('td-qty')]/div[hasclass('css_quantity')]"
position="attributes"
>
<attribute
name="t-attf-class"
>css_quantity input-group oe_website_spinner #{'d-none' if line.secondary_uom_id else None}</attribute>
</xpath>
<xpath
expr="//td[hasclass('td-qty')]/div[hasclass('css_quantity')]"
position="after"
Expand Down Expand Up @@ -213,11 +163,6 @@
</div>
</t>
</xpath>
<xpath expr="//td[hasclass('td-qty')]/div" position="attributes">
<attribute
name="t-attf-class"
>#{'d-none' if line.secondary_uom_id else None}</attribute>
</xpath>
<xpath expr="//td[hasclass('td-qty')]/div" position="after">
<t t-if="not line.secondary_uom_id">
<span t-out="line.product_uom.sudo().name" />
Expand Down

0 comments on commit 8a0cf88

Please sign in to comment.