public static function storePriceHistoryById($productId, $price, $type = 0)
{
$date = Factory::getDate();
$dateNow = $date->toSql();
$db = Factory::getDBO();
$query = 'SELECT a.id, a.price, a.date' . ' FROM #__phocacart_product_price_history AS a' . ' WHERE a.product_id = ' . (int) $productId . ' AND a.type IN (0,1)' . ' AND a.date = (SELECT MAX(date) FROM #__phocacart_product_price_history WHERE product_id = ' . (int) $productId . ' AND type IN (0,1))' . ' ORDER BY a.id';
$db->setQuery($query);
$history = $db->loadAssoc();
$price = PhocacartUtils::replaceCommaWithPoint($price);
if (isset($history['price']) && $history['price'] == $price) {
// Do nothing
} else {
if (isset($history['date']) && isset($history['id'])) {
$dateDb2 = HTMLHelper::_('date', $history['date'], 'Y-m-d');
$dateNow2 = HTMLHelper::_('date', $dateNow, 'Y-m-d');
if ($dateDb2 == $dateNow2) {
$query = ' UPDATE #__phocacart_product_price_history SET price = ' . $db->quote($price) . ', type = ' . (int) $type . ' WHERE id = ' . (int) $history['id'];
$db->setQuery($query);
$db->execute();
} else {
$query = ' INSERT INTO #__phocacart_product_price_history (product_id, date, price, type)' . ' VALUES (' . (int) $productId . ', NOW(), ' . $db->quote($price) . ', ' . (int) $type . ');';
$db->setQuery($query);
$db->execute();
}
} else {
if (empty($history)) {
$query = ' INSERT INTO #__phocacart_product_price_history (product_id, date, price, type)' . ' VALUES (' . (int) $productId . ', NOW(), ' . $db->quote($price) . ', ' . (int) $type . ');';
$db->setQuery($query);
$db->execute();
}
}
}
return true;
}