class Discount extends DiscountCore {
public $restrict_products;
public $affiliate_coupon;
public $affiliate_id;
public function getFields()
{
$fields=parent::getFields();
$fields['restrict_products'] = (int)($this->restrict_products);
$fields['affiliate_id'] = pSQL($this->affiliate_id);
$fields['affiliate_coupon'] = intval($this->affiliate_coupon);
return $fields;
}
public function add($autodate = true, $nullValues = false, $categories = null)
{
$res=parent::add($autodate , $nullValues , $categories);
if ($res)
$this->updateProducts(Tools::getValue('productBox'));
return $res;
}
public function update($autodate = true, $nullValues = false, $categories = false)
{
$ret = NULL;
if (parent::update($autodate, $nullValues,$categories)) {
$ret = true;
$this->updateProducts(Tools::getValue('productBox'));
}
return $ret;
}
public function updateProducts($products)
{
/* false value will avoid category update and null value will force all category to be selected */
if ($products === false)
return ;
elseif (!is_array($products) OR !sizeof($products))
return false;
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'discount_product` WHERE `id_discount`='.(int)($this->id));
foreach($products AS $product)
{
Db::getInstance()->Execute('INSERT IGNORE INTO `'._DB_PREFIX_.'discount_product` (`id_discount`, `id_product`) VALUES('.(int)($this->id).','.(int)($product).')');
}
}
public function delete()
{
if (!parent::delete())
return false;
return (Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'discount_product WHERE id_discount = '.(int)($this->id)));
}
public static function getProducts($id_discount)
{
$res=array();
$rs=Db::getInstance()->ExecuteS('
SELECT `id_product`
FROM `'._DB_PREFIX_.'discount_product`
WHERE `id_discount` = '.(int)($id_discount));
foreach($rs as $r)
$res[]=$r['id_product'];
return $res;
}
public static function deleteByIdCustomer($id_customer)
{
$discounts = Db::getInstance()->ExecuteS('SELECT `id_discount` FROM `'._DB_PREFIX_.'discount` WHERE `id_customer` = '.(int)($id_customer));
foreach ($discounts as $discount)
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'discount_product` WHERE `id_discount` = '.(int)($discount['id_discount']));
return parent::deleteByIdCustomer($id_customer);
}
public static function deleteByIdGroup($id_group)
{
$discounts = Db::getInstance()->ExecuteS('SELECT `id_discount` FROM `'._DB_PREFIX_.'discount` WHERE `id_group` = '.(int)($id_group));
foreach ($discounts as $discount)
{
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'discount_product` WHERE `id_discount` = '.(int)($discount['id_discount']));
}
return true;
}
public function getValue($nb_discounts = 0, $order_total_products = 0, $shipping_fees = 0, $idCart = false, $useTax = true)
{
$totalAmount = 0;
$cart = new Cart((int)($idCart));
if (!Validate::isLoadedObject($cart))
return 0;
if ((!$this->cumulable AND (int)($nb_discounts) > 1) OR !$this->active OR (!$this->quantity AND !$cart->OrderExists()))
return 0;
if ($this->usedByCustomer((int)($cart->id_customer)) >= $this->quantity_per_user AND !$cart->OrderExists())
return 0;
$date_start = strtotime($this->date_from);
$date_end = strtotime($this->date_to);
if ((time() < $date_start OR time() > $date_end) AND !$cart->OrderExists()) return 0;
$products = $cart->getProducts();
$categories = Discount::getCategories((int)($this->id));
foreach ($products AS $product)
if (count($categories) AND Product::idIsOnCategoryId($product['id_product'], $categories))
$totalAmount += $useTax ? $product['price_wt'] * $product['quantity']: $product['price'] * $product['quantity'];
if ($this->minimal > 0 AND $totalAmount < $this->minimal)
return 0;
switch ($this->id_discount_type)
{
/* Relative value (% of the order total) */
case 1:
$percentage = $this->value / 100;
if ($this->name == '99CENTS')
{
$numprod = 0;
foreach ($products AS $product)
{
if (Product::idIsOnCategoryId($product['id_product'], $categories))
{
if ($this->cumulable_reduction OR (!$product['reduction_applies'] AND !$product['on_sale']))
{
if (strpos($product['reference'], '30ML') !== false
AND intval($product['quantity']) <= intval($product['stock_quantity'])
AND intval($numprod) < 3)
{
$numprod = intval($numprod) + intval($product['quantity']);
}
}
}
}
if (intval($numprod) > 3)
{
$numprod = 3;
}
$amount = intval($numprod) * 7;
}
else
{
foreach ($products AS $product)
{
if (Product::idIsOnCategoryId($product['id_product'], $categories))
{
if ($this->cumulable_reduction OR (!$product['reduction_applies'] AND !$product['on_sale']))
{
$amount += ($product['total']) * $percentage;
}
}
}
}
/*
foreach ($products AS $product)
{
if (Product::idIsOnCategoryId($product['id_product'], $categories))
{
if ($this->cumulable_reduction OR (!$product['reduction_applies'] AND !$product['on_sale']))
{
$amount += ($product['total']) * $percentage;
}
}
}
*/
return $amount;
/* Absolute value */
case 2:
// An "absolute" voucher is available in one currency only
$currency = ((int)$cart->id_currency ? Currency::getCurrencyInstance($cart->id_currency) : Currency::getCurrent());
if ($this->id_currency != $currency->id)
return 0;
$taxDiscount = Cart::getTaxesAverageUsed((int)($cart->id));
if (!$useTax AND isset($taxDiscount) AND $taxDiscount != 1)
//$this->value = abs($this->value / (1 + $taxDiscount * 0.01));
// Main return
$value = 0;
foreach ($products AS $product)
if (Product::idIsOnCategoryId($product['id_product'], $categories))
$value = $this->value;
// Return 0 if there are no applicable categories
return $value;
/* Free shipping (does not return a value but a special code) */
case 3:
return '!';
}
return 0;
}
}