amazon_order.php (8096B)
1 <?php 2 class ModelExtensionOpenBayAmazonOrder extends Model { 3 public function acknowledgeOrder($order_id) { 4 $amazon_order_id = $this->getAmazonOrderId($order_id); 5 6 $request_xml = "<Request> 7 <AmazonOrderId>$amazon_order_id</AmazonOrderId> 8 <MerchantOrderId>$order_id</MerchantOrderId> 9 </Request>"; 10 11 $this->openbay->amazon->callNoResponse('order/acknowledge', $request_xml, false); 12 } 13 14 public function getProductId($sku) { 15 $row = $this->db->query("SELECT `product_id` FROM `" . DB_PREFIX . "amazon_product_link` WHERE `amazon_sku` = '" . $this->db->escape($sku) . "'")->row; 16 17 if (isset($row['product_id']) && !empty($row['product_id'])) { 18 return (int)$row['product_id']; 19 } 20 21 return 0; 22 } 23 24 public function getProductVar($sku) { 25 $row = $this->db->query("SELECT `var` FROM `" . DB_PREFIX . "amazon_product_link` WHERE `amazon_sku` = '" . $this->db->escape($sku) . "'")->row; 26 27 if (isset($row['var'])) { 28 return $row['var']; 29 } 30 31 return ''; 32 } 33 34 public function decreaseProductQuantity($product_id, $delta, $var = '') { 35 if ($product_id == 0) { 36 return; 37 } 38 if ($var == '') { 39 $this->db->query("UPDATE `" . DB_PREFIX . "product` SET `quantity` = GREATEST(`quantity` - '" . (int)$delta . "', 0) WHERE `product_id` = '" . (int)$product_id . "' AND `subtract` = '1'"); 40 } else { 41 $this->db->query("UPDATE `" . DB_PREFIX . "product_option_variant` SET `stock` = GREATEST(`stock` - '" . (int)$delta . "', 0) WHERE `product_id` = '" . (int)$product_id . "' AND `sku` = '" . $this->db->escape($var) . "' AND `subtract` = '1'"); 42 } 43 } 44 45 public function getMappedStatus($amazon_status) { 46 $amazon_status = trim(strtolower($amazon_status)); 47 48 switch ($amazon_status) { 49 case 'pending': 50 $order_status = $this->config->get('openbay_amazon_order_status_pending'); 51 break; 52 53 case 'unshipped': 54 $order_status = $this->config->get('openbay_amazon_order_status_unshipped'); 55 break; 56 57 case 'partiallyshipped': 58 $order_status = $this->config->get('openbay_amazon_order_status_partially_shipped'); 59 break; 60 61 case 'shipped': 62 $order_status = $this->config->get('openbay_amazon_order_status_shipped'); 63 break; 64 65 case 'canceled': 66 $order_status = $this->config->get('openbay_amazon_order_status_canceled'); 67 break; 68 69 case 'unfulfillable': 70 $order_status = $this->config->get('openbay_amazon_order_status_unfulfillable'); 71 break; 72 73 default: 74 $order_status = $this->config->get('config_order_status_id'); 75 break; 76 } 77 78 return $order_status; 79 } 80 81 public function getCountryName($country_code) { 82 $row = $this->db->query("SELECT `name` FROM `" . DB_PREFIX . "country` WHERE LOWER(`iso_code_2`) = '" . $this->db->escape(strtolower($country_code)) . "'")->row; 83 84 if (isset($row['name']) && !empty($row['name'])) { 85 return $row['name']; 86 } 87 88 return ''; 89 } 90 91 public function getCountryId($country_code) { 92 $row = $this->db->query("SELECT `country_id` FROM `" . DB_PREFIX . "country` WHERE LOWER(`iso_code_2`) = '" . $this->db->escape(strtolower($country_code)) . "'")->row; 93 94 if (isset($row['country_id']) && !empty($row['country_id'])) { 95 return (int)$row['country_id']; 96 } 97 98 return 0; 99 } 100 101 public function getZoneId($zone_name) { 102 $row = $this->db->query("SELECT `zone_id` FROM `" . DB_PREFIX . "zone` WHERE LOWER(`name`) = '" . $this->db->escape(strtolower($zone_name)) . "'")->row; 103 104 if (isset($row['country_id']) && !empty($row['country_id'])) { 105 return (int)$row['country_id']; 106 } 107 108 return 0; 109 } 110 111 public function updateOrderStatus($order_id, $status_id) { 112 $this->db->query("INSERT INTO `" . DB_PREFIX . "order_history` (`order_id`, `order_status_id`, `notify`, `comment`, `date_added`) VALUES (" . (int)$order_id . ", " . (int)$status_id . ", 0, '', NOW())"); 113 114 $this->db->query("UPDATE `" . DB_PREFIX . "order` SET `order_status_id` = " . (int)$status_id . " WHERE `order_id` = " . (int)$order_id); 115 } 116 117 public function addAmazonOrder($order_id, $amazon_order_id) { 118 $this->db->query("INSERT INTO `" . DB_PREFIX . "amazon_order` (`order_id`, `amazon_order_id`) VALUES (" . (int)$order_id . ", '" . $this->db->escape($amazon_order_id) . "')"); 119 } 120 121 public function addAmazonOrderProducts($order_id, $data) { 122 foreach ($data as $sku => $order_item_id) { 123 124 $row = $this->db->query("SELECT `order_product_id` FROM `" . DB_PREFIX . "order_product` WHERE `model` = '" . $this->db->escape($sku) . "' AND `order_id` = " . (int)$order_id . " LIMIT 1")->row; 125 126 if (!isset($row['order_product_id']) || empty($row['order_product_id'])) { 127 continue; 128 } 129 130 $order_product_id = $row['order_product_id']; 131 132 $this->db->query("INSERT INTO `" . DB_PREFIX . "amazon_order_product` (`order_product_id`, `amazon_order_item_id`) VALUES (" . (int)$order_product_id . ", '" . $this->db->escape($order_item_id) . "')"); 133 } 134 } 135 136 public function getOrderId($amazon_order_id) { 137 $row = $this->db->query("SELECT `order_id` FROM `" . DB_PREFIX . "amazon_order` WHERE `amazon_order_id` = '" . $this->db->escape($amazon_order_id) . "' LIMIT 1")->row; 138 139 if (isset($row['order_id']) && !empty($row['order_id'])) { 140 return $row['order_id']; 141 } 142 143 return ''; 144 } 145 146 public function getOrderStatus($order_id) { 147 $row = $this->db->query("SELECT `order_status_id` FROM `" . DB_PREFIX . "order` WHERE `order_id` = " . (int)$order_id)->row; 148 149 if (isset($row['order_status_id']) && !empty($row['order_status_id'])) { 150 return (int)$row['order_status_id']; 151 } 152 153 return 0; 154 } 155 156 public function getAmazonOrderId($order_id) { 157 $row = $this->db->query("SELECT `amazon_order_id` FROM `" . DB_PREFIX . "amazon_order` WHERE `order_id` = " . (int)$order_id . " LIMIT 1")->row; 158 159 if (isset($row['amazon_order_id']) && !empty($row['amazon_order_id'])) { 160 return $row['amazon_order_id']; 161 } 162 163 return null; 164 } 165 166 public function getProductOptionsByVar($product_var) { 167 $options = array(); 168 169 $option_value_ids = explode(':', $product_var); 170 foreach ($option_value_ids as $option_value_id) { 171 $option_details_row = $this->db->query("SELECT 172 pov.product_option_id, 173 pov.product_option_value_id, 174 od.name, 175 ovd.name as value, 176 opt.type 177 FROM `" . DB_PREFIX . "product_option_value` as pov, 178 `" . DB_PREFIX . "product_option` as po, 179 `" . DB_PREFIX . "option` as opt, 180 `" . DB_PREFIX . "option_value_description` as ovd, 181 `" . DB_PREFIX . "option_description` as od 182 WHERE pov.product_option_value_id = '" . (int)$option_value_id . "' AND 183 po.product_option_id = pov.product_option_id AND 184 opt.option_id = pov.option_id AND 185 ovd.option_value_id = pov.option_value_id AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND 186 od.option_id = pov.option_id AND od.language_id = '" . (int)$this->config->get('config_language_id') . "' 187 ")->row; 188 189 if (!empty($option_details_row)) { 190 $options[] = array( 191 'product_option_id' => (int)$option_details_row['product_option_id'], 192 'product_option_value_id' => (int)$option_details_row['product_option_value_id'], 193 'name' => $option_details_row['name'], 194 'value' => $option_details_row['value'], 195 'type' => $option_details_row['type'] 196 ); 197 } 198 } 199 200 return $options; 201 } 202 203 public function addOrderHistory($order_id) { 204 $logger = new Log('amazon_stocks.log'); 205 $logger->write('addOrderHistory() called with order id: ' . $order_id); 206 207 if ($this->config->get('openbay_amazon_status') != 1) { 208 $logger->write('addOrderHistory() openbay_amazon_status is disabled'); 209 return; 210 } 211 212 $order_products = $this->openbay->getOrderProducts($order_id); 213 214 $logger->write($order_products); 215 216 if (!empty($order_products)) { 217 foreach($order_products as $order_product) { 218 $this->openbay->amazon->productUpdateListen($order_product['product_id']); 219 } 220 } else { 221 $logger->write('Order product array empty!'); 222 } 223 224 225 $logger->write('addOrder() exiting'); 226 } 227 }