2017-04-15 23:16:20 +02:00
$ ( '#save-purchase-button' ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
var jsonForm = $ ( '#purchase-form' ) . serializeJSON ( ) ;
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
{
2017-04-20 17:10:21 +02:00
var amount = jsonForm . amount * productDetails . product . qu _factor _purchase _to _stock ;
2017-04-15 23:16:20 +02:00
2018-07-26 20:27:38 +02:00
var price = "" ;
if ( ! jsonForm . price . toString ( ) . isEmpty ( ) )
{
price = parseFloat ( jsonForm . price ) . toFixed ( 2 ) ;
2019-09-27 13:50:16 +02:00
if ( $ ( "input[name='price-type']:checked" ) . val ( ) == "total-price" )
{
2020-04-13 14:52:13 +02:00
price = price / jsonForm . amount ;
2019-09-27 13:50:16 +02:00
}
}
if ( ! Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _PRICE _TRACKING )
{
price = 0 ;
2018-07-26 20:27:38 +02:00
}
2019-01-19 14:51:51 +01:00
var jsonData = { } ;
jsonData . amount = 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-01-19 14:51:51 +01:00
jsonData . price = price ;
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-01-26 15:35:01 +01:00
if ( BoolVal ( Grocy . UserSettings . scan _mode _purchase _enabled ) )
{
Grocy . UISound . Success ( ) ;
}
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 ( "/purchase" ) ) ;
} ,
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-20 22:01:14 +02:00
console . error ( xhr ) ;
}
) ;
}
2019-12-19 12:48:36 -06:00
var successMessage = _ _t ( 'Added %1$s of %2$s to stock' , result . amount + " " + _ _n ( result . amount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural ) , productDetails . product . name ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + result . transaction _id + '\')"><i class="fas fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ;
2017-04-16 23:11:03 +02:00
2019-09-20 13:37:53 +02:00
if ( GetUriParam ( "embedded" ) !== undefined )
2018-11-14 21:40:17 +01:00
{
2019-09-20 13:37:53 +02:00
window . parent . postMessage ( WindowMessageBag ( "ProductChanged" , jsonForm . product _id ) , Grocy . BaseUrl ) ;
2018-11-17 14:50:52 +01: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 ) ;
2019-09-20 13:37:53 +02:00
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
2018-11-14 21:40:17 +01:00
}
2017-04-20 22:01:14 +02:00
else
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "purchase-form" ) ;
2018-11-17 14:50:52 +01:00
toastr . success ( successMessage ) ;
2019-03-05 17:51:50 +01:00
$ ( "#amount" ) . attr ( "min" , "1" ) ;
$ ( "#amount" ) . attr ( "step" , "1" ) ;
2019-05-01 20:19:18 +02:00
$ ( "#amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %s' , '1' ) ) ;
2020-04-12 15:33:36 +02:00
$ ( '#amount' ) . val ( parseFloat ( Grocy . UserSettings . stock _default _purchase _amount ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : 4 } ) ) ;
2018-07-26 20:27:38 +02:00
$ ( '#price' ) . val ( '' ) ;
2019-03-09 12:31:45 +01:00
$ ( '#amount_qu_unit' ) . text ( "" ) ;
2019-03-05 17:51:50 +01:00
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
2019-09-19 17:46:52 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _LOCATION _TRACKING )
{
Grocy . Components . LocationPicker . Clear ( ) ;
}
2018-10-27 17:56:53 +02:00
Grocy . Components . DateTimePicker . Clear ( ) ;
2018-07-14 14:43:57 +02:00
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 ( '' ) ;
}
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
2020-01-17 11:13:43 -06:00
Grocy . Components . ProductCard . Refresh ( jsonForm . product _id ) ;
2018-07-11 19:43:05 +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-04-12 15:33:36 +02:00
$ ( '#price' ) . val ( parseFloat ( productDetails . last _price ) . toLocaleString ( { minimumFractionDigits : 2 , maximumFractionDigits : 2 } ) ) ;
2020-03-27 13:27:40 -05:00
2020-04-02 08:34:43 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _PRICE _TRACKING ) {
if ( productDetails . last _shopping _location _id != null )
{
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
2019-09-20 13:37:53 +02:00
if ( productDetails . product . qu _id _purchase === productDetails . product . qu _id _stock )
2018-08-06 22:41:35 +02:00
{
2019-09-20 13:37:53 +02:00
$ ( '#amount_qu_unit' ) . text ( productDetails . quantity _unit _purchase . name ) ;
2018-08-06 22:41:35 +02:00
}
else
{
2020-04-12 14:09:38 +02:00
$ ( '#amount_qu_unit' ) . text ( productDetails . quantity _unit _purchase . name + " (" + _ _t ( "will be multiplied by a factor of %1$s to get %2$s" , parseFloat ( productDetails . product . qu _factor _purchase _to _stock ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : 2 } ) , _ _n ( 2 , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural ) ) + ")" ) ;
2018-08-06 22:41:35 +02:00
}
2018-11-21 19:08:36 +01:00
2019-09-20 13:37:53 +02:00
if ( productDetails . product . allow _partial _units _in _stock == 1 )
2018-11-21 19:08:36 +01:00
{
2019-09-20 13:37:53 +02:00
$ ( "#amount" ) . attr ( "min" , "0.01" ) ;
$ ( "#amount" ) . attr ( "step" , "0.01" ) ;
$ ( "#amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %s' , 0.01 . toLocaleString ( ) ) ) ;
2018-11-21 19:08:36 +01:00
}
2019-09-20 13:37:53 +02:00
else
2019-09-19 17:46:52 +02:00
{
2019-09-20 13:37:53 +02:00
$ ( "#amount" ) . attr ( "min" , "1" ) ;
$ ( "#amount" ) . attr ( "step" , "1" ) ;
$ ( "#amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %s' , '1' ) ) ;
}
if ( productDetails . product . enable _tare _weight _handling == 1 )
{
2020-01-03 14:10:43 +01:00
var minAmount = parseFloat ( productDetails . product . tare _weight ) / productDetails . product . qu _factor _purchase _to _stock + parseFloat ( productDetails . stock _amount ) ;
2019-09-20 13:37:53 +02:00
$ ( "#amount" ) . attr ( "min" , minAmount ) ;
2020-01-03 14:10:43 +01:00
$ ( "#amount" ) . attr ( "step" , "0.0001" ) ;
2019-09-20 13:37:53 +02:00
$ ( "#amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %s' , minAmount . toLocaleString ( ) ) ) ;
$ ( "#tare-weight-handling-info" ) . removeClass ( "d-none" ) ;
2019-09-19 17:46:52 +02:00
}
else
{
2019-09-20 13:37:53 +02:00
$ ( "#tare-weight-handling-info" ) . addClass ( "d-none" ) ;
}
2020-02-02 13:01:31 +01:00
if ( ! Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _BEST _BEFORE _DATE _TRACKING )
{
Grocy . Components . DateTimePicker . SetValue ( moment ( ) . format ( 'YYYY-MM-DD' ) ) ;
}
2019-09-20 13:37:53 +02:00
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' ) ) ;
}
2019-09-19 17:46:52 +02:00
$ ( '#amount' ) . focus ( ) ;
2019-09-20 13:37:53 +02:00
}
else
{
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _BEST _BEFORE _DATE _TRACKING )
{
Grocy . Components . DateTimePicker . GetInputElement ( ) . focus ( ) ;
}
else
{
$ ( '#amount' ) . focus ( ) ;
}
2019-09-19 17:46:52 +02:00
}
2020-01-26 15:35:01 +01:00
2020-02-02 13:01:31 +01:00
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
if ( GetUriParam ( "flow" ) === "shoppinglistitemtostock" && BoolVal ( Grocy . UserSettings . shopping _list _to _stock _workflow _auto _submit _when _prefilled ) && document . getElementById ( "purchase-form" ) . checkValidity ( ) === true )
{
$ ( "#save-purchase-button" ) . click ( ) ;
}
2020-01-26 15:35:01 +01:00
if ( BoolVal ( Grocy . UserSettings . scan _mode _purchase _enabled ) )
{
$ ( "#amount" ) . val ( 1 ) ;
Grocy . FrontendHelpers . ValidateForm ( "purchase-form" ) ;
if ( document . getElementById ( "purchase-form" ) . checkValidity ( ) === true )
{
$ ( '#save-purchase-button' ) . click ( ) ;
}
else
{
toastr . warning ( _ _t ( "Scan mode is on but not all required fields could be populated automatically" ) ) ;
Grocy . UISound . Error ( ) ;
}
}
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
2020-04-12 15:33:36 +02:00
$ ( '#amount' ) . val ( parseFloat ( Grocy . UserSettings . stock _default _purchase _amount ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : 4 } ) ) ;
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
{
2019-09-20 13:37:53 +02:00
if ( Grocy . Components . ProductPicker . InProductAddWorkflow ( ) === false )
{
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
}
else
{
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( 'change' ) ;
}
2018-07-14 14:43:57 +02:00
}
2018-04-16 19:11:32 +02:00
$ ( '#amount' ) . on ( 'focus' , function ( e )
{
2018-07-14 14:43:57 +02:00
if ( Grocy . Components . ProductPicker . GetValue ( ) . length === 0 )
2017-11-10 22:45:53 +01:00
{
2018-07-14 14:43:57 +02:00
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
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
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 )
{
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
2018-07-11 19:43:05 +02:00
if ( document . getElementById ( 'purchase-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-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
2018-11-21 19:08:36 +01:00
$ ( '#amount' ) . on ( 'change' , function ( e )
2018-07-11 19:43:05 +02:00
{
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
2017-04-15 23:16:20 +02:00
} ) ;
2018-10-27 17:26:00 +02:00
2018-11-14 21:40:17 +01:00
if ( GetUriParam ( "flow" ) === "shoppinglistitemtostock" )
{
2020-04-12 15:33:36 +02:00
$ ( '#amount' ) . val ( parseFloat ( GetUriParam ( "amount" ) ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : 4 } ) ) ;
2018-11-14 21:40:17 +01:00
}
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" ) ) ;
2019-09-20 13:37:53 +02:00
Grocy . Api . Get ( 'stock/bookings/' + bookingId . toString ( ) ,
function ( result )
{
window . postMessage ( WindowMessageBag ( "ProductChanged" , result . product _id ) , Grocy . BaseUrl ) ;
} ,
function ( xhr )
{
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 )
{
Grocy . Api . Post ( 'stock/transactions/' + transactionId . toString ( ) + '/undo' , { } ,
function ( result )
{
toastr . success ( _ _t ( "Transaction successfully undone" ) ) ;
Grocy . Api . Get ( 'stock/transactions/' + transactionId . toString ( ) ,
function ( result )
{
window . postMessage ( WindowMessageBag ( "ProductChanged" , result [ 0 ] . product _id ) , Grocy . BaseUrl ) ;
} ,
function ( xhr )
{
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 )
{
document . activeElement . blur ( ) ;
$ ( "#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" ) ) ;
}
} ) ;