Skip to content

Commit

Permalink
Merge pull request #80 from magento-firedrakes/pr
Browse files Browse the repository at this point in the history
[Firedrakes] regression bugfixes
  • Loading branch information
Alexander Akimov committed Jun 7, 2016
2 parents 440b6c3 + 8018a85 commit 3fbefa4
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,6 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
$product->lockAttribute('media');
}

if ($this->storeManager->hasSingleStore() && empty($product->getWebsiteIds())) {
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsite()->getId()]);
}

/**
* Check "Use Default Value" checkboxes values
*/
Expand Down
5 changes: 3 additions & 2 deletions app/code/Magento/Catalog/Model/ResourceModel/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,9 @@ public function delete($object)
*/
protected function _saveWebsiteIds($product)
{
if (empty(array_diff($product->getWebsiteIds(), [0]))) {
$product->setWebsiteIds([1]);
if ($this->_storeManager->isSingleStoreMode()) {
$id = $this->_storeManager->getDefaultStoreView()->getWebsiteId();
$product->setWebsiteIds([$id]);
}
$websiteIds = $product->getWebsiteIds();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
/**
* @var MetadataPool
*/
protected $metadataPool;
private $metadataPool;

/**
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
Expand Down Expand Up @@ -219,7 +219,8 @@ public function addProductFilter($products)
if (!is_array($products)) {
$products = [$products];
}
$this->getSelect()->where('links.product_id IN (?)', $products);
$identifierField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getIdentifierField();
$this->getSelect()->where("product_entity_table.$identifierField IN (?)", $products);
$this->_hasLinkFilter = true;
}

Expand Down Expand Up @@ -258,6 +259,7 @@ protected function _beforeLoad()
{
if ($this->getLinkModel()) {
$this->_joinLinks();
$this->joinProductsToLinks();
}
return parent::_beforeLoad();
}
Expand Down Expand Up @@ -423,12 +425,28 @@ public function addLinkAttributeToFilter($code, $condition)
/**
* Get MetadataPool instance
* @return MetadataPool
* @deprecated
*/
protected function getMetadataPool()
private function getMetadataPool()
{
if (!$this->metadataPool) {
$this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
}
return $this->metadataPool;
}

/**
* Join Product To Links
* @return void
*/
private function joinProductsToLinks()
{
if ($this->_hasLinkFilter) {
$metaDataPool = $this->getMetadataPool()->getMetadata(ProductInterface::class);
$linkField = $metaDataPool->getLinkField();
$entityTable = $metaDataPool->getEntityTable();
$this->getSelect()
->join(['product_entity_table' => $entityTable], "links.product_id = product_entity_table.$linkField", []);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,6 @@ protected function setUp()
*/
public function testInitialize()
{
$this->websiteMock->expects($this->once())
->method('getId')
->willReturn($this->websiteId);
$this->storeMock->expects($this->once())
->method('getWebsite')
->willReturn($this->websiteMock);
$this->storeManagerMock->expects($this->once())
->method('getStore')
->with(true)
->willReturn($this->storeMock);
$this->customOptionMock->expects($this->once())
->method('setProductSku');
$this->customOptionMock->expects($this->once())
Expand Down Expand Up @@ -270,9 +260,6 @@ public function testInitialize()
->method('filter')
->with(['stock_data'])
->willReturn(['stock_data']);
$this->storeManagerMock->expects($this->once())
->method('hasSingleStore')
->willReturn(true);
$this->productMock->expects($this->once())
->method('isLockedAttribute')
->with('media')
Expand Down Expand Up @@ -300,9 +287,6 @@ public function testInitialize()
$this->productMock->expects($this->once())
->method('getSku')
->willReturn('sku');
$this->productMock->expects($this->once())
->method('setWebsiteIds')
->with([$this->websiteId]);
$this->productMock->expects($this->any())
->method('getOptionsReadOnly')
->willReturn(false);
Expand Down
10 changes: 10 additions & 0 deletions app/code/Magento/Downloadable/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
<type name="Magento\Catalog\Model\Product\CartConfiguration">
<plugin name="Downloadable" type="Magento\Downloadable\Model\Product\CartConfiguration\Plugin\Downloadable" />
</type>
<virtualType name="context_for_downloadable" type="Magento\Framework\View\Element\Template\Context">
<arguments>
<argument name="urlBuilder" xsi:type="object">Magento\Framework\Url</argument>
</arguments>
</virtualType>
<type name="Magento\Downloadable\Block\Sales\Order\Email\Items\Order\Downloadable">
<arguments>
<argument name="context" xsi:type="object">context_for_downloadable</argument>
</arguments>
</type>
<type name="Magento\Catalog\Model\Product\TypeTransitionManager">
<plugin name="downloadable_product_transition" type="Magento\Downloadable\Model\Product\TypeTransitionManager\Plugin\Downloadable" />
<arguments>
Expand Down
89 changes: 45 additions & 44 deletions app/code/Magento/Sales/Model/Order/Creditmemo/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,28 +127,16 @@ public function setOrderItem(\Magento\Sales\Model\Order\Item $item)
public function getOrderItem()
{
if ($this->_orderItem === null) {
$this->_orderItem = $this->getOrderItemWithoutCaching();
if ($this->getCreditmemo()) {
$orderItem = $this->getCreditmemo()->getOrder()->getItemById($this->getOrderItemId());
} else {
$orderItem = $this->_orderItemFactory->create()->load($this->getOrderItemId());
}
$this->_orderItem = $orderItem;
}
return $this->_orderItem;
}

/**
* Retrieve order item instance without set it to property.
* It is need for ability to process setQty on api when credit memo and order has not built yet.
*
* @return \Magento\Sales\Model\Order\Item
*/
private function getOrderItemWithoutCaching()
{
if ($this->getCreditmemo()) {
$orderItem = $this->getCreditmemo()->getOrder()->getItemById($this->getOrderItemId());
} else {
$orderItem = $this->_orderItemFactory->create()->load($this->getOrderItemId());
}

return $orderItem;
}

/**
* Checks if quantity available for refund
*
Expand All @@ -164,26 +152,12 @@ private function isQtyAvailable($qty, \Magento\Sales\Model\Order\Item $orderItem
/**
* Declare qty
*
* @param float $qty
* @param float $qty
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function setQty($qty)
{
$orderItem = $this->getOrderItemWithoutCaching();
if ($orderItem->getIsQtyDecimal()) {
$qty = (double)$qty;
} else {
$qty = (int)$qty;
}
$qty = $qty > 0 ? $qty : 0;
if ($this->isQtyAvailable($qty, $orderItem)) {
$this->setData('qty', $qty);
} else {
throw new \Magento\Framework\Exception\LocalizedException(
__('We found an invalid quantity to refund item "%1".', $this->getName())
);
}
$this->setData(CreditmemoItemInterface::QTY, $qty);
return $this;
}

Expand All @@ -196,7 +170,8 @@ public function register()
{
$orderItem = $this->getOrderItem();

$orderItem->setQtyRefunded($orderItem->getQtyRefunded() + $this->getQty());
$qty = $this->processQty();
$orderItem->setQtyRefunded($orderItem->getQtyRefunded() + $qty);
$orderItem->setTaxRefunded($orderItem->getTaxRefunded() + $this->getTaxAmount());
$orderItem->setBaseTaxRefunded($orderItem->getBaseTaxRefunded() + $this->getBaseTaxAmount());
$orderItem->setDiscountTaxCompensationRefunded(
Expand All @@ -213,22 +188,46 @@ public function register()
return $this;
}

/**
* @return int|float
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function processQty()
{
$orderItem = $this->getOrderItem();
$qty = $this->getQty();
if ($orderItem->getIsQtyDecimal()) {
$qty = (double)$qty;
} else {
$qty = (int)$qty;
}
$qty = $qty > 0 ? $qty : 0;
if ($this->isQtyAvailable($qty, $orderItem)) {
return $qty;
} else {
throw new \Magento\Framework\Exception\LocalizedException(
__('We found an invalid quantity to refund item "%1".', $this->getName())
);
}
}

/**
* @return $this
*/
public function cancel()
{
$this->getOrderItem()->setQtyRefunded($this->getOrderItem()->getQtyRefunded() - $this->getQty());
$qty = $this->processQty();
$this->getOrderItem()->setQtyRefunded($this->getOrderItem()->getQtyRefunded() - $qty);
$this->getOrderItem()->setTaxRefunded(
$this->getOrderItem()->getTaxRefunded() -
$this->getOrderItem()->getBaseTaxAmount() *
$this->getQty() /
$qty /
$this->getOrderItem()->getQtyOrdered()
);
$this->getOrderItem()->setDiscountTaxCompensationRefunded(
$this->getOrderItem()->getDiscountTaxCompensationRefunded() -
$this->getOrderItem()->getDiscountTaxCompensationAmount() *
$this->getQty() /
$qty /
$this->getOrderItem()->getQtyOrdered()
);
return $this;
Expand All @@ -250,21 +249,22 @@ public function calcRowTotal()
$rowTotalInclTax = $orderItem->getRowTotalInclTax();
$baseRowTotalInclTax = $orderItem->getBaseRowTotalInclTax();

if (!$this->isLast() && $orderItemQtyInvoiced > 0 && $this->getQty() >= 0) {
$qty = $this->processQty();
if (!$this->isLast() && $orderItemQtyInvoiced > 0 && $qty >= 0) {
$availableQty = $orderItemQtyInvoiced - $orderItem->getQtyRefunded();
$rowTotal = $creditmemo->roundPrice($rowTotal / $availableQty * $this->getQty());
$baseRowTotal = $creditmemo->roundPrice($baseRowTotal / $availableQty * $this->getQty(), 'base');
$rowTotal = $creditmemo->roundPrice($rowTotal / $availableQty * $qty);
$baseRowTotal = $creditmemo->roundPrice($baseRowTotal / $availableQty * $qty, 'base');
}
$this->setRowTotal($rowTotal);
$this->setBaseRowTotal($baseRowTotal);

if ($rowTotalInclTax && $baseRowTotalInclTax) {
$orderItemQty = $orderItem->getQtyOrdered();
$this->setRowTotalInclTax(
$creditmemo->roundPrice($rowTotalInclTax / $orderItemQty * $this->getQty(), 'including')
$creditmemo->roundPrice($rowTotalInclTax / $orderItemQty * $qty, 'including')
);
$this->setBaseRowTotalInclTax(
$creditmemo->roundPrice($baseRowTotalInclTax / $orderItemQty * $this->getQty(), 'including_base')
$creditmemo->roundPrice($baseRowTotalInclTax / $orderItemQty * $qty, 'including_base')
);
}
return $this;
Expand All @@ -278,7 +278,8 @@ public function calcRowTotal()
public function isLast()
{
$orderItem = $this->getOrderItem();
if ((string)(double)$this->getQty() == (string)(double)$orderItem->getQtyToRefund()) {
$qty = $this->processQty();
if ((string)(double)$qty == (string)(double)$orderItem->getQtyToRefund()) {
return true;
}
return false;
Expand Down
Loading

0 comments on commit 3fbefa4

Please sign in to comment.