2018-07-09 19:27:22 +02:00
var productsTable = $ ( '#products-table' ) . DataTable ( {
'paginate' : false ,
'order' : [ [ 1 , 'asc' ] ] ,
'columnDefs' : [
{ 'orderable' : false , 'targets' : 0 }
] ,
2018-07-10 20:37:13 +02:00
'language' : JSON . parse ( L ( 'datatables_localization' ) ) ,
2018-07-14 08:48:14 +02:00
'scrollY' : false ,
2018-07-14 10:17:12 +02:00
'colReorder' : true ,
2018-09-08 08:56:32 +02:00
'stateSave' : true ,
'stateSaveParams' : function ( settings , data )
{
data . search . search = "" ;
2018-09-25 08:50:28 +02:00
data . columns . forEach ( column =>
{
column . search . search = "" ;
} ) ;
2018-09-08 08:56:32 +02:00
}
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
$ ( "#search" ) . on ( "keyup" , function ( )
{
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 ( ) ;
} ) ;
2019-04-05 21:08:30 +02:00
$ ( "#product-group-filter" ) . on ( "change" , function ( )
{
var value = $ ( "#product-group-filter option:selected" ) . text ( ) ;
if ( value === L ( "All" ) )
{
value = "" ;
}
productsTable . column ( 7 ) . search ( value ) . draw ( ) ;
} ) ;
if ( typeof GetUriParam ( "product-group" ) !== "undefined" )
{
$ ( "#product-group-filter" ) . val ( GetUriParam ( "product-group" ) ) ;
$ ( "#product-group-filter" ) . trigger ( "change" ) ;
}
2018-07-09 19:27:22 +02:00
$ ( document ) . on ( 'click' , '.product-delete-button' , function ( e )
2017-04-15 23:16:20 +02:00
{
2018-04-21 19:18:00 +02:00
var objectName = $ ( e . currentTarget ) . attr ( 'data-product-name' ) ;
var objectId = $ ( e . currentTarget ) . attr ( 'data-product-id' ) ;
2019-01-19 14:51:51 +01:00
Grocy . Api . Get ( 'stock/products/' + objectId ,
2018-10-02 18:17:26 +02:00
function ( productDetails )
2017-04-15 23:16:20 +02:00
{
2018-10-02 18:17:26 +02:00
var stockAmount = productDetails . stock _amount || '0' ;
if ( stockAmount . toString ( ) == "0" )
2017-04-15 23:16:20 +02:00
{
2018-10-02 18:17:26 +02:00
bootbox . confirm ( {
message : L ( 'Are you sure to delete product "#1"?' , objectName ) ,
buttons : {
confirm : {
label : L ( 'Yes' ) ,
className : 'btn-success'
} ,
cancel : {
label : L ( 'No' ) ,
className : 'btn-danger'
}
2017-04-15 23:16:20 +02:00
} ,
2018-10-02 18:17:26 +02:00
callback : function ( result )
2017-04-15 23:16:20 +02:00
{
2018-10-02 18:17:26 +02:00
if ( result === true )
{
2019-01-19 14:51:51 +01:00
Grocy . Api . Delete ( 'objects/products/' + objectId , { } ,
2018-10-02 18:17:26 +02:00
function ( result )
{
window . location . href = U ( '/products' ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2017-04-15 23:16:20 +02:00
}
2018-10-02 18:17:26 +02:00
} ) ;
2017-04-15 23:16:20 +02:00
}
2018-10-02 18:17:26 +02:00
else
{
bootbox . alert ( {
title : L ( 'Delete not possible' ) ,
message : L ( 'This product cannot be deleted because it is in stock, please remove the stock amount first.' ) + '<br><br>' + L ( 'Stock amount' ) + ': ' + stockAmount + ' ' + Pluralize ( stockAmount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural )
} ) ;
}
} ,
function ( xhr )
{
console . error ( xhr ) ;
2017-04-15 23:16:20 +02:00
}
2018-10-02 18:17:26 +02:00
) ;
2017-04-15 23:16:20 +02:00
} ) ;