97 lines
2.7 KiB
PHP
Executable File
97 lines
2.7 KiB
PHP
Executable File
<?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]);
|
|
}
|
|
} |