2020-01-27 19:19:09 +01:00
$ ( '#save-stockentry-button' ) . on ( 'click' , function ( e )
2019-12-19 12:48:36 -06:00
{
e . preventDefault ( ) ;
2020-01-27 19:19:09 +01:00
var jsonForm = $ ( '#stockentry-form' ) . serializeJSON ( ) ;
Grocy . FrontendHelpers . BeginUiBusy ( "stockentry-form" ) ;
2019-12-19 12:48:36 -06:00
if ( ! jsonForm . price . toString ( ) . isEmpty ( ) )
{
price = parseFloat ( jsonForm . price ) . toFixed ( 2 ) ;
}
var jsonData = { } ;
jsonData . amount = jsonForm . amount ;
jsonData . best _before _date = Grocy . Components . DateTimePicker . GetValue ( ) ;
2020-01-23 18:58:05 +01:00
jsonData . purchased _date = Grocy . Components . DateTimePicker2 . GetValue ( ) ;
2020-03-25 19:34:56 +01:00
jsonData . shopping _location _id = Grocy . Components . ShoppingLocationPicker . GetValue ( ) ;
2019-12-19 12:48:36 -06:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _LOCATION _TRACKING )
{
jsonData . location _id = Grocy . Components . LocationPicker . GetValue ( ) ;
}
else
{
jsonData . location _id = 1 ;
}
jsonData . price = price ;
2020-01-21 20:04:33 +01:00
jsonData . open = $ ( "#open" ) . is ( ":checked" ) ;
2020-01-27 19:00:49 +01:00
Grocy . Api . Put ( "stock/entry/" + Grocy . EditObjectId , jsonData ,
2019-12-19 12:48:36 -06:00
function ( result )
{
2020-01-27 19:00:49 +01:00
var successMessage = _ _t ( 'Stock entry successfully updated' ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBookingEntry(\'' + result . id + '\',\'' + Grocy . EditObjectId + '\')"><i class="fas fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ;
2019-12-19 12:48:36 -06:00
2020-01-27 19:19:09 +01:00
window . parent . postMessage ( WindowMessageBag ( "StockEntryChanged" , Grocy . EditObjectId ) , Grocy . BaseUrl ) ;
2019-12-19 12:48:36 -06:00
window . parent . postMessage ( WindowMessageBag ( "ShowSuccessMessage" , successMessage ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "Ready" ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
} ,
function ( xhr )
{
2020-01-27 19:19:09 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "stockentry-form" ) ;
2019-12-19 12:48:36 -06:00
console . error ( xhr ) ;
}
) ;
} ) ;
2020-01-27 19:19:09 +01:00
Grocy . FrontendHelpers . ValidateForm ( 'stockentry-form' ) ;
2019-12-19 12:48:36 -06:00
2020-01-27 19:19:09 +01:00
$ ( '#stockentry-form input' ) . keyup ( function ( event )
2019-12-19 12:48:36 -06:00
{
2020-01-27 19:19:09 +01:00
Grocy . FrontendHelpers . ValidateForm ( 'stockentry-form' ) ;
2019-12-19 12:48:36 -06:00
} ) ;
2020-01-27 19:19:09 +01:00
$ ( '#stockentry-form input' ) . keydown ( function ( event )
2019-12-19 12:48:36 -06:00
{
if ( event . keyCode === 13 ) //Enter
{
event . preventDefault ( ) ;
2020-01-27 19:19:09 +01:00
if ( document . getElementById ( 'stockentry-form' ) . checkValidity ( ) === false ) //There is at least one validation error
2019-12-19 12:48:36 -06:00
{
return false ;
}
else
{
2020-01-27 19:19:09 +01:00
$ ( '#save-stockentry-button' ) . click ( ) ;
2019-12-19 12:48:36 -06:00
}
}
} ) ;
2020-01-23 18:58:05 +01:00
Grocy . Components . DateTimePicker . GetInputElement ( ) . on ( 'change' , function ( e )
2019-12-19 12:48:36 -06:00
{
2020-01-27 19:19:09 +01:00
Grocy . FrontendHelpers . ValidateForm ( 'stockentry-form' ) ;
2020-01-23 18:58:05 +01:00
} ) ;
2019-12-19 12:48:36 -06:00
2020-01-23 18:58:05 +01:00
Grocy . Components . DateTimePicker . GetInputElement ( ) . on ( 'keypress' , function ( e )
{
2020-01-27 19:19:09 +01:00
Grocy . FrontendHelpers . ValidateForm ( 'stockentry-form' ) ;
2020-01-23 18:58:05 +01:00
} ) ;
Grocy . Components . DateTimePicker2 . GetInputElement ( ) . on ( 'change' , function ( e )
{
2020-01-27 19:19:09 +01:00
Grocy . FrontendHelpers . ValidateForm ( 'stockentry-form' ) ;
2020-01-23 18:58:05 +01:00
} ) ;
Grocy . Components . DateTimePicker2 . GetInputElement ( ) . on ( 'keypress' , function ( e )
{
2020-01-27 19:19:09 +01:00
Grocy . FrontendHelpers . ValidateForm ( 'stockentry-form' ) ;
2020-01-23 18:58:05 +01:00
} ) ;
2020-01-21 17:30:09 +01:00
2020-01-27 19:00:49 +01:00
Grocy . Api . Get ( 'stock/products/' + Grocy . EditObjectProductId ,
function ( productDetails )
2020-01-21 17:30:09 +01:00
{
2020-01-27 19:00:49 +01:00
$ ( '#amount_qu_unit' ) . text ( productDetails . quantity _unit _stock . name ) ;
if ( productDetails . product . allow _partial _units _in _stock == 1 )
{
$ ( "#amount" ) . attr ( "min" , "0.01" ) ;
$ ( "#amount" ) . attr ( "step" , "0.01" ) ;
$ ( "#amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %1$s' , 0.01 . toLocaleString ( ) ) ) ;
}
else
{
$ ( "#amount" ) . attr ( "min" , "1" ) ;
$ ( "#amount" ) . attr ( "step" , "1" ) ;
$ ( "#amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %1$s' , '1' ) ) ;
}
if ( productDetails . product . enable _tare _weight _handling == 1 )
{
$ ( "#amount" ) . attr ( "min" , productDetails . product . tare _weight ) ;
$ ( "#amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %1$s' , parseFloat ( productDetails . product . tare _weight ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : 2 } ) ) ) ;
$ ( "#tare-weight-handling-info" ) . removeClass ( "d-none" ) ;
}
else
{
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
}
2020-01-21 17:30:09 +01:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
2020-01-27 19:00:49 +01:00
$ ( "#amount" ) . on ( "focus" , function ( e )
{
$ ( this ) . select ( ) ;
} ) ;
$ ( "#amount" ) . focus ( ) ;
2020-01-27 19:19:09 +01:00
Grocy . FrontendHelpers . ValidateForm ( "stockentry-form" ) ;