-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mageplaza/develop
Develop
- Loading branch information
Showing
10 changed files
with
393 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* Mageplaza | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Mageplaza.com license that is | ||
* available through the world-wide-web at this URL: | ||
* https://www.mageplaza.com/LICENSE.txt | ||
* | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this extension to newer | ||
* version in the future. | ||
* | ||
* @category Mageplaza | ||
* @package Mageplaza_SameOrderNumber | ||
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/) | ||
* @license https://www.mageplaza.com/LICENSE.txt | ||
*/ | ||
|
||
namespace Mageplaza\SameOrderNumber\Model\System\Config\Source; | ||
|
||
use Magento\Framework\Option\ArrayInterface; | ||
|
||
class Apply implements ArrayInterface | ||
{ | ||
const SHIPMENT = "Shipment"; | ||
const INVOICE = "Invoice"; | ||
const CREDIT_MEMO = "Credit Memo"; | ||
|
||
/** | ||
* Return array of options as value-label pairs | ||
* Create option array for module configuration | ||
* @return array Format: array(array('value' => '<value>', 'label' => '<label>'), ...) | ||
*/ | ||
public function toOptionArray() | ||
{ | ||
$applyOptions = [ | ||
['label' => __('Shipment'), 'value' => self::SHIPMENT], | ||
['label' => __('Invoice'), 'value' => self::INVOICE], | ||
['label' => __('Credit Memo'), 'value' => self::CREDIT_MEMO] | ||
]; | ||
|
||
return $applyOptions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/** | ||
* Mageplaza | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Mageplaza.com license that is | ||
* available through the world-wide-web at this URL: | ||
* https://www.mageplaza.com/LICENSE.txt | ||
* | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this extension to newer | ||
* version in the future. | ||
* | ||
* @category Mageplaza | ||
* @package Mageplaza_SameOrderNumber | ||
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/) | ||
* @license https://www.mageplaza.com/LICENSE.txt | ||
*/ | ||
|
||
namespace Mageplaza\SameOrderNumber\Observer; | ||
|
||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\Framework\Registry; | ||
use Mageplaza\SameOrderNumber\Helper\Data as HelperData; | ||
|
||
class InvoiceSaveBefore implements ObserverInterface | ||
{ | ||
/** | ||
* @var \Magento\Framework\Registry | ||
*/ | ||
protected $_registry; | ||
|
||
/** | ||
* @var HelperData | ||
*/ | ||
protected $_helperData; | ||
|
||
/** | ||
* InvoiceSaveBefore constructor. | ||
* | ||
* @param Registry $registry | ||
* @param HelperData $helperData | ||
*/ | ||
public function __construct( | ||
Registry $registry, | ||
HelperData $helperData) | ||
{ | ||
$this->_registry = $registry; | ||
$this->_helperData = $helperData; | ||
} | ||
|
||
/** | ||
* @param Observer $observer | ||
* | ||
* @return InvoiceSaveBefore | ||
*/ | ||
public function execute(Observer $observer) | ||
{ | ||
/** | ||
* @var \Magento\Sales\Model\Order\Invoice $invoice | ||
*/ | ||
$invoice = $observer->getData('invoice'); | ||
if (!$invoice) { | ||
return $this; | ||
} | ||
if ($invoice->isObjectNew() && $this->_helperData->isApplyInvoice($invoice->getStore()->getId())) { | ||
$this->_registry->register('son_new_invoice', $invoice); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
<?php | ||
/** | ||
* Mageplaza | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Mageplaza.com license that is | ||
* available through the world-wide-web at this URL: | ||
* https://www.mageplaza.com/LICENSE.txt | ||
* | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this extension to newer | ||
* version in the future. | ||
* | ||
* @category Mageplaza | ||
* @package Mageplaza_SameOrderNumber | ||
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/) | ||
* @license https://www.mageplaza.com/LICENSE.txt | ||
*/ | ||
|
||
namespace Mageplaza\SameOrderNumber\Plugin; | ||
|
||
use Magento\Framework\App\Request\Http; | ||
use Magento\Framework\Registry; | ||
use Magento\Sales\Model\Order; | ||
use Magento\Sales\Model\Order\Invoice; | ||
use Magento\SalesSequence\Model\Sequence; | ||
use Mageplaza\SameOrderNumber\Helper\Data as HelperData; | ||
use Mageplaza\SameOrderNumber\Model\System\Config\Source\Apply; | ||
|
||
class SameOrderNumber | ||
{ | ||
/** | ||
* @var Http | ||
*/ | ||
protected $_request; | ||
|
||
/** | ||
* @var Order | ||
*/ | ||
protected $_order; | ||
|
||
/** | ||
* @var HelperData | ||
*/ | ||
protected $_helperData; | ||
|
||
/** | ||
* @var \Magento\Framework\Registry | ||
*/ | ||
protected $_registry; | ||
|
||
/** | ||
* SameOrderNumber constructor. | ||
* | ||
* @param Http $request | ||
* @param Order $order | ||
* @param HelperData $helperData | ||
* @param Registry $registry | ||
*/ | ||
public function __construct( | ||
Http $request, | ||
Order $order, | ||
HelperData $helperData, | ||
Registry $registry) | ||
{ | ||
$this->_request = $request; | ||
$this->_order = $order; | ||
$this->_helperData = $helperData; | ||
$this->_registry = $registry; | ||
} | ||
|
||
/** | ||
* Get the current order | ||
* @return Order | ||
*/ | ||
public function getOrder() | ||
{ | ||
$orderId = $this->_request->getParam('order_id'); | ||
/** @var \Magento\Sales\Model\Order $order */ | ||
$order = $this->_order->load($orderId); | ||
|
||
return $order; | ||
} | ||
|
||
/** | ||
* @param $collection | ||
* | ||
* @return string | ||
*/ | ||
public function getNextId($collection) | ||
{ | ||
$currentIncrementId = $this->getOrder()->getIncrementId(); | ||
$newIncrementId = $currentIncrementId; | ||
if (count($collection) > 0) { | ||
$totalIds = count($collection); | ||
$newIncrementId = $currentIncrementId . "-" . $totalIds; | ||
} | ||
|
||
return $newIncrementId; | ||
} | ||
|
||
/** | ||
* Process next counter | ||
* | ||
* @param $defaultIncrementId | ||
* @param $type | ||
* @param Invoice|null $invoice | ||
* | ||
* @return string | ||
*/ | ||
public function processIncrementId($defaultIncrementId, $type, Invoice $invoice = null) | ||
{ | ||
if ($type != null) { | ||
switch ($type) { | ||
case Apply::INVOICE: | ||
if ($invoice != null) { | ||
return $invoice->getOrder()->getIncrementId(); | ||
} | ||
|
||
$invoiceCollectionIds = $this->getOrder()->getInvoiceCollection()->getAllIds(); | ||
|
||
return $this->getNextId($invoiceCollectionIds); | ||
|
||
case Apply::SHIPMENT: | ||
$shipmentCollectionIds = $this->getOrder()->getShipmentsCollection()->getAllIds(); | ||
|
||
return $this->getNextId($shipmentCollectionIds); | ||
|
||
case Apply::CREDIT_MEMO: | ||
$creditMemoCollectionIds = $this->getOrder()->getCreditmemosCollection()->getAllIds(); | ||
|
||
return $this->getNextId($creditMemoCollectionIds); | ||
|
||
} | ||
} | ||
|
||
return $defaultIncrementId; | ||
} | ||
|
||
/** | ||
* @param Sequence $subject | ||
* @param \Closure $proceed | ||
* | ||
* @return mixed|string | ||
*/ | ||
public function aroundGetCurrentValue(Sequence $subject, \Closure $proceed) | ||
{ | ||
$defaultIncrementId = $proceed(); | ||
$type = null; | ||
$storeId = $this->getOrder()->getStore()->getId(); | ||
if ($this->_helperData->isAdmin() && $this->_helperData->isEnabled($storeId)) { | ||
if ($this->_request->getPost('invoice') && $this->_helperData->isApplyInvoice($storeId)) { | ||
$type = Apply::INVOICE; | ||
} | ||
if ($this->_request->getPost('shipment') && $this->_helperData->isApplyShipment($storeId)) { | ||
$type = Apply::SHIPMENT; | ||
} | ||
if ($this->_request->getPost('creditmemo') && $this->_helperData->isApplyCreditMemo($storeId)) { | ||
$type = Apply::CREDIT_MEMO; | ||
} | ||
|
||
return $this->processIncrementId($defaultIncrementId, $type); | ||
} | ||
/** | ||
* @var \Magento\Sales\Model\Order\Invoice $invoice | ||
*/ | ||
if ($invoice = $this->_registry->registry('son_new_invoice')) { | ||
return $this->processIncrementId($defaultIncrementId, Apply::INVOICE, $invoice); | ||
} | ||
|
||
return $defaultIncrementId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.