2018-07-08 15:16:24 +02:00
var stockOverviewTable = $ ( '#stock-overview-table' ) . DataTable ( {
2018-07-08 16:54:37 +02:00
'paginate' : false ,
2018-05-12 16:15:28 +02:00
'order' : [ [ 3 , 'asc' ] ] ,
'columnDefs' : [
2018-07-08 15:16:24 +02:00
{ 'orderable' : false , 'targets' : 0 } ,
{ 'visible' : false , 'targets' : 4 }
2018-05-12 16:15:28 +02:00
] ,
2018-07-09 19:27:22 +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 ,
'stateSave' : true
2018-07-08 15:16:24 +02:00
} ) ;
2018-07-08 16:54:37 +02:00
$ ( "#location-filter" ) . on ( "change" , function ( )
2018-07-08 15:16:24 +02:00
{
2018-07-08 16:54:37 +02:00
var value = $ ( this ) . val ( ) ;
if ( value === "all" )
2018-07-08 15:16:24 +02:00
{
2018-07-08 16:54:37 +02:00
value = "" ;
}
stockOverviewTable . column ( 4 ) . search ( value ) . draw ( ) ;
} ) ;
2018-07-08 15:16:24 +02:00
2018-07-08 16:54:37 +02:00
$ ( "#search" ) . on ( "keyup" , function ( )
{
var value = $ ( this ) . val ( ) ;
if ( value === "all" )
{
value = "" ;
}
stockOverviewTable . search ( value ) . draw ( ) ;
2017-04-16 23:11:03 +02:00
} ) ;
2018-05-12 16:15:28 +02:00
$ ( document ) . on ( 'click' , '.product-consume-button' , function ( e )
{
var productId = $ ( e . currentTarget ) . attr ( 'data-product-id' ) ;
var productName = $ ( e . currentTarget ) . attr ( 'data-product-name' ) ;
var productQuName = $ ( e . currentTarget ) . attr ( 'data-product-qu-name' ) ;
2018-05-13 08:42:45 +02:00
var consumeAmount = $ ( e . currentTarget ) . attr ( 'data-consume-amount' ) ;
2018-05-12 16:15:28 +02:00
2018-05-13 08:42:45 +02:00
Grocy . Api . Get ( 'stock/consume-product/' + productId + '/' + consumeAmount ,
2018-05-12 16:15:28 +02:00
function ( result )
{
var oldAmount = parseInt ( $ ( '#product-' + productId + '-amount' ) . text ( ) ) ;
2018-05-13 08:42:45 +02:00
var newAmount = oldAmount - consumeAmount ;
2018-05-12 16:15:28 +02:00
if ( newAmount === 0 )
{
2018-05-13 08:42:45 +02:00
$ ( '#product-' + productId + '-row' ) . fadeOut ( 500 , function ( )
{
$ ( this ) . remove ( ) ;
} ) ;
2018-05-12 16:15:28 +02:00
}
else
{
2018-05-13 08:42:45 +02:00
$ ( '#product-' + productId + '-amount' ) . parent ( ) . effect ( 'highlight' , { } , 500 ) ;
$ ( '#product-' + productId + '-amount' ) . fadeOut ( 500 , function ( )
{
$ ( this ) . text ( newAmount ) . fadeIn ( 500 ) ;
} ) ;
$ ( '#product-' + productId + '-consume-all-button' ) . attr ( 'data-consume-amount' , newAmount ) ;
2018-05-12 16:15:28 +02:00
}
2018-05-13 08:42:45 +02:00
toastr . success ( L ( 'Removed #1 #2 of #3 from stock' , consumeAmount , productQuName , productName ) ) ;
2018-08-04 14:25:32 +02:00
RefreshStatistics ( ) ;
2018-05-12 16:15:28 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ) ;
2018-08-04 14:25:32 +02:00
function RefreshStatistics ( )
{
Grocy . Api . Get ( 'stock/get-current-stock' ,
function ( result )
{
var amountSum = 0 ;
result . forEach ( element => {
amountSum += parseInt ( element . amount ) ;
} ) ;
$ ( "#info-current-stock" ) . text ( result . length + " " + Pluralize ( result . length , L ( 'Product' ) , L ( 'Products' ) ) + ", " + amountSum . toString ( ) + " " + Pluralize ( amountSum , L ( 'Unit' ) , L ( 'Units' ) ) ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
var nextXDays = $ ( "#info-expiring-products" ) . data ( "next-x-days" ) ;
Grocy . Api . Get ( 'stock/get-current-volatil-stock?expiring_days=' + nextXDays ,
function ( result )
{
$ ( "#info-expiring-products" ) . text ( Pluralize ( result . expiring _products . length , L ( '#1 product expires within the next #2 days' , result . expiring _products . length , nextXDays ) , L ( '#1 products expiring within the next #2 days' , result . expiring _products . length , nextXDays ) ) ) ;
$ ( "#info-expired-products" ) . text ( Pluralize ( result . expired _products . length , L ( '#1 product is already expired' , result . expired _products . length ) , L ( '#1 products are already expired' , result . expired _products . length ) ) ) ;
$ ( "#info-missing-products" ) . text ( Pluralize ( result . missing _products . length , L ( '#1 product is below defined min. stock amount' , result . missing _products . length ) , L ( '#1 products are below defined min. stock amount' , result . missing _products . length ) ) ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
RefreshStatistics ( ) ;