* @copyright 2007-2012 PrestaShop SA
* @version Release: $Revision: 7040 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_'))
exit;
/*
* TODO:
*
* - Bad behaviour when an order is cancelled after an order return
* - We shouldn't use $cookie->id_currency in all situations
*/
class Loyalty extends Module
{
public function __construct()
{
$this->name = 'loyalty';
$this->tab = 'pricing_promotion';
$this->version = '1.8';
$this->author = 'PrestaShop';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Customer loyalty and rewards');
$this->description = $this->l('Provide a loyalty program to your customers.');
$this->confirmUninstall = $this->l('Are you sure you want to delete all loyalty points and customer history?');
}
private function instanceDefaultStates()
{
include_once(dirname(__FILE__).'/LoyaltyStateModule.php');
/* Recover default loyalty status save at module installation */
$this->loyaltyStateDefault = new LoyaltyStateModule(LoyaltyStateModule::getDefaultId());
$this->loyaltyStateValidation = new LoyaltyStateModule(LoyaltyStateModule::getValidationId());
$this->loyaltyStateCancel = new LoyaltyStateModule(LoyaltyStateModule::getCancelId());
$this->loyaltyStateConvert = new LoyaltyStateModule(LoyaltyStateModule::getConvertId());
$this->loyaltyStateNoneAward = new LoyaltyStateModule(LoyaltyStateModule::getNoneAwardId());
}
public function install()
{
include_once(dirname(__FILE__).'/LoyaltyStateModule.php');
if (!parent::install() || !$this->installDB() || !$this->registerHook('extraRight') || !$this->registerHook('updateOrderStatus')
|| !$this->registerHook('newOrder') || !$this->registerHook('adminCustomers') || !$this->registerHook('shoppingCart')
|| !$this->registerHook('orderReturn') || !$this->registerHook('cancelProduct') || !$this->registerHook('customerAccount')
|| !Configuration::updateValue('PS_LOYALTY_POINT_VALUE', '0.20') || !Configuration::updateValue('PS_LOYALTY_MINIMAL', 0)
|| !Configuration::updateValue('PS_LOYALTY_POINT_RATE', '10') || !Configuration::updateValue('PS_LOYALTY_NONE_AWARD', '1'))
return false;
$defaultTranslations = array('en' => 'Loyalty reward', 'fr' => 'Récompense fidélité');
$conf = array((int)Configuration::get('PS_LANG_DEFAULT') => $this->l('Loyalty reward'));
foreach (Language::getLanguages() as $language)
if (isset($defaultTranslations[$language['iso_code']]))
$conf[(int)$language['id_lang']] = $defaultTranslations[$language['iso_code']];
Configuration::updateValue('PS_LOYALTY_VOUCHER_DETAILS', $conf);
$category_config = '';
$categories = Category::getSimpleCategories((int)(Configuration::get('PS_LANG_DEFAULT')));
foreach ($categories as $category)
$category_config .= (int)$category['id_category'].',';
$category_config = rtrim($category_config, ',');
Configuration::updateValue('PS_LOYALTY_VOUCHER_CATEGORY', $category_config);
/* This hook is optional */
$this->registerHook('displayMyAccountBlock');
if (!LoyaltyStateModule::insertDefaultData())
return false;
return true;
}
public function installDB()
{
Db::getInstance()->execute('
CREATE TABLE `'._DB_PREFIX_.'loyalty` (
`id_loyalty` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`id_loyalty_state` INT UNSIGNED NOT NULL DEFAULT 1,
`id_customer` INT UNSIGNED NOT NULL,
`id_order` 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_cart_rule`),
INDEX index_loyalty_customer (`id_customer`)
) DEFAULT CHARSET=utf8 ;');
Db::getInstance()->execute('
CREATE TABLE `'._DB_PREFIX_.'loyalty_history` (
`id_loyalty_history` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`id_loyalty` INT UNSIGNED DEFAULT NULL,
`id_loyalty_state` INT UNSIGNED NOT NULL DEFAULT 1,
`points` INT NOT NULL DEFAULT 0,
`date_add` DATETIME NOT NULL,
PRIMARY KEY (`id_loyalty_history`),
INDEX `index_loyalty_history_loyalty` (`id_loyalty`),
INDEX `index_loyalty_history_loyalty_state` (`id_loyalty_state`)
) DEFAULT CHARSET=utf8 ;');
Db::getInstance()->execute('
CREATE TABLE `'._DB_PREFIX_.'loyalty_state` (
`id_loyalty_state` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`id_order_state` INT UNSIGNED DEFAULT NULL,
PRIMARY KEY (`id_loyalty_state`),
INDEX index_loyalty_state_order_state (`id_order_state`)
) DEFAULT CHARSET=utf8 ;');
Db::getInstance()->execute('
CREATE TABLE `'._DB_PREFIX_.'loyalty_state_lang` (
`id_loyalty_state` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`id_lang` INT UNSIGNED NOT NULL,
`name` varchar(64) NOT NULL,
UNIQUE KEY `index_unique_loyalty_state_lang` (`id_loyalty_state`,`id_lang`)
) DEFAULT CHARSET=utf8 ;');
return true;
}
public function uninstall()
{
if (!parent::uninstall() || !$this->uninstallDB() || !Configuration::deleteByName('PS_LOYALTY_POINT_VALUE') || !Configuration::deleteByName('PS_LOYALTY_POINT_RATE')
|| !Configuration::deleteByName('PS_LOYALTY_NONE_AWARD') || !Configuration::deleteByName('PS_LOYALTY_MINIMAL') || !Configuration::deleteByName('PS_LOYALTY_VOUCHER_CATEGORY')
|| !Configuration::deleteByName('PS_LOYALTY_VOUCHER_DETAILS'))
return false;
return true;
}
public function uninstallDB()
{
Db::getInstance()->execute('DROP TABLE `'._DB_PREFIX_.'loyalty`;');
Db::getInstance()->execute('DROP TABLE `'._DB_PREFIX_.'loyalty_state`;');
Db::getInstance()->execute('DROP TABLE `'._DB_PREFIX_.'loyalty_state_lang`;');
Db::getInstance()->execute('DROP TABLE `'._DB_PREFIX_.'loyalty_history`;');
return true;
}
private function _postProcess()
{
if (Tools::isSubmit('submitLoyalty'))
{
$id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT');
$languages = Language::getLanguages();
$this->_errors = array();
if (!is_array(Tools::getValue('categoryBox')) || !count(Tools::getValue('categoryBox')))
$this->_errors[] = $this->l('You must choose at least one category for voucher\'s action');
if (!count($this->_errors))
{
Configuration::updateValue('PS_LOYALTY_VOUCHER_CATEGORY', $this->voucherCategories(Tools::getValue('categoryBox')));
Configuration::updateValue('PS_LOYALTY_POINT_VALUE', (float)(Tools::getValue('point_value')));
Configuration::updateValue('PS_LOYALTY_POINT_RATE', (float)(Tools::getValue('point_rate')));
Configuration::updateValue('PS_LOYALTY_NONE_AWARD', (int)(Tools::getValue('PS_LOYALTY_NONE_AWARD')));
Configuration::updateValue('PS_LOYALTY_MINIMAL', (float)(Tools::getValue('minimal')));
$this->loyaltyStateValidation->id_order_state = (int)(Tools::getValue('id_order_state_validation'));
$this->loyaltyStateCancel->id_order_state = (int)(Tools::getValue('id_order_state_cancel'));
$arrayVoucherDetails = array();
foreach ($languages as $language)
{
$arrayVoucherDetails[(int)($language['id_lang'])] = Tools::getValue('voucher_details_'.(int)($language['id_lang']));
$this->loyaltyStateDefault->name[(int)($language['id_lang'])] = Tools::getValue('default_loyalty_state_'.(int)($language['id_lang']));
$this->loyaltyStateValidation->name[(int)($language['id_lang'])] = Tools::getValue('validation_loyalty_state_'.(int)($language['id_lang']));
$this->loyaltyStateCancel->name[(int)($language['id_lang'])] = Tools::getValue('cancel_loyalty_state_'.(int)($language['id_lang']));
$this->loyaltyStateConvert->name[(int)($language['id_lang'])] = Tools::getValue('convert_loyalty_state_'.(int)($language['id_lang']));
$this->loyaltyStateNoneAward->name[(int)($language['id_lang'])] = Tools::getValue('none_award_loyalty_state_'.(int)($language['id_lang']));
}
if (empty($arrayVoucherDetails[$id_lang_default]))
$arrayVoucherDetails[$id_lang_default] = ' ';
Configuration::updateValue('PS_LOYALTY_VOUCHER_DETAILS', $arrayVoucherDetails);
if (empty($this->loyaltyStateDefault->name[$id_lang_default]))
$this->loyaltyStateDefault->name[$id_lang_default] = ' ';
$this->loyaltyStateDefault->save();
if (empty($this->loyaltyStateValidation->name[$id_lang_default]))
$this->loyaltyStateValidation->name[$id_lang_default] = ' ';
$this->loyaltyStateValidation->save();
if (empty($this->loyaltyStateCancel->name[$id_lang_default]))
$this->loyaltyStateCancel->name[$id_lang_default] = ' ';
$this->loyaltyStateCancel->save();
if (empty($this->loyaltyStateConvert->name[$id_lang_default]))
$this->loyaltyStateConvert->name[$id_lang_default] = ' ';
$this->loyaltyStateConvert->save();
if (empty($this->loyaltyStateNoneAward->name[$id_lang_default]))
$this->loyaltyStateNoneAward->name[$id_lang_default] = ' ';
$this->loyaltyStateNoneAward->save();
echo $this->displayConfirmation($this->l('Settings updated.'));
}
else
{
$errors = '';
foreach ($this->_errors as $error)
$errors .= $error.'
';
echo $this->displayError($errors);
}
// redirect($this->context->link);
}
}
private function voucherCategories($categories)
{
$cat = '';
if ($categories)
foreach ($categories as $category)
$cat .= $category.',';
return rtrim($cat, ',');
}
public function getContent()
{
$this->instanceDefaultStates();
$this->_postProcess();
$order_states = OrderState::getOrderStates($this->context->language->id);
$currency = new Currency((int)(Configuration::get('PS_CURRENCY_DEFAULT')));
$id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT');
$languages = Language::getLanguages(false);
$languageIds = 'voucher_details¤default_loyalty_state¤none_award_loyalty_state¤convert_loyalty_state¤validation_loyalty_state¤cancel_loyalty_state';
$html = '
';
$html .= '
| '.$this->l('Order').' | '.$this->l('Date').' | '.$this->l('Total (without shipping)').' | '.$this->l('Points').' | '.$this->l('Points Status').' |
|---|---|---|---|---|
| '.((int)$loyalty['id'] > 0 ? ''.sprintf($this->l('#%d'), $loyalty['id']).'' : '--').' | '.Tools::displayDate($loyalty['date'], (int)$params['cookie']->id_lang).' | '.((int)$loyalty['id'] > 0 ? $loyalty['total_without_shipping'] : '--').' | '.(int)$loyalty['points'].' | '.$loyalty['state'].' |
| '.$this->l('Total points available:').' | '.$points.' | '.$this->l('Voucher value:').' '.Tools::displayPrice( LoyaltyModule::getVoucherValue((int)$points, (int)Configuration::get('PS_CURRENCY_DEFAULT')), new Currency((int)Configuration::get('PS_CURRENCY_DEFAULT')) ).' | ||