PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/catalog/model/openbay/amazon_product.php

https://gitlab.com/reclamare/mao
PHP | 129 lines | 102 code | 27 blank | 0 comment | 11 complexity | 84f36d3508eda6b745c39dcad1ce2a87 MD5 | raw file
  1. <?php
  2. class ModelOpenbayAmazonProduct extends Model {
  3. public function setStatus($insertion_id, $status_string) {
  4. $this->db->query("UPDATE `" . DB_PREFIX . "amazon_product` SET `status` = '" . $this->db->escape($status_string) . "' WHERE `insertion_id` = '" . $this->db->escape($insertion_id) . "'");
  5. }
  6. public function getProductRows($insertion_id) {
  7. return $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazon_product` WHERE `insertion_id` = '" . $this->db->escape($insertion_id) . "'")->rows;
  8. }
  9. public function getProduct($insertion_id) {
  10. return $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazon_product` WHERE `insertion_id` = '" . $this->db->escape($insertion_id) . "'")->row;
  11. }
  12. public function linkItems(array $data) {
  13. foreach ($data as $amazon_sku => $product_id) {
  14. $var_row = $this->db->query("SELECT `var` FROM `" . DB_PREFIX . "amazon_product` WHERE `sku` = '" . $this->db->escape($amazon_sku) . "' AND `product_id` = '" . (int)$product_id . "'")->row;
  15. $var = isset($var_row['var']) ? $var_row['var'] : '';
  16. $this->linkProduct($amazon_sku, $product_id, $var);
  17. }
  18. }
  19. public function insertError($data) {
  20. $this->db->query("INSERT INTO `" . DB_PREFIX . "amazon_product_error` SET `sku` = '" . $this->db->escape($data['sku']) . "', `error_code` = '" . (int)$data['error_code'] . "', `message` = '" . $this->db->escape($data['message']) . "', `insertion_id` = '" . $this->db->escape($data['insertion_id']) . "'");
  21. $this->db->query("UPDATE `" . DB_PREFIX . "amazon_product`SET `status` = 'error' WHERE `sku` = '" . $this->db->escape($data['sku']) . "' AND `insertion_id` = '" . $this->db->escape($data['insertion_id']) . "'");
  22. }
  23. public function deleteErrors($insertion_id) {
  24. $this->db->query("DELETE FROM `" . DB_PREFIX . "amazon_product_error` WHERE `insertion_id` = '" . $this->db->escape($insertion_id) . "'");
  25. }
  26. public function setSubmitError($insertion_id, $message) {
  27. $sku_rows = $this->db->query("SELECT `sku` FROM `" . DB_PREFIX . "amazon_product` WHERE `insertion_id` = '" . $this->db->escape($insertion_id) . "'")->rows;
  28. foreach ($sku_rows as $sku_row) {
  29. $data = array(
  30. 'sku' => $sku_row['sku'],
  31. 'error_code' => '0',
  32. 'message' => $message,
  33. 'insertion_id' => $insertion_id
  34. );
  35. $this->insertError($data);
  36. }
  37. }
  38. public function linkProduct($amazon_sku, $product_id, $var = '') {
  39. $count = $this->db->query("SELECT COUNT(*) as 'count' FROM `" . DB_PREFIX . "amazon_product_link` WHERE `product_id` = '" . (int)$product_id . "' AND `amazon_sku` = '" . $this->db->escape($amazon_sku) . "' AND `var` = '" . $this->db->escape($var) . "' LIMIT 1")->row;
  40. if ($count['count'] == 0) {
  41. $this->db->query("INSERT INTO `" . DB_PREFIX . "amazon_product_link` SET `product_id` = '" . (int)$product_id . "', `amazon_sku` = '" . $this->db->escape($amazon_sku) . "', `var` = '" . $this->db->escape($var) . "'");
  42. }
  43. }
  44. public function getProductQuantity($product_id, $var = '') {
  45. $this->load->library('openbay/amazon');
  46. $result = null;
  47. if ($var !== '' && $this->openbay->addonLoad('openstock')) {
  48. $this->load->model('tool/image');
  49. $this->load->model('module/openstock');
  50. $option_stocks = $this->model_module_openstock->getVariants($product_id);
  51. $option = null;
  52. foreach ($option_stocks as $option_iterator) {
  53. if ($option_iterator['var'] === $var) {
  54. $option = $option_iterator;
  55. break;
  56. }
  57. }
  58. if ($option != null) {
  59. $result = $option['stock'];
  60. }
  61. } else {
  62. $this->load->model('catalog/product');
  63. $product_info = $this->model_catalog_product->getProduct($product_id);
  64. if (isset($product_info['quantity'])) {
  65. $result = $product_info['quantity'];
  66. }
  67. }
  68. return $result;
  69. }
  70. public function updateSearch($results) {
  71. foreach ($results as $result) {
  72. $results_found = count($result['results']);
  73. $data = json_encode($result['results']);
  74. $this->db->query("
  75. UPDATE " . DB_PREFIX . "amazon_product_search
  76. SET matches = " . (int)$results_found . ",
  77. `data` = '" . $this->db->escape($data) . "',
  78. `status` = 'finished'
  79. WHERE product_id = " . (int)$result['product_id'] . " AND
  80. marketplace = '" . $this->db->escape($result['marketplace']) . "'
  81. LIMIT 1
  82. ");
  83. }
  84. }
  85. public function addListingReport($data) {
  86. $sql = "INSERT INTO " . DB_PREFIX . "amazon_listing_report (marketplace, sku, quantity, asin, price) VALUES ";
  87. $sql_values = array();
  88. foreach ($data as $product) {
  89. $sql_values[] = " ('" . $this->db->escape($product['marketplace']) . "', '" . $this->db->escape($product['sku']) . "', " . (int)$product['quantity'] . ", '" . $this->db->escape($product['asin']) . "', " . (double)$product['price'] . ") ";
  90. }
  91. $sql .= implode(',', $sql_values);
  92. $this->db->query($sql);
  93. }
  94. public function removeListingReportLock($marketplace) {
  95. $marketplaces = $this->config->get('openbay_amazon_processing_listing_reports');
  96. if ($marketplaces && ($key = array_search($marketplace, $marketplaces)) !== false) {
  97. unset($marketplaces[$key]);
  98. $this->config->set('openbay_amazon_processing_listing_reports', $marketplaces);
  99. $this->db->query("UPDATE " . DB_PREFIX . "setting SET `value` = '" . $this->db->escape(serialize($marketplaces)) . "', serialized = 1 WHERE `key` = 'openbay_amazon_processing_listing_reports'");
  100. }
  101. }
  102. }