Skip to content

Commit

Permalink
Merge pull request #16 from snowio/feature/adhoc-adjustments
Browse files Browse the repository at this point in the history
Adhoc adjustments and Extra Gaurd Clauses
  • Loading branch information
Alexander Wanyoike authored May 28, 2021
2 parents e4c53e2 + 94df304 commit 07a920d
Show file tree
Hide file tree
Showing 16 changed files with 1,039 additions and 233 deletions.
61 changes: 61 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
language: php
php:
- 7.3
dist: xenial

env:
matrix:
- TEST_GROUP=magento_latest
- TEST_GROUP=magento_23
matrix:
exclude:
- php: 7.4
env: TEST_GROUP=magento_23
- php: 7.3
env: TEST_GROUP=magento_latest

before_install:
- phpenv config-rm xdebug.ini || true
- composer self-update 1.10.16

install:
- composer config repositories.foomanmirror composer https://repo-magento-mirror.fooman.co.nz/ # TODO this allows us to composer install, but we only need the dev dependency. Maybe wget it instead?
- composer install --no-interaction
# Install magento
- if [[ $TEST_GROUP = magento_23 ]]; then NAME=snowepr FULL_INSTALL=0 VERSION=2.3.6 . ./vendor/bin/travis-install-magento.sh; fi
- if [[ $TEST_GROUP = magento_latest ]]; then NAME=snowepr FULL_INSTALL=0 . ./vendor/bin/travis-install-magento.sh; fi
# Install this module
- cd vendor/ampersand/travis-vanilla-magento/instances/snowepr
- export COMPOSER_MEMORY_LIMIT=-1
- composer config repo.snowepr git "../../../../../"
- composer require -vvv snowio/magento2-extended-sales-repositories:"dev-$TRAVIS_BRANCH" || composer require -vvv snowio/magento2-extended-sales-repository:"$TRAVIS_BRANCH"
# Configure for integration tests
- mysql -uroot -e 'SET @@global.sql_mode = NO_ENGINE_SUBSTITUTION; DROP DATABASE IF EXISTS magento_integration_tests; CREATE DATABASE magento_integration_tests;'
- cp dev/tests/integration/etc/install-config-mysql.travis-no-rabbitmq.php.dist dev/tests/integration/etc/install-config-mysql.php
- php $TRAVIS_BUILD_DIR/travis/prepare_phpunit_config.php

script:
- vendor/bin/phpunit -c $(pwd)/dev/tests/integration/phpunit.xml.dist --testsuite Integration

addons:
apt:
packages:
- postfix
- apache2
- libapache2-mod-fastcgi

services:
- mysql

cache:
apt: true
directories:
- $HOME/.composer/cache
- $HOME/bin

after_failure:
- test -d ./vendor/ampersand/travis-vanilla-magento/instances/snowepr/var/report/ && for r in ./vendor/ampersand/travis-vanilla-magento/instances/snowepr/var/report/*; do cat $r; done
- test -f ./vendor/ampersand/travis-vanilla-magento/instances/snowepr/var/log/system.log && grep -v "Broken reference" ./vendor/ampersand/travis-vanilla-magento/instances/snowepr/var/log/system.log
- test -f ./vendor/ampersand/travis-vanilla-magento/instances/snowepr/var/log/exception.log && cat ./vendor/ampersand/travis-vanilla-magento/instances/snowepr/var/log/exception.log
- test -f ./vendor/ampersand/travis-vanilla-magento/instances/snowepr/var/log/support_report.log && grep -v "Broken reference" ./vendor/ampersand/travis-vanilla-magento/instances/snowepr/var/log/support_report.log
- sleep 10;
44 changes: 44 additions & 0 deletions Exception/SnowCreditMemoException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
namespace SnowIO\ExtendedSalesRepositories\Exception;

use Magento\Framework\Phrase;
use Magento\Framework\Webapi\Exception;

/**
* Class SnowCreditMemoException
*
* @package SnowIO\ExtendedSalesRepositories\Exception
*/
class SnowCreditMemoException extends Exception
{
/**
* HTTP code precondition failed
*/
const HTTP_PRECONDITION_FAILED = 412;

/**
* SnowCreditMemoException constructor.
*
* Magento does not support HTTP response code `412` by default, extend the web API exception and set manually
*
* @phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod.Found
* @param \Magento\Framework\Phrase $phrase
* @param int $code
* @param int $httpCode
* @param array $details
* @param string $name
* @param null $errors
* @param null $stackTrace
*/
public function __construct(
Phrase $phrase,
$code = 0,
$httpCode = self::HTTP_PRECONDITION_FAILED,
array $details = [],
$name = '',
$errors = null,
$stackTrace = null
) {
parent::__construct($phrase, $code, $httpCode, $details, $name, $errors, $stackTrace);
}
}
97 changes: 97 additions & 0 deletions Model/AvailableQuantityProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
namespace SnowIO\ExtendedSalesRepositories\Model;

use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderItemInterface;

/**
* Class AvailableQuantityProvider
*
* @package SnowIO\ExtendedSalesRepositories\Model
*/
class AvailableQuantityProvider
{
/**
* @var array
*/
private $availableByOrder = [];

/**
* Provide available quantities
*
* @param \Magento\Sales\Api\Data\OrderInterface $order
* @return array
* @author Daniel Doyle <dd@amp.co>
*/
public function provide(OrderInterface $order) : array
{
if (array_key_exists($order->getEntityId(), $this->availableByOrder)) {
return $this->availableByOrder[$order->getEntityId()];
}

$quantityRefunded = $this->getQuantityRefunded($order);

return $this->availableByOrder[$order->getEntityId()] = array_reduce(
array_keys($quantityRefunded),
static function (array $quantityAvailable, int $orderItemId) use ($quantityRefunded) : array {
if (!array_key_exists($orderItemId, $quantityAvailable)) {
return $quantityAvailable;
}

// Sum all quantities for the order item provided by the collected credit memos
$quantityAvailable[$orderItemId] =
max($quantityAvailable[$orderItemId] - array_sum($quantityRefunded[$orderItemId]), 0);

return $quantityAvailable;
},
$this->getQuantityAvailable($order)
);
}


/**
* Get quantity of items refunded in all credit memos
*
* @param \Magento\Sales\Api\Data\OrderInterface $order
* @return array
* @author Daniel Doyle <dd@amp.co>
*/
private function getQuantityRefunded(OrderInterface $order) : array
{
$refundedItems = [];
foreach ($order->getCreditmemosCollection() as $creditmemo) {
foreach ($creditmemo->getItems() as $creditmemoItem) {
$quantityRefunded = (float) $creditmemoItem->getQty();
if ($quantityRefunded < 1) {
continue;
}

// Dynamically create array of quantities for aggregation later
$refundedItems[$creditmemoItem->getOrderItemId()][] = $quantityRefunded;
}
}

return $refundedItems;
}

/**
* Get quantity of available items from order. As we're querying all credit memos (including broken/invalid ones) we
* base the refunded quantity on them instead of relying on `getQtyRefunded` set against an order item
*
* @param \Magento\Sales\Api\Data\OrderInterface $order
* @return array
* @author Daniel Doyle <dd@amp.co>
*/
private function getQuantityAvailable(OrderInterface $order) : array
{
return array_reduce(
$order->getAllItems(),
function (array $refundedItems, OrderItemInterface $orderItem) : array {
$refundedItems[$orderItem->getItemId()] = (float) $orderItem->getQtyOrdered();

return $refundedItems;
},
[]
);
}
}
Loading

0 comments on commit 07a920d

Please sign in to comment.