Skip to content

Commit

Permalink
Merge pull request #1 from mageplaza/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
imsamthomas authored Dec 3, 2018
2 parents 296e30d + 34730cb commit 824d37a
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 5 deletions.
24 changes: 23 additions & 1 deletion Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,34 @@
namespace Mageplaza\SameOrderNumber\Helper;

use Mageplaza\Core\Helper\AbstractData;
use Mageplaza\SameOrderNumber\Model\System\Config\Source\Apply;

/**
* Class Data
* @package Mageplaza\SameOrderNumber\Helper
*/
class Data extends AbstractData
{
const CONFIG_MODULE_PATH = 'sameordernumber';
const CONFIG_MODULE_PATH = 'mpsameordernumber';

public function getApplyForOption($storeId = null)
{
return explode(",", $this->getConfigGeneral('apply', $storeId));
}

public function isApplyInvoice($storeId = null)
{
return in_array(Apply::INVOICE, $this->getApplyForOption($storeId));
}

public function isApplyShipment($storeId = null)
{
return in_array(Apply::SHIPMENT, $this->getApplyForOption($storeId));
}

public function isApplyCreditMemo($storeId = null)
{
return in_array(Apply::CREDIT_MEMO, $this->getApplyForOption($storeId));
}

}
47 changes: 47 additions & 0 deletions Model/System/Config/Source/Apply.php
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;
}
}
75 changes: 75 additions & 0 deletions Observer/InvoiceSaveBefore.php
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;
}
}
175 changes: 175 additions & 0 deletions Plugin/SameOrderNumber.php
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;
}
}
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "mageplaza/module-same-order-number",
"description": "Magento 2 Same Order Number Extension",
"require": {
"mageplaza/module-core": "^1.4.0"
},
"type": "magento2-module",
"version": "1.0.0",
"license": "proprietary",
Expand Down
10 changes: 9 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@
<tab>mageplaza</tab>
<resource>Mageplaza_SameOrderNumber::configuration</resource>
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General</label>
<label>General Configuration</label>
<field id="enabled" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[Select <b>Yes</b> to enable this module.]]></comment>
</field>
<field id="apply" translate="label comment" type="multiselect" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Apply for</label>
<source_model>\Mageplaza\SameOrderNumber\Model\System\Config\Source\Apply</source_model>
<comment>If increment id exist, a sufix number will be added. Ex: 100001-1, 100001-2</comment>
<depends>
<field id="enabled">1</field>
</depends>
</field>
</group>
</section>
</system>
Expand Down
4 changes: 2 additions & 2 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<sameordernumber>
<mpsameordernumber>
<general>
<enabled>1</enabled>
</general>
</sameordernumber>
</mpsameordernumber>
</default>
</config>
Loading

0 comments on commit 824d37a

Please sign in to comment.