2020-08-30 12:18:16 +02:00
$ ( '#save-transfer-button' ) . on ( 'click' , function ( e )
2019-12-19 12:48:36 -06:00
{
e . preventDefault ( ) ;
2020-12-31 13:45:14 +01:00
if ( $ ( ".combobox-menu-visible" ) . length )
{
return ;
}
2019-12-19 12:48:36 -06:00
var jsonForm = $ ( '#transfer-form' ) . serializeJSON ( ) ;
Grocy . FrontendHelpers . BeginUiBusy ( "transfer-form" ) ;
var apiUrl = 'stock/products/' + jsonForm . product _id + '/transfer' ;
var jsonData = { } ;
jsonData . amount = jsonForm . amount ;
jsonData . location _id _to = $ ( "#location_id_to" ) . val ( ) ;
jsonData . location _id _from = $ ( "#location_id_from" ) . val ( ) ;
if ( $ ( "#use_specific_stock_entry" ) . is ( ":checked" ) )
{
jsonData . stock _entry _id = jsonForm . specific _stock _entry ;
}
var bookingResponse = null ;
Grocy . Api . Get ( 'stock/products/' + jsonForm . product _id ,
function ( productDetails )
{
Grocy . Api . Post ( apiUrl , jsonData ,
function ( result )
{
bookingResponse = result ;
2020-11-14 11:05:36 +01:00
if ( GetUriParam ( "flow" ) === "InplaceAddBarcodeToExistingProduct" )
2019-12-19 12:48:36 -06: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 ;
Grocy . Api . Post ( 'objects/product_barcodes' , jsonDataBarcode ,
2019-12-19 12:48:36 -06:00
function ( result )
{
2020-11-14 11:05:36 +01:00
$ ( "#flow-info-InplaceAddBarcodeToExistingProduct" ) . addClass ( "d-none" ) ;
2019-12-19 12:48:36 -06: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 ( "/transfer" ) ) ;
2019-12-19 12:48:36 -06:00
} ,
function ( xhr )
{
2020-08-17 14:47:33 -05:00
Grocy . FrontendHelpers . EndUiBusy ( "transfer-form" ) ;
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response ) ;
2019-12-19 12:48:36 -06:00
}
) ;
}
if ( productDetails . product . enable _tare _weight _handling == 1 )
{
2020-12-20 14:43:07 +01:00
var successMessage = _ _t ( 'Transfered %1$s of %2$s from %3$s to %4$s' , 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 , $ ( 'option:selected' , "#location_id_from" ) . text ( ) , $ ( 'option:selected' , "#location_id_to" ) . text ( ) ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse [ 0 ] . transaction _id + '\')"><i class="fas fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ;
2019-12-19 12:48:36 -06:00
}
else
{
2020-12-20 14:43:07 +01:00
var successMessage = _ _t ( 'Transfered %1$s of %2$s from %3$s to %4$s' , Math . abs ( jsonForm . amount ) + " " + _ _n ( jsonForm . amount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural ) , productDetails . product . name , $ ( 'option:selected' , "#location_id_from" ) . text ( ) , $ ( 'option:selected' , "#location_id_to" ) . text ( ) ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse [ 0 ] . transaction _id + '\')"><i class="fas fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ;
2019-12-19 12:48:36 -06:00
}
if ( GetUriParam ( "embedded" ) !== undefined )
{
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 ( "transfer-form" ) ;
toastr . success ( successMessage ) ;
2020-11-14 11:05:36 +01:00
Grocy . Components . ProductPicker . FinishFlow ( ) ;
2019-12-19 12:48:36 -06:00
2020-01-26 20:01:30 +01:00
if ( parseInt ( $ ( "#location_id_from option:selected" ) . attr ( "data-is-freezer" ) ) === 0 && parseInt ( $ ( "#location_id_to option:selected" ) . attr ( "data-is-freezer" ) ) === 1 ) // Frozen
{
toastr . info ( '<span>' + _ _t ( "Frozen" ) + "</span> <i class='fas fa-snowflake'></i>" ) ;
}
if ( parseInt ( $ ( "#location_id_from option:selected" ) . attr ( "data-is-freezer" ) ) === 1 && parseInt ( $ ( "#location_id_to option:selected" ) . attr ( "data-is-freezer" ) ) === 0 ) // Thawed
{
toastr . info ( '<span>' + _ _t ( "Thawed" ) + "</span> <i class='fas fa-fire-alt'></i>" ) ;
}
2019-12-19 12:48:36 -06:00
$ ( "#specific_stock_entry" ) . find ( "option" ) . remove ( ) . end ( ) . append ( "<option></option>" ) ;
$ ( "#specific_stock_entry" ) . attr ( "disabled" , "" ) ;
$ ( "#specific_stock_entry" ) . removeAttr ( "required" ) ;
if ( $ ( "#use_specific_stock_entry" ) . is ( ":checked" ) )
{
$ ( "#use_specific_stock_entry" ) . click ( ) ;
}
2020-11-09 19:25:46 +01:00
Grocy . Components . ProductAmountPicker . Reset ( ) ;
2019-12-19 12:48:36 -06:00
$ ( "#location_id_from" ) . find ( "option" ) . remove ( ) . end ( ) . append ( "<option></option>" ) ;
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-11-15 22:29:47 +01:00
$ ( '#display_amount' ) . val ( parseFloat ( Grocy . UserSettings . stock _default _transfer _amount ) ) ;
2020-11-15 14:15:09 +01:00
RefreshLocaleNumberInput ( ) ;
2020-11-12 22:47:00 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2019-12-19 12:48:36 -06:00
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
Grocy . Components . ProductPicker . Clear ( ) ;
$ ( "#location_id_to" ) . val ( "" ) ;
$ ( "#location_id_from" ) . val ( "" ) ;
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
2020-01-17 11:13:43 -06:00
Grocy . Components . ProductCard . Refresh ( jsonForm . product _id ) ;
2019-12-19 12:48:36 -06:00
Grocy . FrontendHelpers . ValidateForm ( 'transfer-form' ) ;
}
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( "transfer-form" ) ;
console . error ( xhr ) ;
}
) ;
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( "transfer-form" ) ;
console . error ( xhr ) ;
}
) ;
} ) ;
Grocy . Components . ProductPicker . GetPicker ( ) . on ( 'change' , function ( e )
{
$ ( "#specific_stock_entry" ) . find ( "option" ) . remove ( ) . end ( ) . append ( "<option></option>" ) ;
2021-02-21 19:36:37 +01:00
if ( $ ( "#use_specific_stock_entry" ) . is ( ":checked" ) && GetUriParam ( "stockId" ) == null )
2019-12-19 12:48:36 -06:00
{
$ ( "#use_specific_stock_entry" ) . click ( ) ;
}
$ ( "#location_id_to" ) . val ( "" ) ;
2021-02-21 19:36:37 +01:00
if ( GetUriParam ( "stockId" ) == null )
{
$ ( "#location_id_from" ) . val ( "" ) ;
}
2019-12-19 12:48:36 -06:00
var productId = $ ( e . target ) . val ( ) ;
if ( productId )
{
Grocy . Components . ProductCard . Refresh ( productId ) ;
Grocy . Api . Get ( 'stock/products/' + productId ,
function ( productDetails )
{
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-09 19:25:46 +01:00
2020-08-30 12:18:16 +02:00
if ( productDetails . product . enable _tare _weight _handling == 1 )
{
2020-11-16 19:10:29 +01:00
Grocy . Components . ProductPicker . GetPicker ( ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'Products with tare weight enabled are currently not supported for transfer' ) ) ;
2019-12-19 12:48:36 -06:00
Grocy . Components . ProductPicker . Clear ( ) ;
return ;
}
$ ( "#location_id_from" ) . find ( "option" ) . remove ( ) . end ( ) . append ( "<option></option>" ) ;
Grocy . Api . Get ( "stock/products/" + productId + '/locations' ,
function ( stockLocations )
{
var setDefault = 0 ;
stockLocations . forEach ( stockLocation =>
{
if ( productDetails . location . id == stockLocation . location _id )
{
$ ( "#location_id_from" ) . append ( $ ( "<option>" , {
value : stockLocation . location _id ,
2020-01-26 20:01:30 +01:00
text : stockLocation . location _name + " (" + _ _t ( "Default location" ) + ")" ,
"data-is-freezer" : stockLocation . location _is _freezer
2019-12-19 12:48:36 -06:00
} ) ) ;
$ ( "#location_id_from" ) . val ( productDetails . location . id ) ;
$ ( "#location_id_from" ) . trigger ( 'change' ) ;
setDefault = 1 ;
}
else
{
$ ( "#location_id_from" ) . append ( $ ( "<option>" , {
value : stockLocation . location _id ,
2020-01-26 20:01:30 +01:00
text : stockLocation . location _name ,
"data-is-freezer" : stockLocation . location _is _freezer
2019-12-19 12:48:36 -06:00
} ) ) ;
}
if ( setDefault == 0 )
{
$ ( "#location_id_from" ) . val ( stockLocation . location _id ) ;
$ ( "#location_id_from" ) . trigger ( 'change' ) ;
}
} ) ;
2021-02-21 19:36:37 +01:00
if ( GetUriParam ( "locationId" ) != null )
{
$ ( "#location_id_from" ) . val ( GetUriParam ( "locationId" ) ) ;
$ ( "#location_id_from" ) . trigger ( "change" ) ;
}
2019-12-19 12:48:36 -06:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
2021-01-04 21:59:19 +01:00
if ( document . getElementById ( "product_id" ) . getAttribute ( "barcode" ) != "null" )
{
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 )
{
if ( barcode . amount != null && ! barcode . amount . isEmpty ( ) )
{
$ ( "#display_amount" ) . val ( barcode . amount ) ;
$ ( "#display_amount" ) . select ( ) ;
}
if ( barcode . qu _id != null )
{
Grocy . Components . ProductAmountPicker . SetQuantityUnit ( barcode . qu _id ) ;
}
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
Grocy . FrontendHelpers . ValidateForm ( 'transfer-form' ) ;
RefreshLocaleNumberInput ( ) ;
}
}
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2019-12-19 12:48:36 -06: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 ) ;
2019-12-19 12:48:36 -06:00
$ ( "#tare-weight-handling-info" ) . removeClass ( "d-none" ) ;
}
else
{
2020-12-23 19:56:37 +01:00
$ ( "#display_amount" ) . attr ( "min" , Grocy . DefaultMinAmount ) ;
2019-12-19 12:48:36 -06:00
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
}
2020-11-17 19:11:02 +01:00
$ ( '#display_amount' ) . attr ( "data-stock-amount" , productDetails . stock _amount ) ;
2019-12-19 12:48:36 -06:00
if ( ( parseFloat ( productDetails . stock _amount ) || 0 ) === 0 )
{
Grocy . Components . ProductPicker . Clear ( ) ;
Grocy . FrontendHelpers . ValidateForm ( 'transfer-form' ) ;
Grocy . Components . ProductPicker . ShowCustomError ( _ _t ( 'This product is not in stock' ) ) ;
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
}
else
{
Grocy . Components . ProductPicker . HideCustomError ( ) ;
Grocy . FrontendHelpers . ValidateForm ( 'transfer-form' ) ;
2020-11-09 19:25:46 +01:00
$ ( '#display_amount' ) . focus ( ) ;
2019-12-19 12:48:36 -06:00
}
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
} ) ;
2020-11-15 22:29:47 +01:00
$ ( '#display_amount' ) . val ( parseFloat ( Grocy . UserSettings . stock _default _transfer _amount ) ) ;
2020-11-12 22:47:00 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2019-12-19 12:48:36 -06:00
Grocy . FrontendHelpers . ValidateForm ( 'transfer-form' ) ;
2020-11-15 22:29:47 +01:00
RefreshLocaleNumberInput ( ) ;
2019-12-19 12:48:36 -06:00
$ ( "#location_id_from" ) . on ( 'change' , function ( e )
{
var locationId = $ ( e . target ) . val ( ) ;
var sumValue = 0 ;
var stockId = null ;
if ( locationId == $ ( "#location_id_to" ) . val ( ) )
{
$ ( "#location_id_to" ) . val ( "" ) ;
}
if ( GetUriParam ( "embedded" ) !== undefined )
{
stockId = GetUriParam ( 'stockId' ) ;
}
$ ( "#specific_stock_entry" ) . find ( "option" ) . remove ( ) . end ( ) . append ( "<option></option>" ) ;
2021-02-21 19:36:37 +01:00
if ( $ ( "#use_specific_stock_entry" ) . is ( ":checked" ) && GetUriParam ( "stockId" ) == null )
2019-12-19 12:48:36 -06:00
{
$ ( "#use_specific_stock_entry" ) . click ( ) ;
}
if ( locationId )
{
Grocy . Api . Get ( "stock/products/" + Grocy . Components . ProductPicker . GetValue ( ) + '/entries' ,
function ( stockEntries )
{
stockEntries . forEach ( stockEntry =>
{
var openTxt = _ _t ( "Not opened" ) ;
if ( stockEntry . open == 1 )
{
openTxt = _ _t ( "Opened" ) ;
}
if ( stockEntry . location _id == locationId )
{
$ ( "#specific_stock_entry" ) . append ( $ ( "<option>" , {
value : stockEntry . stock _id ,
amount : stockEntry . amount ,
2020-11-15 19:53:44 +01:00
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
2019-12-19 12:48:36 -06:00
} ) ) ;
if ( stockEntry . stock _id == stockId )
{
$ ( "#specific_stock_entry" ) . val ( stockId ) ;
}
sumValue = sumValue + parseFloat ( stockEntry . amount ) ;
}
} ) ;
2020-11-17 19:11:02 +01:00
$ ( "#display_amount" ) . attr ( "max" , sumValue * $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) ;
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 ) ;
}
) ;
}
} ) ;
$ ( "#location_id_to" ) . on ( 'change' , function ( e )
{
var locationId = $ ( e . target ) . val ( ) ;
if ( locationId == $ ( "#location_id_from" ) . val ( ) )
{
$ ( "#location_id_to" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'This cannot be the same as the "From" location' ) ) ;
$ ( "#location_id_to" ) . val ( "" ) ;
}
} ) ;
2020-11-17 19:11:02 +01:00
$ ( "#qu_id" ) . on ( 'change' , function ( e )
{
$ ( "#display_amount" ) . attr ( "max" , parseFloat ( $ ( '#display_amount' ) . attr ( "data-stock-amount" ) ) * $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) ;
} ) ;
2020-11-09 19:25:46 +01:00
$ ( '#display_amount' ) . on ( 'focus' , function ( e )
2019-12-19 12:48:36 -06:00
{
$ ( this ) . select ( ) ;
} ) ;
$ ( '#transfer-form input' ) . keyup ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'transfer-form' ) ;
} ) ;
$ ( '#transfer-form select' ) . change ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'transfer-form' ) ;
} ) ;
$ ( '#transfer-form input' ) . keydown ( function ( event )
{
if ( event . keyCode === 13 ) //Enter
{
event . preventDefault ( ) ;
if ( document . getElementById ( 'transfer-form' ) . checkValidity ( ) === false ) //There is at least one validation error
{
return false ;
}
else
{
$ ( '#save-transfer-button' ) . click ( ) ;
}
}
} ) ;
$ ( "#specific_stock_entry" ) . on ( "change" , function ( e )
{
if ( $ ( e . target ) . val ( ) == "" )
{
2020-08-30 12:18:16 +02:00
var 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_from" ) . val ( ) || stockEntry . location _id == "" )
{
sumValue = sumValue + parseFloat ( stockEntry . amount ) ;
}
} ) ;
2020-11-17 19:11:02 +01:00
$ ( "#display_amount" ) . attr ( "max" , sumValue * $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) ;
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
{
2020-11-09 19:25:46 +01:00
$ ( "#display_amount" ) . attr ( "max" , $ ( 'option:selected' , this ) . attr ( 'amount' ) ) ;
2019-12-19 12:48:36 -06:00
}
} ) ;
$ ( "#use_specific_stock_entry" ) . on ( "change" , function ( )
{
var value = $ ( this ) . is ( ":checked" ) ;
if ( value )
{
$ ( "#specific_stock_entry" ) . removeAttr ( "disabled" ) ;
$ ( "#specific_stock_entry" ) . attr ( "required" , "" ) ;
}
else
{
$ ( "#specific_stock_entry" ) . attr ( "disabled" , "" ) ;
$ ( "#specific_stock_entry" ) . removeAttr ( "required" ) ;
$ ( "#specific_stock_entry" ) . val ( "" ) ;
$ ( "#location_id_from" ) . trigger ( 'change' ) ;
}
Grocy . FrontendHelpers . ValidateForm ( "transfer-form" ) ;
} ) ;
function UndoStockBooking ( bookingId )
{
2020-08-30 12:18:16 +02:00
Grocy . Api . Post ( 'stock/bookings/' + bookingId . toString ( ) + '/undo' , { } ,
2019-12-19 12:48:36 -06:00
function ( result )
{
toastr . success ( _ _t ( "Booking successfully undone" ) ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ;
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_from" ) . val ( locationId ) ;
$ ( "#location_id_from" ) . trigger ( 'change' ) ;
$ ( "#use_specific_stock_entry" ) . click ( ) ;
$ ( "#use_specific_stock_entry" ) . trigger ( 'change' ) ;
2021-02-21 19:36:37 +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
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;