Skip to content

Commit

Permalink
Add ship and cancel call actions for marketplace
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminlgw committed Oct 27, 2017
1 parent b6603ce commit 302e7fb
Show file tree
Hide file tree
Showing 16 changed files with 983 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Helper/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function getLastImport()
*
* @param string $name marketplace name
*
* @return array Lengow marketplace
* @return \Lengow\Connector\Model\Import\Marketplace
*/
public function getMarketplaceSingleton($name)
{
Expand Down
2 changes: 1 addition & 1 deletion Model/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
use Magento\Framework\Json\Helper\Data as JsonHelper;
use Magento\Store\Model\WebsiteFactory;
use Magento\Backend\Model\Session as BackendSession;
use Magento\Store\Api\StoreRepositoryInterface;
use Lengow\Connector\Helper\Data as DataHelper;
use Lengow\Connector\Helper\Config as ConfigHelper;
use Lengow\Connector\Helper\Import as ImportHelper;
use Lengow\Connector\Helper\Sync as SyncHelper;
use Lengow\Connector\Model\Import\Ordererror;
use Magento\Store\Api\StoreRepositoryInterface;
use Lengow\Connector\Model\Exception as LengowException;
use Lengow\Connector\Model\Import\Importorder as Importorder;

Expand Down
180 changes: 180 additions & 0 deletions Model/Import/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php
/**
* Copyright 2017 Lengow SAS
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category Lengow
* @package Lengow_Connector
* @subpackage Model
* @author Team module <[email protected]>
* @copyright 2017 Lengow SAS
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

namespace Lengow\Connector\Model\Import;

use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Lengow\Connector\Model\ResourceModel\Action as ResourceAction;
use Lengow\Connector\Model\ResourceModel\Action\CollectionFactory as ActionCollectionFactory;

/**
* Model import action
*/
class Action extends AbstractModel
{
/**
* @var integer action state for new action
*/
const STATE_NEW = 0;

/**
* @var integer action state for action finished
*/
const STATE_FINISH = 1;

/**
* @var \Magento\Framework\Stdlib\DateTime\DateTime Magento datetime instance
*/
protected $_dateTime;

/**
* @var \Lengow\Connector\Model\ResourceModel\Action\CollectionFactory Lengow action collection factory
*/
protected $_actionCollection;


/**
* @var array $_fieldList field list for the table lengow_order_line
* required => Required fields when creating registration
* update => Fields allowed when updating registration
*/
protected $_fieldList = [
'order_id' => ['required' => true, 'updated' => false],
'action_id' => ['required' => true, 'updated' => false],
'order_line_sku' => ['required' => false, 'updated' => false],
'action_type' => ['required' => true, 'updated' => false],
'retry' => ['required' => false, 'updated' => true],
'parameters' => ['required' => true, 'updated' => false],
'state' => ['required' => false, 'updated' => true]
];

/**
* Constructor
*
* @param \Magento\Framework\Model\Context $context Magento context instance
* @param \Magento\Framework\Registry $registry Magento registry instance
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime Magento datetime instance
* @param \Lengow\Connector\Model\ResourceModel\Action\CollectionFactory $actionCollection
*/
public function __construct(
Context $context,
Registry $registry,
DateTime $dateTime,
ActionCollectionFactory $actionCollection
) {
parent::__construct($context, $registry);
$this->_dateTime = $dateTime;
$this->_actionCollection = $actionCollection;
}

/**
* Initialize ordererror model
**
* @return void
*/
protected function _construct()
{
$this->_init(ResourceAction::class);
}

/**
* Create Lengow action
*
* @param array $params action parameters
*
* @return \Lengow\Connector\Model\Import\Action|false
*/
public function createAction($params = [])
{
foreach ($this->_fieldList as $key => $value) {
if (!array_key_exists($key, $params) && $value['required']) {
return false;
}
}
foreach ($params as $key => $value) {
$this->setData($key, $value);
}
$this->setData('state', self::STATE_NEW);
$this->setData('created_at', $this->_dateTime->gmtDate('Y-m-d H:i:s'));
return $this->save();
}

/**
* Update Lengow action
*
* @param array $params action parameters
*
* @return \Lengow\Connector\Model\Import\Action|false
*/
public function updateAction($params = [])
{
if (!$this->getId()) {
return false;
}
if ((int)$this->getData('state') != self::STATE_NEW) {
return false;
}
$updatedFields = $this->getUpdatedFields();
foreach ($params as $key => $value) {
if (in_array($key, $updatedFields)) {
$this->setData($key, $value);
}
}
$this->setData('updated_at', $this->_dateTime->gmtDate('Y-m-d H:i:s'));
return $this->save();
}

/**
* Get updated fields
*
* @return array
*/
public function getUpdatedFields()
{
$updatedFields = [];
foreach ($this->_fieldList as $key => $value) {
if ($value['updated']) {
$updatedFields[] = $key;
}
}
return $updatedFields;
}

/**
* Get active action by API action ID
*
* @param integer $actionId action id from API
*
* @return integer|false
*/
public function getActiveActionByActionId($actionId)
{
$results = $this->_actionCollection->create()
->addFieldToFilter('action_id', $actionId)
->addFieldToFilter('state', self::STATE_NEW)
->getData();
if (count($results) > 0) {
return (int)$results[0]['id'];
}
return false;
}
}
Loading

0 comments on commit 302e7fb

Please sign in to comment.