Skip to content

Commit

Permalink
Merge pull request #576 from magento-firedrakes/bugfixes
Browse files Browse the repository at this point in the history
Bugs
- MAGETWO-59905 Order create page broken after refresh
  • Loading branch information
VladimirZaets authored Nov 8, 2016
2 parents fa0ed96 + c421e93 commit 6619bdf
Show file tree
Hide file tree
Showing 18 changed files with 504 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ protected function _fillTemporaryFlatTable(array $tables, $storeId, $valueFieldS
if (!empty($columnValueNames)) {
$select->joinLeft(
$temporaryValueTableName,
sprintf('e.%1$s = %2$s.%1$s', $linkField, $temporaryTableName),
sprintf('e.%1$s = %2$s.%1$s', $linkField, $temporaryValueTableName),
$columnValueNames
);
$allColumns = array_merge($allColumns, $columnValueNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,44 +107,42 @@ protected function setUp()

public function testBuild()
{
list($storeId, $changedIds, $valueFieldSuffix, $tableDropSuffix, $fillTmpTables) = [1, [], '', '', true];
$storeId = 1;
$changedIds = [];
$valueFieldSuffix = '_value';
$tableDropSuffix = '';
$fillTmpTables = true;
$tableName = 'catalog_product_entity';
$attributeTable = 'catalog_product_entity_int';
$temporaryTableName = 'catalog_product_entity_int_tmp_indexer';
$temporaryValueTableName = 'catalog_product_entity_int_tmp_indexer';
$temporaryValueTableName = 'catalog_product_entity_int_tmp_indexer_value';
$linkField = 'entity_id';
$statusId = 22;
$eavCustomField = 'space_weight';
$eavCustomValueField = $eavCustomField . $valueFieldSuffix;
$this->flatIndexerMock->expects($this->once())->method('getAttributes')->willReturn([]);
$this->flatIndexerMock->expects($this->exactly(3))->method('getFlatColumns')
->willReturnOnConsecutiveCalls(
[],
[$linkField => []],
[$linkField => []]
);
->willReturnOnConsecutiveCalls([], [$eavCustomValueField => []], [$eavCustomValueField => []]);
$this->flatIndexerMock->expects($this->once())->method('getFlatIndexes')->willReturn([]);
$statusAttributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)
->disableOriginalConstructor()
->getMock();
$eavCustomAttributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)
->disableOriginalConstructor()
->getMock();
$this->flatIndexerMock->expects($this->once())->method('getTablesStructure')
->willReturn(
[
'catalog_product_entity' => [
$linkField => $statusAttributeMock
],
'catalog_product_entity' => [$linkField => $statusAttributeMock],
'catalog_product_entity_int' => [
$linkField => $statusAttributeMock
$linkField => $statusAttributeMock,
$eavCustomField => $eavCustomAttributeMock
]
]
);
$this->flatIndexerMock->expects($this->atLeastOnce())->method('getTable')
->withConsecutive(
[$tableName],
['catalog_product_website']
)
->willReturnOnConsecutiveCalls(
$tableName,
'catalog_product_website'
);
->withConsecutive([$tableName], ['catalog_product_website'])
->willReturnOnConsecutiveCalls($tableName, 'catalog_product_website');
$this->flatIndexerMock->expects($this->once())->method('getAttribute')
->with('status')
->willReturn($statusAttributeMock);
Expand All @@ -155,6 +153,9 @@ public function testBuild()
$statusAttributeMock->expects($this->atLeastOnce())->method('getBackend')->willReturn(
$backendMock
);
$eavCustomAttributeMock->expects($this->atLeastOnce())->method('getBackend')->willReturn(
$backendMock
);
$statusAttributeMock->expects($this->atLeastOnce())->method('getId')->willReturn($statusId);
$tableMock = $this->getMockBuilder(\Magento\Framework\DB\Ddl\Table::class)
->disableOriginalConstructor()
Expand Down Expand Up @@ -185,12 +186,12 @@ public function testBuild()
[
$temporaryTableName,
"e.{$linkField} = {$temporaryTableName}.{$linkField}",
[$linkField]
[$linkField, $eavCustomField]
],
[
$temporaryValueTableName,
"e.{$linkField} = " . $temporaryValueTableName . ".{$linkField}",
[$linkField]
"e.{$linkField} = {$temporaryValueTableName}.{$linkField}",
[$eavCustomValueField]
]
)->willReturnSelf();
$this->metadataPoolMock->expects($this->atLeastOnce())->method('getMetadata')->with(ProductInterface::class)
Expand Down
3 changes: 0 additions & 3 deletions app/code/Magento/CatalogInventory/etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
<event name="sales_order_item_cancel">
<observer name="inventory" instance="Magento\CatalogInventory\Observer\CancelOrderItemObserver"/>
</event>
<event name="sales_order_creditmemo_save_after">
<observer name="inventory" instance="Magento\CatalogInventory\Observer\RefundOrderInventoryObserver"/>
</event>
<event name="catalog_product_save_after">
<observer name="inventory" instance="Magento\CatalogInventory\Observer\SaveInventoryDataObserver"/>
</event>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
define([
'jquery',
'mage/template',
'underscore',
'jquery/ui',
'mage/validation'
], function ($, mageTemplate) {
], function ($, mageTemplate, _) {
'use strict';

$.widget('mage.regionUpdater', {
Expand Down Expand Up @@ -124,6 +125,8 @@ define([
* @private
*/
_clearError: function () {
var args = ['clearError', this.options.regionListId, this.options.regionInputId, this.options.postcodeId];

if (this.options.clearError && typeof this.options.clearError === 'function') {
this.options.clearError.call(this);
} else {
Expand All @@ -133,8 +136,8 @@ define([

this.options.form = $(this.options.form);

this.options.form && this.options.form.data('validator') && this.options.form.validation('clearError',
this.options.regionListId, this.options.regionInputId, this.options.postcodeId);
this.options.form && this.options.form.data('validator') &&
this.options.form.validation.apply(this.options.form, _.compact(args));

// Clean up errors on region & zip fix
$(this.options.regionInputId).removeClass('mage-error').parent().find('[generated]').remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ public function load()
foreach ($creditmemo->getAllItems() as $creditmemoItem) {
$orderItem = $creditmemoItem->getOrderItem();
$parentId = $orderItem->getParentItemId();
if (isset($backToStock[$orderItem->getId()])) {
if ($parentId && isset($backToStock[$parentId]) && $backToStock[$parentId]) {
$creditmemoItem->setBackToStock(true);
} elseif ($orderItem->getParentItem() && isset($backToStock[$parentId]) && $backToStock[$parentId]) {
} elseif (isset($backToStock[$orderItem->getId()])) {
$creditmemoItem->setBackToStock(true);
} elseif (empty($savedData)) {
$creditmemoItem->setBackToStock(
Expand Down
54 changes: 51 additions & 3 deletions app/code/Magento/Sales/Model/Order/CreditmemoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sales\Model\Order;

/**
Expand All @@ -23,6 +22,11 @@ class CreditmemoFactory
*/
protected $taxConfig;

/**
* @var \Magento\Framework\Unserialize\Unserialize
*/
protected $unserialize;

/**
* Factory constructor
*
Expand Down Expand Up @@ -57,7 +61,12 @@ public function createByOrder(\Magento\Sales\Model\Order $order, array $data = [

$item = $this->convertor->itemToCreditmemoItem($orderItem);
if ($orderItem->isDummy()) {
$qty = 1;
if (isset($data['qtys'][$orderItem->getParentItemId()])) {
$parentQty = $data['qtys'][$orderItem->getParentItemId()];
} else {
$parentQty = $orderItem->getParentItem() ? $orderItem->getParentItem()->getQtyToRefund() : 1;
}
$qty = $this->calculateProductOptions($orderItem, $parentQty);
$orderItem->setLockedDoShip(true);
} else {
if (isset($qtys[$orderItem->getId()])) {
Expand Down Expand Up @@ -132,7 +141,12 @@ public function createByInvoice(\Magento\Sales\Model\Order\Invoice $invoice, arr

$item = $this->convertor->itemToCreditmemoItem($orderItem);
if ($orderItem->isDummy()) {
$qty = 1;
if (isset($data['qtys'][$orderItem->getParentItemId()])) {
$parentQty = $data['qtys'][$orderItem->getParentItemId()];
} else {
$parentQty = $orderItem->getParentItem() ? $orderItem->getParentItem()->getQtyToRefund() : 1;
}
$qty = $this->calculateProductOptions($orderItem, $parentQty);
} else {
if (isset($qtys[$orderItem->getId()])) {
$qty = (double)$qtys[$orderItem->getId()];
Expand Down Expand Up @@ -245,4 +259,38 @@ protected function initData($creditmemo, $data)
$creditmemo->setAdjustmentNegative($data['adjustment_negative']);
}
}

/**
* @param \Magento\Sales\Api\Data\OrderItemInterface $orderItem
* @param array $qtys
* @return int
*/
private function calculateProductOptions(\Magento\Sales\Api\Data\OrderItemInterface $orderItem, $parentQty)
{
$qty = $parentQty;
$productOptions = $orderItem->getProductOptions();
if (isset($productOptions['bundle_selection_attributes'])) {
$bundleSelectionAttributes = $this->getUnserialize()
->unserialize($productOptions['bundle_selection_attributes']);
if ($bundleSelectionAttributes) {
$qty = $bundleSelectionAttributes['qty'] * $parentQty;
}
}
return $qty;
}

/**
* Get Unserialize
*
* @return \Magento\Framework\Unserialize\Unserialize
* @deprecated
*/
private function getUnserialize()
{
if (!$this->unserialize) {
$this->unserialize = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Unserialize\Unserialize::class);
}
return $this->unserialize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@
<?php endif; ?>

<?php if ($block->canDisplayPrice()): ?>
<td class="col-price"><?php /* @noEscape */ echo $block->getItemPrice($_item) ?></td>
<td class="col-price">
<?php if ($block->getDataId() == 'cart'): ?>
<?php /* @noEscape */ echo $block->getItemPrice($_item->getProduct()) ?>
<?php else: ?>
<?php /* @noEscape */ echo $block->getItemPrice($_item) ?>
<?php endif; ?>
</td>
<?php endif; ?>

<?php if ($block->canRemoveItems()): ?>
Expand Down
45 changes: 6 additions & 39 deletions app/code/Magento/SalesInventory/Model/Order/ReturnProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Magento\SalesInventory\Model\Order;

use Magento\Sales\Api\Data\CreditmemoInterface;
use Magento\Sales\Api\Data\CreditmemoItemInterface;
use Magento\Sales\Api\Data\OrderInterface;

/**
Expand All @@ -29,75 +28,58 @@ class ReturnProcessor
*/
private $priceIndexer;

/**
* @var \Magento\Sales\Api\CreditmemoRepositoryInterface
*/
private $creditmemoRepository;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;

/**
* @var \Magento\Sales\Api\OrderRepositoryInterface
*/
private $orderRepository;

/**
* @var \Magento\Sales\Api\OrderItemRepositoryInterface
*/
private $orderItemRepository;

/**
* ReturnToStockPlugin constructor.
* @param \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration
* ReturnProcessor constructor.
* @param \Magento\CatalogInventory\Api\StockManagementInterface $stockManagement
* @param \Magento\CatalogInventory\Model\Indexer\Stock\Processor $stockIndexer
* @param \Magento\Catalog\Model\Indexer\Product\Price\Processor $priceIndexer
* @param \Magento\Sales\Api\CreditmemoRepositoryInterface $creditmemoRepository
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
* @param \Magento\Sales\Api\OrderItemRepositoryInterface $orderItemRepository
*/
public function __construct(
\Magento\CatalogInventory\Api\StockManagementInterface $stockManagement,
\Magento\CatalogInventory\Model\Indexer\Stock\Processor $stockIndexer,
\Magento\Catalog\Model\Indexer\Product\Price\Processor $priceIndexer,
\Magento\Sales\Api\CreditmemoRepositoryInterface $creditmemoRepository,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
\Magento\Sales\Api\OrderItemRepositoryInterface $orderItemRepository
) {
$this->stockManagement = $stockManagement;
$this->stockIndexerProcessor = $stockIndexer;
$this->priceIndexer = $priceIndexer;
$this->creditmemoRepository = $creditmemoRepository;
$this->storeManager = $storeManager;
$this->orderRepository = $orderRepository;
$this->orderItemRepository = $orderItemRepository;
}

/**
* @param CreditmemoInterface $creditmemo
* @param OrderInterface $order
* @param array $returnToStockItems
* @param bool $isAutoReturn
* @return void
*/
public function execute(
CreditmemoInterface $creditmemo,
OrderInterface $order,
array $returnToStockItems = []
array $returnToStockItems = [],
$isAutoReturn = false
) {
$itemsToUpdate = [];
foreach ($creditmemo->getItems() as $item) {
$qty = $item->getQty();
$productId = $item->getProductId();
$orderItem = $this->orderItemRepository->get($item->getOrderItemId());
$parentItemId = $orderItem->getParentItemId();
if ($this->canReturnItem($item, $qty, $parentItemId, $returnToStockItems)) {
$parentItem = $parentItemId ? $this->getItemByOrderId($creditmemo, $parentItemId) : false;
$qty = $parentItem ? $parentItem->getQty() * $qty : $qty;
$qty = $item->getQty();
if ($isAutoReturn || $this->canReturnItem($item, $qty, $parentItemId, $returnToStockItems)) {
if (isset($itemsToUpdate[$productId])) {
$itemsToUpdate[$productId] += $qty;
} else {
Expand All @@ -122,21 +104,6 @@ public function execute(
}
}

/**
* @param \Magento\Sales\Api\Data\CreditmemoInterface $creditmemo
* @param int $parentItemId
* @return bool|CreditmemoItemInterface
*/
private function getItemByOrderId(\Magento\Sales\Api\Data\CreditmemoInterface $creditmemo, $parentItemId)
{
foreach ($creditmemo->getItems() as $item) {
if ($item->getOrderItemId() == $parentItemId) {
return $item;
}
}
return false;
}

/**
* @param \Magento\Sales\Api\Data\CreditmemoItemInterface $item
* @param int $qty
Expand Down
Loading

0 comments on commit 6619bdf

Please sign in to comment.