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-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 )
{
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" ) ;
2018-10-27 17:26:00 +02:00
toastr . success ( L ( 'Removed #1 #2 of #3 from stock' , jsonForm . amount , Pluralize ( 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 . booking _id + ')"><i class="fas fa-undo"></i> ' + L ( "Undo" ) + '</a>' ) ;
2017-04-17 16:51:49 +02:00
$ ( '#amount' ) . val ( 1 ) ;
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . SetValue ( '' ) ;
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" ) ;
2018-11-17 19:39:37 +01:00
toastr . success ( L ( 'Marked #1 #2 of #3 as opened' , jsonForm . amount , Pluralize ( 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 . booking _id + ')"><i class="fas fa-undo"></i> ' + L ( "Undo" ) + '</a>' ) ;
$ ( '#amount' ) . val ( 1 ) ;
Grocy . Components . ProductPicker . SetValue ( '' ) ;
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
2017-04-20 17:10:21 +02:00
if ( ( productDetails . stock _amount || 0 ) === 0 )
2017-04-17 16:51:49 +02:00
{
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . SetValue ( '' ) ;
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'consume-form' ) ;
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . ShowCustomError ( L ( 'This product is not in stock' ) ) ;
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 =>
{
2018-11-17 19:39:37 +01:00
var openTxt = L ( "Not opened" ) ;
if ( stockEntry . open == 1 )
{
openTxt = L ( "Opened" ) ;
}
2018-11-17 17:51:35 +01:00
for ( i = 0 ; i < stockEntry . amount ; i ++ )
{
$ ( "#specific_stock_entry" ) . append ( $ ( "<option>" , {
value : stockEntry . stock _id ,
2018-11-17 19:39:37 +01:00
text : L ( "Expires on #1; Bought on #2" , 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
} ) ;
2018-04-16 19:11:32 +02:00
$ ( '#amount' ) . val ( 1 ) ;
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 )
{
toastr . success ( L ( "Booking successfully undone" ) ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ;