amazonus_order.php (8224B)
1 <?php 2 class ModelExtensionOpenBayAmazonusOrder extends Model { 3 public function acknowledgeOrder($order_id) { 4 $amazonus_order_id = $this->getAmazonusOrderId($order_id); 5 6 $request_xml = "<Request> 7 <AmazonOrderId>$amazonus_order_id</AmazonOrderId> 8 <MerchantOrderId>$order_id</MerchantOrderId> 9 </Request>"; 10 11 $this->openbay->amazonus->callNoResponse('order/acknowledge', $request_xml, false); 12 } 13 14 public function getProductId($sku) { 15 $row = $this->db->query("SELECT `product_id` FROM `" . DB_PREFIX . "amazonus_product_link` WHERE `amazonus_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 . "amazonus_product_link` WHERE `amazonus_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($amazonus_status) { 46 $amazonus_status = trim(strtolower($amazonus_status)); 47 48 switch ($amazonus_status) { 49 case 'pending': 50 $order_status = $this->config->get('openbay_amazonus_order_status_pending'); 51 break; 52 53 case 'unshipped': 54 $order_status = $this->config->get('openbay_amazonus_order_status_unshipped'); 55 break; 56 57 case 'partiallyshipped': 58 $order_status = $this->config->get('openbay_amazonus_order_status_partially_shipped'); 59 break; 60 61 case 'shipped': 62 $order_status = $this->config->get('openbay_amazonus_order_status_shipped'); 63 break; 64 65 case 'canceled': 66 $order_status = $this->config->get('openbay_amazonus_order_status_canceled'); 67 break; 68 69 case 'unfulfillable': 70 $order_status = $this->config->get('openbay_amazonus_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 addAmazonusOrder($order_id, $amazonus_order_id) { 118 $this->db->query("INSERT INTO `" . DB_PREFIX . "amazonus_order` (`order_id`, `amazonus_order_id`) VALUES (" . (int)$order_id . ", '" . $this->db->escape($amazonus_order_id) . "')"); 119 } 120 121 /* $data = array(PRODUCT_SKU => ORDER_ITEM_ID) */ 122 public function addAmazonusOrderProducts($order_id, $data) { 123 foreach ($data as $sku => $order_item_id) { 124 125 $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; 126 127 if (!isset($row['order_product_id']) || empty($row['order_product_id'])) { 128 continue; 129 } 130 131 $order_product_id = $row['order_product_id']; 132 133 $this->db->query("INSERT INTO `" . DB_PREFIX . "amazonus_order_product` (`order_product_id`, `amazonus_order_item_id`) VALUES (" . (int)$order_product_id . ", '" . $this->db->escape($order_item_id) . "')"); 134 } 135 } 136 137 public function getOrderId($amazonus_order_id) { 138 $row = $this->db->query("SELECT `order_id` FROM `" . DB_PREFIX . "amazonus_order` WHERE `amazonus_order_id` = '" . $this->db->escape($amazonus_order_id) . "' LIMIT 1")->row; 139 140 if (isset($row['order_id']) && !empty($row['order_id'])) { 141 return $row['order_id']; 142 } 143 144 return ''; 145 } 146 147 public function getOrderStatus($order_id) { 148 $row = $this->db->query("SELECT `order_status_id` FROM `" . DB_PREFIX . "order` WHERE `order_id` = " . (int)$order_id)->row; 149 150 if (isset($row['order_status_id']) && !empty($row['order_status_id'])) { 151 return (int)$row['order_status_id']; 152 } 153 154 return 0; 155 } 156 157 public function getAmazonusOrderId($order_id) { 158 $row = $this->db->query("SELECT `amazonus_order_id` FROM `" . DB_PREFIX . "amazonus_order` WHERE `order_id` = " . (int)$order_id . " LIMIT 1")->row; 159 160 if (isset($row['amazonus_order_id']) && !empty($row['amazonus_order_id'])) { 161 return $row['amazonus_order_id']; 162 } 163 164 return null; 165 } 166 167 public function getProductOptionsByVar($product_var) { 168 $options = array(); 169 170 $option_value_ids = explode(':', $product_var); 171 foreach ($option_value_ids as $option_value_id) { 172 $option_details_row = $this->db->query("SELECT 173 pov.product_option_id, 174 pov.product_option_value_id, 175 od.name, 176 ovd.name as value, 177 opt.type 178 FROM `" . DB_PREFIX . "product_option_value` as pov, 179 `" . DB_PREFIX . "product_option` as po, 180 `" . DB_PREFIX . "option` as opt, 181 `" . DB_PREFIX . "option_value_description` as ovd, 182 `" . DB_PREFIX . "option_description` as od 183 WHERE pov.product_option_value_id = '" . (int)$option_value_id . "' AND 184 po.product_option_id = pov.product_option_id AND 185 opt.option_id = pov.option_id AND 186 ovd.option_value_id = pov.option_value_id AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND 187 od.option_id = pov.option_id AND od.language_id = '" . (int)$this->config->get('config_language_id') . "' 188 ")->row; 189 190 if (!empty($option_details_row)) { 191 $options[] = array( 192 'product_option_id' => (int)$option_details_row['product_option_id'], 193 'product_option_value_id' => (int)$option_details_row['product_option_value_id'], 194 'name' => $option_details_row['name'], 195 'value' => $option_details_row['value'], 196 'type' => $option_details_row['type'] 197 ); 198 } 199 } 200 201 return $options; 202 } 203 204 public function addOrderHistory($order_id) { 205 $logger = new Log('amazonus_stocks.log'); 206 $logger->write('addOrderHistory() called with order id: ' . $order_id); 207 208 if ($this->config->get('openbay_amazonus_status') != 1) { 209 $logger->write('addOrderHistory() openbay_amazonus_status is disabled'); 210 return; 211 } 212 213 $order_products = $this->openbay->getOrderProducts($order_id); 214 215 $logger->write($order_products); 216 217 if (!empty($order_products)) { 218 foreach ($order_products as $order_product) { 219 $this->openbay->amazonus->productUpdateListen($order_product['product_id']); 220 } 221 } else { 222 $logger->write('Order product array empty!'); 223 } 224 225 $logger->write('addOrder() exiting'); 226 } 227 }