bebeboutik/override/controllers/ParentOrderController.php
2016-06-20 10:42:30 +02:00

241 lines
7.9 KiB
PHP
Executable File

<?php
if(Module::isInstalled('privatesales')) {
include_once(dirname(__FILE__).'/../../modules/privatesales/Sale.php');
}
class ParentOrderController extends ParentOrderControllerCore {
public function preProcess() {
if(Module::isInstalled('privatesales')) {
$removed = FALSE;
foreach(self::$cart->getProducts() as $product) {
if(($sale = Sale::getSaleFromCategory($product['id_category_default'])) !== NULL) {
if($sale->isFinished() || !$sale->enabled) {
self::$cart->deleteProduct($product['id_product'], $product['id_product_attribute'], $product['id_customization']);
$removed = TRUE;
}
}
}
if($removed === TRUE) {
self::$smarty->assign(array(
'products_removed' => TRUE,
));
$this->nbProducts = self::$cart->nbProducts();
}
if(!$this->nbProducts) {
$this->step = -1;
}
}
parent::preProcess();
}
protected function _assignCarrier() {
$customer = new Customer((int)(self::$cookie->id_customer));
$address = new Address((int)(self::$cart->id_address_delivery));
$id_zone = Address::getZoneById((int)($address->id));
$carriers = Carrier::getCarriersForOrder($id_zone, $customer->getGroups());
$carriers_special = Cart::$carrier_special;
$only_gifts = TRUE;
foreach(self::$cart->getProducts() as $p) {
if($p['id_product'] != 4840) {
$only_gifts = FALSE;
break;
}
}
self::$smarty->assign('only_gifts', $only_gifts);
$sale_delivery = array();
// Restriction du choix du transporteur
if(Module::isInstalled('privatesales')) {
if(Configuration::get('PRIVATESALES_CARRIERFENCE') ) {
$cart_products = self::$cart->getProducts();
if(count($cart_products) > 0) {
$_carriers = array();
$_sales = array();
$carrier_uniq = array();
$shipping_only_athome = false;
foreach ($cart_products as $key => $cart_product) {
$sale = Sale::getSaleFromCategory($cart_product['id_category_default']);
if($sale) {
$sale_carriers = $sale->getCarriers();
if ($sale->shipping_fr == 1) {
$shipping_only_athome = true;
}
$sale_delivery[] = $sale->delivery_delay;
$_sales[(int)$sale->id] = $sale_carriers;
if(count($sale_carriers) == 0) {
mail('thibault@antadis.com', '[BBB] Vente sans transporteur', 'La vente ' . $sale->id . ' n\'a aucun transporteur défini
pour le panier ' . $cart->id);
mail('valentin@bebeboutik.com', '[BBB] Vente sans transporteur', 'La vente ' . $sale->id . ' n\'a aucun transporteur défini
pour le panier ' . $cart->id);
break;
}
foreach($carriers as $carrier) {
if(in_array($carrier['id_carrier'], $sale_carriers)
&& !in_array($carrier['id_carrier'], $carrier_uniq)) {
$_carriers[] = $carrier;
$carrier_uniq[] = $carrier['id_carrier'];
}
}
}
}
// si multiple vente dont vente consommables
foreach ($_sales as $key => $carriers) {
if ((int) $key == _SHOP_PRIVATESALES_CONSUMABLE) {
if (count($_sales) > 1 && count($_carriers) > 1 && count($_carriers)>count($carriers)) {
$carriers_to_remove = array_filter($_carriers, function ($_carrier) use (&$carriers) {
foreach ($carriers as $c) {
if ((int) $c == (int) $_carrier['id_carrier']) {
return true;
}
}
return false;
});
foreach ($carriers_to_remove as $key => $value) {
unset($_carriers[$key]);
}
break;
} elseif (count($_sales)==1) {
// $carrier_shipping_cost = 5;
}
}
}
// si multiple carriers
if (count($_carriers) > 1) {
$extra_carrier = 0;
$carrier_extra_available = array();
$id_carrier_available = array();
$id_carrier_available = array_map(function($carrier){
return $carrier['id_carrier'];
}, $_carriers);
$id_carrier_available = array_unique($id_carrier_available);
foreach ($carriers_special as $carrier_special) {
if (in_array($carrier_special, $id_carrier_available)) {
$extra_carrier++;
$carrier_extra_available[] = $carrier_special;
}
}
if ($extra_carrier == 1) {
$carrier_final = array();
foreach ($_carriers as $key => $carrier) {
if ($carrier['id_carrier'] == $carrier_extra_available[0] ) {
$carrier_final = $carrier;
break;
}
}
unset($carriers);
$carriers[0] = $carrier_final;
} else {
// Gestion carrier socolissimo (soflexibilite)
$carriers_socol = array();
foreach ($_carriers as $key => $carrier) {
if ($carrier['external_module_name'] == "soflexibilite") {
$carriers_socol[] = $carrier;
}
}
if (!empty($carriers_socol)){
unset($carriers);
$carriers = $carriers_socol;
} else {
// TODOOO multi special carrier $carrier_extra_available[]
$carrier_final = array();
foreach ($_carriers as $key => $carrier) {
if ($carrier['id_carrier'] == $carrier_extra_available[0] ) {
$carrier_final = $carrier;
break;
}
}
unset($carriers);
$carriers[0] = $carrier_final;
}
}
} else {
$carriers = $_carriers;
}
// distinction "at home" et "out of home"
$carriers_ah = array();
$carriers_ooh = array();
$config_carrier_ooh = array(88,89); // prod
// $config_carrier_ooh = array(47,48); // dev
// $config_carrier_ooh = array(40,45); // local
foreach ($carriers as $carrier) {
if($shipping_only_athome && $carrier['id_carrier'] != 87) { // prod
// if($shipping_only_athome && $carrier['id_carrier'] != 45) { // dev
// if($shipping_only_athome && $carrier['id_carrier'] != 38) { // local
continue;
}
if (in_array($carrier['id_carrier'], $config_carrier_ooh) ){
$carriers_ooh[] = $carrier;
} else {
$carriers_ah[] = $carrier;
}
}
// fix a check !
if (count($carriers_ah) > 0) {
unset($carriers);
$carriers = $carriers_ah;
}
}
}
}
self::$smarty->assign(array(
'checked' => $this->_setDefaultCarrierSelection($carriers),
'carriers' => $carriers,
'carriers_ooh' => $carriers_ooh,
'carrier_shipping_cost' => $carrier_shipping_cost ? $carrier_shipping_cost:0,
'sale_delivery' => array_unique($sale_delivery),
'default_carrier' => (int)(Configuration::get('PS_CARRIER_DEFAULT'))
));
self::$smarty->assign(array(
'HOOK_EXTRACARRIER' => Module::hookExec('extraCarrier', array('address' => $address)),
'HOOK_BEFORECARRIER' => Module::hookExec('beforeCarrier', array('carriers' => $carriers))
));
}
protected function _processCarrier() {
if(Module::isInstalled('privatesales')) {
// if(Configuration::get('PRIVATESALES_CARRIERFENCE')) {
// $cart_products = self::$cart->getProducts();
// if(count($cart_products) > 0) {
// $sale = Sale::getSaleFromCategory($cart_products[0]['id_category_default']);
// if($sale) {
// $sale_carriers = $sale->getCarriers();
// if(!in_array(Tools::getValue('id_carrier'), $sale_carriers)) {
// $this->errors[] = Tools::displayError('Invalid carrier or no carrier selected');
// }
// }
// }
// }
if(count($this->errors) === 0) {
parent::_processCarrier();
}
} else {
parent::_processCarrier();
}
}
public function setMedia() {
parent::setMedia();
global $css_files;
$css_files = array_slice($css_files, 0, 1);
}
}