Files

148 lines
4.7 KiB
PHP
Executable File

<?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);
}
}