Skip to content

Commit

Permalink
Merge pull request #19 from magmodules/1.4.0
Browse files Browse the repository at this point in the history
1.4.0
  • Loading branch information
Marvin-Magmodules authored May 30, 2018
2 parents d07ed17 + a33100b commit d3f8508
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 38 deletions.
18 changes: 18 additions & 0 deletions app/code/community/Magmodules/Channableapi/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Magmodules_Channableapi_Helper_Data extends Mage_Core_Helper_Abstract
const XPATH_ITEM_RESULT = 'channable_api/item/result';
const XPATH_WHITELISTED = 'channable_api/general/whitelisted_ips';
const XPATH_CRON_FREQUENCY = 'channable_api/crons/frequency';
const XPATH_USE_CUSTOM_STATUS = 'channable_api/order/use_custom_status';
const XPATH_CUSTOM_STATUS = 'channable_api/order/custom_status';

/**
* @return mixed
Expand Down Expand Up @@ -536,6 +538,22 @@ public function getBackendUrl($path, $params = array())
return $helper->getUrl($path, $params);
}

/**
* @param null $storeId
*
* @return mixed
*/
public function getProcessingStatus($storeId = null)
{
$useCustomStatus = Mage::getStoreConfig(self::XPATH_USE_CUSTOM_STATUS, $storeId);
if (!$useCustomStatus) {
return null;
}

return Mage::getStoreConfig(self::XPATH_CUSTOM_STATUS, $storeId);
}


/**
* @param $type
* @param $msg
Expand Down
23 changes: 16 additions & 7 deletions app/code/community/Magmodules/Channableapi/Model/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function invalidateProduct($productId, $type, $reason = null)

$log = Mage::getStoreConfig('channable_api/debug/log');
foreach ($items->load() as $item) {
$item->setNeedsUpdate('1')->setUpdatedAt(now())->save();
$item->setNeedsUpdate('1')->setUpdatedAt(now())->setStatus('In Que')->setResult('')->save();
if ($log) {
$message = 'Scheduled for item update';
if ($reason) {
Expand Down Expand Up @@ -366,7 +366,7 @@ public function postData($postData, $config)
'Item Update',
'API Call',
$productsIds,
json_encode($postData),
json_encode($post),
'',
$results['status']
);
Expand Down Expand Up @@ -416,8 +416,14 @@ public function updateData($postResult)
}

foreach ($postData as $key => $data) {
if(!isset($data['status'])) {
$data['call_result'] = 'No result and/or error in post data';
if (!isset($data['status'])) {

if (isset($itemsResult['message'])) {
$data['call_result'] = $itemsResult['message'];
} else {
$data['call_result'] = 'Empty return data, please check webhook url or project settings.';
}

$data['status'] = 'Error';
$data['needs_update'] = 1;
$data['last_call'] = Mage::getModel('core/date')->gmtDate('Y-m-d H:i:s');
Expand Down Expand Up @@ -447,8 +453,11 @@ public function updateData($postResult)
*/
public function cleanItemStore($storeId)
{
$table = Mage::getSingleton('core/resource')->getTableName('channable_items');
$where = 'store_id = ' . $storeId . ' AND created_at < ' . strtotime("-2 days");
Mage::getSingleton('core/resource')->getConnection('core_write')->delete($table, $where);
/** @var Mage_Core_Model_Resource $resource */
$resource = Mage::getSingleton('core/resource');
$itemTable = $resource->getTableName('channable_items');

$where = "store_id = " . $storeId . " AND created_at < " . strtotime("-2 days");
$resource->getConnection('core_write')->delete($itemTable, $where);
}
}
64 changes: 37 additions & 27 deletions app/code/community/Magmodules/Channableapi/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,40 @@ public function shopitemApi()
}

try {
/** @var Magmodules_Channableapi_Model_Items $itemModel */
$itemModel = Mage::getModel('channableapi/items');
$stores = Mage::helper('channableapi')->getEnabledItemStores();
foreach ($stores as $storeId) {
$timeStart = microtime(true);
/** @var Mage_Core_Model_App_Emulation $appEmulation */
$appEmulation = Mage::getSingleton('core/app_emulation');
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
if ($result = Mage::getModel('channableapi/items')->runUpdate($storeId)) {
if ($result = $itemModel->runUpdate($storeId)) {
if ($result['status'] == 'success') {
$html = $result['status'] . ' - Updates: ' . $result['updates'] . '<br/>';
$html .= '<small>Date: ' . $result['date'] . ' - ';
$html .= 'Time: ' . number_format((microtime(true) - $timeStart), 4) . '</small>';
$html = sprintf(
'%s - Updates: %s <br/><small>Date: %s - Time %s</small>',
$result['status'],
$result['updates'],
$result['date'],
number_format((microtime(true) - $timeStart))
);

} else {
$html = $result['status'] . '<br/><small>Date: ' . $result['date'] . ' - ';
$html .= 'Time: ' . number_format((microtime(true) - $timeStart), 4) . '</small>';
$html = sprintf(
'%s<br/><small>Date: %s - Time %s</small>',
$result['status'],
$result['date'],
number_format((microtime(true) - $timeStart))
);
}

$config = new Mage_Core_Model_Config();
/** @var Mage_Core_Model_Config $config */
$config = Mage::getModel('core/config');
$config->saveConfig('channable_api/item/result', $html, 'stores', $storeId);
}

$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
}
} catch (\Exception $e) {
/** @var Magmodules_Channableapi_Model_Items $model */
$model = Mage::getModel('channableapi/items');
$model->addTolog('catalog_product_save_before', $e->getMessage(), 2);
$itemModel->addTolog('catalog_product_save_before', $e->getMessage(), 2);
}
}

Expand All @@ -72,18 +81,18 @@ public function catalog_product_save_before(Varien_Event_Observer $observer)
return $this;
}

/** @var Magmodules_Channableapi_Model_Items $model */
$model = Mage::getModel('channableapi/items');
/** @var Magmodules_Channableapi_Model_Items $itemModel */
$itemModel = Mage::getModel('channableapi/items');
$type = 'Product Edit';

try {
/** @var Mage_Catalog_Model_Product $product */
$product = $observer->getProduct();
if ($product->hasDataChanges()) {
$model->invalidateProduct($product->getId(), $type);
$itemModel->invalidateProduct($product->getId(), $type);
}
} catch (\Exception $e) {
$model->addTolog('catalog_product_save_before', $e->getMessage(), 2);
$itemModel->addTolog('catalog_product_save_before', $e->getMessage(), 2);
}

return $this;
Expand All @@ -101,17 +110,17 @@ public function cataloginventory_stock_item_save_after(Varien_Event_Observer $ob
return $this;
}

/** @var Magmodules_Channableapi_Model_Items $model */
$model = Mage::getModel('channableapi/items');
/** @var Magmodules_Channableapi_Model_Items $itemModel */
$itemModel = Mage::getModel('channableapi/items');
$type = 'Inventory Change';

try {
$item = $observer->getEvent()->getItem();
if ($item->getStockStatusChangedAuto() || ($item->getQtyCorrection() != 0)) {
$model->invalidateProduct($item->getProductId(), $type);
$itemModel->invalidateProduct($item->getProductId(), $type);
}
} catch (\Exception $e) {
$model->addTolog('cataloginventory_stock_item_save_after', $e->getMessage(), 2);
$itemModel->addTolog('cataloginventory_stock_item_save_after', $e->getMessage(), 2);
}

return $this;
Expand All @@ -129,18 +138,18 @@ public function sales_model_service_quote_submit_before(Varien_Event_Observer $o
return $this;
}

/** @var Magmodules_Channableapi_Model_Items $model */
$model = Mage::getModel('channableapi/items');
/** @var Magmodules_Channableapi_Model_Items $itemModel */
$itemModel = Mage::getModel('channableapi/items');

try {
$type = 'Sales Order';
$reason = 'Item Ordered';
$quote = $observer->getEvent()->getQuote();
foreach ($quote->getAllItems() as $item) {
$model->invalidateProduct($item->getProductId(), $type, $reason);
$itemModel->invalidateProduct($item->getProductId(), $type, $reason);
}
} catch (\Exception $e) {
$model->addTolog('sales_model_service_quote_submit_before', $e->getMessage(), 2);
$itemModel->addTolog('sales_model_service_quote_submit_before', $e->getMessage(), 2);
}

return $this;
Expand All @@ -156,6 +165,9 @@ public function cleanItems()
return $this;
}

/** @var Magmodules_Channableapi_Model_Items $itemModel */
$itemModel = Mage::getModel('channableapi/items');

try {
$stores = Mage::helper('channableapi')->getEnabledItemStores();
if ($stores) {
Expand All @@ -172,9 +184,7 @@ public function cleanItems()
Mage::getSingleton('core/resource')->getConnection('core_read')->delete($debug, $where);
}
} catch (Exception $e) {
/** @var Magmodules_Channableapi_Model_Items $model */
$model = Mage::getModel('channableapi/items');
$model->addTolog('cleanItems', $e->getMessage(), 2);
$itemModel->addTolog('cleanItems', $e->getMessage(), 2);
}
}

Expand Down
47 changes: 47 additions & 0 deletions app/code/community/Magmodules/Channableapi/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public function importOrder($order, $storeId)

Mage::getModel('core/resource_transaction')->addObject($invoice)->addObject($invoice->getOrder())->save();
$_order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true);

if ($status = Mage::helper('channableapi')->getProcessingStatus($storeId)) {
$_order->setStatus($status);
}

$_order->save();
} catch (Exception $e) {
$this->addToLog('importOrder', $e->getMessage(), 2);
Expand Down Expand Up @@ -662,4 +667,46 @@ public function getTracking($order)
return false;
}
}

/**
* @param $timespan
*
* @return array
*/
public function getShipments($timespan)
{
$response = array();
$expression = sprintf('- %s hours', $timespan);
$gmtDate = Mage::getModel('core/date')->gmtDate('Y-m-d H:i:s');
$date = date('Y-m-d H:i:s', strtotime($expression, strtotime($gmtDate)));

$shipments = Mage::getResourceModel('sales/order_shipment_collection')
->addFieldToFilter('main_table.created_at', array('from' => $date))
->join(
array('order' => 'sales/order'),
'main_table.order_id=order.entity_id',
array(
'order_increment_id' => 'order.increment_id',
'channable_id' => 'order.channable_id',
'status' => 'order.status'
)
)
->addFieldToFilter('channable_id', array('gt' => 0));

foreach ($shipments as $shipment) {
$data['id'] = $shipment->getOrderIncrementId();
$data['status'] = $shipment->getStatus();
$data['date'] = Mage::getModel('core/date')->date('Y-m-d H:i:s', $shipment->getCreatedAt());
foreach ($shipment->getAllTracks() as $tracknum) {
$data['fulfillment']['tracking_code'][] = $tracknum->getNumber();
$data['fulfillment']['title'][] = $tracknum->getTitle();
$data['fulfillment']['carrier_code'][] = $tracknum->getCarrierCode();
}

$response[] = $data;
unset($data);
}

return $response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,37 @@ public function getAction()
}
}

/**
* Shipments Action
*/
public function shipmentsAction()
{
$enabled = $this->helper->getEnabled();
$token = $this->helper->getToken();
$code = $this->getRequest()->getParam('code');

if ($enabled && $token && $code) {
if ($code == $token) {
$timespan = intval($this->getRequest()->getParam('timespan'));
if ($timespan >= 1 && $timespan <= 336) {
$response = $this->orderModel->getShipments($timespan);
} else {
$response = $this->helper->jsonResponse('Invalid timespan, supported range: 1-336');
}
} else {
$response = $this->helper->jsonResponse('Unknown Token');
}
} else {
$response = $this->helper->jsonResponse('Extension not enabled!');
}

$this->getResponse()
->clearHeaders()
->setHeader('Content-type', 'application/json', true)
->setHeader('Cache-control', 'no-cache', true)
->setBody(json_encode($response));
}

/**
* Webhook Action
*/
Expand Down
2 changes: 1 addition & 1 deletion app/code/community/Magmodules/Channableapi/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<config>
<modules>
<Magmodules_Channableapi>
<version>1.3.1</version>
<version>1.4.0</version>
</Magmodules_Channableapi>
</modules>
<global>
Expand Down
31 changes: 29 additions & 2 deletions app/code/community/Magmodules/Channableapi/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,38 @@
<show_in_store>1</show_in_store>
<comment><![CDATA[Enable if you want to automatically create an invoice on the order import and set the order state to <b>Processing</b> (default invoice order state) instead of <b>Pending</b>.]]></comment>
</invoice_order>
<use_custom_status translate="label">
<label>Use non-default order status</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>44</sort_order>
<show_in_default>0</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>1</show_in_store>
<comment><![CDATA[Choose if you want to update the status of the order with a non-default order status.]]></comment>
<depends>
<invoice_order>1</invoice_order>
</depends>
</use_custom_status>
<custom_status translate="label">
<label>Status Processing</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_order_status_processing</source_model>
<sort_order>45</sort_order>
<show_in_default>0</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>1</show_in_store>
<comment><![CDATA[Choose if you want to update the status of the order with a non-default order status.]]></comment>
<depends>
<invoice_order>1</invoice_order>
<use_custom_status>1</use_custom_status>
</depends>
</custom_status>
<channel_orderid translate="label">
<label>Use channel Order ID</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>44</sort_order>
<sort_order>46</sort_order>
<show_in_default>0</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>1</show_in_store>
Expand All @@ -207,7 +234,7 @@
<orderid_prefix translate="label">
<label>Order ID Prefix</label>
<frontend_type>text</frontend_type>
<sort_order>45</sort_order>
<sort_order>37</sort_order>
<show_in_default>0</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>1</show_in_store>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "magmodules/magento1-channable-api",
"type": "magento-module",
"description": "Magento 1 Channable integration",
"version": "v1.3.1",
"version": "v1.4.0",
"keywords": [
"magento"
],
Expand Down

0 comments on commit d3f8508

Please sign in to comment.