2017-04-22 15:47:55 +02: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
2018-11-17 17:51:35 +01:00
if ( $ ( "#use_specific_stock_entry" ) . is ( ":checked" ) )
{
jsonForm . amount = 1 ;
}
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 ;
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-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 )
{
2019-09-19 21:10:36 +02:00
bookingResponse = result ;
2019-05-03 18:29:09 +02:00
var addBarcode = GetUriParam ( 'addbarcodetoselection' ) ;
if ( addBarcode !== undefined )
{
var existingBarcodes = productDetails . product . barcode || '' ;
if ( existingBarcodes . length === 0 )
{
productDetails . product . barcode = addBarcode ;
}
else
{
productDetails . product . barcode += ',' + addBarcode ;
}
Grocy . Api . Put ( 'objects/products/' + productDetails . product . id , productDetails . product ,
function ( result )
{
$ ( "#flow-info-addbarcodetoselection" ) . addClass ( "d-none" ) ;
$ ( '#barcode-lookup-disabled-hint' ) . addClass ( 'd-none' ) ;
window . history . replaceState ( { } , document . title , U ( "/consume" ) ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
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 ( ) ;
}
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "consume-form" ) ;
2019-09-19 21:10:36 +02:00
if ( productDetails . product . enable _tare _weight _handling == 1 )
{
toastr . success ( _ _t ( 'Removed %1$s of %2$s from stock' , Math . abs ( jsonForm . amount - parseFloat ( productDetails . product . tare _weight ) ) + " " + _ _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="UndoStockBooking(' + bookingResponse . id + ')"><i class="fas fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ) ;
}
else
{
toastr . success ( _ _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="UndoStockBooking(' + bookingResponse . id + ')"><i class="fas fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ) ;
}
2017-04-17 16:51:49 +02:00
2019-03-05 17:51:50 +01:00
$ ( "#amount" ) . attr ( "min" , "1" ) ;
$ ( "#amount" ) . attr ( "max" , "999999" ) ;
$ ( "#amount" ) . attr ( "step" , "1" ) ;
2019-05-01 20:19:18 +02:00
$ ( "#amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %s' , '1' ) ) ;
2019-05-04 13:19:34 +02:00
$ ( '#amount' ) . val ( Grocy . UserSettings . stock _default _consume _amount ) ;
2019-03-09 12:31:45 +01:00
$ ( '#amount_qu_unit' ) . text ( "" ) ;
2019-03-05 17:51:50 +01:00
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
2019-03-04 17:43:12 +01:00
Grocy . Components . ProductPicker . Clear ( ) ;
2019-03-03 18:20:06 +01:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _RECIPES )
{
2019-03-04 17:43:12 +01:00
Grocy . Components . RecipePicker . Clear ( ) ;
2019-03-03 18:20:06 +01:00
}
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
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
if ( $ ( "#use_specific_stock_entry" ) . is ( ":checked" ) )
{
jsonForm . amount = 1 ;
}
2019-01-19 14:51:51 +01:00
var apiUrl = 'stock/products/' + jsonForm . product _id + '/open' ;
jsonData = { } ;
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-05-05 14:13:50 +02: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="UndoStockBooking(' + result . id + ')"><i class="fas fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ) ;
2018-11-17 19:39:37 +01:00
2019-05-04 13:19:34 +02:00
$ ( '#amount' ) . val ( Grocy . UserSettings . stock _default _consume _amount ) ;
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 ) ;
}
) ;
} ) ;
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . GetPicker ( ) . on ( 'change' , function ( e )
2017-04-15 23:16:20 +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 ( ) ;
}
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
{
2017-04-20 17:10:21 +02:00
$ ( '#amount' ) . attr ( 'max' , productDetails . stock _amount ) ;
2017-04-21 19:02:00 +02:00
$ ( '#amount_qu_unit' ) . text ( productDetails . quantity _unit _stock . name ) ;
2017-04-16 23:11:03 +02:00
2019-01-26 14:17:02 +01:00
if ( productDetails . product . allow _partial _units _in _stock == 1 )
{
$ ( "#amount" ) . attr ( "min" , "0.01" ) ;
$ ( "#amount" ) . attr ( "step" , "0.01" ) ;
2019-05-05 14:13:50 +02:00
$ ( "#amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount must be between %1$s and %2$s' , 0.01 . toLocaleString ( ) , parseFloat ( productDetails . stock _amount ) . toLocaleString ( ) ) ) ;
2019-01-26 14:17:02 +01:00
}
else
{
$ ( "#amount" ) . attr ( "min" , "1" ) ;
$ ( "#amount" ) . attr ( "step" , "1" ) ;
2019-05-05 14:13:50 +02:00
$ ( "#amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount must be between %1$s and %2$s' , "1" , parseFloat ( productDetails . stock _amount ) . toLocaleString ( ) ) ) ;
2019-03-05 17:51:50 +01:00
}
if ( productDetails . product . enable _tare _weight _handling == 1 )
{
$ ( "#amount" ) . attr ( "min" , productDetails . product . tare _weight ) ;
$ ( '#amount' ) . attr ( 'max' , parseFloat ( productDetails . stock _amount ) + parseFloat ( productDetails . product . tare _weight ) ) ;
2019-05-05 14:13:50 +02:00
$ ( "#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
{
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' ) ;
2017-11-05 11:29:17 +01:00
$ ( '#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 ) ;
}
) ;
2018-11-17 17:51:35 +01:00
2019-01-19 14:51:51 +01:00
Grocy . Api . Get ( "stock/products/" + productId + '/entries' ,
function ( stockEntries )
2018-11-17 17:51:35 +01:00
{
stockEntries . forEach ( stockEntry =>
{
2019-05-01 20:19:18 +02:00
var openTxt = _ _t ( "Not opened" ) ;
2018-11-17 19:39:37 +01:00
if ( stockEntry . open == 1 )
{
2019-05-01 20:19:18 +02:00
openTxt = _ _t ( "Opened" ) ;
2018-11-17 19:39:37 +01:00
}
2018-11-17 17:51:35 +01:00
for ( i = 0 ; i < stockEntry . amount ; i ++ )
{
2019-09-19 21:10:36 +02:00
// Do this only for the first 50 entries to prevent a very long loop (is more anytime needed)?
if ( i > 50 )
{
break ;
}
2018-11-17 17:51:35 +01:00
$ ( "#specific_stock_entry" ) . append ( $ ( "<option>" , {
value : stockEntry . stock _id ,
2019-05-05 14:13:50 +02:00
text : _ _t ( "Expires on %1$s; Bought on %2$s" , moment ( stockEntry . best _before _date ) . format ( "YYYY-MM-DD" ) , moment ( stockEntry . purchased _date ) . format ( "YYYY-MM-DD" ) ) + "; " + openTxt
2018-11-17 17:51:35 +01:00
} ) ) ;
}
} ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
2017-04-16 23:11:03 +02:00
}
2017-04-15 23:16:20 +02:00
} ) ;
2019-05-04 13:19:34 +02:00
$ ( '#amount' ) . val ( Grocy . UserSettings . stock _default _consume _amount ) ;
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
2018-07-12 19:12:31 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
2017-04-16 23:11:03 +02:00
2018-04-16 19:11:32 +02:00
$ ( '#amount' ) . on ( 'focus' , function ( e )
{
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
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
2018-11-17 17:51:35 +01:00
$ ( "#use_specific_stock_entry" ) . on ( "change" , function ( )
{
var value = $ ( this ) . is ( ":checked" ) ;
if ( value )
{
$ ( "#specific_stock_entry" ) . removeAttr ( "disabled" ) ;
$ ( "#amount" ) . attr ( "disabled" , "" ) ;
2018-11-18 13:46:23 +01:00
$ ( "#amount" ) . val ( 1 ) ;
2018-11-17 17:51:35 +01:00
$ ( "#amount" ) . removeAttr ( "required" ) ;
$ ( "#specific_stock_entry" ) . attr ( "required" , "" ) ;
}
else
{
$ ( "#specific_stock_entry" ) . attr ( "disabled" , "" ) ;
$ ( "#amount" ) . removeAttr ( "disabled" ) ;
$ ( "#amount" ) . attr ( "required" , "" ) ;
$ ( "#specific_stock_entry" ) . removeAttr ( "required" ) ;
}
Grocy . FrontendHelpers . ValidateForm ( "consume-form" ) ;
} ) ;
2018-10-27 17:26:00 +02:00
function UndoStockBooking ( bookingId )
{
2019-01-19 14:51:51 +01: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 ) ;
}
) ;
} ;