bebeboutik/override/classes/Discount.php

192 lines
6.9 KiB
PHP
Raw Normal View History

2016-01-04 12:48:08 +01:00
<?php
class Discount extends DiscountCore
{
2016-04-21 17:41:04 +02:00
public $orders;
/**
* Return customer discounts
*
* @param integer $id_lang Language ID
* @param boolean $id_customer Customer ID
* @return array Discounts
*/
public static function getCustomerDiscounts($id_lang, $id_customer, $active = false, $includeGenericOnes = true, $stock = false)
{
global $cart;
$res = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT d.*, dtl.`name` AS `type`, dl.`description`
FROM `'._DB_PREFIX_.'discount` d
LEFT JOIN `'._DB_PREFIX_.'discount_lang` dl ON (d.`id_discount` = dl.`id_discount` AND dl.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'discount_type` dt ON dt.`id_discount_type` = d.`id_discount_type`
LEFT JOIN `'._DB_PREFIX_.'discount_type_lang` dtl ON (dt.`id_discount_type` = dtl.`id_discount_type` AND dtl.`id_lang` = '.(int)($id_lang).')
WHERE (`id_customer` = '.(int)($id_customer).'
OR `id_group` IN (SELECT `id_group` FROM `'._DB_PREFIX_.'customer_group` WHERE `id_customer` = '.(int)($id_customer).')'.
($includeGenericOnes ? ' OR (`id_customer` = 0 AND `id_group` = 0)' : '').')
'.($active ? ' AND d.`active` = 1' : '').'
'.($stock ? ' AND d.`quantity` != 0' : ''));
foreach ($res as $i => &$discount) {
if ($discount['quantity_per_user']) {
$quantity_used = Order::getDiscountsCustomer((int)($id_customer), (int)($discount['id_discount']));
if (isset($cart) AND isset($cart->id)) {
$quantity_used += $cart->getDiscountsCustomer((int)($discount['id_discount']));
}
$discount['quantity_for_user'] = $discount['quantity_per_user'] - $quantity_used;
}
else {
$discount['quantity_for_user'] = 0;
}
// Remove discount with zero quantities
if ($discount['quantity_for_user'] == 0) {
unset($res[$i]);
}
}
return $res;
}
public static function createOrderDiscount($order, $productList, $qtyList, $name, $shipping_cost = false, $id_category = 0, $subcategory = 0, $value = 10)
2016-01-04 12:48:08 +01:00
{
$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;
2016-01-04 12:48:08 +01:00
// 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);
2016-01-04 12:48:08 +01:00
$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);
2016-01-04 12:48:08 +01:00
$categories = parent::getCategories($id_discount);
// if only root category is checked return all categories.
2017-07-12 17:14:30 +02:00
if ( count($categories) == 1 ){
2016-01-04 12:48:08 +01:00
$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)
2016-01-04 12:48:08 +01:00
$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)
2016-01-04 12:48:08 +01:00
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;
2016-01-04 12:48:08 +01:00
// 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;
}
2016-01-04 12:48:08 +01:00
}