@@ -10,24 +10,16 @@ odoo.define("website_sale_secondary_unit.animation", function (require) {
10
10
init : function ( parent , editableMode ) {
11
11
this . _super . apply ( this , arguments ) ;
12
12
this . $secondary_uom = null ;
13
- this . $secondary_uom_qty = null ;
14
13
this . $product_qty = null ;
15
- this . secondary_uom_qty = null ;
16
14
this . secondary_uom_factor = null ;
17
15
this . product_uom_factor = null ;
18
16
this . product_qty = null ;
19
17
} ,
20
18
start : function ( ) {
21
19
const _this = this ;
22
20
this . $secondary_uom = $ ( "#secondary_uom" ) ;
23
- this . $secondary_uom_qty = $ ( ".secondary-quantity" ) ;
24
21
this . $product_qty = $ ( ".quantity" ) ;
25
22
this . _setValues ( ) ;
26
- this . $target . on (
27
- "change" ,
28
- ".secondary-quantity" ,
29
- this . _onChangeSecondaryUom . bind ( this )
30
- ) ;
31
23
this . $target . on (
32
24
"change" ,
33
25
"#secondary_uom" ,
@@ -39,16 +31,14 @@ odoo.define("website_sale_secondary_unit.animation", function (require) {
39
31
} ) ;
40
32
} ,
41
33
_setValues : function ( ) {
42
- this . secondary_uom_qty = Number (
43
- this . $target . find ( ".secondary-quantity" ) . val ( )
44
- ) ;
45
34
this . secondary_uom_factor = Number (
46
35
$ ( "option:selected" , this . $secondary_uom ) . data ( "secondary-uom-factor" )
47
36
) ;
48
37
this . product_uom_factor = Number (
49
38
$ ( "option:selected" , this . $secondary_uom ) . data ( "product-uom-factor" )
50
39
) ;
51
40
this . product_qty = Number ( $ ( ".quantity" ) . val ( ) ) ;
41
+ this . uom_factor = this . secondary_uom_factor * this . product_uom_factor
52
42
} ,
53
43
54
44
_onChangeSecondaryUom : function ( ev ) {
@@ -59,14 +49,30 @@ odoo.define("website_sale_secondary_unit.animation", function (require) {
59
49
ev . currentTarget = $ ( ".form-control.quantity" ) ;
60
50
}
61
51
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 ) ;
64
53
this . onChangeAddQuantity ( ev ) ;
65
54
} ,
66
55
_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
67
59
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 ) ;
70
76
} ,
71
77
} ) ;
72
78
@@ -76,7 +82,6 @@ odoo.define("website_sale_secondary_unit.animation", function (require) {
76
82
init : function ( parent , editableMode ) {
77
83
this . _super . apply ( this , arguments ) ;
78
84
this . $product_qty = null ;
79
- this . secondary_uom_qty = null ;
80
85
this . secondary_uom_factor = null ;
81
86
this . product_uom_factor = null ;
82
87
this . product_qty = null ;
@@ -85,25 +90,40 @@ odoo.define("website_sale_secondary_unit.animation", function (require) {
85
90
var _this = this ;
86
91
this . $target . on (
87
92
"change" ,
88
- "input.js_secondary_quantity [data-line-id]" ,
93
+ "input.js_quantity [data-line-id]" ,
89
94
function ( ) {
90
- _this . _onChangeSecondaryUom ( this ) ;
95
+ _this . _onChangeProductQty ( _this ) ;
91
96
}
92
97
) ;
93
98
} ,
94
99
_setValues : function ( order_line ) {
95
100
this . $product_qty = this . $target . find (
96
101
".quantity[data-line-id=" + order_line . dataset . lineId + "]"
97
102
) ;
98
- this . secondary_uom_qty = Number ( order_line . value ) ;
99
103
this . secondary_uom_factor = Number ( order_line . dataset . secondaryUomFactor ) ;
100
104
this . product_uom_factor = Number ( order_line . dataset . productUomFactor ) ;
101
105
} ,
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
103
110
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 ) ;
107
127
} ,
108
128
} ) ;
109
129
} ) ;
@@ -123,9 +143,6 @@ odoo.define("website_sale_secondary_unit.website_sale", function (require) {
123
143
this . rootProduct . secondary_uom_id = $ ( this . $target )
124
144
. find ( "#secondary_uom" )
125
145
. val ( ) ;
126
- this . rootProduct . secondary_uom_qty = $ ( this . $target )
127
- . find ( ".secondary-quantity" )
128
- . val ( ) ;
129
146
}
130
147
131
148
this . _super . apply ( this , arguments ) ;
0 commit comments