70 lines
1.9 KiB
PHP
Executable File
70 lines
1.9 KiB
PHP
Executable File
<?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);
|
|
}
|
|
}
|
|
|
|
} |