2020-01-21 17:30:09 +01: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 ( ) ;
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . BeginUiBusy ( "consume-form" ) ;
2017-04-15 23:16:20 +02:00
2019-01-19 14:51:51 +01:00
var apiUrl = 'stock/products/' + jsonForm . product _id + '/consume' ;
2017-04-15 23:16:20 +02:00
2019-01-19 14:51:51 +01:00
var jsonData = { } ;
jsonData . amount = jsonForm . amount ;
2020-11-09 19:25:46 +01:00
jsonData . exact _amount = $ ( '#consume-exact-amount' ) . is ( ':checked' ) ;
2019-01-19 14:51:51 +01:00
jsonData . spoiled = $ ( '#spoiled' ) . is ( ':checked' ) ;
2018-11-17 17:51:35 +01:00
if ( $ ( "#use_specific_stock_entry" ) . is ( ":checked" ) )
{
2019-01-19 14:51:51 +01:00
jsonData . stock _entry _id = jsonForm . specific _stock _entry ;
2018-11-17 17:51:35 +01:00
}
2019-12-19 12:48:36 -06:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _LOCATION _TRACKING )
{
jsonData . location _id = $ ( "#location_id" ) . val ( ) ;
}
2019-03-03 18:20:06 +01:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _RECIPES && Grocy . Components . RecipePicker . GetValue ( ) . toString ( ) . length > 0 )
{
jsonData . recipe _id = Grocy . Components . RecipePicker . GetValue ( ) ;
}
2019-09-19 21:10:36 +02:00
var bookingResponse = null ;
2019-01-19 14:51:51 +01:00
Grocy . Api . Get ( 'stock/products/' + jsonForm . product _id ,
2018-11-17 19:39:37 +01:00
function ( productDetails )
2017-04-15 23:16:20 +02:00
{
2019-01-19 14:51:51 +01:00
Grocy . Api . Post ( apiUrl , jsonData ,
2017-04-17 16:51:49 +02:00
function ( result )
{
2020-01-26 15:35:01 +01:00
if ( BoolVal ( Grocy . UserSettings . scan _mode _consume _enabled ) )
{
Grocy . UISound . Success ( ) ;
}
2019-09-19 21:10:36 +02:00
bookingResponse = result ;
2020-11-14 11:05:36 +01:00
if ( GetUriParam ( "flow" ) === "InplaceAddBarcodeToExistingProduct" )
2019-05-03 18:29:09 +02:00
{
2020-08-17 14:47:33 -05:00
var jsonDataBarcode = { } ;
2020-11-14 11:05:36 +01:00
jsonDataBarcode . barcode = GetUriParam ( "barcode" ) ;
2020-08-17 14:47:33 -05:00
jsonDataBarcode . product _id = jsonForm . product _id ;
2019-05-03 18:29:09 +02:00
2020-08-17 14:47:33 -05:00
Grocy . Api . Post ( 'objects/product_barcodes' , jsonDataBarcode ,
2019-05-03 18:29:09 +02:00
function ( result )
{
2020-11-14 11:05:36 +01:00
$ ( "#flow-info-InplaceAddBarcodeToExistingProduct" ) . addClass ( "d-none" ) ;
2019-05-03 18:29:09 +02:00
$ ( '#barcode-lookup-disabled-hint' ) . addClass ( 'd-none' ) ;
2020-11-09 21:30:22 +01:00
$ ( '#barcode-lookup-hint' ) . removeClass ( 'd-none' ) ;
2020-08-30 12:18:16 +02:00
window . history . replaceState ( { } , document . title , U ( "/consume" ) ) ;
2019-05-03 18:29:09 +02:00
} ,
function ( xhr )
{
2020-08-17 14:47:33 -05:00
Grocy . FrontendHelpers . EndUiBusy ( "consume-form" ) ;
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response ) ;
2019-05-03 18:29:09 +02:00
}
) ;
}
2018-11-17 17:51:35 +01:00
$ ( "#specific_stock_entry" ) . find ( "option" ) . remove ( ) . end ( ) . append ( "<option></option>" ) ;
if ( $ ( "#use_specific_stock_entry" ) . is ( ":checked" ) )
{
$ ( "#use_specific_stock_entry" ) . click ( ) ;
}
2020-10-14 17:48:37 +02:00
if ( productDetails . product . enable _tare _weight _handling == 1 && ! jsonData . exact _amount )
2019-09-19 21:10:36 +02:00
{
2020-08-24 19:06:33 +02:00
var successMessage = _ _t ( 'Removed %1$s of %2$s from stock' , Math . abs ( jsonForm . amount - ( parseFloat ( productDetails . product . tare _weight ) + parseFloat ( productDetails . stock _amount ) ) ) + " " + _ _n ( jsonForm . amount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural ) , productDetails . product . name ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse . transaction _id + '\')"><i class="fas fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ;
2019-09-19 21:10:36 +02:00
}
else
{
2019-12-19 12:48:36 -06:00
var successMessage = _ _t ( 'Removed %1$s of %2$s from stock' , Math . abs ( jsonForm . amount ) + " " + _ _n ( jsonForm . amount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural ) , productDetails . product . name ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse . transaction _id + '\')"><i class="fas fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ;
2019-09-19 21:10:36 +02:00
}
2017-04-17 16:51:49 +02:00
2019-09-20 13:37:53 +02:00
if ( GetUriParam ( "embedded" ) !== undefined )
2019-03-03 18:20:06 +01:00
{
2019-09-20 13:37:53 +02:00
window . parent . postMessage ( WindowMessageBag ( "ProductChanged" , jsonForm . product _id ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "ShowSuccessMessage" , successMessage ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
}
else
{
Grocy . FrontendHelpers . EndUiBusy ( "consume-form" ) ;
toastr . success ( successMessage ) ;
2020-11-14 11:05:36 +01:00
Grocy . Components . ProductPicker . FinishFlow ( ) ;
2019-09-20 13:37:53 +02:00
2020-11-09 19:25:46 +01:00
Grocy . Components . ProductAmountPicker . Reset ( ) ;
$ ( "#display_amount" ) . attr ( "min" , "1" ) ;
$ ( "#display_amount" ) . attr ( "max" , "999999" ) ;
$ ( "#display_amount" ) . attr ( "step" , "1" ) ;
$ ( "#display_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %s' , '1' ) ) ;
$ ( '#display_amount' ) . val ( parseFloat ( Grocy . UserSettings . stock _default _consume _amount ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) ) ;
2020-11-12 22:47:00 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2019-09-20 13:37:53 +02:00
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
Grocy . Components . ProductPicker . Clear ( ) ;
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _RECIPES )
{
Grocy . Components . RecipePicker . Clear ( ) ;
}
2019-12-19 12:48:36 -06:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _LOCATION _TRACKING )
{
$ ( "#location_id" ) . find ( "option" ) . remove ( ) . end ( ) . append ( "<option></option>" ) ;
}
2019-09-20 13:37:53 +02:00
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
2020-01-17 11:13:43 -06:00
Grocy . Components . ProductCard . Refresh ( jsonForm . product _id ) ;
2019-09-20 13:37:53 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
2020-11-09 19:25:46 +01:00
$ ( "#consume-exact-amount-group" ) . addClass ( "d-none" ) ;
2019-03-03 18:20:06 +01:00
}
2017-04-17 16:51:49 +02:00
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "consume-form" ) ;
2017-04-17 16:51:49 +02:00
console . error ( xhr ) ;
}
) ;
2017-04-15 23:16:20 +02:00
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "consume-form" ) ;
2017-04-15 23:16:20 +02:00
console . error ( xhr ) ;
}
) ;
} ) ;
2018-11-17 19:39:37 +01:00
$ ( '#save-mark-as-open-button' ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
var jsonForm = $ ( '#consume-form' ) . serializeJSON ( ) ;
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . BeginUiBusy ( "consume-form" ) ;
2018-11-17 19:39:37 +01:00
2019-01-19 14:51:51 +01:00
var apiUrl = 'stock/products/' + jsonForm . product _id + '/open' ;
2020-08-30 12:18:16 +02:00
jsonData = { } ;
2019-01-19 14:51:51 +01:00
jsonData . amount = jsonForm . amount ;
2018-11-17 19:39:37 +01:00
if ( $ ( "#use_specific_stock_entry" ) . is ( ":checked" ) )
{
2019-01-19 14:51:51 +01:00
jsonData . stock _entry _id = jsonForm . specific _stock _entry ;
2018-11-17 19:39:37 +01:00
}
2019-01-19 14:51:51 +01:00
Grocy . Api . Get ( 'stock/products/' + jsonForm . product _id ,
2018-11-17 19:39:37 +01:00
function ( productDetails )
{
2019-01-19 14:51:51 +01:00
Grocy . Api . Post ( apiUrl , jsonData ,
2018-11-17 19:39:37 +01:00
function ( result )
{
$ ( "#specific_stock_entry" ) . find ( "option" ) . remove ( ) . end ( ) . append ( "<option></option>" ) ;
if ( $ ( "#use_specific_stock_entry" ) . is ( ":checked" ) )
{
$ ( "#use_specific_stock_entry" ) . click ( ) ;
}
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "consume-form" ) ;
2019-12-19 12:48:36 -06:00
toastr . success ( _ _t ( 'Marked %1$s of %2$s as opened' , jsonForm . amount + " " + _ _n ( jsonForm . amount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural ) , productDetails . product . name ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + result . transaction _id + '\')"><i class="fas fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ) ;
2018-11-17 19:39:37 +01:00
2020-11-09 19:25:46 +01:00
$ ( '#display_amount' ) . val ( parseFloat ( Grocy . UserSettings . stock _default _consume _amount ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) ) ;
2020-11-12 22:47:00 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2019-03-04 17:43:12 +01:00
Grocy . Components . ProductPicker . Clear ( ) ;
2018-11-17 19:39:37 +01:00
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "consume-form" ) ;
2018-11-17 19:39:37 +01:00
console . error ( xhr ) ;
}
) ;
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "consume-form" ) ;
2018-11-17 19:39:37 +01:00
console . error ( xhr ) ;
}
) ;
} ) ;
2020-10-14 17:48:37 +02:00
var sumValue = 0 ;
2019-12-19 12:48:36 -06:00
$ ( "#location_id" ) . on ( 'change' , function ( e )
{
var locationId = $ ( e . target ) . val ( ) ;
2020-10-14 17:48:37 +02:00
sumValue = 0 ;
2019-12-19 12:48:36 -06:00
var stockId = null ;
$ ( "#specific_stock_entry" ) . find ( "option" ) . remove ( ) . end ( ) . append ( "<option></option>" ) ;
if ( $ ( "#use_specific_stock_entry" ) . is ( ":checked" ) )
{
2020-08-30 12:18:16 +02:00
$ ( "#use_specific_stock_entry" ) . click ( ) ;
2019-12-19 12:48:36 -06:00
}
if ( GetUriParam ( "embedded" ) !== undefined )
{
2020-08-30 12:18:16 +02:00
stockId = GetUriParam ( 'stockId' ) ;
2019-12-19 12:48:36 -06:00
}
if ( locationId )
2020-04-12 21:41:04 +02:00
{
Grocy . Api . Get ( "stock/products/" + Grocy . Components . ProductPicker . GetValue ( ) + '/entries' ,
function ( stockEntries )
{
stockEntries . forEach ( stockEntry =>
2019-12-19 12:48:36 -06:00
{
2020-04-12 21:41:04 +02:00
var openTxt = _ _t ( "Not opened" ) ;
if ( stockEntry . open == 1 )
{
openTxt = _ _t ( "Opened" ) ;
}
if ( stockEntry . location _id == locationId )
2019-12-19 12:48:36 -06:00
{
2020-04-12 21:41:04 +02:00
$ ( "#specific_stock_entry" ) . append ( $ ( "<option>" , {
value : stockEntry . stock _id ,
amount : stockEntry . amount ,
text : _ _t ( "Amount: %1$s; Expires on %2$s; Bought on %3$s" , stockEntry . amount , moment ( stockEntry . best _before _date ) . format ( "YYYY-MM-DD" ) , moment ( stockEntry . purchased _date ) . format ( "YYYY-MM-DD" ) ) + "; " + openTxt
} ) ) ;
2020-08-29 16:41:27 +02:00
2020-04-12 21:41:04 +02:00
sumValue = sumValue + parseFloat ( stockEntry . amount ) ;
if ( stockEntry . stock _id == stockId )
2019-12-19 12:48:36 -06:00
{
2020-04-12 21:41:04 +02:00
$ ( "#specific_stock_entry" ) . val ( stockId ) ;
2019-12-19 12:48:36 -06:00
}
2020-04-12 21:41:04 +02:00
}
} ) ;
2019-12-19 12:48:36 -06:00
2020-04-12 21:41:04 +02:00
Grocy . Api . Get ( 'stock/products/' + Grocy . Components . ProductPicker . GetValue ( ) ,
function ( productDetails )
{
2020-10-14 17:48:37 +02:00
current _productDetails = productDetails ;
RefreshForm ( ) ;
2020-04-12 21:41:04 +02:00
} ,
2020-08-30 12:18:16 +02:00
function ( xhr )
2019-12-19 12:48:36 -06:00
{
console . error ( xhr ) ;
2020-04-12 21:41:04 +02:00
}
) ;
} ,
function ( xhr )
{
2020-08-30 12:18:16 +02:00
console . error ( xhr ) ;
2020-04-12 21:41:04 +02:00
}
) ;
}
2019-12-19 12:48:36 -06:00
} ) ;
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . GetPicker ( ) . on ( 'change' , function ( e )
2017-04-15 23:16:20 +02:00
{
2020-01-26 15:35:01 +01:00
if ( BoolVal ( Grocy . UserSettings . scan _mode _consume _enabled ) )
{
Grocy . UISound . BarcodeScannerBeep ( ) ;
}
2018-11-17 17:51:35 +01:00
$ ( "#specific_stock_entry" ) . find ( "option" ) . remove ( ) . end ( ) . append ( "<option></option>" ) ;
if ( $ ( "#use_specific_stock_entry" ) . is ( ":checked" ) )
{
$ ( "#use_specific_stock_entry" ) . click ( ) ;
}
2019-12-19 12:48:36 -06:00
$ ( "#location_id" ) . val ( "" ) ;
2017-04-15 23:16:20 +02:00
2018-11-17 17:51:35 +01:00
var productId = $ ( e . target ) . val ( ) ;
2019-01-19 00:37:21 -07:00
2017-04-16 23:11:03 +02:00
if ( productId )
{
2018-04-14 11:10:38 +02:00
Grocy . Components . ProductCard . Refresh ( productId ) ;
2019-01-19 14:51:51 +01:00
Grocy . Api . Get ( 'stock/products/' + productId ,
2018-11-18 13:35:21 +01:00
function ( productDetails )
2017-04-16 23:11:03 +02:00
{
2020-11-09 19:25:46 +01:00
Grocy . Components . ProductAmountPicker . Reload ( productDetails . product . id , productDetails . quantity _unit _stock . id ) ;
2020-11-09 22:15:25 +01:00
Grocy . Components . ProductAmountPicker . SetQuantityUnit ( productDetails . quantity _unit _stock . id ) ;
2020-11-10 20:53:16 +01:00
$ ( '#display_amount' ) . val ( parseFloat ( Grocy . UserSettings . stock _default _consume _amount ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) ) ;
2020-11-12 22:47:00 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2017-04-16 23:11:03 +02:00
2019-12-19 12:48:36 -06:00
$ ( "#location_id" ) . find ( "option" ) . remove ( ) . end ( ) . append ( "<option></option>" ) ;
2020-01-26 15:35:01 +01:00
Grocy . Api . Get ( "stock/products/" + productId + '/locations' ,
2019-12-19 12:48:36 -06:00
function ( stockLocations )
{
var setDefault = 0 ;
stockLocations . forEach ( stockLocation =>
{
2020-01-26 15:35:01 +01:00
if ( productDetails . location . id == stockLocation . location _id )
{
2019-12-19 12:48:36 -06:00
$ ( "#location_id" ) . append ( $ ( "<option>" , {
value : stockLocation . location _id ,
text : stockLocation . location _name + " (" + _ _t ( "Default location" ) + ")"
} ) ) ;
$ ( "#location_id" ) . val ( productDetails . location . id ) ;
$ ( "#location_id" ) . trigger ( 'change' ) ;
setDefault = 1 ;
}
else
{
$ ( "#location_id" ) . append ( $ ( "<option>" , {
value : stockLocation . location _id ,
text : stockLocation . location _name
} ) ) ;
}
if ( setDefault == 0 )
{
$ ( "#location_id" ) . val ( stockLocation . location _id ) ;
$ ( "#location_id" ) . trigger ( 'change' ) ;
}
} ) ;
2020-01-26 15:35:01 +01:00
if ( BoolVal ( Grocy . UserSettings . scan _mode _consume _enabled ) )
{
2020-11-09 19:25:46 +01:00
$ ( "#display_amount" ) . val ( 1 ) ;
2020-11-12 22:47:00 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2020-01-26 15:35:01 +01:00
Grocy . FrontendHelpers . ValidateForm ( "consume-form" ) ;
if ( document . getElementById ( "consume-form" ) . checkValidity ( ) === true )
{
$ ( '#save-consume-button' ) . click ( ) ;
}
else
{
toastr . warning ( _ _t ( "Scan mode is on but not all required fields could be populated automatically" ) ) ;
Grocy . UISound . Error ( ) ;
}
}
2019-12-19 12:48:36 -06:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
2019-01-26 14:17:02 +01:00
if ( productDetails . product . allow _partial _units _in _stock == 1 )
{
2020-11-09 19:25:46 +01: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 must be between %1$s and %2$s' , "0." + "0" . repeat ( parseInt ( Grocy . UserSettings . stock _decimal _places _amounts ) - 1 ) + "1" , parseFloat ( productDetails . stock _amount ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) ) ) ;
2019-01-26 14:17:02 +01:00
}
else
{
2020-11-09 19:25:46 +01:00
$ ( "#display_amount" ) . attr ( "min" , "1" ) ;
$ ( "#display_amount" ) . attr ( "step" , "1" ) ;
$ ( "#display_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount must be between %1$s and %2$s' , "1" , parseFloat ( productDetails . stock _amount ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) ) ) ;
2019-03-05 17:51:50 +01:00
}
if ( productDetails . product . enable _tare _weight _handling == 1 )
{
2020-11-09 19:25:46 +01:00
$ ( "#display_amount" ) . attr ( "min" , productDetails . product . tare _weight ) ;
$ ( '#display_amount' ) . attr ( 'max' , parseFloat ( productDetails . stock _amount ) + parseFloat ( productDetails . product . tare _weight ) ) ;
$ ( "#display_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount must be between %1$s and %2$s' , parseFloat ( productDetails . product . tare _weight ) . toLocaleString ( ) , ( parseFloat ( productDetails . stock _amount ) + parseFloat ( productDetails . product . tare _weight ) ) . toLocaleString ( ) ) ) ;
2019-03-05 17:51:50 +01:00
$ ( "#tare-weight-handling-info" ) . removeClass ( "d-none" ) ;
}
else
{
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
2019-01-26 14:17:02 +01:00
}
2019-09-14 17:34:36 +02:00
if ( ( parseFloat ( productDetails . stock _amount ) || 0 ) === 0 )
2017-04-17 16:51:49 +02:00
{
2020-11-09 19:25:46 +01:00
Grocy . Components . ProductAmountPicker . Reset ( ) ;
2019-03-04 17:43:12 +01:00
Grocy . Components . ProductPicker . Clear ( ) ;
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
2019-05-01 20:19:18 +02:00
Grocy . Components . ProductPicker . ShowCustomError ( _ _t ( 'This product is not in stock' ) ) ;
2018-07-14 14:43:57 +02:00
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' ) ;
2020-11-09 19:25:46 +01:00
$ ( '#display_amount' ) . focus ( ) ;
2017-04-17 16:51:49 +02:00
}
2018-11-18 13:35:21 +01:00
if ( productDetails . stock _amount == productDetails . stock _amount _opened )
{
$ ( "#save-mark-as-open-button" ) . addClass ( "disabled" ) ;
}
else
{
$ ( "#save-mark-as-open-button" ) . removeClass ( "disabled" ) ;
}
2017-04-16 23:11:03 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2017-04-15 23:16:20 +02:00
} ) ;
2020-11-09 19:25:46 +01:00
$ ( '#display_amount' ) . val ( parseFloat ( Grocy . UserSettings . stock _default _consume _amount ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) ) ;
2020-11-12 22:47:00 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2018-07-12 19:12:31 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
2017-04-16 23:11:03 +02:00
2020-11-09 19:25:46 +01:00
$ ( '#display_amount' ) . on ( 'focus' , function ( e )
2018-04-16 19:11:32 +02:00
{
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
2020-11-08 15:09:10 +01:00
$ ( '#price' ) . on ( 'focus' , function ( e )
{
$ ( this ) . select ( ) ;
} ) ;
2018-11-17 17:51:35 +01:00
$ ( '#consume-form input' ) . keyup ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
} ) ;
$ ( '#consume-form select' ) . change ( function ( event )
2018-07-11 19:43:05 +02:00
{
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 ( ) ;
2019-01-19 00:37:21 -07:00
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
} ) ;
2018-10-27 17:26:00 +02:00
2019-12-19 12:48:36 -06:00
$ ( "#specific_stock_entry" ) . on ( "change" , function ( e )
{
if ( $ ( e . target ) . val ( ) == "" )
{
2020-10-14 17:48:37 +02:00
sumValue = 0 ;
2019-12-19 12:48:36 -06:00
Grocy . Api . Get ( "stock/products/" + Grocy . Components . ProductPicker . GetValue ( ) + '/entries' ,
function ( stockEntries )
{
stockEntries . forEach ( stockEntry =>
{
if ( stockEntry . location _id == $ ( "#location_id" ) . val ( ) || stockEntry . location _id == "" )
{
sumValue = sumValue + parseFloat ( stockEntry . amount ) ;
}
} ) ;
2020-11-09 19:25:46 +01:00
$ ( "#display_amount" ) . attr ( "max" , sumValue ) ;
2019-12-19 12:48:36 -06:00
if ( sumValue == 0 )
{
2020-11-09 19:25:46 +01:00
$ ( "#display_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'There are no units available at this location' ) ) ;
2019-12-19 12:48:36 -06:00
}
else
{
2020-11-09 19:25:46 +01:00
$ ( "#display_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount must be between %1$s and %2$s' , "1" , sumValue ) ) ;
2019-12-19 12:48:36 -06:00
}
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
else
{
2020-11-09 19:25:46 +01:00
$ ( "#display_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount must be between %1$s and %2$s' , "1" , $ ( 'option:selected' , this ) . attr ( 'amount' ) ) ) ;
$ ( "#display_amount" ) . attr ( "max" , $ ( 'option:selected' , this ) . attr ( 'amount' ) ) ;
2019-12-19 12:48:36 -06:00
}
} ) ;
2018-11-17 17:51:35 +01:00
$ ( "#use_specific_stock_entry" ) . on ( "change" , function ( )
{
var value = $ ( this ) . is ( ":checked" ) ;
2019-12-19 12:48:36 -06:00
2018-11-17 17:51:35 +01:00
if ( value )
{
$ ( "#specific_stock_entry" ) . removeAttr ( "disabled" ) ;
$ ( "#specific_stock_entry" ) . attr ( "required" , "" ) ;
}
else
{
$ ( "#specific_stock_entry" ) . attr ( "disabled" , "" ) ;
$ ( "#specific_stock_entry" ) . removeAttr ( "required" ) ;
2019-12-19 12:48:36 -06:00
$ ( "#specific_stock_entry" ) . val ( "" ) ;
$ ( "#location_id" ) . trigger ( 'change' ) ;
2018-11-17 17:51:35 +01:00
}
Grocy . FrontendHelpers . ValidateForm ( "consume-form" ) ;
} ) ;
2018-10-27 17:26:00 +02:00
function UndoStockBooking ( bookingId )
{
2020-08-30 12:18:16 +02:00
Grocy . Api . Post ( 'stock/bookings/' + bookingId . toString ( ) + '/undo' , { } ,
2018-10-27 17:26:00 +02:00
function ( result )
{
2019-05-01 20:19:18 +02:00
toastr . success ( _ _t ( "Booking successfully undone" ) ) ;
2018-10-27 17:26:00 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ;
2019-12-19 12:48:36 -06:00
function UndoStockTransaction ( transactionId )
{
2020-08-30 12:18:16 +02:00
Grocy . Api . Post ( 'stock/transactions/' + transactionId . toString ( ) + '/undo' , { } ,
function ( result )
2019-12-19 12:48:36 -06:00
{
toastr . success ( _ _t ( "Transaction successfully undone" ) ) ;
} ,
2020-08-30 12:18:16 +02:00
function ( xhr )
2019-12-19 12:48:36 -06:00
{
console . error ( xhr ) ;
}
) ;
} ;
2020-01-21 17:30:09 +01:00
if ( GetUriParam ( "embedded" ) !== undefined )
{
var locationId = GetUriParam ( 'locationId' ) ;
if ( typeof locationId === 'undefined' )
{
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( 'change' ) ;
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
}
else
{
$ ( "#location_id" ) . val ( locationId ) ;
$ ( "#location_id" ) . trigger ( 'change' ) ;
$ ( "#use_specific_stock_entry" ) . click ( ) ;
$ ( "#use_specific_stock_entry" ) . trigger ( 'change' ) ;
}
}
2020-01-26 15:35:01 +01:00
// Default input field
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
$ ( document ) . on ( "change" , "#scan-mode" , function ( e )
{
if ( $ ( this ) . prop ( "checked" ) )
{
Grocy . UISound . AskForPermission ( ) ;
}
} ) ;
2020-02-03 21:21:42 +01:00
$ ( "#scan-mode-button" ) . on ( "click" , function ( e )
{
document . activeElement . blur ( ) ;
$ ( "#scan-mode" ) . click ( ) ;
$ ( "#scan-mode-button" ) . toggleClass ( "btn-success" ) . toggleClass ( "btn-danger" ) ;
if ( $ ( "#scan-mode" ) . prop ( "checked" ) )
{
$ ( "#scan-mode-status" ) . text ( _ _t ( "on" ) ) ;
}
else
{
$ ( "#scan-mode-status" ) . text ( _ _t ( "off" ) ) ;
}
} ) ;
2020-10-14 17:48:37 +02:00
$ ( '#consume-exact-amount' ) . on ( 'change' , RefreshForm ) ;
var current _productDetails ;
function RefreshForm ( )
{
var productDetails = current _productDetails ;
if ( productDetails . product . enable _tare _weight _handling == 1 )
{
2020-11-09 19:25:46 +01:00
$ ( "#consume-exact-amount-group" ) . removeClass ( "d-none" ) ;
2020-10-14 17:48:37 +02:00
}
else
{
2020-11-09 19:25:46 +01:00
$ ( "#consume-exact-amount-group" ) . addClass ( "d-none" ) ;
2020-10-14 17:48:37 +02:00
}
if ( productDetails . product . enable _tare _weight _handling == 1 && ! $ ( '#consume-exact-amount' ) . is ( ':checked' ) )
{
2020-11-09 19:25:46 +01:00
$ ( "#display_amount" ) . attr ( "min" , productDetails . product . tare _weight ) ;
$ ( '#display_amount' ) . attr ( 'max' , sumValue + parseFloat ( productDetails . product . tare _weight ) ) ;
$ ( "#display_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount must be between %1$s and %2$s' , parseFloat ( productDetails . product . tare _weight ) . toLocaleString ( ) , ( parseFloat ( productDetails . stock _amount ) + parseFloat ( productDetails . product . tare _weight ) ) . toLocaleString ( ) ) ) ;
2020-10-14 17:48:37 +02:00
$ ( "#tare-weight-handling-info" ) . removeClass ( "d-none" ) ;
}
else
{
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
if ( productDetails . product . allow _partial _units _in _stock == 1 )
{
2020-11-09 19:25:46 +01: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 must be between %1$s and %2$s' , "0." + "0" . repeat ( parseInt ( Grocy . UserSettings . stock _decimal _places _amounts ) - 1 ) + "1" , parseFloat ( productDetails . stock _amount ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) ) ) ;
2020-10-14 17:48:37 +02:00
}
else
{
2020-11-09 19:25:46 +01:00
$ ( "#display_amount" ) . attr ( "min" , "1" ) ;
$ ( "#display_amount" ) . attr ( "step" , "1" ) ;
$ ( "#display_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount must be between %1$s and %2$s' , "1" , parseFloat ( productDetails . stock _amount ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) ) ) ;
2020-10-14 17:48:37 +02:00
}
2020-11-09 19:25:46 +01:00
$ ( '#display_amount' ) . attr ( 'max' , sumValue ) ;
2020-10-14 17:48:37 +02:00
if ( sumValue == 0 )
{
2020-11-09 19:25:46 +01:00
$ ( "#display_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'There are no units available at this location' ) ) ;
2020-10-14 17:48:37 +02:00
}
}
}