etsy_order.php (14085B)
1 <?php 2 class ModelExtensionOpenBayEtsyOrder extends Model { 3 public function inbound($orders) { 4 $this->openbay->etsy->log("Model inbound, Orders count: " . count($orders)); 5 6 $this->load->model('checkout/order'); 7 $this->load->model('localisation/currency'); 8 9 $this->load->language('extension/openbay/etsy_order'); 10 11 if (!empty($orders)) { 12 foreach ($orders as $order) { 13 $etsy_order = $this->openbay->etsy->orderFind(null, $order->receipt_id); 14 15 if ($etsy_order != false) { 16 $this->openbay->etsy->log("Etsy order found"); 17 18 $order_id = (int)$etsy_order['order_id']; 19 20 if (!$this->lockExists($order_id)) { 21 // paid status changed? 22 if ($order->paid != $etsy_order['paid']) { 23 $this->openbay->etsy->log("Order paid status changed (" . $order->paid . ")"); 24 25 $this->updatePaid($order_id, $order->paid); 26 } 27 28 // shipped status changed? 29 if ($order->shipped != $etsy_order['shipped']) { 30 $this->openbay->etsy->log("Order shipped status changed (" . $order->shipped . ")"); 31 32 if ($order->paid == 1) { 33 $this->openbay->etsy->log("Order is shipped and paid"); 34 $this->updateShipped($order_id, $order->shipped); 35 } else { 36 $this->openbay->etsy->log("Order is not paid so setting to unshipped"); 37 $this->updateShipped($order_id, 0); 38 } 39 } 40 41 $this->lockDelete($order_id); 42 } else { 43 $this->openbay->etsy->log("There is an update lock on this order"); 44 } 45 } else { 46 $this->openbay->etsy->log("Etsy order not found, creating"); 47 48 $order_id = $this->create($order); 49 50 $order_status_id = $this->config->get('etsy_order_status_new'); 51 52 // is paid? 53 if ($order->paid == 1) { 54 $order_status_id = $this->config->get('etsy_order_status_paid'); 55 $this->openbay->etsy->log("Order is paid"); 56 57 $this->updatePaid($order_id, $order->paid); 58 59 // is shipped? 60 if ($order->shipped == 1) { 61 $order_status_id = $this->config->get('etsy_order_status_shipped'); 62 $this->openbay->etsy->log("Order is shipped"); 63 $this->updateShipped($order_id, $order->shipped); 64 } 65 } 66 67 if($this->config->get('openbay_amazon_notify_admin') == 1){ 68 $this->openbay->newOrderAdminNotify($order_id, $order_status_id); 69 } 70 71 $this->openbay->etsy->log('Created Order ID: ' . $order_id); 72 } 73 } 74 } 75 } 76 77 public function updateOrderStatus($order_id, $status_id) { 78 $this->openbay->etsy->log("Model updateOrderStatus Order ID: " . $order_id . ", Status ID: " . $status_id); 79 80 $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())"); 81 82 $this->db->query("UPDATE `" . DB_PREFIX . "order` SET `order_status_id` = " . (int)$status_id . " WHERE `order_id` = " . (int)$order_id); 83 } 84 85 public function updatePaid($order_id, $status) { 86 $this->openbay->etsy->log("Model updatePaid Order ID: " . $order_id . ", Status: " . $status); 87 88 if ($status == 1) { 89 $this->updateOrderStatus($order_id, $this->config->get('etsy_order_status_paid')); 90 } 91 92 $this->db->query("UPDATE `" . DB_PREFIX . "etsy_order` SET `paid` = " . (int)$status . " WHERE `order_id` = " . (int)$order_id); 93 } 94 95 public function updateShipped($order_id, $status) { 96 $this->openbay->etsy->log("Model updateShipped Order ID: " . $order_id . ", Status: " . $status); 97 98 if ($status == 1) { 99 $this->updateOrderStatus($order_id, $this->config->get('etsy_order_status_shipped')); 100 } 101 102 $this->db->query("UPDATE `" . DB_PREFIX . "etsy_order` SET `shipped` = " . (int)$status . " WHERE `order_id` = " . (int)$order_id); 103 } 104 105 public function modifyStock($product_id, $qty, $symbol = '-') { 106 $this->openbay->etsy->log("Model modifyStock Product ID: " . $product_id . ", Qty: " . $qty . ", Symbol: " . $symbol); 107 108 $this->db->query("UPDATE `" . DB_PREFIX . "product` SET `quantity` = (`quantity` " . $this->db->escape((string)$symbol) . " " . (int)$qty . ") WHERE `product_id` = '" . (int)$product_id . "' AND `subtract` = '1'"); 109 } 110 111 private function lockAdd($order_id) { 112 $this->openbay->etsy->log("Model lockAdd Order ID: " . $order_id); 113 $this->db->query("INSERT INTO`" . DB_PREFIX . "etsy_order_lock` SET `order_id` = '" . (int)$order_id . "'"); 114 } 115 116 private function lockDelete($order_id) { 117 $this->openbay->etsy->log("Model lockDelete Order ID: " . $order_id); 118 $this->db->query("DELETE FROM `" . DB_PREFIX . "etsy_order_lock` WHERE `order_id` = '" . (int)$order_id . "'"); 119 } 120 121 private function lockExists($order_id) { 122 $this->openbay->etsy->log("Model lockExists Order ID: " . $order_id); 123 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "etsy_order_lock` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1"); 124 125 if ($query->num_rows > 0) { 126 $this->openbay->etsy->log("Yes"); 127 return true; 128 } else { 129 $this->openbay->etsy->log("No"); 130 $this->lockAdd($order_id); 131 return false; 132 } 133 } 134 135 private function create($order) { 136 $currency_code = (string)$order->transactions[0]->currency; 137 $currency = $this->model_localisation_currency->getCurrencyByCode($currency_code); 138 139 $customer_name = $this->openbay->splitName($order->name); 140 141 if (!empty($order->country->iso)){ 142 $country_qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE `iso_code_2` = '" . $this->db->escape($order->country->iso) . "'"); 143 } 144 145 if (!empty($country_qry->num_rows)){ 146 $country_name = $country_qry->row['name']; 147 $country_id = $country_qry->row['country_id']; 148 $zone_id = $this->openbay->getZoneId($order->address_state, $country_id); 149 $country_address_format = $country_qry->row['address_format']; 150 } else { 151 $country_name = (string)$order->country->name; 152 $country_id = ''; 153 $zone_id = ''; 154 $country_address_format = $this->config->get('etsy_address_format'); 155 } 156 157 $this->db->query("INSERT INTO `" . DB_PREFIX . "order` SET 158 `invoice_prefix` = '" . $this->db->escape($this->config->get('config_invoice_prefix')) . "', 159 `store_id` = '" . (int)$this->config->get('config_store_id') . "', 160 `store_name` = '" . $this->db->escape($this->config->get('config_name') . ' / Etsy') . "', 161 `store_url` = '" . $this->db->escape($this->config->get('config_url')) . "', 162 `customer_id` = 0, 163 `customer_group_id` = '" . (int)$this->config->get('config_customer_group_id') . "', 164 `firstname` = '" . $this->db->escape((string)$customer_name['firstname']) . "', 165 `lastname` = '" . $this->db->escape((string)$customer_name['surname']) . "', 166 `email` = '" . $this->db->escape((string)$order->buyer_email) . "', 167 `telephone` = '', 168 `payment_firstname` = '" . $this->db->escape((string)$customer_name['firstname']) . "', 169 `payment_lastname` = '" . $this->db->escape((string)$customer_name['surname']) . "', 170 `payment_company` = '', 171 `payment_address_1` = '" . $this->db->escape((string)$order->address_1) . "', 172 `payment_address_2` = '" . $this->db->escape((string)$order->address_2) . "', 173 `payment_city` = '" . $this->db->escape((string)$order->address_city) . "', 174 `payment_postcode` = '" . $this->db->escape((string)$order->address_zip) . "', 175 `payment_country` = '" . $this->db->escape((string)$country_name) . "', 176 `payment_country_id` = '" . $this->db->escape((string)$country_id) . "', 177 `payment_zone` = '" . $this->db->escape((string)$order->address_state) . "', 178 `payment_zone_id` = '" . (int)$zone_id . "', 179 `payment_address_format` = '" . $this->db->escape((string)$country_address_format) . "', 180 `payment_method` = '" . $this->db->escape((string)$order->payment_method_name) . "', 181 `payment_code` = '', 182 `shipping_firstname` = '" . $this->db->escape((string)$customer_name['firstname']) . "', 183 `shipping_lastname` = '" . $this->db->escape((string)$customer_name['surname']) . "', 184 `shipping_address_1` = '" . $this->db->escape((string)$order->address_1) . "', 185 `shipping_address_2` = '" . $this->db->escape((string)$order->address_2) . "', 186 `shipping_city` = '" . $this->db->escape((string)$order->address_city) . "', 187 `shipping_postcode` = '" . $this->db->escape((string)$order->address_zip) . "', 188 `shipping_country` = '" . $this->db->escape((string)$country_name) . "', 189 `shipping_country_id` = '" . $this->db->escape((string)$country_id) . "', 190 `shipping_zone` = '" . $this->db->escape((string)$order->address_state) . "', 191 `shipping_zone_id` = '" . (int)$zone_id . "', 192 `shipping_address_format` = '" . $this->db->escape((string)$country_address_format) . "', 193 `shipping_method` = '" . $this->db->escape((string)$order->shipping_method_name) . "', 194 `shipping_code` = '', 195 `comment` = '" . $this->db->escape((string)$order->buyer_note) . "', 196 `total` = '" . (double)$order->amount_total . "', 197 `order_status_id` = '', 198 `affiliate_id` = '', 199 `commission` = '', 200 `marketing_id` = '', 201 `tracking` = '', 202 `language_id` = '" . (int)$this->config->get('config_language_id') . "', 203 `currency_id` = '" . (int)$currency['currency_id'] . "', 204 `currency_code` = '" . $this->db->escape($currency_code) . "', 205 `currency_value` = 1, 206 `ip` = '', 207 `forwarded_ip` = '', 208 `user_agent` = '', 209 `accept_language` = '', 210 `date_added` = NOW(), 211 `date_modified` = NOW() 212 "); 213 214 $order_id = $this->db->getLastId(); 215 216 $this->openbay->etsy->log("Model create(order), New Order ID: " . $order_id); 217 218 foreach ($order->transactions as $transaction) { 219 $this->openbay->etsy->log("Listing ID: " . $transaction->etsy_listing_id); 220 221 $product = $this->openbay->etsy->getLinkedProduct($transaction->etsy_listing_id); 222 223 if ($product != false) { 224 $this->openbay->etsy->log("Linked to product ID: " . $product['product_id']); 225 $product_id = $product['product_id']; 226 $product_model = $product['model']; 227 } else { 228 $this->openbay->etsy->log("Item not linked to product"); 229 $product_id = 0; 230 $product_model = ''; 231 } 232 233 $this->db->query("INSERT INTO `" . DB_PREFIX . "order_product` SET 234 `order_id` = '" . (int)$order_id . "', 235 `product_id` = '" . (int)$product_id . "', 236 `name` = '" . $this->db->escape((string)$transaction->title) . "', 237 `model` = '" . $this->db->escape($product_model) . "', 238 `quantity` = '" . (int)$transaction->quantity . "', 239 `price` = '" . (int)$transaction->price . "', 240 `total` = '" . (int)$transaction->price * (int)$transaction->quantity . "', 241 `tax` = '', 242 `reward` = '' 243 "); 244 245 if ($product_id != 0) { 246 $this->modifyStock($product_id, (int)$transaction->quantity); 247 } 248 } 249 250 $this->db->query("INSERT INTO `" . DB_PREFIX . "etsy_order` SET `order_id` = '" . (int)$order_id . "', `receipt_id` = '" . (int)$order->receipt_id . "'"); 251 252 $totals = array(); 253 254 $totals[0] = array( 255 'code' => 'sub_total', 256 'title' => $this->language->get('text_total_sub'), 257 'value' => number_format($order->price_total, 4, '.', ''), 258 'sort_order' => '1' 259 ); 260 261 $totals[1] = array( 262 'code' => 'shipping', 263 'title' => $this->language->get('text_total_shipping'), 264 'value' => number_format($order->price_shipping, 4, '.', ''), 265 'sort_order' => '3' 266 ); 267 268 if ($order->amount_discount != 0.00) { 269 $totals[2] = array( 270 'code' => 'coupon', 271 'title' => $this->language->get('text_total_discount'), 272 'value' => number_format($order->amount_discount, 4, '.', ''), 273 'sort_order' => '4' 274 ); 275 } 276 277 $totals[3] = array( 278 'code' => 'tax', 279 'title' => $this->language->get('text_total_tax'), 280 'value' => number_format($order->price_tax, 3, '.', ''), 281 'sort_order' => '5' 282 ); 283 284 $totals[4] = array( 285 'code' => 'total', 286 'title' => $this->language->get('text_total'), 287 'value' => $order->amount_total, 288 'sort_order' => '6' 289 ); 290 291 foreach ($totals as $total) { 292 $this->db->query("INSERT INTO `" . DB_PREFIX . "order_total` SET `order_id` = '" . (int)$order_id . "', `code` = '" . $this->db->escape($total['code']) . "', `title` = '" . $this->db->escape($total['title']) . "', `value` = '" . (double)$total['value'] . "', `sort_order` = '" . (int)$total['sort_order'] . "'"); 293 } 294 295 $this->openbay->etsy->log("Setting order to new order status ID: " . $this->config->get('etsy_order_status_new')); 296 297 $this->updateOrderStatus($order_id, $this->config->get('etsy_order_status_new')); 298 299 $this->event->trigger('model/checkout/order/addOrderHistory/after', array('model/checkout/order/addOrderHistory/after', array($order_id, $this->config->get('etsy_order_status_new')))); 300 301 return $order_id; 302 } 303 304 public function addOrderHistory($order_id) { 305 $this->openbay->etsy->log("Model addOrderHistory, Order ID: " . $order_id); 306 307 if(!$this->openbay->etsy->orderFind($order_id)) { 308 $order_products = $this->openbay->getOrderProducts($order_id); 309 310 foreach ($order_products as $order_product) { 311 $this->openbay->etsy->log("Model addOrderHistory - Product ID: " . $order_product['product_id']); 312 313 $this->openbay->etsy->productUpdateListen($order_product['product_id']); 314 } 315 } else { 316 $this->openbay->etsy->log("Model addOrderHistory - Etsy order"); 317 } 318 } 319 }