2022-04-06 21:27:47 +02:00
var CurrentProductDetails ;
$ ( '#save-inventory-button' ) . on ( 'click' , function ( e )
2017-04-20 17:10:21 +02:00
{
e . preventDefault ( ) ;
2022-03-26 10:34:00 +01:00
if ( ! Grocy . FrontendHelpers . ValidateForm ( "inventory-form" , true ) )
{
return ;
}
2020-12-31 13:45:14 +01:00
if ( $ ( ".combobox-menu-visible" ) . length )
{
return ;
}
2017-04-20 17:10:21 +02:00
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 = "" ;
2023-02-06 20:22:10 +01:00
if ( jsonForm . price )
2019-05-03 22:11:20 +02:00
{
2023-02-06 20:22:10 +01:00
price = Number . parseFloat ( jsonForm . price * $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) . toFixed ( Grocy . UserSettings . stock _decimal _places _prices _input ) ;
2019-05-03 22:11:20 +02:00
}
2020-08-30 12:18:16 +02:00
var jsonData = { } ;
2020-11-09 21:51:55 +01:00
jsonData . new _amount = jsonForm . amount ;
2019-01-19 14:51:51 +01:00
jsonData . best _before _date = Grocy . Components . DateTimePicker . GetValue ( ) ;
2022-03-30 17:32:53 +02:00
jsonData . note = jsonForm . note ;
2021-12-09 18:32:59 +01:00
jsonData . stock _label _type = jsonForm . stock _label _type ;
2020-08-30 12:18:16 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _PRICE _TRACKING )
{
2020-04-02 08:34:43 +02:00
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-10-29 11:04:34 -05:00
if ( Grocy . UserSettings . show _purchased _date _on _purchase )
{
jsonData . purchased _date = Grocy . Components . DateTimePicker2 . GetValue ( ) ;
}
2020-08-29 16:41:27 +02: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 ;
2020-08-29 16:41:27 +02:00
2020-11-14 11:05:36 +01:00
if ( GetUriParam ( "flow" ) === "InplaceAddBarcodeToExistingProduct" )
2017-04-20 22:01:14 +02:00
{
2020-08-17 14:47:33 -05:00
var jsonDataBarcode = { } ;
2020-11-14 11:05:36 +01:00
jsonDataBarcode . barcode = GetUriParam ( "barcode" ) ;
2020-08-17 14:47:33 -05:00
jsonDataBarcode . product _id = jsonForm . product _id ;
jsonDataBarcode . shopping _location _id = jsonForm . shopping _location _id ;
2023-01-04 19:56:55 +01:00
jsonDataBarcode . note = jsonForm . note ;
2017-04-20 22:01:14 +02:00
2020-08-17 14:47:33 -05:00
Grocy . Api . Post ( 'objects/product_barcodes' , jsonDataBarcode ,
2019-05-03 18:29:09 +02:00
function ( result )
{
2020-11-14 11:05:36 +01:00
$ ( "#flow-info-InplaceAddBarcodeToExistingProduct" ) . addClass ( "d-none" ) ;
2019-05-03 18:29:09 +02:00
$ ( '#barcode-lookup-disabled-hint' ) . addClass ( 'd-none' ) ;
2020-11-09 21:30:22 +01:00
$ ( '#barcode-lookup-hint' ) . removeClass ( 'd-none' ) ;
2020-08-30 12:18:16 +02:00
window . history . replaceState ( { } , document . title , U ( "/inventory" ) ) ;
2019-05-03 18:29:09 +02:00
} ,
2017-04-20 22:01:14 +02:00
function ( xhr )
{
2020-08-17 14:47:33 -05:00
Grocy . FrontendHelpers . EndUiBusy ( "inventory-form" ) ;
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response ) ;
2017-04-20 22:01:14 +02:00
}
) ;
}
2023-02-06 20:22:10 +01:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _LABEL _PRINTER && Number . parseFloat ( $ ( "#display_amount" ) . attr ( "data-estimated-booking-amount" ) ) > 0 )
2021-12-09 18:32:59 +01:00
{
if ( Grocy . Webhooks . labelprinter !== undefined )
{
if ( jsonForm . stock _label _type == 1 ) // Single label
{
var webhookData = { } ;
webhookData . product = productDetails . product . name ;
webhookData . grocycode = 'grcy:p:' + jsonForm . product _id + ":" + result [ 0 ] . stock _id ;
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _BEST _BEFORE _DATE _TRACKING )
{
webhookData . due _date = _ _t ( 'DD' ) + ': ' + result [ 0 ] . best _before _date ;
}
Grocy . FrontendHelpers . RunWebhook ( Grocy . Webhooks . labelprinter , webhookData ) ;
}
else if ( jsonForm . stock _label _type == 2 ) // Label per unit
{
Grocy . Api . Get ( 'stock/transactions/' + result [ 0 ] . transaction _id ,
function ( stockEntries )
{
stockEntries . forEach ( stockEntry =>
{
var webhookData = { } ;
webhookData . product = productDetails . product . name ;
webhookData . grocycode = 'grcy:p:' + jsonForm . product _id + ":" + stockEntry . stock _id ;
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _BEST _BEFORE _DATE _TRACKING )
{
webhookData . due _date = _ _t ( 'DD' ) + ': ' + result [ 0 ] . best _before _date ;
}
Grocy . FrontendHelpers . RunWebhook ( Grocy . Webhooks . labelprinter , webhookData ) ;
} ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
}
}
2022-03-30 17:32:53 +02:00
Grocy . EditObjectId = result [ 0 ] . transaction _id ;
2019-09-19 21:10:36 +02:00
Grocy . Api . Get ( 'stock/products/' + jsonForm . product _id ,
function ( result )
{
2022-04-04 20:10:29 +02: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 , true ) ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse [ 0 ] . transaction _id + '\')"><i class="fa-solid fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ;
2020-08-29 16:41:27 +02:00
2019-09-20 13:37:53 +02:00
if ( GetUriParam ( "embedded" ) !== undefined )
{
2022-03-30 17:32:53 +02:00
Grocy . Components . UserfieldsForm . Save ( function ( )
{
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 ) ;
} ) ;
2019-09-20 13:37:53 +02:00
}
else
{
2022-03-30 17:32:53 +02:00
Grocy . Components . UserfieldsForm . Save ( function ( )
2020-08-30 12:18:16 +02:00
{
2022-03-30 17:32:53 +02:00
Grocy . FrontendHelpers . EndUiBusy ( "inventory-form" ) ;
toastr . success ( successMessage ) ;
Grocy . Components . ProductPicker . FinishFlow ( ) ;
Grocy . Components . ProductAmountPicker . Reset ( ) ;
$ ( '#inventory-change-info' ) . addClass ( 'd-none' ) ;
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
$ ( "#display_amount" ) . attr ( "min" , "0" ) ;
$ ( '#display_amount' ) . val ( '' ) ;
$ ( '#display_amount' ) . removeAttr ( "data-not-equal" ) ;
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
$ ( '#price' ) . val ( '' ) ;
$ ( '#note' ) . val ( '' ) ;
2022-04-06 21:27:47 +02:00
$ ( '#price-hint' ) . text ( "" ) ;
2022-03-30 17:32:53 +02:00
Grocy . Components . DateTimePicker . Clear ( ) ;
Grocy . Components . ProductPicker . SetValue ( '' ) ;
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _PRICE _TRACKING )
{
Grocy . Components . ShoppingLocationPicker . SetValue ( '' ) ;
}
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
Grocy . Components . ProductCard . Refresh ( jsonForm . product _id ) ;
Grocy . Components . UserfieldsForm . Clear ( ) ;
2021-12-09 18:32:59 +01:00
2022-03-30 17:32:53 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _LABEL _PRINTER )
{
$ ( "#stock_label_type" ) . val ( 0 ) ;
}
2021-12-09 18:32:59 +01:00
2022-03-30 17:32:53 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'inventory-form' ) ;
} ) ;
2019-09-20 13:37:53 +02:00
}
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 )
{
2022-04-06 21:27:47 +02:00
CurrentProductDetails = productDetails ;
2020-11-09 21:51:55 +01:00
Grocy . Components . ProductAmountPicker . Reload ( productDetails . product . id , productDetails . quantity _unit _stock . id ) ;
2020-11-09 22:15:25 +01:00
Grocy . Components . ProductAmountPicker . SetQuantityUnit ( productDetails . quantity _unit _stock . id ) ;
2020-11-09 21:51:55 +01:00
2020-11-12 21:35:10 +01:00
$ ( '#display_amount' ) . attr ( "data-stock-amount" , productDetails . stock _amount )
$ ( '#display_amount' ) . attr ( 'data-not-equal' , productDetails . stock _amount * $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) ;
2017-04-20 17:10:21 +02:00
2019-03-05 17:51:50 +01:00
if ( productDetails . product . enable _tare _weight _handling == 1 )
{
2020-11-09 21:51:55 +01:00
$ ( "#display_amount" ) . attr ( "min" , productDetails . product . tare _weight ) ;
2019-03-05 17:51:50 +01:00
$ ( "#tare-weight-handling-info" ) . removeClass ( "d-none" ) ;
}
else
{
2020-12-23 19:56:37 +01:00
$ ( "#display_amount" ) . attr ( "min" , "0" ) ;
2019-03-05 17:51:50 +01:00
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
2019-01-26 14:17:02 +01:00
}
2023-02-06 20:22:10 +01:00
if ( productDetails . last _price )
2022-04-06 21:27:47 +02:00
{
2023-02-06 20:22:10 +01:00
$ ( '#price' ) . val ( productDetails . last _price / $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) ;
2022-04-06 21:27:47 +02:00
}
else
{
$ ( '#price' ) . val ( "" ) ;
}
2020-11-15 14:15:09 +01:00
RefreshLocaleNumberInput ( ) ;
2020-08-30 12:18:16 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _PRICE _TRACKING )
{
2020-04-02 08:34:43 +02:00
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
2020-12-07 19:48:33 +01:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _BEST _BEFORE _DATE _TRACKING )
{
if ( productDetails . product . default _best _before _days . toString ( ) !== '0' )
{
if ( productDetails . product . default _best _before _days == - 1 )
{
if ( ! $ ( "#datetimepicker-shortcut" ) . is ( ":checked" ) )
{
$ ( "#datetimepicker-shortcut" ) . click ( ) ;
}
}
else
{
Grocy . Components . DateTimePicker . SetValue ( moment ( ) . add ( productDetails . product . default _best _before _days , 'days' ) . format ( 'YYYY-MM-DD' ) ) ;
}
}
}
2021-12-09 18:32:59 +01:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _LABEL _PRINTER )
{
$ ( "#stock_label_type" ) . val ( productDetails . product . default _stock _label _type ) ;
}
2021-01-04 21:59:19 +01:00
if ( document . getElementById ( "product_id" ) . getAttribute ( "barcode" ) != "null" )
{
Grocy . Api . Get ( 'objects/product_barcodes?query[]=barcode=' + document . getElementById ( "product_id" ) . getAttribute ( "barcode" ) ,
function ( barcodeResult )
{
if ( barcodeResult != null )
{
var barcode = barcodeResult [ 0 ] ;
if ( barcode != null )
{
2022-12-29 11:21:33 +01:00
if ( barcode . amount != null )
2021-01-04 21:59:19 +01:00
{
$ ( "#display_amount" ) . val ( barcode . amount ) ;
$ ( "#display_amount" ) . select ( ) ;
}
2022-12-29 11:21:33 +01:00
if ( barcode . qu _id != null )
2021-01-04 21:59:19 +01:00
{
Grocy . Components . ProductAmountPicker . SetQuantityUnit ( barcode . qu _id ) ;
}
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
Grocy . FrontendHelpers . ValidateForm ( 'inventory-form' ) ;
RefreshLocaleNumberInput ( ) ;
}
}
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2020-11-09 21:51:55 +01:00
$ ( '#display_amount' ) . val ( productDetails . stock _amount ) ;
2020-11-15 14:15:09 +01:00
RefreshLocaleNumberInput ( ) ;
2020-11-12 22:47:00 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2020-11-09 21:51:55 +01:00
$ ( '#display_amount' ) . focus ( ) ;
$ ( '#display_amount' ) . trigger ( 'keyup' ) ;
2022-04-06 21:27:47 +02:00
refreshPriceHint ( ) ;
2017-04-20 17:10:21 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
} ) ;
2022-04-06 21:27:47 +02:00
function refreshPriceHint ( )
{
if ( $ ( '#amount' ) . val ( ) == 0 || $ ( '#price' ) . val ( ) == 0 )
{
$ ( '#price-hint' ) . text ( "" ) ;
return ;
}
if ( $ ( "#qu_id" ) . attr ( "data-destination-qu-name" ) != $ ( "#qu_id option:selected" ) . text ( ) )
{
var amount = $ ( '#display_amount' ) . val ( ) ;
if ( BoolVal ( CurrentProductDetails . product . enable _tare _weight _handling ) )
{
2023-02-06 20:22:10 +01:00
amount -= CurrentProductDetails . product . tare _weight ;
2022-04-06 21:27:47 +02:00
}
2023-02-06 20:22:10 +01:00
var price = Number . parseFloat ( $ ( '#price' ) . val ( ) * $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) . toFixed ( Grocy . UserSettings . stock _decimal _places _prices _input ) ;
2022-06-04 14:09:35 +02:00
$ ( '#price-hint' ) . text ( _ _t ( 'means %1$s per %2$s' , price . toLocaleString ( undefined , { style : "currency" , currency : Grocy . Currency , minimumFractionDigits : Grocy . UserSettings . stock _decimal _places _prices _display , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _prices _display } ) , $ ( "#qu_id" ) . attr ( "data-destination-qu-name" ) ) ) ;
2022-04-06 21:27:47 +02:00
}
else
{
$ ( '#price-hint' ) . text ( "" ) ;
}
} ;
2020-11-09 21:51:55 +01:00
$ ( '#display_amount' ) . val ( '' ) ;
2020-11-12 22:47:00 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2018-07-12 19:12:31 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'inventory-form' ) ;
2017-04-20 17:10:21 +02:00
2020-11-14 11:53:35 +01:00
if ( Grocy . Components . ProductPicker . InAnyFlow ( ) === false && GetUriParam ( "embedded" ) === undefined )
2018-07-14 14:43:57 +02:00
{
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
}
else
{
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( 'change' ) ;
2020-11-14 11:05:36 +01:00
if ( Grocy . Components . ProductPicker . InProductModifyWorkflow ( ) )
{
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
}
2018-07-14 14:43:57 +02:00
}
2020-11-09 21:51:55 +01:00
$ ( '#display_amount' ) . on ( 'focus' , function ( e )
2018-04-16 19:11:32 +02:00
{
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
2020-08-30 12:18:16 +02:00
$ ( '#inventory-form input' ) . keyup ( function ( event )
2018-07-11 19:43:05 +02:00
{
Grocy . FrontendHelpers . ValidateForm ( 'inventory-form' ) ;
} ) ;
2018-04-16 19:11:32 +02:00
$ ( '#inventory-form input' ) . keydown ( function ( event )
{
2022-03-30 18:00:28 +02:00
if ( event . keyCode === 13 ) // Enter
2018-04-16 19:11:32 +02:00
{
2018-09-29 13:41:56 +02:00
event . preventDefault ( ) ;
2019-01-19 00:37:21 -07:00
2022-03-30 18:00:28 +02:00
if ( ! Grocy . FrontendHelpers . ValidateForm ( 'inventory-form' ) )
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
2020-11-12 21:35:10 +01:00
$ ( '#qu_id' ) . on ( 'change' , function ( e )
2017-11-10 22:45:53 +01:00
{
2023-02-06 20:22:10 +01:00
$ ( '#display_amount' ) . attr ( 'data-not-equal' , Number . parseFloat ( $ ( '#display_amount' ) . attr ( 'data-stock-amount' ) ) * Number . parseFloat ( $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) ) ;
2020-11-12 21:35:10 +01:00
Grocy . FrontendHelpers . ValidateForm ( 'inventory-form' ) ;
2017-11-10 22:45:53 +01:00
} ) ;
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
} ) ;
2022-04-06 21:27:47 +02:00
$ ( '#price' ) . on ( 'focus' , function ( e )
{
$ ( this ) . select ( ) ;
} ) ;
2020-11-09 21:51:55 +01:00
$ ( '#display_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 ( ) ;
2023-02-06 20:22:10 +01:00
var newAmount = Number . parseFloat ( $ ( '#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 )
{
2023-02-06 20:42:11 +01:00
var productStockAmount = productDetails . stock _amount || 0 ;
2020-08-29 16:41:27 +02:00
2023-02-06 20:42:11 +01:00
var containerWeight = 0.0 ;
if ( productDetails . product . enable _tare _weight _handling == 1 )
{
containerWeight = productDetails . product . tare _weight ;
}
2019-09-19 17:46:52 +02:00
2023-02-06 20:42:11 +01:00
var estimatedBookingAmount = ( newAmount - productStockAmount - containerWeight ) . toFixed ( Grocy . UserSettings . stock _decimal _places _amounts ) ;
$ ( "#display_amount" ) . attr ( "data-estimated-booking-amount" , estimatedBookingAmount ) ;
estimatedBookingAmount = Math . abs ( estimatedBookingAmount ) ;
$ ( '#inventory-change-info' ) . removeClass ( 'd-none' ) ;
2017-04-20 22:01:14 +02:00
2023-02-06 20:42:11 +01:00
if ( productDetails . product . enable _tare _weight _handling == 1 && newAmount < containerWeight )
{
$ ( '#inventory-change-info' ) . addClass ( 'd-none' ) ;
}
else if ( newAmount > productStockAmount + containerWeight )
{
$ ( '#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 , true ) ) ) ;
Grocy . Components . DateTimePicker . GetInputElement ( ) . attr ( 'required' , '' ) ;
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _LOCATION _TRACKING )
{
Grocy . Components . LocationPicker . GetInputElement ( ) . attr ( 'required' , '' ) ;
}
}
else if ( newAmount < productStockAmount + containerWeight )
{
$ ( '#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 , true ) ) ) ;
Grocy . Components . DateTimePicker . GetInputElement ( ) . removeAttr ( 'required' ) ;
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _LOCATION _TRACKING )
{
Grocy . Components . LocationPicker . GetInputElement ( ) . removeAttr ( 'required' ) ;
}
}
else if ( newAmount == productStockAmount )
{
$ ( '#inventory-change-info' ) . addClass ( 'd-none' ) ;
}
2023-02-06 20:22:10 +01:00
2023-02-06 20:42:11 +01:00
if ( ! Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _BEST _BEFORE _DATE _TRACKING )
{
Grocy . Components . DateTimePicker . GetInputElement ( ) . removeAttr ( 'required' ) ;
}
2023-02-06 20:22:10 +01:00
2023-02-06 20:42:11 +01:00
refreshPriceHint ( ) ;
Grocy . FrontendHelpers . ValidateForm ( 'inventory-form' ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
2017-04-20 17:10:21 +02:00
) ;
}
} ) ;
2018-10-27 17:26:00 +02:00
function UndoStockBooking ( bookingId )
{
2020-08-30 12:18:16 +02: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 )
{
2020-08-30 12:18:16 +02:00
Grocy . Api . Post ( 'stock/transactions/' + transactionId . toString ( ) + '/undo' , { } ,
function ( result )
2019-12-19 12:48:36 -06:00
{
toastr . success ( _ _t ( "Transaction successfully undone" ) ) ;
} ,
2020-08-30 12:18:16 +02:00
function ( xhr )
2019-12-19 12:48:36 -06:00
{
console . error ( xhr ) ;
}
) ;
} ;
2020-12-23 19:56:37 +01:00
$ ( "#display_amount" ) . attr ( "min" , "0" ) ;