public static function getFromOrder(Order $order) { $translator = Translator::getInstance(); if ($order->getDeliveryModuleId() !== TNTFrance::getModuleId()) { throw new \InvalidArgumentException($translator->trans("The order %id does not use the", ['id' => $order->getId()], TNTFrance::MESSAGE_DOMAIN)); } $data = TNTFrance::getExtraOrderData($order->getId(), false); if (empty($data)) { throw new \InvalidArgumentException($translator->trans("No TNT data for order %id", ['id' => $order->getId()], TNTFrance::MESSAGE_DOMAIN)); } $receiver = new TNTReceiver(); $receiver->setType($data['tnt_service']); $receiver->setEmailAddress($order->getCustomer()->getEmail()); if (array_key_exists('tnt_instructions', $data)) { $receiver->setInstructions($data['tnt_instructions']); } if (array_key_exists('tnt_phoneNumber', $data)) { $phoneNumber = str_replace(" ", "", $data['tnt_phoneNumber']); $receiver->setPhoneNumber($phoneNumber); } //todo : $receiver->setSendNotification(TNTFrance::getConfigValue(TNTFranceConfigValue::NOTIFICATION_USER)); switch ($data['tnt_service']) { case 'INDIVIDUAL': case 'ENTERPRISE': $address = OrderAddressQuery::create()->findPk($order->getDeliveryOrderAddressId()); if (null !== $address) { $receiver->setName($address->getCompany())->setAddress1($address->getAddress1())->setAddress2($address->getAddress2())->setZipCode($address->getZipcode())->setCity($address->getCity())->setContactLastName($address->getLastname())->setContactFirstName($address->getFirstname()); if (array_key_exists('tnt_accessCode', $data)) { $receiver->setAccessCode($data['tnt_accessCode']); } if (array_key_exists('tnt_floorNumber', $data)) { $receiver->setAccessCode($data['tnt_floorNumber']); } if (array_key_exists('tnt_buildingId', $data)) { $receiver->setAccessCode($data['tnt_buildingId']); } } break; case 'DEPOT': $receiver->setTypeId($data['tnt_pexcode'])->setCity($data['tnt_depot_address']['city']); break; case 'DROPOFFPOINT': $receiver->setTypeId($data['tnt_exttcode']); break; default: throw new \InvalidArgumentException($translator->trans("TNT service %service is not valid for order %id", ['id' => $order->getId(), 'service' => $data['tnt_service']], TNTFrance::MESSAGE_DOMAIN)); } return $receiver; }
/** * Check if this module is the delivery module for a given order * * @param Order $order an order * @return bool true if this module is the delivery module for the given order. */ public function isDeliveryModuleFor(Order $order) { $model = $this->getModuleModel(); return $order->getDeliveryModuleId() == $model->getId(); }
public static function getFromOrder(Order $order, $allInOne = true) { $translator = Translator::getInstance(); if ($order->getDeliveryModuleId() !== TNTFrance::getModuleId()) { throw new \InvalidArgumentException($translator->trans("The order %id does not use the", ['id' => $order->getId()], TNTFrance::MESSAGE_DOMAIN)); } $data = TNTFrance::getExtraOrderData($order->getId(), false); if (empty($data)) { throw new \InvalidArgumentException($translator->trans("No TNT data for order %id", ['id' => $order->getId()], TNTFrance::MESSAGE_DOMAIN)); } $maxWeightPackage = TNTFrance::getConfigValue(TNTFranceConfigValue::MAX_WEIGHT_PACKAGE, 25); $parcelsRequest = []; $orderTotalWeight = 0; $packages = []; foreach ($order->getOrderProducts() as $orderProduct) { $orderProductWeight = $orderProduct->getQuantity() * $orderProduct->getWeight(); $orderTotalWeight += $orderProductWeight; if (!$allInOne) { //If customer has choosen a manual number of package if ($orderProduct->getVirtualColumn(TNTFranceCreateExpeditionEvent::PACKAGE) && intval($orderProduct->getVirtualColumn(TNTFranceCreateExpeditionEvent::PACKAGE)) == $orderProduct->getVirtualColumn(TNTFranceCreateExpeditionEvent::PACKAGE)) { $orderProductPackages = $orderProduct->getVirtualColumn(TNTFranceCreateExpeditionEvent::PACKAGE); } else { if ($maxWeightPackage != 0) { $orderProductPackages = ceil($orderProductWeight / $maxWeightPackage); } else { $orderProductPackages = 1; } } //Divide the weight between packages for ($i = 1; $i <= $orderProductPackages; $i++) { $packages[] = round($orderProductWeight / $orderProductPackages, 2); } } } if ($allInOne) { //If customer has choosen a manual number of package if ($order->getVirtualColumn(TNTFranceCreateExpeditionEvent::PACKAGE) && intval($order->getVirtualColumn(TNTFranceCreateExpeditionEvent::PACKAGE)) == $order->getVirtualColumn(TNTFranceCreateExpeditionEvent::PACKAGE)) { $orderPackages = $order->getVirtualColumn(TNTFranceCreateExpeditionEvent::PACKAGE); } else { $orderPackages = ceil($orderTotalWeight / $maxWeightPackage); } //Divide the weight between packages for ($i = 1; $i <= $orderPackages; $i++) { $packages[] = round($orderTotalWeight / $orderPackages, 2); } } foreach ($packages as $key => $packageWeight) { $parcelRequest = new TNTParcelRequest(); $parcelRequest->setSequenceNumber($key + 1)->setCustomerReference($order->getCustomer()->getRef())->setWeight($packageWeight); $parcelsRequest[] = $parcelRequest; } if (count($parcelsRequest) == 0) { $parcelRequest = new TNTParcelRequest(); $weight = 0.0; /** @var OrderProduct $orderProduct */ foreach ($order->getOrderProducts() as $orderProduct) { $weight += $orderProduct->getQuantity() * floatval($orderProduct->getWeight()); } $parcelRequest->setWeight($weight); $parcelRequest->setSequenceNumber(1); //$parcelRequest->setComment($data['tnt_instructions']); $parcelRequest->setCustomerReference($order->getCustomer()->getRef()); $parcelsRequest[] = $parcelRequest; } return $parcelsRequest; }
/** * Check if there is a Coupon removing Postage * * @param Order $order the order for which we have to check if postage is free * * @return bool */ public function isCouponRemovingPostage(Order $order) { $coupons = $this->facade->getCurrentCoupons(); if (count($coupons) == 0) { return false; } $couponsKept = $this->sortCoupons($coupons); /** @var CouponInterface $coupon */ foreach ($couponsKept as $coupon) { if ($coupon->isRemovingPostage()) { // Check if delivery country is on the list of countries for which delivery is free // If the list is empty, the shipping is free for all countries. $couponCountries = $coupon->getFreeShippingForCountries(); if (!$couponCountries->isEmpty()) { if (null === ($deliveryAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress()))) { continue; } $countryValid = false; $deliveryCountryId = $deliveryAddress->getCountryId(); /** @var CouponCountry $couponCountry */ foreach ($couponCountries as $couponCountry) { if ($deliveryCountryId == $couponCountry->getCountryId()) { $countryValid = true; break; } } if (!$countryValid) { continue; } } // Check if shipping method is on the list of methods for which delivery is free // If the list is empty, the shipping is free for all methods. $couponModules = $coupon->getFreeShippingForModules(); if (!$couponModules->isEmpty()) { $moduleValid = false; $shippingModuleId = $order->getDeliveryModuleId(); /** @var CouponModule $couponModule */ foreach ($couponModules as $couponModule) { if ($shippingModuleId == $couponModule->getModuleId()) { $moduleValid = true; break; } } if (!$moduleValid) { continue; } } // All conditions are met, the shipping is free ! return true; } } return false; }
/** * Filter the query by a related \Thelia\Model\Order object * * @param \Thelia\Model\Order|ObjectCollection $order the related object to use as filter * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL * * @return ChildModuleQuery The current query, for fluid interface */ public function filterByOrderRelatedByDeliveryModuleId($order, $comparison = null) { if ($order instanceof \Thelia\Model\Order) { return $this->addUsingAlias(ModuleTableMap::ID, $order->getDeliveryModuleId(), $comparison); } elseif ($order instanceof ObjectCollection) { return $this->useOrderRelatedByDeliveryModuleIdQuery()->filterByPrimaryKeys($order->getPrimaryKeys())->endUse(); } else { throw new PropelException('filterByOrderRelatedByDeliveryModuleId() only accepts arguments of type \\Thelia\\Model\\Order or Collection'); } }