/**
   * Loads invoice information
   * Overwrites globals
   */
  function load_invoice($args = '') {
    global $wpdb, $wpi_settings, $blog_id;

    extract(wp_parse_args($args, array(
      'id' => '',
      'return' => false
    )), EXTR_SKIP);

    if(strlen($id) == 8) {
      $id = wpi_invoice_id_to_post_id($id);
    }

    $new_invoice = is_numeric($id) ? false : true;

    $invoice_data = $wpdb->get_row("SELECT * FROM {$wpdb->posts} WHERE ID = '$id'", ARRAY_A);

    if($new_invoice || count($invoice_data) < 1) {
      $this->error = true;
      $this->new_invoice = true;
      WPI_Functions::console_log('WPI_Invoice::load_invoice() '.__('function executed, no invoice ID found, assuming new invoice.', WPI));
      return;
    }

    WPI_Functions::console_log('WPI_Invoice::load_invoice() '.__('function executed, invoice_id: ', WPI) . $id);

    $object_meta = get_post_custom($id);


    if(is_array($object_meta)) {
      foreach($object_meta as $meta_key => $meta_value) {
        if(is_array($meta_value)) {
          $meta_value = $meta_value[key($meta_value)];
        }

        if ( is_serialized($meta_value) ) {
          $tmp_meta_value = unserialize($meta_value);
        } else {
          $tmp_meta_value = $meta_value;
        }
        $invoice_data[$meta_key] = (empty($tmp_meta_value) || !is_array($tmp_meta_value)) ? $meta_value : $tmp_meta_value;
      }
    }

    WPI_Functions::merge_billings( $wpi_settings['billing'], &$invoice_data['billing'] );

    //** Add support for MS and for old invoice histories which will have a blog_id of 0 after upgrade */
    if($blog_id == 1) {
      $ms_blog_query = " AND ( blog_id = {$blog_id} OR blog_id = 0) ";
    } else {
      $ms_blog_query = " AND blog_id = {$blog_id} ";
    }

    $object_log = $wpdb->get_results("SELECT * FROM {$wpdb->base_prefix}wpi_object_log WHERE object_id = '{$id}' $ms_blog_query", ARRAY_A);

    if(!empty($object_log)) {
      $invoice_data['log'] = $object_log;
    }

    $invoice_data = apply_filters('wpi_load_invoice', $invoice_data);

    if(!empty($invoice_data['user_email'])) {
      $this->load_user("email={$invoice_data['user_email']}");
    }

    if(empty($this->data['user_data'])) {
      WPI_Functions::console_log(__('Warning: no user information loaded for this invoice.', WPI));
    }

    if(!is_array($this->data)) {
     $this->data = array();
    }

    $this->data = array_merge($invoice_data, $this->data);

    if( $return ) {
      return $this->data;
    }
  }