ebay_order.php (54854B)
1 <?php 2 class ModelExtensionOpenBayEbayOrder extends Model{ 3 public function importOrders($data) { 4 $this->default_shipped_id = $this->config->get('ebay_status_shipped_id'); 5 $this->default_paid_id = $this->config->get('ebay_status_paid_id'); 6 $this->default_refunded_id = $this->config->get('ebay_status_refunded_id'); 7 $this->default_pending_id = $this->config->get('ebay_status_import_id'); 8 9 $this->default_part_refunded_id = $this->config->get('ebay_status_partial_refund_id'); 10 if ($this->default_part_refunded_id == null) { 11 $this->default_part_refunded_id = $this->default_paid_id; 12 } 13 14 $this->tax = ($this->config->get('ebay_tax') == '') ? '1' : (($this->config->get('ebay_tax') / 100) + 1); 15 $this->tax_type = $this->config->get('ebay_tax_listing'); 16 $data = unserialize($data); 17 18 if (isset($data->ordersV2)) { 19 if (!empty($data->ordersV2)) { 20 if (is_array($data->ordersV2)) { 21 foreach ($data->ordersV2 as $order) { 22 if (isset($order->smpId) && (int)$order->smpId != 0) { 23 $this->orderHandle($order); 24 } 25 } 26 } else { 27 if (isset($data->ordersV2->smpId) && (int)$data->ordersV2->smpId != 0) { 28 $this->orderHandle($data->ordersV2); 29 } 30 } 31 } else { 32 $this->openbay->ebay->log('Order object empty - no orders'); 33 } 34 } else { 35 $this->openbay->ebay->log('Data failed to unserialize'); 36 } 37 } 38 39 public function orderHandle($order) { 40 $this->load->model('checkout/order'); 41 $this->load->model('extension/openbay/ebay_order'); 42 43 $this->load->language('extension/openbay/ebay_order'); 44 45 if ($this->lockExists($order->smpId) == true) { 46 return; 47 } 48 49 if (!is_array($order->txn)) { 50 $order->txn = array($order->txn); 51 } 52 53 $ebay_order = $this->openbay->ebay->getOrderBySmpId($order->smpId); 54 55 if (isset($ebay_order['order_id'])) { 56 $order_id = $ebay_order['order_id']; 57 } else { 58 $order_id = false; 59 } 60 61 $created_hours = (int)$this->config->get('ebay_created_hours'); 62 if ($created_hours == 0 || $created_hours == '') { 63 $created_hours = 24; 64 } 65 66 $from = date("Y-m-d H:i:00", mktime(date("H")-(int)$created_hours, date("i"), date("s"), date("m"), date("d"), date("y"))); 67 $this->openbay->ebay->log('Accepting orders newer than: ' . $from); 68 69 if ($order_id != false) { 70 $order_loaded = $this->model_checkout_order->getOrder($order_id); 71 $order_history = $this->getHistory($order_id); 72 73 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> Updating'); 74 75 /* check user details to see if we have now been passed the user info */ 76 /* if we have these details then we have the rest of the delivery info */ 77 if (!empty($order->address->name) && !empty($order->address->street1)) { 78 $this->openbay->ebay->log('User info found'); 79 if ($this->hasAddress($order_id) == false) { 80 $this->updateOrderWithConfirmedData($order_id, $order); 81 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> Updated with user info'); 82 } 83 } else { 84 $this->openbay->ebay->log('No user info'); 85 } 86 87 if ($order->shipping->status == 'Shipped' && ($order_loaded['order_status_id'] != $this->default_shipped_id) && $order->payment->status == 'Paid') { 88 $this->update($order_id, $this->default_shipped_id); 89 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> Shipped'); 90 } elseif ($order->payment->status == 'Paid' && isset($order->payment->date) && $order->shipping->status != 'Shipped' && ($order_loaded['order_status_id'] != $this->default_paid_id)) { 91 $this->update($order_id, $this->default_paid_id); 92 $this->updatePaymentDetails($order_id, $order); 93 94 if ($this->config->get('ebay_stock_allocate') == 1) { 95 $this->openbay->ebay->log('Stock allocation is set to allocate stock when an order is paid'); 96 $this->addOrderLines($order, $order_id); 97 98 $this->event->trigger('model/checkout/order/addOrderHistory/after', array('model/checkout/order/addOrderHistory/after', array($order_id, $this->default_paid_id))); 99 } 100 101 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> Paid'); 102 } elseif (($order->payment->status == 'Refunded' || $order->payment->status == 'Unpaid') && ($order_loaded['order_status_id'] != $this->default_refunded_id) && in_array($this->default_paid_id, $order_history)) { 103 $this->update($order_id, $this->default_refunded_id); 104 $this->cancel($order_id); 105 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> Refunded'); 106 107 $this->event->trigger('model/checkout/order/addOrderHistory/after', array('model/checkout/order/addOrderHistory/after', array($order_id, $this->default_refunded_id))); 108 } elseif ($order->payment->status == 'Part-Refunded' && ($order_loaded['order_status_id'] != $this->default_part_refunded_id) && in_array($this->default_paid_id, $order_history)) { 109 $this->update($order_id, $this->default_part_refunded_id); 110 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> Part Refunded'); 111 } else { 112 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> No Update'); 113 } 114 } else { 115 $this->openbay->ebay->log('Created: ' . $order->order->created); 116 117 if (isset($order->order->checkoutstatus)) { 118 $this->openbay->ebay->log('Checkout: ' . $order->order->checkoutstatus); 119 } 120 if (isset($order->payment->date)) { 121 $this->openbay->ebay->log('Paid date: ' . $order->payment->date); 122 } 123 /** 124 * FOLLOWING ORDER STATE TESTS REQUIRED 125 * 126 * - single item order, not checked out but then marked as paid. i.e. user wants to pay by manual method such as cheque 127 * - multi item order, same as above. Is this possible? i dont think the order will combine if checkout not done. 128 */ 129 130 if ($this->config->get('ebay_import_unpaid') == 1) { 131 $this->openbay->ebay->log('Set to import unpaid orders'); 132 } else { 133 $this->openbay->ebay->log('Ignore unpaid orders'); 134 } 135 136 if (($order->order->created >= $from || (isset($order->payment->date) && $order->payment->date >= $from)) && (isset($order->payment->date) || $this->config->get('ebay_import_unpaid') == 1)) { 137 138 $this->openbay->ebay->log('Creating new order'); 139 140 /* need to create the order without creating customer etc */ 141 $order_id = $this->create($order); 142 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> Created . '); 143 144 /* add link */ 145 $this->orderLinkCreate((int)$order_id, (int)$order->smpId); 146 147 /* check user details to see if we have now been passed the user info, if we have these details then we have the rest of the delivery info */ 148 if (!empty($order->address->name) && !empty($order->address->street1)) { 149 $this->openbay->ebay->log('User info found . '); 150 if ($this->hasAddress($order_id) == false) { 151 $this->updateOrderWithConfirmedData($order_id, $order); 152 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> Updated with user info . '); 153 } 154 } else { 155 $this->openbay->ebay->log('No user information.'); 156 } 157 158 $default_import_message = $this->language->get('text_smp_id') . (int)$order->smpId . "\r\n"; 159 $default_import_message .= $this->language->get('text_buyer') . (string)$order->user->userid . "\r\n"; 160 161 //new order, set to pending initially. 162 $this->confirm($order_id, $this->default_pending_id, $default_import_message); 163 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> Pending'); 164 $order_status_id = $this->default_pending_id; 165 166 //order has been paid 167 if ($order->payment->status == 'Paid') { 168 $this->update($order_id, $this->default_paid_id); 169 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> Paid'); 170 $order_status_id = $this->default_paid_id; 171 172 if ($this->config->get('ebay_stock_allocate') == 1) { 173 $this->openbay->ebay->log('Stock allocation is set to allocate stock when an order is paid'); 174 175 $this->addOrderLines($order, $order_id); 176 } 177 } 178 179 //order has been refunded 180 if ($order->payment->status == 'Refunded') { 181 $this->update($order_id, $this->default_refunded_id); 182 $this->cancel($order_id); 183 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> Refunded'); 184 $order_status_id = $this->default_refunded_id; 185 186 $this->event->trigger('model/checkout/order/addOrderHistory/after', array('model/checkout/order/addOrderHistory/after', array($order_id, $order_status_id))); 187 } 188 189 //order is part refunded 190 if ($order->payment->status == 'Part-Refunded') { 191 $this->update($order_id, $this->default_part_refunded_id); 192 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> Part Refunded'); 193 $order_status_id = $this->default_part_refunded_id; 194 } 195 196 //order payment is clearing 197 if ($order->payment->status == 'Clearing') { 198 $this->update($order_id, $this->default_pending_id); 199 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> Clearing'); 200 $order_status_id = $this->default_pending_id; 201 } 202 203 //order is marked shipped 204 if ($order->shipping->status == 'Shipped') { 205 $this->update($order_id, $this->default_shipped_id); 206 $this->openbay->ebay->log('Order ID: ' . $order_id . ' -> Shipped'); 207 $order_status_id = $this->default_shipped_id; 208 } 209 210 // Admin Alert Mail 211 if ($this->config->get('ebay_confirmadmin_notify') == 1) { 212 $this->openbay->newOrderAdminNotify($order_id, $order_status_id); 213 } 214 } 215 216 if ($this->config->get('ebay_stock_allocate') == 0) { 217 $this->openbay->ebay->log('Stock allocation is set to allocate stock when an item is bought'); 218 $this->addOrderLines($order, $order_id); 219 220 $this->event->trigger('model/checkout/order/addOrderHistory/after', array('model/checkout/order/addOrderHistory/after', array($order_id, $order_status_id))); 221 } 222 } 223 224 if (!empty($order->cancelled)) { 225 $this->openbay->ebay->log('There are cancelled items in the order'); 226 $this->removeOrderLines($order->cancelled, $order_id); 227 } 228 229 //remove the lock. 230 $this->lockDelete($order->smpId); 231 } 232 233 private function create($order) { 234 if ($this->openbay->addonLoad('openstock')) { 235 $openstock = true; 236 } else { 237 $openstock = false; 238 } 239 240 $this->load->model('localisation/currency'); 241 $this->load->model('catalog/product'); 242 243 if (isset($order->order->currency_id) && !empty($order->order->currency_id)) { 244 $currency = $this->model_localisation_currency->getCurrencyByCode($order->order->currency_id); 245 } 246 247 if (empty($currency)) { 248 $currency = $this->model_localisation_currency->getCurrencyByCode($this->config->get('ebay_def_currency')); 249 } 250 251 if ($this->config->get('ebay_create_date') == 1) { 252 $created_date_obj = new DateTime((string)$order->order->created); 253 $offset = ($this->config->get('ebay_time_offset') != '') ? (int)$this->config->get('ebay_time_offset') : (int)0; 254 $created_date_obj->modify($offset . ' hour'); 255 $created_date = $created_date_obj->format('Y-m-d H:i:s'); 256 } else { 257 $created_date = date("Y-m-d H:i:s"); 258 $offset = 0; 259 } 260 261 $this->openbay->ebay->log('create() - Order date: ' . $created_date); 262 $this->openbay->ebay->log('create() - Original date: ' . (string)$order->order->created); 263 $this->openbay->ebay->log('create() - Offset: ' . $offset); 264 $this->openbay->ebay->log('create() - Server time: ' . date("Y-m-d H:i:s")); 265 266 $this->db->query("INSERT INTO `" . DB_PREFIX . "order` SET 267 `store_id` = '" . (int)$this->config->get('config_store_id') . "', 268 `store_name` = '" . $this->db->escape($this->config->get('config_name') . ' / eBay') . "', 269 `store_url` = '" . $this->db->escape($this->config->get('config_url')) . "', 270 `invoice_prefix` = '" . $this->db->escape($this->config->get('config_invoice_prefix')) . "', 271 `comment` = '" . $this->db->escape((string)$order->order->message) . "', 272 `total` = '" . (double)$order->order->total . "', 273 `affiliate_id` = '0', 274 `commission` = '0', 275 `language_id` = '" . (int)$this->config->get('config_language_id') . "', 276 `currency_id` = '" . (int)$currency['currency_id'] . "', 277 `currency_code` = '" . $this->db->escape($currency['code']) . "', 278 `currency_value` = '" . (double)$currency['value'] . "', 279 `ip` = '', 280 `date_added` = '" . $this->db->escape($created_date) . "', 281 `date_modified` = NOW(), 282 `customer_id` = 0 283 "); 284 285 $order_id = $this->db->getLastId(); 286 287 foreach ($order->txn as $txn) { 288 $product_id = $this->openbay->ebay->getProductId($txn->item->id); 289 290 if ($product_id != false) { 291 $this->openbay->ebay->log('create() - Product ID: "' . $product_id . '" from ebay item: ' . $txn->item->id . ' was returned'); 292 293 if (!empty($txn->item->variantsku) && $openstock == true) { 294 $model_number = $this->openbay->getProductModelNumber($product_id, $txn->item->variantsku); 295 } else { 296 $model_number = $this->openbay->getProductModelNumber($product_id); 297 } 298 } else { 299 $this->openbay->ebay->log('create() - No product ID from ebay item: ' . $txn->item->id . ' was returned'); 300 $model_number = ''; 301 } 302 303 $qty = (int)$txn->item->qty; 304 $price = (double)$txn->item->price; 305 $this->openbay->ebay->log('create() - Item price: ' . $price); 306 307 if ($this->tax_type == 1) { 308 //calculate taxes that come in from eBay 309 $this->openbay->ebay->log('create() - Using tax rates from eBay'); 310 311 $price_net = $price; 312 $this->openbay->ebay->log('create() - Net price: ' . $price_net); 313 314 $total_net = $price * $qty; 315 $this->openbay->ebay->log('create() - Total net price: ' . $total_net); 316 317 $tax = number_format((double)$txn->item->tax->item, 4, '.', ''); 318 $this->openbay->ebay->log('create() - Tax: ' . $tax); 319 } else { 320 //use the store pre-set tax-rate for everything 321 $this->openbay->ebay->log('create() - Using tax rates from store'); 322 323 $price_net = $price / $this->tax; 324 $this->openbay->ebay->log('create() - Net price: ' . $price_net); 325 326 $total_net = $price_net * $qty; 327 $this->openbay->ebay->log('create() - Total net price: ' . $total_net); 328 329 $tax = number_format(($price - $price_net), 4, '.', ''); 330 $this->openbay->ebay->log('create() - Tax: ' . $tax); 331 } 332 333 $txn->item->name = stripslashes($txn->item->name); 334 $txn->item->varianttitle = stripslashes($txn->item->varianttitle); 335 $txn->item->sku = stripslashes($txn->item->sku); 336 $txn->item->variantsku = stripslashes($txn->item->variantsku); 337 338 if (isset($txn->item->varianttitle) && !empty($txn->item->varianttitle)) { 339 $order_product_name = $txn->item->varianttitle; 340 } else { 341 if ($txn->item->sku != '') { 342 $order_product_name = $txn->item->name . ' [' . $txn->item->sku . ']'; 343 } else { 344 $order_product_name = $txn->item->name; 345 } 346 } 347 348 $this->db->query("INSERT INTO `" . DB_PREFIX . "order_product` SET 349 `order_id` = '" . (int)$order_id . "', 350 `product_id` = '" . (int)$product_id . "', 351 `name` = '" . $this->db->escape($order_product_name) . "', 352 `model` = '" . $this->db->escape($model_number) . "', 353 `quantity` = '" . (int)$qty . "', 354 `price` = '" . (double)$price_net . "', 355 `total` = '" . (double)$total_net . "', 356 `tax` = '" . (double)$tax . "' 357 "); 358 359 $order_product_id = $this->db->getLastId(); 360 361 $this->openbay->ebay->log('create() - Added order product id ' . $order_product_id); 362 363 if ($openstock == true) { 364 $this->openbay->ebay->log('create() - OpenStock enabled'); 365 if (!empty($txn->item->variantsku)) { 366 $this->openbay->ebay->log($txn->item->variantsku); 367 368 if ($product_id != false) { 369 $sku_parts = explode(':', $txn->item->variantsku); 370 $p_options = array(); 371 372 foreach ($sku_parts as $part) { 373 $sql = "SELECT 374 `pv`.`product_option_id`, 375 `pv`.`product_option_value_id`, 376 `od`.`name`, 377 `ovd`.`name` as `value`, 378 `o`.`option_id`, 379 `o`.`type` 380 FROM `" . DB_PREFIX . "product_option_value` `pv` 381 LEFT JOIN `" . DB_PREFIX . "option_value` `ov` ON (`pv`.`option_value_id` = `ov`.`option_value_id`) 382 LEFT JOIN `" . DB_PREFIX . "option_value_description` `ovd` ON (`ovd`.`option_value_id` = `ov`.`option_value_id`) 383 LEFT JOIN `" . DB_PREFIX . "option_description` `od` ON (`ov`.`option_id` = `od`.`option_id`) 384 LEFT JOIN `" . DB_PREFIX . "option` `o` ON (`o`.`option_id` = `od`.`option_id`) 385 WHERE `pv`.`product_option_value_id` = '" . (int)$part . "' 386 AND `pv`.`product_id` = '" . (int)$product_id . "'"; 387 $option_qry = $this->db->query($sql); 388 389 if (!empty($option_qry->row)) { 390 $p_options[] = array( 391 'product_option_id' => $option_qry->row['product_option_id'], 392 'product_option_value_id' => $option_qry->row['product_option_value_id'], 393 'name' => $option_qry->row['name'], 394 'value' => $option_qry->row['value'], 395 'type' => $option_qry->row['type'], 396 ); 397 } 398 } 399 400 //insert into order_option table 401 foreach ($p_options as $option) { 402 $this->db->query(" 403 INSERT INTO `" . DB_PREFIX . "order_option` 404 SET 405 `order_id` = '" . (int)$order_id . "', 406 `order_product_id` = '" . (int)$order_product_id . "', 407 `product_option_id` = '" . (int)$option['product_option_id'] . "', 408 `product_option_value_id` = '" . (int)$option['product_option_value_id'] . "', 409 `name` = '" . $this->db->escape($option['name']) . "', 410 `value` = '" . $this->db->escape($option['value']) . "', 411 `type` = '" . $this->db->escape($option['type']) . "' 412 "); 413 } 414 } 415 } else { 416 $this->openbay->ebay->log('create() - No variant sku'); 417 } 418 } 419 } 420 421 return $order_id; 422 } 423 424 private function updateOrderWithConfirmedData($order_id, $order) { 425 $this->load->model('localisation/currency'); 426 $this->load->model('catalog/product'); 427 $totals_language = $this->load->language('extension/openbay/ebay_order'); 428 429 $name_parts = $this->openbay->splitName((string)$order->address->name); 430 $user = array(); 431 $user['fname'] = $name_parts['firstname']; 432 $user['lname'] = $name_parts['surname']; 433 434 /** get the iso2 code from the data and pull out the correct country for the details. */ 435 if (!empty($order->address->iso2)) { 436 $country_qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE `iso_code_2` = '" . $this->db->escape($order->address->iso2) . "'"); 437 } 438 439 if (!empty($country_qry->num_rows)) { 440 $user['country'] = $country_qry->row['name']; 441 $user['country_id'] = $country_qry->row['country_id']; 442 } else { 443 $user['country'] = (string)$order->address->iso2; 444 $user['country_id'] = ''; 445 } 446 447 $user['email'] = (string)$order->user->email; 448 $user['id'] = $this->openbay->getUserByEmail($user['email']); 449 450 $currency = $this->model_localisation_currency->getCurrencyByCode($this->config->get('ebay_def_currency')); 451 $address_format = $this->getCountryAddressFormat((string)$order->address->iso2); 452 453 //try to get zone id - this will only work if the zone name and country id exist in the DB. 454 $zone_id = $this->openbay->getZoneId($order->address->state, $user['country_id']); 455 456 if (empty($address_format)) { 457 $address_format = (string)$this->config->get('ebay_default_addressformat'); 458 } 459 460 //try to get the friendly name for the shipping service 461 $shipping_service = $this->openbay->ebay->getShippingServiceInfo($order->shipping->service); 462 463 if ($shipping_service != false) { 464 $shipping_service_name = $shipping_service['description']; 465 } else { 466 $shipping_service_name = $order->shipping->service; 467 } 468 469 $this->db->query(" 470 UPDATE `" . DB_PREFIX . "order` 471 SET 472 `customer_id` = '" . (int)$user['id'] . "', 473 `firstname` = '" . $this->db->escape($user['fname']) . "', 474 `lastname` = '" . $this->db->escape($user['lname']) . "', 475 `email` = '" . $this->db->escape($order->user->email) . "', 476 `telephone` = '" . $this->db->escape($order->address->phone) . "', 477 `shipping_firstname` = '" . $this->db->escape($user['fname']) . "', 478 `shipping_lastname` = '" . $this->db->escape($user['lname']) . "', 479 `shipping_address_1` = '" . $this->db->escape($order->address->street1) . "', 480 `shipping_address_2` = '" . $this->db->escape($order->address->street2) . "', 481 `shipping_city` = '" . $this->db->escape($order->address->city) . "', 482 `shipping_postcode` = '" . $this->db->escape($order->address->postcode) . "', 483 `shipping_country` = '" . $this->db->escape($user['country']) . "', 484 `shipping_country_id` = '" . (int)$user['country_id'] . "', 485 `shipping_zone` = '" . $this->db->escape($order->address->state) . "', 486 `shipping_zone_id` = '" . (int)$zone_id . "', 487 `shipping_method` = '" . $this->db->escape($shipping_service_name) . "', 488 `shipping_address_format` = '" . $this->db->escape($address_format) . "', 489 `payment_firstname` = '" . $this->db->escape($user['fname']) . "', 490 `payment_lastname` = '" . $this->db->escape($user['lname']) . "', 491 `payment_address_1` = '" . $this->db->escape($order->address->street1) . "', 492 `payment_address_2` = '" . $this->db->escape($order->address->street2) . "', 493 `payment_city` = '" . $this->db->escape($order->address->city) . "', 494 `payment_postcode` = '" . $this->db->escape($order->address->postcode) . "', 495 `payment_country` = '" . $this->db->escape($user['country']) . "', 496 `payment_country_id` = '" . (int)$user['country_id'] . "', 497 `payment_zone` = '" . $this->db->escape($order->address->state) . "', 498 `payment_zone_id` = '" . (int)$zone_id . "', 499 `comment` = '" . $this->db->escape($order->order->message) . "', 500 `payment_method` = '" . $this->db->escape($order->payment->method) . "', 501 `payment_address_format` = '" . $address_format . "', 502 `total` = '" . (double)$order->order->total . "', 503 `date_modified` = NOW() 504 WHERE `order_id` = '" . $order_id . "' 505 "); 506 507 $total_tax = 0; 508 $total_net = 0; 509 510 /* force array type */ 511 if (!is_array($order->txn)) { 512 $order->txn = array($order->txn); 513 } 514 515 foreach ($order->txn as $txn) { 516 $qty = (int)$txn->item->qty; 517 $price = (double)$txn->item->price; 518 519 if ($this->tax_type == 1) { 520 //calculate taxes that come in from eBay 521 $this->openbay->ebay->log('updateOrderWithConfirmedData() - Using tax rates from eBay'); 522 523 $total_tax += (double)$txn->item->tax->total; 524 $total_net += $price * $qty; 525 } else { 526 //use the store pre-set tax-rate for everything 527 $this->openbay->ebay->log('updateOrderWithConfirmedData() - Using tax rates from store'); 528 529 $item_net = $price / $this->tax; 530 $item_tax = $price - $item_net; 531 $line_net = $item_net * $qty; 532 $line_tax = $item_tax * $qty; 533 534 $total_tax += number_format($line_tax, 4, '.', ''); 535 $total_net += $line_net; 536 } 537 } 538 539 if ($this->tax_type == 1) { 540 $discount_net = (double)$order->order->discount; 541 $shipping_net = (double)$order->shipping->cost; 542 543 $tax = number_format($total_tax, 4, '.', ''); 544 } else { 545 $discount_net = (double)$order->order->discount / $this->tax; 546 $discount_tax = (double)$order->order->discount - $discount_net; 547 $shipping_net = (double)$order->shipping->cost / $this->tax; 548 $shipping_tax = (double)$order->shipping->cost - $shipping_net; 549 550 $tax = number_format($shipping_tax + $total_tax + $discount_tax, 4, '.', ''); 551 } 552 553 $totals = number_format((double)$total_net + (double)$shipping_net + (double)$tax + (double)$discount_net, 4, '.', ''); 554 555 $data = array(); 556 557 $data['totals'][0] = array( 558 'code' => 'sub_total', 559 'title' => $totals_language['text_total_sub'], 560 'value' => number_format((double)$total_net, 4, '.', ''), 561 'sort_order' => '1' 562 ); 563 564 $data['totals'][1] = array( 565 'code' => 'shipping', 566 'title' => $totals_language['text_total_shipping'], 567 'value' => number_format((double)$shipping_net, 4, '.', ''), 568 'sort_order' => '3' 569 ); 570 571 if ($discount_net != 0.00) { 572 $data['totals'][2] = array( 573 'code' => 'coupon', 574 'title' => $totals_language['text_total_discount'], 575 'value' => number_format((double)$discount_net, 4, '.', ''), 576 'sort_order' => '4' 577 ); 578 } 579 580 $data['totals'][3] = array( 581 'code' => 'tax', 582 'title' => $totals_language['text_total_tax'], 583 'value' => number_format((double)$tax, 3, '.', ''), 584 'sort_order' => '5' 585 ); 586 587 $data['totals'][4] = array( 588 'code' => 'total', 589 'title' => $totals_language['text_total'], 590 'value' => $totals, 591 'sort_order' => '6' 592 ); 593 594 foreach ($data['totals'] as $total) { 595 $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'] . "'"); 596 } 597 } 598 599 public function addOrderLine($data, $order_id, $created) { 600 $order_line = $this->getOrderLine($data['txn_id'], $data['item_id']); 601 602 $created_hours = (int)$this->config->get('ebay_created_hours'); 603 if ($created_hours == 0 || $created_hours == '') { 604 $created_hours = 24; 605 } 606 $from = date("Y-m-d H:i:00", mktime(date("H")-$created_hours, date("i"), date("s"), date("m"), date("d"), date("y"))); 607 608 if ($order_line === false) { 609 if ($created >= $from) { 610 $this->openbay->ebay->log('addOrderLine() - New line'); 611 $product_id = $this->openbay->ebay->getProductId($data['item_id']); 612 /* add to the transaction table */ 613 $this->db->query(" 614 INSERT INTO `" . DB_PREFIX . "ebay_transaction` 615 SET 616 `order_id` = '" . (int)$order_id . "', 617 `txn_id` = '" . $this->db->escape($data['txn_id']) . "', 618 `item_id` = '" . $this->db->escape($data['item_id']) . "', 619 `product_id` = '" . (int)$product_id . "', 620 `containing_order_id` = '" . $data['containing_order_id'] . "', 621 `order_line_id` = '" . $this->db->escape($data['order_line_id']) . "', 622 `qty` = '" . (int)$data['qty'] . "', 623 `smp_id` = '" . (int)$data['smp_id'] . "', 624 `sku` = '" . $this->db->escape($data['sku']) . "', 625 `created` = now(), 626 `modified` = now() 627 "); 628 629 if (!empty($product_id)) { 630 $this->openbay->ebay->log('Link found'); 631 $this->modifyStock($product_id, $data['qty'], '-', $data['sku']); 632 } 633 } else { 634 $this->openbay->ebay->log('addOrderLine() - Transaction is older than ' . $this->config->get('ebay_created_hours') . ' hours'); 635 } 636 } else { 637 $this->openbay->ebay->log('addOrderLine() - Line existed'); 638 639 if ($order_id != $order_line['order_id']) { 640 $this->openbay->ebay->log('addOrderLine() - Order ID has changed from "' . $order_line['order_id'] . '" to "' . $order_id . '"'); 641 $this->db->query("UPDATE `" . DB_PREFIX . "ebay_transaction` SET `order_id` = '" . (int)$order_id . "', `modified` = now() WHERE `txn_id` = '" . $this->db->escape((string)$data['txn_id']) . "' AND `item_id` = '" . $this->db->escape((string)$data['item_id']) . "' LIMIT 1"); 642 643 //if the order id has changed then remove the old order details 644 $this->delete($order_line['order_id']); 645 } 646 647 if ($order_line['smp_id'] != $data['smp_id']) { 648 $this->openbay->ebay->log('addOrderLine() - SMP ID for orderLine has changed from "' . $order_line['smp_id'] . '" to "' . $data['smp_id'] . '"'); 649 $this->db->query("UPDATE `" . DB_PREFIX . "ebay_transaction` SET `smp_id` = '" . $data['smp_id'] . "', `modified` = now() WHERE `txn_id` = '" . $this->db->escape((string)$data['txn_id']) . "' AND `item_id` = '" . $this->db->escape((string)$data['item_id']) . "' LIMIT 1"); 650 } 651 652 if ($order_line['containing_order_id'] != $data['containing_order_id']) { 653 $this->openbay->ebay->log('addOrderLine() - Containing order ID for orderLine has changed from "' . $order_line['containing_order_id'] . '" to "' . $data['containing_order_id'] . '"'); 654 $this->db->query("UPDATE `" . DB_PREFIX . "ebay_transaction` SET `containing_order_id` = '" . $this->db->escape($data['containing_order_id']) . "', `modified` = now() WHERE `txn_id` = '" . $this->db->escape((string)$data['txn_id']) . "' AND `item_id` = '" . $this->db->escape((string)$data['item_id']) . "' LIMIT 1"); 655 } 656 } 657 $this->openbay->ebay->log('addOrderLine() - Done'); 658 } 659 660 public function addOrderLines($order, $order_id) { 661 $this->openbay->ebay->log('Adding order lines'); 662 663 foreach ($order->txn as $txn) { 664 $this->model_extension_openbay_ebay_order->addOrderLine(array( 665 'txn_id' => (string)$txn->item->txn, 666 'item_id' => (string)$txn->item->id, 667 'containing_order_id' => (string)$order->order->id, 668 'order_line_id' => (string)$txn->item->line, 669 'qty' => (int)$txn->item->qty, 670 'smp_id' => (string)$order->smp->id, 671 'sku' => (string)$txn->item->variantsku 672 ), (int)$order_id, $order->order->created); 673 } 674 } 675 676 public function getOrderLine($txn_id, $item_id) { 677 $this->openbay->ebay->log('getOrderLine() - Testing for order line txn: ' . $txn_id . ', item: ' . $item_id); 678 $res = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ebay_transaction` WHERE `txn_id` = '" . $this->db->escape($txn_id) . "' AND `item_id` = '" . $this->db->escape($item_id) . "' LIMIT 1"); 679 680 if ($res->num_rows == 0) { 681 return false; 682 } else { 683 return $res->row; 684 } 685 } 686 687 public function getOrderLines($order_id) { 688 $this->openbay->ebay->log('getOrderLines() - Testing for order lines id: ' . $order_id); 689 690 $result = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ebay_transaction` WHERE `order_id` = '" . (int)$order_id . "'"); 691 692 $lines = array(); 693 694 foreach ($result->rows as $line) { 695 $lines[] = $line; 696 } 697 698 return $lines; 699 } 700 701 public function removeOrderLines($canceling) { 702 703 foreach ($canceling as $cancel) { 704 $line = $this->getOrderLine($cancel['txn'], $cancel['id']); 705 706 if ($line === false) { 707 $this->openbay->ebay->log('No line needs cancelling'); 708 } else { 709 $this->openbay->ebay->log('Found order line to cancel'); 710 $this->removeOrderLine($cancel['txn'], $cancel['id'], $line); 711 } 712 } 713 } 714 715 private function removeOrderLine($txn_id, $item_id, $line) { 716 $this->openbay->ebay->log('Removing order line, txn: ' . $txn_id . ',item id: ' . $item_id); 717 718 $this->db->query("DELETE FROM `" . DB_PREFIX . "ebay_transaction` WHERE `txn_id` = '" . $this->db->escape($txn_id) . "' AND `item_id` = '" . $this->db->escape($item_id) . "' LIMIT 1"); 719 720 if ($this->db->countAffected() > 0) { 721 $this->modifyStock($line['product_id'], $line['qty'], '+', $line['sku']); 722 } 723 } 724 725 public function cancel($order_id) { 726 $order_lines = $this->getOrderLines($order_id); 727 728 foreach ($order_lines as $line) { 729 $this->modifyStock($line['product_id'], $line['qty'], '+', $line['sku']); 730 } 731 } 732 733 public function updatePaymentDetails($order_id, $order) { 734 $this->db->query("UPDATE `" . DB_PREFIX . "order` SET `payment_method` = '" . $this->db->escape($order->payment->method) . "', `total` = '" . (double)$order->order->total . "', `date_modified` = NOW() WHERE `order_id` = '" . (int)$order_id . "'"); 735 } 736 737 public function getHistory($order_id) { 738 $this->openbay->ebay->log('Getting order history for ID: ' . $order_id); 739 740 $query = $this->db->query("SELECT `order_status_id` FROM `" . DB_PREFIX . "order_history` WHERE `order_id` = '" . (int)$order_id . "'"); 741 742 $status = array(); 743 744 if ($query->num_rows) { 745 foreach ($query->rows as $row) { 746 $status[] = $row['order_status_id']; 747 } 748 } 749 750 return $status; 751 } 752 753 public function hasAddress($order_id) { 754 // check if the first name, address 1 and country are set 755 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order` WHERE `order_id` = '" . (int)$order_id . "' AND `payment_firstname` != '' AND `payment_address_1` != '' AND `payment_country` != ''"); 756 757 if ($query->num_rows == 0) { 758 return false; 759 } else { 760 return true; 761 } 762 } 763 764 public function update($order_id, $order_status_id, $comment = '') { 765 $order_info = $this->model_checkout_order->getOrder($order_id); 766 767 $notify = $this->config->get('ebay_update_notify'); 768 769 if ($order_info) { 770 $this->db->query("UPDATE `" . DB_PREFIX . "order` SET order_status_id = '" . (int)$order_status_id . "', date_modified = NOW() WHERE order_id = '" . (int)$order_id . "'"); 771 772 $this->db->query("INSERT INTO " . DB_PREFIX . "order_history SET order_id = '" . (int)$order_id . "', order_status_id = '" . (int)$order_status_id . "', notify = '" . (int)$notify . "', comment = '" . $this->db->escape($comment) . "', date_added = NOW()"); 773 774 if ($notify) { 775 if (version_compare(VERSION, '2.2', '>') == true) { 776 $language_code = $order_info['language_code']; 777 } else { 778 $language_code = $order_info['language_directory']; 779 } 780 781 $language = new Language($language_code); 782 $language->load($language_code); 783 $language->load('mail/order'); 784 785 $subject = sprintf($language->get('text_update_subject'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'), $order_id); 786 787 $message = $language->get('text_update_order') . ' ' . $order_id . "\n"; 788 $message .= $language->get('text_update_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n\n"; 789 790 $order_status_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_status WHERE order_status_id = '" . (int)$order_status_id . "' AND language_id = '" . (int)$order_info['language_id'] . "'"); 791 792 if ($order_status_query->num_rows) { 793 $message .= $language->get('text_update_order_status') . "\n\n"; 794 $message .= $order_status_query->row['name'] . "\n\n"; 795 } 796 797 if ($order_info['customer_id']) { 798 $message .= $language->get('text_update_link') . "\n"; 799 $message .= $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id . "\n\n"; 800 } 801 802 if ($comment) { 803 $message .= $language->get('text_update_comment') . "\n\n"; 804 $message .= $comment . "\n\n"; 805 } 806 807 $message .= $language->get('text_update_footer'); 808 809 $message .= "\n\n"; 810 $message .= 'eBay and Amazon order management - http://www.openbaypro.com/'; 811 812 $mail = new Mail($this->config->get('config_mail_engine')); 813 $mail->protocol = $this->config->get('config_mail_protocol'); 814 $mail->parameter = $this->config->get('config_mail_parameter'); 815 $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname'); 816 $mail->smtp_username = $this->config->get('config_mail_smtp_username'); 817 $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'); 818 $mail->smtp_port = $this->config->get('config_mail_smtp_port'); 819 $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout'); 820 821 $mail->setTo($order_info['email']); 822 $mail->setFrom($this->config->get('config_email')); 823 $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')); 824 $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8')); 825 $mail->setText($message); 826 $mail->send(); 827 } 828 } 829 } 830 831 public function confirm($order_id, $order_status_id, $comment = '') { 832 $order_info = $this->model_checkout_order->getOrder($order_id); 833 $notify = $this->config->get('ebay_confirm_notify'); 834 835 if ($order_info && !$order_info['order_status_id']) { 836 $this->db->query("UPDATE `" . DB_PREFIX . "order` SET order_status_id = '" . (int)$order_status_id . "', date_modified = NOW() WHERE order_id = '" . (int)$order_id . "'"); 837 $this->db->query("INSERT INTO " . DB_PREFIX . "order_history SET order_id = '" . (int)$order_id . "', order_status_id = '" . (int)$order_status_id . "', notify = '" . (int)$notify . "', comment = '" . $this->db->escape($comment) . "', date_added = NOW()"); 838 839 if (isset($order_info['email']) && !empty($order_info['email']) && $notify == 1){ 840 $order_product_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "'"); 841 842 $this->cache->delete('product'); 843 844 // Order Totals 845 $order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE `order_id` = '" . (int)$order_id . "' ORDER BY `sort_order` ASC"); 846 847 foreach ($order_total_query->rows as $order_total) { 848 $this->load->model('extension/total/' . $order_total['code']); 849 850 if (property_exists($this->{'model_extension_total_' . $order_total['code']}, 'confirm')) { 851 $this->{'model_extension_total_' . $order_total['code']}->confirm($order_info, $order_total); 852 } 853 } 854 855 // Send out order confirmation mail 856 if (version_compare(VERSION, '2.2', '>') == true) { 857 $language_code = $order_info['language_code']; 858 } else { 859 $language_code = $order_info['language_directory']; 860 } 861 862 $language = new Language($language_code); 863 $language->load($language_code); 864 $language->load('mail/order'); 865 866 $order_status_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_status` WHERE `order_status_id` = '" . (int)$order_status_id . "' AND `language_id` = '" . (int)$order_info['language_id'] . "'"); 867 868 if ($order_status_query->num_rows) { 869 $order_status = $order_status_query->row['name']; 870 } else { 871 $order_status = ''; 872 } 873 874 $subject = sprintf($language->get('text_new_subject'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'), $order_id); 875 876 // HTML Mail 877 $data = array(); 878 879 $data['title'] = sprintf($language->get('text_new_subject'), $order_info['store_name'], $order_id); 880 $data['text_greeting'] = sprintf($language->get('text_new_greeting'), $order_info['store_name']); 881 $data['text_link'] = $language->get('text_new_link'); 882 $data['text_download'] = $language->get('text_new_download'); 883 $data['text_order_detail'] = $language->get('text_new_order_detail'); 884 $data['text_instruction'] = $language->get('text_new_instruction'); 885 $data['text_order_id'] = $language->get('text_new_order_id'); 886 $data['text_date_added'] = $language->get('text_new_date_added'); 887 $data['text_payment_method'] = $language->get('text_new_payment_method'); 888 $data['text_shipping_method'] = $language->get('text_new_shipping_method'); 889 $data['text_email'] = $language->get('text_new_email'); 890 $data['text_telephone'] = $language->get('text_new_telephone'); 891 $data['text_ip'] = $language->get('text_new_ip'); 892 $data['text_order_status'] = $language->get('text_new_order_status'); 893 $data['text_payment_address'] = $language->get('text_new_payment_address'); 894 $data['text_shipping_address'] = $language->get('text_new_shipping_address'); 895 $data['text_product'] = $language->get('text_new_product'); 896 $data['text_model'] = $language->get('text_new_model'); 897 $data['text_quantity'] = $language->get('text_new_quantity'); 898 $data['text_price'] = $language->get('text_new_price'); 899 $data['text_total'] = $language->get('text_new_total'); 900 $data['text_footer'] = $language->get('text_new_footer'); 901 902 if ($this->config->get('ebay_email_brand_disable') == 1) { 903 $data['text_powered'] = ''; 904 } else { 905 $data['text_powered'] = '<a href="http://www.openbaypro.com/">OpenBay Pro - eBay and Amazon order management for OpenCart</a> . '; 906 } 907 908 $data['logo'] = $this->config->get('config_url') . 'image/' . $this->config->get('config_logo'); 909 $data['store_name'] = $order_info['store_name']; 910 $data['store_url'] = $order_info['store_url']; 911 $data['customer_id'] = $order_info['customer_id']; 912 $data['link'] = $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id; 913 914 $data['download'] = ''; 915 916 $data['order_id'] = $order_id; 917 $data['date_added'] = date($language->get('date_format_short'), strtotime($order_info['date_added'])); 918 $data['payment_method'] = $order_info['payment_method']; 919 $data['shipping_method'] = $order_info['shipping_method']; 920 $data['email'] = $order_info['email']; 921 $data['telephone'] = $order_info['telephone']; 922 $data['ip'] = $order_info['ip']; 923 $data['order_status'] = $order_status; 924 925 $data['comment'] = ''; 926 927 if ($comment && $notify) { 928 $data['comment'] = nl2br($comment); 929 } 930 931 if ($order_info['payment_address_format']) { 932 $format = $order_info['payment_address_format']; 933 } else { 934 $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}'; 935 } 936 937 $find = array( 938 '{firstname}', 939 '{lastname}', 940 '{company}', 941 '{address_1}', 942 '{address_2}', 943 '{city}', 944 '{postcode}', 945 '{zone}', 946 '{zone_code}', 947 '{country}' 948 ); 949 950 $replace = array( 951 'firstname' => $order_info['payment_firstname'], 952 'lastname' => $order_info['payment_lastname'], 953 'company' => $order_info['payment_company'], 954 'address_1' => $order_info['payment_address_1'], 955 'address_2' => $order_info['payment_address_2'], 956 'city' => $order_info['payment_city'], 957 'postcode' => $order_info['payment_postcode'], 958 'zone' => $order_info['payment_zone'], 959 'zone_code' => $order_info['payment_zone_code'], 960 'country' => $order_info['payment_country'] 961 ); 962 963 $data['payment_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format)))); 964 965 if ($order_info['shipping_address_format']) { 966 $format = $order_info['shipping_address_format']; 967 } else { 968 $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}'; 969 } 970 971 $find = array( 972 '{firstname}', 973 '{lastname}', 974 '{company}', 975 '{address_1}', 976 '{address_2}', 977 '{city}', 978 '{postcode}', 979 '{zone}', 980 '{zone_code}', 981 '{country}' 982 ); 983 984 $replace = array( 985 'firstname' => $order_info['shipping_firstname'], 986 'lastname' => $order_info['shipping_lastname'], 987 'company' => $order_info['shipping_company'], 988 'address_1' => $order_info['shipping_address_1'], 989 'address_2' => $order_info['shipping_address_2'], 990 'city' => $order_info['shipping_city'], 991 'postcode' => $order_info['shipping_postcode'], 992 'zone' => $order_info['shipping_zone'], 993 'zone_code' => $order_info['shipping_zone_code'], 994 'country' => $order_info['shipping_country'] 995 ); 996 997 $data['shipping_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format)))); 998 $data['products'] = array(); 999 1000 foreach ($order_product_query->rows as $product) { 1001 $option_data = array(); 1002 1003 $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$product['order_product_id'] . "'"); 1004 1005 foreach ($order_option_query->rows as $option) { 1006 if ($option['type'] != 'file') { 1007 $value = $option['value']; 1008 } else { 1009 $value = utf8_substr($option['value'], 0, utf8_strrpos($option['value'], '.')); 1010 } 1011 1012 $option_data[] = array( 1013 'name' => $option['name'], 1014 'value' => (utf8_strlen($value) > 20) ? utf8_substr($value, 0, 20) . '..' : $value 1015 ); 1016 } 1017 1018 $data['products'][] = array( 1019 'name' => $product['name'], 1020 'model' => $product['model'], 1021 'option' => $option_data, 1022 'quantity' => $product['quantity'], 1023 'price' => $this->currency->format($product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']), 1024 'total' => $this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value']) 1025 ); 1026 } 1027 1028 $data['vouchers'] = array(); 1029 1030 foreach ($order_total_query->rows as $total) { 1031 $data['totals'][] = array( 1032 'title' => $total['title'], 1033 'text' => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), 1034 ); 1035 } 1036 1037 // Text Mail 1038 $text = sprintf($language->get('text_new_greeting'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')) . "\n\n"; 1039 $text .= $language->get('text_new_order_id') . ' ' . $order_id . "\n"; 1040 $text .= $language->get('text_new_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n"; 1041 $text .= $language->get('text_new_order_status') . ' ' . $order_status . "\n\n"; 1042 1043 if ($comment && $notify) { 1044 $text .= $language->get('text_new_instruction') . "\n\n"; 1045 $text .= $comment . "\n\n"; 1046 } 1047 1048 // Products 1049 $text .= $language->get('text_new_products') . "\n"; 1050 1051 foreach ($order_product_query->rows as $product) { 1052 $text .= $product['quantity'] . 'x ' . $product['name'] . ' (' . $product['model'] . ') ' . html_entity_decode($this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n"; 1053 1054 $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$product['order_product_id'] . "'"); 1055 1056 foreach ($order_option_query->rows as $option) { 1057 $text .= chr(9) . '-' . $option['name'] . ' ' . (utf8_strlen($option['value']) > 20) ? utf8_substr($option['value'], 0, 20) . '..' : $option['value'] . "\n"; 1058 } 1059 } 1060 1061 $text .= "\n"; 1062 1063 $text .= $language->get('text_new_order_total') . "\n"; 1064 1065 foreach ($order_total_query->rows as $total) { 1066 $text .= $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n"; 1067 } 1068 1069 $text .= "\n"; 1070 1071 if ($order_info['customer_id']) { 1072 $text .= $language->get('text_new_link') . "\n"; 1073 $text .= $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id . "\n\n"; 1074 } 1075 1076 if ($order_info['comment']) { 1077 $text .= $language->get('text_new_comment') . "\n\n"; 1078 $text .= $order_info['comment'] . "\n\n"; 1079 } 1080 1081 $text .= $language->get('text_new_footer') . "\n\n"; 1082 1083 if ($notify == 1) { 1084 $mail = new Mail($this->config->get('config_mail_engine')); 1085 $mail->parameter = $this->config->get('config_mail_parameter'); 1086 $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname'); 1087 $mail->smtp_username = $this->config->get('config_mail_smtp_username'); 1088 $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'); 1089 $mail->smtp_port = $this->config->get('config_mail_smtp_port'); 1090 $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout'); 1091 1092 $mail->setTo($order_info['email']); 1093 $mail->setFrom($this->config->get('config_email')); 1094 $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')); 1095 $mail->setSubject($subject); 1096 $mail->setHtml($this->load->view('mail/order', $data)); 1097 $mail->setText($text); 1098 $mail->send(); 1099 } 1100 } 1101 } 1102 } 1103 1104 private function modifyStock($product_id, $qty, $symbol = '-', $sku = '') { 1105 $this->openbay->ebay->log('modifyStock() - Updating stock. Product id: ' . $product_id . ' qty: ' . $qty . ', symbol: ' . $symbol . ' sku: ' . $sku); 1106 1107 $item_id = $this->openbay->ebay->getEbayItemId($product_id); 1108 1109 if ($this->openbay->addonLoad('openstock') && !empty($sku)) { 1110 $this->db->query("UPDATE `" . DB_PREFIX . "product_option_variant` SET `stock` = (`stock` " . $this->db->escape((string)$symbol) . " " . (int)$qty . ") WHERE `sku` = '" . (string)$sku . "' AND `product_id` = '" . (int)$product_id . "' AND `subtract` = '1'"); 1111 1112 $stock = $this->openbay->ebay->getProductStockLevel($product_id, $sku); 1113 1114 $this->openbay->ebay->log('modifyStock() /variant - Stock is now set to: ' . $stock['quantity']); 1115 1116 $this->openbay->ebay->putStockUpdate($item_id, $stock['quantity'], $sku); 1117 } else { 1118 $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'"); 1119 1120 $stock = $this->openbay->ebay->getProductStockLevel($product_id); 1121 1122 $this->openbay->ebay->log('modifyStock() - Stock is now set to: ' . $stock['quantity']); 1123 1124 //send back stock update to eBay incase of a reserve product level 1125 $this->openbay->ebay->putStockUpdate($item_id, $stock['quantity']); 1126 } 1127 } 1128 1129 public function getCountryAddressFormat($iso2) { 1130 $this->openbay->ebay->log('getCountryAddressFormat() - Getting country from ISO2: ' . $iso2); 1131 1132 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE `iso_code_2` = '" . $this->db->escape($iso2) . "' LIMIT 1"); 1133 1134 if (!isset($query->row['address_format']) || $query->row['address_format'] == '') { 1135 $this->openbay->ebay->log('getCountryAddressFormat() - No country found, default'); 1136 return false; 1137 } else { 1138 $this->openbay->ebay->log('getCountryAddressFormat() - found country: ' . $query->row['address_format']); 1139 return $query->row['address_format']; 1140 } 1141 } 1142 1143 public function orderLinkCreate($order_id, $smp_id) { 1144 $this->openbay->ebay->log('orderLinkCreate() - order_id: ' . $order_id . ', smp_id: ' . $smp_id); 1145 1146 $this->db->query("INSERT INTO `" . DB_PREFIX . "ebay_order` SET `order_id` = '" . (int)$order_id . "', `smp_id` = '" . (int)$smp_id . "', `parent_ebay_order_id` = 0"); 1147 1148 return $this->db->getLastId(); 1149 } 1150 1151 public function delete($order_id) { 1152 $this->db->query("DELETE FROM `" . DB_PREFIX . "order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1"); 1153 $this->db->query("DELETE FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "'"); 1154 $this->db->query("DELETE FROM `" . DB_PREFIX . "order_option` WHERE `order_id` = '" . (int)$order_id . "'"); 1155 $this->db->query("DELETE FROM `" . DB_PREFIX . "order_history` WHERE `order_id` = '" . (int)$order_id . "'"); 1156 $this->db->query("DELETE FROM `" . DB_PREFIX . "order_total` WHERE `order_id` = '" . (int)$order_id . "'"); 1157 } 1158 1159 public function lockAdd($smp_id) { 1160 $this->openbay->ebay->log('lockAdd() - Added lock, smp_id: ' . $smp_id); 1161 $this->db->query("INSERT INTO`" . DB_PREFIX . "ebay_order_lock` SET `smp_id` = '" . (int)$smp_id . "'"); 1162 } 1163 1164 public function lockDelete($smp_id) { 1165 $this->openbay->ebay->log('lockDelete() - Delete lock, smp_id: ' . $smp_id); 1166 $this->db->query("DELETE FROM `" . DB_PREFIX . "ebay_order_lock` WHERE `smp_id` = '" . (int)$smp_id . "'"); 1167 } 1168 1169 public function lockExists($smp_id) { 1170 $this->openbay->ebay->log('lockExists() - Check lock, smp_id: ' . (int)$smp_id); 1171 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ebay_order_lock` WHERE `smp_id` = '" . (int)$smp_id . "' LIMIT 1"); 1172 1173 if ($query->num_rows > 0) { 1174 $this->openbay->ebay->log('lockExists() - Lock found, stopping order . '); 1175 return true; 1176 } else { 1177 $this->lockAdd($smp_id); 1178 return false; 1179 } 1180 } 1181 1182 public function addOrderHistory($order_id) { 1183 $this->openbay->ebay->log('addOrderHistory() - Order id:' . $order_id . ' passed'); 1184 if (!$this->openbay->ebay->getOrder($order_id)) { 1185 $order_products = $this->openbay->getOrderProducts($order_id); 1186 1187 foreach($order_products as $order_product) { 1188 $product = $this->db->query("SELECT * FROM `" . DB_PREFIX . "product` WHERE `product_id` = '" . (int)$order_product['product_id'] . "' LIMIT 1")->row; 1189 1190 if ($this->openbay->addonLoad('openstock') && (isset($product['has_option']) && $product['has_option'] == 1)) { 1191 $order_product_variant = $this->openbay->getOrderProductVariant($order_id, $order_product['product_id'], $order_product['order_product_id']); 1192 1193 if (isset($order_product_variant['sku']) && $order_product_variant['sku'] != '') { 1194 $this->openbay->ebay->ebaySaleStockReduce((int)$order_product['product_id'], (string)$order_product_variant['sku']); 1195 } 1196 } else { 1197 $this->openbay->ebay->ebaySaleStockReduce($order_product['product_id']); 1198 } 1199 } 1200 } 1201 } 1202 }