2017-04-15 23:16:20 +02:00
$ ( '#save-product-button' ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
2017-04-20 22:01:14 +02:00
var redirectDestination = '/products' ;
var returnTo = Grocy . GetUriParam ( 'returnto' ) ;
if ( returnTo !== undefined )
{
redirectDestination = returnTo + '?createdproduct=' + encodeURIComponent ( $ ( '#name' ) . val ( ) ) ;
}
2017-04-15 23:16:20 +02:00
if ( Grocy . EditMode === 'create' )
{
Grocy . PostJson ( '/api/add-object/products' , $ ( '#product-form' ) . serializeJSON ( ) ,
function ( result )
{
2017-04-20 22:01:14 +02:00
window . location . href = redirectDestination ;
2017-04-15 23:16:20 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
else
{
Grocy . PostJson ( '/api/edit-object/products/' + Grocy . EditObjectId , $ ( '#product-form' ) . serializeJSON ( ) ,
function ( result )
{
2017-04-20 22:01:14 +02:00
window . location . href = redirectDestination ;
2017-04-15 23:16:20 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
} ) ;
$ ( function ( )
{
2017-04-18 21:15:45 +02:00
$ ( '#barcode-taginput' ) . tagsManager ( {
'hiddenTagListName' : 'barcode' ,
'tagsContainer' : '#barcode-taginput-container'
} ) ;
if ( Grocy . EditMode === 'edit' )
{
Grocy . FetchJson ( '/api/get-object/products/' + Grocy . EditObjectId ,
function ( product )
{
2017-04-18 23:04:26 +02:00
if ( product . barcode !== null && product . barcode . length > 0 )
2017-04-18 21:15:45 +02:00
{
product . barcode . split ( ',' ) . forEach ( function ( item )
{
$ ( '#barcode-taginput' ) . tagsManager ( 'pushTag' , item ) ;
} ) ;
}
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2017-04-18 19:54:51 +02:00
$ ( '#qu_factor_purchase_to_stock' ) . trigger ( 'change' ) ;
2017-04-15 23:16:20 +02:00
$ ( '#name' ) . focus ( ) ;
2017-04-16 23:11:03 +02:00
$ ( '#product-form' ) . validator ( ) ;
$ ( '#product-form' ) . validator ( 'validate' ) ;
2017-04-20 22:01:14 +02:00
var prefillName = Grocy . GetUriParam ( 'prefillname' ) ;
if ( prefillName !== undefined )
{
$ ( '#name' ) . val ( prefillName ) ;
$ ( '#name' ) . focus ( ) ;
}
2017-04-15 23:16:20 +02:00
} ) ;
2017-04-17 16:51:49 +02:00
2017-04-18 19:54:51 +02:00
$ ( '.input-group-qu' ) . on ( 'change' , function ( e )
{
var factor = $ ( '#qu_factor_purchase_to_stock' ) . val ( ) ;
if ( factor > 1 )
{
$ ( '#qu-conversion-info' ) . text ( 'This means 1 ' + $ ( "#qu_id_purchase option:selected" ) . text ( ) + ' purchased will be converted into ' + ( 1 * factor ) . toString ( ) + ' ' + $ ( "#qu_id_stock option:selected" ) . text ( ) + ' in stock.' ) ;
$ ( '#qu-conversion-info' ) . show ( ) ;
}
else
{
$ ( '#qu-conversion-info' ) . hide ( ) ;
}
} ) ;