ensureInitialized(); parent::__construct(); } private $initialized = false; private function ensureInitialized() { if ($this->initialized) { return; } $this->table = 'packetery_order'; $this->packetery = new Packetery(); $this->initialized = true; } // not sure why I had to do this to make it work public function l($str, $class = 'AdminOrderPacketery', $addslashes = false, $htmlentities = true) { $this->ensureInitialized(); return $this->packetery->l($str, $class, $addslashes, $htmlentities); } private function csvEscape($s) { return str_replace('"', '""', $s); } public function exportCsv() { header("Content-Type: text/csv"); header("Content-Disposition: attachment; filename=\"export-" . date("Ymd-His") . ".csv\""); $db = Db::getInstance(); $is_cods = (is_array(Tools::getValue('packetery_order_is_cod')) ? Tools::getValue('packetery_order_is_cod') : array()); foreach ($is_cods as $id => $is_cod) { $db->execute( 'update `'._DB_PREFIX_.'packetery_order` set is_cod=' . ((int) $is_cod) . ' where id_order=' . ((int) $id) ); } $ids = array_map( 'floor', is_array(Tools::getValue('packetery_order_id')) && count(Tools::getValue('packetery_order_id')) > 0 ? Tools::getValue('packetery_order_id') : array(0) ); $data = $db->executeS( 'select o.id_order, a.firstname, a.lastname, a.phone, a.phone_mobile, c.email, o.total_paid total, po.id_branch, po.is_cod, o.id_currency, po.currency_branch, a.company, a.address1, a.address2, a.postcode, a.city from `'._DB_PREFIX_.'orders` o join `'._DB_PREFIX_.'packetery_order` po on(po.id_order=o.id_order) join `'._DB_PREFIX_.'customer` c on(c.id_customer=o.id_customer) join `'._DB_PREFIX_.'address` a on(a.id_address=o.id_address_delivery) where o.id_order in (' . implode(',', $ids) . ')' ); $cnb_rates = null; foreach ($data as $order) { $phone = ""; foreach (array( 'phone', 'phone_mobile' ) as $field) { if (preg_match( '/^(((?:\+|00)?420)?[67][0-9]{8}|((?:\+|00)?421|0)?9[0-9]{8})$/', preg_replace('/\s+/', '', $order[$field]) )) { $phone = trim($order[$field]); } } $currency = new Currency($order['id_currency']); $total = $order['total']; if ($currency->iso_code != $order['currency_branch']) { $target_currency = Currency::getIdByIsoCode($order['currency_branch']); if ($target_currency) { $target_currency = new Currency($target_currency); $total = round($total * $target_currency->conversion_rate / $currency->conversion_rate, 2); } else { if (!$cnb_rates) { if ($data = @Tools::file_get_contents( 'http://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt' )) { $cnb_rates = array(); foreach (array_slice(explode("\n", $data), 2) as $rate) { $rate = explode('|', $rate); $cnb_rates[$rate[3]] = (float) preg_replace( '/[^0-9.]*/', '', str_replace(',', '.', $rate[4]) ); } $cnb_rates['CZK'] = 1; } } if ($cnb_rates) { $total = round( $total * $cnb_rates[$currency->iso_code] / $cnb_rates[$order['currency_branch']], 2 ); } } } $cod_total = $total; if ($order['currency_branch'] == 'CZK') { $cod_total = round($total); } echo ';"'.$this->csvEscape($order['id_order']).'";"'. $this->csvEscape($order['firstname']).'";"'.$this->csvEscape($order['lastname']). '";"'.$this->csvEscape($order['company']).'";"'.$this->csvEscape($order['email']). '";"'.$this->csvEscape($phone).'";"'.($order['is_cod'] == 1 ? $this->csvEscape($cod_total) : "0"). '";"'.$this->csvEscape($total).'";"'.$this->csvEscape($order['id_branch']). '";"'.Configuration::get('PACKETERY_ESHOP_DOMAIN').'";"'.$this->csvEscape( $order['address1']. ($order['address2'] ? ", " . $order['address2'] : "") ).'";;"'.$this->csvEscape($order['city']). '";"'.$this->csvEscape($order['postcode']).'"'."\r\n"; } $db->execute( 'update `'._DB_PREFIX_.'packetery_order` set exported=1 where id_order in(' . implode(',', $ids) . ')' ); exit(); } public function display() { echo '
" . $error . "
"; } if ($have_error) { echo "" . $this->l( 'Before you will be able to use this page, please go to Packetery module configuration.' ) . "
"; return; } else { echo "