shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

openbay.php (15687B)


      1 <?php
      2 final class Openbay {
      3 	private $registry;
      4 	private $installed_modules = array();
      5 	public $installed_markets = array();
      6 	private $logging = 1;
      7 
      8 	public function __construct($registry) {
      9 		// OpenBay Pro
     10 		$this->registry = $registry;
     11 
     12 		if ($this->db != null) {
     13 			$this->getInstalled();
     14 
     15 			foreach ($this->installed_markets as $market) {
     16 				$class = '\openbay\\'. ucfirst($market);
     17 
     18 				$this->{$market} = new $class($registry);
     19 			}
     20 		}
     21 
     22 		$this->logger = new \Log('openbay.log');
     23 	}
     24 
     25 	public function __get($name) {
     26 		return $this->registry->get($name);
     27 	}
     28 
     29 	public function log($data, $write = true) {
     30 		if ($this->logging == 1) {
     31 			if (function_exists('getmypid')) {
     32 				$process_id = getmypid();
     33 				$data = $process_id . ' - ' . $data;
     34 			}
     35 
     36 			if ($write == true) {
     37 				$this->logger->write($data);
     38 			}
     39 		}
     40 	}
     41 
     42 	public function encrypt($value, $key, $iv, $json = true) {
     43 		if ($json == true) {
     44 		    $value = json_encode($value);
     45         }
     46 
     47 	    return strtr(base64_encode(openssl_encrypt($value, 'aes-128-cbc', hash('sha256', hex2bin($key), true), 0, hex2bin($iv))), '+/=', '-_,');
     48 	}
     49 
     50 	public function decrypt($value, $key, $iv, $json = true) {
     51 		$response = trim(openssl_decrypt(base64_decode(strtr($value, '-_,', '+/=')), 'aes-128-cbc', hash('sha256', hex2bin($key), true), 0, hex2bin($iv)));
     52 
     53 		if ($json == true) {
     54 		    $response =  json_decode($response, true);
     55         }
     56 
     57         return $response;
     58 	}
     59 
     60 	private function getInstalled() {
     61 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "extension WHERE `type` = 'openbay'");
     62 
     63 		foreach ($query->rows as $result) {
     64 			$this->installed_markets[] = $result['code'];
     65 		}
     66 	}
     67 
     68 	public function getInstalledMarkets() {
     69 		return $this->installed_markets;
     70 	}
     71 
     72 	public function putStockUpdateBulk($product_id_array, $end_inactive = false) {
     73 		/**
     74 		 * putStockUpdateBulk
     75 		 *
     76 		 * Takes an array of product id's where stock has been modified
     77 		 *
     78 		 * @param $product_id_array
     79 		 */
     80 
     81 		foreach ($this->installed_markets as $market) {
     82 			if ($this->config->get($market . '_status') == 1 || $this->config->get('openbay_' .$market . '_status') == 1) {
     83 				$this->{$market}->putStockUpdateBulk($product_id_array, $end_inactive);
     84 			}
     85 		}
     86 	}
     87 
     88 	public function testDbColumn($table, $column) {
     89 		$res = $this->db->query("SHOW COLUMNS FROM `" . DB_PREFIX . $table . "` LIKE '" . $column . "'");
     90 		if($res->num_rows != 0) {
     91 			return true;
     92 		} else {
     93 			return false;
     94 		}
     95 	}
     96 
     97 	public function testDbTable($table) {
     98 		$res = $this->db->query("SELECT `table_name` AS `c` FROM `information_schema`.`tables` WHERE `table_schema` = DATABASE()");
     99 
    100 		$tables = array();
    101 
    102 		foreach($res->rows as $row) {
    103 			$tables[] = $row['c'];
    104 		}
    105 
    106 		if(in_array($table, $tables)) {
    107 			return true;
    108 		} else {
    109 			return false;
    110 		}
    111 	}
    112 
    113 	public function splitName($name) {
    114 		$name = explode(' ', $name);
    115 		$fname = $name[0];
    116 		unset($name[0]);
    117 		$lname = implode(' ', $name);
    118 
    119 		return array(
    120 			'firstname' => $fname,
    121 			'surname'   => $lname
    122 		);
    123 	}
    124 
    125 	public function getTaxRates($tax_class_id) {
    126 		$tax_rates = array();
    127 
    128 		$tax_query = $this->db->query("SELECT
    129 					tr2.tax_rate_id,
    130 					tr2.name,
    131 					tr2.rate,
    132 					tr2.type,
    133 					tr1.priority
    134 				FROM " . DB_PREFIX . "tax_rule tr1
    135 				LEFT JOIN " . DB_PREFIX . "tax_rate tr2 ON (tr1.tax_rate_id = tr2.tax_rate_id)
    136 				INNER JOIN " . DB_PREFIX . "tax_rate_to_customer_group tr2cg ON (tr2.tax_rate_id = tr2cg.tax_rate_id)
    137 				LEFT JOIN " . DB_PREFIX . "zone_to_geo_zone z2gz ON (tr2.geo_zone_id = z2gz.geo_zone_id)
    138 				LEFT JOIN " . DB_PREFIX . "geo_zone gz ON (tr2.geo_zone_id = gz.geo_zone_id)
    139 				WHERE tr1.tax_class_id = '" . (int)$tax_class_id . "'
    140 				AND tr1.based = 'shipping'
    141 				AND tr2cg.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "'
    142 				AND z2gz.country_id = '" . (int)$this->config->get('config_country_id') . "'
    143 				AND (z2gz.zone_id = '0' OR z2gz.zone_id = '" . (int)$this->config->get('config_zone_id') . "')
    144 				ORDER BY tr1.priority ASC");
    145 
    146 		foreach ($tax_query->rows as $result) {
    147 			$tax_rates[$result['tax_rate_id']] = array(
    148 				'tax_rate_id' => $result['tax_rate_id'],
    149 				'name'        => $result['name'],
    150 				'rate'        => $result['rate'],
    151 				'type'        => $result['type'],
    152 				'priority'    => $result['priority']
    153 			);
    154 		}
    155 
    156 		return $tax_rates;
    157 	}
    158 
    159 	public function getTaxRate($class_id) {
    160 		$rates = $this->getTaxRates($class_id);
    161 		$percentage = 0.00;
    162 		foreach($rates as $rate) {
    163 			if($rate['type'] == 'P') {
    164 				$percentage += $rate['rate'];
    165 			}
    166 		}
    167 
    168 		return $percentage;
    169 	}
    170 
    171 	public function getZoneId($name, $country_id) {
    172 		$query = $this->db->query("SELECT `zone_id` FROM `" . DB_PREFIX . "zone` WHERE `country_id` = '" . (int)$country_id . "' AND status = '1' AND `name` = '" . $this->db->escape($name) . "'");
    173 
    174 		if($query->num_rows > 0) {
    175 			return $query->row['zone_id'];
    176 		} else {
    177 			return 0;
    178 		}
    179 	}
    180 
    181 	public function newOrderAdminNotify($order_id, $order_status_id) {
    182 		$this->load->model('checkout/order');
    183 		$order_info = $this->model_checkout_order->getOrder($order_id);
    184 
    185 		if (version_compare(VERSION, '2.2', '>') == true) {
    186 			$language_code = $order_info['language_code'];
    187 		} else {
    188 			$language_code = $order_info['language_directory'];
    189 		}
    190 
    191 		$language = new Language($language_code);
    192 		$language->load($language_code);
    193 		$language->load('mail/order');
    194 
    195 		$order_status = $this->db->query("SELECT `name` FROM " . DB_PREFIX . "order_status WHERE order_status_id = '" . (int)$order_status_id . "' AND language_id = '" . (int)$this->config->get('config_language_id') . "' LIMIT 1")->row['name'];
    196 
    197 		// Order Totals
    198 		$order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE `order_id` = '" . (int)$order_id . "' ORDER BY `sort_order` ASC");
    199 
    200 		//Order contents
    201 		$order_product_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "'");
    202 
    203 		$subject = sprintf($language->get('text_new_subject'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'), $order_id);
    204 
    205 		// Text
    206 		$text  = $language->get('text_new_received') . "\n\n";
    207 		$text .= $language->get('text_new_order_id') . ' ' . $order_info['order_id'] . "\n";
    208 		$text .= $language->get('text_new_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n";
    209 		$text .= $language->get('text_new_order_status') . ' ' . $order_status . "\n\n";
    210 		$text .= $language->get('text_new_products') . "\n";
    211 
    212 		foreach ($order_product_query->rows as $product) {
    213 			$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";
    214 
    215 			$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'] . "'");
    216 
    217 			foreach ($order_option_query->rows as $option) {
    218 				if ($option['type'] != 'file') {
    219 					$value = $option['value'];
    220 				} else {
    221 					$value = utf8_substr($option['value'], 0, utf8_strrpos($option['value'], '.'));
    222 				}
    223 
    224 				$text .= chr(9) . '-' . $option['name'] . ' ' . (utf8_strlen($value) > 20) ? utf8_substr($value, 0, 20) . '..' : $value . "\n";
    225 			}
    226 		}
    227 
    228 		if (isset($order_voucher_query) && is_array($order_voucher_query)) {
    229 			foreach ($order_voucher_query->rows as $voucher) {
    230 				$text .= '1x ' . $voucher['description'] . ' ' . $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']);
    231 			}
    232 		}
    233 
    234 		$text .= "\n";
    235 		$text .= $language->get('text_new_order_total') . "\n";
    236 
    237 		foreach ($order_total_query->rows as $total) {
    238 			$text .= $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
    239 		}
    240 
    241 		$text .= "\n";
    242 
    243 		if ($order_info['comment']) {
    244 			$text .= $language->get('text_new_comment') . "\n\n";
    245 			$text .= $order_info['comment'] . "\n\n";
    246 		}
    247 
    248 		$mail = new \Mail();
    249 		$mail->protocol = $this->config->get('config_mail_protocol');
    250 		$mail->parameter = $this->config->get('config_mail_parameter');
    251 		$mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
    252 		$mail->smtp_username = $this->config->get('config_mail_smtp_username');
    253 		$mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
    254 		$mail->smtp_port = $this->config->get('config_mail_smtp_port');
    255 		$mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
    256 
    257 		$mail->setTo($this->config->get('config_email'));
    258 		$mail->setFrom($this->config->get('config_email'));
    259 		$mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
    260 		$mail->setSubject($subject);
    261 		$mail->setText($text);
    262 		$mail->send();
    263 
    264 		// Send to additional alert emails
    265 		$emails = explode(',', $this->config->get('config_mail_alert_email'));
    266 
    267 		foreach ($emails as $email) {
    268 			if ($email && filter_var($email, FILTER_VALIDATE_EMAIL)) {
    269 				$mail->setTo($email);
    270 				$mail->send();
    271 			}
    272 		}
    273 	}
    274 
    275 	public function orderDelete($order_id) {
    276 		/**
    277 		 * Called when an order is deleted in the admin
    278 		 * Use it to add stock back to the marketplaces
    279 		 */
    280 		foreach ($this->installed_markets as $market) {
    281 			if ($this->config->get($market . '_status') == 1 || $this->config->get('openbay_' .$market . '_status') == 1) {
    282 				$this->{$market}->orderDelete($order_id);
    283 			}
    284 		}
    285 	}
    286 
    287 	public function getProductModelNumber($product_id, $sku = null) {
    288 		if($sku != null) {
    289 			$qry = $this->db->query("SELECT `sku` FROM `" . DB_PREFIX . "product_option_variant` WHERE `product_id` = '" . (int)$product_id . "' AND `sku` = '" . $this->db->escape($sku) . "'");
    290 
    291 			if($qry->num_rows > 0) {
    292 				return $qry->row['sku'];
    293 			} else {
    294 				return false;
    295 			}
    296 		} else {
    297 			$qry = $this->db->query("SELECT `model` FROM `" . DB_PREFIX . "product` WHERE `product_id` = '" . (int)$product_id . "' LIMIT 1");
    298 
    299 			if($qry->num_rows > 0) {
    300 				return $qry->row['model'];
    301 			} else {
    302 				return false;
    303 			}
    304 		}
    305 	}
    306 
    307 	public function getProductTaxClassId($product_id) {
    308 		$qry = $this->db->query("SELECT `tax_class_id` FROM `" . DB_PREFIX . "product` WHERE `product_id` = '" . (int)$product_id . "' LIMIT 1");
    309 
    310 		if($qry->num_rows > 0) {
    311 			return $qry->row['tax_class_id'];
    312 		} else {
    313 			return false;
    314 		}
    315 	}
    316 
    317 	public function addonLoad($addon) {
    318 		$addon = strtolower((string)$addon);
    319 
    320 		if (empty($this->installed_modules)) {
    321 			$this->installed_modules = array();
    322 
    323 			$rows = $this->db->query("SELECT `code` FROM " . DB_PREFIX . "extension")->rows;
    324 
    325 			foreach ($rows as $row) {
    326 				$this->installed_modules[] = strtolower($row['code']);
    327 			}
    328 		}
    329 
    330 		return in_array($addon, $this->installed_modules);
    331 	}
    332 
    333 	public function getUserByEmail($email) {
    334 		$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer` WHERE `email` = '" . $this->db->escape($email) . "'");
    335 
    336 		if($qry->num_rows){
    337 			return $qry->row['customer_id'];
    338 		} else {
    339 			return false;
    340 		}
    341 	}
    342 
    343 	public function getProductOptions($product_id) {
    344 		$product_option_data = array();
    345 
    346 		$product_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option po LEFT JOIN `" . DB_PREFIX . "option` o ON (po.option_id = o.option_id) LEFT JOIN " . DB_PREFIX . "option_description od ON (o.option_id = od.option_id) WHERE po.product_id = '" . (int)$product_id . "' AND od.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY o.sort_order");
    347 
    348 		foreach ($product_option_query->rows as $product_option) {
    349 			if ($product_option['type'] == 'select' || $product_option['type'] == 'radio' || $product_option['type'] == 'checkbox' || $product_option['type'] == 'image') {
    350 				$product_option_value_data = array();
    351 
    352 				$product_option_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option_value pov LEFT JOIN " . DB_PREFIX . "option_value ov ON (pov.option_value_id = ov.option_value_id) LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) WHERE pov.product_option_id = '" . (int)$product_option['product_option_id'] . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY ov.sort_order");
    353 
    354 				foreach ($product_option_value_query->rows as $product_option_value) {
    355 					$product_option_value_data[] = array(
    356 						'product_option_value_id' => $product_option_value['product_option_value_id'],
    357 						'option_value_id'         => $product_option_value['option_value_id'],
    358 						'name'                    => $product_option_value['name'],
    359 						'image'                   => $product_option_value['image'],
    360 						'quantity'                => $product_option_value['quantity'],
    361 						'subtract'                => $product_option_value['subtract'],
    362 						'price'                   => $product_option_value['price'],
    363 						'price_prefix'            => $product_option_value['price_prefix'],
    364 						'points'                  => $product_option_value['points'],
    365 						'points_prefix'           => $product_option_value['points_prefix'],
    366 						'weight'                  => $product_option_value['weight'],
    367 						'weight_prefix'           => $product_option_value['weight_prefix']
    368 					);
    369 				}
    370 
    371 				$product_option_data[] = array(
    372 					'product_option_id'    => $product_option['product_option_id'],
    373 					'option_id'            => $product_option['option_id'],
    374 					'name'                 => $product_option['name'],
    375 					'type'                 => $product_option['type'],
    376 					'product_option_value' => $product_option_value_data,
    377 					'required'             => $product_option['required']
    378 				);
    379 			} else {
    380 				$product_option_data[] = array(
    381 					'product_option_id' => $product_option['product_option_id'],
    382 					'option_id'         => $product_option['option_id'],
    383 					'name'              => $product_option['name'],
    384 					'type'              => $product_option['type'],
    385 					'option_value'      => $product_option['value'],
    386 					'required'          => $product_option['required']
    387 				);
    388 			}
    389 		}
    390 
    391 		return $product_option_data;
    392 	}
    393 
    394 	public function getOrderProducts($order_id) {
    395 		$order_products = $this->db->query("SELECT `product_id`, `order_product_id` FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "'");
    396 
    397 		if($order_products->num_rows > 0) {
    398 			return $order_products->rows;
    399 		} else {
    400 			return array();
    401 		}
    402 	}
    403 
    404 	public function getOrderProductVariant($order_id, $product_id, $order_product_id) {
    405 		$this->load->model('extension/module/openstock');
    406 
    407 		$order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$order_product_id . "'");
    408 
    409 		if ($order_option_query->num_rows) {
    410 			$options = array();
    411 
    412 			foreach ($order_option_query->rows as $option) {
    413 				$options[] = $option['product_option_value_id'];
    414 			}
    415 
    416 			return $this->model_extension_module_openstock->getVariantByOptionValues($options, $product_id);
    417 		}
    418 	}
    419 }