Skip to content

Commit

Permalink
Merge pull request #1077 from magento-tsg/2.1.8-develop-pr14
Browse files Browse the repository at this point in the history
[TSG] Backporting for 2.1 (pr14) (2.1.8)
  • Loading branch information
Volodymyr Klymenko committed May 3, 2017
2 parents b8b698b + 7a03b97 commit 75a7b9b
Show file tree
Hide file tree
Showing 20 changed files with 622 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
namespace Magento\Sales\Model\ResourceModel\Order\Payment;

use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Api\Data\OrderPaymentSearchResultInterface;
use Magento\Sales\Model\ResourceModel\Order\Collection\AbstractCollection;

Expand Down Expand Up @@ -68,42 +67,16 @@ protected function _construct()
}

/**
* Unserialize additional_information in each item
* Unserialize additional_information in each item.
*
* @return $this
*/
protected function _afterLoad()
{
foreach ($this->_items as $item) {
$this->getResource()->unserializeFields($item);
if (!empty($item->getData(OrderPaymentInterface::ADDITIONAL_INFORMATION))) {
$additionalInfo = $this->convertAdditionalInfo(
$item->getData(OrderPaymentInterface::ADDITIONAL_INFORMATION)
);
$item->setData(OrderPaymentInterface::ADDITIONAL_INFORMATION, $additionalInfo);
}
}
return parent::_afterLoad();
}

/**
* Convert multidimensional additional information array to single
*
* @param array $info
* @return array
*/
private function convertAdditionalInfo($info)
{
$result = [];
foreach ($info as $key => $item) {
if (is_array($item)) {
$result += $this->convertAdditionalInfo($item);
unset($info[$key]);
} else {
$result[$key] = $item;
}
}

return $result;
return parent::_afterLoad();
}
}
80 changes: 80 additions & 0 deletions app/code/Magento/Sales/Observer/ConvertAdditionalInfoObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sales\Observer;

use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Model\ResourceModel\Order\Payment\Collection;

/**
* Class ConvertAdditionalInfo convert additional info from multidimensional array into single one for API calls.
*/
class ConvertAdditionalInfoObserver implements ObserverInterface
{
/**
* Application state for identifying API calls.
*
* @var State
*/
private $state;

/**
* ConvertAdditionalInfoObserver constructor.
* @param State $state
*/
public function __construct(State $state)
{
$this->state = $state;
}

/**
* Convert additional info from multidimensional array into single one for API calls.
*
* @param Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var Collection $paymentCollection */
$paymentCollection = $observer->getData('order_payment_collection');
$areaCode = $this->state->getAreaCode();
if ($areaCode == Area::AREA_WEBAPI_REST || $areaCode == Area::AREA_WEBAPI_SOAP) {
foreach ($paymentCollection as $payment) {
if (!empty($payment->getData(OrderPaymentInterface::ADDITIONAL_INFORMATION))) {
$additionalInfo = $this->convertAdditionalInfo(
$payment->getData(OrderPaymentInterface::ADDITIONAL_INFORMATION)
);
$payment->setData(OrderPaymentInterface::ADDITIONAL_INFORMATION, $additionalInfo);
}
}
}
}

/**
* Convert multidimensional additional information array to single.
*
* @param array $info
* @return array
*/
private function convertAdditionalInfo($info)
{
$result = [];
foreach ($info as $key => $item) {
if (is_array($item)) {
$result += $this->convertAdditionalInfo($item);
unset($info[$key]);
} else {
$result[$key] = $item;
}
}

return $result;
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/Sales/etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@
<event name="store_add">
<observer name="magento_sequence" instance="Magento\SalesSequence\Observer\SequenceCreatorObserver" />
</event>
<event name="sales_order_payment_collection_load_after">
<observer name="convert_additional_info" instance="Magento\Sales\Observer\ConvertAdditionalInfoObserver" />
</event>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,40 @@

class Generate extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote
{
/**
* @var \Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory
*/
private $generationSpecFactory;

/**
* @var \Magento\SalesRule\Model\Service\CouponManagementService
*/
private $couponManagementService;

/**
* Generate constructor.
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
* @param \Magento\SalesRule\Model\Service\CouponManagementService|null $couponManagementService
* @param \Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory|null $generationSpecFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Registry $coreRegistry,
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
\Magento\SalesRule\Model\Service\CouponManagementService $couponManagementService = null,
\Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory $generationSpecFactory = null
) {
parent::__construct($context, $coreRegistry, $fileFactory, $dateFilter);
$this->generationSpecFactory = $generationSpecFactory ?:
$this->_objectManager->get(\Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory::class);
$this->couponManagementService = $couponManagementService ?:
$this->_objectManager->get(\Magento\SalesRule\Model\Service\CouponManagementService::class);
}

/**
* Generate Coupons action
*
Expand All @@ -17,6 +51,7 @@ public function execute()
{
if (!$this->getRequest()->isAjax()) {
$this->_forward('noroute');

return;
}
$result = [];
Expand All @@ -35,18 +70,15 @@ public function execute()
$data = $inputFilter->getUnescaped();
}

/** @var $generator \Magento\SalesRule\Model\Coupon\Massgenerator */
$generator = $this->_objectManager->get('Magento\SalesRule\Model\Coupon\Massgenerator');
if (!$generator->validateData($data)) {
$result['error'] = __('Invalid data provided');
} else {
$generator->setData($data);
$generator->generatePool();
$generated = $generator->getGeneratedCount();
$this->messageManager->addSuccess(__('%1 coupon(s) have been generated.', $generated));
$this->_view->getLayout()->initMessages();
$result['messages'] = $this->_view->getLayout()->getMessagesBlock()->getGroupedHtml();
}
$data = $this->convertCouponSpecData($data);
$couponSpec = $this->generationSpecFactory->create(['data' => $data]);
$couponCodes = $this->couponManagementService->generate($couponSpec);
$generated = count($couponCodes);
$this->messageManager->addSuccess(__('%1 coupon(s) have been generated.', $generated));
$this->_view->getLayout()->initMessages();
$result['messages'] = $this->_view->getLayout()->getMessagesBlock()->getGroupedHtml();
} catch (\Magento\Framework\Exception\InputException $inputException) {
$result['error'] = __('Invalid data provided');
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$result['error'] = $e->getMessage();
} catch (\Exception $e) {
Expand All @@ -60,4 +92,18 @@ public function execute()
$this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($result)
);
}

/**
* We should map old values to new one
* We need to do this, as new service with another key names was added
*
* @param array $data
* @return array
*/
private function convertCouponSpecData(array $data)
{
$data['quantity'] = $data['qty'];

return $data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*/
namespace Magento\SalesRule\Model\Service;

use Magento\SalesRule\Model\Coupon;

/**
* Coupon management service class
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ define([
],
"mobileUK": [
function(value) {
return value.length > 9 && value.match(/^((0|\+44)7(5|6|7|8|9){1}\d{2}\s?\d{6})$/);
return value.length > 9 && value.match(/^((0|\+44)7\d{3}\s?\d{6})$/);
},
$.mage.__('Please specify a valid mobile number')
],
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/Ui/view/base/web/js/timeline/timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ define([
* @returns {Boolean}
*/
isActive: function (record) {
return record.status === 1;
return Number(record.status) === 1;
},

/**
Expand All @@ -115,7 +115,7 @@ define([
* @returns {Boolean}
*/
isUpcoming: function (record) {
return record.status === 2;
return Number(record.status) === 2;
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,47 @@
use Magento\Mtf\Client\Locator;

/**
* Class DiscountCodes
* Discount codes block
* Discount codes block.
*/
class DiscountCodes extends Form
{
/**
* Form wrapper selector
* Message after coupon applying.
*
* @var string
*/
protected $couponApplyingMessage = './/div[contains(@class,"messages")]//div[contains(@class,"message")]';

/**
* Form wrapper selector.
*
* @var string
*/
protected $formWrapper = '.content';

/**
* Open discount codes form selector
* Open discount codes form selector.
*
* @var string
*/
protected $openForm = '.payment-option-title';

/**
* Fill discount code input selector
* Fill discount code input selector.
*
* @var string
*/
protected $couponCode = '#discount-code';

/**
* Click apply button selector
* Click apply button selector.
*
* @var string
*/
protected $applyButton = '.action.action-apply';

/**
* Enter discount code and click apply button
* Enter discount code and click apply button.
*
* @param string $code
* @return void
Expand All @@ -55,4 +61,14 @@ public function applyCouponCode($code)
$this->_rootElement->find($this->couponCode, Locator::SELECTOR_CSS)->setValue($code);
$this->_rootElement->find($this->applyButton, Locator::SELECTOR_CSS)->click();
}

/**
* Get message after coupon applying.
*
* @return string
*/
public function getCouponApplyingMessage()
{
return $this->_rootElement->find($this->couponApplyingMessage, Locator::SELECTOR_XPATH)->getText();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magento\Sales\Test\Block\Adminhtml\Order\View\Tab;

use Magento\Mtf\Block\Block;
use Magento\Sales\Test\Block\Adminhtml\Order\View\Tab\Info\PaymentInfoBlock;

/**
* Order information tab block.
Expand All @@ -20,6 +21,13 @@ class Info extends Block
*/
protected $orderStatus = '#order_status';

/**
* Selector for 'Payment Information' block.
*
* @var string
*/
private $paymentInfoBlockSelector = '.order-payment-method';

/**
* Get order status from info block
*
Expand All @@ -29,4 +37,17 @@ public function getOrderStatus()
{
return $this->_rootElement->find($this->orderStatus)->getText();
}

/**
* Returns Payment Information block.
*
* @return PaymentInfoBlock
*/
public function getPaymentInfoBlock()
{
return $this->blockFactory->create(
PaymentInfoBlock::class,
['element' => $this->_rootElement->find($this->paymentInfoBlockSelector)]
);
}
}
Loading

0 comments on commit 75a7b9b

Please sign in to comment.