2020-08-30 12:18:16 +02:00
Grocy . Components . ProductCard = { } ;
2018-04-14 11:10:38 +02:00
Grocy . Components . ProductCard . Refresh = function ( productId )
{
2019-01-19 14:51:51 +01:00
Grocy . Api . Get ( 'stock/products/' + productId ,
2018-04-14 11:10:38 +02:00
function ( productDetails )
{
2018-07-27 19:39:34 +02:00
var stockAmount = productDetails . stock _amount || '0' ;
2020-08-17 14:47:33 -05:00
var stockFactorPurchaseAmount = productDetails . stock _factor _purchase _amount || '0' ;
var stockValue = productDetails . stock _value || '0' ;
2018-11-18 13:17:36 +01:00
var stockAmountOpened = productDetails . stock _amount _opened || '0' ;
2018-04-14 11:10:38 +02:00
$ ( '#productcard-product-name' ) . text ( productDetails . product . name ) ;
2019-08-15 14:35:28 +02:00
$ ( '#productcard-product-description' ) . html ( productDetails . product . description ) ;
2018-07-27 19:39:34 +02:00
$ ( '#productcard-product-stock-amount' ) . text ( stockAmount ) ;
2019-05-01 20:19:18 +02:00
$ ( '#productcard-product-stock-qu-name' ) . text ( _ _n ( stockAmount , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural ) ) ;
2020-08-17 14:47:33 -05:00
if ( productDetails . last _qu _factor _purchase _to _stock > 1 )
{
$ ( '#productcard-product-stock-factor-purchase-amount' ) . text ( '(' + stockFactorPurchaseAmount ) ;
$ ( '#productcard-product-stock-factor-purchase-qu-name' ) . text ( _ _n ( stockFactorPurchaseAmount , productDetails . quantity _unit _purchase . name , productDetails . quantity _unit _purchase . name _plural ) + ')' ) ;
}
else
{
$ ( '#productcard-product-stock-factor-purchase-amount' ) . text ( '' ) ;
$ ( '#productcard-product-stock-factor-purchase-qu-name' ) . text ( '' ) ;
}
$ ( '#productcard-product-stock-value' ) . text ( stockValue + ' ' + Grocy . Currency ) ;
2019-09-21 13:40:31 +02:00
$ ( '#productcard-product-last-purchased' ) . text ( ( productDetails . last _purchased || '2999-12-31' ) . substring ( 0 , 10 ) ) ;
$ ( '#productcard-product-last-purchased-timeago' ) . attr ( "datetime" , productDetails . last _purchased || '2999-12-31' ) ;
$ ( '#productcard-product-last-used' ) . text ( ( productDetails . last _used || '2999-12-31' ) . substring ( 0 , 10 ) ) ;
$ ( '#productcard-product-last-used-timeago' ) . attr ( "datetime" , productDetails . last _used || '2999-12-31' ) ;
2020-01-17 11:06:33 -06:00
if ( productDetails . location != null )
{
$ ( '#productcard-product-location' ) . text ( productDetails . location . name ) ;
}
2019-09-21 13:30:44 +02:00
$ ( '#productcard-product-spoil-rate' ) . text ( ( parseFloat ( productDetails . spoil _rate _percent ) / 100 ) . toLocaleString ( undefined , { style : "percent" } ) ) ;
2019-04-22 10:11:58 +02:00
2019-09-17 16:18:00 +02:00
if ( productDetails . is _aggregated _amount == 1 )
{
$ ( '#productcard-product-stock-amount-aggregated' ) . text ( productDetails . stock _amount _aggregated ) ;
$ ( '#productcard-product-stock-qu-name-aggregated' ) . text ( _ _n ( productDetails . stock _amount _aggregated , productDetails . quantity _unit _stock . name , productDetails . quantity _unit _stock . name _plural ) ) ;
if ( productDetails . stock _amount _opened _aggregated > 0 )
{
$ ( '#productcard-product-stock-opened-amount-aggregated' ) . text ( _ _t ( '%s opened' , productDetails . stock _amount _opened _aggregated ) ) ;
}
else
{
$ ( '#productcard-product-stock-opened-amount-aggregated' ) . text ( "" ) ;
}
$ ( "#productcard-aggregated-amounts" ) . removeClass ( "d-none" ) ;
}
else
{
$ ( "#productcard-aggregated-amounts" ) . addClass ( "d-none" ) ;
}
2019-04-22 10:11:58 +02:00
if ( productDetails . product . description != null && ! productDetails . product . description . isEmpty ( ) )
{
$ ( "#productcard-product-description-wrapper" ) . removeClass ( "d-none" ) ;
}
else
{
$ ( "#productcard-product-description-wrapper" ) . addClass ( "d-none" ) ;
}
if ( productDetails . average _shelf _life _days == - 1 )
{
2019-05-01 20:19:18 +02:00
$ ( '#productcard-product-average-shelf-life' ) . text ( _ _t ( "Unknown" ) ) ;
2019-04-22 10:11:58 +02:00
}
2019-09-27 14:04:44 +02:00
else if ( parseInt ( productDetails . average _shelf _life _days ) > 73000 ) // > 200 years aka forever
{
$ ( '#productcard-product-average-shelf-life' ) . text ( _ _t ( "Unlimited" ) ) ;
}
2019-04-22 10:11:58 +02:00
else
{
$ ( '#productcard-product-average-shelf-life' ) . text ( moment . duration ( productDetails . average _shelf _life _days , "days" ) . humanize ( ) ) ;
}
2018-04-14 11:10:38 +02:00
2018-11-18 13:17:36 +01:00
if ( stockAmountOpened > 0 )
{
2019-05-01 20:19:18 +02:00
$ ( '#productcard-product-stock-opened-amount' ) . text ( _ _t ( '%s opened' , stockAmountOpened ) ) ;
2018-11-18 13:17:36 +01:00
}
else
{
$ ( '#productcard-product-stock-opened-amount' ) . text ( "" ) ;
}
2019-09-22 09:03:59 +02:00
$ ( '#productcard-product-edit-button' ) . attr ( "href" , U ( "/product/" + productDetails . product . id . toString ( ) + '?' + 'returnto=' + encodeURIComponent ( Grocy . CurrentUrlRelative ) ) ) ;
2019-09-27 16:54:40 +02:00
$ ( '#productcard-product-journal-button' ) . attr ( "href" , U ( "/stockjournal?embedded&product=" + productDetails . product . id . toString ( ) ) ) ;
2018-10-22 19:13:08 +02:00
$ ( '#productcard-product-edit-button' ) . removeClass ( "disabled" ) ;
2019-09-27 14:19:06 +02:00
$ ( '#productcard-product-journal-button' ) . removeClass ( "disabled" ) ;
2018-10-22 19:13:08 +02:00
2018-07-26 20:27:38 +02:00
if ( productDetails . last _price !== null )
{
2020-08-17 14:47:33 -05:00
if ( productDetails . last _qu _factor _purchase _to _stock > 1 )
{
2020-08-30 12:18:16 +02:00
$ ( '#productcard-product-last-price' ) . text ( Number . parseFloat ( productDetails . last _price ) . toLocaleString ( ) + ' ' + Grocy . Currency + ' per 1 ' + productDetails . quantity _unit _purchase . name + ' of ' + productDetails . last _qu _factor _purchase _to _stock + ' ' + productDetails . quantity _unit _stock . name _plural ) ;
2020-08-17 14:47:33 -05:00
}
else
{
$ ( '#productcard-product-last-price' ) . text ( Number . parseFloat ( productDetails . last _price ) . toLocaleString ( ) + ' ' + Grocy . Currency + ' per ' + productDetails . quantity _unit _purchase . name ) ;
}
2018-07-26 20:27:38 +02:00
}
else
{
2019-05-01 20:19:18 +02:00
$ ( '#productcard-product-last-price' ) . text ( _ _t ( 'Unknown' ) ) ;
2018-07-26 20:27:38 +02:00
}
2020-08-17 14:47:33 -05:00
if ( productDetails . avg _price !== null )
{
$ ( '#productcard-product-average-price' ) . text ( Number . parseFloat ( productDetails . avg _price ) . toLocaleString ( ) + ' ' + Grocy . Currency ) ;
}
else
{
$ ( '#productcard-product-average-price' ) . text ( _ _t ( 'Unknown' ) ) ;
}
2018-10-01 20:20:50 +02:00
if ( productDetails . product . picture _file _name !== null && ! productDetails . product . picture _file _name . isEmpty ( ) )
{
$ ( "#productcard-product-picture" ) . removeClass ( "d-none" ) ;
2019-09-18 13:59:37 +02:00
$ ( "#productcard-product-picture" ) . attr ( "src" , U ( '/api/files/productpictures/' + btoa ( productDetails . product . picture _file _name ) + '?force_serve_as=picture&best_fit_width=400' ) ) ;
2018-10-01 20:20:50 +02:00
}
else
{
$ ( "#productcard-product-picture" ) . addClass ( "d-none" ) ;
}
2019-05-01 20:19:18 +02:00
EmptyElementWhenMatches ( '#productcard-product-last-purchased-timeago' , _ _t ( 'timeago_nan' ) ) ;
EmptyElementWhenMatches ( '#productcard-product-last-used-timeago' , _ _t ( 'timeago_nan' ) ) ;
2020-01-27 19:00:49 +01:00
RefreshContextualTimeago ( ".productcard" ) ;
2018-04-14 11:10:38 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
2018-07-26 20:27:38 +02:00
2019-09-19 17:46:52 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _STOCK _PRICE _TRACKING )
{
Grocy . Api . Get ( 'stock/products/' + productId + '/price-history' ,
function ( priceHistoryDataPoints )
2018-07-26 20:27:38 +02:00
{
2019-09-19 17:46:52 +02:00
if ( priceHistoryDataPoints . length > 0 )
2018-07-26 20:27:38 +02:00
{
2019-09-19 17:46:52 +02:00
$ ( "#productcard-product-price-history-chart" ) . removeClass ( "d-none" ) ;
$ ( "#productcard-no-price-data-hint" ) . addClass ( "d-none" ) ;
Grocy . Components . ProductCard . ReInitPriceHistoryChart ( ) ;
2020-03-25 19:34:56 +01:00
var datasets = { } ;
var chart = Grocy . Components . ProductCard . PriceHistoryChart . data ;
2019-09-19 17:46:52 +02:00
priceHistoryDataPoints . forEach ( ( dataPoint ) =>
{
2020-03-25 20:09:28 +01:00
var key = _ _t ( "Unknown store" ) ;
2020-03-25 19:49:10 +01:00
if ( dataPoint . shopping _location )
{
key = dataPoint . shopping _location . name
}
2020-08-29 16:41:27 +02:00
2020-08-30 12:18:16 +02:00
if ( ! datasets [ key ] )
{
2020-03-25 19:34:56 +01:00
datasets [ key ] = [ ]
}
chart . labels . push ( moment ( dataPoint . date ) . toDate ( ) ) ;
datasets [ key ] . push ( dataPoint . price ) ;
2019-09-19 17:46:52 +02:00
2020-03-25 19:34:56 +01:00
} ) ;
2020-08-30 12:18:16 +02:00
Object . keys ( datasets ) . forEach ( ( key ) =>
{
2020-03-25 19:34:56 +01:00
chart . datasets . push ( {
data : datasets [ key ] ,
fill : false ,
borderColor : "HSL(" + ( 129 * chart . datasets . length ) + ",100%,50%)" ,
label : key ,
} ) ;
2019-09-19 17:46:52 +02:00
} ) ;
Grocy . Components . ProductCard . PriceHistoryChart . update ( ) ;
}
else
{
$ ( "#productcard-product-price-history-chart" ) . addClass ( "d-none" ) ;
$ ( "#productcard-no-price-data-hint" ) . removeClass ( "d-none" ) ;
}
} ,
function ( xhr )
2018-07-26 20:27:38 +02:00
{
2019-09-19 17:46:52 +02:00
console . error ( xhr ) ;
2018-07-26 20:27:38 +02:00
}
2019-09-19 17:46:52 +02:00
) ;
}
2018-04-14 11:10:38 +02:00
} ;
2018-07-26 20:27:38 +02:00
Grocy . Components . ProductCard . ReInitPriceHistoryChart = function ( )
{
if ( typeof Grocy . Components . ProductCard . PriceHistoryChart !== "undefined" )
{
Grocy . Components . ProductCard . PriceHistoryChart . destroy ( ) ;
}
var format = 'YYYY-MM-DD' ;
Grocy . Components . ProductCard . PriceHistoryChart = new Chart ( document . getElementById ( "productcard-product-price-history-chart" ) , {
type : "line" ,
data : {
labels : [ //Date objects
// Will be populated in Grocy.Components.ProductCard.Refresh
] ,
2020-03-25 19:34:56 +01:00
datasets : [ //Datasets
// Will be populated in Grocy.Components.ProductCard.Refresh
]
2018-07-26 20:27:38 +02:00
} ,
options : {
scales : {
xAxes : [ {
type : 'time' ,
time : {
parser : format ,
round : 'day' ,
tooltipFormat : format ,
unit : 'day' ,
unitStepSize : 10 ,
displayFormats : {
'day' : format
}
} ,
ticks : {
autoSkip : true ,
maxRotation : 0
}
} ] ,
yAxes : [ {
ticks : {
beginAtZero : true
}
} ]
} ,
legend : {
2020-03-25 19:34:56 +01:00
display : true
2018-07-26 20:27:38 +02:00
}
}
} ) ;
}
2019-04-22 10:11:58 +02:00
$ ( "#productcard-product-description" ) . on ( "shown.bs.collapse" , function ( )
{
2019-05-01 20:19:18 +02:00
$ ( ".expandable-text" ) . find ( "a[data-toggle='collapse']" ) . text ( _ _t ( "Show less" ) ) ;
2019-04-22 10:11:58 +02:00
} )
$ ( "#productcard-product-description" ) . on ( "hidden.bs.collapse" , function ( )
{
2019-05-01 20:19:18 +02:00
$ ( ".expandable-text" ) . find ( "a[data-toggle='collapse']" ) . text ( _ _t ( "Show more" ) ) ;
2019-04-22 10:11:58 +02:00
} )