Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
haitv282 committed May 23, 2019
1 parent c406450 commit 749bef5
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 119 deletions.
10 changes: 8 additions & 2 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ class Data extends AbstractData
{
const CONFIG_MODULE_PATH = 'mpsameordernumber';

protected $applyForOption;

/**
* @param null $storeId
* @return array
*/
public function getApplyForOption($storeId = null)
{
return explode(",", $this->getConfigGeneral('apply', $storeId));
if (!$this->applyForOption) {
$this->applyForOption = explode(',', $this->getConfigGeneral('apply', $storeId));
}

return $this->applyForOption;
}

/**
Expand Down Expand Up @@ -67,4 +73,4 @@ public function isApplyCreditMemo($storeId = null)
{
return in_array(Apply::CREDIT_MEMO, $this->getApplyForOption($storeId));
}
}
}
6 changes: 3 additions & 3 deletions Model/System/Config/Source/Apply.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
*/
class Apply implements ArrayInterface
{
const SHIPMENT = "shipment";
const INVOICE = "invoice";
const CREDIT_MEMO = "creditmemo";
const SHIPMENT = 'shipment';
const INVOICE = 'invoice';
const CREDIT_MEMO = 'creditmemo';

/**
* Return array of options as value-label pairs
Expand Down
29 changes: 11 additions & 18 deletions Observer/InvoiceSaveBefore.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Registry;
use Magento\Sales\Model\Order\Invoice;
use Mageplaza\SameOrderNumber\Helper\Data as HelperData;

/**
Expand All @@ -33,7 +34,7 @@
class InvoiceSaveBefore implements ObserverInterface
{
/**
* @var \Magento\Framework\Registry
* @var Registry
*/
protected $_registry;

Expand All @@ -50,9 +51,9 @@ class InvoiceSaveBefore implements ObserverInterface
*/
public function __construct(
Registry $registry,
HelperData $helperData)
{
$this->_registry = $registry;
HelperData $helperData
) {
$this->_registry = $registry;
$this->_helperData = $helperData;
}

Expand All @@ -63,25 +64,17 @@ public function __construct(
*/
public function execute(Observer $observer)
{
/**
* @var \Magento\Sales\Model\Order\Invoice $invoice
*/
/** @var Invoice $invoice */
$invoice = $observer->getData('invoice');
if (!$invoice) {
return $this;
}

if($this->_helperData->isAdmin()) {
if (!$invoice || $this->_helperData->isAdmin() || !$invoice->isObjectNew()) {
return $this;
}

if($invoice->isObjectNew()) {
$storeId = $invoice->getStore()->getId();
if ($this->_helperData->isEnabled($storeId) && $this->_helperData->isApplyInvoice($storeId)) {
$this->_registry->register('son_new_invoice', $invoice);
}
$storeId = $invoice->getStore()->getId();
if ($this->_helperData->isEnabled($storeId) && $this->_helperData->isApplyInvoice($storeId)) {
$this->_registry->register('son_new_invoice', $invoice);
}

return $this;
}
}
}
Loading

0 comments on commit 749bef5

Please sign in to comment.