<?php
/**
  @licence GPL 2005-2011  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  22/02/11, 21:12
  @author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
  @encode UTF-8
*/
?>
<!-- body //-->
<h4><?php echo HEADING_TITLE; ?></h4>
<!-- body_text //-->
  <table class="dataTableBase">
    <thead>
      <tr>
        <th><?php echo TABLE_HEADING_NUMBER.tep_link_sort_by(FILENAME_COUNTRIES,'c.customers_id'); ?></th>
        <th style="width:40%"><?php echo TABLE_HEADING_CUSTOMERS.tep_link_sort_by(FILENAME_COUNTRIES,'banners_group'); ?></th>
        <th class="tcenter"><?php echo TABLE_HEADING_TOTAL_PURCHASED.tep_link_sort_by(FILENAME_COUNTRIES,'banners_group'); ?></th>
	<?php if(_cst_bool('MODULE_ACA_COUPON_SORT_ORDER')): ?>
	  <th class="tright"><?php echo TABLE_HEADING_USED_COUPONS.tep_link_sort_by(FILENAME_COUNTRIES,'banners_group'); ?></th>
	<?php endif; ?>
      </tr>
    </thead>
    <tbody>
    <?php
      if (isset($_GET['page']) && ($_GET['page'] > 1)) $rows = $_GET['page'] * MAX_DISPLAY_SEARCH_RESULTS - MAX_DISPLAY_SEARCH_RESULTS;
      $customers_query_raw = "select c.customers_id, c.customers_firstname, c.customers_lastname,c.customers_email_address, sum(op.products_quantity * op.final_price) as ordersum from " . TABLE_CUSTOMERS . " c, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o where c.customers_id = o.customers_id and o.orders_id = op.orders_id group by c.customers_firstname, c.customers_lastname order by ordersum DESC";
      $db_query_split = new splitPageResults($_GET['page'], MAX_DISPLAY_ROW_BY_PAGE, $customers_query_raw, $db_query_numrows);

    // fix counted customers
      $db_query_numrows = tep_db_query("select customers_id from " . TABLE_ORDERS . " group by customers_id");
      $db_query_numrows = tep_db_num_rows($db_query_numrows);

      $rows = 0;
      $customers_query = tep_db_query($customers_query_raw);
      while ($customers = tep_db_fetch_array($customers_query)) {
        $rows++;

        if (strlen($rows) < 2) {
          $rows = '0' . $rows;
        }
    ?>
    <tr class="view parent dataTableRow" >
      <td><?php echo $rows; ?></td>
      <td><?php echo tep_customers_row_action($customers['customers_id'], array('origin'=>FILENAME_STATS_CUSTOMERS));  ?></td>
      <td class="tcenter"><?php echo $currencies->format($customers['ordersum']); ?></td>
      <?php if(_cst_bool('MODULE_ACA_COUPON_SORT_ORDER')): ?>
	<td class="tcenter"><a href="<?php echo tep_href_link(FILENAME_STATS,'module=stats_discount_coupons&amp;custID='.$customers['customers_id'].'&amp;origin='.FILENAME_STATS_CUSTOMERS); ?>"><?php echo IMAGE_USED_COUPONS; ?></a></td>
      <?php endif; ?>
    </tr>
<?php
}
?>
        </tbody>
        </table>
<!-- body_text_eof //-->
<!-- body_eof //-->