Skip to content
This repository has been archived by the owner on Nov 12, 2019. It is now read-only.

Commit

Permalink
[-] MO : Fix bug #PNM-1329 bad loyalty points calculation when cancel…
Browse files Browse the repository at this point in the history
…ling or returning products
  • Loading branch information
gRoussac committed Jun 5, 2013
1 parent 018c40f commit 9dae55c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 29 deletions.
4 changes: 3 additions & 1 deletion admin-dev/tabs/AdminOrders.php
Expand Up @@ -300,7 +300,9 @@ public function postProcess()
// Delete product
if (!$order->deleteProduct($order, $orderDetail, $qtyCancelProduct))
$this->_errors[] = Tools::displayError('An error occurred during deletion of the product.').' <span class="bold">'.Tools::safeOutput($orderDetail->product_name).'</span>';
Module::hookExec('cancelProduct', array('order' => $order, 'id_order_detail' => (int)$id_order_detail));

// id_order_detail is kept for retro-compatibility
Module::hookExec('cancelProduct', array('order' => $order, 'id_order_detail' => (int)$orderDetail->id, 'order_detail' => $orderDetail));
}

if (!count($this->_errors) && $customizationList)
Expand Down
34 changes: 25 additions & 9 deletions modules/loyalty/LoyaltyModule.php
Expand Up @@ -74,6 +74,19 @@ public static function getByOrderId($id_order)

return isset($result['id_loyalty']) ? $result['id_loyalty'] : false;
}

public static function getAllByOrderId($id_order)
{
if (!Validate::isUnsignedId($id_order))
return false;

$result = Db::getInstance()->executeS('
SELECT f.id_loyalty
FROM `'._DB_PREFIX_.'loyalty` f
WHERE f.id_order = '.(int)($id_order));

return $result;
}

public static function getOrderNbPoints($order)
{
Expand Down Expand Up @@ -108,13 +121,14 @@ public static function getCartNbPoints($cart, $newProduct = null)
$smarty->assign('no_pts_discounted', 1);
continue;
}
$total += ($taxesEnabled == PS_TAX_EXC ? $product['price'] : $product['price_wt']) * (int)$product['cart_quantity'];
$price = $taxesEnabled == PS_TAX_EXC ? $product['price'] : $product['price_wt'];
$total += self::getNbPointsByPrice($price, isset($cart->id_currency) ? (int)$cart->id_currency : 0) * (int)$product['cart_quantity'];
}
foreach ($cart->getDiscounts(false) as $discount)
$total -= $discount['value_real'];
$total -= self::getNbPointsByPrice($discount['value_real'], isset($cart->id_currency) ? (int)$cart->id_currency : 0);;
}

return self::getNbPointsByPrice($total, isset($cart->id_currency) ? (int)$cart->id_currency : 0);
return $total;
}

public static function getVoucherValue($nbPoints, $id_currency = NULL)
Expand Down Expand Up @@ -149,18 +163,20 @@ public static function getNbPointsByPrice($price, $id_currency = 0)

public static function getPointsByCustomer($id_customer)
{
return
Db::getInstance()->getValue('

$a = Db::getInstance()->getValue('
SELECT SUM(f.points) points
FROM `'._DB_PREFIX_.'loyalty` f
WHERE f.id_customer = '.(int)($id_customer).'
AND f.id_loyalty_state IN ('.(int)(LoyaltyStateModule::getValidationId()).', '.(int)(LoyaltyStateModule::getNoneAwardId()).')')
+
Db::getInstance()->getValue('
AND f.id_loyalty_state IN ('.(int)(LoyaltyStateModule::getValidationId()).', '.(int)(LoyaltyStateModule::getNoneAwardId()).')');

$b = Db::getInstance()->getValue('
SELECT SUM(f.points) points
FROM `'._DB_PREFIX_.'loyalty` f
WHERE f.id_customer = '.(int)($id_customer).'
AND f.id_loyalty_state = '.(int)LoyaltyStateModule::getCancelId().' AND points < 0');
AND f.id_loyalty_state = '.(int)LoyaltyStateModule::getCancelId().' AND points < 0');

return (int)$a + $b;
}

public static function getAllByIdCustomer($id_customer, $id_lang, $onlyValidate = false, $pagination = false, $nb = 10, $page = 1)
Expand Down
2 changes: 1 addition & 1 deletion modules/loyalty/LoyaltyStateModule.php
Expand Up @@ -70,7 +70,7 @@ public static function insertDefaultData()
$languages = Language::getLanguages();

$defaultTranslations = array('default' => array('id_loyalty_state' => (int)LoyaltyStateModule::getDefaultId(), 'default' => $loyaltyModule->getL('Awaiting validation'), 'en' => 'Awaiting validation', 'fr' => 'En attente de validation'));
$defaultTranslations['validated'] = array('id_loyalty_state' => (int)LoyaltyStateModule::getValidationId(), 'id_order_state' => Configuration::get('PS_OS_DELIVERED'), 'default' => $loyaltyModule->getL('Available'), 'en' => 'Available', 'fr' => 'Disponible');
$defaultTranslations['validated'] = array('id_loyalty_state' => (int)LoyaltyStateModule::getValidationId(), 'id_order_state' => Configuration::get('PS_OS_DELIVERED'), 'default' => $loyaltyModule->getL('Available'), 'en' => 'Available', 'fr' => 'Disponibles');
$defaultTranslations['cancelled'] = array('id_loyalty_state' => (int)LoyaltyStateModule::getCancelId(), 'id_order_state' => Configuration::get('PS_OS_CANCELED'), 'default' => $loyaltyModule->getL('Cancelled'), 'en' => 'Cancelled', 'fr' => 'Annulés');
$defaultTranslations['converted'] = array('id_loyalty_state' => (int)LoyaltyStateModule::getConvertId(), 'default' => $loyaltyModule->getL('Already converted'), 'en' => 'Already converted', 'fr' => 'Déjà convertis');
$defaultTranslations['none_award'] = array('id_loyalty_state' => (int)LoyaltyStateModule::getNoneAwardId(), 'default' => $loyaltyModule->getL('Unavailable on discounts'), 'en' => 'Unavailable on discounts', 'fr' => 'Non disponbile sur produits remisés');
Expand Down
1 change: 1 addition & 0 deletions modules/loyalty/loyalty-program.php
Expand Up @@ -40,6 +40,7 @@
Tools::addJS(array(_PS_JS_DIR_.'jquery/jquery.dimensions.js',_PS_JS_DIR_.'jquery/jquery.cluetip.js'));

$customerPoints = (int)(LoyaltyModule::getPointsByCustomer((int)($cookie->id_customer)));
$customerPoints = $customerPoints < 0 ? 0 : $customerPoints;

/* transform point into voucher if needed */
if (Tools::getValue('transform-points') == 'true' AND $customerPoints > 0)
Expand Down
78 changes: 60 additions & 18 deletions modules/loyalty/loyalty.php
Expand Up @@ -69,9 +69,9 @@ public function install()

if (!parent::install() OR !$this->installDB() OR !$this->registerHook('extraRight') OR !$this->registerHook('updateOrderStatus')
OR !$this->registerHook('newOrder') OR !$this->registerHook('adminCustomers') OR !$this->registerHook('shoppingCart')
OR !$this->registerHook('orderReturn') OR !$this->registerHook('cancelProduct') OR !$this->registerHook('customerAccount')
OR !Configuration::updateValue('PS_LOYALTY_POINT_VALUE', '0.20') OR !Configuration::updateValue('PS_LOYALTY_MINIMAL', 0)
OR !Configuration::updateValue('PS_LOYALTY_POINT_RATE', '10') OR !Configuration::updateValue('PS_LOYALTY_NONE_AWARD', '1'))
OR !$this->registerHook('orderReturn') OR !$this->registerHook('cancelProduct') OR !$this->registerHook('customerAccount')
OR !Configuration::updateValue('PS_LOYALTY_POINT_VALUE', '0.02') OR !Configuration::updateValue('PS_LOYALTY_MINIMAL', 0)
OR !Configuration::updateValue('PS_LOYALTY_POINT_RATE', '1') OR !Configuration::updateValue('PS_LOYALTY_NONE_AWARD', '1'))
return false;

$defaultTranslations = array('en' => 'Loyalty reward', 'fr' => 'Récompense fidélité');
Expand Down Expand Up @@ -586,7 +586,9 @@ public function hookUpdateOrderStatus($params)
if ($order AND !Validate::isLoadedObject($order))
die(Tools::displayError('Incorrect object Order.'));
$this->instanceDefaultStates();


$return = false;

if ($newOrder->id == $this->loyaltyStateValidation->id_order_state OR $newOrder->id == $this->loyaltyStateCancel->id_order_state)
{
if (!Validate::isLoadedObject($loyalty = new LoyaltyModule(LoyaltyModule::getByOrderId($order->id))))
Expand All @@ -599,15 +601,23 @@ public function hookUpdateOrderStatus($params)
$loyalty->id_loyalty_state = LoyaltyStateModule::getValidationId();
if ((int)($loyalty->points) < 0)
$loyalty->points = abs((int)($loyalty->points));
$return &= $loyalty->save();
}
elseif ($newOrder->id == $this->loyaltyStateCancel->id_order_state)
{
$loyalty->id_loyalty_state = LoyaltyStateModule::getCancelId();
$loyalty->points = 0;
$loyalties = LoyaltyModule::getAllByOrderId($order->id);
if(is_array($loyalties))
foreach($loyalties as $entity)
{
$loyalty = new LoyaltyModule($entity['id_loyalty']);
$loyalty->id_loyalty_state = LoyaltyStateModule::getCancelId();
$loyalty->points = 0;
$return &= $loyalty->save();
}
}
return $loyalty->save();
}
return true;
return $return;
}

/* Hook display in tab AdminCustomers on BO */
Expand Down Expand Up @@ -663,22 +673,54 @@ public function hookAdminCustomers($params)

public function hookCancelProduct($params)
{
global $cookie;
include_once(dirname(__FILE__).'/LoyaltyStateModule.php');
include_once(dirname(__FILE__).'/LoyaltyModule.php');

if (!Validate::isLoadedObject($params['order']) OR !Validate::isLoadedObject($orderDetail = new OrderDetail((int)($params['id_order_detail'])))
OR !Validate::isLoadedObject($loyalty = new LoyaltyModule((int)(LoyaltyModule::getByOrderId((int)($params['order']->id))))))
$orderDetail = '';
if (!isset($params['order_detail']) || !Validate::isLoadedObject($params['order_detail']))
{
if (Validate::isLoadedObject($orderDetail = new OrderDetail((int)($params['id_order_detail']))))
$orderDetail = get_object_vars($orderDetail);
}
else
$orderDetail = get_object_vars($params['order_detail']);

if (!Validate::isLoadedObject($order = $params['order']) || !Validate::isLoadedObject($loyalty = new LoyaltyModule((int)LoyaltyModule::getByOrderId((int)($params['order']->id)))))
return false;

if (is_array($orderDetail) && count($orderDetail))
$order->setProductPrices($orderDetail);
else
return false;

$loyaltyNew = new LoyaltyModule();
$loyaltyNew->points = -1 * LoyaltyModule::getNbPointsByPrice(number_format($orderDetail->product_price * (1 + $orderDetail->tax_rate / 100), 2, '.', ''), (int)$params['order']->id_currency) * $orderDetail->product_quantity;
$loyaltyNew->id_loyalty_state = (int)LoyaltyStateModule::getCancelId();
$loyaltyNew->id_order = (int)$params['order']->id;
$loyaltyNew->id_customer = (int)$loyalty->id_customer;
$loyaltyNew->add();

return;
}
$qtyList = Tools::getValue('cancelQuantity');
if (isset($qtyList[$orderDetail['id_order_detail']]))
$qtyList = abs((int)$qtyList[$orderDetail['id_order_detail']]);
else
$qtyList = (int)$orderDetail['product_quantity'];

$product_price = $order->getTaxCalculationMethod() == PS_TAX_EXC ? $orderDetail['product_price'] + $orderDetail['ecotax'] : $orderDetail['product_price_wt'];
$points = (int)LoyaltyModule::getNbPointsByPrice(number_format($product_price, 2, '.', ''), (int)$params['order']->id_currency) * $qtyList;

if (!$order->hasBeenDelivered())
{
if ($points > $loyalty->points)
$points = (int)$loyalty->points;
$loyalty->points = $loyalty->points - $points;
return $loyalty->save();
}
else
{
$loyaltyNew = new LoyaltyModule();
$loyaltyNew->points = -1 * $points;
$loyaltyNew->id_loyalty_state = (int)LoyaltyStateModule::getCancelId();
$loyaltyNew->id_order = (int)$params['order']->id;
$loyaltyNew->id_customer = (int)$loyalty->id_customer;
return $loyaltyNew->add();
}
return false;
}

public function getL($key)
{
Expand Down

0 comments on commit 9dae55c

Please sign in to comment.