2020-01-27 19:19:09 +01:00
var stockEntriesTable = $ ( '#stockentries-table' ) . DataTable ( {
2019-12-19 12:48:36 -06:00
'order' : [ [ 2 , 'asc' ] ] ,
'columnDefs' : [
{ 'orderable' : false , 'targets' : 0 } ,
2020-01-03 14:18:56 +01:00
{ 'searchable' : false , "targets" : 0 }
2019-12-19 12:48:36 -06:00
] ,
} ) ;
2020-01-27 19:19:09 +01:00
$ ( '#stockentries-table tbody' ) . removeClass ( "d-none" ) ;
2019-12-19 12:48:36 -06:00
2020-01-21 17:30:09 +01:00
$ . fn . dataTable . ext . search . push ( function ( settings , data , dataIndex )
{
var productId = Grocy . Components . ProductPicker . GetValue ( ) ;
2019-12-19 12:48:36 -06:00
2020-01-21 17:30:09 +01:00
if ( ( isNaN ( productId ) || productId == "" || productId == data [ 1 ] ) )
{
return true ;
}
2020-08-29 16:41:27 +02:00
2020-01-21 17:30:09 +01:00
return false ;
} ) ;
2019-12-19 12:48:36 -06:00
Grocy . Components . ProductPicker . GetPicker ( ) . on ( 'change' , function ( e )
{
2020-01-27 19:19:09 +01:00
stockEntriesTable . draw ( ) ;
2019-12-19 12:48:36 -06:00
} ) ;
$ ( document ) . on ( 'click' , '.stock-consume-button' , function ( e )
{
e . preventDefault ( ) ;
// Remove the focus from the current button
// to prevent that the tooltip stays until clicked anywhere else
document . activeElement . blur ( ) ;
Grocy . FrontendHelpers . BeginUiBusy ( ) ;
var productId = $ ( e . currentTarget ) . attr ( 'data-product-id' ) ;
var locationId = $ ( e . currentTarget ) . attr ( 'data-location-id' ) ;
var specificStockEntryId = $ ( e . currentTarget ) . attr ( 'data-stock-id' ) ;
var stockRowId = $ ( e . currentTarget ) . attr ( 'data-stockrow-id' ) ;
var consumeAmount = $ ( e . currentTarget ) . attr ( 'data-consume-amount' ) ;
2020-01-17 10:54:34 -06:00
var wasSpoiled = $ ( e . currentTarget ) . hasClass ( "stock-consume-button-spoiled" ) ;
2019-12-19 12:48:36 -06:00
Grocy . Api . Post ( 'stock/products/' + productId + '/consume' , { 'amount' : consumeAmount , 'spoiled' : wasSpoiled , 'location_id' : locationId , 'stock_entry_id' : specificStockEntryId } ,
function ( bookingResponse )
{
Grocy . Api . Get ( 'stock/products/' + productId ,
function ( result )
{
2020-01-17 10:54:34 -06:00
var toastMessage = _ _t ( 'Removed %1$s of %2$s from stock' , consumeAmount . toString ( ) + " " + _ _n ( consumeAmount , result . quantity _unit _stock . name , result . quantity _unit _stock . name _plural ) , result . product . name ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBookingEntry(' + bookingResponse . id + ',' + stockRowId + ')"><i class="fas fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ;
2019-12-19 12:48:36 -06:00
if ( wasSpoiled )
{
toastMessage += " (" + _ _t ( "Spoiled" ) + ")" ;
}
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2020-01-27 19:19:09 +01:00
RefreshStockEntryRow ( stockRowId ) ;
2020-01-17 10:54:34 -06:00
toastr . success ( toastMessage ) ;
2019-12-19 12:48:36 -06:00
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( ) ;
console . error ( xhr ) ;
}
) ;
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( ) ;
console . error ( xhr ) ;
}
) ;
} ) ;
$ ( document ) . on ( 'click' , '.product-open-button' , function ( e )
{
e . preventDefault ( ) ;
// Remove the focus from the current button
// to prevent that the tooltip stays until clicked anywhere else
document . activeElement . blur ( ) ;
Grocy . FrontendHelpers . BeginUiBusy ( ) ;
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' ) ;
2020-01-21 17:30:09 +01:00
var specificStockEntryId = $ ( e . currentTarget ) . attr ( 'data-stock-id' ) ;
var stockRowId = $ ( e . currentTarget ) . attr ( 'data-stockrow-id' ) ;
2019-12-19 12:48:36 -06:00
var button = $ ( e . currentTarget ) ;
2020-08-29 16:41:27 +02:00
2020-01-21 17:30:09 +01:00
Grocy . Api . Post ( 'stock/products/' + productId + '/open' , { 'amount' : 1 , 'stock_entry_id' : specificStockEntryId } ,
2019-12-19 12:48:36 -06:00
function ( bookingResponse )
{
2020-01-21 17:30:09 +01:00
button . addClass ( "disabled" ) ;
Grocy . FrontendHelpers . EndUiBusy ( ) ;
toastr . success ( _ _t ( 'Marked %1$s of %2$s as opened' , 1 + " " + productQuName , productName ) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBookingEntry(' + bookingResponse . id + ',' + stockRowId + ')"><i class="fas fa-undo"></i> ' + _ _t ( "Undo" ) + '</a>' ) ;
2020-01-27 19:19:09 +01:00
RefreshStockEntryRow ( stockRowId ) ;
2019-12-19 12:48:36 -06:00
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( ) ;
console . error ( xhr ) ;
}
) ;
} ) ;
$ ( document ) . on ( "click" , ".stock-name-cell" , function ( e )
{
Grocy . Components . ProductCard . Refresh ( $ ( e . currentTarget ) . attr ( "data-stock-id" ) ) ;
2020-01-27 19:19:09 +01:00
$ ( "#stockentry-productcard-modal" ) . modal ( "show" ) ;
2019-12-19 12:48:36 -06:00
} ) ;
2020-01-27 19:19:09 +01:00
function RefreshStockEntryRow ( stockRowId )
2019-12-19 12:48:36 -06:00
{
2020-01-22 14:08:49 -06:00
Grocy . Api . Get ( "stock/entry/" + stockRowId ,
2019-12-19 12:48:36 -06:00
function ( result )
{
var stockRow = $ ( '#stock-' + stockRowId + '-row' ) ;
2020-01-23 19:13:35 +01:00
// If the stock row not exists / is invisible (happens after consume/undo because the undone new stock row has different id), just reload the page for now
if ( ! stockRow . length || stockRow . hasClass ( "d-none" ) )
{
window . location . reload ( ) ;
}
2020-08-29 16:41:27 +02:00
2019-12-19 12:48:36 -06:00
if ( result == null || result . amount == 0 )
{
2020-01-28 19:27:18 +01:00
animateCSS ( "#stock-" + stockRowId + "-row" , "fadeOut" , function ( )
2019-12-19 12:48:36 -06:00
{
2020-01-28 19:27:18 +01:00
$ ( "#stock-" + stockRowId + "-row" ) . addClass ( "d-none" ) ;
2019-12-19 12:48:36 -06:00
} ) ;
}
else
{
2020-01-21 17:30:09 +01:00
var expiringThreshold = moment ( ) . add ( Grocy . UserSettings . stock _expring _soon _days , "days" ) ;
var now = moment ( ) ;
var bestBeforeDate = moment ( result . best _before _date ) ;
stockRow . removeClass ( "table-warning" ) ;
stockRow . removeClass ( "table-danger" ) ;
stockRow . removeClass ( "table-info" ) ;
stockRow . removeClass ( "d-none" ) ;
stockRow . removeAttr ( "style" ) ;
if ( now . isAfter ( bestBeforeDate ) )
{
stockRow . addClass ( "table-danger" ) ;
}
else if ( bestBeforeDate . isBefore ( expiringThreshold ) )
{
stockRow . addClass ( "table-warning" ) ;
}
2020-01-28 19:27:18 +01:00
animateCSS ( "#stock-" + stockRowId + "-row td:not(:first)" , "shake" ) ;
2019-12-19 12:48:36 -06:00
2020-01-28 19:27:18 +01:00
$ ( '#stock-' + stockRowId + '-amount' ) . text ( result . amount ) ;
$ ( '#stock-' + stockRowId + '-best-before-date' ) . text ( result . best _before _date ) ;
2020-01-21 17:30:09 +01:00
$ ( '#stock-' + stockRowId + '-best-before-date-timeago' ) . attr ( 'datetime' , result . best _before _date + ' 23:59:59' ) ;
2019-12-19 12:48:36 -06:00
2020-01-28 19:27:18 +01:00
$ ( ".stock-consume-button" ) . attr ( 'data-location-id' , result . location _id ) ;
2020-01-22 14:08:49 -06:00
2020-01-17 10:54:34 -06:00
var locationName = "" ;
Grocy . Api . Get ( "objects/locations/" + result . location _id ,
function ( locationResult )
{
locationName = locationResult . name ;
2020-03-25 20:00:52 +01:00
$ ( '#stock-' + stockRowId + '-location' ) . attr ( 'data-location-id' , result . location _id ) ;
$ ( '#stock-' + stockRowId + '-location' ) . text ( locationName ) ;
2020-01-17 10:54:34 -06:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
2020-01-21 17:30:09 +01:00
}
) ;
2020-08-29 16:41:27 +02:00
2020-01-28 19:27:18 +01:00
$ ( '#stock-' + stockRowId + '-price' ) . text ( result . price ) ;
2020-08-17 14:47:33 -05:00
$ ( '#stock-' + stockRowId + '-qu-factor-purchase-to-stock' ) . text ( result . qu _factor _purchase _to _stock ) ;
2020-01-28 19:27:18 +01:00
$ ( '#stock-' + stockRowId + '-purchased-date' ) . text ( result . purchased _date ) ;
2020-01-21 17:30:09 +01:00
$ ( '#stock-' + stockRowId + '-purchased-date-timeago' ) . attr ( 'datetime' , result . purchased _date + ' 23:59:59' ) ;
2020-03-25 20:00:52 +01:00
var shoppingLocationName = "" ;
Grocy . Api . Get ( "objects/shopping_locations/" + result . shopping _location _id ,
function ( shoppingLocationResult )
{
shoppingLocationName = shoppingLocationResult . name ;
$ ( '#stock-' + stockRowId + '-shopping-location' ) . attr ( 'data-shopping-location-id' , result . location _id ) ;
$ ( '#stock-' + stockRowId + '-shopping-location' ) . text ( shoppingLocationName ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
2020-01-28 19:27:18 +01:00
if ( result . open == 1 )
2020-01-21 17:30:09 +01:00
{
2020-01-28 19:27:18 +01:00
$ ( '#stock-' + stockRowId + '-opened-amount' ) . text ( _ _t ( 'Opened' ) ) ;
}
else
{
$ ( '#stock-' + stockRowId + '-opened-amount' ) . text ( "" ) ;
$ ( ".product-open-button[data-stockrow-id='" + stockRowId + "']" ) . removeClass ( "disabled" ) ;
}
2019-12-19 12:48:36 -06:00
}
2020-01-21 17:30:09 +01:00
// Needs to be delayed because of the animation above the date-text would be wrong if fired immediately...
2019-12-19 12:48:36 -06:00
setTimeout ( function ( )
{
2020-01-27 19:00:49 +01:00
RefreshContextualTimeago ( "#stock-" + stockRowId + "-row" ) ;
RefreshLocaleNumberDisplay ( "#stock-" + stockRowId + "-row" ) ;
2019-12-19 12:48:36 -06:00
} , 600 ) ;
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( ) ;
console . error ( xhr ) ;
}
) ;
}
$ ( window ) . on ( "message" , function ( e )
{
var data = e . originalEvent . data ;
2020-01-27 19:19:09 +01:00
if ( data . Message === "StockEntryChanged" )
2019-12-19 12:48:36 -06:00
{
2020-01-27 19:19:09 +01:00
RefreshStockEntryRow ( data . Payload ) ;
2019-12-19 12:48:36 -06:00
}
} ) ;
2020-01-17 10:54:34 -06:00
2020-01-22 14:08:49 -06:00
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( 'change' ) ;
2020-01-17 10:54:34 -06:00
function UndoStockBookingEntry ( bookingId , stockRowId )
{
Grocy . Api . Post ( 'stock/bookings/' + bookingId . toString ( ) + '/undo' , { } ,
function ( result )
{
2020-01-27 19:19:09 +01:00
window . postMessage ( WindowMessageBag ( "StockEntryChanged" , stockRowId ) , Grocy . BaseUrl ) ;
2020-01-17 10:54:34 -06:00
toastr . success ( _ _t ( "Booking successfully undone" ) ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ;
2020-01-21 17:30:09 +01:00
$ ( document ) . on ( "click" , ".product-name-cell" , function ( e )
{
Grocy . Components . ProductCard . Refresh ( $ ( e . currentTarget ) . attr ( "data-product-id" ) ) ;
$ ( "#productcard-modal" ) . modal ( "show" ) ;
} ) ;