147 lines
4.9 KiB
PHP
147 lines
4.9 KiB
PHP
<?php
|
|
class Discount extends DiscountCore
|
|
{
|
|
public $orders;
|
|
|
|
public static function createOrderDiscount($order, $productList, $qtyList, $name, $shipping_cost = false, $id_category = 0, $subcategory = 0, $value = 10)
|
|
{
|
|
$languages = Language::getLanguages($order);
|
|
|
|
// $products = $order->getProducts(false, $productList, $qtyList);
|
|
// Totals are stored in the order currency (or at least should be)
|
|
// $total = $order->getTotalProductsWithTaxes($products);
|
|
// $discounts = $order->getDiscounts(true);
|
|
// $total_tmp = $total;
|
|
// foreach ($discounts as $discount)
|
|
// {
|
|
// if ($discount['id_discount_type'] == 1)
|
|
// $total -= $total_tmp * ($discount['value'] / 100);
|
|
// elseif ($discount['id_discount_type'] == 2)
|
|
// $total -= ($discount['value'] * ($total_tmp / $order->total_products_wt));
|
|
// }
|
|
// $total = 10;
|
|
// if ($shipping_cost)
|
|
// $total += $order->total_shipping;
|
|
|
|
// create discount
|
|
$voucher = new Discount();
|
|
$voucher->id_discount_type = 2;
|
|
foreach ($languages as $language)
|
|
$voucher->description[$language['id_lang']] = strval($name).(int)($order->id);
|
|
$voucher->value = (float)($value);
|
|
$voucher->name = 'V0C'.(int)($order->id_customer).'O'.(int)($order->id);
|
|
$voucher->id_customer = (int)($order->id_customer);
|
|
$voucher->id_currency = (int)($order->id_currency);
|
|
$voucher->quantity = 1;
|
|
$voucher->quantity_per_user = 1;
|
|
$voucher->cumulable = 1;
|
|
$voucher->cumulable_reduction = 1;
|
|
$voucher->minimal = (float)($voucher->value);
|
|
$voucher->active = 1;
|
|
$voucher->cart_display = 1;
|
|
$now = time();
|
|
$voucher->date_from = date('Y-m-d H:i:s', $now);
|
|
$voucher->date_to = date('Y-m-d H:i:s', $now + (3600 * 24 * 365.25)); /* 1 year */
|
|
if (!$voucher->validateFieldsLang(false) OR !$voucher->add())
|
|
return false;
|
|
// set correct name
|
|
$voucher->name = 'V'.(int)($voucher->id).'C'.(int)($order->id_customer).'O'.$order->id;
|
|
if (!$voucher->update())
|
|
return false;
|
|
|
|
return $voucher;
|
|
}
|
|
|
|
public function updateCategories($categories)
|
|
{
|
|
return true;
|
|
/* false value will avoid category update and null value will force all category to be selected */
|
|
if ($categories === null){
|
|
$rootCategory = Category::getRootCategory();
|
|
$categories = array((int)$rootCategory->id);
|
|
}
|
|
parent::updateCategories($categories);
|
|
}
|
|
|
|
public static function getCategories($id_discount, $admDisplay=false)
|
|
{
|
|
if ( $admDisplay )
|
|
return parent::getCategories($id_discount);
|
|
|
|
$categories = parent::getCategories($id_discount);
|
|
// if only root category is checked return all categories.
|
|
if ( count($categories == 1) ){
|
|
$rootCategory = Category::getRootCategory();
|
|
if ( $categories[0]['id_category'] == (int)$rootCategory->id ){
|
|
$result = Db::getInstance()->ExecuteS('SELECT id_category FROM '._DB_PREFIX_.'category');
|
|
$categories = array();
|
|
foreach ($result as $row)
|
|
$categories[]['id_category'] = $row['id_category'];
|
|
return $categories;
|
|
}
|
|
}
|
|
return $categories;
|
|
}
|
|
|
|
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();
|
|
|
|
foreach ($products AS $product)
|
|
$totalAmount += $this->include_tax ? $product['total_wt'] : $product['total'];
|
|
|
|
if ($this->minimal > 0 AND $totalAmount < $this->minimal)
|
|
return 0;
|
|
switch ($this->id_discount_type)
|
|
{
|
|
/* Relative value (% of the order total) */
|
|
case 1:
|
|
$amount = 0;
|
|
$percentage = $this->value / 100;
|
|
foreach ($products AS $product)
|
|
if ($this->cumulable_reduction OR (!$product['reduction_applies'] AND !$product['on_sale']))
|
|
$amount += ($useTax? $product['total_wt'] : $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)
|
|
$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;
|
|
}
|
|
|
|
} |