function tep_upload_order($order_id, $delimiter = ',', $upload_dir = '') { if (empty($upload_dir)) { $upload_dir = UPLOAD_DIR . 'orders1/'; } // $order_info_query = tep_db_query("select orders_id from " . TABLE_ORDERS . " where orders_id = '" . (int)$order_id . "'"); // if (tep_db_num_rows($order_info_query) < 1) return false; $order_info = tep_db_fetch_array($order_info_query); $insert_id = $order_info['orders_id']; $insert_id = $order_id; $order = new order($insert_id); $shop_info_query = tep_db_query("select shops_url, shops_ssl, shops_prefix, shops_database from " . TABLE_SHOPS . " where shops_id = '" . (int) $order->info['shops_id'] . "'"); $shop_info = tep_db_fetch_array($shop_info_query); $domain_zone = $shop_info['shops_prefix']; $payment_modules = tep_get_payment_modules($order->info['shops_id']); $shipping_modules = tep_get_shipping_modules($order->info['shops_id']); $order_file = $upload_dir . $domain_zone . $insert_id . '.csv'; if (!file_exists($order_file)) { $fp = fopen($order_file, 'w'); $order_history_link = ($order->info['enabled_ssl'] == '1' && tep_not_null($shop_info['shops_ssl']) ? $shop_info['shops_ssl'] : $shop_info['shops_url']) . '/account_history_info.php?order_id=' . $insert_id; $order_total_sum = 0; $order_shipping_sum = 0; $order_discount_sum = 0; reset($order->totals); while (list(, $order_total) = each($order->totals)) { if ($order_total['class'] == 'ot_total') { $order_total_sum = $order_total['value']; } elseif ($order_total['class'] == 'ot_shipping') { $order_shipping_sum = $order_total['value']; $order_shipping_title = $order_total['title']; } elseif (($order_total['class'] == 'ot_discount' || $order_total['class'] == 'ot_custom') && $order_total['value'] < 0) { $order_discount_sum = $order_total['value']; } } $order_payment_id = $order->info['payment_method_class']; if (empty($order_payment_id)) { reset($payment_modules); $payment_found = false; while (list($k, $v) = each($payment_modules)) { if (strpos($v, $order->info['payment_method']) !== false) { $order_payment_id = $k; break; } } if (empty($order_payment_id)) { $order_payment_id = $order->info['payment_method']; } } $order_shipping_id = $order->delivery['delivery_method_class']; if (empty($order_shipping_id)) { reset($shipping_modules); $shipping_found = false; while (list($k, $v) = each($shipping_modules)) { if (strpos($v, $order_shipping_title) !== false) { $order_shipping_id = $k; break; } } if (empty($order_shipping_id)) { $order_shipping_id = $order_shipping_title; } } $self_delivery_id = $order->delivery['delivery_self_address_id']; if (tep_not_null($order->delivery['delivery_self_address']) && (int) $self_delivery_id <= 0) { $shop_info_query = tep_db_query("select shops_database from " . TABLE_SHOPS . " where shops_id = '" . (int) $order->info['shops_id'] . "'"); $shop_info = tep_db_fetch_array($shop_info_query); tep_db_select_db($shop_info['shops_database']); $self_delivery_query = tep_db_query("select self_delivery_id, self_delivery_cost, self_delivery_free, entry_suburb as suburb, entry_city as city, entry_street_address as street_address, entry_telephone as telephone, self_delivery_description from " . TABLE_SELF_DELIVERY . " where 1 order by city, street_address"); while ($self_delivery = tep_db_fetch_array($self_delivery_query)) { $self_delivery_address = tep_address_format($order->delivery['format_id'], $self_delivery, 1, '', ', '); if (strpos($order->delivery['delivery_self_address'], $self_delivery_address) !== false) { $self_delivery_id = $self_delivery['self_delivery_id']; break; } } tep_db_select_db(DB_DATABASE); if ($self_delivery_id == 0) { $self_delivery_id = $order->info['self_delivery']; } } $date_purchased = preg_replace('/(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})/', '$3.$2.$1 $4:$5:$6', $order->info['date_purchased']); $date_purchased = preg_replace('/\\s{2,}/', ' ', $date_purchased); $is_europe = ''; $europe_check_query = tep_db_query("select count(*) as total from setbook_eu." . TABLE_COUNTRIES . " where countries_name like '" . tep_db_input($order->delivery['country']) . "' or countries_ru_name like '" . tep_db_input($order->delivery['country']) . "' or countries_iso_code_2 like '" . tep_db_input($order->delivery['country']) . "' or countries_iso_code_3 like '" . tep_db_input($order->delivery['country']) . "'"); $europe_check = tep_db_fetch_array($europe_check_query); if ($europe_check['total'] > 0) { $is_europe = 'e'; } $order_delivery_country_code = ''; $country_code_info_query = tep_db_query("select countries_iso_code_2 from " . $shop_info['shops_database'] . "." . TABLE_COUNTRIES . " where countries_name = '" . tep_db_input($order->delivery['country']) . "' or countries_ru_name = '" . tep_db_input($order->delivery['country']) . "'"); $country_code_info = tep_db_fetch_array($country_code_info_query); $order_delivery_country_code = $country_code_info['countries_iso_code_2']; if ($order_delivery_country_code == '') { $order_delivery_country = strtolower($order->delivery['country']); $all_countries_file = UPLOAD_DIR . 'csv/all_countries.csv'; $fc = fopen($all_countries_file, 'r'); while ((list($country_name, $country_ru_name, $country_iso_code_2, $country_iso_code_3) = fgetcsv($fc, 40000, ";")) !== FALSE) { $country_name = strtolower($country_name); $country_ru_name = strtolower($country_ru_name); $country_iso_code_3 = strtolower($country_iso_code_3); if ($order_delivery_country == $country_name || $order_delivery_country == $country_ru_name || $order_delivery_country == $country_iso_code_3) { $order_delivery_country_code = $country_iso_code_2; break; } } fclose($fc); } if ($order_delivery_country_code == '') { $fc = fopen($all_countries_file, 'r'); while ((list($country_name, $country_ru_name, $country_iso_code_2, $country_iso_code_3) = fgetcsv($fc, 40000, ";")) !== FALSE) { $country_name = strtolower($country_name); $country_ru_name = strtolower($country_ru_name); if (strpos($country_name, $order_delivery_country) !== false || strpos($country_ru_name, $order_delivery_country) !== false || strpos($country_iso_code_3, $order_delivery_country) !== false) { $order_delivery_country_code = $country_iso_code_2; break; } } fclose($fc); } $common_data = array($insert_id, $date_purchased, $order->info['shops_id'], $order->customer['id'], $order->customer['email_address'], $order->delivery['name'], '', '', $order_payment_id, $order_shipping_id, str_replace(',', '.', $order_shipping_sum), $order->info['currency'], str_replace(',', '.', $order->info['currency_value']), tep_html_entity_decode($order->delivery['state']), tep_html_entity_decode($order->delivery['suburb']), tep_html_entity_decode($order->delivery['city']), $order->delivery['postcode'], tep_html_entity_decode($order->delivery['street_address']), $order->delivery['telephone'], tep_html_entity_decode($order->info['comments']), $self_delivery_id, $order_history_link, str_replace(',', '.', $order_total_sum), str_replace(',', '.', abs($order_discount_sum)), tep_html_entity_decode($order->customer['company']), $order->customer['company_inn'], $order->customer['company_kpp'], $order->customer['company_address_corporate'], $is_europe, $order->info['delivery_transfer_days'], $order->info['code'], $order->delivery['country'], tep_html_entity_decode($order_delivery_country_code), $order->customer['company_corporate']); fputcsvsafe($fp, $common_data, $delimiter); // tep_db_query("update " . TABLE_ORDERS . " set payment_method_class = '" . tep_db_input($order_payment_id) . "', delivery_method_class = '" . tep_db_input($order_shipping_id) . "', delivery_self_address_id = '" . (int)$self_delivery_id . "' where orders_id = '" . (int)$insert_id . "'"); reset($order->products); while (list(, $product) = each($order->products)) { $product_code = (int) str_replace('bbk', '', $product['code']); $common_data = array($product['type'], $product_code, $product['qty'], str_replace(',', '.', $product['final_price']), $product['id'], $product['seller_code'], $product['name'], tep_get_products_name($product['id']), $product['code'], $product['warranty']); fputcsvsafe($fp, $common_data, $delimiter); } fclose($fp); } }
} } tep_order_log($insert_id, 'Creating csv file for order'); $delimiter = ','; $date_purchased = preg_replace('/\\s+/', ' ', preg_replace('/(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})/', '$3.$2.$1 $4:$5:$6', $order->info['date_purchased'])); $order_file = UPLOAD_DIR . 'temp_orders/' . SHOP_PREFIX . $insert_id . '.csv'; $fp = fopen($order_file, 'w'); $common_data = array($insert_id, $date_purchased, SHOP_ID, $is_dummy_account == true ? '0' : $customer_id, $order->customer['email_address'], $order->delivery['name'], '', '', $payment, $order_shipping_title, str_replace(',', '.', $order_shipping_sum), $order->info['currency'], str_replace(',', '.', $order->info['currency_value']), tep_html_entity_decode($order->delivery['state']), tep_html_entity_decode($order->delivery['suburb']), tep_html_entity_decode($order->delivery['city']), $order->delivery['postcode'], tep_html_entity_decode($order->delivery['street_address']), $order->delivery['telephone'], tep_html_entity_decode($order->info['comments']), $self_delivery_id, tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, 'order_id=' . $insert_id, 'SSL', false), str_replace(',', '.', $order_total_sum), str_replace(',', '.', abs($order_discount_sum)), tep_not_null($order->customer['company_full']) ? tep_html_entity_decode($order->customer['company_full']) : tep_html_entity_decode($order->customer['company']), $order->customer['company_inn'], $order->customer['company_kpp'], $order->customer['company_address_corporate'], $is_europe, $order_delivery_transfer, $order->info['code'], tep_html_entity_decode($order->delivery['country']), tep_html_entity_decode($order_delivery_country_code), $order->customer['company_corporate']); fputcsvsafe($fp, $common_data, $delimiter); tep_order_log($insert_id, 'Order is saved to CSV file'); tep_db_query("update " . TABLE_ORDERS . " set " . (empty($order->info['code']) ? "orders_code = orders_id, " : "") . "payment_method_class = '" . tep_db_input($payment) . "', delivery_method = '" . tep_db_input($order_shipping_method) . "', delivery_method_class = '" . tep_db_input($order_shipping_title) . "', delivery_self_address = '" . tep_db_input($self_delivery_address) . "', delivery_self_address_id = '" . (int) $self_delivery_id . "' where orders_id = '" . (int) $insert_id . "'"); reset($order->products); while (list(, $product) = each($order->products)) { $product_code = (int) str_replace('bbk', '', $product['code']); $common_data = array($product['type'], $product_code, $product['qty'], str_replace(',', '.', $product['final_price']), $product['id'], '', $product['name'], tep_get_products_info($product['id']), $product['code'], $product['warranty']); fputcsvsafe($fp, $common_data, $delimiter); } fclose($fp); tep_order_log($insert_id, 'Order products are saved and file is closed'); copy($order_file, str_replace('temp_orders/', 'orders1/', $order_file)); unlink($order_file); tep_order_log($insert_id, 'Creating archive order'); tep_db_query("insert into " . TABLE_ARCHIVE_ORDERS . " select * from " . TABLE_ORDERS . " where orders_id = '" . (int) $insert_id . "'"); tep_db_query("insert into " . TABLE_ARCHIVE_ORDERS_TOTAL . " select * from " . TABLE_ORDERS_TOTAL . " where orders_id = '" . (int) $insert_id . "'"); tep_db_query("insert into " . TABLE_ARCHIVE_ORDERS_STATUS_HISTORY . " select * from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id = '" . (int) $insert_id . "'"); tep_db_query("insert into " . TABLE_ARCHIVE_ORDERS_PRODUCTS . " select * from " . TABLE_ORDERS_PRODUCTS . " where orders_id = '" . (int) $insert_id . "'"); tep_db_query("insert into " . TABLE_ARCHIVE_ORDERS_PRODUCTS_DOWNLOAD . " select * from " . TABLE_ORDERS_PRODUCTS_DOWNLOAD . " where orders_id = '" . (int) $insert_id . "'"); tep_order_log($insert_id, 'Archive order is created'); $cart->reset(true); // unregister session variables used during checkout tep_session_unregister('sendto');
$temp_array[] = html_entity_decode($products_cover_info['products_covers_name'], ENT_QUOTES); break; case 'products_formats_name': $products_format_info_query = tep_db_query("select products_formats_name from " . TABLE_PRODUCTS_FORMATS . " where products_formats_id = '" . $product_info['products_formats_id'] . "' and language_id = '" . (int) $languages_id . "'"); $products_format_info = tep_db_fetch_array($products_format_info_query); $temp_array[] = html_entity_decode($products_format_info['products_formats_name'], ENT_QUOTES); break; case 'products_url': $temp_array[] = tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $product_info['products_id'], 'NONSSL', false); break; case 'products_quantity': $temp_array[] = ''; break; } } fputcsvsafe($out, $temp_array, ";"); } fclose($out); tep_exit(); } } else { $messageStack->add('header', strip_tags(ENTRY_REQUEST_FORM_AUTHORIZATION_NEEDED)); } break; case 'corporate_order': if (tep_session_is_registered('customer_id')) { $cells = array(); $file_cells = array(); $error = false; if (is_uploaded_file($_FILES['corporate_file']['tmp_name'])) { $ext = strtolower(substr($_FILES['corporate_file']['name'], strrpos($_FILES['corporate_file']['name'], '.') + 1));
function before_process() { global $customer_id, $order, $order_totals, $sendto, $billto, $languages_id, $payment, $currencies, $cart, $cart_PayPal_Standard_ID; $insert_id = substr($cart_PayPal_Standard_ID, strpos($cart_PayPal_Standard_ID, '-') + 1); $check_query = tep_db_query("select orders_status from " . TABLE_ORDERS . " where orders_id = '" . (int) $insert_id . "'"); if (tep_db_num_rows($check_query)) { $check = tep_db_fetch_array($check_query); if ($check['orders_status'] == MODULE_PAYMENT_PAYPAL_STANDARD_PREPARE_ORDER_STATUS_ID) { $sql_data_array = array('orders_id' => $insert_id, 'orders_status_id' => MODULE_PAYMENT_PAYPAL_STANDARD_PREPARE_ORDER_STATUS_ID, 'date_added' => 'now()', 'customer_notified' => '0', 'comments' => ''); tep_db_perform(TABLE_ORDERS_STATUS_HISTORY, $sql_data_array); } } tep_db_query("update " . TABLE_ORDERS . " set orders_status = '" . (MODULE_PAYMENT_PAYPAL_STANDARD_ORDER_STATUS_ID > 0 ? (int) MODULE_PAYMENT_PAYPAL_STANDARD_ORDER_STATUS_ID : (int) DEFAULT_ORDERS_STATUS_ID) . "', last_modified = now() where orders_id = '" . (int) $insert_id . "'"); $sql_data_array = array('orders_id' => $insert_id, 'orders_status_id' => MODULE_PAYMENT_PAYPAL_STANDARD_ORDER_STATUS_ID > 0 ? (int) MODULE_PAYMENT_PAYPAL_STANDARD_ORDER_STATUS_ID : (int) DEFAULT_ORDERS_STATUS_ID, 'date_added' => 'now()', 'customer_notified' => SEND_EMAILS == 'true' ? '1' : '0', 'comments' => $order->info['comments']); tep_db_perform(TABLE_ORDERS_STATUS_HISTORY, $sql_data_array); // initialized for the email confirmation $products_ordered = ''; $subtotal = 0; $total_tax = 0; for ($i = 0, $n = sizeof($order->products); $i < $n; $i++) { // Update products_ordered (for bestsellers list) tep_db_query("update " . TABLE_PRODUCTS . " set products_ordered = products_ordered + " . sprintf('%d', $order->products[$i]['qty']) . " where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'"); //------insert customer choosen option eof ---- $total_weight += $order->products[$i]['qty'] * $order->products[$i]['weight']; $total_tax += tep_calculate_tax($total_products_price, $products_tax) * $order->products[$i]['qty']; $total_cost += $total_products_price; $products_ordered .= $order->products[$i]['qty'] . ' x ' . $order->products[$i]['name'] . ' = ' . $currencies->display_price($order->products[$i]['final_price'], $order->products[$i]['tax'], $order->products[$i]['qty']) . "\n"; } // lets start with the email confirmation $email_order = STORE_NAME . "\n" . EMAIL_SEPARATOR . "\n" . EMAIL_TEXT_ORDER_NUMBER . ' ' . $insert_id . "\n" . EMAIL_TEXT_INVOICE_URL . ' ' . tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, 'order_id=' . $insert_id, 'SSL', false) . "\n" . EMAIL_TEXT_DATE_ORDERED . ' ' . tep_date_long(DATE_FORMAT_LONG) . "\n\n"; if ($order->info['comments']) { $email_order .= tep_db_output($order->info['comments']) . "\n\n"; } $email_order .= EMAIL_TEXT_PRODUCTS . "\n" . EMAIL_SEPARATOR . "\n" . $products_ordered . EMAIL_SEPARATOR . "\n"; for ($i = 0, $n = sizeof($order_totals); $i < $n; $i++) { $email_order .= strip_tags($order_totals[$i]['title']) . ' ' . strip_tags($order_totals[$i]['text']) . "\n"; } if ($order->content_type != 'virtual') { $email_order .= "\n" . EMAIL_TEXT_DELIVERY_ADDRESS . "\n" . EMAIL_SEPARATOR . "\n" . tep_address_label($customer_id, $sendto, false) . "\n"; } if ($billto != false) { // $email_order .= "\n" . EMAIL_TEXT_BILLING_ADDRESS . "\n" . // EMAIL_SEPARATOR . "\n" . // tep_address_label($customer_id, $billto, false) . "\n\n"; } if (is_object(${$payment})) { $email_order .= EMAIL_TEXT_PAYMENT_METHOD . "\n" . EMAIL_SEPARATOR . "\n"; $payment_class = ${$payment}; $email_order .= $payment_class->title . "\n\n"; if ($payment_class->email_footer) { $email_order .= strip_tags($payment_class->email_footer) . "\n\n"; } } $email_subject = STORE_NAME . ' - ' . sprintf(EMAIL_TEXT_SUBJECT, $insert_id); tep_mail($order->customer['firstname'] . ' ' . $order->customer['lastname'], $order->customer['email_address'], $email_subject, $email_order, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS); // send emails to other people if (SEND_EXTRA_ORDER_EMAILS_TO != '') { tep_mail('', SEND_EXTRA_ORDER_EMAILS_TO, $email_subject, $email_order, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS); } // load the after_process function from the payment modules $this->after_process(); $cart->reset(true); $order = new order($insert_id); $order_total_sum = 0; $order_shipping_sum = 0; reset($order->totals); while (list(, $order_total) = each($order->totals)) { if ($order_total['class'] == 'ot_total') { $order_total_sum = $order_total['value']; } elseif ($order_total['class'] == 'ot_shipping') { $order_shipping_sum = $order_total['value']; } } $order_shipping_title = strtolower($order->info['shipping_method']); if (mb_strpos($order_shipping_title, 'самовывоз', 0, 'CP1251') !== false) { $order_shipping_id = 1; } elseif (mb_strpos($order_shipping_title, 'подмосковье', 0, 'CP1251') !== false) { if ($order_shipping_sum == 0) { $order_shipping_id = 5; } else { $order_shipping_id = 4; } } elseif (mb_strpos($order_shipping_title, 'курьером', 0, 'CP1251') !== false) { if ($order_shipping_sum == 0) { $order_shipping_id = 3; } else { $order_shipping_id = 2; } } elseif (mb_strpos($order_shipping_title, 'почт', 0, 'CP1251') !== false) { if (DOMAIN_ZONE == 'ru') { $order_shipping_id = 6; } elseif (DOMAIN_ZONE == 'ua') { $order_shipping_id = 8; } elseif (DOMAIN_ZONE == 'by') { $order_shipping_id = 9; } } elseif (strpos($order_shipping_title, 'postal') !== false) { $order_shipping_id = 7; } elseif (strpos($order_shipping_title, 'deutsche') !== false) { $order_shipping_id = 10; } else { $order_shipping_id = 0; } $order_payment_id = 0; $order_payment_title = strtolower($order->info['payment_method']); if (mb_strpos($order_payment_title, 'налич', 0, 'CP1251') !== false) { $order_payment_id = 1; } elseif (strpos($order_payment_title, 'order') !== false) { $order_payment_id = 6; } elseif (strpos($order_payment_title, 'check') !== false) { $order_payment_id = 7; } elseif (strpos($order_payment_title, 'pal') !== false) { $order_payment_id = 8; } elseif (mb_strpos($order_payment_title, 'налож', 0, 'CP1251') !== false) { $order_payment_id = 2; } elseif (mb_strpos($order_payment_title, 'банк', 0, 'CP1251') !== false) { $order_payment_id = 3; } elseif (mb_strpos($order_payment_title, 'безнал', 0, 'CP1251') !== false) { $order_payment_id = 4; } elseif (mb_strpos($order_payment_title, 'почтовым', 0, 'CP1251') !== false) { $order_payment_id = 5; } $date_purchased = preg_replace('/(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})/', '$3-$2-$1 $4:$5:$6', $order->info['date_purchased']); $order_file = UPLOAD_DIR . 'orders/' . SHOP_PREFIX . 'im' . $insert_id . '.csv'; $fp = fopen($order_file, 'w'); reset($order->products); $common_data = array('ORDER_ID', 'ORDER_DATE_INSERT', 'USER_ID', 'PAY_SYSTEM_ID', 'DELIVERY_ID', 'PRICE_DELIVERY', 'ORDER_PRICE', 'REGION', 'RAYON', 'CITY', 'INDEX', 'ADDRESS', 'PHONE', 'COMMENTS', 'PRODUCT_ID', 'PRODUCT_QUANTITY', 'PRODUCT_PRICE'); fputcsvsafe($fp, $common_data, ';'); while (list(, $product) = each($order->products)) { $common_data = array(); $common_data[] = $server_prefix . 'im' . $insert_id; $common_data[] = $date_purchased; $common_data[] = $order->customer['id']; $common_data[] = $order_payment_id; // тип оплаты $common_data[] = $order_shipping_id; // тип доставки $common_data[] = $order_shipping_sum; // стоимость доставки $common_data[] = $order_total_sum; // сумма заказа $common_data[] = $order->delivery['state']; $common_data[] = $order->delivery['suburb']; $common_data[] = $order->delivery['city']; $common_data[] = $order->delivery['postcode']; $common_data[] = $order->delivery['street_address']; $common_data[] = $order->customer['telephone']; $common_data[] = $order->info['comments']; $common_data[] = $product['code']; $common_data[] = $product['qty']; $common_data[] = $product['final_price']; fputcsvsafe($fp, $common_data, ';'); } fclose($fp); // unregister session variables used during checkout tep_session_unregister('sendto'); tep_session_unregister('billto'); tep_session_unregister('shipping'); tep_session_unregister('payment'); tep_session_unregister('comments'); tep_session_unregister('cart_PayPal_Standard_ID'); tep_redirect(tep_href_link(FILENAME_CHECKOUT_SUCCESS, '', 'SSL')); }