Skip to content

Commit

Permalink
Merge pull request #516 from myparcelnl/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
RichardPerdaan authored Nov 24, 2020
2 parents 7a258f1 + 443f58c commit 6a790fb
Show file tree
Hide file tree
Showing 25 changed files with 309 additions and 218 deletions.
4 changes: 2 additions & 2 deletions Api/ShippingMethodsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ interface ShippingMethodsInterface
/**
* @param mixed $deliveryOptions
*
* @return array
* @return mixed[]
* @api
*/
public function getFromDeliveryOptions($deliveryOptions): array;
public function getFromDeliveryOptions($deliveryOptions);
}
4 changes: 0 additions & 4 deletions Controller/Adminhtml/Order/CreateAndPrintMyParcelTrack.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ private function filterCorrectAddress(array $orderIds): array
$destinationCountry = $order->getShippingAddress()->getCountryId();
$keyOrderId = array_search($orderId, $orderIds);

// Check if country is not NL or BE
if ($destinationCountry != AbstractConsignment::CC_NL && $destinationCountry != AbstractConsignment::CC_BE) {
continue;
}
// Validate the street and house number. If the address is wrong then get the orderId from the array and delete it.
if (! ValidateStreet::validate($fullStreet, AbstractConsignment::CC_NL, $destinationCountry)) {
$errorHuman = 'An error has occurred while validating the order number ' . $order->getIncrementId() . '. Check street.';
Expand Down
4 changes: 2 additions & 2 deletions Model/Checkout/ShippingMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class ShippingMethods implements ShippingMethodsInterface
/**
* @param mixed $deliveryOptions
*
* @return array
* @return mixed[]
* @throws Exception
*/
public function getFromDeliveryOptions($deliveryOptions): array
public function getFromDeliveryOptions($deliveryOptions)
{
if (! $deliveryOptions[0]) {
return [];
Expand Down
5 changes: 3 additions & 2 deletions Model/Quote/SaveOrderBeforeSalesModelQuoteObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ public function execute(Observer $observer)

if ($quote->hasData(Checkout::FIELD_DELIVERY_OPTIONS && $this->hasMyParcelDeliveryOptions($quote))) {
$jsonDeliveryOptions = $quote->getData(Checkout::FIELD_DELIVERY_OPTIONS);
$deliveryOptions = json_decode($jsonDeliveryOptions, true) ?? [];

$order->setData(Checkout::FIELD_DELIVERY_OPTIONS, $jsonDeliveryOptions);

$dropOffDay = $this->delivery->getDropOffDayFromJson($jsonDeliveryOptions);
$dropOffDay = $this->delivery->getDropOffDayFromDeliveryOptions($deliveryOptions);
$order->setData(Checkout::FIELD_DROP_OFF_DAY, $dropOffDay);

$selectedCarrier = $this->delivery->getCarrierFromJson($jsonDeliveryOptions);
$selectedCarrier = $this->delivery->getCarrierFromDeliveryOptions($deliveryOptions);
$order->setData(Checkout::FIELD_MYPARCEL_CARRIER, $selectedCarrier);
}

Expand Down
4 changes: 0 additions & 4 deletions Model/Rate/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,13 @@ private function addMyParcelRates($parentRate)

foreach ($this->getMethods() as $alias => $settingPath) {
$map = Data::CARRIERS_XML_PATH_MAP['postnl'];
$active = $this->myParcelHelper->getBoolConfig($map, $settingPath . '/active');

if ($active) {
$method = $this->getShippingMethod(
$this->getFullSettingPath($map, $settingPath),
$parentRate
);

$this->_rates[] = $method;
}
}

$this::$myParcelRatesAlreadyAdded = true;
}
Expand Down
11 changes: 9 additions & 2 deletions Model/Sales/MagentoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

use Magento\Framework\ObjectManagerInterface;
use Magento\Sales\Model\Order;
use MyParcelNL\magento\Model\Order\Email\Sender\TrackSender;
use MyParcelNL\Magento\Model\Order\Email\Sender\TrackSender;
use MyParcelNL\Magento\Model\Source\ReturnInTheBox;
use MyParcelNL\Magento\Observer\NewShipment;
use MyParcelNL\Magento\Ui\Component\Listing\Column\TrackAndTrace;
use MyParcelNL\Sdk\src\Helper\MyParcelCollection;
Expand Down Expand Up @@ -90,6 +91,7 @@ class MagentoCollection implements MagentoCollectionInterface
'insurance' => null,
'label_amount' => NewShipment::DEFAULT_LABEL_AMOUNT,
'digital_stamp_weight' => null,
'return_in_the_box' => false,
];

/**
Expand All @@ -113,7 +115,7 @@ public function __construct(ObjectManagerInterface $objectManagerInterface, $req
$this->helper = $objectManagerInterface->create(self::PATH_HELPER_DATA);
$this->modelTrack = $objectManagerInterface->create(self::PATH_ORDER_TRACK);
$this->messageManager = $objectManagerInterface->create(self::PATH_MANAGER_INTERFACE);
$this->myParcelCollection = (new MyParcelCollection())->setUserAgentArray(['Magento2'=> $this->helper->getVersion()]);
$this->myParcelCollection = (new MyParcelCollection())->setUserAgents(['Magento2'=> $this->helper->getVersion()]);
}

/**
Expand Down Expand Up @@ -157,6 +159,11 @@ public function setOptionsFromParameters()
$this->options['create_track_if_one_already_exist'] = false;
}

$returnInTheBox = $this->helper->getGeneralConfig('print/return_in_the_box');
if (ReturnInTheBox::NO_OPTIONS === $returnInTheBox || ReturnInTheBox::EQUAL_TO_SHIPMENT === $returnInTheBox) {
$this->options['return_in_the_box'] = $returnInTheBox;
}

return $this;
}

Expand Down
48 changes: 42 additions & 6 deletions Model/Sales/MagentoOrderCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Model\Order;
use MyParcelNL\Magento\Model\Source\ReturnInTheBox;
use MyParcelNL\Sdk\src\Helper\MyParcelCollection;
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;

/**
* Class MagentoOrderCollection
Expand Down Expand Up @@ -167,10 +169,8 @@ public function setMyParcelTrack()
foreach ($shipment->getTracksCollection() as $magentoTrack) {
if ($magentoTrack->getCarrierCode() == TrackTraceHolder::MYPARCEL_CARRIER_CODE) {
$trackTraceHolder = $this->createConsignmentAndGetTrackTraceHolder($magentoTrack);
$this->myParcelCollection->addConsignment($trackTraceHolder->consignment);
}
}

if (! empty($trackTraceHolder)) {
$consignment = $trackTraceHolder->consignment->setReferenceId($shipment->getEntityId());
$newCollection->addMultiCollo($consignment, $this->getOption('label_amount'));
Expand All @@ -180,6 +180,10 @@ public function setMyParcelTrack()

$this->myParcelCollection = $newCollection;

if ($this->options['return_in_the_box']) {
$this->addreturnInTheBox($this->options['return_in_the_box']);
}

return $this;
}

Expand Down Expand Up @@ -210,6 +214,41 @@ public function downloadPdfOfLabels()
return $this;
}

/**
* @param string $returnOptions
*
* @return void
* @throws \MyParcelNL\Sdk\src\Exception\ApiException
* @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException
*/
public function addReturnInTheBox(string $returnOptions): void
{
$this->myParcelCollection
->generateReturnConsignments(
false,
function (
AbstractConsignment $returnConsignment,
AbstractConsignment $parent
) use ($returnOptions): AbstractConsignment {
$returnConsignment->setLabelDescription(
'Return: ' . $parent->getLabelDescription() .
' This label is valid until: ' . date("d-m-Y", strtotime("+ 28 days"))
);

if (ReturnInTheBox::NO_OPTIONS === $returnOptions) {
$returnConsignment->setOnlyRecipient(false);
$returnConsignment->setSignature(false);
$returnConsignment->setAgeCheck(false);
$returnConsignment->setReturn(false);
$returnConsignment->setLargeFormat(false);
$returnConsignment->setInsurance(false);
}

return $returnConsignment;
}
);
}

/**
* Create MyParcel concepts and update Magento Track
*
Expand Down Expand Up @@ -263,7 +302,7 @@ public function setLatestData()
*/
public function sendReturnLabelMails()
{
$this->myParcelCollection->sendReturnLabelMails();
$this->myParcelCollection->generateReturnConsignments(true);

return $this;
}
Expand Down Expand Up @@ -317,7 +356,6 @@ private function sendTrackEmailFromOrder(Order $order)
return $this;
}


/**
* This create a shipment. Observer/NewShipment() create Magento and MyParcel Track
*
Expand Down Expand Up @@ -364,7 +402,6 @@ private function createShipment(Order $order)
// Send email
$this->objectManager->create('Magento\Shipping\Model\ShipmentNotifier')
->notify($shipment);

} catch (\Exception $e) {
throw new LocalizedException(
__($e->getMessage())
Expand All @@ -386,7 +423,6 @@ public function updateMagentoTrack()
* @var $magentoTrack Order\Shipment\Track
*/
foreach ($this->getShipmentsCollection() as $shipment) {

$trackCollection = $shipment->getAllTracks();
foreach ($trackCollection as $magentoTrack) {
$myParcelTrack = $this->myParcelCollection->getConsignmentByApiId(
Expand Down
27 changes: 9 additions & 18 deletions Model/Sales/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@

namespace MyParcelNL\Magento\Model\Sales;


use Magento\Framework\App\ObjectManager;
use Magento\Framework\Module\ModuleListInterface;
use Magento\Framework\App\Helper\Context;
use Magento\Quote\Api\Data\EstimateAddressInterfaceFactory;
use MyParcelNL\Magento\Helper\Data;
use MyParcelNL\Sdk\src\Services\CheckApiKeyService;
use Psr\Log\LoggerInterface;

class Package extends Data implements PackageInterface
{
Expand Down Expand Up @@ -85,27 +78,27 @@ class Package extends Data implements PackageInterface
private $package_type = null;

/**
* @return int
* @return float
*/
public function getWeight()
{
return $this->weight;
}

/**
* @param $weight
* @param float $weight
*/
public function setWeight(int $weight)
public function setWeight(float $weight)
{
$this->weight = (int) $weight;
$this->weight = $weight;
}

/**
* @param int $weight
* @param float $weight
*/
public function addWeight(int $weight)
public function addWeight(float $weight)
{
$this->weight += (int) $weight;
$this->weight += $weight;
}

/**
Expand All @@ -117,14 +110,13 @@ public function getMaxWeight(): int
}

/**
* @param int $max_weight
* @param float $max_weight
*/
public function setMaxWeight(int $max_weight)
public function setMaxWeight(float $max_weight)
{
$this->max_weight = $max_weight;
}


/**
* @return bool
*/
Expand Down Expand Up @@ -222,7 +214,6 @@ public function setAllProductsFit(bool $all_products_fit)
}
}


/**
* package = 1
*
Expand Down
8 changes: 4 additions & 4 deletions Model/Sales/PackageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
interface PackageInterface
{
/**
* @return int
* @return float
*/
public function getWeight();
/**
* @param $weight
*/
public function setWeight(int $weight);
public function setWeight(float $weight);
/**
* @param int $weight
* @param float $weight
*/
public function addWeight(int $weight);
public function addWeight(float $weight);
/**
* @return bool
*/
Expand Down
27 changes: 5 additions & 22 deletions Model/Sales/Repository/DeliveryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,12 @@ class DeliveryRepository extends Delivery
/**
* Get drop off day with chosen options from checkout
*
* @param $jsonDeliveryOptions
*
* @param array $deliveryOptions
* @return string
*/
public function getDropOffDayFromJson($jsonDeliveryOptions)
public function getDropOffDayFromDeliveryOptions(array $deliveryOptions): ?string
{
if ($jsonDeliveryOptions === null) {
return null;
}

$deliveryOptions = json_decode($jsonDeliveryOptions, true);
if (key_exists('date', $deliveryOptions)) {

if (! $deliveryOptions['date']) {
return date('Y-m-d', strtotime("+1 day"));
}
Expand Down Expand Up @@ -82,21 +75,11 @@ public function getDropOffDay()
/**
* Get carrier with chosen options from checkout
*
* @param string|null $jsonDeliveryOptions
*
* @param array $deliveryOptions
* @return string|null
*/
public function getCarrierFromJson(?string $jsonDeliveryOptions): ?string
public function getCarrierFromDeliveryOptions(array $deliveryOptions): ?string
{
if ($jsonDeliveryOptions === null) {
return null;
}

$deliveryOptions = json_decode($jsonDeliveryOptions, true);
if (key_exists('carrier', $deliveryOptions)) {
return $deliveryOptions['carrier'];
}

return null;
return $deliveryOptions['carrier'] ?? null;
}
}
10 changes: 7 additions & 3 deletions Model/Sales/TrackTraceHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ public function convertDataFromMagentoToApi($magentoTrack, $options)
$packageType = $options['package_type'] ? $options['package_type'] : AbstractConsignment::PACKAGE_TYPE_PACKAGE;
}

if (!is_numeric($packageType)) {
$packageType = AbstractConsignment::PACKAGE_TYPES_NAMES_IDS_MAP[$packageType];
}

$apiKey = $this->helper->getGeneralConfig(
'api/key',
$shipment->getOrder()->getStoreId()
Expand Down Expand Up @@ -284,7 +288,7 @@ public function getLabelDescription($magentoTrack, ?string $checkoutData): strin
$this->helper->convertDeliveryDate($checkoutData) ? $deliveryDate : '',
$this->getProductInfo($productInfo, 'product_id'),
$this->getProductInfo($productInfo, 'name'),
$this->getProductInfo($productInfo, 'qty'),
round($this->getProductInfo($productInfo, 'qty'), 0),
],
$labelDescription
);
Expand Down Expand Up @@ -327,7 +331,7 @@ private function convertDataForCdCountry($magentoTrack)
$myParcelProduct = (new MyParcelCustomsItem())
->setDescription($product->getName())
->setAmount($product->getQty())
->setWeight($product->getWeight() ?: 1)
->setWeight($this->getWeightTypeOfOption($product->getWeight()) ?: 1)
->setItemValue($this->getCentsByPrice($product->getPrice()))
->setClassification(
(int) $this->getAttributeValue('catalog_product_entity_int', $product['product_id'], 'classification')
Expand All @@ -343,7 +347,7 @@ private function convertDataForCdCountry($magentoTrack)
$myParcelProduct = (new MyParcelCustomsItem())
->setDescription($item->getName())
->setAmount($item->getQty())
->setWeight($this->getWeightTypeOfOption($item->getWeight()))
->setWeight($this->getWeightTypeOfOption($item->getWeight() * $item->getQty()))
->setItemValue($item->getPrice() * 100)
->setClassification((int) $this->getAttributeValue('catalog_product_entity_int', $item->getProductId(), 'classification'))
->setCountry($this->getCountryOfOrigin($item->getProductId()));
Expand Down
Loading

0 comments on commit 6a790fb

Please sign in to comment.