2017-04-22 15:47:55 +02:00
$ ( '#save-consume-button' ) . on ( 'click' , function ( e )
2017-04-15 23:16:20 +02:00
{
e . preventDefault ( ) ;
2017-04-22 15:47:55 +02:00
var jsonForm = $ ( '#consume-form' ) . serializeJSON ( ) ;
2017-04-15 23:16:20 +02:00
var spoiled = 0 ;
if ( $ ( '#spoiled' ) . is ( ':checked' ) )
{
spoiled = 1 ;
}
2018-04-18 19:03:39 +02:00
Grocy . Api . Get ( 'stock/get-product-details/' + jsonForm . product _id ,
2017-04-17 16:51:49 +02:00
function ( productDetails )
2017-04-15 23:16:20 +02:00
{
2018-04-18 19:03:39 +02:00
Grocy . Api . Get ( 'stock/consume-product/' + jsonForm . product _id + '/' + jsonForm . amount + '?spoiled=' + spoiled ,
2017-04-17 16:51:49 +02:00
function ( result )
{
2018-10-20 14:09:19 +02:00
toastr . success ( L ( 'Removed #1 #2 of #3 from stock' , jsonForm . amount , Pluralize ( jsonForm . amount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural ) , productDetails . product . name ) ) ;
2017-04-17 16:51:49 +02:00
$ ( '#amount' ) . val ( 1 ) ;
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . SetValue ( '' ) ;
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
2017-04-17 16:51:49 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
2017-04-15 23:16:20 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ) ;
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . GetPicker ( ) . on ( 'change' , function ( e )
2017-04-15 23:16:20 +02:00
{
var productId = $ ( e . target ) . val ( ) ;
2017-04-16 23:11:03 +02:00
if ( productId )
{
2018-04-14 11:10:38 +02:00
Grocy . Components . ProductCard . Refresh ( productId ) ;
2018-04-18 19:03:39 +02:00
Grocy . Api . Get ( 'stock/get-product-details/' + productId ,
2017-04-20 17:10:21 +02:00
function ( productDetails )
2017-04-16 23:11:03 +02:00
{
2017-04-20 17:10:21 +02:00
$ ( '#amount' ) . attr ( 'max' , productDetails . stock _amount ) ;
2017-04-21 19:02:00 +02:00
$ ( '#amount_qu_unit' ) . text ( productDetails . quantity _unit _stock . name ) ;
2017-04-16 23:11:03 +02:00
2017-04-20 17:10:21 +02:00
if ( ( productDetails . stock _amount || 0 ) === 0 )
2017-04-17 16:51:49 +02:00
{
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . SetValue ( '' ) ;
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . ShowCustomError ( L ( 'This product is not in stock' ) ) ;
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
2017-04-17 16:51:49 +02:00
}
else
{
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . HideCustomError ( ) ;
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
2017-11-05 11:29:17 +01:00
$ ( '#amount' ) . focus ( ) ;
2017-04-17 16:51:49 +02:00
}
2017-04-16 23:11:03 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2017-04-15 23:16:20 +02:00
} ) ;
2018-04-16 19:11:32 +02:00
$ ( '#amount' ) . val ( 1 ) ;
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
2018-07-12 19:12:31 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
2017-04-16 23:11:03 +02:00
2018-04-16 19:11:32 +02:00
$ ( '#amount' ) . on ( 'focus' , function ( e )
{
2018-07-12 19:12:31 +02:00
$ ( this ) . select ( ) ;
2018-04-16 19:11:32 +02:00
} ) ;
2017-04-20 17:10:21 +02:00
2018-07-11 19:43:05 +02:00
$ ( '#consume-form input' ) . keyup ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
} ) ;
2018-04-16 19:11:32 +02:00
$ ( '#consume-form input' ) . keydown ( function ( event )
{
if ( event . keyCode === 13 ) //Enter
2017-04-17 16:51:49 +02:00
{
2018-09-29 13:41:56 +02:00
event . preventDefault ( ) ;
2018-07-11 19:43:05 +02:00
if ( document . getElementById ( 'consume-form' ) . checkValidity ( ) === false ) //There is at least one validation error
2017-04-17 16:51:49 +02:00
{
2018-04-16 19:11:32 +02:00
return false ;
2017-04-17 16:51:49 +02:00
}
2018-07-11 19:43:05 +02:00
else
{
$ ( '#save-consume-button' ) . click ( ) ;
}
2018-04-16 19:11:32 +02:00
}
2017-04-15 23:16:20 +02:00
} ) ;