2020-11-14 23:15:34 +01:00
var CurrentProductDetails ;
$ ( '#save-purchase-button' ) . on ( 'click' , function ( e )
2017-04-15 23:16:20 +02:00
{
e . preventDefault ( ) ;
2022-03-26 10:34:00 +01:00
if ( ! Grocy . FrontendHelpers . ValidateForm ( "purchase-form" , true ) )
{
return ;
}
2020-12-31 13:45:14 +01:00
if ( $ ( ".combobox-menu-visible" ) . length )
{
return ;
}
if ( $ ( ".combobox-menu-visible" ) . length )
{
return ;
}
2017-04-15 23:16:20 +02:00
var jsonForm = $ ( '#purchase-form' ) . serializeJSON ( ) ;
2020-11-09 19:25:46 +01:00
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . BeginUiBusy ( "purchase-form" ) ;
2017-04-15 23:16:20 +02:00
2019-01-19 14:51:51 +01:00
Grocy . Api . Get ( 'stock/products/' + jsonForm . product _id ,
2018-10-27 17:26:00 +02:00
function ( productDetails )
2017-04-15 23:16:20 +02:00
{
2020-10-20 13:08:54 -05:00
var jsonData = { } ;
2020-11-09 19:25:46 +01:00
jsonData . amount = jsonForm . amount ;
2022-03-30 17:32:53 +02:00
jsonData . note = jsonForm . note ;
2021-11-14 15:37:10 +01:00
jsonData . stock _label _type = jsonForm . stock _label _type ;
2017-04-15 23:16:20 +02:00
2020-10-20 13:08:54 -05:00
if ( ! Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _PRICE _TRACKING )
{
jsonData . price = 0 ;
2020-11-10 18:11:33 +01:00
}
else
2018-07-26 20:27:38 +02:00
{
2023-02-06 20:22:10 +01:00
var amount = Number . parseFloat ( jsonForm . display _amount ) ;
2020-12-17 16:50:15 +01:00
if ( BoolVal ( productDetails . product . enable _tare _weight _handling ) )
{
2023-02-06 20:22:10 +01:00
amount -= productDetails . product . tare _weight ;
2020-12-17 16:50:15 +01:00
}
2019-09-27 13:50:16 +02:00
2023-02-06 20:22:10 +01:00
var price = Number . parseFloat ( jsonForm . price * $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) . toFixed ( Grocy . UserSettings . stock _decimal _places _prices _input ) ;
2019-09-27 13:50:16 +02:00
if ( $ ( "input[name='price-type']:checked" ) . val ( ) == "total-price" )
{
2023-02-06 20:22:10 +01:00
price = ( price / amount ) . toFixed ( Grocy . UserSettings . stock _decimal _places _prices _input ) ;
2019-09-27 13:50:16 +02:00
}
2020-12-17 16:50:15 +01:00
2020-10-20 13:08:54 -05:00
jsonData . price = price ;
2019-09-27 13:50:16 +02:00
}
2021-01-01 20:00:24 +01:00
if ( BoolVal ( Grocy . UserSettings . show _purchased _date _on _purchase ) )
2020-10-17 11:03:47 +02:00
{
jsonData . purchased _date = Grocy . Components . DateTimePicker2 . GetValue ( ) ;
}
2020-09-14 11:15:11 +02:00
if ( Grocy . Components . DateTimePicker )
{
jsonData . best _before _date = Grocy . Components . DateTimePicker . GetValue ( ) ;
}
2020-09-14 11:20:29 +02:00
else
{
2020-09-14 11:15:11 +02:00
jsonData . best _before _date = null ;
}
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 ( ) ;
}
2019-01-19 14:51:51 +01:00
Grocy . Api . Post ( 'stock/products/' + jsonForm . product _id + '/add' , jsonData ,
2017-04-20 22:01:14 +02:00
function ( result )
{
2020-11-17 19:11:02 +01:00
if ( $ ( "#purchase-form" ) . hasAttr ( "data-used-barcode" ) )
{
2020-11-17 22:06:52 +01:00
Grocy . Api . Put ( 'objects/product_barcodes/' + $ ( "#purchase-form" ) . attr ( "data-used-barcode" ) , { last _price : $ ( "#price" ) . val ( ) } ,
2020-11-17 19:11:02 +01:00
function ( result )
{ } ,
function ( xhr )
{ }
) ;
}
2020-01-26 15:35:01 +01:00
if ( BoolVal ( Grocy . UserSettings . scan _mode _purchase _enabled ) )
{
Grocy . UISound . Success ( ) ;
}
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 ;
2022-12-29 11:21:33 +01:00
jsonDataBarcode . qu _id = jsonForm . qu _id ;
jsonDataBarcode . amount = jsonForm . display _amount ;
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 ( "/purchase" ) ) ;
2019-05-03 18:29:09 +02:00
} ,
2017-04-20 22:01:14 +02:00
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "purchase-form" ) ;
2020-08-17 14:47:33 -05:00
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
var amountMessage = Number . parseFloat ( jsonForm . amount ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) ;
2020-12-21 18:27:12 +01:00
if ( BoolVal ( productDetails . product . enable _tare _weight _handling ) )
{
2023-02-06 20:22:10 +01:00
amountMessage = Number . parseFloat ( jsonForm . amount ) - productDetails . stock _amount - productDetails . product . tare _weight ;
2020-12-21 18:27:12 +01:00
}
2022-04-04 20:10:29 +02:00
var successMessage = _ _t ( 'Added %1$s of %2$s to stock' , amountMessage + " " + _ _n ( amountMessage , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural , true ) , productDetails . product . name ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + result [ 0 ] . transaction _id + '\')"><i class="fa-solid fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ;
2017-04-16 23:11:03 +02:00
2021-07-13 19:29:23 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _LABEL _PRINTER )
2021-06-12 17:21:12 +02:00
{
if ( Grocy . Webhooks . labelprinter !== undefined )
{
2021-11-14 15:26:38 +01:00
if ( jsonForm . stock _label _type == 1 ) // Single label
2021-06-12 17:21:12 +02:00
{
2021-11-14 15:26:38 +01:00
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 )
2021-06-12 17:21:12 +02:00
{
2021-11-14 15:37:10 +01:00
webhookData . due _date = _ _t ( 'DD' ) + ': ' + result [ 0 ] . best _before _date ;
2021-06-12 17:21:12 +02:00
}
2021-11-14 15:26:38 +01:00
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 )
{
2021-11-14 15:37:10 +01:00
webhookData . due _date = _ _t ( 'DD' ) + ': ' + result [ 0 ] . best _before _date ;
2021-11-14 15:26:38 +01:00
}
Grocy . FrontendHelpers . RunWebhook ( Grocy . Webhooks . labelprinter , webhookData ) ;
} ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
2021-06-12 17:21:12 +02:00
}
}
}
2022-03-30 17:32:53 +02:00
Grocy . EditObjectId = result [ 0 ] . transaction _id ;
2019-09-20 13:37:53 +02:00
if ( GetUriParam ( "embedded" ) !== undefined )
2018-11-14 21:40:17 +01:00
{
2022-03-30 17:32:53 +02:00
Grocy . Components . UserfieldsForm . Save ( function ( )
{
2025-01-10 17:15:09 +01:00
window . top . postMessage ( WindowMessageBag ( "BroadcastMessage" , WindowMessageBag ( "ProductChanged" , jsonForm . product _id ) ) , Grocy . BaseUrl ) ;
2022-03-30 17:32:53 +02:00
window . parent . postMessage ( WindowMessageBag ( "AfterItemAdded" , GetUriParam ( "listitemid" ) ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "ShowSuccessMessage" , successMessage ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "Ready" ) , Grocy . BaseUrl ) ;
2025-01-10 17:15:09 +01:00
window . parent . postMessage ( WindowMessageBag ( "CloseLastModal" ) , Grocy . BaseUrl ) ;
2022-03-30 17:32:53 +02:00
} ) ;
2018-11-14 21:40:17 +01:00
}
2017-04-20 22:01:14 +02:00
else
{
2022-03-30 17:32:53 +02:00
Grocy . Components . UserfieldsForm . Save ( function ( )
2020-11-14 23:15:34 +01:00
{
2022-03-30 17:32:53 +02:00
Grocy . FrontendHelpers . EndUiBusy ( "purchase-form" ) ;
toastr . success ( successMessage ) ;
Grocy . Components . ProductPicker . FinishFlow ( ) ;
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _BEST _BEFORE _DATE _TRACKING && BoolVal ( Grocy . UserSettings . show _warning _on _purchase _when _due _date _is _earlier _than _next ) )
2020-11-14 23:15:34 +01:00
{
2022-03-30 17:32:53 +02:00
if ( moment ( jsonData . best _before _date ) . isBefore ( CurrentProductDetails . next _due _date ) )
{
toastr . warning ( _ _t ( "This is due earlier than already in-stock items" ) ) ;
}
2020-11-14 23:15:34 +01:00
}
2022-03-30 17:32:53 +02:00
Grocy . Components . ProductAmountPicker . Reset ( ) ;
$ ( "#purchase-form" ) . removeAttr ( "data-used-barcode" ) ;
$ ( "#display_amount" ) . attr ( "min" , Grocy . DefaultMinAmount ) ;
2023-02-06 20:22:10 +01:00
$ ( '#display_amount' ) . val ( Grocy . UserSettings . stock _default _purchase _amount ) ;
2022-03-30 17:32:53 +02:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
$ ( '#price' ) . val ( '' ) ;
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _LOCATION _TRACKING )
{
Grocy . Components . LocationPicker . Clear ( ) ;
}
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _BEST _BEFORE _DATE _TRACKING )
{
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 ) ;
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _LABEL _PRINTER )
{
$ ( "#stock_label_type" ) . val ( 0 ) ;
}
2020-11-10 18:11:33 +01:00
2022-03-30 17:32:53 +02:00
$ ( '#price-hint' ) . text ( "" ) ;
$ ( '#note' ) . val ( "" ) ;
var priceTypeUnitPrice = $ ( "#price-type-unit-price" ) ;
var priceTypeUnitPriceLabel = $ ( "[for=" + priceTypeUnitPrice . attr ( "id" ) + "]" ) ;
priceTypeUnitPriceLabel . text ( _ _t ( "Unit price" ) ) ;
Grocy . Components . UserfieldsForm . Clear ( ) ;
2020-11-10 18:11:33 +01:00
2022-03-30 17:32:53 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
} ) ;
2017-04-20 22:01:14 +02:00
}
2017-04-15 23:16:20 +02:00
} ,
2017-04-20 22:01:14 +02:00
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "purchase-form" ) ;
2017-04-15 23:16:20 +02:00
console . error ( xhr ) ;
}
) ;
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "purchase-form" ) ;
2017-04-15 23:16:20 +02:00
console . error ( xhr ) ;
}
) ;
} ) ;
2019-09-20 13:37:53 +02:00
if ( Grocy . Components . ProductPicker !== undefined )
2017-04-15 23:16:20 +02:00
{
2019-09-20 13:37:53 +02:00
Grocy . Components . ProductPicker . GetPicker ( ) . on ( 'change' , function ( e )
2017-04-16 23:11:03 +02:00
{
2020-01-26 15:35:01 +01:00
if ( BoolVal ( Grocy . UserSettings . scan _mode _purchase _enabled ) )
{
Grocy . UISound . BarcodeScannerBeep ( ) ;
}
2019-09-20 13:37:53 +02:00
var productId = $ ( e . target ) . val ( ) ;
2019-03-09 17:00:57 +01:00
2019-09-20 13:37:53 +02:00
if ( productId )
{
Grocy . Components . ProductCard . Refresh ( productId ) ;
2019-01-26 14:17:02 +01:00
2019-09-20 13:37:53 +02:00
Grocy . Api . Get ( 'stock/products/' + productId ,
2020-01-26 15:35:01 +01:00
function ( productDetails )
2019-03-05 17:51:50 +01:00
{
2020-11-14 23:15:34 +01:00
CurrentProductDetails = productDetails ;
2020-11-09 19:25:46 +01:00
Grocy . Components . ProductAmountPicker . Reload ( productDetails . product . id , productDetails . quantity _unit _stock . id ) ;
2022-02-20 19:33:55 +01:00
if ( productDetails . product . enable _tare _weight _handling == 1 )
{
Grocy . Components . ProductAmountPicker . SetQuantityUnit ( productDetails . quantity _unit _stock . id ) ;
$ ( "#qu_id" ) . attr ( "disabled" , "" ) ;
}
else
{
Grocy . Components . ProductAmountPicker . SetQuantityUnit ( productDetails . default _quantity _unit _purchase . id ) ;
}
2023-02-06 20:22:10 +01:00
$ ( '#display_amount' ) . val ( Grocy . UserSettings . stock _default _purchase _amount ) ;
2020-11-12 22:47:00 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2020-08-29 16:41:27 +02:00
2020-11-12 21:35:10 +01:00
if ( GetUriParam ( "flow" ) === "shoppinglistitemtostock" )
{
2020-11-13 17:30:57 +01:00
Grocy . Components . ProductAmountPicker . SetQuantityUnit ( GetUriParam ( "quId" ) ) ;
2023-02-06 20:22:10 +01:00
$ ( '#display_amount' ) . val ( Number . parseFloat ( GetUriParam ( "amount" ) * $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) ) ;
2020-11-12 21:35:10 +01:00
}
2020-11-12 22:47:00 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2020-08-30 12:18:16 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _PRICE _TRACKING )
{
2020-11-10 18:11:33 +01:00
if ( productDetails . last _shopping _location _id != null )
2020-04-02 08:34:43 +02:00
{
Grocy . Components . ShoppingLocationPicker . SetId ( productDetails . last _shopping _location _id ) ;
}
else
{
Grocy . Components . ShoppingLocationPicker . SetId ( productDetails . default _shopping _location _id ) ;
}
2020-03-27 13:27:40 -05:00
}
2019-09-20 13:37:53 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _LOCATION _TRACKING )
{
Grocy . Components . LocationPicker . SetId ( productDetails . location . id ) ;
}
2019-03-05 17:51:50 +01:00
2020-11-17 19:11:02 +01:00
if ( productDetails . last _price == null || productDetails . last _price == 0 )
2020-11-15 14:15:09 +01:00
{
$ ( "#price" ) . val ( "" )
}
else
{
2023-04-24 18:17:39 +02:00
$ ( '#price' ) . val ( ( productDetails . last _price / Number . parseFloat ( $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) ) . toFixed ( Grocy . UserSettings . stock _decimal _places _prices _display ) ) ;
2020-11-15 14:15:09 +01:00
}
2020-10-15 12:55:58 -05:00
2020-04-22 10:38:24 -05:00
var priceTypeUnitPrice = $ ( "#price-type-unit-price" ) ;
var priceTypeUnitPriceLabel = $ ( "[for=" + priceTypeUnitPrice . attr ( "id" ) + "]" ) ;
2020-11-10 18:11:33 +01:00
priceTypeUnitPriceLabel . text ( $ ( "#qu_id option:selected" ) . text ( ) + " " + _ _t ( "price" ) ) ;
2020-04-22 10:38:24 -05:00
2023-04-24 18:17:39 +02:00
RefreshPriceHint ( ) ;
2020-04-22 10:38:24 -05:00
2019-09-20 13:37:53 +02:00
if ( productDetails . product . enable _tare _weight _handling == 1 )
{
2023-02-06 20:22:10 +01:00
var minAmount = productDetails . product . tare _weight / $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) + productDetails . stock _amount ;
2020-11-09 19:25:46 +01:00
$ ( "#display_amount" ) . attr ( "min" , minAmount ) ;
2019-09-20 13:37:53 +02:00
$ ( "#tare-weight-handling-info" ) . removeClass ( "d-none" ) ;
2019-09-19 17:46:52 +02:00
}
else
{
2020-12-23 19:56:37 +01:00
$ ( "#display_amount" ) . attr ( "min" , Grocy . DefaultMinAmount ) ;
2019-09-20 13:37:53 +02:00
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
}
2021-11-13 11:41:04 -05:00
PrefillBestBeforeDate ( productDetails . product , productDetails . location ) ;
2020-01-26 15:35:01 +01:00
2021-07-13 19:29:23 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _LABEL _PRINTER )
2021-06-12 17:21:12 +02:00
{
2021-11-14 15:26:38 +01:00
$ ( "#stock_label_type" ) . val ( productDetails . product . default _stock _label _type ) ;
2023-05-22 21:23:19 +02:00
$ ( "#stock_label_type" ) . trigger ( "change" ) ;
2021-06-12 17:21:12 +02:00
}
2025-01-10 20:26:37 +01:00
setTimeout ( function ( )
{
$ ( '#display_amount' ) . focus ( ) ;
2025-01-11 15:41:04 +01:00
} , 500 ) ;
2020-04-22 10:38:24 -05:00
2020-02-02 13:01:31 +01:00
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
2022-03-30 18:00:28 +02:00
if ( GetUriParam ( "flow" ) === "shoppinglistitemtostock" && BoolVal ( Grocy . UserSettings . shopping _list _to _stock _workflow _auto _submit _when _prefilled ) && Grocy . FrontendHelpers . ValidateForm ( "purchase-form" ) )
2020-02-02 13:01:31 +01:00
{
$ ( "#save-purchase-button" ) . click ( ) ;
}
2020-11-17 22:06:52 +01:00
RefreshLocaleNumberInput ( ) ;
2020-11-10 18:11:33 +01:00
if ( document . getElementById ( "product_id" ) . getAttribute ( "barcode" ) != "null" )
{
2024-08-24 18:20:40 +02:00
Grocy . Api . Get ( 'objects/product_barcodes_view?query[]=barcode=' + document . getElementById ( "product_id" ) . getAttribute ( "barcode" ) ,
2020-11-10 18:11:33 +01:00
function ( barcodeResult )
{
2024-08-24 11:32:15 +02:00
if ( barcodeResult && barcodeResult . length > 0 )
2020-11-10 18:11:33 +01:00
{
var barcode = barcodeResult [ 0 ] ;
2020-11-17 19:11:02 +01:00
$ ( "#purchase-form" ) . attr ( "data-used-barcode" , barcode . id ) ;
2020-11-10 18:11:33 +01:00
2023-04-17 19:54:24 +02:00
if ( barcode )
2020-11-10 18:11:33 +01:00
{
2023-04-17 19:54:24 +02:00
if ( barcode . amount )
2020-11-12 21:35:10 +01:00
{
$ ( "#display_amount" ) . val ( barcode . amount ) ;
$ ( "#display_amount" ) . select ( ) ;
}
2023-04-17 19:54:24 +02:00
if ( barcode . qu _id )
2020-11-12 21:35:10 +01:00
{
Grocy . Components . ProductAmountPicker . SetQuantityUnit ( barcode . qu _id ) ;
}
2020-12-21 10:52:40 +01:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _PRICE _TRACKING && barcode . shopping _location _id != null )
2020-11-12 21:35:10 +01:00
{
Grocy . Components . ShoppingLocationPicker . SetId ( barcode . shopping _location _id ) ;
}
2023-02-06 20:22:10 +01:00
if ( barcode . last _price )
2020-11-17 19:11:02 +01:00
{
$ ( "#price" ) . val ( barcode . last _price ) ;
$ ( "#price-type-total-price" ) . click ( ) ;
}
2023-04-17 19:54:24 +02:00
if ( barcode . note )
{
$ ( "#note" ) . val ( barcode . note ) ;
}
2020-11-13 17:30:57 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2020-11-12 21:35:10 +01:00
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
2020-11-17 22:06:52 +01:00
RefreshLocaleNumberInput ( ) ;
2020-11-10 18:11:33 +01:00
}
}
2021-01-04 21:15:22 +01:00
ScanModeSubmit ( false ) ;
2020-11-10 18:11:33 +01:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2020-11-17 19:11:02 +01:00
else
{
$ ( "#purchase-form" ) . removeAttr ( "data-used-barcode" ) ;
2021-01-04 21:15:22 +01:00
ScanModeSubmit ( ) ;
2020-11-17 19:11:02 +01:00
}
2020-11-10 21:19:28 +01:00
$ ( '#display_amount' ) . trigger ( "keyup" ) ;
2019-09-20 13:37:53 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
2018-04-14 11:10:38 +02:00
}
2019-09-20 13:37:53 +02:00
) ;
}
} ) ;
}
2017-04-15 23:16:20 +02:00
2021-11-13 11:41:04 -05:00
function PrefillBestBeforeDate ( product , location )
{
2022-03-23 17:34:51 +01:00
if ( location == null )
{
location = { }
}
2021-11-13 11:41:04 -05:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _BEST _BEFORE _DATE _TRACKING )
{
var dueDays ;
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _PRODUCT _FREEZING && BoolVal ( location . is _freezer ) )
{
dueDays = product . default _best _before _days _after _freezing ;
}
else
{
dueDays = product . default _best _before _days ;
}
if ( dueDays != 0 )
{
if ( dueDays == - 1 )
{
if ( ! $ ( "#datetimepicker-shortcut" ) . is ( ":checked" ) )
{
$ ( "#datetimepicker-shortcut" ) . click ( ) ;
}
}
else
{
Grocy . Components . DateTimePicker . SetValue ( moment ( ) . add ( dueDays , 'days' ) . format ( 'YYYY-MM-DD' ) ) ;
}
}
}
}
2021-11-14 15:26:38 +01:00
if ( Grocy . Components . LocationPicker !== undefined )
2021-11-13 11:41:04 -05:00
{
2021-11-14 15:26:38 +01:00
Grocy . Components . LocationPicker . GetPicker ( ) . on ( 'change' , function ( e )
2021-11-13 11:41:04 -05:00
{
2021-11-14 15:26:38 +01:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _PRODUCT _FREEZING )
{
Grocy . Api . Get ( 'objects/locations/' + Grocy . Components . LocationPicker . GetValue ( ) ,
function ( location )
{
PrefillBestBeforeDate ( CurrentProductDetails . product , location ) ;
} ,
function ( xhr )
{ }
) ;
}
} ) ;
}
2021-11-13 11:41:04 -05:00
2023-02-06 20:22:10 +01:00
$ ( '#display_amount' ) . val ( Grocy . UserSettings . stock _default _purchase _amount ) ;
2020-11-15 14:15:09 +01:00
RefreshLocaleNumberInput ( ) ;
2020-11-12 22:47:00 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2018-07-12 19:12:31 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
2017-11-10 22:45:53 +01:00
2019-09-20 13:37:53 +02:00
if ( Grocy . Components . ProductPicker )
2018-07-14 14:43:57 +02:00
{
2020-11-14 11:53:35 +01:00
if ( Grocy . Components . ProductPicker . InAnyFlow ( ) === false && GetUriParam ( "embedded" ) === undefined )
2019-09-20 13:37:53 +02:00
{
2025-01-12 17:57:50 +01:00
setTimeout ( function ( )
{
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
} , 500 ) ;
2019-09-20 13:37:53 +02:00
}
else
{
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( 'change' ) ;
2020-11-14 11:05:36 +01:00
if ( Grocy . Components . ProductPicker . InProductModifyWorkflow ( ) )
{
2025-01-12 17:57:50 +01:00
setTimeout ( function ( )
{
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
} , 500 ) ;
2020-11-14 11:05:36 +01:00
}
2019-09-20 13:37:53 +02:00
}
2018-07-14 14:43:57 +02:00
}
2020-11-09 19:25:46 +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-11-10 22:45:53 +01:00
{
2025-01-12 17:57:50 +01:00
setTimeout ( function ( )
{
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
} , 500 ) ;
2018-04-16 19:11:32 +02:00
}
else
2017-04-17 16:51:49 +02:00
{
2018-04-16 19:11:32 +02:00
$ ( this ) . select ( ) ;
}
} ) ;
2017-04-20 22:01:14 +02:00
2020-11-08 15:09:10 +01:00
$ ( '#price' ) . on ( 'focus' , function ( e )
{
$ ( this ) . select ( ) ;
} ) ;
2018-07-11 19:43:05 +02:00
$ ( '#purchase-form input' ) . keyup ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
} ) ;
2018-04-16 19:11:32 +02:00
$ ( '#purchase-form input' ) . keydown ( function ( event )
{
2022-03-30 18:00:28 +02:00
if ( event . keyCode === 13 ) // Enter
2017-04-20 22:01:14 +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 ( 'purchase-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-purchase-button' ) . click ( ) ;
}
2018-04-16 19:11:32 +02:00
}
} ) ;
2017-04-20 22:01:14 +02:00
2019-09-20 13:37:53 +02:00
if ( Grocy . Components . DateTimePicker )
2017-04-17 16:51:49 +02:00
{
2019-09-20 13:37:53 +02:00
Grocy . Components . DateTimePicker . GetInputElement ( ) . on ( 'change' , function ( e )
{
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
} ) ;
2017-04-17 16:51:49 +02:00
2019-09-20 13:37:53 +02:00
Grocy . Components . DateTimePicker . GetInputElement ( ) . on ( 'keypress' , function ( e )
{
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
} ) ;
}
2018-07-11 19:43:05 +02:00
2020-11-10 20:57:49 +01:00
if ( Grocy . Components . DateTimePicker2 )
{
Grocy . Components . DateTimePicker2 . GetInputElement ( ) . on ( 'change' , function ( e )
{
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
} ) ;
Grocy . Components . DateTimePicker2 . GetInputElement ( ) . on ( 'keypress' , function ( e )
{
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
} ) ;
Grocy . Components . DateTimePicker2 . GetInputElement ( ) . trigger ( "input" ) ;
}
2020-11-13 17:43:28 +01:00
$ ( '#price' ) . on ( 'keyup' , function ( e )
2020-04-22 10:38:24 -05:00
{
2023-04-24 18:17:39 +02:00
RefreshPriceHint ( ) ;
2020-04-22 10:38:24 -05:00
} ) ;
$ ( '#price-type-unit-price' ) . on ( 'change' , function ( e )
{
2023-04-24 18:17:39 +02:00
RefreshPriceHint ( ) ;
2020-04-22 10:38:24 -05:00
} ) ;
$ ( '#price-type-total-price' ) . on ( 'change' , function ( e )
{
2023-04-24 18:17:39 +02:00
RefreshPriceHint ( ) ;
2020-04-22 10:38:24 -05:00
} ) ;
2020-11-09 19:25:46 +01:00
$ ( '#display_amount' ) . on ( 'change' , function ( e )
2020-08-17 14:47:33 -05:00
{
2023-04-24 18:17:39 +02:00
RefreshPriceHint ( ) ;
2020-08-17 14:47:33 -05:00
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
} ) ;
2023-04-24 18:17:39 +02:00
function RefreshPriceHint ( )
2020-04-22 10:38:24 -05:00
{
2020-11-10 18:11:33 +01:00
if ( $ ( '#amount' ) . val ( ) == 0 || $ ( '#price' ) . val ( ) == 0 )
2020-04-22 10:38:24 -05:00
{
$ ( '#price-hint' ) . text ( "" ) ;
return ;
}
2020-11-13 17:43:28 +01:00
if ( $ ( "input[name='price-type']:checked" ) . val ( ) == "total-price" || $ ( "#qu_id" ) . attr ( "data-destination-qu-name" ) != $ ( "#qu_id option:selected" ) . text ( ) )
2020-04-22 10:38:24 -05:00
{
2023-02-06 20:22:10 +01:00
var amount = Number . parseFloat ( $ ( '#display_amount' ) . val ( ) ) ;
2020-12-17 16:50:15 +01:00
if ( BoolVal ( CurrentProductDetails . product . enable _tare _weight _handling ) )
{
2023-02-06 20:22:10 +01:00
amount -= CurrentProductDetails . product . tare _weight ;
2020-12-17 16:50:15 +01:00
}
2023-04-24 18:17:39 +02:00
var price = Number . parseFloat ( $ ( '#price' ) . val ( ) * $ ( "#qu_id option:selected" ) . attr ( "data-qu-factor" ) ) . toFixed ( Grocy . UserSettings . stock _decimal _places _prices _display ) ;
2020-11-13 17:43:28 +01:00
if ( $ ( "input[name='price-type']:checked" ) . val ( ) == "total-price" )
{
2023-04-24 18:17:39 +02:00
price = ( price / amount ) . toFixed ( Grocy . UserSettings . stock _decimal _places _prices _display ) ;
2020-11-13 17:43:28 +01:00
}
2020-04-22 10:38:24 -05:00
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" ) ) ) ;
2020-04-22 10:38:24 -05:00
}
else
{
2020-11-10 18:11:33 +01:00
$ ( '#price-hint' ) . text ( "" ) ;
2020-04-22 10:38:24 -05: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" ) ) ;
2019-09-20 13:37:53 +02:00
Grocy . Api . Get ( 'stock/bookings/' + bookingId . toString ( ) ,
function ( result )
{
2025-01-10 17:15:09 +01:00
window . top . postMessage ( WindowMessageBag ( "BroadcastMessage" , WindowMessageBag ( "ProductChanged" , result . product _id ) ) , Grocy . BaseUrl ) ;
2019-09-20 13:37:53 +02:00
} ,
2020-08-30 12:18:16 +02:00
function ( xhr )
2019-09-20 13:37:53 +02:00
{
console . error ( xhr ) ;
}
) ;
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' , { } ,
2019-12-19 12:48:36 -06:00
function ( result )
{
toastr . success ( _ _t ( "Transaction successfully undone" ) ) ;
Grocy . Api . Get ( 'stock/transactions/' + transactionId . toString ( ) ,
function ( result )
{
2025-01-10 17:15:09 +01:00
window . top . postMessage ( WindowMessageBag ( "BroadcastMessage" , WindowMessageBag ( "ProductChanged" , result [ 0 ] . product _id ) ) , Grocy . BaseUrl ) ;
2019-12-19 12:48:36 -06:00
} ,
2020-08-30 12:18:16 +02:00
function ( xhr )
2019-12-19 12:48:36 -06:00
{
console . error ( xhr ) ;
}
) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ;
2020-01-26 15:35:01 +01:00
2020-02-03 21:21:42 +01:00
$ ( "#scan-mode" ) . on ( "change" , function ( e )
2020-01-26 15:35:01 +01:00
{
if ( $ ( this ) . prop ( "checked" ) )
{
Grocy . UISound . AskForPermission ( ) ;
}
} ) ;
2020-02-03 21:21:42 +01:00
$ ( "#scan-mode-button" ) . on ( "click" , function ( e )
{
$ ( "#scan-mode" ) . click ( ) ;
$ ( "#scan-mode-button" ) . toggleClass ( "btn-success" ) . toggleClass ( "btn-danger" ) ;
if ( $ ( "#scan-mode" ) . prop ( "checked" ) )
{
$ ( "#scan-mode-status" ) . text ( _ _t ( "on" ) ) ;
}
else
{
$ ( "#scan-mode-status" ) . text ( _ _t ( "off" ) ) ;
}
} ) ;
2020-11-10 18:11:33 +01:00
$ ( '#qu_id' ) . on ( 'change' , function ( e )
{
var priceTypeUnitPrice = $ ( "#price-type-unit-price" ) ;
var priceTypeUnitPriceLabel = $ ( "[for=" + priceTypeUnitPrice . attr ( "id" ) + "]" ) ;
priceTypeUnitPriceLabel . text ( $ ( "#qu_id option:selected" ) . text ( ) + " " + _ _t ( "price" ) ) ;
2023-04-24 18:17:39 +02:00
RefreshPriceHint ( ) ;
2020-11-10 18:11:33 +01:00
} ) ;
2021-01-04 21:15:22 +01:00
function ScanModeSubmit ( singleUnit = true )
{
if ( BoolVal ( Grocy . UserSettings . scan _mode _purchase _enabled ) )
{
if ( singleUnit )
{
$ ( "#display_amount" ) . val ( 1 ) ;
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
}
Grocy . FrontendHelpers . ValidateForm ( "purchase-form" ) ;
2022-03-30 18:00:28 +02:00
if ( Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) )
2021-01-04 21:15:22 +01:00
{
$ ( '#save-purchase-button' ) . click ( ) ;
}
else
{
toastr . warning ( _ _t ( "Scan mode is on but not all required fields could be populated automatically" ) ) ;
Grocy . UISound . Error ( ) ;
}
}
}
2023-05-22 21:23:19 +02:00
2023-05-22 22:33:36 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _LABEL _PRINTER )
2023-05-22 21:23:19 +02:00
{
2023-05-22 22:33:36 +02:00
$ ( "#stock_label_type, #amount" ) . on ( "change" , function ( e )
2023-05-22 21:23:19 +02:00
{
2023-05-22 22:33:36 +02:00
if ( $ ( "#stock_label_type" ) . val ( ) == 2 )
{
$ ( "#stock-entry-label-info" ) . text ( _ _n ( Number . parseFloat ( $ ( "#amount" ) . val ( ) ) , "This means 1 label will be printed" , "This means %1$s labels will be printed" ) ) ;
}
else
{
$ ( "#stock-entry-label-info" ) . text ( "" ) ;
}
} ) ;
}
2025-01-12 17:40:39 +01:00
2025-01-13 17:41:08 +01:00
if ( Grocy . Components . UserfieldsForm )
{
Grocy . Components . UserfieldsForm . Load ( ) ;
}