Skip to content

Commit

Permalink
Merge pull request #31 from Paazl/1.1.3
Browse files Browse the repository at this point in the history
Combined 1.1.3 edits
  • Loading branch information
Marvin-Magmodules authored Dec 2, 2019
2 parents 354122e + 1944036 commit 52a5009
Show file tree
Hide file tree
Showing 19 changed files with 979 additions and 166 deletions.
52 changes: 52 additions & 0 deletions Api/Data/Delivery/OrderPickupLocationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © 2019 Paazl. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Paazl\CheckoutWidget\Api\Data\Delivery;

/**
* Paazl Order Pickup Location Interface
*
* @api
*/
interface OrderPickupLocationInterface
{
const PICKUP_CODE = 'pickup_code';
const NAME = 'name';
const COUNTRY = 'country';
const POSTCODE = 'postcode';
const CITY = 'city';
const STREET = 'street';

/**
* @return string
*/
public function getPickupCode();

/**
* @return string
*/
public function getName();

/**
* @return string
*/
public function getCountry();

/**
* @return string
*/
public function getPostcode();

/**
* @return string
*/
public function getCity();

/**
* @return string[]
*/
public function getStreet();
}
90 changes: 55 additions & 35 deletions Model/Api/Builder/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@

namespace Paazl\CheckoutWidget\Model\Api\Builder;

use Magento\Catalog\Model\Product\Type;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\NotFoundException;
use Magento\GroupedProduct\Model\Product\Type\Grouped;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order\Address;
use Magento\Sales\Model\Order\Item;
use Paazl\CheckoutWidget\Api\OrderReferenceRepositoryInterface;
use Paazl\CheckoutWidget\Model\Api\Builder\Reference as Reference;
use Paazl\CheckoutWidget\Model\Api\Field\DeliveryType;
use Paazl\CheckoutWidget\Model\ExtInfoHandler;
use Paazl\CheckoutWidget\Model\Order\OrderReferenceFactory;
use Paazl\CheckoutWidget\Model\Config;
use Paazl\CheckoutWidget\Model\ExtInfoHandler;
use Paazl\CheckoutWidget\Model\Handler\Item as ItemHandler;

/**
* Class Order
Expand All @@ -25,7 +28,6 @@
*/
class Order
{

/**
* @var ExtInfoHandler
*/
Expand All @@ -46,24 +48,32 @@ class Order
*/
private $orderReferenceRepository;

/**
* @var ItemHandler
*/
private $itemHandler;

/**
* Order constructor.
*
* @param ExtInfoHandler $extInfoHandler
* @param Reference $referenceBuilder
* @param Config $config
* @param OrderReferenceRepositoryInterface $orderReferenceRepository
* @param ItemHandler $itemHandler
*/
public function __construct(
ExtInfoHandler $extInfoHandler,
Reference $referenceBuilder,
Config $config,
OrderReferenceRepositoryInterface $orderReferenceRepository
OrderReferenceRepositoryInterface $orderReferenceRepository,
ItemHandler $itemHandler
) {
$this->extInfoHandler = $extInfoHandler;
$this->referenceBuilder = $referenceBuilder;
$this->config = $config;
$this->orderReferenceRepository = $orderReferenceRepository;
$this->itemHandler = $itemHandler;
}

/**
Expand All @@ -72,6 +82,7 @@ public function __construct(
* @param OrderInterface $order
*
* @return array
* @throws LocalizedException
* @throws NotFoundException
*/
public function getCreateOrderData(OrderInterface $order)
Expand All @@ -82,9 +93,12 @@ public function getCreateOrderData(OrderInterface $order)
throw new NotFoundException(__('Reference information not found'));
}

$extInformation = $this->extInfoHandler->getInfoFromOrderReference($reference);
/** @var Address $shippingAddress */
$shippingAddress = $order->getShippingAddress();
$extInformation = $this->extInfoHandler->getInfoFromOrderReference($reference);
if ($extInformation->getType() === DeliveryType::PICKUP) {
$shippingAddress = $order->getBillingAddress();
}
$address = $this->parseAddress($shippingAddress);

$result = [
Expand All @@ -101,7 +115,7 @@ public function getCreateOrderData(OrderInterface $order)
'province' => $shippingAddress->getRegionCode(),
'street' => $address['street'],
'houseNumber' => $address['houseNumber'],
'houseNumberExtension' => $address['houseNumberExtension'],
'houseNumberExtension' => $address['houseNumberExtension'] ?? '',
]
],
'customsValue' => [
Expand Down Expand Up @@ -162,9 +176,32 @@ public function getCreateOrderData(OrderInterface $order)
private function parseAddress(Address $shippingAddress)
{
if ($this->config->housenumberOnSecondStreet() && trim($shippingAddress->getStreetLine(2) != '')) {
$extraStreet = $shippingAddress->getStreetLine(2) . $shippingAddress->getStreetLine(3);
$houseNumber = (int)filter_var($extraStreet, FILTER_SANITIZE_NUMBER_INT);
$houseNumberExtension = preg_replace('/^[0-9\-]+/', '', $extraStreet);
$pattern = '/^(?<houseNumber>\d{1,5})((?<houseNumberExtension>[\-\/\s]*[\w[:print:]]+)*)/';
preg_match($pattern, $shippingAddress->getStreetLine(2), $matches);
$parsedAddress = array_filter($matches, function ($key) {
return !is_int($key);
}, ARRAY_FILTER_USE_KEY);

$houseNumber = $parsedAddress['houseNumber'] ?? '';
$houseNumberExtension = preg_replace(
'/^[\s\/]/',
'',
$parsedAddress['houseNumberExtension'] ?? ''
);

if ($houseNumberExtension) {
return [
'street' => $shippingAddress->getStreetLine(1),
'houseNumber' => $houseNumber,
'houseNumberExtension' => $houseNumberExtension
];
}

if ($this->config->housenumberExtensionOnThirdStreet()
&& trim($shippingAddress->getStreetLine(3) != '')
) {
$houseNumberExtension = trim($shippingAddress->getStreetLine(3));
}

return [
'street' => $shippingAddress->getStreetLine(1),
Expand All @@ -174,38 +211,35 @@ private function parseAddress(Address $shippingAddress)
}

$address = implode(' ', $shippingAddress->getStreet());
$pattern = '#^([\w[:punct:] ]+) ([0-9]{1,5})([\w[:punct:]\-/]*)$#';
$pattern = '/^(?<street>[\w[:alpha:]]+[ \w[:alpha:]]*) (?<houseNumber>\d{1,5})((?<houseNumberExtension>[\-\/\s]*\w+)*)/';
preg_match($pattern, $address, $matches);
$street = (isset($matches[1])) ? $matches[1] : '';
$houseNumber = (isset($matches[2])) ? $matches[2] : '';
$houseNumberExtension = (isset($matches[3])) ? $matches[3] : '';

return [
'street' => $street,
'houseNumber' => $houseNumber,
'houseNumberExtension' => $houseNumberExtension
];
$parsedAddress = array_filter($matches, function ($key) {
return !is_int($key);
}, ARRAY_FILTER_USE_KEY);

return $parsedAddress;
}

/**
* @param $order
*
* @return array
* @throws LocalizedException
*/
private function getProducts(OrderInterface $order)
{
$products = [];

/** @var Item $item */
foreach ($order->getItems() as $item) {
if ($item->getProductType() != 'simple') {
if ($item->getProductType() !== Type::TYPE_SIMPLE && $item->getProductType() !== Grouped::TYPE_CODE) {
continue;
}

$itemData = [
'quantity' => (int)$item->getQtyOrdered(),
'unitPrice' => [
'value' => $this->getItemPrice($item),
'value' => $this->itemHandler->getPriceValue($item),
'currency' => $order->getOrderCurrencyCode()
],
'description' => $this->getItemDescription($item),
Expand All @@ -227,20 +261,6 @@ private function getProducts(OrderInterface $order)
return $products;
}

/**
* @param Item $item
*
* @return float|null
*/
private function getItemPrice(Item $item)
{
if ($item->getParentItemId() > 0) {
return $item->getParentItem()->getPriceInclTax();
} else {
return $item->getPriceInclTax();
}
}

/**
* @param Item $item
*
Expand Down
64 changes: 64 additions & 0 deletions Model/Delivery/OrderPickupLocation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © 2019 Paazl. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Paazl\CheckoutWidget\Model\Delivery;

use Magento\Framework\Model\AbstractModel;
use Paazl\CheckoutWidget\Api\Data\Delivery\OrderPickupLocationInterface;

/**
* Paazl Order Pickup Location Entity
*/
class OrderPickupLocation extends AbstractModel implements OrderPickupLocationInterface
{
/**
* @return string
*/
public function getPickupCode()
{
return $this->getData(OrderPickupLocationInterface::PICKUP_CODE);
}

/**
* @return string
*/
public function getName()
{
return $this->getData(OrderPickupLocationInterface::NAME);
}

/**
* @return string
*/
public function getCountry()
{
return $this->getData(OrderPickupLocationInterface::COUNTRY);
}

/**
* @return string
*/
public function getPostcode()
{
return $this->getData(OrderPickupLocationInterface::POSTCODE);
}

/**
* @return string
*/
public function getCity()
{
return $this->getData(OrderPickupLocationInterface::CITY);
}

/**
* @return string[]
*/
public function getStreet()
{
return $this->getData(OrderPickupLocationInterface::STREET);
}
}
56 changes: 56 additions & 0 deletions Model/Handler/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © 2019 Paazl. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Paazl\CheckoutWidget\Model\Handler;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Model\AbstractExtensibleModel;
use Magento\Sales\Model\Order\Item as OrderItem;
use Magento\Quote\Model\Quote\Item as QuoteItem;
use Paazl\CheckoutWidget\Helper\General as GeneralHelper;

/**
* Handler Quote or Order Item
*/
class Item
{
/**
* @var GeneralHelper
*/
private $generalHelper;

public function __construct(GeneralHelper $generalHelper)
{
$this->generalHelper = $generalHelper;
}

/**
* Gets formatted price from quote or order item
* When price value = 0 method will return value equivalent 0.01
*
* @param $item
* @return string
* @throws LocalizedException
*/
public function getPriceValue(AbstractExtensibleModel $item)
{
if (!($item instanceof OrderItem) && !($item instanceof QuoteItem)) {
$exception = new LocalizedException(
__('Cannot get price value from the item. Item must be Item from Quote or Item from Order.')
);
$this->generalHelper->addTolog('exception', $exception->getMessage());
throw $exception;
}

if ($item->getParentItemId() > 0) {
$priceValue = $item->getParentItem()->getPriceInclTax();
} else {
$priceValue = $item->getPriceInclTax();
}

return $priceValue <= 0.01 ? 0.01 : $priceValue;
}
}
Loading

0 comments on commit 52a5009

Please sign in to comment.