Christophe Latour 1cb0ba30ef handling relays
2017-10-12 12:31:06 +02:00

322 lines
15 KiB
PHP

<?php
namespace App\Models;
use Antadis\API\Front\Models\Cart as BaseCart;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use ApiAddress;
use ApiCarrier;
use ApiCountry;
use ApiConfiguration;
use ApiModule;
use ApiTools;
class Cart extends BaseCart
{
public function getMondialRelayAddress() {
global $cookie;
$result = \Db::getInstance()->getRow('
SELECT * FROM `'._DB_PREFIX_.'mr_selected`
WHERE `id_cart` = '.(int)$this->id.'
');
if ($result === false) {
return null;
}
$address = (new ApiAddress($this->id_address_delivery));
return (new ApiAddress())->populate(array(
'alias' => '',
'address1' => $result['MR_Selected_LgAdr3'],
'address2' => $result['MR_Selected_LgAdr4'],
'city' => $result['MR_Selected_Ville'],
'company' => '',
'country' => ApiCountry::getNameById($cookie->id_lang, $address->id_country),//TODO: getNameByIso $result['MR_Selected_Pays'],
'firstname' => $address->firstname,
'lastname' => $address->lastname,
'phone' => $address->phone,
'phone_mobile' => $address->phone_mobile,
'postcode' => $result['MR_Selected_CP'],
), 0)->toArray();
}
public function getColissimoAddress() {
global $cookie;
$result = \Db::getInstance()->getRow('
SELECT * FROM `'._DB_PREFIX_.'socolissimo_delivery_info`
WHERE `id_cart` = '.(int)$this->id.'
');
if ($result === false) {
return null;
}
$address = (new ApiAddress($this->id_address_delivery));
return (new ApiAddress())->populate(array(
'alias' => '',
'address1' => $result['pradress1'],
'address2' => $result['pradress2'],
'city' => $result['prtown'],
'company' => '',
'country' => ApiCountry::getNameById($cookie->id_lang, $address->id_country),//TODO: getNameByIso $result['cecountry],
'firstname' => empty($result['cefirstname']) ? $address->firstname : $result['cefirstname'],
'lastname' => empty($result['cename']) ? $address->lastname : $result['cename'],
'phone' => empty($result['cephonenumber']) ? $address->phone : $result['cephonenumber'],
'phone_mobile' => '',
'postcode' => $result['przipcode'],
), 0)->toArray();
}
protected function getAddressRelay() {
global $cookie;
$carrierModel = $this->getCarrierModelClassName();
$carrier = new $carrierModel($this->id_carrier, $cookie->id_lang);
if ($carrier->isRelay()) {
$type = $carrier->getType();
if ($type === $carrierModel::MONDIALRELAY) {
return $this->getMondialRelayAddress();
} elseif ($type === $carrierModel::COLSSIMO_OFFICE) {
return $this->getColissimoAddress();
} else {
return $this->getColissimoAddress();
}
}
return null;
}
/**
* {@inheritdoc}
*/
public function toArray() {
if (!class_exists('SaleDelay')) {
require_once _PS_ROOT_DIR_.'/modules/privatesales_delay/saledelay.php';
}
$products_delay = \SaleDelay::associateDelay($this->getProducts());
$delays = array_keys($products_delay);
$date = new \DateTime();
$delivery_date = \SaleDelay::getDeliveryDate($delays, null, $date, true);
$economy = 0;
foreach ($this->_products as $product) {
$economy += (($product['price_without_reduc'] * $product['quantity']) - $product['total_wt']);
}
return array_merge(parent::toArray(), array(
'delay' => empty($delivery_date) ? '' : $delivery_date,
'economy' => ApiTools::displayPrice($economy),
'address_relay' => $this->getAddressRelay(),
));
}
public function apiGetAvailableCarriers(Request $request) {
$customer = $request->user();
$address = new ApiAddress((int)($this->id_address_delivery));
$id_zone = ApiAddress::getZoneById((int)($address->id));
$carriers = ApiCarrier::getCarriersForOrder($id_zone, $customer->getGroups());
$carriers_special = array_map('intval',explode(',', ApiConfiguration::get('ANT_CARRIERS_SPECIAL')));
$sale_delivery = array();
// Restriction du choix du transporteur
if(ApiModule::isInstalled('privatesales')) {
$carrier_dropshipping = (int)ApiConfiguration::get('ANT_CARRIER_DROP');
$carrier_dom = (int)ApiConfiguration::get('ANT_CARRIER_DOM');
if(ApiConfiguration::get('PRIVATESALES_CARRIERFENCE') ) {
$cart_products = $this->getProducts();
if(count($cart_products) > 0) {
$_sales = array();
$_carriers = array();
$carrier_uniq = array();
$count_carriers = array();
$dropshipping = false;
$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 ' . $this->id);
// mail('valentin@bebeboutik.com', '[BBB] Vente sans transporteur', 'La vente ' . $sale->id . ' n\'a aucun transporteur défini
// pour le panier ' . $this->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'];
if((int)$carrier['id_carrier'] == $carrier_dropshipping) {
$dropshipping = true;
}
}
}
}
}
// 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;
}
} else {
foreach ($carriers as $key => $value) {
if(isset($count_carriers[(int)$value])) {
$count_carriers[(int)$value]++;
} else {
$count_carriers[(int)$value] = 1;
}
}
}
}
// 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();
$config_carrier_mr = array_map('intval',explode(',', ApiConfiguration::get('ANT_CARRIERS_MR')));
$config_carrier_socol = array_map('intval',explode(',', ApiConfiguration::get('ANT_CARRIERS_SOCOL')));
foreach ($_carriers as $key => $carrier) {
if($dropshipping && $carrier['id_carrier'] == $carrier_dropshipping) {
$carrier_drop = $carrier;
}
if ($carrier['external_module_name'] == "soflexibilite") {
if($address->id_country == 3) {
$carrier['delay'] = str_replace(array("France",'10 000','8 000'),array("Belgique",'des','des'),$carrier['delay']);
}
}
if (in_array((int)$carrier['id_carrier'], $config_carrier_socol)) {
$carriers_socol[] = $carrier;
}
if(in_array((int)$carrier['id_carrier'], $config_carrier_mr)
&& isset($count_carriers[$config_carrier_socol[1]]) && isset($count_carriers[(int)$carrier['id_carrier']])
&& $count_carriers[$config_carrier_socol[1]] == $count_carriers[(int)$carrier['id_carrier']]
) {
// $count_carriers[$config_carrier_socol[1]] - socol ooh
$carriers_socol[] = $carrier;
}
}
if(isset($carrier_drop) && !$shipping_only_athome) {
unset($carriers);
$carriers[0] = $carrier_drop;
} elseif (!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 (isset($carrier_extra_available[0]) && $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_map('intval',explode(',', ApiConfiguration::get('ANT_CARRIERS_OOH')));
foreach ($carriers as $carrier) {
if($shipping_only_athome && isset($carrier['id_carrier']) && $carrier['id_carrier'] != $carrier_dom) {
continue;
}
if(isset($carrier['id_carrier']) && $carrier['id_carrier'] != $carrier_dom
&& isset($count_carriers[(int)$carrier['id_carrier']]) && isset($count_carriers[$carrier_dom])
&& $count_carriers[(int)$carrier['id_carrier']] < $count_carriers[$carrier_dom]) {
continue;
}
if (isset($carrier['id_carrier']) && 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;
} elseif(count($carriers_ooh) == 0) {
$carriers_ah = $carriers;
}
}
}
}
$available_carriers = array();
foreach ($carriers_ah as $carrier_ah) {
if (isset($carrier_ah['id_carrier'])) {
$carrier = (new ApiCarrier())->populate($carrier_ah, $carrier_ah['id_carrier']);
$carrier->is_relay = false;
$available_carriers[] = $carrier;
}
}
foreach ($carriers_ooh as $carrier_ooh) {
if (isset($carrier_ooh['id_carrier'])) {
$carrier = (new ApiCarrier())->populate($carrier_ooh, $carrier_ooh['id_carrier']);
$carrier->is_relay = true;
$available_carriers[] = $carrier;
}
}
return new Collection($available_carriers);
}
}