2017-04-20 17:10:21 +02:00
$ ( '#save-inventory-button' ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
var jsonForm = $ ( '#inventory-form' ) . serializeJSON ( ) ;
2018-04-18 19:03:39 +02:00
Grocy . Api . Get ( 'stock/get-product-details/' + jsonForm . product _id ,
2017-04-20 17:10:21 +02:00
function ( productDetails )
{
2018-07-12 19:12:31 +02:00
Grocy . Api . Get ( 'stock/inventory-product/' + jsonForm . product _id + '/' + jsonForm . new _amount + '?bestbeforedate=' + Grocy . Components . DateTimePicker . GetValue ( ) ,
2017-04-20 22:01:14 +02:00
function ( result )
{
2018-04-14 11:10:38 +02:00
var addBarcode = GetUriParam ( 'addbarcodetoselection' ) ;
2017-04-20 22:01:14 +02:00
if ( addBarcode !== undefined )
{
var existingBarcodes = productDetails . product . barcode || '' ;
if ( existingBarcodes . length === 0 )
{
productDetails . product . barcode = addBarcode ;
}
else
{
productDetails . product . barcode += ',' + addBarcode ;
}
2018-04-18 19:03:39 +02:00
Grocy . Api . Get ( 'edit-object/products/' + productDetails . product . id , productDetails . product ,
2017-04-20 22:01:14 +02:00
function ( result ) { } ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2018-10-20 14:09:19 +02:00
toastr . success ( L ( 'Stock amount of #1 is now #2 #3' , productDetails . product . name , jsonForm . new _amount , Pluralize ( jsonForm . new _amount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural ) ) ) ;
2017-04-20 17:10:21 +02:00
2017-04-20 22:01:14 +02:00
if ( addBarcode !== undefined )
{
2018-04-18 19:03:39 +02:00
window . location . href = U ( '/inventory' ) ;
2017-04-20 22:01:14 +02:00
}
else
{
2018-07-11 19:43:05 +02:00
$ ( '#inventory-change-info' ) . addClass ( 'd-none' ) ;
2017-04-20 22:01:14 +02:00
$ ( '#new_amount' ) . val ( '' ) ;
2018-07-12 19:12:31 +02:00
Grocy . Components . DateTimePicker . SetValue ( '' ) ;
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . SetValue ( '' ) ;
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'inventory-form' ) ;
2017-04-20 22:01:14 +02:00
}
2017-04-20 17:10:21 +02:00
} ,
2017-04-20 22:01:14 +02:00
function ( xhr )
{
2017-04-20 17:10:21 +02:00
console . error ( xhr ) ;
}
) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ) ;
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . GetPicker ( ) . on ( 'change' , function ( e )
2017-04-20 17:10:21 +02:00
{
var productId = $ ( e . target ) . val ( ) ;
if ( productId )
{
2018-04-14 11:10:38 +02:00
Grocy . Components . ProductCard . Refresh ( productId ) ;
2018-04-18 19:03:39 +02:00
Grocy . Api . Get ( 'stock/get-product-details/' + productId ,
2017-04-20 17:10:21 +02:00
function ( productDetails )
{
$ ( '#new_amount' ) . attr ( 'not-equal' , productDetails . stock _amount ) ;
$ ( '#new_amount_qu_unit' ) . text ( productDetails . quantity _unit _stock . name ) ;
2017-11-05 11:29:17 +01:00
$ ( '#new_amount' ) . focus ( ) ;
2017-04-20 17:10:21 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
} ) ;
2018-04-16 19:11:32 +02:00
$ ( '#new_amount' ) . val ( '' ) ;
2018-07-12 19:12:31 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'inventory-form' ) ;
2017-04-20 17:10:21 +02:00
2018-07-14 14:43:57 +02:00
if ( Grocy . Components . ProductPicker . InProductAddWorkflow ( ) === false )
{
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
}
else
{
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( 'change' ) ;
}
2018-04-16 19:11:32 +02:00
$ ( '#new_amount' ) . on ( 'focus' , function ( e )
{
2018-07-14 14:43:57 +02:00
if ( Grocy . Components . ProductPicker . GetValue ( ) . length === 0 )
2017-04-20 22:01:14 +02:00
{
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
2018-04-16 19:11:32 +02:00
}
else
{
$ ( this ) . select ( ) ;
}
} ) ;
2017-04-20 22:01:14 +02:00
2018-07-11 19:43:05 +02:00
$ ( '#inventory-form input' ) . keyup ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'inventory-form' ) ;
} ) ;
2018-04-16 19:11:32 +02:00
$ ( '#inventory-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 ( 'inventory-form' ) . checkValidity ( ) === false ) //There is at least one validation error
2017-04-20 22:01:14 +02:00
{
2018-04-16 19:11:32 +02:00
return false ;
2017-04-20 22:01:14 +02:00
}
2018-07-11 19:43:05 +02:00
else
{
$ ( '#save-inventory-button' ) . click ( ) ;
}
2017-04-20 22:01:14 +02:00
}
2018-04-16 19:11:32 +02:00
} ) ;
2017-04-20 22:01:14 +02:00
2017-11-10 22:45:53 +01:00
$ ( '#new_amount' ) . on ( 'keypress' , function ( e )
{
$ ( '#new_amount' ) . trigger ( 'change' ) ;
} ) ;
2018-07-12 19:12:31 +02:00
Grocy . Components . DateTimePicker . GetInputElement ( ) . on ( 'change' , function ( e )
2017-04-20 17:10:21 +02:00
{
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'inventory-form' ) ;
2017-04-20 17:10:21 +02:00
} ) ;
2018-07-12 19:12:31 +02:00
Grocy . Components . DateTimePicker . GetInputElement ( ) . on ( 'keypress' , function ( e )
2017-04-20 17:10:21 +02:00
{
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'inventory-form' ) ;
2017-04-20 17:10:21 +02:00
} ) ;
2018-07-11 19:43:05 +02:00
$ ( '#new_amount' ) . on ( 'keyup' , function ( e )
2017-04-20 17:10:21 +02:00
{
2018-07-14 14:43:57 +02:00
var productId = Grocy . Components . ProductPicker . GetValue ( ) ;
2018-07-12 19:12:31 +02:00
var newAmount = parseInt ( $ ( '#new_amount' ) . val ( ) ) ;
2017-04-20 17:10:21 +02:00
if ( productId )
{
2018-04-18 19:03:39 +02:00
Grocy . Api . Get ( 'stock/get-product-details/' + productId ,
2017-04-20 17:10:21 +02:00
function ( productDetails )
{
2018-07-12 19:12:31 +02:00
var productStockAmount = parseInt ( productDetails . stock _amount || '0' ) ;
2017-04-20 17:10:21 +02:00
if ( newAmount > productStockAmount )
{
var amountToAdd = newAmount - productDetails . stock _amount ;
2018-04-21 19:18:00 +02:00
$ ( '#inventory-change-info' ) . text ( L ( 'This means #1 will be added to stock' , amountToAdd . toString ( ) + ' ' + productDetails . quantity _unit _stock . name ) ) ;
2018-07-12 19:12:31 +02:00
$ ( '#inventory-change-info' ) . removeClass ( 'd-none' ) ;
Grocy . Components . DateTimePicker . GetInputElement ( ) . attr ( 'required' , '' ) ;
2017-04-20 17:10:21 +02:00
}
else if ( newAmount < productStockAmount )
{
var amountToRemove = productStockAmount - newAmount ;
2018-04-21 19:18:00 +02:00
$ ( '#inventory-change-info' ) . text ( L ( 'This means #1 will be removed from stock' , amountToRemove . toString ( ) + ' ' + productDetails . quantity _unit _stock . name ) ) ;
2018-07-12 19:12:31 +02:00
$ ( '#inventory-change-info' ) . removeClass ( 'd-none' ) ;
Grocy . Components . DateTimePicker . GetInputElement ( ) . removeAttr ( 'required' ) ;
2017-04-20 17:10:21 +02:00
}
else
{
2018-07-11 19:43:05 +02:00
$ ( '#inventory-change-info' ) . addClass ( 'd-none' ) ;
2017-04-20 17:10:21 +02:00
}
2017-04-20 22:01:14 +02:00
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'inventory-form' ) ;
2017-04-20 17:10:21 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
} ) ;