Skip to content

Commit

Permalink
Merge 705cf3d into 752f7a0
Browse files Browse the repository at this point in the history
  • Loading branch information
renatocason committed Oct 3, 2018
2 parents 752f7a0 + 705cf3d commit 8bbc48f
Show file tree
Hide file tree
Showing 22 changed files with 979 additions and 113 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -12,8 +12,8 @@ install:
- composer create-project --repository=https://repo.magento.com magento/marketplace-eqp marketplace-eqp

script:
- php marketplace-eqp/vendor/bin/phpcs Api/ Console/ Cron/ Helper/ Model/ Test/ --standard=MEQP2 --severity=10
- php vendor/bin/phpmd Api/,Console/,Cron/,Helper/,Model/,Test/ text cleancode,codesize,controversial,design,naming,unusedcode --ignore-violations-on-exit
- php marketplace-eqp/vendor/bin/phpcs Api/ Block/ Console/ Cron/ Helper/ Model/ Test/ --standard=MEQP2 --severity=10
- php vendor/bin/phpmd Api/,Block/,Console/,Cron/,Helper/,Model/,Test/ text cleancode,codesize,controversial,design,naming,unusedcode --ignore-violations-on-exit
- php vendor/phpunit/phpunit/phpunit --coverage-clover Test/Unit/logs/clover.xml Test

after_script:
Expand Down
37 changes: 37 additions & 0 deletions Api/Data/InvoiceProcessItemInterface.php
@@ -0,0 +1,37 @@
<?php

namespace Aune\AutoInvoice\Api\Data;

interface InvoiceProcessItemInterface
{
const KEY_ORDER = 'order';
const KEY_DESTINATION_STATUS = 'destination_status';

/**
* Returns the order to invoice
*
* @returns \Magento\Sales\Api\Data\OrderInterface
*/
public function getOrder();

/**
* Sets the order to invoice
*
* @returns $this
*/
public function setOrder(\Magento\Sales\Api\Data\OrderInterface $order);

/**
* Returns the destination status
*
* @returns string
*/
public function getDestinationStatus();

/**
* Sets the destination status
*
* @returns $this
*/
public function setDestinationStatus(string $status);
}
14 changes: 9 additions & 5 deletions Api/InvoiceProcessInterface.php
Expand Up @@ -2,19 +2,23 @@

namespace Aune\AutoInvoice\Api;

/**
* @api
*/
interface InvoiceProcessInterface
{
/**
* Returns a list of orders that should be invoiced.
* Returns a list of items to process.
* Every item consists of an order, and a destination status.
*
* @returns \Magento\Sales\Model\ResourceModel\Order\Collection
* @returns \Aune\AutoInvoice\Api\Data\InvoiceProcessItemInterface
*/
public function getOrdersToInvoice();
public function getItemsToProcess();

/**
* Invoice order
*
* @param \Magento\Sales\Model\Order $order
* @param \Aune\AutoInvoice\Api\Data\InvoiceProcessItemInterface $item
*/
public function invoice(\Magento\Sales\Model\Order $order);
public function invoice(\Aune\AutoInvoice\Api\Data\InvoiceProcessItemInterface $item);
}
67 changes: 67 additions & 0 deletions Block/Adminhtml/Form/Field/PaymentMethod.php
@@ -0,0 +1,67 @@
<?php

namespace Aune\AutoInvoice\Block\Adminhtml\Form\Field;

use Magento\Framework\View\Element\Context;
use Magento\Framework\View\Element\Html\Select;
use Magento\Payment\Model\Config as PaymentConfig;
use Aune\AutoInvoice\Helper\Data as HelperData;

class PaymentMethod extends Select
{
/**
* @var PaymentConfig
*/
private $paymentConfig;

/**
* @param Context $context
* @param array $data
*/
public function __construct(
Context $context,
PaymentConfig $paymentConfig,
array $data = []
) {
$this->paymentConfig = $paymentConfig;

parent::__construct($context, $data);
}

/**
* Render block HTML
*
* @return string
*/
protected function _toHtml()
{
if (!$this->getOptions()) {
$options = [
['value' => HelperData::RULE_PAYMENT_METHOD_ALL, 'label' => __('Any')]
];

$paymentMethods = $this->paymentConfig->getActiveMethods();
foreach ($paymentMethods as $code => $model) {
$options []= [
'value' => $code,
'label' => $model->getTitle() ?: $code,
];
}

$this->setOptions($options);
}

return parent::_toHtml();
}

/**
* Sets name for input element
*
* @param string $value
* @return $this
*/
public function setInputName($value)
{
return $this->setName($value);
}
}
130 changes: 130 additions & 0 deletions Block/Adminhtml/Form/Field/ProcessingRule.php
@@ -0,0 +1,130 @@
<?php

namespace Aune\AutoInvoice\Block\Adminhtml\Form\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\DataObject;

class ProcessingRule extends AbstractFieldArray
{
/**
* @var Status
*/
private $srcStatusRenderer = null;

/**
* @var Status
*/
private $dstStatusRenderer = null;

/**
* @var PaymentMethod
*/
private $paymentMethodRenderer = null;

/**
* Returns renderer for source status element
*/
protected function getSrcStatusRenderer()
{
if (!$this->srcStatusRenderer) {
$this->srcStatusRenderer = $this->getLayout()->createBlock(
Status::class,
'',
['data' => ['is_render_to_js_template' => true]]
);
}

return $this->srcStatusRenderer;
}

/**
* Returns renderer for destination status element
*/
protected function getDstStatusRenderer()
{
if (!$this->dstStatusRenderer) {
$this->dstStatusRenderer = $this->getLayout()->createBlock(
Status::class,
'',
['data' => ['is_render_to_js_template' => true]]
);
}

return $this->dstStatusRenderer;
}

/**
* Returns renderer for payment method
*/
protected function getPaymentMethodRenderer()
{
if (!$this->paymentMethodRenderer) {
$this->paymentMethodRenderer = $this->getLayout()->createBlock(
PaymentMethod::class,
'',
['data' => ['is_render_to_js_template' => true]]
);
}
return $this->paymentMethodRenderer;
}

/**
* Prepare to render
* @return void
*/
protected function _prepareToRender()
{
$this->addColumn(
'src_status',
[
'label' => __('Source Status'),
'renderer' => $this->getSrcStatusRenderer(),
]
);
$this->addColumn(
'payment_method',
[
'label' => __('Payment Method'),
'renderer' => $this->getPaymentMethodRenderer(),
]
);
$this->addColumn(
'dst_status',
[
'label' => __('Destination Status'),
'renderer' => $this->getDstStatusRenderer(),
]
);

$this->_addAfter = false;
$this->_addButtonLabel = __('Add Rule');
}

/**
* Prepare existing row data object
*
* @param DataObject $row
* @return void
*/
protected function _prepareArrayRow(DataObject $row)
{
$srcStatus = $row->getSrcStatus();
$dstStatus = $row->getDstStatus();
$paymentMethod = $row->getPaymentMethod();

$options = [];
if ($srcStatus) {
$options['option_' . $this->getSrcStatusRenderer()->calcOptionHash($srcStatus)]
= 'selected="selected"';

$options['option_' . $this->getDstStatusRenderer()->calcOptionHash($dstStatus)]
= 'selected="selected"';

$options['option_' . $this->getPaymentMethodRenderer()->calcOptionHash($paymentMethod)]
= 'selected="selected"';
}

$row->setData('option_extra_attrs', $options);
}
}
70 changes: 70 additions & 0 deletions Block/Adminhtml/Form/Field/Status.php
@@ -0,0 +1,70 @@
<?php

namespace Aune\AutoInvoice\Block\Adminhtml\Form\Field;

use Magento\Framework\View\Element\Context;
use Magento\Framework\View\Element\Html\Select;
use Magento\Sales\Model\Order\Config;

class Status extends Select
{
/**
* @var string[]
*/
private $stateStatuses = [
\Magento\Sales\Model\Order::STATE_NEW,
\Magento\Sales\Model\Order::STATE_PROCESSING,
\Magento\Sales\Model\Order::STATE_COMPLETE,
\Magento\Sales\Model\Order::STATE_CLOSED,
\Magento\Sales\Model\Order::STATE_CANCELED,
\Magento\Sales\Model\Order::STATE_HOLDED,
];

/**
* @var Config
*/
private $orderConfig;

/**
* @param Context $context
* @param Config $orderConfig
* @param array $data
*/
public function __construct(
Context $context,
Config $orderConfig,
array $data = []
) {
$this->orderConfig = $orderConfig;

parent::__construct($context, $data);
}

/**
* Render block HTML
*
* @return string
*/
protected function _toHtml()
{
if (!$this->getOptions()) {
$statuses = $this->stateStatuses
? $this->orderConfig->getStateStatuses($this->stateStatuses)
: $this->orderConfig->getStatuses();

$this->setOptions($statuses);
}
return parent::_toHtml();
}

/**
* Sets name for input element
*
* @param string $value
* @return $this
*/
public function setInputName($value)
{
return $this->setName($value);
}
}
9 changes: 5 additions & 4 deletions Console/ProcessCommand.php
Expand Up @@ -94,12 +94,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln('<fg=yellow>This is a dry run, no orders will actually be invoiced.</>');
}

$collection = $this->invoiceProcess->getOrdersToInvoice();
foreach ($collection as $order) {
$items = $this->invoiceProcess->getItemsToProcess();
foreach ($items as $item) {
try {

$order = $item->getOrder();
$message = sprintf(
'Invoicing completed order #%s',
'Invoicing order #%s',
$order->getIncrementId()
);
$output->writeln('<fg=green>' . $message . '</>');
Expand All @@ -109,7 +110,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$this->logger->info($message);
$this->invoiceProcess->invoice($order);
$this->invoiceProcess->invoice($item);

} catch (\Exception $ex) {
$output->writeln(sprintf(
Expand Down
9 changes: 5 additions & 4 deletions Cron/InvoiceProcess.php
Expand Up @@ -48,16 +48,17 @@ public function execute()
}

$this->logger->info('Starting auto invoice procedure.');
$collection = $this->invoiceProcess->getOrdersToInvoice();
$items = $this->invoiceProcess->getItemsToProcess();

foreach ($collection as $order) {
foreach ($items as $item) {
try {

$order = $item->getOrder();
$this->logger->info(sprintf(
'Invoicing completed order #%s',
'Invoicing order #%s',
$order->getIncrementId()
));
$this->invoiceProcess->invoice($order);
$this->invoiceProcess->invoice($item);

} catch (\Exception $ex) {
$this->logger->critical($ex->getMessage());
Expand Down

0 comments on commit 8bbc48f

Please sign in to comment.