Skip to content

Commit 03a5fa8

Browse files
WIP FIX behavior
1 parent 99fc1f1 commit 03a5fa8

File tree

2 files changed

+43
-81
lines changed

2 files changed

+43
-81
lines changed

website_sale_secondary_unit/static/src/js/website_sale_secondary_unit.js

+43-26
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,16 @@ odoo.define("website_sale_secondary_unit.animation", function (require) {
1010
init: function (parent, editableMode) {
1111
this._super.apply(this, arguments);
1212
this.$secondary_uom = null;
13-
this.$secondary_uom_qty = null;
1413
this.$product_qty = null;
15-
this.secondary_uom_qty = null;
1614
this.secondary_uom_factor = null;
1715
this.product_uom_factor = null;
1816
this.product_qty = null;
1917
},
2018
start: function () {
2119
const _this = this;
2220
this.$secondary_uom = $("#secondary_uom");
23-
this.$secondary_uom_qty = $(".secondary-quantity");
2421
this.$product_qty = $(".quantity");
2522
this._setValues();
26-
this.$target.on(
27-
"change",
28-
".secondary-quantity",
29-
this._onChangeSecondaryUom.bind(this)
30-
);
3123
this.$target.on(
3224
"change",
3325
"#secondary_uom",
@@ -39,16 +31,14 @@ odoo.define("website_sale_secondary_unit.animation", function (require) {
3931
});
4032
},
4133
_setValues: function () {
42-
this.secondary_uom_qty = Number(
43-
this.$target.find(".secondary-quantity").val()
44-
);
4534
this.secondary_uom_factor = Number(
4635
$("option:selected", this.$secondary_uom).data("secondary-uom-factor")
4736
);
4837
this.product_uom_factor = Number(
4938
$("option:selected", this.$secondary_uom).data("product-uom-factor")
5039
);
5140
this.product_qty = Number($(".quantity").val());
41+
this.uom_factor = this.secondary_uom_factor * this.product_uom_factor
5242
},
5343

5444
_onChangeSecondaryUom: function (ev) {
@@ -59,14 +49,30 @@ odoo.define("website_sale_secondary_unit.animation", function (require) {
5949
ev.currentTarget = $(".form-control.quantity");
6050
}
6151
this._setValues();
62-
const factor = this.secondary_uom_factor * this.product_uom_factor;
63-
this.$product_qty.val(this.secondary_uom_qty * factor);
52+
this.$product_qty.val(this.uom_factor);
6453
this.onChangeAddQuantity(ev);
6554
},
6655
_onChangeProductQty: function () {
56+
// This method is called when the product quantity is changed
57+
// It will adjust the quantity to be a multiple of the uom factor
58+
// Constraint: Quantity cannot be less than 0
6759
this._setValues();
68-
const factor = this.secondary_uom_factor * this.product_uom_factor;
69-
this.$secondary_uom_qty.val(this.product_qty / factor);
60+
const product_qty = this.$product_qty.val();
61+
var qty_ratio = parseFloat(product_qty / this.uom_factor);
62+
if (qty_ratio < 1) {
63+
qty_ratio = 1;
64+
}
65+
// By using round, we get the closest ratio telling us if the value
66+
// is decreased (1.75 -> 2) or increased (2.25 -> 2)
67+
var nearest_ratio = Math.round(qty_ratio);
68+
if (nearest_ratio !== qty_ratio) {
69+
if (nearest_ratio < qty_ratio) { // increased
70+
qty_ratio = Math.ceil(qty_ratio);
71+
} else { // decreased
72+
qty_ratio = Math.floor(qty_ratio);
73+
}
74+
}
75+
this.$product_qty.val(qty_ratio * this.uom_factor);
7076
},
7177
});
7278

@@ -76,7 +82,6 @@ odoo.define("website_sale_secondary_unit.animation", function (require) {
7682
init: function (parent, editableMode) {
7783
this._super.apply(this, arguments);
7884
this.$product_qty = null;
79-
this.secondary_uom_qty = null;
8085
this.secondary_uom_factor = null;
8186
this.product_uom_factor = null;
8287
this.product_qty = null;
@@ -85,25 +90,40 @@ odoo.define("website_sale_secondary_unit.animation", function (require) {
8590
var _this = this;
8691
this.$target.on(
8792
"change",
88-
"input.js_secondary_quantity[data-line-id]",
93+
"input.js_quantity[data-line-id]",
8994
function () {
90-
_this._onChangeSecondaryUom(this);
95+
_this._onChangeProductQty(_this);
9196
}
9297
);
9398
},
9499
_setValues: function (order_line) {
95100
this.$product_qty = this.$target.find(
96101
".quantity[data-line-id=" + order_line.dataset.lineId + "]"
97102
);
98-
this.secondary_uom_qty = Number(order_line.value);
99103
this.secondary_uom_factor = Number(order_line.dataset.secondaryUomFactor);
100104
this.product_uom_factor = Number(order_line.dataset.productUomFactor);
101105
},
102-
_onChangeSecondaryUom: function (order_line) {
106+
_onChangeProductQty: function (order_line) {
107+
// This method is called when the product quantity is changed
108+
// It will adjust the quantity to be a multiple of the uom factor
109+
// Constraint: Quantity cannot be less than 0
103110
this._setValues(order_line);
104-
const factor = this.secondary_uom_factor * this.product_uom_factor;
105-
this.$product_qty.val(this.secondary_uom_qty * factor);
106-
this.$product_qty.trigger("change");
111+
const product_qty = this.$product_qty.val();
112+
var qty_ratio = parseFloat(product_qty / this.uom_factor);
113+
if (qty_ratio < 1) {
114+
qty_ratio = 1;
115+
}
116+
// By using round, we get the closest ratio telling us if the value
117+
// is decreased (1.75 -> 2) or increased (2.25 -> 2)
118+
var nearest_ratio = Math.round(qty_ratio);
119+
if (nearest_ratio !== qty_ratio) {
120+
if (nearest_ratio < qty_ratio) { // increased
121+
qty_ratio = Math.ceil(qty_ratio);
122+
} else { // decreased
123+
qty_ratio = Math.floor(qty_ratio);
124+
}
125+
}
126+
this.$product_qty.val(qty_ratio * this.uom_factor);
107127
},
108128
});
109129
});
@@ -123,9 +143,6 @@ odoo.define("website_sale_secondary_unit.website_sale", function (require) {
123143
this.rootProduct.secondary_uom_id = $(this.$target)
124144
.find("#secondary_uom")
125145
.val();
126-
this.rootProduct.secondary_uom_qty = $(this.$target)
127-
.find(".secondary-quantity")
128-
.val();
129146
}
130147

131148
this._super.apply(this, arguments);

website_sale_secondary_unit/views/templates.xml

-55
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,6 @@
22
<!-- Copyright 2019 Tecnativa - Sergio Teruel
33
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
44
<odoo>
5-
<template id="secondary_qty">
6-
<div
7-
class="css_quantity input-group oe_website_spinner"
8-
contenteditable="false"
9-
>
10-
<div class="input-group-prepend">
11-
<a
12-
t-attf-href="#"
13-
class="btn btn-secondary js_add_cart_json"
14-
aria-label="Remove one"
15-
title="Remove one"
16-
>
17-
<i class="fa fa-minus" />
18-
</a>
19-
</div>
20-
<input
21-
type="text"
22-
class="form-control secondary-quantity"
23-
data-min="1"
24-
name="add_secondary_qty"
25-
value="1"
26-
/>
27-
<div class="input-group-append">
28-
<a
29-
t-attf-href="#"
30-
class="btn btn-secondary float_left js_add_cart_json"
31-
aria-label="Add one"
32-
title="Add one"
33-
>
34-
<i class="fa fa-plus" />
35-
</a>
36-
</div>
37-
</div>
38-
</template>
395
<template id="second_qty_description">
406
<t
417
t-set="factor"
@@ -59,7 +25,6 @@
5925
>
6026
<t t-if="secondary_uom_ids">
6127
<div class="mb8 secondary-unit">
62-
<t t-call="website_sale_secondary_unit.secondary_qty" />
6328
<select
6429
class="form-control mt4"
6530
id="secondary_uom"
@@ -101,13 +66,6 @@
10166
</t>
10267
</xpath>
10368
</template>
104-
<template id="product_quantity" inherit_id="website_sale.product_quantity">
105-
<xpath expr="//input[@name='add_qty']/.." position="attributes">
106-
<attribute
107-
name="t-attf-class"
108-
>css_quantity input-group oe_website_spinner #{'d-none' if secondary_uom_ids else None}</attribute>
109-
</xpath>
110-
</template>
11169
<template id="cart_lines" inherit_id="website_sale.cart_lines">
11270
<xpath expr="//td[hasclass('td-qty')]/div" position="before">
11371
<t t-if="line.secondary_uom_id">
@@ -152,14 +110,6 @@
152110
</div>
153111
</t>
154112
</xpath>
155-
<xpath
156-
expr="//td[hasclass('td-qty')]/div[hasclass('css_quantity')]"
157-
position="attributes"
158-
>
159-
<attribute
160-
name="t-attf-class"
161-
>css_quantity input-group oe_website_spinner #{'d-none' if line.secondary_uom_id else None}</attribute>
162-
</xpath>
163113
<xpath
164114
expr="//td[hasclass('td-qty')]/div[hasclass('css_quantity')]"
165115
position="after"
@@ -213,11 +163,6 @@
213163
</div>
214164
</t>
215165
</xpath>
216-
<xpath expr="//td[hasclass('td-qty')]/div" position="attributes">
217-
<attribute
218-
name="t-attf-class"
219-
>#{'d-none' if line.secondary_uom_id else None}</attribute>
220-
</xpath>
221166
<xpath expr="//td[hasclass('td-qty')]/div" position="after">
222167
<t t-if="not line.secondary_uom_id">
223168
<span t-out="line.product_uom.sudo().name" />

0 commit comments

Comments
 (0)