2017-04-20 17:10:21 +02:00
$ ( '#save-inventory-button' ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
var jsonForm = $ ( '#inventory-form' ) . serializeJSON ( ) ;
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . BeginUiBusy ( "inventory-form" ) ;
2017-04-20 17:10:21 +02:00
2019-01-19 14:51:51 +01:00
Grocy . Api . Get ( 'stock/products/' + jsonForm . product _id ,
2019-03-05 17:51:50 +01:00
function ( productDetails )
2017-04-20 17:10:21 +02:00
{
2019-05-03 22:11:20 +02:00
var price = "" ;
if ( ! jsonForm . price . toString ( ) . isEmpty ( ) )
{
price = parseFloat ( jsonForm . price ) . toFixed ( 2 ) ;
}
2019-01-19 14:51:51 +01:00
var jsonData = { } ;
jsonData . new _amount = jsonForm . new _amount ;
jsonData . best _before _date = Grocy . Components . DateTimePicker . GetValue ( ) ;
2020-04-02 08:34:43 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _PRICE _TRACKING ) {
jsonData . shopping _location _id = Grocy . Components . ShoppingLocationPicker . GetValue ( ) ;
}
2019-09-19 17:46:52 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _LOCATION _TRACKING )
{
jsonData . location _id = Grocy . Components . LocationPicker . GetValue ( ) ;
}
2020-01-31 21:27:51 +01:00
2019-05-03 22:11:20 +02:00
jsonData . price = price ;
2019-01-19 14:51:51 +01:00
2019-09-19 21:10:36 +02:00
var bookingResponse = null ;
2019-01-19 14:51:51 +01:00
Grocy . Api . Post ( 'stock/products/' + jsonForm . product _id + '/inventory' , jsonData ,
2017-04-20 22:01:14 +02:00
function ( result )
{
2019-09-19 21:10:36 +02:00
bookingResponse = 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 ;
}
2019-01-19 14:51:51 +01:00
Grocy . Api . Put ( 'objects/products/' + productDetails . product . id , productDetails . product ,
2019-05-03 18:29:09 +02:00
function ( result )
{
$ ( "#flow-info-addbarcodetoselection" ) . addClass ( "d-none" ) ;
$ ( '#barcode-lookup-disabled-hint' ) . addClass ( 'd-none' ) ;
window . history . replaceState ( { } , document . title , U ( "/inventory" ) ) ;
} ,
2017-04-20 22:01:14 +02:00
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2019-09-19 21:10:36 +02:00
Grocy . Api . Get ( 'stock/products/' + jsonForm . product _id ,
function ( result )
{
2019-12-19 12:48:36 -06:00
var successMessage = _ _t ( 'Stock amount of %1$s is now %2$s' , result . product . name , result . stock _amount + " " + _ _n ( result . stock _amount , result . quantity _unit _stock . name , result . quantity _unit _stock . name _plural ) ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse . transaction _id + '\')"><i class="fas fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ;
2019-09-20 13:37:53 +02:00
if ( GetUriParam ( "embedded" ) !== undefined )
{
window . parent . postMessage ( WindowMessageBag ( "ProductChanged" , jsonForm . product _id ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "ShowSuccessMessage" , successMessage ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
}
else
{
Grocy . FrontendHelpers . EndUiBusy ( "inventory-form" ) ;
toastr . success ( successMessage ) ;
2017-04-20 17:10:21 +02:00
2019-09-20 13:37:53 +02:00
$ ( '#inventory-change-info' ) . addClass ( 'd-none' ) ;
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
$ ( "#new_amount" ) . attr ( "min" , "0" ) ;
$ ( "#new_amount" ) . attr ( "step" , "1" ) ;
$ ( "#new_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %s' , '0' ) ) ;
$ ( '#new_amount' ) . val ( '' ) ;
$ ( '#new_amount_qu_unit' ) . text ( "" ) ;
$ ( '#price' ) . val ( '' ) ;
Grocy . Components . DateTimePicker . Clear ( ) ;
Grocy . Components . ProductPicker . SetValue ( '' ) ;
2020-04-02 08:34:43 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _PRICE _TRACKING ) {
Grocy . Components . ShoppingLocationPicker . SetValue ( '' ) ;
}
2019-09-20 13:37:53 +02:00
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
2020-01-17 11:13:43 -06:00
Grocy . Components . ProductCard . Refresh ( jsonForm . product _id ) ;
2019-09-20 13:37:53 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'inventory-form' ) ;
}
2019-09-19 21:10:36 +02:00
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( ) ;
console . error ( xhr ) ;
}
) ;
2017-04-20 17:10:21 +02:00
} ,
2017-04-20 22:01:14 +02:00
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "inventory-form" ) ;
2017-04-20 17:10:21 +02:00
console . error ( xhr ) ;
}
) ;
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "inventory-form" ) ;
2017-04-20 17:10:21 +02:00
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 ) ;
2019-01-19 14:51:51 +01:00
Grocy . Api . Get ( 'stock/products/' + productId ,
2017-04-20 17:10:21 +02:00
function ( productDetails )
{
2019-05-03 19:08:54 +02:00
$ ( '#new_amount' ) . attr ( 'data-not-equal' , productDetails . stock _amount ) ;
2017-04-20 17:10:21 +02:00
$ ( '#new_amount_qu_unit' ) . text ( productDetails . quantity _unit _stock . name ) ;
2019-01-26 14:17:02 +01:00
if ( productDetails . product . allow _partial _units _in _stock == 1 )
{
$ ( "#new_amount" ) . attr ( "min" , "0.01" ) ;
$ ( "#new_amount" ) . attr ( "step" , "0.01" ) ;
2019-05-05 14:13:50 +02:00
$ ( "#new_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %1$s or equal %2$s' , 0.01 . toLocaleString ( ) , productDetails . stock _amount . toLocaleString ( ) ) ) ;
2019-01-26 14:17:02 +01:00
}
else
{
2019-03-05 17:51:50 +01:00
$ ( "#new_amount" ) . attr ( "min" , "0" ) ;
2019-01-26 14:17:02 +01:00
$ ( "#new_amount" ) . attr ( "step" , "1" ) ;
2019-05-05 14:13:50 +02:00
$ ( "#new_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %1$s or equal %2$s' , '0' , productDetails . stock _amount . toLocaleString ( ) ) ) ;
2019-03-05 17:51:50 +01:00
}
if ( productDetails . product . enable _tare _weight _handling == 1 )
{
$ ( "#new_amount" ) . attr ( "min" , productDetails . product . tare _weight ) ;
2019-09-19 21:10:36 +02:00
$ ( "#new_amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %1$s or equal %2$s' , parseFloat ( productDetails . product . tare _weight ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : 2 } ) , ( parseFloat ( productDetails . stock _amount ) + parseFloat ( productDetails . product . tare _weight ) ) . toLocaleString ( ) ) ) ;
2019-03-05 17:51:50 +01:00
$ ( "#tare-weight-handling-info" ) . removeClass ( "d-none" ) ;
}
else
{
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
2019-01-26 14:17:02 +01:00
}
2019-05-03 22:11:20 +02:00
$ ( '#price' ) . val ( productDetails . last _price ) ;
2020-04-02 08:34:43 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _PRICE _TRACKING ) {
Grocy . Components . ShoppingLocationPicker . SetId ( productDetails . last _shopping _location _id ) ;
}
2019-09-19 17:46:52 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _LOCATION _TRACKING )
{
Grocy . Components . LocationPicker . SetId ( productDetails . location . id ) ;
}
2020-01-27 19:00:49 +01:00
$ ( '#new_amount' ) . val ( productDetails . stock _amount ) ;
2017-11-05 11:29:17 +01:00
$ ( '#new_amount' ) . focus ( ) ;
2020-01-27 19:00:49 +01:00
$ ( '#new_amount' ) . trigger ( 'keyup' ) ;
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 ( ) ;
2019-01-19 00:37:21 -07:00
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' ) ;
} ) ;
2019-01-19 00:37:21 -07:00
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 ( ) ) ;
2019-01-19 00:37:21 -07:00
2017-04-20 17:10:21 +02:00
if ( productId )
{
2019-01-19 14:51:51 +01:00
Grocy . Api . Get ( 'stock/products/' + productId ,
2017-04-20 17:10:21 +02:00
function ( productDetails )
{
2019-03-05 17:51:50 +01:00
var productStockAmount = parseFloat ( productDetails . stock _amount || parseFloat ( '0' ) ) ;
var containerWeight = parseFloat ( "0" ) ;
if ( productDetails . product . enable _tare _weight _handling == 1 )
{
containerWeight = parseFloat ( productDetails . product . tare _weight ) ;
}
2019-01-19 00:37:21 -07:00
2019-03-05 17:51:50 +01:00
var estimatedBookingAmount = Math . abs ( newAmount - productStockAmount - containerWeight ) ;
$ ( '#inventory-change-info' ) . removeClass ( 'd-none' ) ;
if ( productDetails . product . enable _tare _weight _handling == 1 && newAmount < containerWeight )
2017-04-20 17:10:21 +02:00
{
2019-03-05 17:51:50 +01:00
$ ( '#inventory-change-info' ) . addClass ( 'd-none' ) ;
2017-04-20 17:10:21 +02:00
}
2019-03-05 17:51:50 +01:00
else if ( newAmount > productStockAmount + containerWeight )
2017-04-20 17:10:21 +02:00
{
2019-05-01 20:19:18 +02:00
$ ( '#inventory-change-info' ) . text ( _ _t ( 'This means %s will be added to stock' , estimatedBookingAmount . toLocaleString ( ) + ' ' + _ _n ( estimatedBookingAmount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural ) ) ) ;
2019-03-05 17:51:50 +01:00
Grocy . Components . DateTimePicker . GetInputElement ( ) . attr ( 'required' , '' ) ;
2019-09-19 17:46:52 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _LOCATION _TRACKING )
{
Grocy . Components . LocationPicker . GetInputElement ( ) . attr ( 'required' , '' ) ;
}
2017-04-20 17:10:21 +02:00
}
2019-03-05 17:51:50 +01:00
else if ( newAmount < productStockAmount + containerWeight )
2017-04-20 17:10:21 +02:00
{
2019-05-01 20:19:18 +02:00
$ ( '#inventory-change-info' ) . text ( _ _t ( 'This means %s will be removed from stock' , estimatedBookingAmount . toLocaleString ( ) + ' ' + _ _n ( estimatedBookingAmount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural ) ) ) ;
2019-03-05 17:51:50 +01:00
Grocy . Components . DateTimePicker . GetInputElement ( ) . removeAttr ( 'required' ) ;
2019-09-19 17:46:52 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _LOCATION _TRACKING )
{
Grocy . Components . LocationPicker . GetInputElement ( ) . removeAttr ( 'required' ) ;
}
}
if ( ! Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _BEST _BEFORE _DATE _TRACKING )
{
Grocy . Components . DateTimePicker . GetInputElement ( ) . removeAttr ( 'required' ) ;
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 ) ;
}
) ;
}
} ) ;
2018-10-27 17:26:00 +02:00
function UndoStockBooking ( bookingId )
{
2019-01-21 22:13:42 +01:00
Grocy . Api . Post ( 'stock/bookings/' + bookingId . toString ( ) + '/undo' , { } ,
2018-10-27 17:26:00 +02:00
function ( result )
{
2019-05-01 20:19:18 +02:00
toastr . success ( _ _t ( "Booking successfully undone" ) ) ;
2018-10-27 17:26:00 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ;
2019-12-19 12:48:36 -06:00
function UndoStockTransaction ( transactionId )
{
Grocy . Api . Post ( 'stock/transactions/' + transactionId . toString ( ) + '/undo' , { } ,
function ( result )
{
toastr . success ( _ _t ( "Transaction successfully undone" ) ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ;