mirror of
https://github.com/grocy/grocy.git
synced 2025-08-18 11:27:03 +00:00
Add something for product price tracking (references #22)
This commit is contained in:
@@ -14,6 +14,15 @@ Grocy.Components.ProductCard.Refresh = function(productId)
|
||||
$('#productcard-product-last-used').text((productDetails.last_used || L('never')).substring(0, 10));
|
||||
$('#productcard-product-last-used-timeago').text($.timeago(productDetails.last_used || ''));
|
||||
|
||||
if (productDetails.last_price !== null)
|
||||
{
|
||||
$('#productcard-product-last-price').text(Number.parseFloat(productDetails.last_price).toLocaleString() + ' ' + Grocy.Currency);
|
||||
}
|
||||
else
|
||||
{
|
||||
$('#productcard-product-last-price').text(L('Unknown'));
|
||||
}
|
||||
|
||||
EmptyElementWhenMatches('#productcard-product-last-purchased-timeago', L('timeago_nan'));
|
||||
EmptyElementWhenMatches('#productcard-product-last-used-timeago', L('timeago_nan'));
|
||||
},
|
||||
@@ -22,4 +31,90 @@ Grocy.Components.ProductCard.Refresh = function(productId)
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
|
||||
Grocy.Api.Get('stock/get-product-price-history/' + productId,
|
||||
function(priceHistoryDataPoints)
|
||||
{
|
||||
if (priceHistoryDataPoints.length > 0)
|
||||
{
|
||||
$("#productcard-product-price-history-chart").removeClass("d-none");
|
||||
$("#productcard-no-price-data-hint").addClass("d-none");
|
||||
|
||||
Grocy.Components.ProductCard.ReInitPriceHistoryChart();
|
||||
priceHistoryDataPoints.forEach((dataPoint) =>
|
||||
{
|
||||
Grocy.Components.ProductCard.PriceHistoryChart.data.labels.push(moment(dataPoint.date).toDate());
|
||||
|
||||
var dataset = Grocy.Components.ProductCard.PriceHistoryChart.data.datasets[0];
|
||||
dataset.data.push(dataPoint.price);
|
||||
});
|
||||
Grocy.Components.ProductCard.PriceHistoryChart.update();
|
||||
}
|
||||
else
|
||||
{
|
||||
$("#productcard-product-price-history-chart").addClass("d-none");
|
||||
$("#productcard-no-price-data-hint").removeClass("d-none");
|
||||
}
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
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
|
||||
new Date()
|
||||
// Will be populated in Grocy.Components.ProductCard.Refresh
|
||||
],
|
||||
datasets: [{
|
||||
data: [
|
||||
0
|
||||
// Will be populated in Grocy.Components.ProductCard.Refresh
|
||||
],
|
||||
fill: false,
|
||||
borderColor: '#17a2b8'
|
||||
}]
|
||||
},
|
||||
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: {
|
||||
display: false
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user