Loaded Commerce Community

Banner


Board index » CRE Loaded Support » CRE Loaded 6.3

All times are UTC - 5 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Because of another posting RE: Coupons I found something....
PostPosted: Mon Jul 25, 2011 10:31 pm 
Offline
CRE Talented

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 489
Location: CO
... about the coupons on 6.3.3.

For some reason the coupon redeems based on the retail price versus the salemaker price. Yup I know 6.3.3 and salemaker had its problems but I am hoping someone has a (simple) fix. I just advertised a 10% off coupon but unfortunately I didn't remember this problem but the ad is out there so I have to honor it, BUT if there is a fix I can same myself some heartache and save myself from selling some things for under what I pay for them. :o

EDIT: HOLY crap.... I have played around with the coupons a lot in the last 40 minutes then I ever have and I also ran into other things that make me go UT OH....
I can change the number of products in the cart and the discount amount never changes... In other words I could add $10000.00 of items to the cart, add the coupon, remove a bunch of products and the discount amount remains the same even with a LOT less value in the cart!!!!

I am afraid to see what would happen if someone else figures this out! :(


Thank you,
Mike


Top
 Profile  
 
 Post subject: Re: Because of another posting RE: Coupons I found something....
PostPosted: Tue Jul 26, 2011 5:35 am 
Offline
CRE Legend
User avatar

Joined: Thu Jun 12, 2008 6:39 am
Posts: 2404
Location: New Zealand
Yep, coupons with % discounts have issues, even in 6.4. Salemaker was discontinued in 6.4.x, 6.3.x is no longer supported by Cre ... so if you're using either, I'd put in some disclaimers and expect issues.

Simon

_________________
www.codemehappy.com
For Cre Loaded tips, how-to articles and more


Top
 Profile  
 
 Post subject: Re: Because of another posting RE: Coupons I found something....
PostPosted: Tue Jul 26, 2011 6:22 am 
Offline
CRE Talented

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 489
Location: CO
soundzgood2 wrote:
Yep, coupons with % discounts have issues, even in 6.4. Salemaker was discontinued in 6.4.x, 6.3.x is no longer supported by Cre ... so if you're using either, I'd put in some disclaimers and expect issues.

Simon


Simon,

I agree but now with what seems to be storing of the discount amount this would kill us. Is there anything you can think of to fix the storing of the amount? i.e. "It stores it in this variable so unset it" or something of the like?

That is crazy that I can load up the cart, process the discount then remove items from the cart and the discount amount stays as originally calculated.

Yes at first I wanted a good fix for use with salemaker (far reaching hope) but now I would just like the discount amount cleared if they navigate away from the page (or go to the cart to make updates).

Any thoughts on where the amount is stored and how to clear it?

Mike


Top
 Profile  
 
 Post subject: Re: Because of another posting RE: Coupons I found something....
PostPosted: Tue Jul 26, 2011 6:47 am 
Offline
CRE Legend
User avatar

Joined: Thu Jun 12, 2008 6:39 am
Posts: 2404
Location: New Zealand
I'm not 100% clear on what you're trying to do, or even if you have your order totals setup in right order etc ... hard to suggest what to do based on above. I do know that % coupon discounts have problems. I assume you're using the 3 page checkout?

Simon

_________________
www.codemehappy.com
For Cre Loaded tips, how-to articles and more


Top
 Profile  
 
 Post subject: Re: Because of another posting RE: Coupons I found something....
PostPosted: Tue Jul 26, 2011 3:12 pm 
Offline
CRE Talented

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 489
Location: CO
soundzgood2 wrote:
I'm not 100% clear on what you're trying to do, or even if you have your order totals setup in right order etc ... hard to suggest what to do based on above. I do know that % coupon discounts have problems. I assume you're using the 3 page checkout?

Simon


Hi again and thank you very much for being around still to help (I try but the really knowledgeable ones, like you, have mostly left...)

I have both the one page check-out and 3 page checkout. It happens with both.

I am trying to ensure a customer doesn't find the flaw (bug) that I found and exploit it.... The flaw is that I can add a large amount to the cart, apply the % discount, then navigate away from the page, remove items from the cart and still have that % discount based on the original cart value.

- If they add $1000 to the cart (I give a 10% discount coupon code), they apply the discount code and get $100 discount.
- Then they choose to not proceed all the way to the checkout but go back to the cart, remove all items except for maybe one.
- When they return to the order_checkout.php page (or checkout_confirmation.php) cart shows a $100 discount still.

I have no idea why it would hold onto that value (in this case $100) and how to stop it from holding onto that value.


The order of the "order total modules" are:

Sub-Total 1
Discount Coupons 100
Gift Vouchers 101
Low Order Fee 200
Shipping 900
Tax 901
Total 10000

I can send you the url to my site but I would like to avoid posting it because of this.

Thank you again,

Mike

P.S. I am thinking the discount amount is stored in the session variable... If that is true, is there a way to zero that out when shopping_cart.php is called? This would, or at least I think it would, force the coupon code to be re-entered but that is MUCH better then have it stored and never recalculated.


Top
 Profile  
 
 Post subject: Re: Because of another posting RE: Coupons I found something....
PostPosted: Tue Jul 26, 2011 5:24 pm 
Offline
CRE Legend
User avatar

Joined: Thu Jun 12, 2008 6:39 am
Posts: 2404
Location: New Zealand
Right, so have you actually placed an order with this method? Ie gone through a payment method and got an order in the admin plus an invoice that shows the discount applied to an edited cart?

I'd change the order total sort order for coupons/gvs - I always put them last right before the total.

Also - tried this on a free CE 641a install?

Simon

_________________
www.codemehappy.com
For Cre Loaded tips, how-to articles and more


Top
 Profile  
 
 Post subject: Re: Because of another posting RE: Coupons I found something....
PostPosted: Tue Jul 26, 2011 5:52 pm 
Offline
CRE Talented

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 489
Location: CO
soundzgood2 wrote:
Right, so have you actually placed an order with this method? Ie gone through a payment method and got an order in the admin plus an invoice that shows the discount applied to an edited cart?

I'd change the order total sort order for coupons/gvs - I always put them last right before the total.

Also - tried this on a free CE 641a install?

Simon


I have placed an order and it does come out exactly as shown throughout the checkout process.

I just tried resorting and it gives the same. The really hard part of this is that I have a non-stop, constant amount being applied for that coupon. No matter what... I clear the session table, deleted products, apply the 10% discount and it always has the same bloody amount being given as a discount. BUT if I go into admin --- coupon admin, and change the discount to lets say 8% instead of 10% the discount amount changes..... Grrr... How is the discount logic applied?

Hey! .... Here is what I just did ---
One page checkout.
On page: order_checkout.php
Changed coupon percentage.
Refreshed the order_checkout.php page - amount of discount changed.
Changed coupon percentage again.
Again refreshed the order_checkout.php page -discount changed.

So it looks like I should look through order_checkout.php and see if I can find where the coupon logic is called... CRAZY. (I hope that file is the right one to look in).

To answer the 6.4 question.... I have not yet... If I move to 6.4 it will have to have great functionality... Or I'm moving cart systems. I have, no kidding 48 pages of documented changes to the 6.3.3 code that make my site do what I want to that it would pain me to redo.... So I might try and see if I have enough time to set up 6.4 to test but for right now I have not tried it.

Thank you,
Mike


Top
 Profile  
 
 Post subject: Re: Because of another posting RE: Coupons I found something....
PostPosted: Tue Jul 26, 2011 8:30 pm 
Offline
CRE Legend
User avatar

Joined: Thu Jun 12, 2008 6:39 am
Posts: 2404
Location: New Zealand
I can't reproduce this error in any 6.4.x cart - there are issues with % coupons BUT not in such an obvious flaw.

The main files to check would be /i/m/order_total/ot_coupon.php and /checkout_process.php.
If you compare a 6.3.x ot_coupon file with a 6.4.x ot_coupon file, there's a lot of difference (firstly the 6.3.x file is missing the global call to $order in the process area, so that'll cause multiple fails.)

48 pages of changes aside, your options are upgrade OR at the very least nick the files out of a CE version that are missing the right code.

Said it a couple times before: only use 6.3.x carts if you are running limited functionality and expect issues.
UPGRADE!

Simon

_________________
www.codemehappy.com
For Cre Loaded tips, how-to articles and more


Top
 Profile  
 
 Post subject: Re: Because of another posting RE: Coupons I found something....
PostPosted: Tue Jul 26, 2011 9:31 pm 
Offline
CRE Talented

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 489
Location: CO
Simon,

I understand 100% why you say upgrade. I really do. But I can't now.

I'll be attempting to compare and see where it changes that might help me. Also I'll see if I can load up the 6.4.1a B2B I have a license for in a spare directory and see if it works or not for me.

I don't expect I'll get to the 6.4.1a version before mid-august as this is not the only thing I have going on but I will try my damdest to get it done for a test setup - I do not think I will ever move the production store to 6.4.x unless it has the salemaker functionality..

As you may remember I was one of the people who was upset with the removal of salemaker since I used that so that I could also have the specials.php and if I don't use salemaker and only use specials then every product in my store will show up as a special because everything is discounted off MSRP.

I hope you can empathize with why I feel I need to get 6.3.3 working.

Thank you for the suggestions and I hope future help. :)

Mike


Top
 Profile  
 
 Post subject: Re: Because of another posting RE: Coupons I found something....
PostPosted: Tue Jul 26, 2011 10:02 pm 
Offline
CRE Talented

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 489
Location: CO
As an update:
Between 6.3.3 and 6.4.1a includes\modules\order_total\ot_coupon.php are identical.
I see the global $order; in both files.

Also with \checkout_process.php the only change is some Payflow Pro related stuff but all the rest is 100% the same.

So those two are not the cause. I am doing a grep search on all files for "coupon" to see if I can figure something else out but any other suggestions are always welcome. :)

Mike


Top
 Profile  
 
 Post subject: Re: Because of another posting RE: Coupons I found something....
PostPosted: Tue Jul 26, 2011 11:41 pm 
Offline
CRE Legend
User avatar

Joined: Thu Jun 12, 2008 6:39 am
Posts: 2404
Location: New Zealand
Really? Well this is what I've got in 6.3 ot_coupon.php ...

Code:
<?php
/*
  $Id: ot_coupon.php,v 1.4 2004/03/09 17:56:06 ccwjr Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2002 osCommerce

  Released under the GNU General Public License
*/

class ot_coupon {
var $title, $output,$credit_class;

function ot_coupon() {
  $this->code = 'ot_coupon';
  $this->header = (defined('MODULE_ORDER_TOTAL_COUPON_HEADER')) ? MODULE_ORDER_TOTAL_COUPON_HEADER : ''; 
  $this->title = (defined('MODULE_ORDER_TOTAL_COUPON_TITLE')) ? MODULE_ORDER_TOTAL_COUPON_TITLE : '';
  $this->description = (defined('MODULE_ORDER_TOTAL_COUPON_DESCRIPTION')) ? MODULE_ORDER_TOTAL_COUPON_DESCRIPTION : '';
  $this->enabled = (defined('MODULE_ORDER_TOTAL_COUPON_STATUS') && MODULE_ORDER_TOTAL_COUPON_STATUS == 'true') ? true : false;
  $this->sort_order = (defined('MODULE_ORDER_TOTAL_COUPON_SORT_ORDER')) ? (int)MODULE_ORDER_TOTAL_COUPON_SORT_ORDER : 30;
  $this->include_shipping = (defined('MODULE_ORDER_TOTAL_COUPON_INC_SHIPPING')) ? MODULE_ORDER_TOTAL_COUPON_INC_SHIPPING : false;
  $this->include_tax = (defined('MODULE_ORDER_TOTAL_COUPON_INC_TAX')) ? MODULE_ORDER_TOTAL_COUPON_INC_TAX : false;
  $this->calculate_tax = (defined('MODULE_ORDER_TOTAL_COUPON_CALC_TAX')) ? MODULE_ORDER_TOTAL_COUPON_CALC_TAX : false;
  $this->tax_class = (defined('MODULE_ORDER_TOTAL_COUPON_TAX_CLASS')) ? MODULE_ORDER_TOTAL_COUPON_TAX_CLASS : '';
  $this->credit_class = true;
  $this->output = array();
}

function process() {
global $PHP_SELF, $order, $currencies;
  $order_total=$this->get_order_total();
  $od_amount = $this->calculate_credit($order_total);
  $tod_amount = 0.0; //Fred
  $this->deduction = $od_amount;
  if ($this->calculate_tax != 'None') { //Fred - changed from 'none' to 'None'!
    $tod_amount = $this->calculate_tax_deduction($order_total, $this->deduction, $this->calculate_tax);
  }

  if ($od_amount > 0) {
    $order->info['total'] = $order->info['total'] - $od_amount;
    $this->output[] = array('title' => $this->title . ':' . $this->coupon_code .':','text' => '<b>-' . $currencies->format($od_amount) . '</b>', 'value' => $od_amount); //Fred added hyphen
  }
}

function selection_test() {
  return false;
}

function pre_confirmation_check($order_total) {

  return $this->calculate_credit($order_total);
}

function use_credit_amount() {
  $output_string  = '';
  return $output_string;
}

/*
function credit_selection() {
global  $currencies, $language;
  $selection_string = '';
  $selection_string .= '<tr>' . "\n";
  $selection_string .= ' <td width="10">' . tep_draw_separator('pixel_trans.gif', '10', '1') .'</td>';
  $selection_string .= ' <td class="main">' . "\n";
  $image_submit = '<input type="image" name="submit_redeem" onClick="submitFunction()" src="' . DIR_WS_TEMPLATES . TEMPLATE_NAME . '/images/buttons/' . $language . '/button_redeem.gif" border="0" alt="' . IMAGE_REDEEM_VOUCHER . '" title = "' . IMAGE_REDEEM_VOUCHER . '">';
  $selection_string .= TEXT_ENTER_COUPON_CODE . tep_draw_input_field('gv_redeem_code') . '</td>';
  $selection_string .= ' <td align="right">' . $image_submit . '</td>';
  $selection_string .= ' <td width="10">' . tep_draw_separator('pixel_trans.gif', '10', '1') . '</td>';
  $selection_string .= '</tr>' . "\n";
  return $selection_string;
}
*/
function credit_selection() {
global $customer_id, $currencies, $language;
  $selection_string2 = '';
  $selection_string2 .= '<tr>' . "\n";
  $selection_string2 .= '<td class="main">' . "\n";
        $selection_string2 .= TEXT_ENTER_COUPON_CODE . tep_draw_input_field('gv_redeem_code') . ' and click ';
  $image_submit2 = tep_template_image_submit('button_redeem.gif', IMAGE_REDEEM_VOUCHER, 'onClick="submitFunction()"');
  $selection_string2 .= ' </td><td align="right">' . $image_submit2 . '</td>';
  $selection_string2 .= '</tr>' . "\n";
        return $selection_string2;
}

function collect_posts() {
global  $currencies;
  if ($_POST['gv_redeem_code']) {

// get some info from the coupon table
  $coupon_query=tep_db_query("select coupon_id, coupon_amount, coupon_type, coupon_minimum_order,uses_per_coupon, uses_per_user, restrict_to_products,restrict_to_categories from " . TABLE_COUPONS . " where coupon_code='".tep_db_input($_POST['gv_redeem_code'])."' and coupon_active='Y'");
  $coupon_result=tep_db_fetch_array($coupon_query);

  if ($coupon_result['coupon_type'] != 'G') {

    if (tep_db_num_rows($coupon_query)==0) {
      tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error='.$this->code.'&error=' . urlencode(ERROR_NO_INVALID_REDEEM_COUPON), 'SSL'));
    }

    $date_query=tep_db_query("select coupon_start_date from " . TABLE_COUPONS . " where coupon_start_date <= now() and coupon_code='".tep_db_input($_POST['gv_redeem_code'])."'");

    if (tep_db_num_rows($date_query)==0) {
      tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error='.$this->code.'&error=' . urlencode(ERROR_INVALID_STARTDATE_COUPON), 'SSL'));
  }

    $date_query=tep_db_query("select coupon_expire_date from " . TABLE_COUPONS . " where coupon_expire_date >= now() and coupon_code='".tep_db_input($_POST['gv_redeem_code'])."'");

    if (tep_db_num_rows($date_query)==0) {
        tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error='.$this->code.'&error=' . urlencode(ERROR_INVALID_FINISDATE_COUPON), 'SSL'));
    }

    $coupon_count = tep_db_query("select coupon_id from " . TABLE_COUPON_REDEEM_TRACK . " where coupon_id = '" . $coupon_result['coupon_id']."'");
    $coupon_count_customer = tep_db_query("select coupon_id from " . TABLE_COUPON_REDEEM_TRACK . " where coupon_id = '" . $coupon_result['coupon_id']."' and customer_id = '" . $_SESSION['customer_id'] . "'");

    if (tep_db_num_rows($coupon_count)>=$coupon_result['uses_per_coupon'] && $coupon_result['uses_per_coupon'] > 0) {
      tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error='.$this->code.'&error=' . urlencode(ERROR_INVALID_USES_COUPON . $coupon_result['uses_per_coupon'] . TIMES ), 'SSL'));
  }

    if (tep_db_num_rows($coupon_count_customer)>=$coupon_result['uses_per_user'] && $coupon_result['uses_per_user'] > 0) {
      tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error='.$this->code.'&error=' . urlencode(ERROR_INVALID_USES_USER_COUPON . $coupon_result['uses_per_user'] . TIMES ), 'SSL'));
  }

                global $order,$ot_coupon,$currency;

                $_SESSION['cc_id'] = $coupon_result['coupon_id'];

                $coupon_amount= tep_round($ot_coupon->pre_confirmation_check($order->info['subtotal']), $currencies->currencies[$currency]['decimal_places']);

/* you will need to uncomment this if your tax order total module is AFTER shipping eg you have all of your tax, including tax from shipping module, in your tax total.
                if ($coupon_result['coupon_type']=='S')  {
                        //if not zero rated add vat to shipping
                        $coupon_amount = tep_add_tax($coupon_amount, '17.5');
                }
*/
                $coupon_amount_out = $currencies->format($coupon_amount) . ' ';
                if ($coupon_result['coupon_minimum_order']>0) $coupon_amount_out .= 'on orders greater than ' . $currencies->format($coupon_result['coupon_minimum_order']);

                if ( strlen($_SESSION['cc_id']) > 0 && $coupon_amount == 0 ) {
                        $err_msg = ERROR_REDEEMED_AMOUNT_ZERO;
                } else {
                        $err_msg = ERROR_REDEEMED_AMOUNT.$coupon_amount_out;
                }
                tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error='.$this->code.'&error=' . urlencode($err_msg), 'SSL'));
//**si** 09-11-05 end

                // $_SESSION['cc_id'] = $coupon_result['coupon_id']; //Fred commented out, do not use $_SESSION[] due to backward comp. Reference the global var instead.
          } // ENDIF valid coupon code
        } // ENDIF code entered
        // v5.13a If no code entered and coupon redeem button pressed, give an alarm
        if (isset($_POST['submit_redeem_coupon_x'])) tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error='.$this->code.'&error=' . urlencode(ERROR_NO_REDEEM_CODE), 'SSL'));
}

function calculate_credit($amount) {
  global $customer_id, $order, $cc_id;
  $cc_id = $_SESSION['cc_id'];
  $od_amount = 0;
  if (isset($cc_id) ) {
    $coupon_query = tep_db_query("select coupon_code from " . TABLE_COUPONS . " where coupon_id = '" . $cc_id . "'");
    if (tep_db_num_rows($coupon_query) !=0 ) {
      $coupon_result = tep_db_fetch_array($coupon_query);
      $this->coupon_code = $coupon_result['coupon_code'];
      $coupon_get = tep_db_query("select coupon_amount, coupon_minimum_order, restrict_to_products, restrict_to_categories, coupon_type from " . TABLE_COUPONS ." where coupon_code = '". $coupon_result['coupon_code'] . "'");
      $get_result = tep_db_fetch_array($coupon_get);
      $c_deduct = $get_result['coupon_amount'];
      if ($get_result['coupon_type']=='S') $c_deduct = $order->info['shipping_cost'];
      //v5.14 id coupon total > 0
      $p_processed = false;
      if ($get_result['coupon_type']=='S') $c_deduct = $order->info['shipping_cost'];
      if ($get_result['coupon_type']=='S' && $get_result['coupon_amount'] > 0 ) $c_deduct = $order->info['shipping_cost'] + $get_result['coupon_amount'];
      if ($get_result['coupon_minimum_order'] <= $this->get_order_total()) {
        if ($get_result['restrict_to_products'] || $get_result['restrict_to_categories']) {
          if ($get_result['restrict_to_categories']) {
            $cat_ids = split("[,]", $get_result['restrict_to_categories']);
            for ($j=0; $j < sizeof($order->products); $j++) {
              $my_path = tep_get_product_path(tep_get_prid($order->products[$j]['id']));
              $sub_cat_ids = split("[_]", $my_path);
              for ($iii = 0; $iii < count($sub_cat_ids); $iii++) {
                for ($ii = 0; $ii < count($cat_ids); $ii++) {
                  if ($sub_cat_ids[$iii] == $cat_ids[$ii]) {
                    if ($get_result['coupon_type'] == 'P') {
                      $p_processed = true; 
                      $t = tep_get_prid($order->products[$i]['id']);
                      $pr_c = $this->product_price(tep_get_prid($order->products[$j]['id']));
                      $pod_amount = tep_round($pr_c*10,2)/10*$c_deduct/100;
                      $od_amount = $od_amount + $pod_amount;
                      continue 3;
                    } else {
                      $od_amount = $c_deduct;
                      continue 3;
                    }
                  }
                }
              }
            }
          } //endif $get_result['restrict_to_products']
         
          if ($get_result['restrict_to_products']) {
            $pr_ids = explode(",", $get_result['restrict_to_products']);
            for ($ii = 0; $ii < count($pr_ids); $ii++) {
              for ($i=0; $i < sizeof($order->products); $i++) {
                if ($pr_ids[$ii] == tep_get_prid($order->products[$i]['id'])) {
                  if ($get_result['coupon_type'] == 'P') {   
                    $p_processed = true;                       
                    $pr_c = $this->product_price($pr_ids[$ii]); //Fred 2003-10-28, fix for the row above, otherwise the discount is calc based on price excl VAT!
                    $pod_amount = tep_round($pr_c*10,2)/10*$c_deduct/100;
                    $od_amount = $od_amount + $pod_amount;
                  } else {
                    $od_amount = $c_deduct;
                  }
                }
              }
            }
          }
        } else {
          if ($get_result['coupon_type'] !='P') {
            $od_amount = $c_deduct;
          } else {
            $od_amount = $amount * $get_result['coupon_amount'] / 100;
          }
        }
      }
    }
    if ($od_amount>$amount) $od_amount = $amount;
  }
  return $od_amount;
}

function calculate_tax_deduction($amount, $od_amount, $method) {
global $order, $cart;
  $coupon_query = tep_db_query("select coupon_code from " . TABLE_COUPONS . " where coupon_id = '" . $_SESSION['cc_id'] . "'");
  if (tep_db_num_rows($coupon_query) !=0 ) {
    $coupon_result = tep_db_fetch_array($coupon_query);
    $coupon_get = tep_db_query("select coupon_amount, coupon_minimum_order, restrict_to_products, restrict_to_categories, coupon_type from " . TABLE_COUPONS . " where coupon_code = '". $coupon_result['coupon_code'] . "'");
    $get_result = tep_db_fetch_array($coupon_get);
    if ($get_result['coupon_type'] != 'S') {

      //RESTRICTION--------------------------------
      if ($get_result['restrict_to_products'] || $get_result['restrict_to_categories']) {
        // What to do here.
        // Loop through all products and build a list of all product_ids, price, tax class
        // at the same time create total net amount.
        // then
        // for percentage discounts. simply reduce tax group per product by discount percentage
        // or
        // for fixed payment amount
        // calculate ratio based on total net
        // for each product reduce tax group per product by ratio amount.
        $products = $cart->get_products();
        $valid_product = false;
        for ($i=0; $i < sizeof($products); $i++) {
        $valid_product = false;
          $t_prid = tep_get_prid($products[$i]['id']);
          $cc_query = tep_db_query("select products_tax_class_id from " . TABLE_PRODUCTS . " where products_id = '" . $t_prid . "'");
          $cc_result = tep_db_fetch_array($cc_query);
          if ($get_result['restrict_to_products']) {
            $pr_ids = split("[,]", $get_result['restrict_to_products']);
            for ($p = 0; $p < sizeof($pr_ids); $p++) {
              if ($pr_ids[$p] == $t_prid) $valid_product = true;
            }
          }
          if ($get_result['restrict_to_categories']) {
                        // Tanaka 2005-4-30:  Original Code
                        /*$cat_ids = split("[,]", $get_result['restrict_to_categories']);
                        for ($c = 0; $c < sizeof($cat_ids); $c++) {
                            // Tanaka 2005-4-30:  changed $products_id to $t_prid and changed $i to $c
                            $cat_query = tep_db_query("select products_id from products_to_categories where products_id = '" . $t_prid . "' and categories_id = '" . $cat_ids[$c] . "'");
                            if (tep_db_num_rows($cat_query) !=0 ) $valid_product = true;
                        }*/
                        // v5.13a Tanaka 2005-4-30:  New code, this correctly identifies valid products in subcategories
                        $cat_ids = split("[,]", $get_result['restrict_to_categories']);
                        $my_path = tep_get_product_path($t_prid);
                        $sub_cat_ids = split("[_]", $my_path);
                        for ($iii = 0; $iii < count($sub_cat_ids); $iii++) {
                            for ($ii = 0; $ii < count($cat_ids); $ii++) {
                                if ($sub_cat_ids[$iii] == $cat_ids[$ii]) {
                                    $valid_product = true;
                                    continue 2;
                                }
                            }
                        }
          }
          if ($valid_product) {
            $price_excl_vat = $products[$i]['final_price'] * $products[$i]['quantity']; //Fred - added
            $price_incl_vat = $this->product_price($t_prid); //Fred - added
            $valid_array[] = array('product_id' => $t_prid, 'products_price' => $price_excl_vat, 'products_tax_class' => $cc_result['products_tax_class_id']); //jason //Fred - changed from $products[$i]['final_price'] 'products_tax_class' => $cc_result['products_tax_class_id']);
//            $total_price += $price_incl_vat; //Fred - changed
            $total_price += $price_excl_vat; // changed
          }
        }
        if (sizeof($valid_array) > 0) { // if ($valid_product) {
          if ($get_result['coupon_type'] == 'P') {
            $ratio = $get_result['coupon_amount']/100;
          } else {
            $ratio = $od_amount / $total_price;
          }
          if ($get_result['coupon_type'] == 'S') $ratio = 1;
          if ($method=='Credit Note') {
            $tax_rate = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
            $tax_desc = tep_get_tax_description($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
            if ($get_result['coupon_type'] == 'P') {
              $tod_amount = $od_amount / (100 + $tax_rate)* $tax_rate;
            } else {
              $tod_amount = $order->info['tax_groups'][$tax_desc] * $od_amount/100;
            }
            $order->info['tax_groups'][$tax_desc] -= $tod_amount;
            $order->info['total'] -= $tod_amount; //  need to modify total ...OLD
            $order->info['tax'] -= $tod_amount; //Fred - added
          } else {
            for ($p=0; $p<sizeof($valid_array); $p++) {
              $tax_rate = tep_get_tax_rate($valid_array[$p]['products_tax_class'], $order->delivery['country']['id'], $order->delivery['zone_id']);
              $tax_desc = tep_get_tax_description($valid_array[$p]['products_tax_class'], $order->delivery['country']['id'], $order->delivery['zone_id']);
              if ($tax_rate > 0) {
                //Fred $tod_amount[$tax_desc] += ($valid_array[$p]['products_price'] * $tax_rate)/100 * $ratio; //OLD
                $tod_amount = ($valid_array[$p]['products_price'] * $tax_rate)/100 * $ratio; // calc total tax Fred - added
                $order->info['tax_groups'][$tax_desc] -= ($valid_array[$p]['products_price'] * $tax_rate)/100 * $ratio;
                $order->info['total'] -= ($valid_array[$p]['products_price'] * $tax_rate)/100 * $ratio; // adjust total
                $order->info['tax'] -= ($valid_array[$p]['products_price'] * $tax_rate)/100 * $ratio; // adjust tax -- Fred - added
              }
            }
          }
        }
        //NO RESTRICTION--------------------------------
      } else {
        if ($get_result['coupon_type'] =='F') {
          $tod_amount = 0;
          if ($method=='Credit Note') {
            $tax_rate = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
            $tax_desc = tep_get_tax_description($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
            $tod_amount = $od_amount / (100 + $tax_rate)* $tax_rate;
            $order->info['tax_groups'][$tax_desc] -= $tod_amount;
          } else {
//            $ratio1 = $od_amount/$amount;   // this produces the wrong ratipo on fixed amounts
            reset($order->info['tax_groups']);
            while (list($key, $value) = each($order->info['tax_groups'])) {
              $ratio1 = $od_amount/($amount-$order->info['tax_groups'][$key]); ////debug
              $tax_rate = tep_get_tax_rate_from_desc($key);
              $net = $tax_rate * $order->info['tax_groups'][$key];
              if ($net>0) {
                $god_amount = $order->info['tax_groups'][$key] * $ratio1;
                $tod_amount += $god_amount;
                $order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;
              }
            }
          }
          $order->info['total'] -= $tod_amount; //OLD
          $order->info['tax'] -= $tod_amount; //Fred - added
      }
      if ($get_result['coupon_type'] =='P') {
        $tod_amount=0;
        if ($method=='Credit Note') {
          $tax_desc = tep_get_tax_description($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
          $tod_amount = $order->info['tax_groups'][$tax_desc] * $od_amount/100;
          $order->info['tax_groups'][$tax_desc] -= $tod_amount;
        } else {
          reset($order->info['tax_groups']);
          while (list($key, $value) = each($order->info['tax_groups'])) {
            $god_amount=0;
            $tax_rate = tep_get_tax_rate_from_desc($key);
            $net = $tax_rate * $order->info['tax_groups'][$key];
            if ($net>0) {
              $god_amount = $order->info['tax_groups'][$key] * $get_result['coupon_amount']/100;
              $tod_amount += $god_amount;
              $order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;
            }
          }
        }
        $order->info['total'] -= $tod_amount; // have to modify total also
        $order->info['tax'] -= $tod_amount;
      }
    }
  }
}
return $tod_amount;
}

function update_credit_account($i) {
  return false;
}

function apply_credit() {
global $insert_id,  $REMOTE_ADDR;
  if ($this->deduction !=0) {
    tep_db_query("insert into " . TABLE_COUPON_REDEEM_TRACK . " (coupon_id, redeem_date, redeem_ip, customer_id, order_id) values ('" . $_SESSION['cc_id'] . "', now(), '" . $REMOTE_ADDR . "', '" . $_SESSION['customer_id'] . "', '" . $insert_id . "')");
  }
  unset($_SESSION['cc_id']);
}

function get_order_total() {
global $order, $cart;
  $order_total = $order->info['total'];
  // Check if gift voucher is in cart and adjust total
  $products = $cart->get_products();
  for ($i=0; $i<sizeof($products); $i++) {
    $t_prid = tep_get_prid($products[$i]['id']);
    $gv_query = tep_db_query("select products_price, products_tax_class_id, products_model from " . TABLE_PRODUCTS . " where products_id = '" . $t_prid . "'");
    $gv_result = tep_db_fetch_array($gv_query);
    if (ereg('^GIFT', addslashes($gv_result['products_model']))) {
      $qty = $cart->get_quantity($t_prid);
      $products_tax = tep_get_tax_rate($gv_result['products_tax_class_id']);
      if ($this->include_tax == false) {
        $gv_amount = $gv_result['products_price'] * $qty;
      } else {
        $gv_amount = ($gv_result['products_price'] + tep_calculate_tax($gv_result['products_price'],$products_tax)) * $qty;
      }
      $order_total=$order_total - $gv_amount;
    }
  }
  if ($this->include_tax == false) $order_total=$order_total-$order->info['tax'];
  if ($this->include_shipping == false) $order_total=$order_total-$order->info['shipping_cost'];
  // OK thats fine for global coupons but what about restricted coupons
  // where you can only redeem against certain products/categories.
  // and I though this was going to be easy !!!

  if(!isset($_SESSION['cc_id'])) {
    $_SESSION['cc_id'] = false;
  }

  $coupon_query=tep_db_query("select coupon_code from " . TABLE_COUPONS . " where coupon_id='" . $_SESSION['cc_id'] . "'");
  if (tep_db_num_rows($coupon_query) !=0) {
    $coupon_result=tep_db_fetch_array($coupon_query);
    $coupon_get=tep_db_query("select coupon_amount, coupon_minimum_order,restrict_to_products,restrict_to_categories, coupon_type from " . TABLE_COUPONS . " where coupon_code='".$coupon_result['coupon_code']."'");
    $get_result=tep_db_fetch_array($coupon_get);
    $in_cat = true;
    if ($get_result['restrict_to_categories']) {
      $cat_ids = split("[,]", $get_result['restrict_to_categories']);
      $in_cat=false;
      for ($i = 0; $i < count($cat_ids); $i++) {
        if (is_array($this->contents)) {
          reset($this->contents);
          while (list($products_id, ) = each($this->contents)) {
            $cat_query = tep_db_query("select products_id from products_to_categories where products_id = '" . $products_id . "' and categories_id = '" . $cat_ids[$i] . "'");
            if (tep_db_num_rows($cat_query) !=0 ) {
              $in_cat = true;
              $total_price += $this->get_product_price($products_id);
            }
          }
        }
      }
    }
    $in_cart = true;
    if ($get_result['restrict_to_products']) {

      $pr_ids = split("[,]", $get_result['restrict_to_products']);

      $in_cart=false;
      $products_array = $cart->get_products();

      for ($i = 0; $i < sizeof($pr_ids); $i++) {
        for ($ii = 1; $ii<=sizeof($products_array); $ii++) {
          if (tep_get_prid($products_array[$ii-1]['id']) == $pr_ids[$i]) {
            $in_cart=true;
            $total_price += $this->get_product_price($products_array[$ii-1]['id']);
          }
        }
      }
      //$order_total = $total_price;
      if ($total_price > 0) {
        $order_total = $total_price;
      }
    }
  }
return $order_total;
}

function get_product_price($product_id) {
global $cart, $order;
  $products_id = tep_get_prid($product_id);
  // products price
  $qty = $cart->contents[$product_id]['qty'];

  /***********    GSR Start  19 Sept 2007  *************/
  foreach ($cart->contents as $x_key => $x_val) {
    $tmp_arr = explode('{',$x_key);
    if (is_array($tmp_arr) && $tmp_arr[0] == (int)$product_id && $qty =='') {
      $qty = $cart->contents[$x_key]['qty'];
    }
  }
  /***********    GSR End   19 Sept 2007   *************/

  $product_query = tep_db_query("select products_id, products_price, products_tax_class_id, products_weight from " . TABLE_PRODUCTS . " where products_id='" . $product_id . "'");
  if ($product = tep_db_fetch_array($product_query)) {
    $prid = $product['products_id'];
    $products_tax = tep_get_tax_rate($product['products_tax_class_id']);
    $products_price = $product['products_price'];
    $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . $prid . "' and status = '1'");
    if (tep_db_num_rows ($specials_query)) {
      $specials = tep_db_fetch_array($specials_query);
      $products_price = $specials['specials_new_products_price'];
    }
    if ($this->include_tax == true) {
      $total_price += ($products_price + tep_calculate_tax($products_price, $products_tax)) * $qty;
    } else {
      $total_price += $products_price * $qty;
    }

    // attributes price
    if (isset($cart->contents[$product_id]['attributes'])) {
      reset($cart->contents[$product_id]['attributes']);
      while (list($option, $value) = each($cart->contents[$product_id]['attributes'])) {
        $attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . $prid . "' and options_id = '" . $option . "' and options_values_id = '" . $value . "'");
        $attribute_price = tep_db_fetch_array($attribute_price_query);
        if ($attribute_price['price_prefix'] == '+') {
          if ($this->include_tax == true) {
            $total_price += $qty * ($attribute_price['options_values_price'] + tep_calculate_tax($attribute_price['options_values_price'], $products_tax));
          } else {
            $total_price += $qty * ($attribute_price['options_values_price']);
          }
        } else {
          if ($this->include_tax == true) {
            $total_price -= $qty * ($attribute_price['options_values_price'] + tep_calculate_tax($attribute_price['options_values_price'], $products_tax));
          } else {
            $total_price -= $qty * ($attribute_price['options_values_price']);
          }
        }
      }
    }
  }
  if ($this->include_shipping == true) {

    $total_price += $order->info['shipping_cost'];
  }
  return $total_price;
}

//Added by Fred -- BOF -----------------------------------------------------
//JUST RETURN THE PRODUCT PRICE (INCL ATTRIBUTE PRICES) WITH OR WITHOUT TAX
function product_price($product_id) {
  $total_price = $this->get_product_price($product_id);
  if ($this->include_shipping == true) $total_price -= $order->info['shipping_cost'];
  return $total_price;
}
//Added by Fred -- EOF -----------------------------------------------------

// START added by Rigadin in v5.13, needed to show module errors on checkout_payment page
    function get_error() {
      $error = array('title' => MODULE_ORDER_TOTAL_COUPON_TEXT_ERROR,
                     'error' => stripslashes(urldecode($_GET['error'])));

      return $error;
    }

function check() {
  if (!isset($this->check)) {
    $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_ORDER_TOTAL_COUPON_STATUS'");
    $this->check = tep_db_num_rows($check_query);
  }

  return $this->check;
}

function keys() {
  return array('MODULE_ORDER_TOTAL_COUPON_STATUS', 'MODULE_ORDER_TOTAL_COUPON_SORT_ORDER', 'MODULE_ORDER_TOTAL_COUPON_INC_SHIPPING', 'MODULE_ORDER_TOTAL_COUPON_INC_TAX', 'MODULE_ORDER_TOTAL_COUPON_CALC_TAX', 'MODULE_ORDER_TOTAL_COUPON_TAX_CLASS');
}

function install() {
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Display Total', 'MODULE_ORDER_TOTAL_COUPON_STATUS', 'true', 'Do you want to display the Discount Coupon value?', '6', '1','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_ORDER_TOTAL_COUPON_SORT_ORDER', '30', 'Sort order of display.', '6', '2', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Include Shipping', 'MODULE_ORDER_TOTAL_COUPON_INC_SHIPPING', 'true', 'Include Shipping in calculation', '6', '5', 'tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Include Tax', 'MODULE_ORDER_TOTAL_COUPON_INC_TAX', 'true', 'Include Tax in calculation.', '6', '6','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Re-calculate Tax', 'MODULE_ORDER_TOTAL_COUPON_CALC_TAX', 'None', 'Re-Calculate Tax', '6', '7','tep_cfg_select_option(array(\'None\', \'Standard\', \'Credit Note\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_ORDER_TOTAL_COUPON_TAX_CLASS', '0', 'Use the following tax class when treating Discount Coupon as Credit Note.', '6', '0', 'tep_get_tax_class_title', 'tep_cfg_pull_down_tax_classes(', now())");
}

function remove() {
  $keys = '';
  $keys_array = $this->keys();
  for ($i=0; $i<sizeof($keys_array); $i++) {
    $keys .= "'" . $keys_array[$i] . "',";
  }
  $keys = substr($keys, 0, -1);

  tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in (" . $keys . ")");
  }
}
?>

Save yerself some time and worry - load up a free CE 641a cart, run the coupon through that. Works fine on the 6.4.1a CE cart I checked using % - have a look at checkout_payment.php and the shopping_cart.php (logic and tpls both) ... something won't be adding up. And as an outside chance, if you have 48 pages of changes, any chance one of those mods in a seemingly unrelated area has stuffed up the coupons?

Simon

_________________
www.codemehappy.com
For Cre Loaded tips, how-to articles and more


Top
 Profile  
 
 Post subject: Re: Because of another posting RE: Coupons I found something....
PostPosted: Wed Jul 27, 2011 6:14 am 
Offline
CRE Talented

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 489
Location: CO
Well...... I went into the database. I found at least 100 coupon codes (including coupons we created a long time ago and deleted) while the coupon section in admin showed only 5. I deleted most coupons in the table in the database.

Now before I say "Holy Crap!" First WHY were all the coupon codes still in the database after they were deleted in admin, then WHY after clearing most out did my new coupon start working as it should?

I am going to spend the next few hours going through all my changes, all the logic (as best as I can follow it), and try to see what the heck is happening. My documentation shows I worked on the coupon section on 15 May 2011. I am going to find a back-up of the coupons from before 15 may and compare but my notes say I didn't make too big of changes but I did do a comparison and fix on some things (logic states I may have done more then I documented if the two files are identical).

IF this shows back up I'll revisit this posting but as of right now the coupon is working as it should 100% (including taking the discount from the reduced price of the items in the cart).
But I have to figure this out... I'm so irritated I'm spending my sleep time to work on this and I have about 3 hours before I need to be at work, so I'll use that time to go through all the files and logic and see if I can figure it out.

Thank you Simon for always being around for us. Honestly without you I think CRE would be in worse shape then it is today.

Mike


Top
 Profile  
 
 Post subject: Re: Because of another posting RE: Coupons I found something....
PostPosted: Wed Jul 27, 2011 6:27 am 
Offline
CRE Legend
User avatar

Joined: Thu Jun 12, 2008 6:39 am
Posts: 2404
Location: New Zealand
Didn't do much at all really, but it's always worth checking the database tables for corruptions that can affect the code. Optimizing tables never hurt either.

A lot of the dbase handling isn't that flash - for example delete a product and expect the all the other tables to cascade the delete through them. Oscommerce isn't that flash. Wouldn't stress too much about it - well done perservering ... generally the only way stuff gets debugged.

Simon

_________________
www.codemehappy.com
For Cre Loaded tips, how-to articles and more


Top
 Profile  
 
 Post subject: Re: Because of another posting RE: Coupons I found something....
PostPosted: Wed Jul 27, 2011 5:12 pm 
Offline
CRE Talented

Joined: Thu Dec 25, 2008 5:09 pm
Posts: 489
Location: CO
The thanks was for being willing to help no matter if you felt you helped or not.

Because of these odd issues I document the many changes I do but I am a hack at PHP and knowledgeable coders here are a god send.

Thank you,
Mike


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

Board index » CRE Loaded Support » CRE Loaded 6.3

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
It is currently Wed May 23, 2012 7:23 am
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

Login

Forums Latest Activity

Top Listing

1. Cart2Cart - Shopping...
    Category: Shopping Cart Database Conversion Scripts
    
2. Points & Rewards PLUS!...
    Category: Add-Ons
    
3. Configuration Server...
    Category: Fixes
    
4. Credit Card with CCV
    Category: Payment Modules
    
5. CC7333_ATS
    Category: Templates
    
Show more...

© CRE Loaded is a product of Chain Reaction Ecommerce, Inc. Usage & Privacy Policy