shop.balmet.com

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

order.php (67338B)


      1 <?php
      2 class ControllerSaleOrder extends Controller {
      3 	private $error = array();
      4 
      5 	public function index() {
      6 		$this->load->language('sale/order');
      7 
      8 		$this->document->setTitle($this->language->get('heading_title'));
      9 
     10 		$this->load->model('sale/order');
     11 
     12 		$this->getList();
     13 	}
     14 
     15 	public function add() {
     16 		$this->load->language('sale/order');
     17 
     18 		$this->document->setTitle($this->language->get('heading_title'));
     19 
     20 		$this->load->model('sale/order');
     21 
     22 		$this->getForm();
     23 	}
     24 
     25 	public function edit() {
     26 		$this->load->language('sale/order');
     27 
     28 		$this->document->setTitle($this->language->get('heading_title'));
     29 
     30 		$this->load->model('sale/order');
     31 
     32 		$this->getForm();
     33 	}
     34 	
     35 	public function delete() {
     36 		$this->load->language('sale/order');
     37 
     38 		$this->document->setTitle($this->language->get('heading_title'));
     39 
     40 		$this->session->data['success'] = $this->language->get('text_success');
     41 
     42 		$url = '';
     43 
     44 		if (isset($this->request->get['filter_order_id'])) {
     45 			$url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
     46 		}
     47 
     48 		if (isset($this->request->get['filter_customer'])) {
     49 			$url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
     50 		}
     51 
     52 		if (isset($this->request->get['filter_order_status'])) {
     53 			$url .= '&filter_order_status=' . $this->request->get['filter_order_status'];
     54 		}
     55 	
     56 		if (isset($this->request->get['filter_order_status_id'])) {
     57 			$url .= '&filter_order_status_id=' . $this->request->get['filter_order_status_id'];
     58 		}
     59 			
     60 		if (isset($this->request->get['filter_total'])) {
     61 			$url .= '&filter_total=' . $this->request->get['filter_total'];
     62 		}
     63 
     64 		if (isset($this->request->get['filter_date_added'])) {
     65 			$url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
     66 		}
     67 
     68 		if (isset($this->request->get['filter_date_modified'])) {
     69 			$url .= '&filter_date_modified=' . $this->request->get['filter_date_modified'];
     70 		}
     71 
     72 		if (isset($this->request->get['sort'])) {
     73 			$url .= '&sort=' . $this->request->get['sort'];
     74 		}
     75 
     76 		if (isset($this->request->get['order'])) {
     77 			$url .= '&order=' . $this->request->get['order'];
     78 		}
     79 
     80 		if (isset($this->request->get['page'])) {
     81 			$url .= '&page=' . $this->request->get['page'];
     82 		}
     83 
     84 		$this->response->redirect($this->url->link('sale/order', 'user_token=' . $this->session->data['user_token'] . $url, true));
     85 	}
     86 			
     87 	protected function getList() {
     88 		if (isset($this->request->get['filter_order_id'])) {
     89 			$filter_order_id = $this->request->get['filter_order_id'];
     90 		} else {
     91 			$filter_order_id = '';
     92 		}
     93 
     94 		if (isset($this->request->get['filter_customer'])) {
     95 			$filter_customer = $this->request->get['filter_customer'];
     96 		} else {
     97 			$filter_customer = '';
     98 		}
     99 
    100 		if (isset($this->request->get['filter_order_status'])) {
    101 			$filter_order_status = $this->request->get['filter_order_status'];
    102 		} else {
    103 			$filter_order_status = '';
    104 		}
    105 		
    106 		if (isset($this->request->get['filter_order_status_id'])) {
    107 			$filter_order_status_id = $this->request->get['filter_order_status_id'];
    108 		} else {
    109 			$filter_order_status_id = '';
    110 		}
    111 		
    112 		if (isset($this->request->get['filter_total'])) {
    113 			$filter_total = $this->request->get['filter_total'];
    114 		} else {
    115 			$filter_total = '';
    116 		}
    117 
    118 		if (isset($this->request->get['filter_date_added'])) {
    119 			$filter_date_added = $this->request->get['filter_date_added'];
    120 		} else {
    121 			$filter_date_added = '';
    122 		}
    123 
    124 		if (isset($this->request->get['filter_date_modified'])) {
    125 			$filter_date_modified = $this->request->get['filter_date_modified'];
    126 		} else {
    127 			$filter_date_modified = '';
    128 		}
    129 
    130 		if (isset($this->request->get['sort'])) {
    131 			$sort = $this->request->get['sort'];
    132 		} else {
    133 			$sort = 'o.order_id';
    134 		}
    135 
    136 		if (isset($this->request->get['order'])) {
    137 			$order = $this->request->get['order'];
    138 		} else {
    139 			$order = 'DESC';
    140 		}
    141 
    142 		if (isset($this->request->get['page'])) {
    143 			$page = $this->request->get['page'];
    144 		} else {
    145 			$page = 1;
    146 		}
    147 
    148 		$url = '';
    149 
    150 		if (isset($this->request->get['filter_order_id'])) {
    151 			$url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
    152 		}
    153 
    154 		if (isset($this->request->get['filter_customer'])) {
    155 			$url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
    156 		}
    157 
    158 		if (isset($this->request->get['filter_order_status'])) {
    159 			$url .= '&filter_order_status=' . $this->request->get['filter_order_status'];
    160 		}
    161 	
    162 		if (isset($this->request->get['filter_order_status_id'])) {
    163 			$url .= '&filter_order_status_id=' . $this->request->get['filter_order_status_id'];
    164 		}
    165 			
    166 		if (isset($this->request->get['filter_total'])) {
    167 			$url .= '&filter_total=' . $this->request->get['filter_total'];
    168 		}
    169 
    170 		if (isset($this->request->get['filter_date_added'])) {
    171 			$url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
    172 		}
    173 
    174 		if (isset($this->request->get['filter_date_modified'])) {
    175 			$url .= '&filter_date_modified=' . $this->request->get['filter_date_modified'];
    176 		}
    177 
    178 		if (isset($this->request->get['sort'])) {
    179 			$url .= '&sort=' . $this->request->get['sort'];
    180 		}
    181 
    182 		if (isset($this->request->get['order'])) {
    183 			$url .= '&order=' . $this->request->get['order'];
    184 		}
    185 
    186 		if (isset($this->request->get['page'])) {
    187 			$url .= '&page=' . $this->request->get['page'];
    188 		}
    189 
    190 		$data['breadcrumbs'] = array();
    191 
    192 		$data['breadcrumbs'][] = array(
    193 			'text' => $this->language->get('text_home'),
    194 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
    195 		);
    196 
    197 		$data['breadcrumbs'][] = array(
    198 			'text' => $this->language->get('heading_title'),
    199 			'href' => $this->url->link('sale/order', 'user_token=' . $this->session->data['user_token'] . $url, true)
    200 		);
    201 
    202 		$data['invoice'] = $this->url->link('sale/order/invoice', 'user_token=' . $this->session->data['user_token'], true);
    203 		$data['shipping'] = $this->url->link('sale/order/shipping', 'user_token=' . $this->session->data['user_token'], true);
    204 		$data['add'] = $this->url->link('sale/order/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
    205 		$data['delete'] = str_replace('&amp;', '&', $this->url->link('sale/order/delete', 'user_token=' . $this->session->data['user_token'] . $url, true));
    206 
    207 		$data['orders'] = array();
    208 
    209 		$filter_data = array(
    210 			'filter_order_id'        => $filter_order_id,
    211 			'filter_customer'	     => $filter_customer,
    212 			'filter_order_status'    => $filter_order_status,
    213 			'filter_order_status_id' => $filter_order_status_id,
    214 			'filter_total'           => $filter_total,
    215 			'filter_date_added'      => $filter_date_added,
    216 			'filter_date_modified'   => $filter_date_modified,
    217 			'sort'                   => $sort,
    218 			'order'                  => $order,
    219 			'start'                  => ($page - 1) * $this->config->get('config_limit_admin'),
    220 			'limit'                  => $this->config->get('config_limit_admin')
    221 		);
    222 
    223 		$order_total = $this->model_sale_order->getTotalOrders($filter_data);
    224 
    225 		$results = $this->model_sale_order->getOrders($filter_data);
    226 
    227 		foreach ($results as $result) {
    228 			$data['orders'][] = array(
    229 				'order_id'      => $result['order_id'],
    230 				'customer'      => $result['customer'],
    231 				'order_status'  => $result['order_status'] ? $result['order_status'] : $this->language->get('text_missing'),
    232 				'total'         => $this->currency->format($result['total'], $result['currency_code'], $result['currency_value']),
    233 				'date_added'    => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
    234 				'date_modified' => date($this->language->get('date_format_short'), strtotime($result['date_modified'])),
    235 				'shipping_code' => $result['shipping_code'],
    236 				'view'          => $this->url->link('sale/order/info', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $result['order_id'] . $url, true),
    237 				'edit'          => $this->url->link('sale/order/edit', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $result['order_id'] . $url, true)
    238 			);
    239 		}
    240 
    241 		$data['user_token'] = $this->session->data['user_token'];
    242 
    243 		if (isset($this->error['warning'])) {
    244 			$data['error_warning'] = $this->error['warning'];
    245 		} else {
    246 			$data['error_warning'] = '';
    247 		}
    248 
    249 		if (isset($this->session->data['success'])) {
    250 			$data['success'] = $this->session->data['success'];
    251 
    252 			unset($this->session->data['success']);
    253 		} else {
    254 			$data['success'] = '';
    255 		}
    256 		
    257 		if (isset($this->request->post['selected'])) {
    258 			$data['selected'] = (array)$this->request->post['selected'];
    259 		} else {
    260 			$data['selected'] = array();
    261 		}
    262 
    263 		$url = '';
    264 
    265 		if (isset($this->request->get['filter_order_id'])) {
    266 			$url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
    267 		}
    268 
    269 		if (isset($this->request->get['filter_customer'])) {
    270 			$url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
    271 		}
    272 
    273 		if (isset($this->request->get['filter_order_status'])) {
    274 			$url .= '&filter_order_status=' . $this->request->get['filter_order_status'];
    275 		}
    276 		
    277 		if (isset($this->request->get['filter_order_status_id'])) {
    278 			$url .= '&filter_order_status_id=' . $this->request->get['filter_order_status_id'];
    279 		}
    280 			
    281 		if (isset($this->request->get['filter_total'])) {
    282 			$url .= '&filter_total=' . $this->request->get['filter_total'];
    283 		}
    284 
    285 		if (isset($this->request->get['filter_date_added'])) {
    286 			$url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
    287 		}
    288 
    289 		if (isset($this->request->get['filter_date_modified'])) {
    290 			$url .= '&filter_date_modified=' . $this->request->get['filter_date_modified'];
    291 		}
    292 
    293 		if ($order == 'ASC') {
    294 			$url .= '&order=DESC';
    295 		} else {
    296 			$url .= '&order=ASC';
    297 		}
    298 
    299 		if (isset($this->request->get['page'])) {
    300 			$url .= '&page=' . $this->request->get['page'];
    301 		}
    302 
    303 		$data['sort_order'] = $this->url->link('sale/order', 'user_token=' . $this->session->data['user_token'] . '&sort=o.order_id' . $url, true);
    304 		$data['sort_customer'] = $this->url->link('sale/order', 'user_token=' . $this->session->data['user_token'] . '&sort=customer' . $url, true);
    305 		$data['sort_status'] = $this->url->link('sale/order', 'user_token=' . $this->session->data['user_token'] . '&sort=order_status' . $url, true);
    306 		$data['sort_total'] = $this->url->link('sale/order', 'user_token=' . $this->session->data['user_token'] . '&sort=o.total' . $url, true);
    307 		$data['sort_date_added'] = $this->url->link('sale/order', 'user_token=' . $this->session->data['user_token'] . '&sort=o.date_added' . $url, true);
    308 		$data['sort_date_modified'] = $this->url->link('sale/order', 'user_token=' . $this->session->data['user_token'] . '&sort=o.date_modified' . $url, true);
    309 
    310 		$url = '';
    311 
    312 		if (isset($this->request->get['filter_order_id'])) {
    313 			$url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
    314 		}
    315 
    316 		if (isset($this->request->get['filter_customer'])) {
    317 			$url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
    318 		}
    319 
    320 		if (isset($this->request->get['filter_order_status'])) {
    321 			$url .= '&filter_order_status=' . $this->request->get['filter_order_status'];
    322 		}
    323 		
    324 		if (isset($this->request->get['filter_order_status_id'])) {
    325 			$url .= '&filter_order_status_id=' . $this->request->get['filter_order_status_id'];
    326 		}
    327 			
    328 		if (isset($this->request->get['filter_total'])) {
    329 			$url .= '&filter_total=' . $this->request->get['filter_total'];
    330 		}
    331 
    332 		if (isset($this->request->get['filter_date_added'])) {
    333 			$url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
    334 		}
    335 
    336 		if (isset($this->request->get['filter_date_modified'])) {
    337 			$url .= '&filter_date_modified=' . $this->request->get['filter_date_modified'];
    338 		}
    339 
    340 		if (isset($this->request->get['sort'])) {
    341 			$url .= '&sort=' . $this->request->get['sort'];
    342 		}
    343 
    344 		if (isset($this->request->get['order'])) {
    345 			$url .= '&order=' . $this->request->get['order'];
    346 		}
    347 
    348 		$pagination = new Pagination();
    349 		$pagination->total = $order_total;
    350 		$pagination->page = $page;
    351 		$pagination->limit = $this->config->get('config_limit_admin');
    352 		$pagination->url = $this->url->link('sale/order', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true);
    353 
    354 		$data['pagination'] = $pagination->render();
    355 
    356 		$data['results'] = sprintf($this->language->get('text_pagination'), ($order_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($order_total - $this->config->get('config_limit_admin'))) ? $order_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $order_total, ceil($order_total / $this->config->get('config_limit_admin')));
    357 
    358 		$data['filter_order_id'] = $filter_order_id;
    359 		$data['filter_customer'] = $filter_customer;
    360 		$data['filter_order_status'] = $filter_order_status;
    361 		$data['filter_order_status_id'] = $filter_order_status_id;
    362 		$data['filter_total'] = $filter_total;
    363 		$data['filter_date_added'] = $filter_date_added;
    364 		$data['filter_date_modified'] = $filter_date_modified;
    365 
    366 		$data['sort'] = $sort;
    367 		$data['order'] = $order;
    368 
    369 		$this->load->model('localisation/order_status');
    370 
    371 		$data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
    372 
    373 		// API login
    374 		$data['catalog'] = $this->request->server['HTTPS'] ? HTTPS_CATALOG : HTTP_CATALOG;
    375 		
    376 		// API login
    377 		$this->load->model('user/api');
    378 
    379 		$api_info = $this->model_user_api->getApi($this->config->get('config_api_id'));
    380 
    381 		if ($api_info && $this->user->hasPermission('modify', 'sale/order')) {
    382 			$session = new Session($this->config->get('session_engine'), $this->registry);
    383 			
    384 			$session->start();
    385 					
    386 			$this->model_user_api->deleteApiSessionBySessonId($session->getId());
    387 			
    388 			$this->model_user_api->addApiSession($api_info['api_id'], $session->getId(), $this->request->server['REMOTE_ADDR']);
    389 			
    390 			$session->data['api_id'] = $api_info['api_id'];
    391 
    392 			$data['api_token'] = $session->getId();
    393 		} else {
    394 			$data['api_token'] = '';
    395 		}
    396 
    397 		$data['header'] = $this->load->controller('common/header');
    398 		$data['column_left'] = $this->load->controller('common/column_left');
    399 		$data['footer'] = $this->load->controller('common/footer');
    400 
    401 		$this->response->setOutput($this->load->view('sale/order_list', $data));
    402 	}
    403 		
    404 	public function getForm() {
    405 		$data['text_form'] = !isset($this->request->get['order_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
    406 
    407 		$url = '';
    408 
    409 		if (isset($this->request->get['filter_order_id'])) {
    410 			$url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
    411 		}
    412 
    413 		if (isset($this->request->get['filter_customer'])) {
    414 			$url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
    415 		}
    416 
    417 		if (isset($this->request->get['filter_order_status'])) {
    418 			$url .= '&filter_order_status=' . $this->request->get['filter_order_status'];
    419 		}
    420 		
    421 		if (isset($this->request->get['filter_order_status_id'])) {
    422 			$url .= '&filter_order_status_id=' . $this->request->get['filter_order_status_id'];
    423 		}
    424 			
    425 		if (isset($this->request->get['filter_total'])) {
    426 			$url .= '&filter_total=' . $this->request->get['filter_total'];
    427 		}
    428 
    429 		if (isset($this->request->get['filter_date_added'])) {
    430 			$url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
    431 		}
    432 
    433 		if (isset($this->request->get['filter_date_modified'])) {
    434 			$url .= '&filter_date_modified=' . $this->request->get['filter_date_modified'];
    435 		}
    436 
    437 		if (isset($this->request->get['sort'])) {
    438 			$url .= '&sort=' . $this->request->get['sort'];
    439 		}
    440 
    441 		if (isset($this->request->get['order'])) {
    442 			$url .= '&order=' . $this->request->get['order'];
    443 		}
    444 
    445 		if (isset($this->request->get['page'])) {
    446 			$url .= '&page=' . $this->request->get['page'];
    447 		}
    448 
    449 		$data['breadcrumbs'] = array();
    450 
    451 		$data['breadcrumbs'][] = array(
    452 			'text' => $this->language->get('text_home'),
    453 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
    454 		);
    455 
    456 		$data['breadcrumbs'][] = array(
    457 			'text' => $this->language->get('heading_title'),
    458 			'href' => $this->url->link('sale/order', 'user_token=' . $this->session->data['user_token'] . $url, true)
    459 		);
    460 
    461 		$data['cancel'] = $this->url->link('sale/order', 'user_token=' . $this->session->data['user_token'] . $url, true);
    462 
    463 		$data['user_token'] = $this->session->data['user_token'];
    464 
    465 		if (isset($this->request->get['order_id'])) {
    466 			$order_info = $this->model_sale_order->getOrder($this->request->get['order_id']);
    467 		}
    468 
    469 		if (!empty($order_info)) {
    470 			$data['order_id'] = $this->request->get['order_id'];
    471 			$data['store_id'] = $order_info['store_id'];
    472 			$data['store_url'] = $this->request->server['HTTPS'] ? HTTPS_CATALOG : HTTP_CATALOG;
    473 
    474 			$data['customer'] = $order_info['customer'];
    475 			$data['customer_id'] = $order_info['customer_id'];
    476 			$data['customer_group_id'] = $order_info['customer_group_id'];
    477 			$data['firstname'] = $order_info['firstname'];
    478 			$data['lastname'] = $order_info['lastname'];
    479 			$data['email'] = $order_info['email'];
    480 			$data['telephone'] = $order_info['telephone'];
    481 			$data['account_custom_field'] = $order_info['custom_field'];
    482 
    483 			$this->load->model('customer/customer');
    484 
    485 			$data['addresses'] = $this->model_customer_customer->getAddresses($order_info['customer_id']);
    486 
    487 			$data['payment_firstname'] = $order_info['payment_firstname'];
    488 			$data['payment_lastname'] = $order_info['payment_lastname'];
    489 			$data['payment_company'] = $order_info['payment_company'];
    490 			$data['payment_address_1'] = $order_info['payment_address_1'];
    491 			$data['payment_address_2'] = $order_info['payment_address_2'];
    492 			$data['payment_city'] = $order_info['payment_city'];
    493 			$data['payment_postcode'] = $order_info['payment_postcode'];
    494 			$data['payment_country_id'] = $order_info['payment_country_id'];
    495 			$data['payment_zone_id'] = $order_info['payment_zone_id'];
    496 			$data['payment_custom_field'] = $order_info['payment_custom_field'];
    497 			$data['payment_method'] = $order_info['payment_method'];
    498 			$data['payment_code'] = $order_info['payment_code'];
    499 
    500 			$data['shipping_firstname'] = $order_info['shipping_firstname'];
    501 			$data['shipping_lastname'] = $order_info['shipping_lastname'];
    502 			$data['shipping_company'] = $order_info['shipping_company'];
    503 			$data['shipping_address_1'] = $order_info['shipping_address_1'];
    504 			$data['shipping_address_2'] = $order_info['shipping_address_2'];
    505 			$data['shipping_city'] = $order_info['shipping_city'];
    506 			$data['shipping_postcode'] = $order_info['shipping_postcode'];
    507 			$data['shipping_country_id'] = $order_info['shipping_country_id'];
    508 			$data['shipping_zone_id'] = $order_info['shipping_zone_id'];
    509 			$data['shipping_custom_field'] = $order_info['shipping_custom_field'];
    510 			$data['shipping_method'] = $order_info['shipping_method'];
    511 			$data['shipping_code'] = $order_info['shipping_code'];
    512 
    513 			// Products
    514 			$data['order_products'] = array();
    515 
    516 			$products = $this->model_sale_order->getOrderProducts($this->request->get['order_id']);
    517 
    518 			foreach ($products as $product) {
    519 				$data['order_products'][] = array(
    520 					'product_id' => $product['product_id'],
    521 					'name'       => $product['name'],
    522 					'model'      => $product['model'],
    523 					'option'     => $this->model_sale_order->getOrderOptions($this->request->get['order_id'], $product['order_product_id']),
    524 					'quantity'   => $product['quantity'],
    525 					'price'      => $product['price'],
    526 					'total'      => $product['total'],
    527 					'reward'     => $product['reward']
    528 				);
    529 			}
    530 
    531 			// Vouchers
    532 			$data['order_vouchers'] = $this->model_sale_order->getOrderVouchers($this->request->get['order_id']);
    533 
    534 			$data['coupon'] = '';
    535 			$data['voucher'] = '';
    536 			$data['reward'] = '';
    537 
    538 			$data['order_totals'] = array();
    539 
    540 			$order_totals = $this->model_sale_order->getOrderTotals($this->request->get['order_id']);
    541 
    542 			foreach ($order_totals as $order_total) {
    543 				// If coupon, voucher or reward points
    544 				$start = strpos($order_total['title'], '(') + 1;
    545 				$end = strrpos($order_total['title'], ')');
    546 
    547 				if ($start && $end) {
    548 					$data[$order_total['code']] = substr($order_total['title'], $start, $end - $start);
    549 				}
    550 			}
    551 
    552 			$data['order_status_id'] = $order_info['order_status_id'];
    553 			$data['comment'] = $order_info['comment'];
    554 			$data['affiliate_id'] = $order_info['affiliate_id'];
    555 			$data['affiliate'] = $order_info['affiliate_firstname'] . ' ' . $order_info['affiliate_lastname'];
    556 			$data['currency_code'] = $order_info['currency_code'];
    557 		} else {
    558 			$data['order_id'] = 0;
    559 			$data['store_id'] = 0;
    560 			$data['store_url'] = $this->request->server['HTTPS'] ? HTTPS_CATALOG : HTTP_CATALOG;
    561 			
    562 			$data['customer'] = '';
    563 			$data['customer_id'] = '';
    564 			$data['customer_group_id'] = $this->config->get('config_customer_group_id');
    565 			$data['firstname'] = '';
    566 			$data['lastname'] = '';
    567 			$data['email'] = '';
    568 			$data['telephone'] = '';
    569 			$data['customer_custom_field'] = array();
    570 
    571 			$data['addresses'] = array();
    572 
    573 			$data['payment_firstname'] = '';
    574 			$data['payment_lastname'] = '';
    575 			$data['payment_company'] = '';
    576 			$data['payment_address_1'] = '';
    577 			$data['payment_address_2'] = '';
    578 			$data['payment_city'] = '';
    579 			$data['payment_postcode'] = '';
    580 			$data['payment_country_id'] = '';
    581 			$data['payment_zone_id'] = '';
    582 			$data['payment_custom_field'] = array();
    583 			$data['payment_method'] = '';
    584 			$data['payment_code'] = '';
    585 
    586 			$data['shipping_firstname'] = '';
    587 			$data['shipping_lastname'] = '';
    588 			$data['shipping_company'] = '';
    589 			$data['shipping_address_1'] = '';
    590 			$data['shipping_address_2'] = '';
    591 			$data['shipping_city'] = '';
    592 			$data['shipping_postcode'] = '';
    593 			$data['shipping_country_id'] = '';
    594 			$data['shipping_zone_id'] = '';
    595 			$data['shipping_custom_field'] = array();
    596 			$data['shipping_method'] = '';
    597 			$data['shipping_code'] = '';
    598 
    599 			$data['order_products'] = array();
    600 			$data['order_vouchers'] = array();
    601 			$data['order_totals'] = array();
    602 
    603 			$data['order_status_id'] = $this->config->get('config_order_status_id');
    604 			$data['comment'] = '';
    605 			$data['affiliate_id'] = '';
    606 			$data['affiliate'] = '';
    607 			$data['currency_code'] = $this->config->get('config_currency');
    608 
    609 			$data['coupon'] = '';
    610 			$data['voucher'] = '';
    611 			$data['reward'] = '';
    612 		}
    613 
    614 		// Stores
    615 		$this->load->model('setting/store');
    616 
    617 		$data['stores'] = array();
    618 
    619 		$data['stores'][] = array(
    620 			'store_id' => 0,
    621 			'name'     => $this->language->get('text_default')
    622 		);
    623 
    624 		$results = $this->model_setting_store->getStores();
    625 
    626 		foreach ($results as $result) {
    627 			$data['stores'][] = array(
    628 				'store_id' => $result['store_id'],
    629 				'name'     => $result['name']
    630 			);
    631 		}
    632 
    633 		// Customer Groups
    634 		$this->load->model('customer/customer_group');
    635 
    636 		$data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups();
    637 
    638 		// Custom Fields
    639 		$this->load->model('customer/custom_field');
    640 
    641 		$data['custom_fields'] = array();
    642 
    643 		$filter_data = array(
    644 			'sort'  => 'cf.sort_order',
    645 			'order' => 'ASC'
    646 		);
    647 
    648 		$custom_fields = $this->model_customer_custom_field->getCustomFields($filter_data);
    649 
    650 		foreach ($custom_fields as $custom_field) {
    651 			$data['custom_fields'][] = array(
    652 				'custom_field_id'    => $custom_field['custom_field_id'],
    653 				'custom_field_value' => $this->model_customer_custom_field->getCustomFieldValues($custom_field['custom_field_id']),
    654 				'name'               => $custom_field['name'],
    655 				'value'              => $custom_field['value'],
    656 				'type'               => $custom_field['type'],
    657 				'location'           => $custom_field['location'],
    658 				'sort_order'         => $custom_field['sort_order']
    659 			);
    660 		}
    661 
    662 		$this->load->model('localisation/order_status');
    663 
    664 		$data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
    665 
    666 		$this->load->model('localisation/country');
    667 
    668 		$data['countries'] = $this->model_localisation_country->getCountries();
    669 
    670 		$this->load->model('localisation/currency');
    671 
    672 		$data['currencies'] = $this->model_localisation_currency->getCurrencies();
    673 
    674 		$data['voucher_min'] = $this->config->get('config_voucher_min');
    675 
    676 		$this->load->model('sale/voucher_theme');
    677 
    678 		$data['voucher_themes'] = $this->model_sale_voucher_theme->getVoucherThemes();
    679 
    680 		// API login
    681 		$data['catalog'] = $this->request->server['HTTPS'] ? HTTPS_CATALOG : HTTP_CATALOG;
    682 		
    683 		// API login
    684 		$this->load->model('user/api');
    685 
    686 		$api_info = $this->model_user_api->getApi($this->config->get('config_api_id'));
    687 
    688 		if ($api_info && $this->user->hasPermission('modify', 'sale/order')) {
    689 			$session = new Session($this->config->get('session_engine'), $this->registry);
    690 			
    691 			$session->start();
    692 					
    693 			$this->model_user_api->deleteApiSessionBySessonId($session->getId());
    694 			
    695 			$this->model_user_api->addApiSession($api_info['api_id'], $session->getId(), $this->request->server['REMOTE_ADDR']);
    696 			
    697 			$session->data['api_id'] = $api_info['api_id'];
    698 
    699 			$data['api_token'] = $session->getId();
    700 		} else {
    701 			$data['api_token'] = '';
    702 		}
    703 
    704 		$data['header'] = $this->load->controller('common/header');
    705 		$data['column_left'] = $this->load->controller('common/column_left');
    706 		$data['footer'] = $this->load->controller('common/footer');
    707 
    708 		$this->response->setOutput($this->load->view('sale/order_form', $data));
    709 	}
    710 
    711 	public function info() {
    712 		$this->load->model('sale/order');
    713 
    714 		if (isset($this->request->get['order_id'])) {
    715 			$order_id = $this->request->get['order_id'];
    716 		} else {
    717 			$order_id = 0;
    718 		}
    719 
    720 		$order_info = $this->model_sale_order->getOrder($order_id);
    721 
    722 		if ($order_info) {
    723 			$this->load->language('sale/order');
    724 
    725 			$this->document->setTitle($this->language->get('heading_title'));
    726 
    727 			$data['text_ip_add'] = sprintf($this->language->get('text_ip_add'), $this->request->server['REMOTE_ADDR']);
    728 			$data['text_order'] = sprintf($this->language->get('text_order'), $this->request->get['order_id']);
    729 
    730 			$url = '';
    731 
    732 			if (isset($this->request->get['filter_order_id'])) {
    733 				$url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
    734 			}
    735 
    736 			if (isset($this->request->get['filter_customer'])) {
    737 				$url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
    738 			}
    739 
    740 			if (isset($this->request->get['filter_order_status'])) {
    741 				$url .= '&filter_order_status=' . $this->request->get['filter_order_status'];
    742 			}
    743 			
    744 			if (isset($this->request->get['filter_order_status_id'])) {
    745 				$url .= '&filter_order_status_id=' . $this->request->get['filter_order_status_id'];
    746 			}
    747 			
    748 			if (isset($this->request->get['filter_total'])) {
    749 				$url .= '&filter_total=' . $this->request->get['filter_total'];
    750 			}
    751 
    752 			if (isset($this->request->get['filter_date_added'])) {
    753 				$url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
    754 			}
    755 
    756 			if (isset($this->request->get['filter_date_modified'])) {
    757 				$url .= '&filter_date_modified=' . $this->request->get['filter_date_modified'];
    758 			}
    759 
    760 			if (isset($this->request->get['sort'])) {
    761 				$url .= '&sort=' . $this->request->get['sort'];
    762 			}
    763 
    764 			if (isset($this->request->get['order'])) {
    765 				$url .= '&order=' . $this->request->get['order'];
    766 			}
    767 
    768 			if (isset($this->request->get['page'])) {
    769 				$url .= '&page=' . $this->request->get['page'];
    770 			}
    771 
    772 			$data['breadcrumbs'] = array();
    773 
    774 			$data['breadcrumbs'][] = array(
    775 				'text' => $this->language->get('text_home'),
    776 				'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
    777 			);
    778 
    779 			$data['breadcrumbs'][] = array(
    780 				'text' => $this->language->get('heading_title'),
    781 				'href' => $this->url->link('sale/order', 'user_token=' . $this->session->data['user_token'] . $url, true)
    782 			);
    783 
    784 			$data['shipping'] = $this->url->link('sale/order/shipping', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . (int)$this->request->get['order_id'], true);
    785 			$data['invoice'] = $this->url->link('sale/order/invoice', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . (int)$this->request->get['order_id'], true);
    786 			$data['edit'] = $this->url->link('sale/order/edit', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . (int)$this->request->get['order_id'], true);
    787 			$data['cancel'] = $this->url->link('sale/order', 'user_token=' . $this->session->data['user_token'] . $url, true);
    788 
    789 			$data['user_token'] = $this->session->data['user_token'];
    790 
    791 			$data['order_id'] = $this->request->get['order_id'];
    792 
    793 			$data['store_id'] = $order_info['store_id'];
    794 			$data['store_name'] = $order_info['store_name'];
    795 			
    796 			if ($order_info['store_id'] == 0) {
    797 				$data['store_url'] = $this->request->server['HTTPS'] ? HTTPS_CATALOG : HTTP_CATALOG;
    798 			} else {
    799 				$data['store_url'] = $order_info['store_url'];
    800 			}
    801 
    802 			if ($order_info['invoice_no']) {
    803 				$data['invoice_no'] = $order_info['invoice_prefix'] . $order_info['invoice_no'];
    804 			} else {
    805 				$data['invoice_no'] = '';
    806 			}
    807 
    808 			$data['date_added'] = date($this->language->get('date_format_short'), strtotime($order_info['date_added']));
    809 
    810 			$data['firstname'] = $order_info['firstname'];
    811 			$data['lastname'] = $order_info['lastname'];
    812 
    813 			if ($order_info['customer_id']) {
    814 				$data['customer'] = $this->url->link('customer/customer/edit', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . $order_info['customer_id'], true);
    815 			} else {
    816 				$data['customer'] = '';
    817 			}
    818 
    819 			$this->load->model('customer/customer_group');
    820 
    821 			$customer_group_info = $this->model_customer_customer_group->getCustomerGroup($order_info['customer_group_id']);
    822 
    823 			if ($customer_group_info) {
    824 				$data['customer_group'] = $customer_group_info['name'];
    825 			} else {
    826 				$data['customer_group'] = '';
    827 			}
    828 
    829 			$data['email'] = $order_info['email'];
    830 			$data['telephone'] = $order_info['telephone'];
    831 
    832 			$data['shipping_method'] = $order_info['shipping_method'];
    833 			$data['payment_method'] = $order_info['payment_method'];
    834 
    835 			// Payment Address
    836 			if ($order_info['payment_address_format']) {
    837 				$format = $order_info['payment_address_format'];
    838 			} else {
    839 				$format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
    840 			}
    841 
    842 			$find = array(
    843 				'{firstname}',
    844 				'{lastname}',
    845 				'{company}',
    846 				'{address_1}',
    847 				'{address_2}',
    848 				'{city}',
    849 				'{postcode}',
    850 				'{zone}',
    851 				'{zone_code}',
    852 				'{country}'
    853 			);
    854 
    855 			$replace = array(
    856 				'firstname' => $order_info['payment_firstname'],
    857 				'lastname'  => $order_info['payment_lastname'],
    858 				'company'   => $order_info['payment_company'],
    859 				'address_1' => $order_info['payment_address_1'],
    860 				'address_2' => $order_info['payment_address_2'],
    861 				'city'      => $order_info['payment_city'],
    862 				'postcode'  => $order_info['payment_postcode'],
    863 				'zone'      => $order_info['payment_zone'],
    864 				'zone_code' => $order_info['payment_zone_code'],
    865 				'country'   => $order_info['payment_country']
    866 			);
    867 
    868 			$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))));
    869 
    870 			// Shipping Address
    871 			if ($order_info['shipping_address_format']) {
    872 				$format = $order_info['shipping_address_format'];
    873 			} else {
    874 				$format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
    875 			}
    876 
    877 			$find = array(
    878 				'{firstname}',
    879 				'{lastname}',
    880 				'{company}',
    881 				'{address_1}',
    882 				'{address_2}',
    883 				'{city}',
    884 				'{postcode}',
    885 				'{zone}',
    886 				'{zone_code}',
    887 				'{country}'
    888 			);
    889 
    890 			$replace = array(
    891 				'firstname' => $order_info['shipping_firstname'],
    892 				'lastname'  => $order_info['shipping_lastname'],
    893 				'company'   => $order_info['shipping_company'],
    894 				'address_1' => $order_info['shipping_address_1'],
    895 				'address_2' => $order_info['shipping_address_2'],
    896 				'city'      => $order_info['shipping_city'],
    897 				'postcode'  => $order_info['shipping_postcode'],
    898 				'zone'      => $order_info['shipping_zone'],
    899 				'zone_code' => $order_info['shipping_zone_code'],
    900 				'country'   => $order_info['shipping_country']
    901 			);
    902 
    903 			$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))));
    904 
    905 			// Uploaded files
    906 			$this->load->model('tool/upload');
    907 
    908 			$data['products'] = array();
    909 
    910 			$products = $this->model_sale_order->getOrderProducts($this->request->get['order_id']);
    911 
    912 			foreach ($products as $product) {
    913 				$option_data = array();
    914 
    915 				$options = $this->model_sale_order->getOrderOptions($this->request->get['order_id'], $product['order_product_id']);
    916 
    917 				foreach ($options as $option) {
    918 					if ($option['type'] != 'file') {
    919 						$option_data[] = array(
    920 							'name'  => $option['name'],
    921 							'value' => $option['value'],
    922 							'type'  => $option['type']
    923 						);
    924 					} else {
    925 						$upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
    926 
    927 						if ($upload_info) {
    928 							$option_data[] = array(
    929 								'name'  => $option['name'],
    930 								'value' => $upload_info['name'],
    931 								'type'  => $option['type'],
    932 								'href'  => $this->url->link('tool/upload/download', 'user_token=' . $this->session->data['user_token'] . '&code=' . $upload_info['code'], true)
    933 							);
    934 						}
    935 					}
    936 				}
    937 
    938 				$data['products'][] = array(
    939 					'order_product_id' => $product['order_product_id'],
    940 					'product_id'       => $product['product_id'],
    941 					'name'    	 	   => $product['name'],
    942 					'model'    		   => $product['model'],
    943 					'option'   		   => $option_data,
    944 					'quantity'		   => $product['quantity'],
    945 					'price'    		   => $this->currency->format($product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']),
    946 					'total'    		   => $this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value']),
    947 					'href'     		   => $this->url->link('catalog/product/edit', 'user_token=' . $this->session->data['user_token'] . '&product_id=' . $product['product_id'], true)
    948 				);
    949 			}
    950 
    951 			$data['vouchers'] = array();
    952 
    953 			$vouchers = $this->model_sale_order->getOrderVouchers($this->request->get['order_id']);
    954 
    955 			foreach ($vouchers as $voucher) {
    956 				$data['vouchers'][] = array(
    957 					'description' => $voucher['description'],
    958 					'amount'      => $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']),
    959 					'href'        => $this->url->link('sale/voucher/edit', 'user_token=' . $this->session->data['user_token'] . '&voucher_id=' . $voucher['voucher_id'], true)
    960 				);
    961 			}
    962 
    963 			$data['totals'] = array();
    964 
    965 			$totals = $this->model_sale_order->getOrderTotals($this->request->get['order_id']);
    966 
    967 			foreach ($totals as $total) {
    968 				$data['totals'][] = array(
    969 					'title' => $total['title'],
    970 					'text'  => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value'])
    971 				);
    972 			}
    973 
    974 			$data['comment'] = nl2br($order_info['comment']);
    975 
    976 			$this->load->model('customer/customer');
    977 
    978 			$data['reward'] = $order_info['reward'];
    979 
    980 			$data['reward_total'] = $this->model_customer_customer->getTotalCustomerRewardsByOrderId($this->request->get['order_id']);
    981 
    982 			$data['affiliate_firstname'] = $order_info['affiliate_firstname'];
    983 			$data['affiliate_lastname'] = $order_info['affiliate_lastname'];
    984 
    985 			if ($order_info['affiliate_id']) {
    986 				$data['affiliate'] = $this->url->link('customer/customer/edit', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . $order_info['affiliate_id'], true);
    987 			} else {
    988 				$data['affiliate'] = '';
    989 			}
    990 
    991 			$data['commission'] = $this->currency->format($order_info['commission'], $order_info['currency_code'], $order_info['currency_value']);
    992 
    993 			$this->load->model('customer/customer');
    994 
    995 			$data['commission_total'] = $this->model_customer_customer->getTotalTransactionsByOrderId($this->request->get['order_id']);
    996 
    997 			$this->load->model('localisation/order_status');
    998 
    999 			$order_status_info = $this->model_localisation_order_status->getOrderStatus($order_info['order_status_id']);
   1000 
   1001 			if ($order_status_info) {
   1002 				$data['order_status'] = $order_status_info['name'];
   1003 			} else {
   1004 				$data['order_status'] = '';
   1005 			}
   1006 
   1007 			$data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
   1008 
   1009 			$data['order_status_id'] = $order_info['order_status_id'];
   1010 
   1011 			$data['account_custom_field'] = $order_info['custom_field'];
   1012 
   1013 			// Custom Fields
   1014 			$this->load->model('customer/custom_field');
   1015 
   1016 			$data['account_custom_fields'] = array();
   1017 
   1018 			$filter_data = array(
   1019 				'sort'  => 'cf.sort_order',
   1020 				'order' => 'ASC'
   1021 			);
   1022 
   1023 			$custom_fields = $this->model_customer_custom_field->getCustomFields($filter_data);
   1024 
   1025 			foreach ($custom_fields as $custom_field) {
   1026 				if ($custom_field['location'] == 'account' && isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
   1027 					if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio') {
   1028 						$custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($order_info['custom_field'][$custom_field['custom_field_id']]);
   1029 
   1030 						if ($custom_field_value_info) {
   1031 							$data['account_custom_fields'][] = array(
   1032 								'name'  => $custom_field['name'],
   1033 								'value' => $custom_field_value_info['name']
   1034 							);
   1035 						}
   1036 					}
   1037 
   1038 					if ($custom_field['type'] == 'checkbox' && is_array($order_info['custom_field'][$custom_field['custom_field_id']])) {
   1039 						foreach ($order_info['custom_field'][$custom_field['custom_field_id']] as $custom_field_value_id) {
   1040 							$custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($custom_field_value_id);
   1041 
   1042 							if ($custom_field_value_info) {
   1043 								$data['account_custom_fields'][] = array(
   1044 									'name'  => $custom_field['name'],
   1045 									'value' => $custom_field_value_info['name']
   1046 								);
   1047 							}
   1048 						}
   1049 					}
   1050 
   1051 					if ($custom_field['type'] == 'text' || $custom_field['type'] == 'textarea' || $custom_field['type'] == 'file' || $custom_field['type'] == 'date' || $custom_field['type'] == 'datetime' || $custom_field['type'] == 'time') {
   1052 						$data['account_custom_fields'][] = array(
   1053 							'name'  => $custom_field['name'],
   1054 							'value' => $order_info['custom_field'][$custom_field['custom_field_id']]
   1055 						);
   1056 					}
   1057 
   1058 					if ($custom_field['type'] == 'file') {
   1059 						$upload_info = $this->model_tool_upload->getUploadByCode($order_info['custom_field'][$custom_field['custom_field_id']]);
   1060 
   1061 						if ($upload_info) {
   1062 							$data['account_custom_fields'][] = array(
   1063 								'name'  => $custom_field['name'],
   1064 								'value' => $upload_info['name']
   1065 							);
   1066 						}
   1067 					}
   1068 				}
   1069 			}
   1070 
   1071 			// Custom fields
   1072 			$data['payment_custom_fields'] = array();
   1073 
   1074 			foreach ($custom_fields as $custom_field) {
   1075 				if ($custom_field['location'] == 'address' && isset($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
   1076 					if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio') {
   1077 						$custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($order_info['payment_custom_field'][$custom_field['custom_field_id']]);
   1078 
   1079 						if ($custom_field_value_info) {
   1080 							$data['payment_custom_fields'][] = array(
   1081 								'name'  => $custom_field['name'],
   1082 								'value' => $custom_field_value_info['name'],
   1083 								'sort_order' => $custom_field['sort_order']
   1084 							);
   1085 						}
   1086 					}
   1087 
   1088 					if ($custom_field['type'] == 'checkbox' && is_array($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
   1089 						foreach ($order_info['payment_custom_field'][$custom_field['custom_field_id']] as $custom_field_value_id) {
   1090 							$custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($custom_field_value_id);
   1091 
   1092 							if ($custom_field_value_info) {
   1093 								$data['payment_custom_fields'][] = array(
   1094 									'name'  => $custom_field['name'],
   1095 									'value' => $custom_field_value_info['name'],
   1096 									'sort_order' => $custom_field['sort_order']
   1097 								);
   1098 							}
   1099 						}
   1100 					}
   1101 
   1102 					if ($custom_field['type'] == 'text' || $custom_field['type'] == 'textarea' || $custom_field['type'] == 'file' || $custom_field['type'] == 'date' || $custom_field['type'] == 'datetime' || $custom_field['type'] == 'time') {
   1103 						$data['payment_custom_fields'][] = array(
   1104 							'name'  => $custom_field['name'],
   1105 							'value' => $order_info['payment_custom_field'][$custom_field['custom_field_id']],
   1106 							'sort_order' => $custom_field['sort_order']
   1107 						);
   1108 					}
   1109 
   1110 					if ($custom_field['type'] == 'file') {
   1111 						$upload_info = $this->model_tool_upload->getUploadByCode($order_info['payment_custom_field'][$custom_field['custom_field_id']]);
   1112 
   1113 						if ($upload_info) {
   1114 							$data['payment_custom_fields'][] = array(
   1115 								'name'  => $custom_field['name'],
   1116 								'value' => $upload_info['name'],
   1117 								'sort_order' => $custom_field['sort_order']
   1118 							);
   1119 						}
   1120 					}
   1121 				}
   1122 			}
   1123 
   1124 			// Shipping
   1125 			$data['shipping_custom_fields'] = array();
   1126 
   1127 			foreach ($custom_fields as $custom_field) {
   1128 				if ($custom_field['location'] == 'address' && isset($order_info['shipping_custom_field'][$custom_field['custom_field_id']])) {
   1129 					if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio') {
   1130 						$custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($order_info['shipping_custom_field'][$custom_field['custom_field_id']]);
   1131 
   1132 						if ($custom_field_value_info) {
   1133 							$data['shipping_custom_fields'][] = array(
   1134 								'name'  => $custom_field['name'],
   1135 								'value' => $custom_field_value_info['name'],
   1136 								'sort_order' => $custom_field['sort_order']
   1137 							);
   1138 						}
   1139 					}
   1140 
   1141 					if ($custom_field['type'] == 'checkbox' && is_array($order_info['shipping_custom_field'][$custom_field['custom_field_id']])) {
   1142 						foreach ($order_info['shipping_custom_field'][$custom_field['custom_field_id']] as $custom_field_value_id) {
   1143 							$custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($custom_field_value_id);
   1144 
   1145 							if ($custom_field_value_info) {
   1146 								$data['shipping_custom_fields'][] = array(
   1147 									'name'  => $custom_field['name'],
   1148 									'value' => $custom_field_value_info['name'],
   1149 									'sort_order' => $custom_field['sort_order']
   1150 								);
   1151 							}
   1152 						}
   1153 					}
   1154 
   1155 					if ($custom_field['type'] == 'text' || $custom_field['type'] == 'textarea' || $custom_field['type'] == 'file' || $custom_field['type'] == 'date' || $custom_field['type'] == 'datetime' || $custom_field['type'] == 'time') {
   1156 						$data['shipping_custom_fields'][] = array(
   1157 							'name'  => $custom_field['name'],
   1158 							'value' => $order_info['shipping_custom_field'][$custom_field['custom_field_id']],
   1159 							'sort_order' => $custom_field['sort_order']
   1160 						);
   1161 					}
   1162 
   1163 					if ($custom_field['type'] == 'file') {
   1164 						$upload_info = $this->model_tool_upload->getUploadByCode($order_info['shipping_custom_field'][$custom_field['custom_field_id']]);
   1165 
   1166 						if ($upload_info) {
   1167 							$data['shipping_custom_fields'][] = array(
   1168 								'name'  => $custom_field['name'],
   1169 								'value' => $upload_info['name'],
   1170 								'sort_order' => $custom_field['sort_order']
   1171 							);
   1172 						}
   1173 					}
   1174 				}
   1175 			}
   1176 
   1177 			$data['ip'] = $order_info['ip'];
   1178 			$data['forwarded_ip'] = $order_info['forwarded_ip'];
   1179 			$data['user_agent'] = $order_info['user_agent'];
   1180 			$data['accept_language'] = $order_info['accept_language'];
   1181 
   1182 			// Additional Tabs
   1183 			$data['tabs'] = array();
   1184 
   1185 			if ($this->user->hasPermission('access', 'extension/payment/' . $order_info['payment_code'])) {
   1186 				if (is_file(DIR_CATALOG . 'controller/extension/payment/' . $order_info['payment_code'] . '.php')) {
   1187 					$content = $this->load->controller('extension/payment/' . $order_info['payment_code'] . '/order');
   1188 				} else {
   1189 					$content = '';
   1190 				}
   1191 
   1192 				if ($content) {
   1193 					$this->load->language('extension/payment/' . $order_info['payment_code']);
   1194 
   1195 					$data['tabs'][] = array(
   1196 						'code'    => $order_info['payment_code'],
   1197 						'title'   => $this->language->get('heading_title'),
   1198 						'content' => $content
   1199 					);
   1200 				}
   1201 			}
   1202 
   1203 			$this->load->model('setting/extension');
   1204 
   1205 			$extensions = $this->model_setting_extension->getInstalled('fraud');
   1206 
   1207 			foreach ($extensions as $extension) {
   1208 				if ($this->config->get('fraud_' . $extension . '_status')) {
   1209 					$this->load->language('extension/fraud/' . $extension, 'extension');
   1210 
   1211 					$content = $this->load->controller('extension/fraud/' . $extension . '/order');
   1212 
   1213 					if ($content) {
   1214 						$data['tabs'][] = array(
   1215 							'code'    => $extension,
   1216 							'title'   => $this->language->get('extension')->get('heading_title'),
   1217 							'content' => $content
   1218 						);
   1219 					}
   1220 				}
   1221 			}
   1222 			
   1223 			// The URL we send API requests to
   1224 			$data['catalog'] = $this->request->server['HTTPS'] ? HTTPS_CATALOG : HTTP_CATALOG;
   1225 			
   1226 			// API login
   1227 			$this->load->model('user/api');
   1228 
   1229 			$api_info = $this->model_user_api->getApi($this->config->get('config_api_id'));
   1230 
   1231 			if ($api_info && $this->user->hasPermission('modify', 'sale/order')) {
   1232 				$session = new Session($this->config->get('session_engine'), $this->registry);
   1233 				
   1234 				$session->start();
   1235 				
   1236 				$this->model_user_api->deleteApiSessionBySessonId($session->getId());
   1237 				
   1238 				$this->model_user_api->addApiSession($api_info['api_id'], $session->getId(), $this->request->server['REMOTE_ADDR']);
   1239 				
   1240 				$session->data['api_id'] = $api_info['api_id'];
   1241 
   1242 				$data['api_token'] = $session->getId();
   1243 			} else {
   1244 				$data['api_token'] = '';
   1245 			}
   1246 
   1247 			$data['header'] = $this->load->controller('common/header');
   1248 			$data['column_left'] = $this->load->controller('common/column_left');
   1249 			$data['footer'] = $this->load->controller('common/footer');
   1250 
   1251 			$this->response->setOutput($this->load->view('sale/order_info', $data));
   1252 		} else {
   1253 			return new Action('error/not_found');
   1254 		}
   1255 	}
   1256 	
   1257 	protected function validate() {
   1258 		if (!$this->user->hasPermission('modify', 'sale/order')) {
   1259 			$this->error['warning'] = $this->language->get('error_permission');
   1260 		}
   1261 
   1262 		return !$this->error;
   1263 	}
   1264 	
   1265 	public function createInvoiceNo() {
   1266 		$this->load->language('sale/order');
   1267 
   1268 		$json = array();
   1269 
   1270 		if (!$this->user->hasPermission('modify', 'sale/order')) {
   1271 			$json['error'] = $this->language->get('error_permission');
   1272 		} elseif (isset($this->request->get['order_id'])) {
   1273 			if (isset($this->request->get['order_id'])) {
   1274 				$order_id = $this->request->get['order_id'];
   1275 			} else {
   1276 				$order_id = 0;
   1277 			}
   1278 
   1279 			$this->load->model('sale/order');
   1280 
   1281 			$invoice_no = $this->model_sale_order->createInvoiceNo($order_id);
   1282 
   1283 			if ($invoice_no) {
   1284 				$json['invoice_no'] = $invoice_no;
   1285 			} else {
   1286 				$json['error'] = $this->language->get('error_action');
   1287 			}
   1288 		}
   1289 
   1290 		$this->response->addHeader('Content-Type: application/json');
   1291 		$this->response->setOutput(json_encode($json));
   1292 	}
   1293 
   1294 	public function addReward() {
   1295 		$this->load->language('sale/order');
   1296 
   1297 		$json = array();
   1298 
   1299 		if (!$this->user->hasPermission('modify', 'sale/order')) {
   1300 			$json['error'] = $this->language->get('error_permission');
   1301 		} else {
   1302 			if (isset($this->request->get['order_id'])) {
   1303 				$order_id = $this->request->get['order_id'];
   1304 			} else {
   1305 				$order_id = 0;
   1306 			}
   1307 
   1308 			$this->load->model('sale/order');
   1309 
   1310 			$order_info = $this->model_sale_order->getOrder($order_id);
   1311 
   1312 			if ($order_info && $order_info['customer_id'] && ($order_info['reward'] > 0)) {
   1313 				$this->load->model('customer/customer');
   1314 
   1315 				$reward_total = $this->model_customer_customer->getTotalCustomerRewardsByOrderId($order_id);
   1316 
   1317 				if (!$reward_total) {
   1318 					$this->model_customer_customer->addReward($order_info['customer_id'], $this->language->get('text_order_id') . ' #' . $order_id, $order_info['reward'], $order_id);
   1319 				}
   1320 			}
   1321 
   1322 			$json['success'] = $this->language->get('text_reward_added');
   1323 		}
   1324 
   1325 		$this->response->addHeader('Content-Type: application/json');
   1326 		$this->response->setOutput(json_encode($json));
   1327 	}
   1328 
   1329 	public function removeReward() {
   1330 		$this->load->language('sale/order');
   1331 
   1332 		$json = array();
   1333 
   1334 		if (!$this->user->hasPermission('modify', 'sale/order')) {
   1335 			$json['error'] = $this->language->get('error_permission');
   1336 		} else {
   1337 			if (isset($this->request->get['order_id'])) {
   1338 				$order_id = $this->request->get['order_id'];
   1339 			} else {
   1340 				$order_id = 0;
   1341 			}
   1342 
   1343 			$this->load->model('sale/order');
   1344 
   1345 			$order_info = $this->model_sale_order->getOrder($order_id);
   1346 
   1347 			if ($order_info) {
   1348 				$this->load->model('customer/customer');
   1349 
   1350 				$this->model_customer_customer->deleteReward($order_id);
   1351 			}
   1352 
   1353 			$json['success'] = $this->language->get('text_reward_removed');
   1354 		}
   1355 
   1356 		$this->response->addHeader('Content-Type: application/json');
   1357 		$this->response->setOutput(json_encode($json));
   1358 	}
   1359 
   1360 	public function addCommission() {
   1361 		$this->load->language('sale/order');
   1362 
   1363 		$json = array();
   1364 
   1365 		if (!$this->user->hasPermission('modify', 'sale/order')) {
   1366 			$json['error'] = $this->language->get('error_permission');
   1367 		} else {
   1368 			if (isset($this->request->get['order_id'])) {
   1369 				$order_id = $this->request->get['order_id'];
   1370 			} else {
   1371 				$order_id = 0;
   1372 			}
   1373 
   1374 			$this->load->model('sale/order');
   1375 
   1376 			$order_info = $this->model_sale_order->getOrder($order_id);
   1377 
   1378 			if ($order_info) {
   1379 				$this->load->model('customer/customer');
   1380 
   1381 				$affiliate_total = $this->model_customer_customer->getTotalTransactionsByOrderId($order_id);
   1382 
   1383 				if (!$affiliate_total) {
   1384 					$this->model_customer_customer->addTransaction($order_info['affiliate_id'], $this->language->get('text_order_id') . ' #' . $order_id, $order_info['commission'], $order_id);
   1385 				}
   1386 			}
   1387 
   1388 			$json['success'] = $this->language->get('text_commission_added');
   1389 		}
   1390 
   1391 		$this->response->addHeader('Content-Type: application/json');
   1392 		$this->response->setOutput(json_encode($json));
   1393 	}
   1394 
   1395 	public function removeCommission() {
   1396 		$this->load->language('sale/order');
   1397 
   1398 		$json = array();
   1399 
   1400 		if (!$this->user->hasPermission('modify', 'sale/order')) {
   1401 			$json['error'] = $this->language->get('error_permission');
   1402 		} else {
   1403 			if (isset($this->request->get['order_id'])) {
   1404 				$order_id = $this->request->get['order_id'];
   1405 			} else {
   1406 				$order_id = 0;
   1407 			}
   1408 
   1409 			$this->load->model('sale/order');
   1410 
   1411 			$order_info = $this->model_sale_order->getOrder($order_id);
   1412 
   1413 			if ($order_info) {
   1414 				$this->load->model('customer/customer');
   1415 
   1416 				$this->model_customer_customer->deleteTransactionByOrderId($order_id);
   1417 			}
   1418 
   1419 			$json['success'] = $this->language->get('text_commission_removed');
   1420 		}
   1421 
   1422 		$this->response->addHeader('Content-Type: application/json');
   1423 		$this->response->setOutput(json_encode($json));
   1424 	}
   1425 
   1426 	public function history() {
   1427 		$this->load->language('sale/order');
   1428 
   1429 		if (isset($this->request->get['page'])) {
   1430 			$page = $this->request->get['page'];
   1431 		} else {
   1432 			$page = 1;
   1433 		}
   1434 
   1435 		$data['histories'] = array();
   1436 
   1437 		$this->load->model('sale/order');
   1438 
   1439 		$results = $this->model_sale_order->getOrderHistories($this->request->get['order_id'], ($page - 1) * 10, 10);
   1440 
   1441 		foreach ($results as $result) {
   1442 			$data['histories'][] = array(
   1443 				'notify'     => $result['notify'] ? $this->language->get('text_yes') : $this->language->get('text_no'),
   1444 				'status'     => $result['status'],
   1445 				'comment'    => nl2br($result['comment']),
   1446 				'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
   1447 			);
   1448 		}
   1449 
   1450 		$history_total = $this->model_sale_order->getTotalOrderHistories($this->request->get['order_id']);
   1451 
   1452 		$pagination = new Pagination();
   1453 		$pagination->total = $history_total;
   1454 		$pagination->page = $page;
   1455 		$pagination->limit = 10;
   1456 		$pagination->url = $this->url->link('sale/order/history', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $this->request->get['order_id'] . '&page={page}', true);
   1457 
   1458 		$data['pagination'] = $pagination->render();
   1459 
   1460 		$data['results'] = sprintf($this->language->get('text_pagination'), ($history_total) ? (($page - 1) * 10) + 1 : 0, ((($page - 1) * 10) > ($history_total - 10)) ? $history_total : ((($page - 1) * 10) + 10), $history_total, ceil($history_total / 10));
   1461 
   1462 		$this->response->setOutput($this->load->view('sale/order_history', $data));
   1463 	}
   1464 
   1465 	public function invoice() {
   1466 		$this->load->language('sale/order');
   1467 
   1468 		$data['title'] = $this->language->get('text_invoice');
   1469 
   1470 		if ($this->request->server['HTTPS']) {
   1471 			$data['base'] = HTTPS_SERVER;
   1472 		} else {
   1473 			$data['base'] = HTTP_SERVER;
   1474 		}
   1475 
   1476 		$data['direction'] = $this->language->get('direction');
   1477 		$data['lang'] = $this->language->get('code');
   1478 
   1479 		$this->load->model('sale/order');
   1480 
   1481 		$this->load->model('setting/setting');
   1482 
   1483 		$data['orders'] = array();
   1484 
   1485 		$orders = array();
   1486 
   1487 		if (isset($this->request->post['selected'])) {
   1488 			$orders = $this->request->post['selected'];
   1489 		} elseif (isset($this->request->get['order_id'])) {
   1490 			$orders[] = $this->request->get['order_id'];
   1491 		}
   1492 
   1493 		foreach ($orders as $order_id) {
   1494 			$order_info = $this->model_sale_order->getOrder($order_id);
   1495 
   1496 			if ($order_info) {
   1497 				$store_info = $this->model_setting_setting->getSetting('config', $order_info['store_id']);
   1498 
   1499 				if ($store_info) {
   1500 					$store_address = $store_info['config_address'];
   1501 					$store_email = $store_info['config_email'];
   1502 					$store_telephone = $store_info['config_telephone'];
   1503 					$store_fax = $store_info['config_fax'];
   1504 				} else {
   1505 					$store_address = $this->config->get('config_address');
   1506 					$store_email = $this->config->get('config_email');
   1507 					$store_telephone = $this->config->get('config_telephone');
   1508 					$store_fax = $this->config->get('config_fax');
   1509 				}
   1510 
   1511 				if ($order_info['invoice_no']) {
   1512 					$invoice_no = $order_info['invoice_prefix'] . $order_info['invoice_no'];
   1513 				} else {
   1514 					$invoice_no = '';
   1515 				}
   1516 
   1517 				if ($order_info['payment_address_format']) {
   1518 					$format = $order_info['payment_address_format'];
   1519 				} else {
   1520 					$format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
   1521 				}
   1522 
   1523 				$find = array(
   1524 					'{firstname}',
   1525 					'{lastname}',
   1526 					'{company}',
   1527 					'{address_1}',
   1528 					'{address_2}',
   1529 					'{city}',
   1530 					'{postcode}',
   1531 					'{zone}',
   1532 					'{zone_code}',
   1533 					'{country}'
   1534 				);
   1535 
   1536 				$replace = array(
   1537 					'firstname' => $order_info['payment_firstname'],
   1538 					'lastname'  => $order_info['payment_lastname'],
   1539 					'company'   => $order_info['payment_company'],
   1540 					'address_1' => $order_info['payment_address_1'],
   1541 					'address_2' => $order_info['payment_address_2'],
   1542 					'city'      => $order_info['payment_city'],
   1543 					'postcode'  => $order_info['payment_postcode'],
   1544 					'zone'      => $order_info['payment_zone'],
   1545 					'zone_code' => $order_info['payment_zone_code'],
   1546 					'country'   => $order_info['payment_country']
   1547 				);
   1548 
   1549 				$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))));
   1550 
   1551 				if ($order_info['shipping_address_format']) {
   1552 					$format = $order_info['shipping_address_format'];
   1553 				} else {
   1554 					$format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
   1555 				}
   1556 
   1557 				$find = array(
   1558 					'{firstname}',
   1559 					'{lastname}',
   1560 					'{company}',
   1561 					'{address_1}',
   1562 					'{address_2}',
   1563 					'{city}',
   1564 					'{postcode}',
   1565 					'{zone}',
   1566 					'{zone_code}',
   1567 					'{country}'
   1568 				);
   1569 
   1570 				$replace = array(
   1571 					'firstname' => $order_info['shipping_firstname'],
   1572 					'lastname'  => $order_info['shipping_lastname'],
   1573 					'company'   => $order_info['shipping_company'],
   1574 					'address_1' => $order_info['shipping_address_1'],
   1575 					'address_2' => $order_info['shipping_address_2'],
   1576 					'city'      => $order_info['shipping_city'],
   1577 					'postcode'  => $order_info['shipping_postcode'],
   1578 					'zone'      => $order_info['shipping_zone'],
   1579 					'zone_code' => $order_info['shipping_zone_code'],
   1580 					'country'   => $order_info['shipping_country']
   1581 				);
   1582 
   1583 				$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))));
   1584 
   1585 				$this->load->model('tool/upload');
   1586 
   1587 				$product_data = array();
   1588 
   1589 				$products = $this->model_sale_order->getOrderProducts($order_id);
   1590 
   1591 				foreach ($products as $product) {
   1592 					$option_data = array();
   1593 
   1594 					$options = $this->model_sale_order->getOrderOptions($order_id, $product['order_product_id']);
   1595 
   1596 					foreach ($options as $option) {
   1597 						if ($option['type'] != 'file') {
   1598 							$value = $option['value'];
   1599 						} else {
   1600 							$upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
   1601 
   1602 							if ($upload_info) {
   1603 								$value = $upload_info['name'];
   1604 							} else {
   1605 								$value = '';
   1606 							}
   1607 						}
   1608 
   1609 						$option_data[] = array(
   1610 							'name'  => $option['name'],
   1611 							'value' => $value
   1612 						);
   1613 					}
   1614 
   1615 					$product_data[] = array(
   1616 						'name'     => $product['name'],
   1617 						'model'    => $product['model'],
   1618 						'option'   => $option_data,
   1619 						'quantity' => $product['quantity'],
   1620 						'price'    => $this->currency->format($product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']),
   1621 						'total'    => $this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value'])
   1622 					);
   1623 				}
   1624 
   1625 				$voucher_data = array();
   1626 
   1627 				$vouchers = $this->model_sale_order->getOrderVouchers($order_id);
   1628 
   1629 				foreach ($vouchers as $voucher) {
   1630 					$voucher_data[] = array(
   1631 						'description' => $voucher['description'],
   1632 						'amount'      => $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value'])
   1633 					);
   1634 				}
   1635 
   1636 				$total_data = array();
   1637 
   1638 				$totals = $this->model_sale_order->getOrderTotals($order_id);
   1639 
   1640 				foreach ($totals as $total) {
   1641 					$total_data[] = array(
   1642 						'title' => $total['title'],
   1643 						'text'  => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value'])
   1644 					);
   1645 				}
   1646 
   1647 				$data['orders'][] = array(
   1648 					'order_id'	       => $order_id,
   1649 					'invoice_no'       => $invoice_no,
   1650 					'date_added'       => date($this->language->get('date_format_short'), strtotime($order_info['date_added'])),
   1651 					'store_name'       => $order_info['store_name'],
   1652 					'store_url'        => rtrim($order_info['store_url'], '/'),
   1653 					'store_address'    => nl2br($store_address),
   1654 					'store_email'      => $store_email,
   1655 					'store_telephone'  => $store_telephone,
   1656 					'store_fax'        => $store_fax,
   1657 					'email'            => $order_info['email'],
   1658 					'telephone'        => $order_info['telephone'],
   1659 					'shipping_address' => $shipping_address,
   1660 					'shipping_method'  => $order_info['shipping_method'],
   1661 					'payment_address'  => $payment_address,
   1662 					'payment_method'   => $order_info['payment_method'],
   1663 					'product'          => $product_data,
   1664 					'voucher'          => $voucher_data,
   1665 					'total'            => $total_data,
   1666 					'comment'          => nl2br($order_info['comment'])
   1667 				);
   1668 			}
   1669 		}
   1670 
   1671 		$this->response->setOutput($this->load->view('sale/order_invoice', $data));
   1672 	}
   1673 
   1674 	public function shipping() {
   1675 		$this->load->language('sale/order');
   1676 
   1677 		$data['title'] = $this->language->get('text_shipping');
   1678 
   1679 		if ($this->request->server['HTTPS']) {
   1680 			$data['base'] = HTTPS_SERVER;
   1681 		} else {
   1682 			$data['base'] = HTTP_SERVER;
   1683 		}
   1684 
   1685 		$data['direction'] = $this->language->get('direction');
   1686 		$data['lang'] = $this->language->get('code');
   1687 
   1688 		$this->load->model('sale/order');
   1689 
   1690 		$this->load->model('catalog/product');
   1691 
   1692 		$this->load->model('setting/setting');
   1693 
   1694 		$data['orders'] = array();
   1695 
   1696 		$orders = array();
   1697 
   1698 		if (isset($this->request->post['selected'])) {
   1699 			$orders = $this->request->post['selected'];
   1700 		} elseif (isset($this->request->get['order_id'])) {
   1701 			$orders[] = $this->request->get['order_id'];
   1702 		}
   1703 
   1704 		foreach ($orders as $order_id) {
   1705 			$order_info = $this->model_sale_order->getOrder($order_id);
   1706 
   1707 			// Make sure there is a shipping method
   1708 			if ($order_info && $order_info['shipping_code']) {
   1709 				$store_info = $this->model_setting_setting->getSetting('config', $order_info['store_id']);
   1710 
   1711 				if ($store_info) {
   1712 					$store_address = $store_info['config_address'];
   1713 					$store_email = $store_info['config_email'];
   1714 					$store_telephone = $store_info['config_telephone'];
   1715 				} else {
   1716 					$store_address = $this->config->get('config_address');
   1717 					$store_email = $this->config->get('config_email');
   1718 					$store_telephone = $this->config->get('config_telephone');
   1719 				}
   1720 
   1721 				if ($order_info['invoice_no']) {
   1722 					$invoice_no = $order_info['invoice_prefix'] . $order_info['invoice_no'];
   1723 				} else {
   1724 					$invoice_no = '';
   1725 				}
   1726 
   1727 				if ($order_info['shipping_address_format']) {
   1728 					$format = $order_info['shipping_address_format'];
   1729 				} else {
   1730 					$format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
   1731 				}
   1732 
   1733 				$find = array(
   1734 					'{firstname}',
   1735 					'{lastname}',
   1736 					'{company}',
   1737 					'{address_1}',
   1738 					'{address_2}',
   1739 					'{city}',
   1740 					'{postcode}',
   1741 					'{zone}',
   1742 					'{zone_code}',
   1743 					'{country}'
   1744 				);
   1745 
   1746 				$replace = array(
   1747 					'firstname' => $order_info['shipping_firstname'],
   1748 					'lastname'  => $order_info['shipping_lastname'],
   1749 					'company'   => $order_info['shipping_company'],
   1750 					'address_1' => $order_info['shipping_address_1'],
   1751 					'address_2' => $order_info['shipping_address_2'],
   1752 					'city'      => $order_info['shipping_city'],
   1753 					'postcode'  => $order_info['shipping_postcode'],
   1754 					'zone'      => $order_info['shipping_zone'],
   1755 					'zone_code' => $order_info['shipping_zone_code'],
   1756 					'country'   => $order_info['shipping_country']
   1757 				);
   1758 
   1759 				$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))));
   1760 
   1761 				$this->load->model('tool/upload');
   1762 
   1763 				$product_data = array();
   1764 
   1765 				$products = $this->model_sale_order->getOrderProducts($order_id);
   1766 
   1767 				foreach ($products as $product) {
   1768 					$option_weight = '';
   1769 
   1770 					$product_info = $this->model_catalog_product->getProduct($product['product_id']);
   1771 
   1772 					if ($product_info) {
   1773 						$option_data = array();
   1774 
   1775 						$options = $this->model_sale_order->getOrderOptions($order_id, $product['order_product_id']);
   1776 
   1777 						foreach ($options as $option) {
   1778 							if ($option['type'] != 'file') {
   1779 								$value = $option['value'];
   1780 							} else {
   1781 								$upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
   1782 
   1783 								if ($upload_info) {
   1784 									$value = $upload_info['name'];
   1785 								} else {
   1786 									$value = '';
   1787 								}
   1788 							}
   1789 
   1790 							$option_data[] = array(
   1791 								'name'  => $option['name'],
   1792 								'value' => $value
   1793 							);
   1794 
   1795 							$product_option_value_info = $this->model_catalog_product->getProductOptionValue($product['product_id'], $option['product_option_value_id']);
   1796 
   1797 							if ($product_option_value_info) {
   1798 								if ($product_option_value_info['weight_prefix'] == '+') {
   1799 									$option_weight += $product_option_value_info['weight'];
   1800 								} elseif ($product_option_value_info['weight_prefix'] == '-') {
   1801 									$option_weight -= $product_option_value_info['weight'];
   1802 								}
   1803 							}
   1804 						}
   1805 
   1806 						$product_data[] = array(
   1807 							'name'     => $product_info['name'],
   1808 							'model'    => $product_info['model'],
   1809 							'option'   => $option_data,
   1810 							'quantity' => $product['quantity'],
   1811 							'location' => $product_info['location'],
   1812 							'sku'      => $product_info['sku'],
   1813 							'upc'      => $product_info['upc'],
   1814 							'ean'      => $product_info['ean'],
   1815 							'jan'      => $product_info['jan'],
   1816 							'isbn'     => $product_info['isbn'],
   1817 							'mpn'      => $product_info['mpn'],
   1818 							'weight'   => $this->weight->format(($product_info['weight'] + (float)$option_weight) * $product['quantity'], $product_info['weight_class_id'], $this->language->get('decimal_point'), $this->language->get('thousand_point'))
   1819 						);
   1820 					}
   1821 				}
   1822 
   1823 				$data['orders'][] = array(
   1824 					'order_id'	       => $order_id,
   1825 					'invoice_no'       => $invoice_no,
   1826 					'date_added'       => date($this->language->get('date_format_short'), strtotime($order_info['date_added'])),
   1827 					'store_name'       => $order_info['store_name'],
   1828 					'store_url'        => rtrim($order_info['store_url'], '/'),
   1829 					'store_address'    => nl2br($store_address),
   1830 					'store_email'      => $store_email,
   1831 					'store_telephone'  => $store_telephone,
   1832 					'email'            => $order_info['email'],
   1833 					'telephone'        => $order_info['telephone'],
   1834 					'shipping_address' => $shipping_address,
   1835 					'shipping_method'  => $order_info['shipping_method'],
   1836 					'product'          => $product_data,
   1837 					'comment'          => nl2br($order_info['comment'])
   1838 				);
   1839 			}
   1840 		}
   1841 
   1842 		$this->response->setOutput($this->load->view('sale/order_shipping', $data));
   1843 	}
   1844 }