Skip to content

Commit

Permalink
0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrii-fediuk committed Sep 13, 2019
1 parent eb022a3 commit 8166b68
Show file tree
Hide file tree
Showing 23 changed files with 1,144 additions and 2 deletions.
84 changes: 84 additions & 0 deletions Console/Command/ExportCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
namespace Julio\Order\Console\Command;
use Julio\Order\Helper\GeneralHelper;
use Julio\Order\Model\ProcessObserverInterface;
use Julio\Order\Model\QueueProcessor;
use Magento\Framework\App\State;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ExportCommand extends Command implements ProcessObserverInterface {
/**
* @var State
*/
private $appState;

/**
* @var QueueProcessor
*/
private $queueProcessor;

/**
* @var GeneralHelper
*/
private $generalHelper;

/**
* @var \Symfony\Component\Console\Output\OutputInterface
*/
private $output;

/**
* ExportCommand constructor.
* @param State $appState
* @param QueueProcessor $queueProcessor
* @param GeneralHelper $generalHelper
* @param null $name
*/
public function __construct(
State $appState,
QueueProcessor $queueProcessor,
GeneralHelper $generalHelper,
$name = null
) {
parent::__construct($name);
$this->appState = $appState;
$this->queueProcessor = $queueProcessor;
$this->generalHelper = $generalHelper;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('sales:order:export')
->setDescription('Sales Order Export');
}

/**
* Execute
* @param InputInterface $input
* @param OutputInterface $output
* @return null
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
$this->queueProcessor
->setTimeInterval(QueueProcessor::LONG_TIME_INTERVAL)
->process($this);
}

/**
* Notify
* @param string $message
* @return mixed
*/
public function notify(string $message)
{
$this->output->writeln($message);
}
}
51 changes: 51 additions & 0 deletions Cron/ProcessQueue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace Julio\Order\Cron;
use Julio\Order\Helper\GeneralHelper;
use Julio\Order\Model\ProcessObserverInterface;
use Julio\Order\Model\QueueProcessor;
class ProcessQueue implements ProcessObserverInterface {
/**
* @var QueueProcessor
*/
private $queueProcessor;

/**
* @var GeneralHelper
*/
private $generalHelper;

/**
* ProcessQueue constructor.
* @param QueueProcessor $queueProcessor
* @param GeneralHelper $generalHelper
*/
public function __construct(QueueProcessor $queueProcessor, GeneralHelper $generalHelper)
{
$this->queueProcessor = $queueProcessor;
$this->generalHelper = $generalHelper;
}

/**
* Execute
*/
public function execute()
{
if (!$this->generalHelper->isQueueActive()) {
return false;
}

$this->queueProcessor
->setTimeInterval(QueueProcessor::DEFAULT_TIME_INTERVAL)
->process($this);
}

/**
* Notify
* @param string $message
* @return mixed
*/
public function notify(string $message)
{
// do nothing;
}
}
189 changes: 189 additions & 0 deletions Helper/GeneralHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php
namespace Julio\Order\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Sales\Model\Order\Item;
use Magento\Store\Model\StoreManagerInterface;
class GeneralHelper extends AbstractHelper {
const PATH = 'sales/orderexport/%s';

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var EncryptorInterface
*/
private $encryptor;

/**
* GeneralHelper constructor.
* @param Context $context
* @param StoreManagerInterface $storeManager
*/
public function __construct(Context $context, StoreManagerInterface $storeManager, EncryptorInterface $encryptor)
{
parent::__construct($context);
$this->storeManager = $storeManager;
$this->encryptor = $encryptor;
}

/**
* Check if queue is active
* @return bool
*/
public function isQueueActive()
{
return $this->scopeConfig->isSetFlag(sprintf(self::PATH, 'queue_active'));
}

/**
* Decrypted config value
* @param $key
* @return string
*/
public function getConfigDecrypted($key)
{
$value = $this->scopeConfig->getValue(sprintf(self::PATH, $key));
if (empty($value)) {
return '';
}

return $this->encryptor->decrypt($value);
}

/**
* Check if grid active
* @return boolean
*/
public function isAsyncGridActive()
{
return $this->scopeConfig->isSetFlag('dev/grid/async_indexing');
}

/**
* Upload directory path
* @return string
*/
public function getUploadDirPath()
{
$path = $this->scopeConfig->getValue(sprintf(self::PATH, 'dropbox_dir_path'));
$path = trim($path);
$path = trim($path, '/');

if (empty($path)) {
return '/';
}

return '/' . $path . '/';
}

/**
* Xml representation of order
* @param \Magento\Sales\Model\Order $order
*/
public function convertToXml(\Magento\Sales\Model\Order $order)
{
$xml = new \DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$orderEl = $xml->createElement('order');
$orderEl->setAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
$xml->appendChild($orderEl);

$orderDataEl = $xml->createElement('order_data');
$orderEl->appendChild($orderDataEl);
$this->addXmlChild($xml, $orderDataEl, 'order_date', $order->getCreatedAt());
$this->addXmlChild($xml, $orderDataEl, 'item_count', count($order->getAllVisibleItems()));
$this->addXmlChild($xml, $orderDataEl, 'total_item_amount', $order->getBaseSubtotal());
$this->addXmlChild($xml, $orderDataEl, 'channel', $this->storeManager->getStore($order->getStoreId())->getName());
$this->addXmlChild($xml, $orderDataEl, 'payment_method', $order->getPayment()->getMethod());
$this->addXmlChild($xml, $orderDataEl, 'seller_shipping_cost', '0');
$this->addXmlChild($xml, $orderDataEl, 'reference', $order->getIncrementId());
$this->addXmlChild($xml, $orderDataEl, 'client_id', $order->getCustomerId() ?: '');

$billing = $order->getBillingAddress();
$billingAddressEl = $xml->createElement('invoice_address');
$orderEl->appendChild($billingAddressEl);
$this->addXmlChild($xml, $billingAddressEl, 'address_id', $billing->getId());
$this->addXmlCDataChild($xml, $billingAddressEl, 'firstname', $billing->getFirstname());
$this->addXmlCDataChild($xml, $billingAddressEl, 'lastname', $billing->getLastname());
$this->addXmlChild($xml, $billingAddressEl, 'neighbourhood', '');
$this->addXmlCDataChild($xml, $billingAddressEl, 'street', $billing->getStreetLine(1));
$this->addXmlCDataChild($xml, $billingAddressEl, 'street_no', $billing->getStreetLine(2));
$this->addXmlChild($xml, $billingAddressEl, 'zip', $billing->getPostcode());
$this->addXmlCDataChild($xml, $billingAddressEl, 'city', $billing->getCity());
$this->addXmlChild($xml, $billingAddressEl, 'country', $billing->getCountryId());
$this->addXmlChild($xml, $billingAddressEl, 'email', $order->getCustomerEmail());
$this->addXmlChild($xml, $billingAddressEl, 'phone', $billing->getTelephone());
$this->addXmlChild($xml, $billingAddressEl, 'rfc', '');

$shipping = $order->getShippingAddress();
$shippingAddressEl = $xml->createElement('shipping_address');
$orderEl->appendChild($shippingAddressEl);
$this->addXmlChild($xml, $shippingAddressEl, 'address_id', $shipping->getId());
$this->addXmlCDataChild($xml, $shippingAddressEl, 'firstname', $shipping->getFirstname());
$this->addXmlCDataChild($xml, $shippingAddressEl, 'lastname', $shipping->getLastname());
$this->addXmlChild($xml, $shippingAddressEl, 'neighbourhood', '');
$this->addXmlCDataChild($xml, $shippingAddressEl, 'street', $shipping->getStreetLine(1));
$this->addXmlCDataChild($xml, $shippingAddressEl, 'street_no', $shipping->getStreetLine(2));
$this->addXmlChild($xml, $shippingAddressEl, 'zip', $shipping->getPostcode());
$this->addXmlCDataChild($xml, $shippingAddressEl, 'city', $shipping->getCity());
$this->addXmlChild($xml, $shippingAddressEl, 'country', $shipping->getCountryId());
$this->addXmlChild($xml, $shippingAddressEl, 'phone', $shipping->getTelephone());

$itemsEl = $xml->createElement('items');
$xml->appendChild($itemsEl);

foreach ($order->getAllVisibleItems() as $item) {
/** @var Item $item */
$itemEl = $xml->createElement('item');
$itemsEl->appendChild($itemEl);
$this->addXmlChild($xml, $itemEl, 'item_id', $item->getId());
$this->addXmlChild($xml, $itemEl, 'quantity', $item->getQtyOrdered());
$this->addXmlCDataChild($xml, $itemEl, 'label', $item->getName());
$this->addXmlChild($xml, $itemEl, 'item_price', $item->getBasePrice());
$this->addXmlChild($xml, $itemEl, 'carrier', '');
$this->addXmlChild($xml, $itemEl, 'tracking_code', '');
$this->addXmlChild($xml, $itemEl, 'status', $order->getStatus());
}

return $xml;
}

/**
* Append new child with basic value
* @param \DOMDocument $doc
* @param \DOMElement $parentEl
* @param $name
* @param null $value
* @return \DOMElement
*/
protected function addXmlChild(\DOMDocument $doc, \DOMElement $parentEl, $name, $value = null)
{
$el = $doc->createElement($name, $value);
$parentEl->appendChild($el);

return $el;
}

/**
* Append new CData section
* @param \DOMDocument $doc
* @param \DOMElement $parentEl
* @param $name
* @param null $value
* @return \DOMElement
*/
protected function addXmlCDataChild(\DOMDocument $doc, \DOMElement $parentEl, $name, $value = null)
{
$el = $doc->createElement($name);
$cdata = $doc->createCDATASection($value);
$el->appendChild($cdata);
$parentEl->appendChild($el);

return $el;
}
}
8 changes: 8 additions & 0 deletions Model/Api/ApiInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Julio\Order\Model\Api;
interface ApiInterface {
/**
* @return mixed
*/
public function push(\Magento\Sales\Model\Order $order, \DOMDocument $xml);
}
Loading

0 comments on commit 8166b68

Please sign in to comment.