/* $new = 1 When we copy a product, we create new one and we need to create new items for this product
*/
public static function storeDiscountsById($productId, $discsArray, $new = 0)
{
if ((int) $productId > 0) {
$db = Factory::getDBO();
/*$query = ' DELETE '
.' FROM #__phocacart_product_discounts'
. ' WHERE product_id = '. (int)$productId;
$db->setQuery($query);
$db->execute();*/
$notDeleteDiscs = array();
if (!empty($discsArray)) {
$values = array();
$i = 1;
foreach ($discsArray as $k => $v) {
// Don't store empty discounts
/*if ($v['title'] == '') {
continue;
}*/
if (empty($v['alias'])) {
$v['alias'] = $v['title'];
}
$v['alias'] = PhocacartUtils::getAliasName($v['alias']);
// correct simple xml
if (empty($v['title'])) {
$v['title'] = '';
}
if (empty($v['alias'])) {
$v['alias'] = '';
}
if (empty($v['access'])) {
$v['access'] = '';
}
if (empty($v['discount'])) {
$v['discount'] = '';
}
if (empty($v['calculation_type'])) {
$v['calculation_type'] = '';
}
if (empty($v['quantity_from'])) {
$v['quantity_from'] = '';
}
if (empty($v['quantity_to'])) {
$v['quantity_to'] = '';
}
if (empty($v['valid_from'])) {
$v['valid_from'] = '0000-00-00';
}
if (empty($v['valid_to'])) {
$v['valid_to'] = '0000-00-00';
}
// Valid to - day including the last second
if ($v['valid_to'] == '0000-00-00' || $v['valid_to'] == '0000-00-00 00:00:00') {
} else {
$v['valid_to'] = str_replace('00:00:00', '23:59:59', Factory::getDate($v['valid_to'])->toSql());
}
if (empty($v['groups'])) {
$v['groups'] = array();
}
if (empty($v['description'])) {
$v['description'] = '';
}
if (empty($v['background_image'])) {
$v['background_image'] = '';
}
if ($v['discount'] == '') {
continue;
}
$idExists = 0;
if ($new == 0) {
if (isset($v['id']) && $v['id'] > 0) {
// Does the row exist
$query = ' SELECT id ' . ' FROM #__phocacart_product_discounts' . ' WHERE id = ' . (int) $v['id'] . ' ORDER BY id';
$db->setQuery($query);
$idExists = $db->loadResult();
}
}
if ((int) $idExists > 0) {
$v['discount'] = PhocacartUtils::replaceCommaWithPoint($v['discount']);
$query = 'UPDATE #__phocacart_product_discounts SET' . ' product_id = ' . (int) $productId . ',' . ' title = ' . $db->quote($v['title']) . ',' . ' alias = ' . $db->quote($v['alias']) . ',' . ' access = ' . (int) $v['access'] . ',' . ' discount = ' . $db->quote($v['discount']) . ',' . ' calculation_type = ' . (int) $v['calculation_type'] . ',' . ' quantity_from = ' . (int) $v['quantity_from'] . ',' . ' quantity_to = ' . (int) $v['quantity_to'] . ',' . ' valid_from = ' . $db->quote($v['valid_from']) . ',' . ' valid_to = ' . $db->quote($v['valid_to']) . ',' . ' description = ' . $db->quote($v['description']) . ',' . ' background_image = ' . $db->quote($v['background_image']) . ',' . ' ordering = ' . (int) $i . ' WHERE id = ' . (int) $idExists;
$db->setQuery($query);
$db->execute();
$i++;
$newIdD = $idExists;
} else {
$v['discount'] = PhocacartUtils::replaceCommaWithPoint($v['discount']);
$values = '(' . (int) $productId . ', ' . $db->quote($v['title']) . ', ' . $db->quote($v['alias']) . ', ' . (int) $v['access'] . ', ' . $db->quote($v['discount']) . ', ' . (int) $v['calculation_type'] . ', ' . (int) $v['quantity_from'] . ', ' . (int) $v['quantity_to'] . ', ' . $db->quote($v['valid_from']) . ', ' . $db->quote($v['valid_to']) . ', ' . $db->quote($v['description']) . ', ' . $db->quote($v['background_image']) . ', ' . (int) $i . ')';
$query = ' INSERT INTO #__phocacart_product_discounts (product_id, title, alias, access, discount, calculation_type, quantity_from, quantity_to, valid_from, valid_to, description, background_image, ordering)' . ' VALUES ' . $values;
$db->setQuery($query);
$db->execute();
$i++;
$newIdD = $db->insertid();
}
PhocacartGroup::storeGroupsById((int) $newIdD, 4, $v['groups'], $productId);
$notDeleteDiscs[] = $newIdD;
}
}
// Remove all discounts except the active
if (!empty($notDeleteDiscs)) {
$notDeleteDiscsString = implode(',', $notDeleteDiscs);
$query = ' DELETE ' . ' FROM #__phocacart_product_discounts' . ' WHERE product_id = ' . (int) $productId . ' AND id NOT IN (' . $notDeleteDiscsString . ')';
$query2 = 'DELETE FROM #__phocacart_item_groups' . ' WHERE item_id NOT IN ( ' . $notDeleteDiscsString . ' )' . ' AND product_id = ' . (int) $productId . ' AND type = 4';
} else {
$query = ' DELETE ' . ' FROM #__phocacart_product_discounts' . ' WHERE product_id = ' . (int) $productId;
$query2 = 'DELETE FROM #__phocacart_item_groups' . ' WHERE product_id = ' . (int) $productId . ' AND type = 4';
}
$db->setQuery($query);
$db->execute();
$db->setQuery($query2);
$db->execute();
}
}