2018-07-09 19:27:22 +02:00
var productsTable = $ ( '#products-table' ) . DataTable ( {
'order' : [ [ 1 , 'asc' ] ] ,
'columnDefs' : [
2020-01-03 14:18:56 +01:00
{ 'orderable' : false , 'targets' : 0 } ,
2020-12-17 17:33:24 +01:00
{ 'searchable' : false , "targets" : 0 } ,
2020-12-19 10:51:07 +01:00
{ 'visible' : false , 'targets' : 7 } ,
2022-03-14 22:39:35 +01:00
{ 'visible' : false , 'targets' : 8 } ,
2025-01-13 17:41:08 +01:00
{ 'visible' : false , 'targets' : 9 } ,
2020-12-17 17:33:24 +01:00
{ "type" : "html-num-fmt" , "targets" : 3 }
2020-12-07 19:48:33 +01:00
] . concat ( $ . fn . dataTable . defaults . columnDefs )
2018-07-09 19:27:22 +02:00
} ) ;
2019-01-05 20:06:35 +01:00
$ ( '#products-table tbody' ) . removeClass ( "d-none" ) ;
2019-03-04 17:43:12 +01:00
productsTable . columns . adjust ( ) . draw ( ) ;
2018-07-09 19:27:22 +02:00
2019-10-15 19:59:14 +02:00
$ ( "#search" ) . on ( "keyup" , Delay ( function ( )
2018-07-09 19:27:22 +02:00
{
var value = $ ( this ) . val ( ) ;
if ( value === "all" )
{
value = "" ;
}
2019-01-19 00:37:21 -07:00
2018-07-09 19:27:22 +02:00
productsTable . search ( value ) . draw ( ) ;
2025-01-31 15:35:34 +01:00
} , Grocy . FormFocusDelay ) ) ;
2018-07-09 19:27:22 +02:00
2019-04-05 21:08:30 +02:00
$ ( "#product-group-filter" ) . on ( "change" , function ( )
{
var value = $ ( "#product-group-filter option:selected" ) . text ( ) ;
2019-05-01 20:19:18 +02:00
if ( value === _ _t ( "All" ) )
2019-04-05 21:08:30 +02:00
{
2024-08-24 10:49:19 +02:00
productsTable . column ( productsTable . colReorder . transpose ( 6 ) ) . search ( "" ) . draw ( ) ;
}
else
{
2025-01-27 17:25:25 +01:00
productsTable . column ( productsTable . colReorder . transpose ( 6 ) ) . search ( "^" + $ . fn . dataTable . util . escapeRegex ( value ) + "$" , true , false ) . draw ( ) ;
2019-04-05 21:08:30 +02:00
}
} ) ;
2020-11-07 14:53:45 +01:00
$ ( "#clear-filter-button" ) . on ( "click" , function ( )
{
$ ( "#search" ) . val ( "" ) ;
$ ( "#product-group-filter" ) . val ( "all" ) ;
2021-11-12 18:26:19 +01:00
productsTable . column ( productsTable . colReorder . transpose ( 6 ) ) . search ( "" ) . draw ( ) ;
2020-11-07 14:53:45 +01:00
productsTable . search ( "" ) . draw ( ) ;
2023-04-13 20:28:28 +02:00
if ( $ ( "#show-disabled" ) . is ( ":checked" ) )
2021-07-06 20:08:02 +02:00
{
$ ( "#show-disabled" ) . prop ( "checked" , false ) ;
RemoveUriParam ( "include_disabled" ) ;
RemoveUriParam ( "only_in_stock" ) ;
window . location . reload ( ) ;
}
2023-04-13 20:28:28 +02:00
if ( $ ( "#status-filter" ) . val ( ) != "all" )
{
$ ( "#status-filter" ) . val ( "all" ) ;
$ ( "#status-filter" ) . trigger ( "change" ) ;
}
2020-11-07 14:53:45 +01:00
} ) ;
2019-04-05 21:08:30 +02:00
if ( typeof GetUriParam ( "product-group" ) !== "undefined" )
{
$ ( "#product-group-filter" ) . val ( GetUriParam ( "product-group" ) ) ;
$ ( "#product-group-filter" ) . trigger ( "change" ) ;
}
2020-08-30 12:18:16 +02:00
$ ( document ) . on ( 'click' , '.product-delete-button' , function ( e )
2017-04-15 23:16:20 +02:00
{
2020-10-14 22:58:26 +02:00
var objectName = $ ( e . currentTarget ) . attr ( 'data-product-name' ) ;
2018-04-21 19:18:00 +02:00
var objectId = $ ( e . currentTarget ) . attr ( 'data-product-id' ) ;
2020-12-07 19:48:33 +01:00
bootbox . confirm ( {
2024-12-26 10:46:54 +01:00
message : _ _t ( 'Are you sure you want to delete product "%s"?' , objectName ) + '<br><br>' + _ _t ( 'This also removes any stock amount, the journal and all other references of this product - consider disabling it instead, if you want to keep that and just hide the product.' ) ,
2020-12-07 19:48:33 +01:00
closeButton : false ,
buttons : {
confirm : {
label : _ _t ( 'Yes' ) ,
className : 'btn-success'
} ,
cancel : {
label : _ _t ( 'No' ) ,
className : 'btn-danger'
}
} ,
callback : function ( result )
2017-04-15 23:16:20 +02:00
{
2020-12-07 19:48:33 +01:00
if ( result === true )
2017-04-15 23:16:20 +02:00
{
2020-12-07 19:48:33 +01:00
jsonData = { } ;
jsonData . active = 0 ;
Grocy . Api . Delete ( 'objects/products/' + objectId , { } ,
function ( result )
{
window . location . href = U ( '/products' ) ;
2017-04-15 23:16:20 +02:00
} ,
2020-12-07 19:48:33 +01:00
function ( xhr )
2017-04-15 23:16:20 +02:00
{
2020-12-07 19:48:33 +01:00
console . error ( xhr ) ;
2017-04-15 23:16:20 +02:00
}
2020-12-07 19:48:33 +01:00
) ;
2017-04-15 23:16:20 +02:00
}
}
2020-12-07 19:48:33 +01:00
} ) ;
} ) ;
2020-12-20 10:19:44 +01:00
$ ( "#show-disabled" ) . change ( function ( )
2020-12-07 19:48:33 +01:00
{
if ( this . checked )
{
2021-07-06 20:08:02 +02:00
UpdateUriParam ( "include_disabled" , "true" ) ;
2020-12-07 19:48:33 +01:00
}
else
{
2021-07-06 20:08:02 +02:00
RemoveUriParam ( "include_disabled" ) ;
2020-12-07 19:48:33 +01:00
}
2021-07-06 20:08:02 +02:00
window . location . reload ( ) ;
} ) ;
2023-04-13 20:28:28 +02:00
$ ( "#status-filter" ) . change ( function ( )
2021-07-06 20:08:02 +02:00
{
2023-04-13 20:28:28 +02:00
var value = $ ( this ) . val ( ) ;
if ( value == "all" )
2021-07-06 20:08:02 +02:00
{
UpdateUriParam ( "only_in_stock" , "true" ) ;
2023-04-13 20:28:28 +02:00
RemoveUriParam ( "only_in_stock" ) ;
RemoveUriParam ( "only_out_of_stock" ) ;
2021-07-06 20:08:02 +02:00
}
2023-04-13 20:28:28 +02:00
else if ( value == "out-of-stock" )
2021-07-06 20:08:02 +02:00
{
RemoveUriParam ( "only_in_stock" ) ;
2023-04-13 20:28:28 +02:00
UpdateUriParam ( "only_out_of_stock" , "true" ) ;
}
else if ( value == "in-stock" )
{
RemoveUriParam ( "only_out_of_stock" ) ;
UpdateUriParam ( "only_in_stock" , "true" ) ;
2021-07-06 20:08:02 +02:00
}
window . location . reload ( ) ;
2017-04-15 23:16:20 +02:00
} ) ;
2020-12-07 19:48:33 +01:00
if ( GetUriParam ( 'include_disabled' ) )
{
2020-12-20 10:19:44 +01:00
$ ( "#show-disabled" ) . prop ( 'checked' , true ) ;
2020-12-07 19:48:33 +01:00
}
2020-12-20 20:58:22 +01:00
$ ( ".merge-products-button" ) . on ( "click" , function ( e )
{
var productId = $ ( e . currentTarget ) . attr ( "data-product-id" ) ;
$ ( "#merge-products-keep" ) . val ( productId ) ;
$ ( "#merge-products-remove" ) . val ( "" ) ;
$ ( "#merge-products-modal" ) . modal ( "show" ) ;
} ) ;
2022-03-26 11:17:08 +01:00
$ ( "#merge-products-save-button" ) . on ( "click" , function ( e )
2020-12-20 20:58:22 +01:00
{
2022-03-26 11:17:08 +01:00
e . preventDefault ( ) ;
if ( ! Grocy . FrontendHelpers . ValidateForm ( "merge-products-form" , true ) )
{
return ;
}
2020-12-20 20:58:22 +01:00
var productIdToKeep = $ ( "#merge-products-keep" ) . val ( ) ;
var productIdToRemove = $ ( "#merge-products-remove" ) . val ( ) ;
Grocy . Api . Post ( "stock/products/" + productIdToKeep . toString ( ) + "/merge/" + productIdToRemove . toString ( ) , { } ,
function ( result )
{
window . location . href = U ( '/products' ) ;
} ,
function ( xhr )
{
2021-11-14 16:19:52 +01:00
Grocy . FrontendHelpers . ShowGenericError ( 'Error while merging' , xhr . response ) ;
2020-12-20 20:58:22 +01:00
}
) ;
} ) ;