2019-04-22 22:16:35 +02:00
$ ( '#save-product-button' ) . on ( 'click' , function ( e )
2017-04-15 23:16:20 +02:00
{
e . preventDefault ( ) ;
2018-04-18 19:03:39 +02:00
var redirectDestination = U ( '/products' ) ;
2018-04-14 11:10:38 +02:00
var returnTo = GetUriParam ( 'returnto' ) ;
2017-04-20 22:01:14 +02:00
if ( returnTo !== undefined )
{
2019-05-03 20:22:48 +02:00
redirectDestination = U ( returnTo ) + '?createdproduct=' + encodeURIComponent ( $ ( '#name' ) . val ( ) ) ;
2017-04-20 22:01:14 +02:00
}
2019-09-15 16:40:54 +02:00
if ( Grocy . ProductEditFormRedirectUri !== undefined )
{
redirectDestination = Grocy . ProductEditFormRedirectUri ;
}
2019-01-26 14:17:02 +01:00
var jsonData = $ ( '#product-form' ) . serializeJSON ( { checkboxUncheckedValue : "0" } ) ;
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 ;
}
if ( Grocy . DeleteProductPictureOnSave )
{
jsonData . picture _file _name = null ;
2018-09-30 23:22:17 +02:00
}
2017-04-15 23:16:20 +02:00
if ( Grocy . EditMode === 'create' )
{
2019-01-19 14:51:51 +01:00
Grocy . Api . Post ( 'objects/products' , jsonData ,
2019-04-22 22:16:35 +02:00
function ( result )
2017-04-15 23:16:20 +02:00
{
2019-04-22 22:16:35 +02:00
Grocy . EditObjectId = result . created _object _id ;
Grocy . Components . UserfieldsForm . Save ( function ( )
2018-09-30 23:22:17 +02:00
{
2019-04-22 22:16:35 +02:00
if ( jsonData . hasOwnProperty ( "picture_file_name" ) && ! Grocy . DeleteProductPictureOnSave )
{
Grocy . Api . UploadFile ( $ ( "#product-picture" ) [ 0 ] . files [ 0 ] , 'productpictures' , jsonData . picture _file _name ,
function ( result )
{
2019-10-19 10:33:46 +02:00
if ( GetUriParam ( "closeAfterCreation" ) !== undefined )
{
window . close ( ) ;
}
else if ( redirectDestination == "reload" )
2019-09-15 16:40:54 +02:00
{
window . location . reload ( ) ;
}
else
{
2019-09-16 09:35:20 +02:00
window . location . href = redirectDestination . replace ( "editobjectid" , Grocy . EditObjectId ) ; ;
2019-09-15 16:40:54 +02:00
}
2019-04-22 22:16:35 +02:00
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( "product-form" ) ;
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
}
) ;
}
else
{
2019-10-19 10:33:46 +02:00
if ( GetUriParam ( "closeAfterCreation" ) !== undefined )
{
window . close ( ) ;
}
else if ( redirectDestination == "reload" )
2019-09-15 16:40:54 +02:00
{
window . location . reload ( ) ;
}
else
{
2019-09-16 09:35:20 +02:00
window . location . href = redirectDestination . replace ( "editobjectid" , Grocy . EditObjectId ) ; ;
2019-09-15 16:40:54 +02:00
}
2019-04-22 22:16:35 +02:00
}
} ) ;
2017-04-15 23:16:20 +02:00
} ,
2018-10-01 20:20:50 +02:00
function ( xhr )
2017-04-15 23:16:20 +02:00
{
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
}
) ;
}
else
{
2018-10-01 20:20:50 +02:00
if ( Grocy . DeleteProductPictureOnSave )
{
2019-01-19 14:51:51 +01:00
Grocy . Api . DeleteFile ( Grocy . ProductPictureFileName , 'productpictures' , { } ,
2018-10-01 20:20:50 +02:00
function ( result )
{
// Nothing to do
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "product-form" ) ;
2018-10-01 20:20:50 +02:00
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
}
) ;
} ;
2019-01-19 14:51:51 +01:00
Grocy . Api . Put ( 'objects/products/' + Grocy . EditObjectId , jsonData ,
2017-04-15 23:16:20 +02:00
function ( result )
{
2019-04-22 22:16:35 +02:00
Grocy . Components . UserfieldsForm . Save ( function ( )
2018-09-30 23:22:17 +02:00
{
2019-04-22 22:16:35 +02:00
if ( jsonData . hasOwnProperty ( "picture_file_name" ) && ! Grocy . DeleteProductPictureOnSave )
{
Grocy . Api . UploadFile ( $ ( "#product-picture" ) [ 0 ] . files [ 0 ] , 'productpictures' , jsonData . picture _file _name ,
function ( result )
{
2019-10-19 10:33:46 +02:00
if ( GetUriParam ( "closeAfterCreation" ) !== undefined )
{
window . close ( ) ;
}
else if ( redirectDestination == "reload" )
2019-09-15 16:40:54 +02:00
{
window . location . reload ( ) ;
}
else
{
2019-09-16 09:35:20 +02:00
window . location . href = redirectDestination . replace ( "editobjectid" , Grocy . EditObjectId ) ; ;
2019-09-15 16:40:54 +02:00
}
2019-04-22 22:16:35 +02:00
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( "product-form" ) ;
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
}
) ;
}
else
{
2019-10-19 10:33:46 +02:00
if ( GetUriParam ( "closeAfterCreation" ) !== undefined )
{
window . close ( ) ;
}
else if ( redirectDestination == "reload" )
2019-09-15 16:40:54 +02:00
{
window . location . reload ( ) ;
}
else
{
2019-09-16 09:35:20 +02:00
window . location . href = redirectDestination . replace ( "editobjectid" , Grocy . EditObjectId ) ; ;
2019-09-15 16:40:54 +02:00
}
2019-04-22 22:16:35 +02:00
}
} ) ;
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
}
) ;
}
} ) ;
2018-04-16 19:11:32 +02:00
$ ( '#barcode-taginput' ) . tagsManager ( {
'hiddenTagListName' : 'barcode' ,
2018-10-03 13:34:38 +02:00
'tagsContainer' : '#barcode-taginput-container' ,
'tagClass' : 'badge badge-secondary'
2018-04-16 19:11:32 +02:00
} ) ;
2017-04-18 21:15:45 +02:00
2018-04-16 19:11:32 +02:00
if ( Grocy . EditMode === 'edit' )
{
2019-01-19 14:51:51 +01:00
Grocy . Api . Get ( 'objects/products/' + Grocy . EditObjectId ,
2018-04-16 19:11:32 +02:00
function ( product )
{
if ( product . barcode !== null && product . barcode . length > 0 )
2017-04-18 21:15:45 +02:00
{
2018-04-16 19:11:32 +02:00
product . barcode . split ( ',' ) . forEach ( function ( item )
2017-04-18 21:15:45 +02:00
{
2018-04-16 19:11:32 +02:00
$ ( '#barcode-taginput' ) . tagsManager ( 'pushTag' , item ) ;
} ) ;
2017-04-18 21:15:45 +02:00
}
2018-04-16 19:11:32 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2017-04-18 21:15:45 +02:00
2018-04-16 19:11:32 +02:00
var prefillName = GetUriParam ( 'prefillname' ) ;
if ( prefillName !== undefined )
{
$ ( '#name' ) . val ( prefillName ) ;
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
2018-04-16 19:11:32 +02:00
var prefillBarcode = GetUriParam ( 'prefillbarcode' ) ;
if ( prefillBarcode !== undefined )
{
$ ( '#barcode-taginput' ) . tagsManager ( 'pushTag' , prefillBarcode ) ;
$ ( '#name' ) . focus ( ) ;
}
2017-04-17 16:51:49 +02:00
2019-06-09 09:21:56 +02:00
$ ( "#barcode-taginput" ) . on ( "blur" , function ( e )
{
$ ( "#barcode-taginput" ) . tagsManager ( "pushTag" , $ ( "#barcode-taginput" ) . val ( ) ) ;
} ) ;
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-09-15 16:40:54 +02:00
$ ( "#qu-conversion-headline-info" ) . text ( _ _t ( '1 %s is the same as...' , $ ( "#qu_id_stock option:selected" ) . text ( ) ) ) ;
quConversionsTable . column ( 4 ) . search ( "from_qu_id xx" + $ ( "#qu_id_stock" ) . val ( ) . toString ( ) + "xx" ) . draw ( ) ;
2019-03-05 17:51:50 +01:00
$ ( "#tare_weight_qu_info" ) . text ( $ ( "#qu_id_stock option:selected" ) . text ( ) ) ;
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" ) ;
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" ) ;
}
} ) ;
$ ( '#product-form select' ) . change ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'product-form' ) ;
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" ) ;
}
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" ) ;
} ) ;
2019-04-06 15:27:00 +02:00
$ ( "#allow_partial_units_in_stock" ) . on ( "click" , function ( )
{
if ( this . checked )
{
2019-09-17 20:02:27 +02:00
$ ( "#min_stock_amount" ) . attr ( "min" , "0.0000" ) ;
$ ( "#min_stock_amount" ) . attr ( "step" , "0.0001" ) ;
$ ( "#qu_factor_purchase_to_stock" ) . attr ( "min" , "0.0001" ) ;
$ ( "#qu_factor_purchase_to_stock" ) . attr ( "step" , "0.0001" ) ;
$ ( "#qu_factor_purchase_to_stock" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'This cannot be lower than %1$s and must be a valid number with max. %2$s decimal places' , 0.0001 . toLocaleString ( undefined , { minimumFractionDigits : 0 , maximumFractionDigits : 4 } ) , '4' ) ) ;
2019-04-06 15:27:00 +02:00
}
else
{
$ ( "#min_stock_amount" ) . attr ( "min" , "0" ) ;
$ ( "#min_stock_amount" ) . attr ( "step" , "1" ) ;
2019-09-17 20:02:27 +02:00
$ ( "#qu_factor_purchase_to_stock" ) . attr ( "min" , "1" ) ;
$ ( "#qu_factor_purchase_to_stock" ) . attr ( "step" , "1" ) ;
$ ( "#qu_factor_purchase_to_stock" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'This cannot be lower than %1$s and must be a valid number with max. %2$s decimal places' , '1' , '0' ) ) ;
2019-04-06 15:27:00 +02:00
}
Grocy . FrontendHelpers . ValidateForm ( "product-form" ) ;
} ) ;
2018-10-01 20:20:50 +02:00
Grocy . DeleteProductPictureOnSave = false ;
$ ( '#delete-current-product-picture-button' ) . on ( 'click' , function ( e )
{
Grocy . DeleteProductPictureOnSave = true ;
$ ( "#current-product-picture" ) . addClass ( "d-none" ) ;
$ ( "#delete-current-product-picture-on-save-hint" ) . removeClass ( "d-none" ) ;
$ ( "#delete-current-product-picture-button" ) . addClass ( "disabled" ) ;
} ) ;
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 ) ;
}
}
2019-09-15 16:40:54 +02:00
var quConversionsTable = $ ( '#qu-conversions-table' ) . DataTable ( {
'order' : [ [ 1 , 'asc' ] ] ,
"orderFixed" : [ [ 3 , 'asc' ] ] ,
'columnDefs' : [
{ 'orderable' : false , 'targets' : 0 } ,
2020-01-03 14:18:56 +01:00
{ 'searchable' : false , "targets" : 0 } ,
2019-09-15 16:40:54 +02:00
{ 'visible' : false , 'targets' : 3 }
] ,
'rowGroup' : {
dataSrc : 3
}
} ) ;
$ ( '#qu-conversions-table tbody' ) . removeClass ( "d-none" ) ;
quConversionsTable . columns . adjust ( ) . draw ( ) ;
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
// Click twice to trigger on-click but not change the actual checked state
$ ( "#allow_partial_units_in_stock" ) . click ( ) ;
$ ( "#allow_partial_units_in_stock" ) . click ( ) ;
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 )
{
Grocy . Api . Delete ( 'objects/quantity_unit_conversions/' + objectId , { } ,
function ( result )
{
Grocy . ProductEditFormRedirectUri = "reload" ;
$ ( '#save-product-button' ) . click ( ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
}
} ) ;
} ) ;
$ ( document ) . on ( 'click' , '.qu-conversion-edit-button' , function ( e )
{
var id = $ ( e . currentTarget ) . attr ( 'data-qu-conversion-id' ) ;
2019-09-16 09:35:20 +02:00
Grocy . ProductEditFormRedirectUri = U ( "/quantityunitconversion/" + id . toString ( ) + "?product=editobjectid" ) ;
2019-09-15 16:40:54 +02:00
$ ( '#save-product-button' ) . click ( ) ;
} ) ;
$ ( "#qu-conversion-add-button" ) . on ( "click" , function ( e )
{
2019-09-16 09:35:20 +02:00
Grocy . ProductEditFormRedirectUri = U ( "/quantityunitconversion/new?product=editobjectid" ) ;
2019-09-15 16:40:54 +02:00
$ ( '#save-product-button' ) . click ( ) ;
} ) ;