2020-11-08 19:00:12 +01:00
function saveProductPicture ( result , location , jsonData )
2017-04-15 23:16:20 +02:00
{
2020-11-08 19:00:12 +01:00
var productId = Grocy . EditObjectId || result . created _object _id ;
2020-11-14 11:05:36 +01:00
2020-11-08 19:00:12 +01:00
Grocy . Components . UserfieldsForm . Save ( ( ) =>
2017-04-20 22:01:14 +02:00
{
2020-11-08 19:00:12 +01:00
if ( jsonData . hasOwnProperty ( "picture_file_name" ) && ! Grocy . DeleteProductPictureOnSave )
{
Grocy . Api . UploadFile ( $ ( "#product-picture" ) [ 0 ] . files [ 0 ] , 'productpictures' , jsonData . picture _file _name ,
( result ) =>
{
2020-11-10 18:11:33 +01:00
if ( Grocy . ProductEditFormRedirectUri == "reload" )
{
window . location . reload ( ) ;
return
}
2020-11-08 19:00:12 +01:00
var returnTo = GetUriParam ( 'returnto' ) ;
if ( GetUriParam ( "closeAfterCreation" ) !== undefined )
{
window . close ( ) ;
}
else if ( returnTo !== undefined )
{
2020-11-14 11:49:46 +01:00
if ( GetUriParam ( "flow" ) !== undefined )
{
window . location . href = U ( returnTo ) + '&product-name=' + encodeURIComponent ( $ ( '#name' ) . val ( ) ) ;
}
else
{
window . location . href = U ( returnTo ) ;
}
2020-11-08 19:00:12 +01:00
}
else
{
window . location . href = U ( location + productId ) ;
}
2017-04-20 22:01:14 +02:00
2020-11-08 19:00:12 +01:00
} ,
( xhr ) =>
{
Grocy . FrontendHelpers . EndUiBusy ( "product-form" ) ;
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
}
) ;
}
else
{
2020-11-10 18:11:33 +01:00
if ( Grocy . ProductEditFormRedirectUri == "reload" )
{
window . location . reload ( ) ;
return
}
2020-11-08 19:00:12 +01:00
var returnTo = GetUriParam ( 'returnto' ) ;
if ( GetUriParam ( "closeAfterCreation" ) !== undefined )
{
window . close ( ) ;
}
else if ( returnTo !== undefined )
{
2020-11-14 11:49:46 +01:00
if ( GetUriParam ( "flow" ) !== undefined )
{
window . location . href = U ( returnTo ) + '&product-name=' + encodeURIComponent ( $ ( '#name' ) . val ( ) ) ;
}
else
{
window . location . href = U ( returnTo ) ;
}
2020-11-08 19:00:12 +01:00
}
else
{
window . location . href = U ( location + productId ) ;
}
}
} ) ;
}
$ ( '.save-product-button' ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
2019-09-15 16:40:54 +02:00
2020-11-11 17:34:37 +01:00
var jsonData = $ ( '#product-form' ) . serializeJSON ( ) ;
2019-09-14 17:34:36 +02:00
var parentProductId = jsonData . product _id ;
delete jsonData . product _id ;
jsonData . parent _product _id = parentProductId ;
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . BeginUiBusy ( "product-form" ) ;
2019-09-14 17:34:36 +02:00
if ( jsonData . parent _product _id . toString ( ) . isEmpty ( ) )
{
jsonData . parent _product _id = null ;
}
2018-09-30 23:22:17 +02:00
if ( $ ( "#product-picture" ) [ 0 ] . files . length > 0 )
{
2018-10-01 20:20:50 +02:00
var someRandomStuff = Math . random ( ) . toString ( 36 ) . substring ( 2 , 100 ) + Math . random ( ) . toString ( 36 ) . substring ( 2 , 100 ) ;
jsonData . picture _file _name = someRandomStuff + $ ( "#product-picture" ) [ 0 ] . files [ 0 ] . name ;
}
2020-11-08 19:00:12 +01:00
const location = $ ( e . currentTarget ) . attr ( 'data-location' ) == 'return' ? '/products?product=' : '/product/' ;
2018-09-30 23:22:17 +02:00
2020-11-08 19:00:12 +01:00
if ( Grocy . EditMode == 'create' )
2017-04-15 23:16:20 +02:00
{
2019-01-19 14:51:51 +01:00
Grocy . Api . Post ( 'objects/products' , jsonData ,
2020-11-12 13:09:46 +01:00
( result ) => saveProductPicture ( result , location , jsonData ) ,
( xhr ) =>
{
Grocy . FrontendHelpers . EndUiBusy ( "product-form" ) ;
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
} ) ;
2020-11-08 19:00:12 +01:00
return ;
2017-04-15 23:16:20 +02:00
}
2020-11-08 19:00:12 +01:00
if ( Grocy . DeleteProductPictureOnSave )
2017-04-15 23:16:20 +02:00
{
2020-11-08 19:00:12 +01:00
jsonData . picture _file _name = null ;
2018-10-01 20:20:50 +02:00
2020-11-08 19:00:12 +01:00
Grocy . Api . DeleteFile ( Grocy . ProductPictureFileName , 'productpictures' , { } ,
2017-04-15 23:16:20 +02:00
function ( result )
{
2020-11-08 19:00:12 +01:00
// Nothing to do
2017-04-15 23:16:20 +02:00
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "product-form" ) ;
2018-09-08 09:26:12 +02:00
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
2017-04-15 23:16:20 +02:00
}
) ;
}
2020-11-08 19:00:12 +01:00
Grocy . Api . Put ( 'objects/products/' + Grocy . EditObjectId , jsonData ,
( result ) => saveProductPicture ( result , location , jsonData ) ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( "product-form" ) ;
console . error ( xhr ) ;
}
) ;
2017-04-15 23:16:20 +02:00
} ) ;
2020-11-14 11:05:36 +01:00
if ( Grocy . EditMode == "edit" )
{
Grocy . Api . Get ( 'stock/products/' + Grocy . EditObjectId ,
function ( productDetails )
{
if ( productDetails . last _purchased == null )
{
$ ( '#qu_id_stock' ) . removeAttr ( "disabled" ) ;
}
} ,
function ( xhr )
2018-04-16 19:11:32 +02:00
{
2020-11-14 11:05:36 +01:00
console . error ( xhr ) ;
2018-04-16 19:11:32 +02:00
}
2020-11-14 11:05:36 +01:00
) ;
}
2017-04-18 21:15:45 +02:00
2020-11-14 11:05:36 +01:00
if ( GetUriParam ( "flow" ) == "InplaceNewProductWithName" )
2018-04-16 19:11:32 +02:00
{
2020-11-14 11:05:36 +01:00
$ ( '#name' ) . val ( GetUriParam ( "name" ) ) ;
2017-04-15 23:16:20 +02:00
$ ( '#name' ) . focus ( ) ;
2018-04-16 19:11:32 +02:00
}
2017-04-20 22:01:14 +02:00
2020-11-14 11:49:46 +01:00
if ( GetUriParam ( "flow" ) !== undefined || GetUriParam ( "returnto" ) !== undefined )
2020-11-14 11:05:36 +01:00
{
$ ( "#save-hint" ) . addClass ( "d-none" ) ;
$ ( ".save-product-button[data-location='return']" ) . addClass ( "d-none" ) ;
}
2019-06-09 09:21:56 +02:00
2017-04-18 19:54:51 +02:00
$ ( '.input-group-qu' ) . on ( 'change' , function ( e )
{
2018-10-03 13:27:36 +02:00
var quIdPurchase = $ ( "#qu_id_purchase" ) . val ( ) ;
var quIdStock = $ ( "#qu_id_stock" ) . val ( ) ;
2017-04-18 19:54:51 +02:00
var factor = $ ( '#qu_factor_purchase_to_stock' ) . val ( ) ;
2018-10-03 13:27:36 +02:00
2019-09-17 20:02:27 +02:00
if ( factor > 1 || quIdPurchase != quIdStock )
2017-04-18 19:54:51 +02:00
{
2019-09-20 10:30:46 +02:00
$ ( '#qu-conversion-info' ) . text ( _ _t ( 'This means 1 %1$s purchased will be converted into %2$s %3$s in stock' , $ ( "#qu_id_purchase option:selected" ) . text ( ) , ( 1 * factor ) . toString ( ) , _ _n ( ( 1 * factor ) . toString ( ) , $ ( "#qu_id_stock option:selected" ) . text ( ) , $ ( "#qu_id_stock option:selected" ) . data ( "plural-form" ) ) ) ) ;
2018-07-11 19:43:05 +02:00
$ ( '#qu-conversion-info' ) . removeClass ( 'd-none' ) ;
2017-04-18 19:54:51 +02:00
}
else
{
2018-07-11 19:43:05 +02:00
$ ( '#qu-conversion-info' ) . addClass ( 'd-none' ) ;
}
2018-10-03 13:27:36 +02:00
2019-03-05 17:51:50 +01:00
$ ( "#tare_weight_qu_info" ) . text ( $ ( "#qu_id_stock option:selected" ) . text ( ) ) ;
2020-11-15 09:57:45 +01:00
$ ( "#quick_consume_qu_info" ) . text ( $ ( "#qu_id_stock option:selected" ) . text ( ) ) ;
2019-03-05 17:51:50 +01:00
2018-10-03 13:27:36 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'product-form' ) ;
2018-07-11 19:43:05 +02:00
} ) ;
$ ( '#product-form input' ) . keyup ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'product-form' ) ;
2019-09-26 13:59:41 +02:00
$ ( ".input-group-qu" ) . trigger ( "change" ) ;
2020-11-14 11:05:36 +01:00
$ ( "#product-form select" ) . trigger ( "select" ) ;
2019-09-16 09:35:20 +02:00
if ( document . getElementById ( 'product-form' ) . checkValidity ( ) === false ) //There is at least one validation error
{
$ ( "#qu-conversion-add-button" ) . addClass ( "disabled" ) ;
}
else
{
$ ( "#qu-conversion-add-button" ) . removeClass ( "disabled" ) ;
}
2020-08-17 14:47:33 -05:00
if ( document . getElementById ( 'product-form' ) . checkValidity ( ) === false ) //There is at least one validation error
{
$ ( "#barcode-add-button" ) . addClass ( "disabled" ) ;
}
2018-07-11 19:43:05 +02:00
} ) ;
2019-01-26 13:13:38 +01:00
$ ( '#location_id' ) . change ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'product-form' ) ;
} ) ;
2018-07-11 19:43:05 +02:00
$ ( '#product-form input' ) . keydown ( function ( event )
{
if ( event . keyCode === 13 ) //Enter
{
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 ( 'product-form' ) . checkValidity ( ) === false ) //There is at least one validation error
{
return false ;
}
else
{
$ ( '#save-product-button' ) . click ( ) ;
}
2017-04-18 19:54:51 +02:00
}
} ) ;
2018-04-16 19:11:32 +02:00
2019-03-05 17:51:50 +01:00
$ ( "#enable_tare_weight_handling" ) . on ( "click" , function ( )
{
if ( this . checked )
{
$ ( "#tare_weight" ) . removeAttr ( "disabled" ) ;
}
else
{
$ ( "#tare_weight" ) . attr ( "disabled" , "" ) ;
}
Grocy . FrontendHelpers . ValidateForm ( "product-form" ) ;
} ) ;
2020-11-08 19:00:12 +01:00
$ ( "#product-picture" ) . on ( "change" , function ( e )
2020-09-08 17:52:35 +02:00
{
2020-11-08 19:00:12 +01:00
$ ( "#product-picture-label" ) . removeClass ( "d-none" ) ;
$ ( "#product-picture-label-none" ) . addClass ( "d-none" ) ;
$ ( "#delete-current-product-picture-on-save-hint" ) . addClass ( "d-none" ) ;
$ ( "#current-product-picture" ) . addClass ( "d-none" ) ;
Grocy . DeleteProductPictureOnSave = false ;
2020-09-08 17:46:37 +02:00
} ) ;
2018-10-01 20:20:50 +02:00
Grocy . DeleteProductPictureOnSave = false ;
2020-11-08 19:00:12 +01:00
$ ( "#delete-current-product-picture-button" ) . on ( "click" , function ( e )
2018-10-01 20:20:50 +02:00
{
Grocy . DeleteProductPictureOnSave = true ;
$ ( "#current-product-picture" ) . addClass ( "d-none" ) ;
$ ( "#delete-current-product-picture-on-save-hint" ) . removeClass ( "d-none" ) ;
2020-11-08 19:00:12 +01:00
$ ( "#product-picture-label" ) . addClass ( "d-none" ) ;
$ ( "#product-picture-label-none" ) . removeClass ( "d-none" ) ;
2018-10-01 20:20:50 +02:00
} ) ;
2018-10-20 14:55:49 +02:00
if ( Grocy . EditMode === 'create' )
{
if ( Grocy . UserSettings . product _presets _location _id . toString ( ) !== '-1' )
{
$ ( "#location_id" ) . val ( Grocy . UserSettings . product _presets _location _id ) ;
}
if ( Grocy . UserSettings . product _presets _product _group _id . toString ( ) !== '-1' )
{
$ ( "#product_group_id" ) . val ( Grocy . UserSettings . product _presets _product _group _id ) ;
}
if ( Grocy . UserSettings . product _presets _qu _id . toString ( ) !== '-1' )
{
$ ( "select.input-group-qu" ) . val ( Grocy . UserSettings . product _presets _qu _id ) ;
}
}
2020-11-15 14:59:54 +01:00
var quConversionsTable = $ ( '#qu-conversions-table-products' ) . DataTable ( {
2019-09-15 16:40:54 +02:00
'order' : [ [ 1 , 'asc' ] ] ,
2020-11-10 18:11:33 +01:00
"orderFixed" : [ [ 4 , 'asc' ] ] ,
2019-09-15 16:40:54 +02:00
'columnDefs' : [
{ 'orderable' : false , 'targets' : 0 } ,
2020-01-03 14:18:56 +01:00
{ 'searchable' : false , "targets" : 0 } ,
2020-11-10 18:11:33 +01:00
{ 'visible' : false , 'targets' : 4 }
2019-09-15 16:40:54 +02:00
] ,
'rowGroup' : {
2020-11-10 18:11:33 +01:00
dataSrc : 4
2019-09-15 16:40:54 +02:00
}
} ) ;
$ ( '#qu-conversions-table tbody' ) . removeClass ( "d-none" ) ;
quConversionsTable . columns . adjust ( ) . draw ( ) ;
2020-08-17 14:47:33 -05:00
var barcodeTable = $ ( '#barcode-table' ) . DataTable ( {
'order' : [ [ 1 , 'asc' ] ] ,
"orderFixed" : [ [ 1 , 'asc' ] ] ,
'columnDefs' : [
{ 'orderable' : false , 'targets' : 0 } ,
{ 'searchable' : false , "targets" : 0 }
]
} ) ;
$ ( '#barcode-table tbody' ) . removeClass ( "d-none" ) ;
barcodeTable . columns . adjust ( ) . draw ( ) ;
2020-11-07 14:53:45 +01:00
$ ( '.dataTables_scrollBody' ) . addClass ( "dragscroll" ) ;
dragscroll . reset ( ) ;
2020-08-17 14:47:33 -05:00
2019-04-22 22:16:35 +02:00
Grocy . Components . UserfieldsForm . Load ( ) ;
2019-09-16 09:35:20 +02:00
$ ( "#name" ) . trigger ( "keyup" ) ;
2018-04-16 19:11:32 +02:00
$ ( '#name' ) . focus ( ) ;
$ ( '.input-group-qu' ) . trigger ( 'change' ) ;
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'product-form' ) ;
2019-04-06 15:27:00 +02:00
2019-09-15 16:40:54 +02:00
$ ( document ) . on ( 'click' , '.qu-conversion-delete-button' , function ( e )
{
var objectId = $ ( e . currentTarget ) . attr ( 'data-qu-conversion-id' ) ;
bootbox . confirm ( {
message : _ _t ( 'Are you sure to remove this conversion?' ) ,
2019-09-24 10:24:47 +02:00
closeButton : false ,
2019-09-15 16:40:54 +02:00
buttons : {
confirm : {
label : _ _t ( 'Yes' ) ,
className : 'btn-success'
} ,
cancel : {
label : _ _t ( 'No' ) ,
className : 'btn-danger'
}
} ,
callback : function ( result )
{
if ( result === true )
{
2020-08-30 12:18:16 +02:00
Grocy . Api . Delete ( 'objects/quantity_unit_conversions/' + objectId , { } ,
2019-09-15 16:40:54 +02:00
function ( result )
{
Grocy . ProductEditFormRedirectUri = "reload" ;
$ ( '#save-product-button' ) . click ( ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
}
} ) ;
} ) ;
2020-08-17 14:47:33 -05:00
$ ( document ) . on ( 'click' , '.barcode-delete-button' , function ( e )
2019-09-15 16:40:54 +02:00
{
2020-08-17 14:47:33 -05:00
var objectId = $ ( e . currentTarget ) . attr ( 'data-barcode-id' ) ;
2019-09-15 16:40:54 +02:00
2020-08-17 14:47:33 -05:00
bootbox . confirm ( {
message : _ _t ( 'Are you sure to remove this barcode?' ) ,
closeButton : false ,
buttons : {
confirm : {
label : _ _t ( 'Yes' ) ,
className : 'btn-success'
} ,
cancel : {
label : _ _t ( 'No' ) ,
className : 'btn-danger'
}
} ,
callback : function ( result )
{
if ( result === true )
{
2020-08-30 12:18:16 +02:00
Grocy . Api . Delete ( 'objects/product_barcodes/' + objectId , { } ,
2020-08-17 14:47:33 -05:00
function ( result )
{
Grocy . ProductEditFormRedirectUri = "reload" ;
$ ( '#save-product-button' ) . click ( ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
}
} ) ;
2019-09-15 16:40:54 +02:00
} ) ;
2020-04-02 08:36:20 +02:00
2020-11-15 16:05:25 +01:00
$ ( '#qu_id_stock' ) . change ( function ( e )
2020-04-02 08:36:20 +02:00
{
2020-11-15 16:05:25 +01:00
// Preset QU purchase with stock QU if unset
var quIdStock = $ ( '#qu_id_stock' ) ;
var quIdPurchase = $ ( '#qu_id_purchase' ) ;
if ( quIdPurchase [ 0 ] . selectedIndex === 0 && quIdStock [ 0 ] . selectedIndex !== 0 )
2020-08-30 12:18:16 +02:00
{
2020-11-15 16:05:25 +01:00
quIdPurchase [ 0 ] . selectedIndex = quIdStock [ 0 ] . selectedIndex ;
2020-04-07 16:37:30 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'product-form' ) ;
2020-04-02 08:36:20 +02:00
}
} ) ;
2020-04-13 15:55:27 +02:00
2020-08-17 14:47:33 -05:00
$ ( window ) . on ( "message" , function ( e )
2020-04-13 15:55:27 +02:00
{
2020-08-17 14:47:33 -05:00
var data = e . originalEvent . data ;
if ( data . Message === "ProductBarcodesChanged" || data . Message === "ProductQUConversionChanged" )
2020-04-13 15:55:27 +02:00
{
2020-08-17 14:47:33 -05:00
window . location . reload ( ) ;
2020-04-13 15:55:27 +02:00
}
} ) ;
2020-11-15 16:05:25 +01:00
if ( Grocy . EditMode == "create" && GetUriParam ( "copy-of" ) != undefined )
{
Grocy . Api . Get ( 'objects/products/' + GetUriParam ( "copy-of" ) ,
function ( sourceProduct )
{
if ( sourceProduct . parent _product _id != null )
{
Grocy . Components . ProductPicker . SetId ( sourceProduct . parent _product _id ) ;
}
if ( sourceProduct . description != null )
{
$ ( "#description" ) . summernote ( "pasteHTML" , sourceProduct . description ) ;
}
$ ( "#location_id" ) . val ( sourceProduct . location _id ) ;
if ( sourceProduct . shopping _location _id != null )
{
Grocy . Components . ShoppingLocationPicker . SetId ( sourceProduct . shopping _location _id ) ;
}
$ ( "#min_stock_amount" ) . val ( sourceProduct . min _stock _amount ) ;
if ( BoolVal ( sourceProduct . cumulate _min _stock _amount _of _sub _products ) )
{
$ ( "#cumulate_min_stock_amount_of_sub_products" ) . prop ( "checked" , true ) ;
}
$ ( "#default_best_before_days" ) . val ( sourceProduct . default _best _before _days ) ;
$ ( "#default_best_before_days_after_open" ) . val ( sourceProduct . default _best _before _days _after _open ) ;
if ( sourceProduct . product _group _id != null )
{
$ ( "#product_group_id" ) . val ( sourceProduct . product _group _id ) ;
}
$ ( "#qu_id_stock" ) . val ( sourceProduct . qu _id _stock ) ;
$ ( "#qu_id_purchase" ) . val ( sourceProduct . qu _id _purchase ) ;
$ ( "#qu_factor_purchase_to_stock" ) . val ( sourceProduct . qu _factor _purchase _to _stock ) ;
if ( BoolVal ( sourceProduct . enable _tare _weight _handling ) )
{
$ ( "#enable_tare_weight_handling" ) . prop ( "checked" , true ) ;
}
$ ( "#tare_weight" ) . val ( sourceProduct . tare _weight ) ;
if ( BoolVal ( sourceProduct . not _check _stock _fulfillment _for _recipes ) )
{
$ ( "#not_check_stock_fulfillment_for_recipes" ) . prop ( "checked" , true ) ;
}
if ( sourceProduct . calories != null )
{
$ ( "#calories" ) . val ( sourceProduct . calories ) ;
}
$ ( "#default_best_before_days_after_freezing" ) . val ( sourceProduct . default _best _before _days _after _freezing ) ;
$ ( "#default_best_before_days_after_thawing" ) . val ( sourceProduct . default _best _before _days _after _thawing ) ;
$ ( "#quick_consume_amount" ) . val ( sourceProduct . quick _consume _amount ) ;
Grocy . FrontendHelpers . ValidateForm ( 'product-form' ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}