$this->load->model('checkout/order');
$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
if ($order_info) {
$this->language->load('payment/klarna_invoice');
$this->data['text_additional'] = $this->language->get('text_additional');
$this->data['text_payment_option'] = $this->language->get('text_payment_option');
$this->data['text_wait'] = $this->language->get('text_wait');
$this->data['text_day'] = $this->language->get('text_day');
$this->data['text_month'] = $this->language->get('text_month');
$this->data['text_year'] = $this->language->get('text_year');
$this->data['text_male'] = $this->language->get('text_male');
$this->data['text_female'] = $this->language->get('text_female');
$this->data['entry_pno'] = $this->language->get('entry_pno');
$this->data['entry_dob'] = $this->language->get('entry_dob');
$this->data['entry_gender'] = $this->language->get('entry_gender');
$this->data['entry_street'] = $this->language->get('entry_street');
$this->data['entry_house_no'] = $this->language->get('entry_house_no');
$this->data['entry_house_ext'] = $this->language->get('entry_house_ext');
$this->data['entry_phone_no'] = $this->language->get('entry_phone_no');
$this->data['entry_company'] = $this->language->get('entry_company');
$this->data['button_confirm'] = $this->language->get('button_confirm');
$this->data['days'] = array();
for ($i = 1; $i <= 31; $i++) {
$this->data['days'][] = array(
'text' => sprintf('%02d', $i),
'value' => $i
);
}
$this->data['months'] = array();
for ($i = 1; $i <= 12; $i++) {
$this->data['months'][] = array(
'text' => sprintf('%02d', $i),
'value' => $i
);
}
$this->data['years'] = array();
for ($i = date('Y'); $i >= 1900; $i--) {
$this->data['years'][] = array(
'text' => $i,
'value' => $i
);
}
// Store Taxes to send to Klarna
$total_data = array();
$total = 0;
$this->load->model('setting/extension');
$sort_order = array();
$results = $this->model_setting_extension->getExtensions('total');
foreach ($results as $key => $value) {
$sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
}
array_multisort($sort_order, SORT_ASC, $results);
$klarna_tax = array();
foreach ($results as $result) {
if ($this->config->get($result['code'] . '_status')) {
$this->load->model('total/' . $result['code']);
$taxes = array();
$this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
$amount = 0;
foreach ($taxes as $tax_id => $value) {
$amount += $value;
}
$klarna_tax[$result['code']] = $amount;
}
}
foreach ($total_data as $key => $value) {
$sort_order[$key] = $value['sort_order'];
if (isset($klarna_tax[$value['code']])) {
if ($klarna_tax[$value['code']]) {
$total_data[$key]['tax_rate'] = abs($klarna_tax[$value['code']] / $value['value'] * 100);
} else {
$total_data[$key]['tax_rate'] = 0;
}
} else {
$total_data[$key]['tax_rate'] = '0';
}
}
$this->session->data['klarna'][$this->session->data['order_id']] = $total_data;
// Order must have identical shipping and billing address or have no shipping address at all
if ($this->cart->hasShipping() && !($order_info['payment_firstname'] == $order_info['shipping_firstname'] && $order_info['payment_lastname'] == $order_info['shipping_lastname'] && $order_info['payment_address_1'] == $order_info['shipping_address_1'] && $order_info['payment_address_2'] == $order_info['shipping_address_2'] && $order_info['payment_postcode'] == $order_info['shipping_postcode'] && $order_info['payment_city'] == $order_info['shipping_city'] && $order_info['payment_zone_id'] == $order_info['shipping_zone_id'] && $order_info['payment_zone_code'] == $order_info['shipping_zone_code'] && $order_info['payment_country_id'] == $order_info['shipping_country_id'] && $order_info['payment_country'] == $order_info['shipping_country'] && $order_info['payment_iso_code_3'] == $order_info['shipping_iso_code_3'])) {
$this->data['error_warning'] = $this->language->get('error_address_match');
} else {
$this->data['error_warning'] = '';
}
// The title stored in the DB gets truncated which causes order_info.tpl to not be displayed properly
$this->db->query("UPDATE `" . DB_PREFIX . "order` SET `payment_method` = '" . $this->db->escape($this->language->get('text_title')) . "' WHERE `order_id` = " . (int)$this->session->data['order_id']);
$klarna_invoice = $this->config->get('klarna_invoice');
$this->data['merchant'] = $klarna_invoice[$order_info['payment_iso_code_3']]['merchant'];
$this->data['phone_number'] = $order_info['telephone'];
if ($order_info['payment_iso_code_3'] == 'DEU' || $order_info['payment_iso_code_3'] == 'NLD') {
$address = $this->splitAddress($order_info['payment_address_1']);
$this->data['street'] = $address[0];
$this->data['street_number'] = $address[1];
$this->data['street_extension'] = $address[2];
if ($order_info['payment_iso_code_3'] == 'DEU') {
$this->data['street_number'] = trim($address[1] . ' ' . $address[2]);
}
} else {
$this->data['street'] = '';
$this->data['street_number'] = '';
$this->data['street_extension'] = '';
}
$this->data['company'] = $order_info['payment_company'];
$this->data['iso_code_2'] = $order_info['payment_iso_code_2'];
$this->data['iso_code_3'] = $order_info['payment_iso_code_3'];
// Get the invoice fee
$query = $this->db->query("SELECT `value` FROM `" . DB_PREFIX . "order_total` WHERE `order_id` = " . (int)$order_info['order_id'] . " AND `code` = 'klarna_fee'");
if ($query->num_rows && !$query->row['value']) {
$this->data['klarna_fee'] = $query->row['value'];
} else {
$this->data['klarna_fee'] = '';
}
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/klarna_invoice.tpl')) {
$this->template = $this->config->get('config_template') . '/template/payment/klarna_invoice.tpl';
} else {
$this->template = 'default/template/payment/klarna_invoice.tpl';
}
$this->render();
}