2020-10-17 10:52:25 +02:00
Grocy . RecipePosFormInitialLoadDone = false ;
2019-09-16 09:35:20 +02:00
2020-08-30 12:18:16 +02:00
$ ( '#save-recipe-pos-button' ) . on ( 'click' , function ( e )
2018-07-14 18:23:41 +02:00
{
e . preventDefault ( ) ;
2018-08-09 17:24:04 +02:00
var jsonData = $ ( '#recipe-pos-form' ) . serializeJSON ( { checkboxUncheckedValue : "0" } ) ;
2018-07-14 18:23:41 +02:00
jsonData . recipe _id = Grocy . EditObjectParentId ;
2019-09-16 09:35:20 +02:00
delete jsonData . display _amount ;
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . BeginUiBusy ( "recipe-pos-form" ) ;
2018-07-14 18:23:41 +02:00
if ( Grocy . EditMode === 'create' )
{
2019-01-19 14:51:51 +01:00
Grocy . Api . Post ( 'objects/recipes_pos' , jsonData ,
2018-07-14 18:23:41 +02:00
function ( result )
{
2019-12-21 05:36:02 -06:00
window . parent . postMessage ( WindowMessageBag ( "IngredientsChanged" ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
2018-07-14 18:23:41 +02:00
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "recipe-pos-form" ) ;
2018-09-08 09:26:12 +02:00
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
2018-07-14 18:23:41 +02:00
}
) ;
}
else
{
2019-01-19 14:51:51 +01:00
Grocy . Api . Put ( 'objects/recipes_pos/' + Grocy . EditObjectId , jsonData ,
2018-07-14 18:23:41 +02:00
function ( result )
{
2019-12-21 05:36:02 -06:00
window . parent . postMessage ( WindowMessageBag ( "IngredientsChanged" ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
2018-07-14 18:23:41 +02:00
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "recipe-pos-form" ) ;
2018-09-08 09:26:12 +02:00
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
2018-07-14 18:23:41 +02:00
}
) ;
}
} ) ;
Grocy . Components . ProductPicker . GetPicker ( ) . on ( 'change' , function ( e )
{
var productId = $ ( e . target ) . val ( ) ;
if ( productId )
{
Grocy . Components . ProductCard . Refresh ( productId ) ;
2020-08-29 16:41:27 +02:00
2019-01-19 14:51:51 +01:00
Grocy . Api . Get ( 'stock/products/' + productId ,
2019-04-06 15:42:40 +02:00
function ( productDetails )
2018-07-14 18:23:41 +02:00
{
2020-10-17 10:52:25 +02:00
if ( ! Grocy . RecipePosFormInitialLoadDone )
2019-09-16 09:35:20 +02:00
{
Grocy . Components . ProductAmountPicker . Reload ( productDetails . product . id , productDetails . quantity _unit _stock . id , true ) ;
}
else
2018-08-09 17:24:04 +02:00
{
2019-09-16 09:35:20 +02:00
Grocy . Components . ProductAmountPicker . Reload ( productDetails . product . id , productDetails . quantity _unit _stock . id ) ;
2018-08-09 17:24:04 +02:00
}
2019-01-26 14:17:02 +01:00
if ( productDetails . product . allow _partial _units _in _stock == 1 )
{
2020-10-20 13:08:54 -05:00
$ ( "#display_amount" ) . attr ( "min" , "0." + "0" . repeat ( parseInt ( Grocy . UserSettings . stock _decimal _places _amounts ) - 1 ) + "1" ) ;
$ ( "#display_amount" ) . attr ( "step" , "." + "0" . repeat ( parseInt ( Grocy . UserSettings . stock _decimal _places _amounts ) - 1 ) + "1" ) ;
$ ( "#display_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %s' , "0." + "0" . repeat ( parseInt ( Grocy . UserSettings . stock _decimal _places _amounts ) - 1 ) + "1" ) ) ;
2019-01-26 14:17:02 +01:00
}
else
{
2019-09-16 09:35:20 +02:00
$ ( "#display_amount" ) . attr ( "min" , "1" ) ;
$ ( "#display_amount" ) . attr ( "step" , "1" ) ;
$ ( "#display_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %s' , '1' ) ) ;
2019-01-26 14:17:02 +01:00
}
2020-08-24 18:16:32 +02:00
if ( Grocy . Mode == "create" )
{
$ ( "#not_check_stock_fulfillment" ) . prop ( "checked" , productDetails . product . not _check _stock _fulfillment _for _recipes == 1 ) ;
}
2020-02-04 20:04:48 +01:00
2020-10-17 10:52:25 +02:00
if ( ! $ ( "#only_check_single_unit_in_stock" ) . prop ( "checked" ) && Grocy . RecipePosFormInitialLoadDone )
2020-04-12 13:55:26 +02:00
{
Grocy . Components . ProductAmountPicker . SetQuantityUnit ( productDetails . quantity _unit _stock . id ) ;
}
2019-04-06 15:42:40 +02:00
2019-09-16 09:35:20 +02:00
$ ( '#display_amount' ) . focus ( ) ;
2018-07-14 22:49:42 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'recipe-pos-form' ) ;
2020-10-17 10:52:25 +02:00
Grocy . RecipePosFormInitialLoadDone = true ;
2018-07-14 18:23:41 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
} ) ;
Grocy . FrontendHelpers . ValidateForm ( 'recipe-pos-form' ) ;
if ( Grocy . Components . ProductPicker . InProductAddWorkflow ( ) === false )
{
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
}
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( 'change' ) ;
2020-10-17 10:52:25 +02:00
if ( Grocy . EditMode == "create" )
{
Grocy . RecipePosFormInitialLoadDone = true ;
}
2019-09-16 09:35:20 +02:00
$ ( '#display_amount' ) . on ( 'focus' , function ( e )
2018-07-14 18:23:41 +02:00
{
if ( Grocy . Components . ProductPicker . GetValue ( ) . length === 0 )
{
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
}
else
{
$ ( this ) . select ( ) ;
}
} ) ;
2018-08-09 17:24:04 +02:00
$ ( '#recipe-pos-form input' ) . keyup ( function ( event )
2018-07-14 18:23:41 +02:00
{
Grocy . FrontendHelpers . ValidateForm ( 'recipe-pos-form' ) ;
} ) ;
2020-08-24 18:32:50 +02:00
$ ( '#qu_id' ) . change ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'recipe-pos-form' ) ;
} ) ;
2018-08-09 17:24:04 +02:00
$ ( '#recipe-pos-form input' ) . keydown ( function ( event )
2018-07-14 18:23:41 +02:00
{
if ( event . keyCode === 13 ) //Enter
{
2018-09-29 13:41:56 +02:00
event . preventDefault ( ) ;
2019-01-19 00:37:21 -07:00
2018-07-14 18:23:41 +02:00
if ( document . getElementById ( 'recipe-pos-form' ) . checkValidity ( ) === false ) //There is at least one validation error
{
return false ;
}
else
{
$ ( '#save-recipe-pos-button' ) . click ( ) ;
}
}
} ) ;
2018-08-09 17:24:04 +02:00
$ ( "#only_check_single_unit_in_stock" ) . on ( "click" , function ( )
{
if ( this . checked )
{
2020-10-20 13:08:54 -05:00
$ ( "#display_amount" ) . attr ( "min" , "0." + "0" . repeat ( parseInt ( Grocy . UserSettings . stock _decimal _places _amounts ) - 1 ) + "1" ) ;
$ ( "#display_amount" ) . attr ( "step" , "." + "0" . repeat ( parseInt ( Grocy . UserSettings . stock _decimal _places _amounts ) - 1 ) + "1" ) ;
2019-09-16 09:35:20 +02:00
$ ( "#display_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( "This cannot be negative" ) ) ;
2020-02-02 12:33:48 +01:00
Grocy . Components . ProductAmountPicker . AllowAnyQu ( true ) ;
2018-11-02 19:53:01 +01:00
Grocy . FrontendHelpers . ValidateForm ( "recipe-pos-form" ) ;
2018-08-09 17:24:04 +02:00
}
else
{
2019-09-16 09:35:20 +02:00
$ ( "#display_amount" ) . attr ( "min" , "0" ) ;
$ ( "#display_amount" ) . attr ( "step" , "1" ) ;
2018-11-02 19:53:01 +01:00
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( "change" ) ; // Selects the default quantity unit of the selected product
2019-09-16 09:35:20 +02:00
$ ( "#display_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( "This cannot be negative and must be an integral number" ) ) ;
Grocy . Components . ProductAmountPicker . AllowAnyQuEnabled = false ;
2018-11-02 19:53:01 +01:00
Grocy . FrontendHelpers . ValidateForm ( "recipe-pos-form" ) ;
2018-08-09 17:24:04 +02:00
}
} ) ;
2018-11-02 19:53:01 +01:00
2020-02-02 12:33:48 +01:00
if ( $ ( "#only_check_single_unit_in_stock" ) . prop ( "checked" ) )
{
Grocy . Components . ProductAmountPicker . AllowAnyQu ( true ) ;
}