ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace JR\NetSuite\Model\ResourceModel;
|
||||
|
||||
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
class CustomerQueue extends AbstractDb {
|
||||
/**
|
||||
* Initialize resource
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function _construct()
|
||||
{
|
||||
$this->_init('jr_netsuite_customer_queue', 'id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace JR\NetSuite\Model\ResourceModel\CustomerQueue;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use JR\NetSuite\Model\CustomerQueue;
|
||||
use JR\NetSuite\Model\ResourceModel\CustomerQueue as CustomerQueueResource;
|
||||
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(CustomerQueue::class, CustomerQueueResource::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next items in the queue based on batch size
|
||||
*
|
||||
* @param int $type_id
|
||||
* @param int $batchSize
|
||||
* @return \Magento\Framework\Data\Collection\AbstractDb
|
||||
*/
|
||||
public function getNext($batchSize = null)
|
||||
{
|
||||
$collection = $this
|
||||
->addFieldToSelect('*')
|
||||
->addFieldToFilter('status', ['eq' => 'pending'])
|
||||
->setOrder('id', 'asc');
|
||||
|
||||
if ($batchSize) {
|
||||
$collection->getSelect()->limit($batchSize);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert or update based on UNIQUE combo 'key-processing'
|
||||
* @param $key
|
||||
* @param $value
|
||||
*/
|
||||
public function insertOrUpdate($item)
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
try {
|
||||
// Start a transaction
|
||||
// $connection->beginTransaction();
|
||||
|
||||
// echo json_encode($item, JSON_PRETTY_PRINT) . "\n";
|
||||
|
||||
// update the existing item with same netsuite_customer_id
|
||||
$select = $connection->select()
|
||||
->from($this->getMainTable())
|
||||
->where('status = ?', 'pending')
|
||||
->where('netsuite_customer_id = ?', $item['netsuite_customer_id']);
|
||||
$existingItem = $connection->fetchRow($select);
|
||||
|
||||
if (!$existingItem) {
|
||||
$existingItem = [];
|
||||
} else {
|
||||
$existingItem['data'] = json_decode($existingItem['data'], true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
$data = [
|
||||
'netsuite_customer_id' => $item['netsuite_customer_id'],
|
||||
'netsuite_entity_id' => $item['netsuite_entity_id'],
|
||||
'data' => json_encode($item),
|
||||
'messages' => '',
|
||||
// 'created_at' => (new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
|
||||
'status' => 'pending',
|
||||
];
|
||||
|
||||
// echo json_encode($data, JSON_PRETTY_PRINT) . "\n";
|
||||
|
||||
// update the existing item with default shipping or billing
|
||||
if (isset($existingItem['id'])) {
|
||||
$data['id'] = $existingItem['id'];
|
||||
$item['isdefaultshipping'] = $item['isdefaultshipping'] || ($existingItem['data']['isdefaultshipping'] ?? false);
|
||||
$item['isdefaultbilling'] = $item['isdefaultbilling'] || ($existingItem['data']['isdefaultbilling'] ?? false);
|
||||
$item['shipping_address'] = $item['shipping_address'] ?? $existingItem['data']['shipping_address'] ?? [];
|
||||
$item['billing_address'] = $item['billing_address'] ?? $existingItem['data']['billing_address'] ?? [];
|
||||
$data['data'] = json_encode($item);
|
||||
}
|
||||
|
||||
// Insert new item or update existing one
|
||||
$connection->insertOnDuplicate($this->getMainTable(), $data, ['data', 'status', 'messages']);
|
||||
|
||||
// Commit the transaction
|
||||
// $connection->commit();
|
||||
} catch (\Exception $e) {
|
||||
// Rollback the transaction in case of an error
|
||||
$connection->rollBack();
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
__('Error in insertOrUpdate: %1', $e->getMessage()),
|
||||
$e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace JR\NetSuite\Model\ResourceModel;
|
||||
|
||||
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
class Location extends AbstractDb {
|
||||
/**
|
||||
* Initialize resource
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function _construct()
|
||||
{
|
||||
$this->_init('jr_netsuite_location', 'location_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert or update based on UNIQUE combo 'key-processing'
|
||||
*
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @param $type_id
|
||||
*/
|
||||
public function insertOrUpdate($item)
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
|
||||
// get the existing item with same netsuite_id
|
||||
$location = $connection->fetchRow(
|
||||
$connection->select()
|
||||
->from($this->getMainTable())
|
||||
->where('netsuite_id = ?', $item['netsuite_id'])
|
||||
);
|
||||
if (!$location) {
|
||||
$location = [];
|
||||
}
|
||||
|
||||
// $this->getConnection()->delete($this->getMainTable(), ['`netsuite_id` = ?' => $item['netsuite_id']]);
|
||||
|
||||
if (isset($location['location_id'])) {
|
||||
$item['location_id'] = $location['location_id'];
|
||||
}
|
||||
|
||||
if (strpos($item['address'], "\r\n") !== false) {
|
||||
$address = explode("\r\n", $item['address']);
|
||||
// if (trim($address[0]) == 'LTS Associate Inc.') {
|
||||
// array_shift($address);
|
||||
// }
|
||||
// if (trim($address[count($address) - 1]) == 'United States') {
|
||||
// array_pop($address);
|
||||
// }
|
||||
$item['address'] = implode(" ", $address);
|
||||
}
|
||||
|
||||
$connection->insertOnDuplicate($this->getMainTable(), $item, ['netsuite_id', 'name', 'address']);
|
||||
}
|
||||
|
||||
/**
|
||||
* save Locations
|
||||
* @param $locations
|
||||
*/
|
||||
public function saveLocations($locations)
|
||||
{
|
||||
foreach ($locations as $location) {
|
||||
$this->insertOrUpdate($location);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace JR\NetSuite\Model\ResourceModel\Location;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use JR\NetSuite\Model\Location;
|
||||
use JR\NetSuite\Model\ResourceModel\Location as LocationResource;
|
||||
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(Location::class, LocationResource::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert or update based on UNIQUE combo 'key-processing'
|
||||
* @param $key
|
||||
* @param $value
|
||||
*/
|
||||
public function insertOrUpdate($item)
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
try {
|
||||
// Start a transaction
|
||||
// $connection->beginTransaction();
|
||||
|
||||
// Insert new item or update existing one
|
||||
$connection->insertOnDuplicate($this->getMainTable(), $item, ['data', 'status']);
|
||||
|
||||
// Commit the transaction
|
||||
// $connection->commit();
|
||||
} catch (\Exception $e) {
|
||||
// Rollback the transaction in case of an error
|
||||
$connection->rollBack();
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
__('Error in insertOrUpdate: %1', $e->getMessage()),
|
||||
$e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace JR\NetSuite\Model\ResourceModel\NetSuite;
|
||||
|
||||
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
|
||||
{
|
||||
|
||||
protected $_idFieldName = 'entity_id';
|
||||
protected $_eventPrefix = 'jrnetsuite_product_queue_collection';
|
||||
protected $_eventObject = 'jrnetsuite_product_collection';
|
||||
|
||||
/**
|
||||
* Define resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(
|
||||
\JR\NetSuite\Model\Data\ProductQueueItem::class,
|
||||
\JR\NetSuite\Model\ResourceModel\ProductQueue::class
|
||||
);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace JR\NetSuite\Model\ResourceModel;
|
||||
|
||||
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
class OrderQueue extends AbstractDb {
|
||||
/**
|
||||
* Initialize resource
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function _construct()
|
||||
{
|
||||
$this->_init('jr_netsuite_order_queue', 'id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace JR\NetSuite\Model\ResourceModel\OrderQueue;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use JR\NetSuite\Model\OrderQueue;
|
||||
use JR\NetSuite\Model\ResourceModel\OrderQueue as OrderQueueResource;
|
||||
use JR\NetSuite\Helper\Data as Helper;
|
||||
|
||||
class Collection extends AbstractCollection {
|
||||
|
||||
protected function _construct() {
|
||||
$this->_init(OrderQueue::class, OrderQueueResource::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next items in the queue based on batch size
|
||||
*
|
||||
* @param int $type_id
|
||||
* @param int $batchSize
|
||||
* @return \Magento\Framework\Data\Collection\AbstractDb
|
||||
*/
|
||||
public function getNext($batchSize = null) {
|
||||
$collection = $this->addFieldToSelect('*')
|
||||
->addFieldToFilter('status', ['eq' => 'pending'])
|
||||
->setOrder('id', 'asc');
|
||||
|
||||
if ($batchSize) {
|
||||
$collection->getSelect()->limit($batchSize);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert or update based on UNIQUE combo 'key-processing'
|
||||
* @param $key
|
||||
* @param $value
|
||||
*/
|
||||
public function insertOrUpdate($item) {
|
||||
$connection = $this->getConnection();
|
||||
try {
|
||||
// Start a transaction
|
||||
// $connection->beginTransaction();
|
||||
|
||||
// echo json_encode($item, JSON_PRETTY_PRINT) . "\n";
|
||||
|
||||
// update the existing item with same netsuite_order_id
|
||||
$select = $connection->select()
|
||||
->from($this->getMainTable())
|
||||
->where('netsuite_order_id = ?', $item['netsuite_order_id']);
|
||||
$existingItem = $connection->fetchRow($select);
|
||||
|
||||
if (!$existingItem) {
|
||||
$existingItem = [];
|
||||
}
|
||||
|
||||
// if the existing item is already success and the data is the same, skip
|
||||
if (isset($existingItem['status']) && $existingItem['status'] == 'success'
|
||||
&& isset($existingItem['data']) && $existingItem['data'] == json_encode($item)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$recorded_at = \DateTime::createFromFormat('m/d/Y g:i a', $item['last_modified_date'], new \DateTimeZone('America/Los_Angeles'));
|
||||
|
||||
$recorded_at->setTimezone(new \DateTimeZone('UTC'));
|
||||
|
||||
$data = [
|
||||
'netsuite_order_id' => $item['netsuite_order_id'],
|
||||
'data' => json_encode($item),
|
||||
'messages' => '',
|
||||
'recorded_at' => $recorded_at->format('Y-m-d H:i:s'),
|
||||
// 'created_at' => (new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
|
||||
'status' => 'pending',
|
||||
];
|
||||
|
||||
// update the existing item with same netsuite_order_id
|
||||
if (isset($existingItem['id'])) {
|
||||
$data['id'] = $existingItem['id'];
|
||||
}
|
||||
|
||||
// Insert new item or update existing one
|
||||
$connection->insertOnDuplicate($this->getMainTable(), $data, ['data', 'status', 'messages', 'recorded_at']);
|
||||
|
||||
// Commit the transaction
|
||||
// $connection->commit();
|
||||
} catch (\Exception $e) {
|
||||
// Rollback the transaction in case of an error
|
||||
$connection->rollBack();
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
__('Error in insertOrUpdate: %1', $e->getMessage()),
|
||||
$e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last sync netsuite order id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLastSyncRecordDate() {
|
||||
$select = $this->getConnection()->select()
|
||||
->from($this->getMainTable(), ['recorded_at'])
|
||||
->order('recorded_at DESC')
|
||||
->limit(1);
|
||||
|
||||
return $this->getConnection()->fetchOne($select);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace JR\NetSuite\Model\ResourceModel;
|
||||
|
||||
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
class ProductQueue extends AbstractDb
|
||||
{
|
||||
/**
|
||||
* Initialize resource
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function _construct()
|
||||
{
|
||||
$this->_init('jr_netsuite_product_queue', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert or update based on UNIQUE combo 'key-processing'
|
||||
*
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @param $type_id
|
||||
*/
|
||||
public function insertOrUpdate($item)
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
|
||||
// get the existing item with same netsuite_product_id
|
||||
$productQueue = $connection->fetchRow(
|
||||
$connection->select()
|
||||
->from($this->getMainTable())
|
||||
->where('netsuite_product_id = ?', $item['netsuite_product_id'])
|
||||
->where('data like ?', '%"locationId":"' . ($item['locationId'] ?? "") . '"%')
|
||||
);
|
||||
|
||||
if (!$productQueue) {
|
||||
$productQueue = [];
|
||||
}
|
||||
|
||||
// if the existing item is already success and the data is the same, skip
|
||||
if (isset($productQueue['status']) && $productQueue['status'] == 'success'
|
||||
&& isset($productQueue['data']) && $productQueue['data'] == json_encode($item)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// $this->getConnection()->delete($this->getMainTable(), ['`netsuite_product_id` = ?' => $item['netsuite_product_id']]);
|
||||
|
||||
$data = [
|
||||
'netsuite_product_id' => $item['netsuite_product_id'],
|
||||
'sku' => $item['sku'],
|
||||
'data' => json_encode($item),
|
||||
// 'created_at' => (new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
|
||||
'status' => 'pending',
|
||||
];
|
||||
|
||||
// update the existing item with same netsuite_product_id
|
||||
if (isset($productQueue['id'])) {
|
||||
$data['id'] = $productQueue['id'];
|
||||
}
|
||||
|
||||
$connection->insertOnDuplicate($this->getMainTable(), $data, ['data', 'status']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the item in the queue as processing
|
||||
* @param $id
|
||||
* @param string $value
|
||||
* @return bool
|
||||
*/
|
||||
public function setProcessing($id, $value = 1)
|
||||
{
|
||||
if (!$id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->getConnection()->update(
|
||||
$this->getMainTable(),
|
||||
['status' => $value],
|
||||
['id = ?' => (int) $id]
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getProcessing($id)
|
||||
{
|
||||
$select = $this->getConnection()->select()->from(
|
||||
$this->getMainTable(),
|
||||
'status'
|
||||
)->where(
|
||||
'id = :id'
|
||||
);
|
||||
return $this->getConnection()->fetchOne($select, [':id' => $id]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace JR\NetSuite\Model\ResourceModel\ProductQueue;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use JR\NetSuite\Model\Data\ProductQueueItem;
|
||||
use JR\NetSuite\Model\ResourceModel\ProductQueue as ProductQueueResource;
|
||||
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(ProductQueueItem::class, ProductQueueResource::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next items in the queue based on batch size
|
||||
*
|
||||
* @param int $type_id
|
||||
* @param int $batchSize
|
||||
* @return \Magento\Framework\Data\Collection\AbstractDb
|
||||
*/
|
||||
public function getNext($batchSize = null)
|
||||
{
|
||||
$collection = $this
|
||||
->addFieldToSelect('*')
|
||||
->addFieldToFilter('status', ['eq' => 'pending'])
|
||||
->setOrder('id', 'asc');
|
||||
|
||||
if ($batchSize) {
|
||||
$collection->getSelect()->limit($batchSize);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
// Custom methods to add more specialized filters
|
||||
public function addStatusFilter($status)
|
||||
{
|
||||
$this->addFieldToFilter('status', $status);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addDateRangeFilter($fromDate, $toDate)
|
||||
{
|
||||
$this->addFieldToFilter('updated_at', ['from' => $fromDate, 'to' => $toDate]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addUnprocessedFilter()
|
||||
{
|
||||
$this->addFieldToFilter('status', ['neq' => 'processed']);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addPendingFilter()
|
||||
{
|
||||
$this->addFieldToFilter('status', ['eq' => 'pending']);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addProcessingFilter()
|
||||
{
|
||||
$this->addFieldToFilter('status', ['eq' => 'processing']);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addFailedFilter()
|
||||
{
|
||||
$this->addFieldToFilter('status', ['eq' => 'failed']);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addProcessedFilter()
|
||||
{
|
||||
$this->addFieldToFilter('status', ['eq' => 'processed']);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addUpdatedAtFilter($fromDate, $toDate)
|
||||
{
|
||||
$this->addFieldToFilter('updated_at', ['from' => $fromDate, 'to' => $toDate]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addCreatedAtFilter($fromDate, $toDate)
|
||||
{
|
||||
$this->addFieldToFilter('created_at', ['from' => $fromDate, 'to' => $toDate]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addNetsuiteProductIdFilter($netsuiteProductId)
|
||||
{
|
||||
$this->addFieldToFilter('netsuite_product_id', $netsuiteProductId);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addSkuFilter($sku)
|
||||
{
|
||||
$this->addFieldToFilter('sku', $sku);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addTypeFilter($type)
|
||||
{
|
||||
$this->addFieldToFilter('type', $type);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* get all product queue items that are pending
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getPendingItems()
|
||||
{
|
||||
return $this->addPendingFilter();
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace JR\NetSuite\Model\ResourceModel;
|
||||
|
||||
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
class RmaQueue extends AbstractDb {
|
||||
/**
|
||||
* Initialize resource
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function _construct()
|
||||
{
|
||||
$this->_init('jr_netsuite_rma_queue', 'id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
namespace JR\NetSuite\Model\ResourceModel\RmaQueue;
|
||||
|
||||
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
|
||||
{
|
||||
protected $_idFieldName = 'id';
|
||||
protected $_eventPrefix = 'jr_netsuite_rma_queue_collection';
|
||||
protected $_eventObject = 'rma_queue_collection';
|
||||
|
||||
/**
|
||||
* Define the resource model & the model.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init('JR\NetSuite\Model\RmaQueue', 'JR\NetSuite\Model\ResourceModel\RmaQueue');
|
||||
}
|
||||
|
||||
/**
|
||||
* get collection of RmaQueue with netsuite_tran_id filter
|
||||
*
|
||||
*/
|
||||
public function getCollectionByNetsuiteTranId($netsuiteTranId){
|
||||
$collection = $this
|
||||
->addFieldToSelect('*')
|
||||
->addFieldToFilter('netsuite_tran_id', ['eq' => $netsuiteTranId])
|
||||
->setOrder('id', 'desc');
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next items in the queue based on batch size
|
||||
*
|
||||
* @param int $batchSize
|
||||
*/
|
||||
public function getNext($batchSize = null)
|
||||
{
|
||||
$collection = $this
|
||||
->addFieldToSelect('*')
|
||||
->addFieldToFilter('status', ['eq' => 'pending'])
|
||||
->addFieldToFilter('data', ['notnull' => true])
|
||||
->setOrder('id', 'asc');
|
||||
|
||||
if ($batchSize) {
|
||||
$collection->getSelect()->limit($batchSize);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert or update based on UNIQUE combo 'key-processing'
|
||||
* @param $key
|
||||
* @param $value
|
||||
*/
|
||||
public function insertOrUpdate($item)
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
try {
|
||||
// Start a transaction
|
||||
// $connection->beginTransaction();
|
||||
|
||||
// echo json_encode($item, JSON_PRETTY_PRINT) . "\n";
|
||||
|
||||
// update the existing item with same netsuite_order_id
|
||||
$select = $connection->select()
|
||||
->from($this->getMainTable())
|
||||
// ->where('status = ?', 'pending')
|
||||
// ->where('netsuite_tran_id = ?', $item['netsuite_tran_id'])
|
||||
->where('netsuite_id = ?', $item['netsuite_id']);
|
||||
$existingItem = $connection->fetchRow($select);
|
||||
|
||||
if (!$existingItem) {
|
||||
$existingItem = [];
|
||||
}
|
||||
|
||||
// if the existing item is already success and the data is the same, skip
|
||||
if (isset($existingItem['status']) && $existingItem['status'] == 'success'
|
||||
&& isset($existingItem['data']) && $existingItem['data'] == json_encode($item)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$recorded_at = \DateTime::createFromFormat('m/d/Y g:i a', $item['recorded_at'], new \DateTimeZone('America/Los_Angeles'));
|
||||
$recorded_at->setTimezone(new \DateTimeZone('UTC'));
|
||||
|
||||
$data = [
|
||||
'netsuite_id' => $item['netsuite_id'],
|
||||
'netsuite_tran_id' => $item['netsuite_tran_id'],
|
||||
'data' => json_encode($item),
|
||||
'messages' => '',
|
||||
'recorded_at' => $recorded_at->format('Y-m-d H:i:s'),
|
||||
// 'created_at' => (new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
|
||||
'status' => 'pending',
|
||||
];
|
||||
|
||||
// update the existing item with same netsuite_order_id
|
||||
if (isset($existingItem['id'])) {
|
||||
$data['id'] = $existingItem['id'];
|
||||
}
|
||||
|
||||
// Insert new item or update existing one
|
||||
$connection->insertOnDuplicate($this->getMainTable(), $data);
|
||||
|
||||
// Commit the transaction
|
||||
// $connection->commit();
|
||||
} catch (\Exception $e) {
|
||||
// Rollback the transaction in case of an error
|
||||
$connection->rollBack();
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
__('Error in insertOrUpdate: %1', $e->getMessage()),
|
||||
$e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last sync netsuite rma id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLastSyncRecordDate()
|
||||
{
|
||||
$select = $this->getConnection()->select()
|
||||
->from($this->getMainTable(), ['recorded_at'])
|
||||
->order('recorded_at DESC')
|
||||
->limit(1);
|
||||
|
||||
return $this->getConnection()->fetchOne($select);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last sync netsuite rma id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLastSyncNetsuiteId()
|
||||
{
|
||||
$select = $this->getConnection()->select()
|
||||
->from($this->getMainTable(), ['netsuite_id'])
|
||||
->where('data IS NOT NULL')
|
||||
->order('netsuite_id DESC')
|
||||
->limit(1);
|
||||
|
||||
return $this->getConnection()->fetchOne($select);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user