ebay.php (31457B)
1 <?php 2 class ModelExtensionOpenBayEbay extends Model{ 3 public function install() { 4 $this->load->model('setting/event'); 5 6 $this->model_setting_event->addEvent('openbay_ebay_add_order', 'catalog/model/checkout/order/addOrderHistory/after', 'extension/openbay/ebay/eventAddOrderHistory'); 7 8 $settings = array(); 9 $settings["ebay_token"] = ''; 10 $settings["ebay_secret"] = ''; 11 $settings["ebay_encryption_key"] = ''; 12 $settings["ebay_encryption_iv"] = ''; 13 $settings["ebay_enditems"] = '0'; 14 $settings["ebay_logging"] = '1'; 15 $settings["ebay_payment_instruction"] = ''; 16 $settings["ebay_payment_paypal_address"] = ''; 17 $settings["ebay_tax"] = '0'; 18 $settings["ebay_status_import_id"] = '1'; 19 $settings["ebay_status_shipped_id"] = '3'; 20 $settings["ebay_status_paid_id"] = '2'; 21 $settings["ebay_status_cancelled_id"] = '7'; 22 $settings["ebay_status_refunded_id"] = '11'; 23 $settings["ebay_def_currency"] = 'GBP'; 24 $settings["ebay_stock_allocate"] = '0'; 25 $settings["ebay_update_notify"] = '1'; 26 $settings["ebay_confirm_notify"] = '1'; 27 $settings["ebay_confirmadmin_notify"] = '1'; 28 $settings["ebay_created_hours"] = '48'; 29 $settings["ebay_create_date"] = '0'; 30 $settings["ebay_itm_link"] = 'http://www.ebay.com/itm/'; 31 $settings["ebay_relistitems"] = 0; 32 $settings["ebay_time_offset"] = 0; 33 $settings["ebay_default_addressformat"] = '{firstname} {lastname} 34 {company} 35 {address_1} 36 {address_2} 37 {city} 38 {zone} 39 {postcode} 40 {country}'; 41 42 $this->model_setting_setting->editSetting('ebay', $settings); 43 44 $this->db->query(" 45 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_category` ( 46 `ebay_category_id` int(11) NOT NULL AUTO_INCREMENT, 47 `CategoryID` int(11) NOT NULL, 48 `CategoryParentID` int(11) NOT NULL, 49 `CategoryLevel` smallint(6) NOT NULL, 50 `CategoryName` char(100) NOT NULL, 51 `BestOfferEnabled` tinyint(1) NOT NULL, 52 `AutoPayEnabled` tinyint(1) NOT NULL, 53 PRIMARY KEY (`ebay_category_id`) 54 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 55 56 $this->db->query(" 57 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_category_history` ( 58 `ebay_category_history_id` int(11) NOT NULL AUTO_INCREMENT, 59 `CategoryID` int(11) NOT NULL, 60 `breadcrumb` varchar(255) NOT NULL, 61 `used` int(6) NOT NULL, 62 PRIMARY KEY (`ebay_category_history_id`) 63 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 64 65 $this->db->query(" 66 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_listing` ( 67 `ebay_listing_id` int(11) NOT NULL AUTO_INCREMENT, 68 `ebay_item_id` char(100) NOT NULL, 69 `product_id` int(11) NOT NULL, 70 `variant` int(11) NOT NULL, 71 `status` SMALLINT(3) NOT NULL DEFAULT '1', 72 PRIMARY KEY (`ebay_listing_id`), 73 KEY `product_id` (`product_id`) 74 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 75 ; 76 $this->db->query(" 77 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_listing_pending` ( 78 `ebay_listing_pending_id` int(11) NOT NULL AUTO_INCREMENT, 79 `ebay_item_id` char(25) NOT NULL, 80 `product_id` int(11) NOT NULL, 81 `key` char(50) NOT NULL, 82 `variant` int(11) NOT NULL, 83 PRIMARY KEY (`ebay_listing_pending_id`), 84 KEY `product_id` (`product_id`) 85 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 86 87 $this->db->query(" 88 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_shipping` ( 89 `ebay_shipping_id` int(11) NOT NULL AUTO_INCREMENT, 90 `description` varchar(100) NOT NULL, 91 `InternationalService` tinyint(4) NOT NULL, 92 `ShippingService` varchar(100) NOT NULL, 93 `ShippingServiceID` int(11) NOT NULL, 94 `ServiceType` varchar(100) NOT NULL, 95 `ValidForSellingFlow` tinyint(4) NOT NULL, 96 `ShippingCategory` varchar(100) NOT NULL, 97 `ShippingTimeMin` int(11) NOT NULL, 98 `ShippingTimeMax` int(11) NOT NULL, 99 PRIMARY KEY (`ebay_shipping_id`) 100 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 101 102 $this->db->query(" 103 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_shipping_location` ( 104 `ebay_shipping_id` int(11) NOT NULL AUTO_INCREMENT, 105 `description` varchar(100) NOT NULL, 106 `detail_version` varchar(100) NOT NULL, 107 `shipping_location` varchar(100) NOT NULL, 108 `update_time` varchar(100) NOT NULL, 109 PRIMARY KEY (`ebay_shipping_id`) 110 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 111 112 $this->db->query(" 113 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_payment_method` ( 114 `ebay_payment_method_id` int(11) NOT NULL AUTO_INCREMENT, 115 `ebay_name` char(50) NOT NULL, 116 `local_name` char(50) NOT NULL, 117 PRIMARY KEY (`ebay_payment_method_id`) 118 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=5;"); 119 120 $this->db->query(" 121 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_transaction` ( 122 `ebay_transaction_id` int(11) NOT NULL AUTO_INCREMENT, 123 `order_id` int(11) NOT NULL, 124 `product_id` int(11) NOT NULL, 125 `sku` varchar(100) NOT NULL, 126 `txn_id` varchar(100) NOT NULL, 127 `item_id` varchar(100) NOT NULL, 128 `containing_order_id` varchar(100) NOT NULL, 129 `order_line_id` varchar(100) NOT NULL, 130 `qty` int(11) NOT NULL, 131 `smp_id` int(11) NOT NULL, 132 `created` DATETIME NOT NULL, 133 `modified` DATETIME NOT NULL, 134 PRIMARY KEY (`ebay_transaction_id`), 135 KEY `product_id` (`product_id`), 136 KEY `order_id` (`order_id`), 137 KEY `smp_id` (`smp_id`) 138 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 139 140 $this->db->query(" 141 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_order` ( 142 `ebay_order_id` int(11) NOT NULL AUTO_INCREMENT, 143 `parent_ebay_order_id` int(11) NOT NULL, 144 `order_id` int(11) NOT NULL, 145 `smp_id` int(11) NOT NULL, 146 `tracking_no` varchar(100) NOT NULL, 147 `carrier_id` varchar(100) NOT NULL, 148 PRIMARY KEY (`ebay_order_id`), 149 KEY `order_id` (`order_id`), 150 KEY `smp_id` (`smp_id`), 151 KEY `parent_ebay_order_id` (`parent_ebay_order_id`) 152 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 153 154 $this->db->query(" 155 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_profile` ( 156 `ebay_profile_id` int(11) NOT NULL AUTO_INCREMENT, 157 `name` varchar(100) NOT NULL, 158 `description` text NOT NULL, 159 `type` int(11) NOT NULL, 160 `default` TINYINT(1) NOT NULL, 161 `data` text NOT NULL, 162 PRIMARY KEY (`ebay_profile_id`) 163 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 164 165 $this->db->query(" 166 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_setting_option` ( 167 `ebay_setting_option_id` INT(11) NOT NULL AUTO_INCREMENT, 168 `key` VARCHAR(100) NOT NULL, 169 `last_updated` DATETIME NOT NULL, 170 `data` TEXT NOT NULL, 171 PRIMARY KEY (`ebay_setting_option_id`) 172 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 173 174 $this->db->query(" 175 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_image_import` ( 176 `id` int(11) NOT NULL AUTO_INCREMENT, 177 `image_original` text NOT NULL, 178 `image_new` text NOT NULL, 179 `name` text NOT NULL, 180 `product_id` int(11) NOT NULL, 181 `imgcount` int(11) NOT NULL, 182 PRIMARY KEY (`id`) 183 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 184 185 $this->db->query(" 186 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_shipping_location_exclude` ( 187 `ebay_shipping_exclude_id` int(11) NOT NULL AUTO_INCREMENT, 188 `description` varchar(100) NOT NULL, 189 `location` varchar(100) NOT NULL, 190 `region` varchar(100) NOT NULL, 191 PRIMARY KEY (`ebay_shipping_exclude_id`) 192 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 193 194 $this->db->query(" 195 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_stock_reserve` ( 196 `id` int(11) NOT NULL AUTO_INCREMENT, 197 `product_id` int(11) NOT NULL, 198 `variant_id` varchar(100) NOT NULL, 199 `item_id` varchar(100) NOT NULL, 200 `reserve` int(11) NOT NULL, 201 PRIMARY KEY (`id`), 202 KEY `product_id` (`product_id`) 203 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 204 205 $this->db->query(" 206 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_order_lock` ( 207 `smp_id` int(11) NOT NULL, 208 PRIMARY KEY (`smp_id`) 209 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 210 211 $this->db->query(" 212 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "ebay_template` ( 213 `template_id` INT(11) NOT NULL AUTO_INCREMENT, 214 `name` VARCHAR(100) NOT NULL, 215 `html` MEDIUMTEXT NOT NULL, 216 PRIMARY KEY (`template_id`) 217 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); 218 } 219 220 public function uninstall() { 221 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "ebay_category`;"); 222 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "ebay_category_history`;"); 223 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "ebay_listing`;"); 224 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "ebay_listing_pending`;"); 225 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "ebay_shipping`;"); 226 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "ebay_shipping_location`;"); 227 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "ebay_payment_method`;"); 228 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "ebay_transaction`;"); 229 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "ebay_order`;"); 230 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "ebay_profile`;"); 231 232 $this->load->model('setting/event'); 233 $this->model_setting_event->deleteEventByCode('openbay_ebay_add_order'); 234 } 235 236 public function patch() { 237 if ($this->config->get('ebay_status') == 1) { 238 239 } 240 } 241 242 public function totalLinked() { 243 $sql = "SELECT COUNT(DISTINCT p.product_id) AS total 244 FROM `" . DB_PREFIX . "ebay_listing` `el` 245 LEFT JOIN `" . DB_PREFIX . "product` `p` ON (`el`.`product_id` = `p`.`product_id`) 246 LEFT JOIN `" . DB_PREFIX . "product_description` `pd` ON (`p`.`product_id` = `pd`.`product_id`) 247 WHERE `el`.`status` = '1' 248 AND `pd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'"; 249 250 $query = $this->db->query($sql); 251 252 return $query->row['total']; 253 } 254 255 public function loadLinked($limit = 100, $page = 1) { 256 $this->load->model('tool/image'); 257 258 $start = $limit * ($page - 1); 259 260 $has_option = ''; 261 if ($this->openbay->addonLoad('openstock') ) { 262 $this->load->model('extension/module/openstock'); 263 $has_option = '`p`.`has_option`, '; 264 } 265 266 $sql = " 267 SELECT 268 " . $has_option . " 269 `el`.`ebay_item_id`, 270 `p`.`product_id`, 271 `p`.`sku`, 272 `p`.`model`, 273 `p`.`quantity`, 274 `pd`.`name`, 275 `esr`.`reserve` 276 FROM `" . DB_PREFIX . "ebay_listing` `el` 277 LEFT JOIN `" . DB_PREFIX . "product` `p` ON (`el`.`product_id` = `p`.`product_id`) 278 LEFT JOIN `" . DB_PREFIX . "product_description` `pd` ON (`p`.`product_id` = `pd`.`product_id`) 279 LEFT JOIN `" . DB_PREFIX . "ebay_stock_reserve` `esr` ON (`esr`.`product_id` = `p`.`product_id`) 280 WHERE `el`.`status` = '1' 281 AND `pd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'"; 282 283 $sql .= " LIMIT " . (int)$start . "," . (int)$limit; 284 285 $qry = $this->db->query($sql); 286 287 $data = array(); 288 if ($qry->num_rows) { 289 foreach ($qry->rows as $row) { 290 $data[$row['ebay_item_id']] = array( 291 'product_id' => $row['product_id'], 292 'sku' => $row['sku'], 293 'model' => $row['model'], 294 'qty' => $row['quantity'], 295 'name' => $row['name'], 296 'link_edit' => $this->url->link('catalog/product/edit', 'user_token=' . $this->session->data['user_token'] . '&product_id=' . $row['product_id'], true), 297 'link_ebay' => $this->config->get('ebay_itm_link') . $row['ebay_item_id'], 298 'reserve' => (int)$row['reserve'], 299 ); 300 301 $data[$row['ebay_item_id']]['options'] = 0; 302 303 if ((isset($row['has_option']) && $row['has_option'] == 1) && $this->openbay->addonLoad('openstock')) { 304 $data[$row['ebay_item_id']]['options'] = $this->model_setting_module_openstock->getVariants((int)$row['product_id']); 305 } 306 307 //get the allocated stock - items that have been bought but not assigned to an order 308 if ($this->config->get('ebay_stock_allocate') == 0) { 309 $data[$row['ebay_item_id']]['allocated'] = $this->openbay->ebay->getAllocatedStock($row['product_id']); 310 } else { 311 $data[$row['ebay_item_id']]['allocated'] = 0; 312 } 313 } 314 } 315 316 return $data; 317 } 318 319 public function loadLinkedStatus($item_ids) { 320 $this->openbay->ebay->log('loadLinkedStatus() - Get item status from ebay for multiple IDs'); 321 return $this->openbay->ebay->call('item/getItemsById/', array('item_ids' => $item_ids)); 322 } 323 324 public function loadUnlinked($limit = 200, $page = 1, $filter = array()) { 325 $unlinked = array(); 326 $current = 1; 327 $stop_flag = 0; 328 329 while(count($unlinked) < 5) { 330 if ($current > 5) { 331 $stop_flag = 1; 332 break; 333 } else { 334 $current++; 335 } 336 337 $this->openbay->ebay->log('Checking unlinked page: ' . $page); 338 339 $response = $this->openbay->ebay->getEbayItemList($limit, $page, $filter); 340 341 if ($this->openbay->ebay->lasterror == true) { 342 break; 343 } 344 345 foreach ($response['items'] as $item_id => $item) { 346 if ($this->openbay->ebay->getProductId($item_id, 1) == false) { 347 $unlinked[$item_id] = $item; 348 } 349 } 350 351 $this->openbay->ebay->log('Unlinked count: ' . count($unlinked)); 352 353 if ($response['max_page'] == $page || count($unlinked) >= 5) { 354 break; 355 } else { 356 $page++; 357 } 358 } 359 360 return array( 361 'items' => $unlinked, 362 'break' => $stop_flag, 363 'next_page' => $response['page']+1, 364 'max_page' => $response['max_page'] 365 ); 366 } 367 368 public function loadItemLinks() { 369 $local = $this->openbay->ebay->getLiveListingArray(); 370 $response = $this->openbay->ebay->getEbayActiveListings(); 371 372 $data = array( 373 'unlinked' => array(), 374 'linked' => array() 375 ); 376 377 if (!empty($response)) { 378 foreach ($response as $key => $value) { 379 if (!in_array($key, $local)) { 380 $data['unlinked'][$key] = $value; 381 } else { 382 $data['linked'][$key] = $value; 383 } 384 } 385 } 386 387 return $data; 388 } 389 390 public function saveItemLink($data) { 391 $this->openbay->ebay->log('Creating item link.'); 392 $this->openbay->ebay->createLink($data['pid'], $data['itemId'], $data['variants']); 393 394 if (($data['qty'] != $data['ebayqty']) || $data['variants'] == 1) { 395 $this->load->model('catalog/product'); 396 $this->openbay->ebay->log('Updating eBay with new qty'); 397 $this->openbay->ebay->productUpdateListen($data['pid'], $this->model_catalog_product->getProduct($data['pid'])); 398 } else { 399 $this->openbay->ebay->log('Qty on eBay is the same as our stock, no update needed'); 400 return array('msg' => 'ok', 'error' => false); 401 } 402 } 403 404 public function getSellerStoreCategories() { 405 $qry = $this->db->query("SHOW TABLES LIKE '" . DB_PREFIX . "ebay_store_category'"); 406 407 if ($qry->num_rows) { 408 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ebay_store_category` WHERE `parent_id` = '0' ORDER BY `CategoryName` ASC"); 409 410 if ($qry->num_rows) { 411 $cats = array(); 412 413 foreach ($qry->rows as $row) { 414 $lev1 = $row['CategoryName']; 415 $qry2 = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ebay_store_category` WHERE `parent_id` = '" . $row['ebay_store_category_id'] . "' ORDER BY `CategoryName` ASC"); 416 417 if ($qry2->num_rows) { 418 foreach ($qry2->rows as $row2) { 419 $qry3 = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ebay_store_category` WHERE `parent_id` = '" . $row2['ebay_store_category_id'] . "' ORDER BY `CategoryName` ASC"); 420 421 if ($qry3->num_rows) { 422 foreach ($qry3->rows as $row3) { 423 $cats[$row3['CategoryID']] = $lev1 . ' > ' . $row2['CategoryName'] . ' > ' . $row3['CategoryName']; 424 } 425 } else { 426 $cats[$row2['CategoryID']] = $lev1 . ' > ' . $row2['CategoryName']; 427 } 428 } 429 } else { 430 $cats[$row['CategoryID']] = $lev1; 431 } 432 } 433 434 return $cats; 435 } else { 436 return false; 437 } 438 } else { 439 return false; 440 } 441 } 442 443 public function getCategory($parent) { 444 $this->load->language('extension/openbay/ebay_new'); 445 446 $json = array(); 447 448 if (empty($parent)) { 449 $cat_qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ebay_category` WHERE `CategoryID` = `CategoryParentID`"); 450 } else { 451 $cat_qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ebay_category` WHERE `CategoryParentID` = '" . $parent . "'"); 452 } 453 454 if ($cat_qry->num_rows) { 455 $json['cats'] = array(); 456 foreach ($cat_qry->rows as $row) { 457 $json['cats'][] = $row; 458 } 459 $json['items'] = $cat_qry->num_rows; 460 461 } else { 462 if (empty($parent)) { 463 $json['error'] = $this->language->get('error_category_sync'); 464 } 465 466 $json['items'] = null; 467 } 468 469 return $json; 470 } 471 472 public function getSuggestedCategories($qry) { 473 $response = array( 474 'data' => $this->openbay->ebay->call('listing/getSuggestedCategories/', array('qry' => $qry)), 475 'error' => $this->openbay->ebay->lasterror, 476 'msg' => $this->openbay->ebay->lastmsg 477 ); 478 479 return $response; 480 } 481 482 public function getShippingService($international, $type) { 483 $json = array(); 484 $result = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ebay_shipping` WHERE `InternationalService` = '" . (int)$international . "' AND `ValidForSellingFlow` = '1' AND `ServiceType` LIKE '%" . $this->db->escape($type) . "%'"); 485 486 if ($result->num_rows) { 487 $json['service'] = array(); 488 foreach ($result->rows as $row) { 489 $json['service'][$row['ShippingService']] = $row; 490 } 491 } 492 493 return $json; 494 } 495 496 public function getShippingLocations() { 497 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ebay_shipping_location` WHERE `shipping_location` != 'None' AND `shipping_location` != 'Worldwide'"); 498 499 if ($qry->num_rows) { 500 $json = array(); 501 foreach ($qry->rows as $row) { 502 $json[] = $row; 503 } 504 return $json; 505 } else { 506 return false; 507 } 508 } 509 510 public function getEbayCategorySpecifics($category_id) { 511 $response['data'] = $this->openbay->ebay->call('listing/getEbayCategorySpecifics/', array('id' => $category_id)); 512 $response['error'] = $this->openbay->ebay->lasterror; 513 $response['msg'] = $this->openbay->ebay->lastmsg; 514 return $response; 515 } 516 517 public function getCategoryFeatures($category_id) { 518 $response['data'] = $this->openbay->ebay->call('listing/getCategoryFeatures/', array('id' => $category_id)); 519 $response['error'] = $this->openbay->ebay->lasterror; 520 $response['msg'] = $this->openbay->ebay->lastmsg; 521 return $response; 522 } 523 524 public function getSellerSummary() { 525 $response['data'] = $this->openbay->ebay->call('account/getSellerSummary/'); 526 $response['error'] = $this->openbay->ebay->lasterror; 527 $response['msg'] = $this->openbay->ebay->lastmsg; 528 529 return $response; 530 } 531 532 public function getPaymentTypes() { 533 $cat_payment = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ebay_payment_method`"); 534 $payments = array(); 535 536 foreach ($cat_payment->rows as $row) { 537 $payments[] = $row; 538 } 539 540 return $payments; 541 } 542 543 public function getPopularCategories() { 544 $res = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ebay_category_history` ORDER BY `used` DESC LIMIT 5"); 545 $cats = array(); 546 547 foreach ($res->rows as $row) { 548 $cats[] = $row; 549 } 550 551 return $cats; 552 } 553 554 private function getCategoryStructure($id) { 555 $res = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ebay_category` WHERE `CategoryID` = '" . $this->db->escape($id) . "' LIMIT 1"); 556 return $res->row; 557 } 558 559 public function ebayVerifyAddItem($data, $options) { 560 if ($options == 'yes') { 561 $response['data'] = $this->openbay->ebay->call('listing/verifyFixedPrice/', $data); 562 } else { 563 $response['data'] = $this->openbay->ebay->call('listing/ebayVerifyAddItem/', $data); 564 } 565 566 $response['error'] = $this->openbay->ebay->lasterror; 567 $response['msg'] = $this->openbay->ebay->lastmsg; 568 569 return $response; 570 } 571 572 public function ebayAddItem($data, $options) { 573 if ($options == 'yes') { 574 $response = $this->openbay->ebay->call('listing/addFixedPrice/', $data); 575 $variant = 1; 576 } else { 577 $response = $this->openbay->ebay->call('listing/ebayAddItem/', $data); 578 $variant = 0; 579 } 580 581 $data2 = array(); 582 $data2['data'] = $response; 583 $data2['error'] = $this->openbay->ebay->lasterror; 584 $data2['msg'] = $this->openbay->ebay->lastmsg; 585 586 if (!empty($response['ItemID'])) { 587 $this->openbay->ebay->createLink($data['product_id'], $response['ItemID'], $variant); 588 $this->openbay->ebay->addReserve($data, $response['ItemID'], $variant); 589 590 $item_link = $this->config->get('ebay_itm_link'); 591 592 if (!empty($item_link)) { 593 $data2['data']['view_link'] = html_entity_decode($this->config->get('ebay_itm_link') . $response['ItemID']); 594 } else { 595 $data2['data']['view_link'] = ''; 596 } 597 } else { 598 $data2['error'] = false; 599 $data2['msg'] = 'ok'; 600 $data2['data']['Failed'] = true; 601 } 602 603 return $data2; 604 } 605 606 public function logCategoryUsed($category_id) { 607 $breadcrumb = array(); 608 $original_id = $category_id; 609 $stop = false; 610 $i = 0; //fallback to stop infinate loop 611 $err = false; 612 613 while($stop == false && $i < 10) { 614 $cat = $this->getCategoryStructure($category_id); 615 616 if (!empty($cat)) { 617 $breadcrumb[] = $cat['CategoryName']; 618 619 if ($cat['CategoryParentID'] == $category_id) { 620 $stop = true; 621 } else { 622 $category_id = $cat['CategoryParentID']; 623 } 624 625 $i++; 626 } else { 627 $stop = true; 628 $err = true; 629 } 630 } 631 632 if ($err == false) { 633 $res = $this->db->query("SELECT `used` FROM `" . DB_PREFIX . "ebay_category_history` WHERE `CategoryID` = '" . $original_id . "' LIMIT 1"); 634 635 if ($res->num_rows) { 636 $new = $res->row['used'] + 1; 637 $this->db->query("UPDATE `" . DB_PREFIX . "ebay_category_history` SET `used` = '" . $new . "' WHERE `CategoryID` = '" . $original_id . "' LIMIT 1"); 638 } else { 639 $this->db->query("INSERT INTO `" . DB_PREFIX . "ebay_category_history` SET `CategoryID` = '" . $original_id . "', `breadcrumb` = '" . $this->db->escape(implode(' > ', array_reverse($breadcrumb))) . "', `used` = '1'"); 640 } 641 } 642 } 643 644 public function getProductStock($id) { 645 $res = $this->db->query("SELECT * FROM `" . DB_PREFIX . "product` WHERE `product_id` = '" . $this->db->escape($id) . "' LIMIT 1"); 646 647 if (isset($res->row['has_option']) && $res->row['has_option'] == 1) { 648 if ($this->openbay->addonLoad('openstock')) { 649 $this->load->model('extension/module/openstock'); 650 $this->load->model('tool/image'); 651 $variant = $this->model_setting_module_openstock->getVariants((int)$id); 652 } else { 653 $variant = 0; 654 } 655 } else { 656 $variant = 0; 657 } 658 659 return array( 660 'qty' => $res->row['quantity'], 661 'subtract' => (int)$res->row['subtract'], 662 'allocated' => $this->openbay->ebay->getAllocatedStock($id), 663 'variant' => $variant 664 ); 665 } 666 667 public function getUsage() { 668 return $this->openbay->ebay->call('report/accountUse/'); 669 } 670 671 public function getPlans() { 672 return $this->openbay->ebay->call('plan/getPlans/'); 673 } 674 675 public function getMyPlan() { 676 return $this->openbay->ebay->call('plan/myPlan/'); 677 } 678 679 public function getLiveListingArray() { 680 $qry = $this->db->query("SELECT `product_id`, `ebay_item_id` FROM `" . DB_PREFIX . "ebay_listing` WHERE `status` = 1"); 681 682 $data = array(); 683 if ($qry->num_rows) { 684 foreach ($qry->rows as $row) { 685 $data[$row['product_id']] = $row['ebay_item_id']; 686 } 687 } 688 689 return $data; 690 } 691 692 public function verifyCredentials() { 693 $this->request->post['domain'] = HTTPS_SERVER; 694 695 $data = $this->openbay->ebay->call('account/validate/', $this->request->post, array(), 'json', 1); 696 697 $return = array('error' => $this->openbay->ebay->lasterror, 'msg' => $this->openbay->ebay->lastmsg); 698 699 if ($this->openbay->ebay->lasterror != true) { 700 $return['data'] = $data; 701 } 702 703 return $return; 704 } 705 706 public function editSave($data) { 707 $this->openbay->ebay->log('editSave() - start..'); 708 709 //get product id 710 $product_id = $this->openbay->ebay->getProductId($data['itemId']); 711 712 $this->openbay->ebay->log('editSave() - product_id: ' . $product_id); 713 714 if ($data['variant'] == 0) { 715 //save the reserve level 716 $this->openbay->ebay->updateReserve($product_id, $data['itemId'], $data['qty_reserve']); 717 718 //get the stock info 719 $stock = $this->openbay->ebay->getProductStockLevel($product_id); 720 721 //do the stock sync 722 $this->openbay->ebay->putStockUpdate($data['itemId'], $stock['quantity']); 723 724 //finish the revise item call 725 return $this->openbay->ebay->call('listing/reviseItem/', $data); 726 } else { 727 $this->openbay->ebay->log('editSave() - variant item'); 728 729 $variant_data = array(); 730 $this->load->model('tool/image'); 731 $this->load->model('catalog/product'); 732 $this->load->model('extension/module/openstock'); 733 734 //get the options list for this product 735 $options = $this->model_setting_module_openstock->getVariants($product_id); 736 737 $variant_data['option_list'] = base64_encode(serialize($options[key($options)]['option_values'])); 738 739 $variant_data['groups'] = $data['optGroupArray']; 740 $variant_data['related'] = $data['optGroupRelArray']; 741 $variant_data['id'] = $data['itemId']; 742 743 $stock_flag = false; 744 745 foreach ($data['opt'] as $k => $opt) { 746 //update the variant reserve level 747 $this->openbay->ebay->updateReserve($product_id, $data['itemId'], $opt['reserve'], $opt['sku'], 1); 748 749 //get the stock info 750 $stock = $this->openbay->ebay->getProductStockLevel($product_id, $opt['sku']); 751 752 $this->openbay->ebay->log('editSave() - stock: ' . serialize($stock)); 753 754 if ($stock['quantity'] > 0 || $stock == true) { 755 $stock_flag = true; 756 } 757 758 // PRODUCT RESERVE LEVELS FOR VARIANT ITEMS (DOES NOT PASS THROUGH NORMAL SYSTEM) 759 $reserve = $this->openbay->ebay->getReserve($product_id, $data['itemId'], $opt['sku']); 760 761 $this->openbay->ebay->log('editSave() - reserve level: ' . $reserve); 762 763 if ($reserve != false) { 764 $this->openbay->ebay->log('editSave() / Variant (' . $opt['sku'] . ') - Reserve stock: ' . $reserve); 765 766 if ($stock['quantity'] > $reserve) { 767 $this->openbay->ebay->log('editSave() - Stock (' . $stock['quantity'] . ') is larger than reserve (' . $reserve . '), setting level to reserve'); 768 $stock['quantity'] = $reserve; 769 } 770 } 771 772 $variant_data['opt'][$k]['sku'] = $opt['sku']; 773 $variant_data['opt'][$k]['qty'] = $stock['quantity']; 774 $variant_data['opt'][$k]['price'] = number_format($opt['price'], 2, '.', ''); 775 776 // if any of the variants have 0 stock or no SKU set to inactive 777 if ($opt['sku'] == '' || $variant_data['opt'][$k]['qty'] < 1) { 778 $variant_data['opt'][$k]['active'] = 0; 779 } else { 780 $variant_data['opt'][$k]['active'] = $opt['active']; 781 } 782 783 784 $variant_option_values = $this->model_setting_module_openstock->getVariant($opt['product_option_variant_id']); 785 786 foreach ($variant_option_values as $variant_option_value) { 787 $variant_data['opt'][$k]['specifics'][] = array('name' => $variant_option_value['option_name'], 'value' => $variant_option_value['option_value_name']); 788 } 789 } 790 791 $this->openbay->ebay->log('editSave() - Debug - ' . serialize($variant_data)); 792 793 //send to the api to process 794 if ($stock_flag == true) { 795 $this->openbay->ebay->log('editSave() - Sending to API'); 796 $response = $this->openbay->ebay->call('item/reviseVariants', $variant_data); 797 return $response; 798 } else { 799 $this->openbay->ebay->log('editSave() - Ending item'); 800 $this->openbay->ebay->endItem($data['itemId']); 801 } 802 } 803 } 804 805 public function getProductAttributes($product_id) { 806 $product_attribute_group_data = array(); 807 808 $product_attribute_group_query = $this->db->query("SELECT ag.attribute_group_id, agd.name FROM " . DB_PREFIX . "product_attribute pa LEFT JOIN " . DB_PREFIX . "attribute a ON (pa.attribute_id = a.attribute_id) LEFT JOIN " . DB_PREFIX . "attribute_group ag ON (a.attribute_group_id = ag.attribute_group_id) LEFT JOIN " . DB_PREFIX . "attribute_group_description agd ON (ag.attribute_group_id = agd.attribute_group_id) WHERE pa.product_id = '" . (int)$product_id . "' AND agd.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY ag.attribute_group_id ORDER BY ag.sort_order, agd.name"); 809 810 foreach ($product_attribute_group_query->rows as $product_attribute_group) { 811 $product_attribute_data = array(); 812 813 $product_attribute_query = $this->db->query("SELECT a.attribute_id, ad.name, pa.text FROM " . DB_PREFIX . "product_attribute pa LEFT JOIN " . DB_PREFIX . "attribute a ON (pa.attribute_id = a.attribute_id) LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE pa.product_id = '" . (int)$product_id . "' AND a.attribute_group_id = '" . (int)$product_attribute_group['attribute_group_id'] . "' AND ad.language_id = '" . (int)$this->config->get('config_language_id') . "' AND pa.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY a.sort_order, ad.name"); 814 815 foreach ($product_attribute_query->rows as $product_attribute) { 816 $product_attribute_data[] = array( 817 'attribute_id' => $product_attribute['attribute_id'], 818 'name' => $product_attribute['name'], 819 'text' => $product_attribute['text'] 820 ); 821 } 822 823 $product_attribute_group_data[] = array( 824 'attribute_group_id' => $product_attribute_group['attribute_group_id'], 825 'name' => $product_attribute_group['name'], 826 'attribute' => $product_attribute_data 827 ); 828 } 829 830 return $product_attribute_group_data; 831 } 832 }