// Retro compatibility and updater improved for Discount/CartRules

This commit is contained in:
dMetzger
2011-11-02 14:23:44 +00:00
parent 61213efb5b
commit 4fbb25168e
21 changed files with 238 additions and 275 deletions
+15 -15
View File
@@ -33,13 +33,13 @@ class LoyaltyModule extends ObjectModel
public $id_loyalty_state;
public $id_customer;
public $id_order;
public $id_discount;
public $id_cart_rule;
public $points;
public $date_add;
public $date_upd;
protected $fieldsRequired = array('id_customer', 'points');
protected $fieldsValidate = array('id_loyalty_state' => 'isInt', 'id_customer' => 'isInt', 'id_discount' => 'isInt', 'id_order' => 'isInt', 'points' => 'isInt');
protected $fieldsValidate = array('id_loyalty_state' => 'isInt', 'id_customer' => 'isInt', 'id_cart_rule' => 'isInt', 'id_order' => 'isInt', 'points' => 'isInt');
protected $table = 'loyalty';
protected $identifier = 'id_loyalty';
@@ -50,7 +50,7 @@ class LoyaltyModule extends ObjectModel
$fields['id_loyalty_state'] = (int)$this->id_loyalty_state;
$fields['id_customer'] = (int)$this->id_customer;
$fields['id_order'] = (int)$this->id_order;
$fields['id_discount'] = (int)$this->id_discount;
$fields['id_cart_rule'] = (int)$this->id_cart_rule;
$fields['points'] = (int)$this->points;
$fields['date_add'] = pSQL($this->date_add);
$fields['date_upd'] = pSQL($this->date_upd);
@@ -110,8 +110,8 @@ class LoyaltyModule extends ObjectModel
}
$total += ($taxesEnabled == PS_TAX_EXC ? $product['price'] : $product['price_wt'])* (int)($product['cart_quantity']);
}
foreach ($cart->getDiscounts(false) AS $discount)
$total -= $discount['value_real'];
foreach ($cart->getCartRules(false) AS $cart_rule)
$total -= $cart_rule['value_real'];
}
return self::getNbPointsByPrice($total);
@@ -175,24 +175,24 @@ class LoyaltyModule extends ObjectModel
public static function getDiscountByIdCustomer($id_customer, $last=false)
{
$query = '
SELECT f.id_discount AS id_discount, f.date_upd AS date_add
SELECT f.id_cart_rule AS id_cart_rule, f.date_upd AS date_add
FROM `'._DB_PREFIX_.'loyalty` f
LEFT JOIN `'._DB_PREFIX_.'orders` o ON (f.`id_order` = o.`id_order`)
WHERE f.`id_customer` = '.(int)($id_customer).'
AND f.`id_discount` > 0
AND f.`id_cart_rule` > 0
AND o.`valid` = 1';
if ($last === true)
$query.= ' ORDER BY f.id_loyalty DESC LIMIT 0,1';
$query.= ' GROUP BY f.id_discount';
$query.= ' GROUP BY f.id_cart_rule';
return Db::getInstance()->executeS($query);
}
public static function registerDiscount($discount)
public static function registerDiscount($cartRule)
{
if (!Validate::isLoadedObject($discount))
die(Tools::displayError('Incorrect object Discount.'));
$items = self::getAllByIdCustomer((int)$discount->id_customer, NULL, true);
if (!Validate::isLoadedObject($cartRule))
die(Tools::displayError('Incorrect object CartRule.'));
$items = self::getAllByIdCustomer((int)$cartRule->id_customer, NULL, true);
foreach ($items AS $item)
{
$f = new LoyaltyModule((int)$item['id_loyalty']);
@@ -203,18 +203,18 @@ class LoyaltyModule extends ObjectModel
if ($f->points + $negativePoints <= 0)
continue;
$f->id_discount = (int)$discount->id;
$f->id_cart_rule = (int)$cartRule->id;
$f->id_loyalty_state = (int)LoyaltyStateModule::getConvertId();
$f->save();
}
}
public static function getOrdersByIdDiscount($id_discount)
public static function getOrdersByIdDiscount($id_cart_rule)
{
$items = Db::getInstance()->executeS('
SELECT f.id_order AS id_order, f.points AS points, f.date_upd AS date
FROM `'._DB_PREFIX_.'loyalty` f
WHERE f.id_discount = '.(int)($id_discount).' AND f.id_loyalty_state = '.(int)(LoyaltyStateModule::getConvertId()));
WHERE f.id_cart_rule = '.(int)$id_cart_rule.' AND f.id_loyalty_state = '.(int)LoyaltyStateModule::getConvertId());
if (!empty($items) AND is_array($items))
{
+24 -26
View File
@@ -47,56 +47,54 @@ if (Tools::getValue('transform-points') == 'true' AND $customerPoints > 0)
{
/* Generate a voucher code */
$voucherCode = NULL;
do $voucherCode = 'FID'.rand(1000, 100000);
while (Discount::discountExists($voucherCode));
do
$voucherCode = 'FID'.rand(1000, 100000);
while (CartRule::cartRuleExists($voucherCode));
/* Voucher creation and affectation to the customer */
$voucher = new Discount();
$voucher->name = $voucherCode;
$voucher->id_discount_type = Discount::AMOUNT; // Discount on order (amount)
$voucher->id_customer = (int)($cookie->id_customer);
$voucher->id_currency = (int)($cookie->id_currency);
$voucher->value = LoyaltyModule::getVoucherValue((int)$customerPoints);
$voucher->quantity = 1;
$voucher->quantity_per_user = 1;
$voucher->cumulable = 1;
$voucher->cumulable_reduction = 1;
$cartRule = new CartRule();
$cartRule->code = $voucherCode;
$cartRule->id_customer = (int)$cookie->id_customer;
$cartRule->id_currency = (int)$cookie->id_currency;
$cartRule->reduction_amount = LoyaltyModule::getVoucherValue((int)$customerPoints);
$cartRule->quantity = 1;
$cartRule->quantity_per_user = 1;
/* If merchandise returns are allowed, the voucher musn't be usable before this max return date */
$dateFrom = Db::getInstance()->getValue('
SELECT UNIX_TIMESTAMP(date_add) n
FROM '._DB_PREFIX_.'loyalty
WHERE id_discount = 0 AND id_customer = '.(int)$cookie->id_customer.'
WHERE id_cart_rule = 0 AND id_customer = '.(int)$cookie->id_customer.'
ORDER BY date_add DESC');
if (Configuration::get('PS_ORDER_RETURN'))
$dateFrom += 60 * 60 * 24 * (int)Configuration::get('PS_ORDER_RETURN_NB_DAYS');
$voucher->date_from = date('Y-m-d H:i:s', $dateFrom);
$voucher->date_to = date('Y-m-d H:i:s', $dateFrom + 31536000); // + 1 year
$cartRule->date_from = date('Y-m-d H:i:s', $dateFrom);
$cartRule->date_to = date('Y-m-d H:i:s', $dateFrom + 31536000); // + 1 year
$voucher->minimal = (float)Configuration::get('PS_LOYALTY_MINIMAL');
$voucher->active = 1;
$cartRule->minimum_amount = (float)Configuration::get('PS_LOYALTY_MINIMAL');
$cartRule->active = 1;
$categories = Configuration::get('PS_LOYALTY_VOUCHER_CATEGORY');
if ($categories != '' AND $categories != 0)
$categories = explode(',', Configuration::get('PS_LOYALTY_VOUCHER_CATEGORY'));
else
die(Tools::displayError());
die (Tools::displayError());
$languages = Language::getLanguages(true);
$default_text = Configuration::get('PS_LOYALTY_VOUCHER_DETAILS', (int)(Configuration::get('PS_LANG_DEFAULT')));
$default_text = Configuration::get('PS_LOYALTY_VOUCHER_DETAILS', (int)Configuration::get('PS_LANG_DEFAULT'));
foreach ($languages AS $language)
{
$text = Configuration::get('PS_LOYALTY_VOUCHER_DETAILS', (int)($language['id_lang']));
$voucher->description[(int)($language['id_lang'])] = $text ? strval($text) : strval($default_text);
$text = Configuration::get('PS_LOYALTY_VOUCHER_DETAILS', (int)$language['id_lang']);
$cartRule->name[(int)$language['id_lang']] = $text ? strval($text) : strval($default_text);
}
if (is_array($categories) AND sizeof($categories))
$voucher->add(true, false, $categories);
$cartRule->add(true, false, $categories);
else
$voucher->add();
$cartRule->add();
/* Register order(s) which contributed to create this voucher */
LoyaltyModule::registerDiscount($voucher);
@@ -125,13 +123,13 @@ $smarty->assign(array(
/* Discounts */
$nbDiscounts = 0;
$discounts = array();
if ($ids_discount = LoyaltyModule::getDiscountByIdCustomer((int)($cookie->id_customer)))
if ($ids_discount = LoyaltyModule::getDiscountByIdCustomer((int)$cookie->id_customer))
{
$nbDiscounts = count($ids_discount);
foreach ($ids_discount AS $key => $discount)
{
$discounts[$key] = new Discount((int)$discount['id_discount'], (int)($cookie->id_lang));
$discounts[$key]->orders = LoyaltyModule::getOrdersByIdDiscount((int)$discount['id_discount']);
$discounts[$key] = new Discount((int)$discount['id_cart_rule'], (int)$cookie->id_lang);
$discounts[$key]->orders = LoyaltyModule::getOrdersByIdDiscount((int)$discount['id_cart_rule']);
}
}
+2 -2
View File
@@ -104,14 +104,14 @@ class Loyalty extends Module
`id_loyalty_state` INT UNSIGNED NOT NULL DEFAULT 1,
`id_customer` INT UNSIGNED NOT NULL,
`id_order` INT UNSIGNED DEFAULT NULL,
`id_discount` INT UNSIGNED DEFAULT NULL,
`id_cart_rule` INT UNSIGNED DEFAULT NULL,
`points` INT NOT NULL DEFAULT 0,
`date_add` DATETIME NOT NULL,
`date_upd` DATETIME NOT NULL,
PRIMARY KEY (`id_loyalty`),
INDEX index_loyalty_loyalty_state (`id_loyalty_state`),
INDEX index_loyalty_order (`id_order`),
INDEX index_loyalty_discount (`id_discount`),
INDEX index_loyalty_discount (`id_cart_rule`),
INDEX index_loyalty_customer (`id_customer`)
) DEFAULT CHARSET=utf8 ;');