2018-07-09 19:27:22 +02:00
var productsTable = $ ( '#products-table' ) . DataTable ( {
'paginate' : false ,
'order' : [ [ 1 , 'asc' ] ] ,
'columnDefs' : [
{ 'orderable' : false , 'targets' : 0 }
] ,
2019-09-20 16:26:50 +02:00
'language' : IsJsonString ( _ _t ( 'datatables_localization' ) ) ? JSON . parse ( _ _t ( '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 ( ) ;
2019-05-01 20:19:18 +02:00
if ( value === _ _t ( "All" ) )
2019-04-05 21:08:30 +02:00
{
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 ( {
2019-05-01 20:19:18 +02:00
message : _ _t ( 'Are you sure to delete product "%s"?' , objectName ) ,
2019-09-24 10:24:47 +02:00
closeButton : false ,
2018-10-02 18:17:26 +02:00
buttons : {
confirm : {
2019-05-01 20:19:18 +02:00
label : _ _t ( 'Yes' ) ,
2018-10-02 18:17:26 +02:00
className : 'btn-success'
} ,
cancel : {
2019-05-01 20:19:18 +02:00
label : _ _t ( 'No' ) ,
2018-10-02 18:17:26 +02:00
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 ( {
2019-05-01 20:19:18 +02:00
title : _ _t ( 'Delete not possible' ) ,
2019-09-24 10:24:47 +02:00
message : _ _t ( 'This product cannot be deleted because it is in stock, please remove the stock amount first.' ) + '<br><br>' + _ _t ( 'Stock amount' ) + ': ' + stockAmount + ' ' + _ _n ( stockAmount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural ) ,
closeButton : false
2018-10-02 18:17:26 +02:00
} ) ;
}
} ,
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
} ) ;