Back to PhocacartPriceHistory class

Method storePriceHistoryCustomById

public static
storePriceHistoryCustomById
(mixed $data, mixed $productId, mixed $type = 0)

Method storePriceHistoryCustomById - Source code

/*
 * Difference to automatic way of adding history prices:
 * custom - the same price can be used for more dates - we don't ask for latest date
 * automatic - when the same price then is is not written to database - we ask for latest date so we can compare and detect
 *             that the latest price is the same and in such case we don't add it to the database
 */
public static function storePriceHistoryCustomById($data, $productId, $type = 0)
{
    $db = Factory::getDBO();
    $notDeleteIds = array();
    if (!empty($data)) {
        $i = 1;
        foreach ($data as $k => $v) {
            if (isset($v['date']) && isset($v['price']) && (float) $v['price'] > 0) {
                $date = Factory::getDate($v['date']);
                $dateDb = $date->toSql();
                $query = 'SELECT a.id' . ' FROM #__phocacart_product_price_history AS a' . ' WHERE a.product_id = ' . (int) $productId . ' AND a.type IN (0,1)' . ' AND DATE_FORMAT(a.date, \'%Y-%m-%d\') = DATE_FORMAT(' . $db->quote($dateDb) . ', \'%Y-%m-%d\')' . ' ORDER BY a.id';
                $db->setQuery($query);
                $history = $db->loadAssocList();
                // Remove duplicates
                if (!empty($history)) {
                    foreach ($history as $k2 => $v2) {
                        if ((int) $k2 > 0 && (int) $v2['id'] > 0) {
                            $query = ' DELETE ' . ' FROM #__phocacart_product_price_history' . ' WHERE product_id = ' . (int) $productId . ' AND id = ' . (int) $v2['id'] . ' AND type IN (0,1)';
                            $db->setQuery($query);
                            $db->execute();
                        }
                    }
                }
                if (isset($history[0]['id']) && (int) $history[0]['id']) {
                    $query = ' UPDATE #__phocacart_product_price_history SET price = ' . $db->quote($v['price']) . ', type = ' . (int) $type . ', ordering = ' . $i . ' WHERE id = ' . (int) $history[0]['id'];
                    $db->setQuery($query);
                    $db->execute();
                    $i++;
                    $notDeleteIds[] = (int) $history[0]['id'];
                } else {
                    $query = ' INSERT INTO #__phocacart_product_price_history (product_id, date, price, type, ordering)' . ' VALUES (' . (int) $productId . ', ' . $db->quote($dateDb) . ', ' . $db->quote($v['price']) . ', ' . (int) $v['type'] . ', ' . $i . ');';
                    $db->setQuery($query);
                    $db->execute();
                    $i++;
                    $newIdA = $db->insertid();
                    $notDeleteIds[] = (int) $newIdA;
                }
            }
        }
        // Remove all ids except the active
        if (!empty($notDeleteIds)) {
            $notDeleteIdsString = implode(',', $notDeleteIds);
            $query = ' DELETE ' . ' FROM #__phocacart_product_price_history' . ' WHERE product_id = ' . (int) $productId . ' AND id NOT IN (' . $notDeleteIdsString . ')' . ' AND type IN (0,1)';
        } else {
            $query = ' DELETE ' . ' FROM #__phocacart_product_price_history' . ' WHERE product_id = ' . (int) $productId . ' AND type IN (0,1)';
        }
        $db->setQuery($query);
        $db->execute();
        return true;
    } else {
        $query = ' DELETE ' . ' FROM #__phocacart_product_price_history' . ' WHERE product_id = ' . (int) $productId . ' AND type IN (0,1)';
        $db->setQuery($query);
        $db->execute();
        return true;
    }
    return false;
}