2020-01-21 17:30:09 +01:00
$ ( '#save-stockedit-button' ) . on ( 'click' , function ( e )
2019-12-19 12:48:36 -06:00
{
e . preventDefault ( ) ;
var jsonForm = $ ( '#stockedit-form' ) . serializeJSON ( ) ;
Grocy . FrontendHelpers . BeginUiBusy ( "stockedit-form" ) ;
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 ( ) ;
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" ) ;
2019-12-19 12:48:36 -06:00
var stockRowId = GetUriParam ( 'stockRowId' ) ;
2020-01-22 14:08:49 -06:00
Grocy . Api . Put ( "stock/entry/" + stockRowId , jsonData ,
2019-12-19 12:48:36 -06:00
function ( result )
{
2020-01-17 10:54:34 -06:00
var successMessage = _ _t ( 'Stock entry successfully updated' ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBookingEntry(\'' + result . id + '\',\'' + stockRowId + '\')"><i class="fas fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ;
2019-12-19 12:48:36 -06:00
window . parent . postMessage ( WindowMessageBag ( "StockDetailChanged" , stockRowId ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "ShowSuccessMessage" , successMessage ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "Ready" ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( "stockedit-form" ) ;
console . error ( xhr ) ;
}
) ;
} ) ;
Grocy . FrontendHelpers . ValidateForm ( 'stockedit-form' ) ;
$ ( '#stockedit-form input' ) . keyup ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'stockedit-form' ) ;
} ) ;
$ ( '#stockedit-form input' ) . keydown ( function ( event )
{
if ( event . keyCode === 13 ) //Enter
{
event . preventDefault ( ) ;
if ( document . getElementById ( 'stockedit-form' ) . checkValidity ( ) === false ) //There is at least one validation error
{
return false ;
}
else
{
$ ( '#save-stockedit-button' ) . click ( ) ;
}
}
} ) ;
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-23 18:58:05 +01:00
Grocy . FrontendHelpers . ValidateForm ( 'stockedit-form' ) ;
} ) ;
2019-12-19 12:48:36 -06:00
2020-01-23 18:58:05 +01:00
Grocy . Components . DateTimePicker . GetInputElement ( ) . on ( 'keypress' , function ( e )
{
Grocy . FrontendHelpers . ValidateForm ( 'stockedit-form' ) ;
} ) ;
Grocy . Components . DateTimePicker2 . GetInputElement ( ) . on ( 'change' , function ( e )
{
Grocy . FrontendHelpers . ValidateForm ( 'stockedit-form' ) ;
} ) ;
Grocy . Components . DateTimePicker2 . GetInputElement ( ) . on ( 'keypress' , function ( e )
{
Grocy . FrontendHelpers . ValidateForm ( 'stockedit-form' ) ;
} ) ;
2020-01-21 17:30:09 +01:00
var stockRowId = GetUriParam ( 'stockRowId' ) ;
2020-01-22 14:08:49 -06:00
Grocy . Api . Get ( "stock/entry/" + stockRowId ,
2020-01-21 17:30:09 +01:00
function ( stockEntry )
{
Grocy . Components . LocationPicker . SetId ( stockEntry . location _id ) ;
$ ( '#amount' ) . val ( stockEntry . amount ) ;
$ ( '#price' ) . val ( stockEntry . price ) ;
2020-01-21 20:04:33 +01:00
$ ( "#open" ) . prop ( 'checked' , BoolVal ( stockEntry . open ) ) ;
2020-01-21 17:30:09 +01:00
Grocy . Components . DateTimePicker . SetValue ( stockEntry . best _before _date ) ;
2020-01-23 18:58:05 +01:00
Grocy . Components . DateTimePicker2 . SetValue ( stockEntry . purchased _date ) ;
2020-01-21 17:30:09 +01:00
Grocy . Api . Get ( 'stock/products/' + stockEntry . product _id ,
function ( productDetails )
{
$ ( '#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" ) ;
}
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;