391 lines
17 KiB
PHP
Raw Normal View History

2016-04-14 16:14:31 +02:00
<?php
/*
* 2007-2014 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2014 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class OrderController extends OrderControllerCore
{
/**
* @Override
* @see ParentOrderController::init()
*/
public function init()
{
global $orderTotal;
ParentOrderController::init();
$this->step = (int)(Tools::getValue('step'));
if (!$this->nbProducts)
$this->step = -1;
/**
* @Override
*/
// If some products have disappear
$isProductOut = $this->context->cart->checkQuantitiesMore();
if ($isProductOut) {
$this->step = 0;
//$this->errors[] = Tools::displayError('An item in your cart is no longer available in this quantity. You cannot proceed with your order until the quantity is adjusted.');
$this->errors[] = sprintf(Tools::displayError('%s n\'est plus disponible dans cette quantité. Vous ne pouvez pas procéder à la commande tant que la quantité n\'est pas ajustée.'), $isProductOut['name']);
}
// Check minimal amount
$currency = Currency::getCurrency((int)$this->context->cart->id_currency);
$orderTotal = $this->context->cart->getOrderTotal();
$minimal_purchase = Tools::convertPrice((float)Configuration::get('PS_PURCHASE_MINIMUM'), $currency);
if ($this->context->cart->getOrderTotal(false, Cart::ONLY_PRODUCTS) < $minimal_purchase && $this->step > 0)
{
$this->step = 0;
$this->errors[] = sprintf(
Tools::displayError('A minimum purchase total of %1s (tax excl.) is required in order to validate your order, current purchase total is %2s (tax excl.).'),
Tools::displayPrice($minimal_purchase, $currency), Tools::displayPrice($this->context->cart->getOrderTotal(false, Cart::ONLY_PRODUCTS), $currency)
);
}
if (!$this->context->customer->isLogged(true) && in_array($this->step, array(1, 2, 3)))
{
$back_url = $this->context->link->getPageLink('order', true, (int)$this->context->language->id, array('step' => $this->step, 'multi-shipping' => (int)Tools::getValue('multi-shipping')));
$params = array('multi-shipping' => (int)Tools::getValue('multi-shipping'), 'display_guest_checkout' => (int)Configuration::get('PS_GUEST_CHECKOUT_ENABLED'), 'back' => $back_url);
Tools::redirect($this->context->link->getPageLink('authentication', true, (int)$this->context->language->id, $params));
}
if (Tools::getValue('multi-shipping') == 1)
$this->context->smarty->assign('multi_shipping', true);
else
$this->context->smarty->assign('multi_shipping', false);
if ($this->context->customer->id)
$this->context->smarty->assign('address_list', $this->context->customer->getAddresses($this->context->language->id));
else
$this->context->smarty->assign('address_list', array());
}
/**
* Assign template vars related to page content
* @see FrontController::initContent()
*/
public function initContent()
{
ParentOrderController::initContent();
if (Tools::isSubmit('ajax') && Tools::getValue('method') == 'updateExtraCarrier')
{
// Change virtualy the currents delivery options
$delivery_option = $this->context->cart->getDeliveryOption();
$delivery_option[(int)Tools::getValue('id_address')] = Tools::getValue('id_delivery_option');
$this->context->cart->setDeliveryOption($delivery_option);
$this->context->cart->save();
$return = array(
'content' => Hook::exec(
'displayCarrierList',
array(
'address' => new Address((int)Tools::getValue('id_address'))
)
)
);
die(Tools::jsonEncode($return));
}
if ($this->nbProducts)
$this->context->smarty->assign('virtual_cart', $this->context->cart->isVirtualCart());
if (!Tools::getValue('multi-shipping'))
$this->context->cart->setNoMultishipping();
// 4 steps to the order
switch ((int)$this->step)
{
case -1;
// // Hack referralprogram Cart rule compatibily
// Db::getInstance()->update('referralprogram', array('id_cart_rule' => 0));
// $sql = "SELECT DISTINCT(id_cart_rule_sponsor) FROM "._DB_PREFIX_."referralprogram";
// $results = Db::getInstance()->ExecuteS($sql);
// foreach($results as $row)
// {
// //PS_cart_rule
// Db::getInstance()->update('cart_rule', array('cart_rule_restriction' => 1),'id_cart_rule='.$row['id_cart_rule_sponsor']);
// //PS_cart_rule_combination
// Db::getInstance()->delete('cart_rule_combination','id_cart_rule_1='.$row['id_cart_rule_sponsor'].' OR id_cart_rule_2='.$row['id_cart_rule_sponsor']);
// }
/** @Override [ticket 7902] */
$all_cart_rules = CartRule::getCustomerCartRules($this->context->language->id, $this->context->customer->id, true, true, true);
$notEligible = false;
$cartRulesAlreadyInCart = $this->context->cart->getCartRules(CartRule::FILTER_ACTION_SHIPPING);
foreach ($all_cart_rules as $key => $cart_rule) {
if($cart_rule['id_discount_type'] == Discount::FREE_SHIPPING){
$cr = new CartRule((int)$cart_rule['id_cart_rule']);
if($cr->checkValidityWithoutAmount($this->context,false,false) && $cart_rule['minimal']>0){
if(!isset($minimum_amount))
$minimum_amount = $cart_rule['minimal'];
if($minimum_amount < $cart_rule['minimal'])
continue;
$minimum_amount = ($minimum_amount < $cart_rule['minimal'])?$minimum_amount:$cart_rule['minimal'];
$minimum_amount_tax = (int)$cart_rule['minimum_amount_tax'] ;
} elseif (!$cr->checkValidityWithoutAmount($this->context,false,false) && empty($cartRulesAlreadyInCart)) {
$notEligible = true;
}
}
}
$free_shipping_ok = false;
if (!empty($cartRulesAlreadyInCart)) {
$free_shipping_ok = true;
}
$taxCalculationMethod = Group::getPriceDisplayMethod((int)Group::getCurrent()->id);
$useTax = !($taxCalculationMethod == PS_TAX_EXC);
$totalWithoutShipping = floatval($this->context->cart->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING));
$totalWithoutShippingAndTax = floatval($this->context->cart->getOrderTotal(false, Cart::BOTH_WITHOUT_SHIPPING));
// $nbOrder = Order::getCustomerNbOrders($this->context->customer->id); // Ajouter en condition s'il faut checker la première commande
if (isset($minimum_amount) && !$this->context->cart->getCartRules(CartRule::FILTER_ACTION_SHIPPING)){
if($minimum_amount_tax && $minimum_amount > $totalWithoutShipping)
$remaining_for_free_shipping = $minimum_amount - $totalWithoutShipping;
elseif(!$minimum_amount_tax && $minimum_amount > $totalWithoutShippingAndTax)
$remaining_for_free_shipping = $minimum_amount - $totalWithoutShippingAndTax;
}
$this->context->smarty->assign('remaining_for_free_shipping', $remaining_for_free_shipping);
$this->context->smarty->assign('notEligible', $notEligible);
$this->context->smarty->assign('free_shipping_ok', $free_shipping_ok);
$this->context->smarty->assign('minimum_amount', $minimum_amount);
/** end @Override [ticket 7902] */
/** @Override [ticket 9073] */
$otherDelay = $this->context->cart->checkProductOtherDeliveryDelai();
if ($otherDelay) {
$this->context->smarty->assign('isOtherDelay', true);
$this->context->smarty->assign('otherDelayTitle', $otherDelay);
}
$this->context->smarty->assign('empty', 1);
$this->setTemplate(_PS_THEME_DIR_.'shopping-cart.tpl');
break;
case 1:
$this->_assignAddress();
$this->processAddressFormat();
if (Tools::getValue('multi-shipping') == 1)
{
$this->_assignSummaryInformations();
$this->context->smarty->assign('product_list', $this->context->cart->getProducts());
$this->setTemplate(_PS_THEME_DIR_.'order-address-multishipping.tpl');
}
else
$this->setTemplate(_PS_THEME_DIR_.'order-address.tpl');
break;
case 2:
if (Tools::isSubmit('processAddress'))
$this->processAddress();
$this->autoStep();
$this->_assignCarrier();
$this->setTemplate(_PS_THEME_DIR_.'order-carrier.tpl');
break;
case 3:
// Check that the conditions (so active) were accepted by the customer
$cgv = Tools::getValue('cgv') || $this->context->cookie->check_cgv;
if (Configuration::get('PS_CONDITIONS') && (!Validate::isBool($cgv) || $cgv == false))
Tools::redirect('index.php?controller=order&step=2');
Context::getContext()->cookie->check_cgv = true;
// Check the delivery option is set
if (!$this->context->cart->isVirtualCart())
{
if (!Tools::getValue('delivery_option') && !Tools::getValue('id_carrier') && !$this->context->cart->delivery_option && !$this->context->cart->id_carrier)
Tools::redirect('index.php?controller=order&step=2');
elseif (!Tools::getValue('id_carrier') && !$this->context->cart->id_carrier)
{
$deliveries_options = Tools::getValue('delivery_option');
if (!$deliveries_options)
$deliveries_options = $this->context->cart->delivery_option;
foreach ($deliveries_options as $delivery_option)
if (empty($delivery_option))
Tools::redirect('index.php?controller=order&step=2');
}
}
$this->autoStep();
// Bypass payment step if total is 0
if (($id_order = $this->_checkFreeOrder()) && $id_order)
{
if ($this->context->customer->is_guest)
{
$order = new Order((int)$id_order);
$email = $this->context->customer->email;
$this->context->customer->mylogout(); // If guest we clear the cookie for security reason
Tools::redirect('index.php?controller=guest-tracking&id_order='.urlencode($order->reference).'&email='.urlencode($email));
}
else
Tools::redirect('index.php?controller=history');
}
$this->_assignPayment();
// assign some informations to display cart
$this->_assignSummaryInformations();
$this->setTemplate(_PS_THEME_DIR_.'order-payment.tpl');
break;
default:
$this->_assignSummaryInformations();
// // Hack referralprogram Cart rule compatibily
// Db::getInstance()->update('referralprogram', array('id_cart_rule' => 0));
// $sql = "SELECT DISTINCT(id_cart_rule_sponsor) FROM "._DB_PREFIX_."referralprogram";
// $results = Db::getInstance()->ExecuteS($sql);
// foreach($results as $row)
// {
// //PS_cart_rule
// Db::getInstance()->update('cart_rule', array('cart_rule_restriction' => 1),'id_cart_rule='.$row['id_cart_rule_sponsor']);
// //PS_cart_rule_combination
// Db::getInstance()->delete('cart_rule_combination','id_cart_rule_1='.$row['id_cart_rule_sponsor'].' OR id_cart_rule_2='.$row['id_cart_rule_sponsor']);
// }
/** @Override [ticket 7902] */
$all_cart_rules = CartRule::getCustomerCartRules($this->context->language->id, $this->context->customer->id, true, true, true);
$notEligible = false;
$cartRulesAlreadyInCart = $this->context->cart->getCartRules(CartRule::FILTER_ACTION_SHIPPING);
foreach ($all_cart_rules as $key => $cart_rule) {
if($cart_rule['id_discount_type'] == Discount::FREE_SHIPPING){
$cr = new CartRule((int)$cart_rule['id_cart_rule']);
if($cr->checkValidityWithoutAmount($this->context,false,false) && $cart_rule['minimal']>0){
if(!isset($minimum_amount))
$minimum_amount = $cart_rule['minimal'];
if($minimum_amount < $cart_rule['minimal'])
continue;
$minimum_amount = ($minimum_amount < $cart_rule['minimal'])?$minimum_amount:$cart_rule['minimal'];
$minimum_amount_tax = (int)$cart_rule['minimum_amount_tax'] ;
} elseif (!$cr->checkValidityWithoutAmount($this->context,false,false) && empty($cartRulesAlreadyInCart)) {
$notEligible = true;
}
}
}
$free_shipping_ok = false;
if (!empty($cartRulesAlreadyInCart)) {
$free_shipping_ok = true;
}
$taxCalculationMethod = Group::getPriceDisplayMethod((int)Group::getCurrent()->id);
$useTax = !($taxCalculationMethod == PS_TAX_EXC);
$totalWithoutShipping = floatval($this->context->cart->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING));
$totalWithoutShippingAndTax = floatval($this->context->cart->getOrderTotal(false, Cart::BOTH_WITHOUT_SHIPPING));
// $nbOrder = Order::getCustomerNbOrders($this->context->customer->id); // Ajouter en condition s'il faut checker la première commande
if (isset($minimum_amount) && !$this->context->cart->getCartRules(CartRule::FILTER_ACTION_SHIPPING)){
if($minimum_amount_tax && $minimum_amount > $totalWithoutShipping)
$remaining_for_free_shipping = $minimum_amount - $totalWithoutShipping;
elseif(!$minimum_amount_tax && $minimum_amount > $totalWithoutShippingAndTax)
$remaining_for_free_shipping = $minimum_amount - $totalWithoutShippingAndTax;
}
$this->context->smarty->assign('remaining_for_free_shipping', $remaining_for_free_shipping);
$this->context->smarty->assign('notEligible', $notEligible);
$this->context->smarty->assign('free_shipping_ok', $free_shipping_ok);
$this->context->smarty->assign('totalWithoutShippingAndTax', $totalWithoutShippingAndTax);
$this->context->smarty->assign('totalWithoutShipping', $totalWithoutShipping);
$this->context->smarty->assign('taxCalculationMethod', $taxCalculationMethod);
/** end @Override [ticket 7902] */
/** @Override [ticket 9073] */
$otherDelay = $this->context->cart->checkProductOtherDeliveryDelai();
if ($otherDelay) {
$this->context->smarty->assign('isOtherDelay', true);
$this->context->smarty->assign('otherDelayTitle', $otherDelay);
}
$this->setTemplate(_PS_THEME_DIR_.'shopping-cart.tpl');
break;
}
$this->context->smarty->assign(array(
'remaining_for_free_shipping', $remaining_for_free_shipping,
'currencySign' => $this->context->currency->sign,
'currencyRate' => $this->context->currency->conversion_rate,
'currencyFormat' => $this->context->currency->format,
'currencyBlank' => $this->context->currency->blank,
));
}
/**
* Manage address (override)
*/
public function processAddress()
{
$same = Tools::isSubmit('same');
if(!Tools::getValue('id_address_invoice', false) && !$same)
$same = true;
if (!Customer::customerHasAddress($this->context->customer->id, (int)Tools::getValue('id_address_delivery'))
|| (!$same && Tools::getValue('id_address_delivery') != Tools::getValue('id_address_invoice')
&& !Customer::customerHasAddress($this->context->customer->id, (int)Tools::getValue('id_address_invoice'))))
$this->errors[] = Tools::displayError('Invalid address', !Tools::getValue('ajax'));
else
{
$this->context->cart->id_address_delivery = (int)Tools::getValue('id_address_delivery');
$this->context->cart->id_address_invoice = $same ? $this->context->cart->id_address_delivery : (int)Tools::getValue('id_address_invoice');
CartRule::autoRemoveFromCart($this->context);
CartRule::autoAddToCart($this->context);
if (!$this->context->cart->update())
$this->errors[] = Tools::displayError('An error occurred while updating your cart.', !Tools::getValue('ajax'));
if (!$this->context->cart->isMultiAddressDelivery())
$this->context->cart->setNoMultishipping(); // If there is only one delivery address, set each delivery address lines with the main delivery address
if (Tools::isSubmit('message'))
$this->_updateMessage(Tools::getValue('message'));
// Add checking for all addresses
$address_without_carriers = $this->context->cart->getDeliveryAddressesWithoutCarriers();
$address = new Address((int)Tools::getValue('id_address_delivery'));
$id_zone = Address::getZoneById($address->id);
if (count($address_without_carriers) && !$this->context->cart->isVirtualCart())
{
if (count($address_without_carriers) > 1)
$this->errors[] = sprintf(Tools::displayError('There are no carriers that deliver to some addresses you selected.', !Tools::getValue('ajax')));
elseif ($this->context->cart->isMultiAddressDelivery())
$this->errors[] = sprintf(Tools::displayError('There are no carriers that deliver to one of the address you selected.', !Tools::getValue('ajax')));
elseif ($id_zone == 13)
$this->context->smarty->assign('id_zone', $id_zone);
else
$this->errors[] = sprintf(Tools::displayError('There are no carriers that deliver to the address you selected !', !Tools::getValue('ajax')));
}
}
if ($this->errors)
{
if (Tools::getValue('ajax'))
die('{"hasError" : true, "errors" : ["'.implode('\',\'', $this->errors).'"]}');
$this->step = 1;
}
if ($this->ajax)
die(true);
}
}