/** ** Affiche le transporteur Dejala.fr dans la liste des transporteurs sur le Front Office */ public function hookExtraCarrier($params) { global $smarty, $cart, $cookie, $defaultCountry; $this->hooklog("ExtraCarrier", $params); // Dejala n'est pas actif sur la boutique if ($this->dejalaConfig->active != 1) { return; } $djlUtil = new DejalaUtils(); $responseGetStore = $djlUtil->getStoreAttributes($this->dejalaConfig, $store); if ($responseGetStore['status'] != '200') { return; } $isCartOutOfStock = '0'; if ($this->isCartOutOfStock()) { $isCartOutOfStock = '1'; } $this->mylog('isCartOutOfStock=' . $isCartOutOfStock . ''); $acceptPartial = true; if (!isset($store['attributes']) || !isset($store['attributes']['delivery_partial']) || $store['attributes']['delivery_partial'] != '1') { $acceptPartial = false; } if ($isCartOutOfStock == '1' && !$acceptPartial) { return; } $totalCartWeight = floatval($cart->getTotalWeight()); $address = $params['address']; // ask dejala.fr for a quotation $quotation["receiver_name"] = $address->lastname; $quotation["receiver_firstname"] = $address->firstname; $quotation["receiver_company"] = $address->company; $quotation["receiver_address"] = $address->address1; $quotation["receiver_address2"] = $address->address2; $quotation["receiver_zipcode"] = $address->postcode; $quotation["receiver_city"] = $address->city; $quotation["receiver_phone"] = $address->phone; $quotation["receiver_phone_mobile"] = $address->phone_mobile; $quotation["receiver_comments"] = $address->other; $quotation["timelimit"] = 3; $quotation["weight"] = $totalCartWeight; $this->mylog("asking for quotation=" . $this->logValue($quotation, 1)); $products = array(); $responseArray = $djlUtil->getStoreQuotation($this->dejalaConfig, $quotation, $products); if ($responseArray['status'] != '200') { return; } $this->mylog("found quotation=" . $this->logValue($responseArray['response'], 1)); $electedProduct = NULL; foreach ($products as $key => $product) { // if (floatval($product['max_weight']) >= $totalCartWeight) { if (is_null($electedProduct) || intval($electedProduct['priority']) > intval($key)) { $electedProduct = $product; } // } } if (is_null($electedProduct)) { return; } $this->mylog("electedProduct=" . $this->logValue($electedProduct, 1)); $electedCarrier = DejalaCarrierUtils::getDejalaCarrier($this->dejalaConfig, $electedProduct); $this->mylog("electedCarrier=" . $this->logValue($electedCarrier, 1)); if (null == $electedCarrier) { $this->mylog("creating a new carrier"); $electedCarrier = DejalaCarrierUtils::createDejalaCarrier($this->dejalaConfig, $electedProduct); } // Calcul des dates dispo $productCalendar = $electedProduct['calendar']['entries']; // MFR090831 - add picking time : the store is open to (stop_hour - picking time), it is more natural to merchants to set opening hours instead of dejala delivery time if ($electedProduct['pickingtime']) { $pickingtime = intval($electedProduct['pickingtime']); } else { $pickingtime = $electedProduct['timelimit']; } $djlUtil = new DejalaUtils(); $storeCalendar = array(); $calendar = array(); $response = $djlUtil->getStoreCalendar($this->dejalaConfig, $storeCalendar); $this->mylog("productCalendar=" . $this->logValue($productCalendar, 1)); $this->mylog("storeCalendar=" . $this->logValue($storeCalendar, 1)); $this->mylog("response['status']=" . $response['status']); if ($response['status'] == 200) { foreach ($storeCalendar['entries'] as $weekday => $calEntry) { if (isset($productCalendar[$weekday])) { $calendar[$weekday]["weekday"] = $weekday; $calendar[$weekday]["start_hour"] = max(intval($productCalendar[$weekday]["start_hour"]), intval($calEntry["start_hour"])); // MFR090831 - manage picking time : the store is open to (stop_hour - picking time) $calendar[$weekday]["stop_hour"] = min(intval($productCalendar[$weekday]["stop_hour"] - 1), intval($calEntry["stop_hour"] - $pickingtime)); if ($calendar[$weekday]["stop_hour"] < $calendar[$weekday]["start_hour"]) { unset($calendar[$weekday]); } } } } // Calcul de la date de démarrage pour les créneaux : // Avancement jusque jour dispo & ouvert // Ajout du temps de préparation : 0.5 jour ou 1 nb de jours // Ajustement de l'heure sur l'ouverture ou l'heure suivante xxh00 $deliveryDelay = $store['attributes']['delivery_delay']; $calUtils = new CalendarUtils(); $all_exceptions = array_merge($storeCalendar['exceptions'], $electedProduct['calendar']['exceptions']); $dateUtc = $calUtils->getNextDateAvailable(time(), $calendar, $all_exceptions); if ($dateUtc == NULL) { return; } if ($deliveryDelay > 0) { $dateUtc = $calUtils->addDelay($dateUtc, $deliveryDelay, $calendar, $all_exceptions); } if ($dateUtc == NULL) { return; } $dateUtc = $calUtils->adjustHour($dateUtc, $calendar); $this->mylog("calendar=" . $this->logValue($calendar, 1)); $this->mylog("starting date=" . $this->logValue(date("d/m/Y - H:i:s", $dateUtc), 1)); /** Dates[0] = { [label]=lundi [value]=23/04/2009 [start_hour]=9 [stop_hour]=17 } **/ $today = getDate(); $ctime = time(); $wday_labels = array($this->l('Dimanche'), $this->l('Lundi'), $this->l('Mardi'), $this->l('Mercredi'), $this->l('Jeudi'), $this->l('Vendredi'), $this->l('Samedi')); $nbDeliveryDates = $deliveryDelay = $store['attributes']['nb_days_displayed']; $iDate = 0; $dates = array(); $balladUtc = $dateUtc; do { $dates[$iDate]['value'] = date("Y/m/d", $balladUtc); $wd = date("w", $balladUtc); $dates[$iDate]['label'] = $wday_labels[$wd] . " " . date("j", $balladUtc); $dates[$iDate]['start_hour'] = $calendar[$wd]['start_hour']; $dates[$iDate]['stop_hour'] = $calendar[$wd]['stop_hour']; $balladUtc += 3600 * 24; $balladUtc = $calUtils->getNextDateAvailable($balladUtc, $calendar, $all_exceptions); $iDate++; } while ($iDate < $nbDeliveryDates && $balladUtc); // impossibilité de trouver un jour dispo if (!isset($dates[0])) { return; } $dates[0]['start_hour'] = date("H", $dateUtc); $this->mylog("date\$=" . $this->logValue($dates, 1)); $smarty->assign('nb_days', $nbDeliveryDates); $smarty->assign('dates', $dates); for ($i = 0; $i < 24; $i++) { $endHour = ($i + $electedProduct['timelimit']) % 24; if ($endHour == 0) { $endHour = 24; } $hourLabels[] = $i . 'h-' . $endHour . 'h'; } $smarty->assign('hourLabels', $hourLabels); $smarty->assign('timetable_css', _MODULE_DIR_ . $this->name . '/timetable.css'); $smarty->assign("timetable_js", _MODULE_DIR_ . $this->name . '/timetable.js'); $this->mylog("electedCarrier->id=" . $this->logValue($electedCarrier->id)); $mCarrier = $electedCarrier; $row['id_carrier'] = intval($electedCarrier->id); $row['name'] = $this->l('Dejala.fr'); $row['delay'] = $electedCarrier->delay[$cookie->id_lang]; $row['price'] = $cart->getOrderShippingCost($electedCarrier->id); $row['price_tax_exc'] = $cart->getOrderShippingCost($electedCarrier->id, false); $row['img'] = _MODULE_DIR_ . $this->name . '/dejala_carrier.gif'; $resultsArray[] = $row; $smarty->assign('carriers', $resultsArray); if ($cart->id_carrier) { $smarty->assign('checked', $cart->id_carrier); } $smarty->assign('product', $electedProduct); $djlCart = new DejalaCart($cart->id); $setDefaultDate = TRUE; if ($djlCart && isset($djlCart->shipping_date) && !empty($djlCart->shipping_date)) { $mShippingDate = $djlCart->shipping_date; $this->mylog("shipping_date=" . $this->logValue($mShippingDate)); $m_day = date("d", $mShippingDate); $m_hour = date("H", $mShippingDate); $deliveryDateSelected = date("Y/m/d", $mShippingDate); $this->mylog("shipping_date=" . $this->logValue($deliveryDateSelected)); foreach ($dates as $l_key => $l_date) { if ($l_date['value'] == $deliveryDateSelected) { $smarty->assign("deliveryDateIndexSelected", $l_key); $smarty->assign("deliveryDateSelected", $deliveryDateSelected); $smarty->assign("deliveryHourSelected", $m_hour); $setDefaultDate = FALSE; } } } if ($setDefaultDate) { $smarty->assign("deliveryDateIndexSelected", 0); $smarty->assign("deliveryDateSelected", date("Y/m/d", $dateUtc)); $smarty->assign("deliveryHourSelected", intval(date("H", $dateUtc))); } $smarty->assign("isCartOutOfStock", $isCartOutOfStock); if (!$isCartOutOfStock) { $buffer = $this->display(__FILE__, 'dejala_carrier.tpl'); $buffer = $buffer . $this->display(__FILE__, 'dejala_timetable.tpl'); } else { $smarty->assign('nostock_info', $this->l(utf8_encode('Je choisirai mon heure de livraison quand mon colis sera prêt.'))); $buffer = $this->display(__FILE__, 'dejala_carrier_nostock.tpl'); } return $buffer; }
private function getDejalaProduct($cart, $productId = -1) { // echo "Date : " . $cart->date_upd . "<br>" ; $djlCart = $this->getDejalaCart($cart->id); if (isset($djlCart->delivery_price) && $cart->date_upd <= $djlCart->cart_date_upd && isset($djlCart->product)) { if ($productId >= 0 && $djlCart->product["id"] == $productId) { return $djlCart->product; } } $djlUtil = new DejalaUtils(); $responseGetStore = $djlUtil->getStoreAttributes($this->dejalaConfig, $store); if ($responseGetStore['status'] != '200') { return; } $isCartOutOfStock = '0'; if ($this->isCartOutOfStock($cart)) { $isCartOutOfStock = '1'; } $this->mylog('isCartOutOfStock=' . $isCartOutOfStock . ''); $acceptPartial = true; if (!isset($store['attributes']) || !isset($store['attributes']['delivery_partial']) || $store['attributes']['delivery_partial'] != '1') { $acceptPartial = false; } if ($isCartOutOfStock == '1' && !$acceptPartial) { return; } $address = new Address($cart->id_address_delivery); // ask dejala.com for a quotation $quotation["receiver_name"] = $address->lastname; $quotation["receiver_firstname"] = $address->firstname; $quotation["receiver_company"] = $address->company; $quotation["receiver_address"] = $address->address1; $quotation["receiver_address2"] = $address->address2; $quotation["receiver_zipcode"] = $address->postcode; $quotation["receiver_city"] = $address->city; $quotation["receiver_phone"] = $address->phone; $quotation["receiver_phone_mobile"] = $address->phone_mobile; $quotation["receiver_comments"] = $address->other; $quotation["timelimit"] = 10; $quotation["weight"] = Configuration::get('PS_WEIGHT_UNIT') == 'g' ? (double) ($cart->getTotalWeight() / 1000) : (double) $cart->getTotalWeight(); $quotation["price"] = $cart->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING); $quotation["module_version"] = $this->internal_version; $quotation["platform"] = "PS " . _PS_VERSION_; $this->mylog("asking for quotation=" . $this->logValue($quotation, 1)); $products = array(); $storeAttributes = array(); $responseArray = $djlUtil->getStoreQuotation($this->dejalaConfig, $quotation, $products, $storeAttributes); if ($responseArray['status'] != '200') { $djlCart->delivery_price = -1; $djlCart->cart_date_upd = $cart->date_upd; $djlCart->save(); return null; } $this->mylog("found quotation=" . $this->logValue($responseArray['response'], 1)); $electedProduct = NULL; foreach ($products as $key => $product) { if (is_null($electedProduct) || (int) $electedProduct['priority'] > (int) $key) { $electedProduct = $product; } } if (is_null($electedProduct)) { $djlCart->delivery_price = -1; $djlCart->cart_date_upd = $cart->date_upd; $djlCart->save(); return null; } $djlCart->id_dejala_product = (int) $electedProduct["id"]; $djlCart->id_delivery = NULL; $djlCart->mode = $this->dejalaConfig->mode; $vat_factor = 1 + $electedProduct['vat'] / 100; $priceTTC = round($electedProduct['price'] * $vat_factor + $electedProduct['margin'], 2); $priceHT = round($priceTTC / $vat_factor, 5); $productPrice = $priceHT; $djlCart->delivery_price = $priceHT; $djlCart->cart_date_upd = $cart->date_upd; $djlCart->product = $electedProduct; $djlCart->save(); $electedProduct['store_attributes'] = $storeAttributes; return $electedProduct; }
/** * Save information to enable delivery to be ordered after payment */ public function hookCart($param) { global $cookie; $this->hooklog("hookCart", ""); if (Tools::getIsset('ajax')) { return; } /** * Totally awful code duplication. Will have to clean this up !! * There's probably some unhandled cases in which the user changes his cart * and does not go back properly in the carrier selection process (order.php's steps). * He might end-up with too heavy a cart for which Dejala should not have appeared. * But this is not supposed to happen. */ $cart = $param['cart']; $carrier = new DejalaCarrier($cart->id_carrier, intval($this->id_lang)); if ($carrier->name != 'dejala') { return; } $djlUtil = new DejalaUtils(); $responseGetStore = $djlUtil->getStoreAttributes($this->dejalaConfig, $store); if ($responseGetStore['status'] != '200') { return; } $isCartOutOfStock = '0'; if ($this->isCartOutOfStock()) { $isCartOutOfStock = '1'; } $this->mylog('isCartOutOfStock=' . $isCartOutOfStock . ''); $acceptPartial = true; if (!isset($store['attributes']) || !isset($store['attributes']['delivery_partial']) || $store['attributes']['delivery_partial'] != '1') { $acceptPartial = false; } if ($isCartOutOfStock == '1' && !$acceptPartial) { return; } $totalCartWeight = floatval($cart->getTotalWeight()); $address = new Address($cart->id_address_delivery); // ask dejala.fr for a quotation $quotation["receiver_name"] = $address->lastname; $quotation["receiver_firstname"] = $address->firstname; $quotation["receiver_company"] = $address->company; $quotation["receiver_address"] = $address->address1; $quotation["receiver_address2"] = $address->address2; $quotation["receiver_zipcode"] = $address->postcode; $quotation["receiver_city"] = $address->city; $quotation["receiver_phone"] = $address->phone; $quotation["receiver_phone_mobile"] = $address->phone_mobile; $quotation["receiver_comments"] = $address->other; $quotation["timelimit"] = 3; $quotation["weight"] = $totalCartWeight; $this->mylog("asking for quotation=" . $this->logValue($quotation, 1)); $products = array(); $responseArray = $djlUtil->getStoreQuotation($this->dejalaConfig, $quotation, $products); if ($responseArray['status'] != '200') { return; } $this->mylog("found quotation=" . $this->logValue($responseArray['response'], 1)); $electedProduct = NULL; foreach ($products as $key => $product) { if (is_null($electedProduct) || intval($electedProduct['priority']) > intval($key)) { $electedProduct = $product; } } if (is_null($electedProduct)) { return; } $electedCarrier = DejalaCarrierUtils::getDejalaCarrier($this->dejalaConfig, $electedProduct); // Should not be null at this point. if ($electedCarrier == null) { return null; } // Process the cart for storage in dejala_cart. $errors = array(); $dejalaCarrierID = Tools::getValue('dejala_id_carrier'); $carrierID = Tools::getValue('id_carrier'); $dejalaProductID = Tools::getValue('dejala_id_product'); if (!empty($dejalaCarrierID) && !empty($carrierID) && intval($dejalaCarrierID) == intval($carrierID)) { $id_cart = intval($param['cart']->id); $product = array(); $djlUtil = new DejalaUtils(); $response = $djlUtil->getStoreProductByID($this->dejalaConfig, $dejalaProductID, $product); if ($response['status'] != 200) { $errors[] = $this->l('An error occured while fetching shipping product from Dejala'); } else { $timelimit = 3; if (isset($product['timelimit'])) { $timelimit = intval($product['timelimit']); } /* manage shipping preferences */ $date_shipping = 'NULL'; if (isset($_POST['shipping_day']) and !empty($_POST['shipping_day']) and 10 <= strlen($_POST['shipping_day'])) { $shippingHour = intval($_POST['shipping_hour']); $shipping_day = $_POST['shipping_day']; $ship_year = intval(substr($shipping_day, 0, 4)); $ship_month = intval(substr($shipping_day, 5, 2)); $ship_day = intval(substr($shipping_day, 8, 2)); $shippingTime = mktime($shippingHour, 0, 0, $ship_month, $ship_day, $ship_year); // check that delivery date is in the future (5 min delay) if ($shippingTime > time() - 5 * 60) { $date_shipping = $shippingTime; } } $djlCart = new DejalaCart($id_cart); $djlCart->shipping_date = $date_shipping; $djlCart->id_dejala_product = $dejalaProductID; $djlCart->id_delivery = NULL; $djlCart->mode = $this->dejalaConfig->mode; $sqlQuery = 'REPLACE INTO ' . _DB_PREFIX_ . 'dejala_cart SET id_cart = ' . intval($id_cart) . ', id_dejala_product = ' . intval($djlCart->id_dejala_product) . ', shipping_date = ' . intval($djlCart->shipping_date) . ', mode="' . pSQL($djlCart->mode) . '";'; $this->mylog('cart SQLQuery=' . $sqlQuery); Db::getInstance()->Execute($sqlQuery); } } }