2018-10-01 20:20:50 +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 )
{
redirectDestination = returnTo + '?createdproduct=' + encodeURIComponent ( $ ( '#name' ) . val ( ) ) ;
}
2018-09-30 23:22:17 +02:00
var jsonData = $ ( '#product-form' ) . serializeJSON ( ) ;
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' )
{
2018-09-30 23:22:17 +02:00
Grocy . Api . Post ( 'add-object/products' , jsonData ,
2018-10-01 20:20:50 +02:00
function ( result )
2017-04-15 23:16:20 +02:00
{
2018-10-01 20:20:50 +02:00
if ( jsonData . hasOwnProperty ( "picture_file_name" ) && ! Grocy . DeleteProductPictureOnSave )
2018-09-30 23:22:17 +02:00
{
2018-10-01 20:20:50 +02:00
Grocy . Api . UploadFile ( $ ( "#product-picture" ) [ 0 ] . files [ 0 ] , 'productpictures' , jsonData . picture _file _name ,
function ( result )
2018-09-30 23:22:17 +02:00
{
window . location . href = redirectDestination ;
} ,
2018-10-01 20:20:50 +02:00
function ( xhr )
2018-09-30 23:22:17 +02:00
{
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
}
) ;
}
else
{
window . location . href = redirectDestination ;
}
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-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 )
{
Grocy . Api . DeleteFile ( Grocy . ProductPictureFileName , 'productpictures' ,
function ( result )
{
// Nothing to do
} ,
function ( xhr )
{
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
}
) ;
} ;
2018-09-30 23:22:17 +02:00
Grocy . Api . Post ( 'edit-object/products/' + Grocy . EditObjectId , jsonData ,
2017-04-15 23:16:20 +02:00
function ( result )
{
2018-10-01 20:20:50 +02:00
if ( jsonData . hasOwnProperty ( "picture_file_name" ) && ! Grocy . DeleteProductPictureOnSave )
2018-09-30 23:22:17 +02:00
{
2018-10-01 20:20:50 +02:00
Grocy . Api . UploadFile ( $ ( "#product-picture" ) [ 0 ] . files [ 0 ] , 'productpictures' , jsonData . picture _file _name ,
2018-09-30 23:22:17 +02:00
function ( result )
{
window . location . href = redirectDestination ;
} ,
function ( xhr )
{
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
}
) ;
}
else
{
window . location . href = redirectDestination ;
}
2017-04-15 23:16:20 +02:00
} ,
function ( xhr )
{
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' )
{
2018-04-18 19:03:39 +02:00
Grocy . Api . Get ( 'get-object/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
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
if ( quIdPurchase != quIdStock )
{
$ ( '#qu_factor_purchase_to_stock' ) . attr ( "min" , 2 ) ;
$ ( "#qu_factor_purchase_to_stock" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( L ( 'The amount cannot be lower than #1' , '2' ) ) ;
}
else
{
$ ( '#qu_factor_purchase_to_stock' ) . attr ( "min" , 1 ) ;
$ ( "#qu_factor_purchase_to_stock" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( L ( 'The amount cannot be lower than #1' , '1' ) ) ;
}
2017-04-18 19:54:51 +02:00
if ( factor > 1 )
{
2018-04-16 19:11:32 +02:00
$ ( '#qu-conversion-info' ) . text ( L ( 'This means 1 #1 purchased will be converted into #2 #3 in stock' , $ ( "#qu_id_purchase option:selected" ) . text ( ) , ( 1 * factor ) . toString ( ) , $ ( "#qu_id_stock option:selected" ) . text ( ) ) ) ;
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
Grocy . FrontendHelpers . ValidateForm ( 'product-form' ) ;
2018-07-11 19:43:05 +02:00
} ) ;
$ ( '#product-form input' ) . keyup ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'product-form' ) ;
} ) ;
$ ( '#product-form input' ) . keydown ( function ( event )
{
if ( event . keyCode === 13 ) //Enter
{
2018-09-29 13:41:56 +02:00
event . preventDefault ( ) ;
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
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-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' ) ;