2017-04-15 23:16:20 +02:00
$ ( '#save-purchase-button' ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
var jsonForm = $ ( '#purchase-form' ) . serializeJSON ( ) ;
2018-04-18 19:03:39 +02:00
Grocy . Api . Get ( 'stock/get-product-details/' + jsonForm . product _id ,
2017-04-17 16:51:49 +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 ) ;
}
Grocy . Api . Get ( 'stock/add-product/' + jsonForm . product _id + '/' + amount + '?bestbeforedate=' + Grocy . Components . DateTimePicker . GetValue ( ) + '&price=' + price ,
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 . Post ( '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 ( 'Added #1 #2 of #3 to stock' , amount , Pluralize ( amount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural ) , productDetails . product . name ) ) ;
2017-04-16 23:11:03 +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 ( '/purchase' ) ;
2017-04-20 22:01:14 +02:00
}
else
{
2017-04-22 11:36:05 +02:00
$ ( '#amount' ) . val ( 0 ) ;
2018-07-26 20:27:38 +02:00
$ ( '#price' ) . 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 ( '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 )
{
2017-04-15 23:16:20 +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-15 23:16:20 +02:00
{
var productId = $ ( e . target ) . val ( ) ;
2017-04-16 23:11:03 +02:00
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-17 16:51:49 +02:00
function ( productDetails )
2017-04-16 23:11:03 +02:00
{
2017-04-21 19:02:00 +02:00
$ ( '#amount_qu_unit' ) . text ( productDetails . quantity _unit _purchase . name ) ;
2018-07-26 20:27:38 +02:00
$ ( '#price' ) . val ( productDetails . last _price ) ;
2017-04-20 17:10:21 +02:00
if ( productDetails . product . default _best _before _days . toString ( ) !== '0' )
{
2018-08-06 22:41:35 +02:00
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' ) ) ;
}
2017-11-05 11:29:17 +01:00
$ ( '#amount' ) . focus ( ) ;
2017-04-20 17:10:21 +02:00
}
2017-11-05 11:29:17 +01:00
else
{
2018-07-12 19:12:31 +02:00
Grocy . Components . DateTimePicker . GetInputElement ( ) . focus ( ) ;
2018-04-14 11:10:38 +02:00
}
2017-04-16 23:11:03 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2017-04-15 23:16:20 +02:00
} ) ;
2018-04-16 19:11:32 +02:00
$ ( '#amount' ) . val ( 0 ) ;
2018-07-12 19:12:31 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
2017-11-10 22:45:53 +01: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
$ ( '#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 ( ) ;
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
2018-07-12 19:12:31 +02:00
Grocy . Components . DateTimePicker . GetInputElement ( ) . on ( 'change' , function ( e )
2017-04-17 16:51:49 +02:00
{
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
2017-04-17 16:51:49 +02:00
} ) ;
2018-07-12 19:12:31 +02:00
Grocy . Components . DateTimePicker . GetInputElement ( ) . on ( 'keypress' , function ( e )
2017-04-17 16:51:49 +02:00
{
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
} ) ;
$ ( '#amount' ) . on ( 'change' , function ( e )
{
Grocy . FrontendHelpers . ValidateForm ( 'purchase-form' ) ;
2017-04-15 23:16:20 +02:00
} ) ;