<?php
/**
  @licence GPL 2005-2010  The osCSS developers - osCSS Open Source E-commerce
  @portion code Copyright (c) 2002 osCommerce
  @package osCSS-2 <www http://www.oscss.org>
  @version 2.1.0
  @date  19/12/10, 19:49
  @author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
  @encode UTF-8

  TODO: a centraliser avec la class ckeckout_process public , qui contient une methode equivalent
*/

/**
  Traitement de la recup commande, et bascule dans les commande de la held_commande en cours
*/
function tep_confirm_held_order($oID){

  $order_query = tep_db_query("select customers_id, customers_name, customers_company, customers_street_address, customers_suburb, customers_city, customers_postcode, customers_state, customers_country, customers_telephone, customers_email_address, customers_address_format_id, delivery_name, delivery_company, delivery_street_address, delivery_suburb, delivery_city, delivery_postcode, delivery_state, delivery_country, delivery_address_format_id, billing_name, billing_company, billing_street_address, billing_suburb, billing_city, billing_postcode, billing_state, billing_country, billing_address_format_id, payment_method, cc_type, cc_owner, cc_number, cc_expires, currency, currency_value, date_purchased, orders_status, last_modified, ip_address from " . TABLE_HOLDING_ORDERS . " where orders_id = '" . $oID . "'");
  $order = tep_db_fetch_array($order_query);

      //$this->info = array
          $sql_data_array = array('currency' => $order['currency'],
                                  'customers_id' => $order['customers_id'],
                                  'currency_value' => $order['currency_value'],
                                  'payment_method' => $order['payment_method'],
                                  'cc_type' => $order['cc_type'],
                                  'cc_owner' => $order['cc_owner'],
                                  'cc_number' => $order['cc_number'],
                                  'cc_expires' => $order['cc_expires'],
                                  'date_purchased' => $order['date_purchased'],
                                  'orders_status' => $order['orders_status'],
				  'ip_address' => $order['ip_address'],
				    'last_modified' => $order['last_modified'],
				    'customers_name' => $order['customers_name'],
				    'customers_company' => $order['customers_company'],
				    'customers_street_address' => $order['customers_street_address'],
				    'customers_suburb' => $order['customers_suburb'],
				    'customers_city' => $order['customers_city'],
				    'customers_postcode' => $order['customers_postcode'],
				    'customers_state' => $order['customers_state'],
				    'customers_country' => $order['customers_country'],
				    'customers_address_format_id' => $order['customers_address_format_id'],
				    'customers_telephone' => $order['customers_telephone'],
				    'customers_email_address' => $order['customers_email_address'],
				    'delivery_name' => $order['delivery_name'],
				    'delivery_company' => $order['delivery_company'],
				    'delivery_street_address' => $order['delivery_street_address'],
				    'delivery_suburb' => $order['delivery_suburb'],
				    'delivery_city' => $order['delivery_city'],
				    'delivery_postcode' => $order['delivery_postcode'],
				    'delivery_state' => $order['delivery_state'],
				    'delivery_country' => $order['delivery_country'],
				    'delivery_address_format_id' => $order['delivery_address_format_id'],
				    'billing_name' => $order['billing_name'],
				    'billing_company' => $order['billing_company'],
				    'billing_street_address' => $order['billing_street_address'],
				    'billing_suburb' => $order['billing_suburb'],
				    'billing_city' => $order['billing_city'],
				    'billing_postcode' => $order['billing_postcode'],
				    'billing_state' => $order['billing_state'],
				    'billing_country' => $order['billing_country'],
				    'billing_address_format_id' => $order['billing_address_format_id'],
				    'holding_origin_id'=>$oID);

      tep_db_perform(TABLE_ORDERS, $sql_data_array);

      $cs_s=tep_db_query("select orders_id from " . TABLE_ORDERS . "  where customers_id='".$order['customers_id']."' and ip_address='".$order['ip_address']."' order by orders_id DESC");
      $cs=tep_db_fetch_array($cs_s);
      $insert_id=$cs['orders_id'];

//       $insert_id = tep_db_insert_id();

      $orders_products_query = tep_db_query("select orders_products_id, products_id,products_name, products_model, products_price, products_tax, products_quantity, final_price from " . TABLE_HOLDING_ORDERS_PRODUCTS . " where orders_id = '" . $oID . "'");
      while ($orders_products = tep_db_fetch_array($orders_products_query)) {

        $sql_data_array2 = array('orders_id' => $insert_id,
                                 'products_id' => $orders_products['products_id'],
                                 'products_quantity' => $orders_products['products_quantity'],
                                 'products_name' => $orders_products['products_name'],
                                 'products_model' => $orders_products['products_model'],
                                 'products_tax' => $orders_products['products_tax'],
                                 'products_price' => $orders_products['products_price'],
                                 'final_price' => $orders_products['final_price']);

        tep_db_perform(TABLE_ORDERS_PRODUCTS, $sql_data_array2);

        $cs_s=tep_db_query("select orders_products_id from " . TABLE_ORDERS_PRODUCTS . "  where orders_id='".$insert_id."' and products_id='".$orders_products['products_id']."' ");
        $cs=tep_db_fetch_array($cs_s);
        $order_products_id=$cs['orders_products_id'];
//         $order_products_id = tep_db_insert_id();


        $attributes_query = tep_db_query("select products_options, products_options_values, options_values_price, price_prefix from " . TABLE_HOLDING_ORDERS_PRODUCTS_ATTRIBUTES . " where orders_id = '" . $oID . "' and orders_products_id = '" . $orders_products['orders_products_id'] . "'");
        if (tep_db_num_rows($attributes_query)) {
          while ($attributes = tep_db_fetch_array($attributes_query)) {
            $sql_data_array1 = array('orders_id' => $insert_id,
                                     'orders_products_id' => $order_products_id,
                                     'products_options' => $attributes['products_options'],
                                     'products_options_values' => $attributes['products_options_values'],
                                     'price_prefix' => $attributes['price_prefix'],
                                     'options_values_price' => $attributes['options_values_price']);

            tep_db_perform(TABLE_ORDERS_PRODUCTS_ATTRIBUTES, $sql_data_array1);
          }
        }
      }
      $totals_query = tep_db_query("select title, text, value, class, sort_order from " . TABLE_HOLDING_ORDERS_TOTAL . " where orders_id = '" . $oID . "' order by sort_order");
      while ($totals = tep_db_fetch_array($totals_query)) {
        $sql_data_array3 = array('orders_id' => $insert_id,
                                 'title' => $totals['title'],
                                 'text' => $totals['text'],
                                 'value' => $totals['value'],
                                 'class' => $totals['class'],
                                 'sort_order' => $totals['sort_order']);
        tep_db_perform(TABLE_ORDERS_TOTAL, $sql_data_array3);
      }

      $status_query = tep_db_query("select orders_status_id, date_added, customer_notified, comments from " . TABLE_HOLDING_ORDERS_STATUS_HISTORY . " where orders_id = '" . $oID . "'");
      while ($status = tep_db_fetch_array($status_query)) {
        $sql_data_array4 = array('orders_id' => $insert_id,
                                 'orders_status_id' => $status['orders_status_id'],
                                 'date_added' => $status['date_added'],
                                 'customer_notified' => $status['customer_notified'],
                                 'comments' => $status['comments']);
        tep_db_perform(TABLE_ORDERS_STATUS_HISTORY, $sql_data_array4);
      }

      tep_db_query("delete from " . TABLE_HOLDING_ORDERS . " where orders_id = '" . $oID . "'");
      tep_db_query("delete from " . TABLE_HOLDING_ORDERS_PRODUCTS . " where orders_id = '" . $oID . "'");
      tep_db_query("delete from " . TABLE_HOLDING_ORDERS_PRODUCTS_ATTRIBUTES . " where orders_id = '" . $oID . "'");
      tep_db_query("delete from " . TABLE_HOLDING_ORDERS_STATUS_HISTORY . " where orders_id = '" . $oID . "'");
      tep_db_query("delete from " . TABLE_HOLDING_ORDERS_TOTAL . " where orders_id = '" . $oID . "'");
}

?>