@@ -13,9 +13,9 @@ export var PackagingQtyPickerMixin = {
13
13
methods : {
14
14
on_change_pkg_qty : function ( event ) {
15
15
const input = event . target ;
16
- let new_qty = parseInt ( input . value , 10 ) ;
16
+ let new_qty = parseInt ( input . value || 0 , 10 ) ;
17
17
const data = $ ( input ) . data ( ) ;
18
- const origvalue = data . origvalue || 0 ;
18
+ const origvalue = parseInt ( data . origvalue || 0 , 10 ) ;
19
19
// Check max qty reached
20
20
const future_qty = this . value + data . pkg . qty * ( new_qty - origvalue ) ;
21
21
if ( new_qty && future_qty > this . original_value ) {
@@ -75,6 +75,10 @@ export var PackagingQtyPickerMixin = {
75
75
// const min_unit = _.last(pkg_by_qty);
76
76
pkg_by_qty . forEach ( function ( pkg ) {
77
77
let qty_per_pkg = 0 ;
78
+ if ( ! pkg . qty ) {
79
+ console . error ( "Packaging with no quantity: skipping" , pkg ) ;
80
+ return { } ;
81
+ }
78
82
[ qty_per_pkg , qty ] = self . _qty_by_pkg ( pkg . qty , qty ) ;
79
83
if ( qty_per_pkg ) res [ pkg . id ] = qty_per_pkg ;
80
84
if ( ! qty ) return ;
@@ -108,36 +112,33 @@ export var PackagingQtyPickerMixin = {
108
112
compute_qty : function ( newVal , oldVal ) {
109
113
this . value = this . _compute_qty ( ) ;
110
114
} ,
111
- } ,
112
- watch : {
113
- value : {
114
- handler : function ( newVal , oldVal ) {
115
- this . $root . trigger ( "qty_edit" , this . value ) ;
116
- } ,
115
+ _init_editable ( ) {
116
+ const self = this ;
117
+ this . $watch (
118
+ "qty_by_pkg" ,
119
+ function ( ) {
120
+ self . compute_qty ( ) ;
121
+ } ,
122
+ { deep : true }
123
+ ) ;
124
+ this . qty_by_pkg = this . product_qty_by_packaging ( ) ;
125
+ this . orig_qty_by_pkg = this . qty_by_pkg ;
126
+ // hooking via `v-on:change` we don't get the full event but only the qty :/
127
+ // And forget about using v-text-field because it loses the full event object
128
+ $ ( ".pkg-value" , this . $el ) . change ( this . on_change_pkg_qty ) ;
129
+ $ ( ".pkg-value" , this . $el ) . on ( "focus click" , function ( ) {
130
+ $ ( this ) . select ( ) ;
131
+ } ) ;
132
+ } ,
133
+ _init_readonly ( ) {
134
+ this . qty_by_pkg = this . product_qty_by_packaging ( ) ;
135
+ this . compute_qty ( ) ;
117
136
} ,
118
137
} ,
119
138
created : function ( ) {
120
139
this . original_value = parseInt ( this . opts . init_value , 10 ) ;
121
140
this . value = parseInt ( this . opts . init_value , 10 ) ;
122
141
} ,
123
- mounted : function ( ) {
124
- const self = this ;
125
- this . $watch (
126
- "qty_by_pkg" ,
127
- function ( ) {
128
- self . compute_qty ( ) ;
129
- } ,
130
- { deep : true }
131
- ) ;
132
- this . qty_by_pkg = this . product_qty_by_packaging ( ) ;
133
- this . orig_qty_by_pkg = this . qty_by_pkg ;
134
- // hooking via `v-on:change` we don't get the full event but only the qty :/
135
- // And forget about using v-text-field because it loses the full event object
136
- $ ( ".pkg-value" , this . $el ) . change ( this . on_change_pkg_qty ) ;
137
- $ ( ".pkg-value" , this . $el ) . on ( "focus click" , function ( ) {
138
- $ ( this ) . select ( ) ;
139
- } ) ;
140
- } ,
141
142
computed : {
142
143
opts ( ) {
143
144
const opts = _ . defaults ( { } , this . $props . options , {
@@ -180,6 +181,16 @@ export var PackagingQtyPickerMixin = {
180
181
181
182
export var PackagingQtyPicker = Vue . component ( "packaging-qty-picker" , {
182
183
mixins : [ PackagingQtyPickerMixin ] ,
184
+ watch : {
185
+ value : {
186
+ handler : function ( newVal , oldVal ) {
187
+ this . $root . trigger ( "qty_edit" , this . value ) ;
188
+ } ,
189
+ } ,
190
+ } ,
191
+ mounted : function ( ) {
192
+ this . _init_editable ( ) ;
193
+ } ,
183
194
template : `
184
195
<div :class="[$options._componentTag, opts.mode ? 'mode-' + opts.mode: '']">
185
196
<v-row class="unit-value">
@@ -211,12 +222,17 @@ export var PackagingQtyPicker = Vue.component("packaging-qty-picker", {
211
222
212
223
export var PackagingQtyPickerDisplay = Vue . component ( "packaging-qty-picker-display" , {
213
224
mixins : [ PackagingQtyPickerMixin ] ,
225
+ mounted : function ( ) {
226
+ this . _init_readonly ( ) ;
227
+ } ,
214
228
template : `
215
229
<div :class="[$options._componentTag, opts.mode ? 'mode-' + opts.mode: '', 'd-inline']">
216
230
<span class="packaging" v-for="(pkg, index) in sorted_packaging">
217
231
<span class="pkg-qty" v-text="qty_by_pkg[pkg.id] || 0" />
218
232
<span class="pkg-name" v-text="pkg.name" /><span class="sep" v-if="index != Object.keys(sorted_packaging).length - 1">, </span>
219
233
</span>
234
+ <!-- TOOO: use product uom -->
235
+ <span class="min-unit">({{ opts.init_value }} Units)</span>
220
236
</div>
221
237
` ,
222
238
} ) ;
0 commit comments