public static function insertDefaultData()
 {
     $defaultLanguage = intval(Configuration::get('PS_LANG_DEFAULT'));
     $default = new LoyaltyStateModule(LoyaltyStateModule::getDefaultId());
     $default->name[$defaultLanguage] = 'Validation';
     $default->save();
     $validation = new LoyaltyStateModule(LoyaltyStateModule::getValidationId());
     $validation->id_order_state = _PS_OS_DELIVERED_;
     $validation->name[$defaultLanguage] = 'Available';
     $validation->save();
     $cancel = new LoyaltyStateModule(LoyaltyStateModule::getCancelId());
     $cancel->id_order_state = _PS_OS_CANCELED_;
     $cancel->name[$defaultLanguage] = 'Canceled';
     $cancel->save();
     $convert = new LoyaltyStateModule(LoyaltyStateModule::getConvertId());
     $convert->name[$defaultLanguage] = 'Converted';
     $convert->save();
     $noneAward = new LoyaltyStateModule(LoyaltyStateModule::getNoneAwardId());
     $noneAward->name[$defaultLanguage] = 'Unavailable on discounts';
     $noneAward->save();
     return true;
 }
Example #2
0
 public static function insertDefaultData()
 {
     $loyaltyModule = new Loyalty();
     $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['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');
     foreach ($defaultTranslations as $loyaltyState) {
         $state = new LoyaltyStateModule((int) $loyaltyState['id_loyalty_state']);
         if (isset($loyaltyState['id_order_state'])) {
             $state->id_order_state = (int) $loyaltyState['id_order_state'];
         }
         $state->name[(int) Configuration::get('PS_LANG_DEFAULT')] = $loyaltyState['default'];
         foreach ($languages as $language) {
             if (isset($loyaltyState[$language['iso_code']])) {
                 $state->name[(int) $language['id_lang']] = $loyaltyState[$language['iso_code']];
             }
         }
         $state->save();
     }
     return true;
 }
Example #3
0
 /**
  * Assign summary template
  */
 public function assignSummaryExecution()
 {
     $customer_points = (int) LoyaltyModule::getPointsByCustomer((int) $this->context->customer->id);
     $orders = LoyaltyModule::getAllByIdCustomer((int) $this->context->customer->id, (int) $this->context->language->id);
     $displayorders = LoyaltyModule::getAllByIdCustomer((int) $this->context->customer->id, (int) $this->context->language->id, false, true, (int) Tools::getValue('n') > 0 ? (int) Tools::getValue('n') : 10, (int) Tools::getValue('p') > 0 ? (int) Tools::getValue('p') : 1);
     $this->context->smarty->assign(array('orders' => $orders, 'displayorders' => $displayorders, 'totalPoints' => (int) $customer_points, 'voucher' => LoyaltyModule::getVoucherValue($customer_points, (int) $this->context->currency->id), 'validation_id' => LoyaltyStateModule::getValidationId(), 'transformation_allowed' => $customer_points > 0, 'page' => (int) Tools::getValue('p') > 0 ? (int) Tools::getValue('p') : 1, 'nbpagination' => (int) Tools::getValue('n') > 0 ? (int) Tools::getValue('n') : 10, 'nArray' => array(10, 20, 50), 'max_page' => floor(count($orders) / ((int) Tools::getValue('n') > 0 ? (int) Tools::getValue('n') : 10)), 'pagination_link' => $this->getSummaryPaginationLink(array(), $this->context->smarty)));
     /* Discounts */
     $nb_discounts = 0;
     $discounts = array();
     if ($ids_discount = LoyaltyModule::getDiscountByIdCustomer((int) $this->context->customer->id)) {
         $nb_discounts = count($ids_discount);
         foreach ($ids_discount as $key => $discount) {
             $discounts[$key] = new CartRule((int) $discount['id_cart_rule'], (int) $this->context->cookie->id_lang);
             $discounts[$key]->orders = LoyaltyModule::getOrdersByIdDiscount((int) $discount['id_cart_rule']);
         }
     }
     $all_categories = Category::getSimpleCategories((int) $this->context->cookie->id_lang);
     $voucher_categories = Configuration::get('PS_LOYALTY_VOUCHER_CATEGORY');
     if ($voucher_categories != '' && $voucher_categories != 0) {
         $voucher_categories = explode(',', Configuration::get('PS_LOYALTY_VOUCHER_CATEGORY'));
     } else {
         die(Tools::displayError());
     }
     if (count($voucher_categories) == count($all_categories)) {
         $categories_names = null;
     } else {
         $categories_names = array();
         foreach ($all_categories as $k => $all_category) {
             if (in_array($all_category['id_category'], $voucher_categories)) {
                 $categories_names[$all_category['id_category']] = trim($all_category['name']);
             }
         }
         if (!empty($categories_names)) {
             $categories_names = Tools::truncate(implode(', ', $categories_names), 100) . '.';
         } else {
             $categories_names = null;
         }
     }
     $this->context->smarty->assign(array('nbDiscounts' => (int) $nb_discounts, 'discounts' => $discounts, 'minimalLoyalty' => (double) Configuration::get('PS_LOYALTY_MINIMAL'), 'categories' => $categories_names));
     $this->setTemplate('loyalty.tpl');
 }
Example #4
0
 public function hookCancelProduct($params)
 {
     if (!Validate::isLoadedObject($params['order']) or !Validate::isLoadedObject($orderDetail = new OrderDetail(intval($params['id_order_detail'])))) {
         return false;
     }
     if (!Validate::isLoadedObject($loyalty = new LoyaltyModule(intval(LoyaltyModule::getByOrderId(intval($params['order']->id)))))) {
         return false;
     }
     $loyalty->points = $loyalty->points - LoyaltyModule::getNbPointsByPrice($orderDetail->product_price * (1 + $orderDetail->tax_rate / 100) * $orderDetail->product_quantity);
     $loyalty->id_loyalty_state = LoyaltyStateModule::getCancelId();
     return $loyalty->save();
     // Automatically "historize" the modification
 }
Example #5
0
 public function hookCancelProduct($params)
 {
     include_once dirname(__FILE__) . '/LoyaltyStateModule.php';
     include_once dirname(__FILE__) . '/LoyaltyModule.php';
     if (!Validate::isLoadedObject($params['order']) || !Validate::isLoadedObject($order_detail = new OrderDetail((int) $params['id_order_detail'])) || !Validate::isLoadedObject($loyalty = new LoyaltyModule((int) LoyaltyModule::getByOrderId((int) $params['order']->id)))) {
         return false;
     }
     $loyalty_new = new LoyaltyModule();
     $loyalty_new->points = -1 * LoyaltyModule::getNbPointsByPrice(number_format($order_detail->total_price_tax_incl, 2, '.', ''));
     $loyalty_new->id_loyalty_state = (int) LoyaltyStateModule::getCancelId();
     $loyalty_new->id_order = (int) $params['order']->id;
     $loyalty_new->id_customer = (int) $loyalty->id_customer;
     $loyalty_new->add();
     return;
 }
    }
    if (is_array($categories) and sizeof($categories)) {
        $cartRule->add(true, false, $categories);
    } else {
        $cartRule->add();
    }
    /* Register order(s) which contributed to create this voucher */
    if (!LoyaltyModule::registerDiscount($cartRule)) {
        $cartRule->delete();
    }
    Tools::redirect('modules/loyalty/loyalty-program.php');
}
include dirname(__FILE__) . '/../../header.php';
$orders = LoyaltyModule::getAllByIdCustomer((int) $cookie->id_customer, (int) $cookie->id_lang);
$displayorders = LoyaltyModule::getAllByIdCustomer((int) $cookie->id_customer, (int) $cookie->id_lang, false, true, (int) Tools::getValue('n') > 0 ? (int) Tools::getValue('n') : 10, (int) Tools::getValue('p') > 0 ? (int) Tools::getValue('p') : 1);
$smarty->assign(array('orders' => $orders, 'displayorders' => $displayorders, 'pagination_link' => __PS_BASE_URI__ . 'modules/loyalty/loyalty-program.php', 'totalPoints' => (int) $customerPoints, 'voucher' => LoyaltyModule::getVoucherValue($customerPoints, (int) $context->currency->id), 'validation_id' => LoyaltyStateModule::getValidationId(), 'transformation_allowed' => $customerPoints > 0, 'page' => (int) Tools::getValue('p') > 0 ? (int) Tools::getValue('p') : 1, 'nbpagination' => (int) (Tools::getValue('n') > 0) ? (int) Tools::getValue('n') : 10, 'nArray' => array(10, 20, 50), 'max_page' => floor(sizeof($orders) / ((int) (Tools::getValue('n') > 0) ? (int) Tools::getValue('n') : 10))));
/* Discounts */
$nbDiscounts = 0;
$discounts = array();
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_cart_rule'], (int) $cookie->id_lang);
        $discounts[$key]->orders = LoyaltyModule::getOrdersByIdDiscount((int) $discount['id_cart_rule']);
    }
}
$allCategories = Category::getSimpleCategories((int) $cookie->id_lang);
$voucherCategories = Configuration::get('PS_LOYALTY_VOUCHER_CATEGORY');
if ($voucherCategories != '' and $voucherCategories != 0) {
    $voucherCategories = explode(',', Configuration::get('PS_LOYALTY_VOUCHER_CATEGORY'));
} else {
    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_cart_rule = ' . (int) $id_cart_rule . ' AND f.id_loyalty_state = ' . (int) LoyaltyStateModule::getConvertId());
        if (!empty($items) and is_array($items)) {
            foreach ($items as $key => $item) {
                $order = new Order((int) $item['id_order']);
                $items[$key]['id_currency'] = (int) $order->id_currency;
                $items[$key]['id_lang'] = (int) $order->id_lang;
                $items[$key]['total_paid'] = $order->total_paid;
                $items[$key]['total_shipping'] = $order->total_shipping;
            }
            return $items;
        }
        return false;
    }
    // + 1 year
    $voucher->minimal = 0;
    $voucher->active = 1;
    $languages = Language::getLanguages(true);
    $default_text = Configuration::get('PS_LOYALTY_VOUCHER_DETAILS', intval(Configuration::get('PS_LANG_DEFAULT')));
    foreach ($languages as $language) {
        $text = Configuration::get('PS_LOYALTY_VOUCHER_DETAILS', intval($language['id_lang']));
        $voucher->description[intval($language['id_lang'])] = $text ? strval($text) : strval($default_text);
    }
    $voucher->save();
    /* register order(s) which contribute to create this voucher */
    LoyaltyModule::registerDiscount($voucher);
    Tools::redirect('modules/loyalty/loyalty-program.php');
}
include dirname(__FILE__) . '/../../header.php';
$orders = LoyaltyModule::getAllByIdCustomer(intval($cookie->id_customer), intval($cookie->id_lang));
$smarty->assign(array('orders' => $orders, 'totalPoints' => $customerPoints, 'voucher' => LoyaltyModule::getVoucherValue($customerPoints, intval($cookie->id_currency)), 'validation_id' => LoyaltyStateModule::getValidationId(), 'transformation_allowed' => $customerPoints > 0));
/* Discounts */
$nbDiscounts = 0;
$discounts = array();
if ($ids_discount = LoyaltyModule::getDiscountByIdCustomer(intval($cookie->id_customer))) {
    $nbDiscounts = count($ids_discount);
    foreach ($ids_discount as $key => $discount) {
        $discounts[$key] = new Discount($discount['id_discount'], intval($cookie->id_lang));
        $discounts[$key]->date_add = $discount['date_add'];
        $discounts[$key]->orders = LoyaltyModule::getOrdersByIdDiscount($discount['id_discount']);
    }
}
$smarty->assign(array('nbDiscounts' => $nbDiscounts, 'discounts' => $discounts));
echo Module::display(dirname(__FILE__) . '/loyalty.php', 'loyalty.tpl');
include dirname(__FILE__) . '/../../footer.php';
Example #9
0
 public function hookCancelProduct($params)
 {
     global $cookie;
     include_once dirname(__FILE__) . '/LoyaltyStateModule.php';
     include_once dirname(__FILE__) . '/LoyaltyModule.php';
     $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;
     }
     $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;
 }