<?php if (!defined('HTTP_SERVER')) die('You can not access this file directly!');
/**
  \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  29/11/10, 11:33
  \author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
  \encode UTF-8
  \class discount_coupon_auto
  \dir includes/classes/
  
  \brief
*/
class discount_coupon_auto {

    var $messages, $coupon, $applied_discount, $cart_info;

    function discount_coupon( $code, $delivery ) {
      $this->messages = array();
      $this->coupon = array();
      $this->applied_discount = array();
      $this->cart_info = array( 'valid_products' => array( 'count' => 0, 'line_items' => 0, 'total' => 0 ),
                                'total_products' => array( 'count' => 0, 'line_items' => 0, 'total' => 0 ),
                                'exclusions' => array() );
      $this->get_coupon( $code, $delivery );
      //get the module configuration values for debugging
          if( _cst_bool('MODULE_ORDER_TOTAL_DISCOUNT_COUPON_DEBUG')) {
        $check_values_query = tep_db_query( $sql = "SELECT configuration_key, configuration_value
                                                    FROM " . TABLE_CONFIGURATION . "
                                                    WHERE configuration_key LIKE 'MODULE_ORDER_TOTAL_DISCOUNT_COUPON%'
                                                      OR configuration_key = 'DISPLAY_PRICE_WITH_TAX'
                                                      OR configuration_key = 'MODULE_SHIPPING_TABLE_STATUS'
                                                      OR configuration_key = 'MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING'" );
        while( $row = tep_db_fetch_array( $check_values_query ) ) {
          $this->message( 'INFO: '.$row['configuration_key'].' = '.$row['configuration_value'], 'debug' );
        }
      }
    }
/*
    function verify_code() {
      //check the global number of discounts that may be used
      if( $this->coupon['coupons_number_available'] != 0 ) {
        $this->check_num_available();
      }
      //if coupons_max_use==0, then use is unlimited, otherwise, we need to verify the customer hasn't used this coupon more than coupons_max_use times
      if( $this->coupon['coupons_max_use'] != 0 ) {
        $this->check_coupons_max_use();
      }
    }*/

//     function get_coupon( $code, $delivery ) {
//       global $customer_id; //needed for customer_exclusions
//       $check_code_query = tep_db_query( $sql = "SELECT dc.*
//                                                 FROM " . TABLE_DISCOUNT_COUPONS . " dc
//                                                 WHERE coupons_id = '".tep_db_input( $code )."'
//                                                   AND ( coupons_date_start <= CURDATE() OR coupons_date_start IS NULL )
//                                                   AND ( coupons_date_end >= CURDATE() OR coupons_date_end IS NULL )" );
//       if( tep_db_num_rows( $check_code_query ) != 1 ) { //if no rows are returned, then they haven't entered a valid code
//         $this->message( ENTRY_DISCOUNT_COUPON_ERROR ); //display the error message
//       } else {
//         //customer_exclusions
//         $check_user_query = tep_db_query( $sql = 'SELECT dc2u.customers_id
//                                                   FROM '.TABLE_DISCOUNT_COUPONS_TO_CUSTOMERS.' dc2u
//                                                   WHERE customers_id='.(int)$customer_id.'
//                                                     AND coupons_id="'.tep_db_input( $code ).'"' );
//         if( tep_db_num_rows( $check_user_query ) > 0 ) {
//           $this->message( ENTRY_DISCOUNT_COUPON_ERROR ); //display the error message
//           //use this to debug exclusions:
//           //$this->message( 'Customer exclusion check failed' );
//         }
//         //shipping zone exclusions
//         $check_user_query = tep_db_query($sql = 'SELECT dc2z.geo_zone_id
//                                                   FROM '.TABLE_DISCOUNT_COUPONS_TO_ZONES.' dc2z
//                                                   LEFT JOIN '.TABLE_ZONES_TO_GEO_ZONES.' z2g
//                                                     USING( geo_zone_id )
//                                                   WHERE ( z2g.zone_id='.(int)$delivery['zone_id'].' or z2g.zone_id = 0 or z2g.zone_id IS NULL )
//                                                     AND ( z2g.zone_country_id='.(int)$delivery['country_id'].' or z2g.zone_country_id = 0 )
//                                                     AND dc2z.coupons_id="'.tep_db_input( $code ).'"' );
//
//           if (tep_db_num_rows( $check_user_query ) > 0 ) {
//             $this->message( ENTRY_DISCOUNT_COUPON_ERROR ); //display the error message
//           //use this to debug exclusions:
//             //$this->message( 'Shipping Zones exclusion check failed' );
//           }
//           //end shipping zone exclusions
//         $row = tep_db_fetch_array( $check_code_query ); //since there is one record, we have a valid code
//         $this->coupon = $row;
//       }
//     }


}
?>