@@ -13,6 +13,7 @@ class ArticleForm {
1313 this . supplierUnitSelect$ = $ ( `#${ this . unitFieldsIdPrefix } _supplier_order_unit` , this . articleForm$ ) ;
1414 this . unitRatiosTable$ = $ ( '#fc_base_price' , this . articleForm$ ) ;
1515 this . minimumOrderQuantity$ = $ ( `#${ this . unitFieldsIdPrefix } _minimum_order_quantity` , this . articleForm$ ) ;
16+ this . maximumOrderQuantity$ = $ ( `#${ this . unitFieldsIdPrefix } _maximum_order_quantity` , this . articleForm$ ) ;
1617 this . billingUnit$ = $ ( `#${ this . unitFieldsIdPrefix } _billing_unit` , this . articleForm$ ) ;
1718 this . groupOrderGranularity$ = $ ( `#${ this . unitFieldsIdPrefix } _group_order_granularity` , this . articleForm$ ) ;
1819 this . groupOrderUnit$ = $ ( `#${ this . unitFieldsIdPrefix } _group_order_unit` , this . articleForm$ ) ;
@@ -60,7 +61,7 @@ class ArticleForm {
6061 const tax = parseFloat ( this . tax$ . val ( ) ) ;
6162 const deposit = parseFloat ( this . deposit$ . val ( ) ) ;
6263 const grossPrice = ( price + deposit ) * ( tax / 100 + 1 ) ;
63- const fcPrice = grossPrice * ( this . priceMarkup / 100 + 1 ) ;
64+ const fcPrice = grossPrice * ( this . priceMarkup / 100 + 1 ) ;
6465 const priceUnitLabel = this . getUnitLabel ( this . priceUnit$ . val ( ) ) ;
6566 this . fcPrice$ . find ( '.price_value' ) . text ( isNaN ( fcPrice ) ? '?' : I18n . l ( 'currency' , fcPrice ) ) ;
6667 this . fcPrice$ . find ( '.price_per_text' ) . toggle ( priceUnitLabel . trim ( ) !== '' ) ;
@@ -88,7 +89,7 @@ class ArticleForm {
8889 this . loadRatios ( ) ;
8990 this . undoPriceConversion ( ) ;
9091 this . undoOrderAndReceivedUnitsConversion ( ) ;
91- } catch ( err ) {
92+ } catch ( err ) {
9293 e . preventDefault ( ) ;
9394 throw err ;
9495 }
@@ -191,6 +192,7 @@ class ArticleForm {
191192 initializeRegularFormFields ( ) {
192193 this . unit$ . change ( ( ) => {
193194 this . setMinimumOrderUnitDisplay ( ) ;
195+ this . setMaximumOrderUnitDisplay ( ) ;
194196 this . updateAvailableBillingAndGroupOrderUnits ( ) ;
195197 this . updateUnitMultiplierLabels ( ) ;
196198 this . updateCustomUnitWarning ( ) ;
@@ -204,6 +206,28 @@ class ArticleForm {
204206 this . updateCustomUnitWarning ( ) ;
205207 } ) ;
206208 this . onSupplierUnitChanged ( ) ;
209+
210+ // Add validation for minimum/maximum order quantity
211+ this . initializeOrderQuantityValidation ( ) ;
212+ }
213+
214+ initializeOrderQuantityValidation ( ) {
215+ const validateOrderQuantities = ( ) => {
216+ const minValue = parseFloat ( this . minimumOrderQuantity$ . val ( ) ) || 0 ;
217+ const maxValue = parseFloat ( this . maximumOrderQuantity$ . val ( ) ) || Infinity ;
218+
219+ if ( this . minimumOrderQuantity$ . val ( ) && this . maximumOrderQuantity$ . val ( ) && minValue > maxValue ) {
220+ const errorMessage = I18n . t ( 'activerecord.errors.models.article_version.attributes.minimum_order_quantity.greater_than_maximum' ) ;
221+ this . minimumOrderQuantity$ [ 0 ] . setCustomValidity ( errorMessage ) ;
222+ this . maximumOrderQuantity$ [ 0 ] . setCustomValidity ( errorMessage ) ;
223+ } else {
224+ this . minimumOrderQuantity$ [ 0 ] . setCustomValidity ( '' ) ;
225+ this . maximumOrderQuantity$ [ 0 ] . setCustomValidity ( '' ) ;
226+ }
227+ } ;
228+
229+ this . minimumOrderQuantity$ . on ( 'input change' , validateOrderQuantities ) ;
230+ this . maximumOrderQuantity$ . on ( 'input change' , validateOrderQuantities ) ;
207231 }
208232
209233 updateCustomUnitWarning ( ) {
@@ -227,6 +251,7 @@ class ArticleForm {
227251 this . unit$ . toggle ( ! valueChosen ) ;
228252 this . filterAvailableRatioUnits ( ) ;
229253 this . setMinimumOrderUnitDisplay ( ) ;
254+ this . setMaximumOrderUnitDisplay ( ) ;
230255 this . updateAvailableBillingAndGroupOrderUnits ( ) ;
231256 this . updateUnitMultiplierLabels ( ) ;
232257 }
@@ -245,6 +270,20 @@ class ArticleForm {
245270 this . minimumOrderQuantity$ . attr ( 'step' , converter . isUnitSiConversible ( this . supplierUnitSelect$ . val ( ) ) ? 'any' : 1 ) ;
246271 }
247272
273+ setMaximumOrderUnitDisplay ( ) {
274+ const chosenOptionLabel = this . supplierUnitSelect$ . val ( ) !== ''
275+ ? $ ( `option[value="${ this . supplierUnitSelect$ . val ( ) } "]` , this . supplierUnitSelect$ ) . text ( )
276+ : undefined ;
277+ const unitVal = $ ( `#${ this . unitFieldsIdPrefix } _unit` ) . val ( ) ;
278+ this . maximumOrderQuantity$
279+ . parents ( '.input-group' )
280+ . find ( '.input-group-addon' )
281+ . text ( chosenOptionLabel !== undefined ? chosenOptionLabel : unitVal ) ;
282+
283+ const converter = this . getUnitsConverter ( ) ;
284+ this . maximumOrderQuantity$ . attr ( 'step' , converter . isUnitSiConversible ( this . supplierUnitSelect$ . val ( ) ) ? 'any' : 1 ) ;
285+ }
286+
248287 bindAddRatioButton ( ) {
249288 $ ( '*[data-add-ratio]' , this . articleForm$ ) . on ( 'click' , ( e ) => {
250289 e . preventDefault ( ) ;
0 commit comments