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 ( ) ;
2022-03-26 10:34:00 +01:00
if ( ! Grocy . FrontendHelpers . ValidateForm ( "consume-form" , true ) )
{
return ;
}
2020-12-31 13:45:14 +01:00
if ( $ ( ".combobox-menu-visible" ) . length )
{
return ;
}
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' ) ;
2020-12-07 19:48:33 +01:00
jsonData . allow _subproduct _substitution = true ;
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
{
2023-02-06 20:22:10 +01:00
var successMessage = _ _t ( 'Removed %1$s of %2$s from stock' , Math . abs ( jsonForm . amount - ( productDetails . product . tare _weight + productDetails . stock _amount ) ) + " " + _ _n ( jsonForm . amount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural , true ) , productDetails . product . name ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse [ 0 ] . transaction _id + '\')"><i class="fa-solid fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ;
2019-09-19 21:10:36 +02:00
}
else
{
2022-04-04 20:10:29 +02: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 , true ) , productDetails . product . name ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse [ 0 ] . transaction _id + '\')"><i class="fa-solid 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
{
2025-01-10 17:15:09 +01:00
window . top . postMessage ( WindowMessageBag ( "BroadcastMessage" , WindowMessageBag ( "ProductChanged" , jsonForm . product _id ) ) , Grocy . BaseUrl ) ;
2019-09-20 13:37:53 +02:00
window . parent . postMessage ( WindowMessageBag ( "ShowSuccessMessage" , successMessage ) , Grocy . BaseUrl ) ;
2025-01-10 17:15:09 +01:00
window . parent . postMessage ( WindowMessageBag ( "CloseLastModal" ) , Grocy . BaseUrl ) ;
2019-09-20 13:37:53 +02:00
}
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 ( ) ;
2020-12-23 19:56:37 +01:00
$ ( "#display_amount" ) . attr ( "min" , Grocy . DefaultMinAmount ) ;
2020-11-17 19:11:02 +01:00
$ ( "#display_amount" ) . removeAttr ( "max" ) ;
2020-12-04 18:16:58 +01:00
if ( BoolVal ( Grocy . UserSettings . stock _default _consume _amount _use _quick _consume _amount ) )
{
2022-12-26 11:11:55 +01:00
$ ( '#display_amount' ) . val ( productDetails . product . quick _consume _amount * $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) ;
2020-12-04 18:16:58 +01:00
}
else
{
2023-02-06 20:22:10 +01:00
$ ( '#display_amount' ) . val ( Grocy . UserSettings . stock _default _consume _amount ) ;
2020-12-04 18:16:58 +01:00
}
2020-11-15 14:15:09 +01:00
RefreshLocaleNumberInput ( ) ;
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 )
{
2021-07-11 10:21:36 +02:00
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response ) ;
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 ( ) ;
2022-03-26 10:34:00 +01:00
if ( ! Grocy . FrontendHelpers . ValidateForm ( "consume-form" , true ) )
{
return ;
}
2020-12-31 13:45:14 +01:00
if ( $ ( ".combobox-menu-visible" ) . length )
{
return ;
}
2018-11-17 19:39:37 +01:00
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 ;
2020-12-07 19:48:33 +01:00
jsonData . allow _subproduct _substitution = true ;
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" ) ;
2023-02-06 20:22:10 +01:00
toastr . success ( _ _t ( 'Marked %1$s of %2$s as opened' , Number . parseFloat ( jsonForm . amount ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) + " " + _ _n ( jsonForm . amount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural , true ) , productDetails . product . name ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + result [ 0 ] . transaction _id + '\')"><i class="fa-solid fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ) ;
2018-11-17 19:39:37 +01:00
2022-08-27 11:27:49 +02:00
if ( productDetails . product . move _on _open == 1 && productDetails . default _consume _location != null )
2022-04-18 17:25:08 +01:00
{
toastr . info ( '<span>' + _ _t ( "Moved to %1$s" , productDetails . default _consume _location . name ) + "</span> <i class='fa-solid fa-exchange-alt'></i>" ) ;
}
2020-12-04 18:16:58 +01:00
if ( BoolVal ( Grocy . UserSettings . stock _default _consume _amount _use _quick _consume _amount ) )
{
2022-12-26 11:11:55 +01:00
$ ( '#display_amount' ) . val ( productDetails . product . quick _consume _amount * $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) ;
2020-12-04 18:16:58 +01:00
}
else
{
2023-02-06 20:22:10 +01:00
$ ( '#display_amount' ) . val ( Grocy . UserSettings . stock _default _consume _amount ) ;
2020-12-04 18:16:58 +01:00
}
2020-11-15 14:15:09 +01:00
RefreshLocaleNumberInput ( ) ;
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 ( ) ;
$ ( "#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 )
{
2021-11-14 15:26:38 +01:00
OnLocationChange ( locationId , GetUriParam ( 'stockId' ) ) ;
2019-12-19 12:48:36 -06:00
}
2021-06-12 17:21:12 +02:00
else
{
2023-05-19 18:08:26 +02:00
// try to get stock id from Grocycode
2021-06-12 17:21:12 +02:00
if ( $ ( "#product_id" ) . data ( "grocycode" ) )
{
var gc = $ ( "#product_id" ) . attr ( "barcode" ) . split ( ":" ) ;
if ( gc . length == 4 )
{
2021-11-14 15:26:38 +01:00
Grocy . Api . Get ( "stock/products/" + Grocy . Components . ProductPicker . GetValue ( ) + '/entries?query[]=stock_id=' + gc [ 3 ] ,
function ( stockEntries )
{
OnLocationChange ( stockEntries [ 0 ] . location _id , gc [ 3 ] ) ;
2022-02-07 20:48:17 +01:00
$ ( '#display_amount' ) . val ( stockEntries [ 0 ] . amount ) ;
2021-11-14 15:26:38 +01:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
2021-06-12 17:21:12 +02:00
}
2024-08-24 18:20:40 +02:00
else
{
OnLocationChange ( locationId , null ) ;
}
2021-06-12 17:21:12 +02:00
}
2021-11-14 15:26:38 +01:00
else
{
OnLocationChange ( locationId , null ) ;
}
2021-06-12 17:21:12 +02:00
}
2021-11-14 15:26:38 +01:00
} ) ;
function OnLocationChange ( locationId , stockId )
{
sumValue = 0 ;
2019-12-19 12:48:36 -06:00
if ( locationId )
2020-04-12 21:41:04 +02:00
{
2021-11-14 15:26:38 +01:00
if ( $ ( "#location_id" ) . val ( ) != locationId )
{
$ ( "#location_id" ) . val ( locationId ) ;
}
2020-12-07 19:48:33 +01:00
Grocy . Api . Get ( "stock/products/" + Grocy . Components . ProductPicker . GetValue ( ) + '/entries?include_sub_products=true' ,
2020-04-12 21:41:04 +02:00
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 )
{
2025-01-14 17:54:06 +01:00
openTxt = _ _n ( stockEntry . amount , "Opened" , "Opened" ) ;
2020-04-12 21:41:04 +02:00
}
if ( stockEntry . location _id == locationId )
2019-12-19 12:48:36 -06:00
{
2025-01-13 17:41:08 +01:00
var noteTxt = "" ;
if ( stockEntry . note )
2021-07-02 16:59:37 +02:00
{
2025-01-13 17:41:08 +01:00
noteTxt = " " + stockEntry . note ;
2021-07-02 16:59:37 +02:00
}
2020-08-29 16:41:27 +02:00
2025-01-13 17:41:08 +01:00
$ ( "#specific_stock_entry" ) . append ( $ ( "<option>" , {
"value" : stockEntry . stock _id ,
"text" : _ _t ( "Amount: %1$s; Due 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 + noteTxt ,
"data-amount" : stockEntry . amount ,
"data-id" : stockEntry . id
} ) ) ;
2023-02-06 20:22:10 +01:00
sumValue = sumValue + ( stockEntry . amount || 0 ) ;
2020-04-12 21:41:04 +02:00
if ( stockEntry . stock _id == stockId )
2019-12-19 12:48:36 -06:00
{
2021-06-12 17:21:12 +02:00
$ ( "#use_specific_stock_entry" ) . click ( ) ;
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
}
) ;
2021-01-30 13:06:44 +01:00
2024-08-24 18:20:40 +02:00
if ( document . getElementById ( "product_id" ) . getAttribute ( "barcode" ) == "null" || $ ( "#product_id" ) . data ( "grocycode" ) )
2021-01-30 13:06:44 +01:00
{
ScanModeSubmit ( ) ;
}
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
}
) ;
}
2021-11-14 15:26:38 +01: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-17 19:11:02 +01:00
current _productDetails = productDetails ;
2020-11-09 19:25:46 +01:00
Grocy . Components . ProductAmountPicker . Reload ( productDetails . product . id , productDetails . quantity _unit _stock . id ) ;
2022-12-26 11:11:55 +01:00
Grocy . Components . ProductAmountPicker . SetQuantityUnit ( productDetails . default _quantity _unit _consume . id ) ;
2020-12-04 18:16:58 +01:00
if ( BoolVal ( Grocy . UserSettings . stock _default _consume _amount _use _quick _consume _amount ) )
{
2022-12-26 11:11:55 +01:00
$ ( '#display_amount' ) . val ( productDetails . product . quick _consume _amount * $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) ;
2020-12-04 18:16:58 +01:00
}
else
{
2023-02-06 20:22:10 +01:00
$ ( '#display_amount' ) . val ( Grocy . UserSettings . stock _default _consume _amount ) ;
2020-12-04 18:16:58 +01:00
}
2020-11-15 14:15:09 +01:00
RefreshLocaleNumberInput ( ) ;
2020-11-12 22:47:00 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2017-04-16 23:11:03 +02:00
2022-04-03 21:15:05 +02:00
var defaultLocationId = productDetails . location . id ;
2023-02-06 20:22:10 +01:00
if ( productDetails . product . default _consume _location _id )
2022-04-03 21:15:05 +02:00
{
defaultLocationId = productDetails . product . default _consume _location _id ;
}
2019-12-19 12:48:36 -06:00
$ ( "#location_id" ) . find ( "option" ) . remove ( ) . end ( ) . append ( "<option></option>" ) ;
2020-12-07 19:48:33 +01:00
Grocy . Api . Get ( "stock/products/" + productId + '/locations?include_sub_products=true' ,
2019-12-19 12:48:36 -06:00
function ( stockLocations )
{
var setDefault = 0 ;
2022-04-03 21:15:05 +02:00
var stockAmountAtDefaultLocation = 0 ;
2023-09-02 10:17:43 +02:00
var addedLocations = [ ] ;
2019-12-19 12:48:36 -06:00
stockLocations . forEach ( stockLocation =>
{
2022-04-03 21:15:05 +02:00
if ( stockLocation . location _id == defaultLocationId )
2020-01-26 15:35:01 +01:00
{
2023-09-02 10:17:43 +02:00
if ( ! addedLocations . includes ( stockLocation . location _id ) )
{
$ ( "#location_id" ) . append ( $ ( "<option>" , {
value : stockLocation . location _id ,
text : stockLocation . location _name + " (" + _ _t ( "Default location" ) + ")"
} ) ) ;
$ ( "#location_id" ) . val ( defaultLocationId ) ;
$ ( "#location_id" ) . trigger ( 'change' ) ;
setDefault = 1 ;
}
2023-02-06 20:22:10 +01:00
stockAmountAtDefaultLocation += stockLocation . amount ;
2019-12-19 12:48:36 -06:00
}
else
{
2023-09-02 10:17:43 +02:00
if ( ! addedLocations . includes ( stockLocation . location _id ) )
{
$ ( "#location_id" ) . append ( $ ( "<option>" , {
value : stockLocation . location _id ,
text : stockLocation . location _name
} ) ) ;
}
2019-12-19 12:48:36 -06:00
}
2023-09-02 10:17:43 +02:00
addedLocations . push ( stockLocation . location _id ) ;
2019-12-19 12:48:36 -06:00
if ( setDefault == 0 )
{
2022-04-03 21:15:05 +02:00
$ ( "#location_id" ) . val ( defaultLocationId ) ;
2019-12-19 12:48:36 -06:00
$ ( "#location_id" ) . trigger ( 'change' ) ;
}
} ) ;
2020-01-26 15:35:01 +01:00
2022-04-03 21:15:05 +02:00
if ( stockAmountAtDefaultLocation == 0 )
{
$ ( "#location_id option" ) [ 1 ] . selected = true ;
$ ( "#location_id" ) . trigger ( 'change' ) ;
}
2021-01-04 21:59:19 +01:00
if ( document . getElementById ( "product_id" ) . getAttribute ( "barcode" ) != "null" )
2020-01-26 15:35:01 +01:00
{
2021-01-04 21:59:19 +01:00
Grocy . Api . Get ( 'objects/product_barcodes?query[]=barcode=' + document . getElementById ( "product_id" ) . getAttribute ( "barcode" ) ,
function ( barcodeResult )
{
if ( barcodeResult != null )
{
var barcode = barcodeResult [ 0 ] ;
if ( barcode != null )
{
2022-12-29 11:21:33 +01:00
if ( barcode . amount != null )
2021-01-04 21:59:19 +01:00
{
$ ( "#display_amount" ) . val ( barcode . amount ) ;
$ ( "#display_amount" ) . select ( ) ;
}
2022-12-29 11:21:33 +01:00
if ( barcode . qu _id != null )
2021-01-04 21:59:19 +01:00
{
Grocy . Components . ProductAmountPicker . SetQuantityUnit ( barcode . qu _id ) ;
}
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
RefreshLocaleNumberInput ( ) ;
2021-01-30 13:06:44 +01:00
ScanModeSubmit ( false ) ;
2021-01-04 21:59:19 +01:00
}
}
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2019-12-19 12:48:36 -06:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
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 ) ;
2024-09-10 21:55:31 +02:00
$ ( '#display_amount' ) . attr ( 'max' , ( productDetails . stock _amount + productDetails . product . tare _weight ) . toFixed ( Grocy . UserSettings . stock _decimal _places _amounts ) ) ;
2019-03-05 17:51:50 +01:00
$ ( "#tare-weight-handling-info" ) . removeClass ( "d-none" ) ;
}
else
{
2020-12-23 19:56:37 +01:00
$ ( "#display_amount" ) . attr ( "min" , Grocy . DefaultMinAmount ) ;
2019-03-05 17:51:50 +01:00
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
2019-01-26 14:17:02 +01:00
}
2021-07-09 22:16:08 +02:00
Grocy . Components . ProductPicker . HideCustomError ( ) ;
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
2025-01-10 20:26:37 +01:00
setTimeout ( function ( )
{
$ ( '#display_amount' ) . focus ( ) ;
2025-01-11 15:41:04 +01:00
} , 500 ) ;
2018-11-18 13:35:21 +01:00
2020-11-14 22:51:06 +01:00
if ( productDetails . stock _amount == productDetails . stock _amount _opened || productDetails . product . enable _tare _weight _handling == 1 )
2018-11-18 13:35:21 +01:00
{
$ ( "#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
} ) ;
2023-02-06 20:22:10 +01:00
$ ( '#display_amount' ) . val ( Grocy . UserSettings . stock _default _consume _amount ) ;
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 )
{
2022-03-30 18:00:28 +02:00
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
2022-03-30 18:00:28 +02:00
if ( ! Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) )
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 ;
2020-12-07 19:48:33 +01:00
Grocy . Api . Get ( "stock/products/" + Grocy . Components . ProductPicker . GetValue ( ) + '/entries?include_sub_products=true' ,
2019-12-19 12:48:36 -06:00
function ( stockEntries )
{
stockEntries . forEach ( stockEntry =>
{
if ( stockEntry . location _id == $ ( "#location_id" ) . val ( ) || stockEntry . location _id == "" )
{
2023-02-06 20:22:10 +01:00
sumValue = sumValue + stockEntry . amount _aggregated ;
2019-12-19 12:48:36 -06:00
}
} ) ;
2024-09-10 21:55:31 +02:00
$ ( "#display_amount" ) . attr ( "max" , sumValue . toFixed ( Grocy . UserSettings . stock _decimal _places _amounts ) ) ;
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
}
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
else
{
2025-01-13 17:41:08 +01:00
$ ( "#display_amount" ) . attr ( "max" , Number . parseFloat ( $ ( 'option:selected' , this ) . attr ( 'data-amount' ) ) . toFixed ( Grocy . UserSettings . stock _decimal _places _amounts ) ) ;
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 ( "" ) ;
2018-11-17 17:51:35 +01:00
}
Grocy . FrontendHelpers . ValidateForm ( "consume-form" ) ;
} ) ;
2020-11-17 19:11:02 +01:00
$ ( "#qu_id" ) . on ( "change" , function ( )
{
RefreshForm ( ) ;
} ) ;
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' ) ;
}
else
{
$ ( "#location_id" ) . val ( locationId ) ;
$ ( "#location_id" ) . trigger ( 'change' ) ;
$ ( "#use_specific_stock_entry" ) . click ( ) ;
$ ( "#use_specific_stock_entry" ) . trigger ( 'change' ) ;
2022-01-06 13:54:42 +01:00
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( 'change' ) ;
2020-01-21 17:30:09 +01:00
}
}
2020-01-26 15:35:01 +01:00
// Default input field
2025-01-12 17:57:50 +01:00
setTimeout ( function ( )
{
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
} , 500 ) ;
2020-01-26 15:35:01 +01:00
$ ( 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 )
{
$ ( "#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 ;
2021-01-30 13:06:44 +01:00
if ( ! productDetails )
{
return ;
}
2020-10-14 17:48:37 +02:00
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
}
2020-11-16 19:10:29 +01:00
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 ) ;
2024-09-10 21:55:31 +02:00
$ ( '#display_amount' ) . attr ( 'max' , ( sumValue + productDetails . product . tare _weight ) . toFixed ( Grocy . UserSettings . stock _decimal _places _amounts ) ) ;
2020-10-14 17:48:37 +02:00
$ ( "#tare-weight-handling-info" ) . removeClass ( "d-none" ) ;
}
else
{
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
2020-12-23 19:56:37 +01:00
$ ( "#display_amount" ) . attr ( "min" , Grocy . DefaultMinAmount ) ;
2024-09-10 21:55:31 +02:00
$ ( '#display_amount' ) . attr ( 'max' , ( sumValue * $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) . toFixed ( Grocy . UserSettings . stock _decimal _places _amounts ) ) ;
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
}
}
2020-12-29 18:11:45 +01:00
2021-07-11 10:21:36 +02:00
if ( productDetails . has _childs )
{
$ ( "#display_amount" ) . removeAttr ( "max" ) ;
}
2020-12-29 18:11:45 +01:00
Grocy . FrontendHelpers . ValidateForm ( "consume-form" ) ;
2020-10-14 17:48:37 +02:00
}
2021-01-04 21:59:19 +01:00
function ScanModeSubmit ( singleUnit = true )
{
if ( BoolVal ( Grocy . UserSettings . scan _mode _consume _enabled ) )
{
if ( singleUnit )
{
$ ( "#display_amount" ) . val ( 1 ) ;
2021-01-30 13:06:44 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2021-01-04 21:59:19 +01:00
}
2022-03-30 18:00:28 +02:00
if ( Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) )
2021-01-04 21:59:19 +01:00
{
$ ( '#save-consume-button' ) . click ( ) ;
}
else
{
toastr . warning ( _ _t ( "Scan mode is on but not all required fields could be populated automatically" ) ) ;
Grocy . UISound . Error ( ) ;
}
}
}