Zen Cart Logo
Forums / Addon Admin Tools / Edit Orders v4.0 Support Thread

Edit Orders v4.0 Support Thread

Views: 346,083

Results 1,661 to 1,680 of 1,927
11 May 2021, 3:17 PM
#1661
spanky avatar

spanky

New Zenner

Join Date:
May 2021
Location:
New York
Posts:
16
Plugin Contributions:
0

Edit Orders v4.0 Support Thread

Here I am going to outline everything that is going on with the math calculated for orders on our site. Ever since zencart was updated to v1.5.7, there have been serious issues with how totals are being calculated. Our site uses the Edit Orders plugin, which has also been updated to the latest version.
First, allow me to explain how the math is supposed to work:
Obviously, each item in the cart has a cost per item, and the quantity of that item times its cost, gives you the total for that item. Ie. itemA cost 5.00, and the customer has 5x of them in the cart, therefor the total for that item is 5x5 = $25.00. As of now this seems to be working fine.
The Subtotal is then calculated by adding up all of the item totals in the cart. Again, this is working fine. Once subtotal is calculated, shipping gets calculated. I know that shipping is calculated differently for international, vs domestic. For all intents and purposes, this is working fine.
Finally, we have a processing fee, which is supposed to be 3.5% x (subtotal + shipping). For the sake of clarity, I will say that at the moment this 3.5% is only being applied to the subtotal. However, we have someone who is working on this, and it isn’t a massive deal at this moment. Going forward, I am going to present examples as if this is working correct, for the sake of showing how sales tax, and total are calculated for unedited orders.
The calculation for sales tax is also pretty straight forward: (subtotal + shipping + fee) x 8.125% = SalesTax. For a default order without any edits, this appears to be working fine.
Finally, as you may have guessed: Total = Subtotal + Shipping + Fee + SalesTax. Simple.
If a customer is to use a coupon, the math should be: Total = (subtotal – discount) + shipping + Fee + Sales Tax
I will demonstrate that for a default, unedited order, this is working fine (minus the fee issue I mentioned earlier). However, once I try to edit any aspect of an order, or apply a coupon, things become very hairy.

Default Unedited Order:
Attachment 19558
Lets break down the math in the above screenshot. I will use this order going forward to show the effects of coupons and edits.
We have two items, TestItemA which costs $5.00 and TestItemB which costs $25.00. The order contains 5 each.
TotalA = 5 x $5.00 = $25.00
TotalB = 5 x $25.00 = $125.00
Subtotal = TotalA + TotalB:
$25.00 + $125.00 = $150.00
Shipping is then calculated for US & Canada (domestic), and we get $28.50
Next, the Processing fee is calculated. In this case:
3.5% x 150.00 = $5.25
Next, Tax gets calculated:
Tax = (Subtotal + Shipping + Fee) x 8.125% -
(150 + 28.5 + 5.25) * 8.125% = $14.93
Finally, lets calculate the total:
Total = Subtotal + Shipping + Fee + SalesTax
150 + 28.5 + 5.25 + 14.93 = 198.68
So to summarize, although the fee is being applied incorrectly (as previously mentioned) The subtotal, tax, shipping, and total are all being calculated correctly for an unedited order.

Now, lets make a simple change to this using the Edit Order plugin:
Attachment 19559
Attachment 19560
In this case, I have simply reduced the shipping from $28.50, to $10.00. Lets now take a look at how the math adds up.
So at first glance, our subtotal appears to be fine:
125 + 20 = 150
Since as of now the fee is only being applied to the subtotal, it is unchanged by this.
Lets move on to Tax. Oddly, you can see that after editing the order, tax gets broken down into NY Sales tax, and Shipping Tax. However, if you add them together, they should add up fine.
First, lets just calculate what the total tax should be:
Tax = (Subtotal + Shipping + Fee) x 8.125%
(150 + 10 + 5.25) x 8.125% = $13.43
So just to verify, lets add the “shipping tax” and “sales tax” together:
12.62 + 0.81 = $13.43
So evidently, tax is still being calculated correctly here. So last but not least, we add it all together to get the total:
Total = Subtotal + Shipping + Fee + SalesTax
150 + 10.00 + 13.43 = ……..$173.43
However, in the above screenshot, the total we are getting from zencart is $178.25
Lets get the difference for the sake of trying to debug this down the road. We’ll call this the “Error Difference”
Error Difference = (Total On Zencart) – (Total We Calculated ourselves):
178.25 – 173.43 = $4.28
So somehow, even after doing all the other math correct, when it comes time to add everything up, we’re getting an unexplained error difference of $4.28.
For redundancy, and to see how the “Error Difference” changes, I’m going to edit the shipping for this same order again. This time ill raise it to 25.00
Attachment 19561
For the sake of keeping this simple, I will simply tell you that I calculated sales tax and everything up until Total is being calculated correctly. So lets try to get the total:
Total = Subtotal + Shipping + Fee + SalesTax
150 + 25 + 5.25 + 14.65 = 194.90
However, as you can see, in zencart our total is 194.47
In this case the error difference is much smaller:
194.90 – 194.47 = $0.43
This is what makes this so confusing. It would be one thing if I could say “oh its not taking sales tax into account, or its messing up the subtotal” but in this case, its literally just doing the addition and spitting out an incorrect number seemingly at random. Its like its saying 2 + 2 = 3.43 and 2 + 1 = 6. I'm hoping someone here can help as this has been a serious issue ever since the latest update of zencart.

11 May 2021, 3:32 PM
#1662
spanky avatar

spanky

New Zenner

Join Date:
May 2021
Location:
New York
Posts:
16
Plugin Contributions:
0

Re: Edit Orders v4.0 Support Thread

Im just noticing that diptimoy posted as well, I'd like to clarify that he works with us and this is the same issue he has posted.

11 May 2021, 3:34 PM
#1663
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,069
Plugin Contributions:
56

Re: Edit Orders v4.0 Support Thread

@Spanky, just like my response to @diptimoy, where did the code for that Handling Fee come from? I'm guessing that it's got some serious play in the miscalculations.

Also, are you using manual- or automatic-calculations of pricing?

11 May 2021, 3:40 PM
#1664
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,955
Plugin Contributions:
8

Re: Edit Orders v4.0 Support Thread

why don't you post the code for the Processing and Handling Fee module? and specifically the process method.

the problem is there.

has nothing to do with edit orders, although everyone likes to blame edit orders (me included!!)

11 May 2021, 3:53 PM
#1665
spanky avatar

spanky

New Zenner

Join Date:
May 2021
Location:
New York
Posts:
16
Plugin Contributions:
0

Re: Edit Orders v4.0 Support Thread

Here is the order total module that handles the fee: Attachment 19562

My knowledge of zencart is pretty limited, so please advise if you can what to do from here

Also, I can't see my last response to you for some reason so I don't know if I said this, but I have tried both manual and automatic pricing. But it is suppose to be set to automatic.

11 May 2021, 3:54 PM
#1666
diptimoy avatar

diptimoy

Zen Follower

Join Date:
Apr 2012
Location:
Bardhaman , Bharat
Posts:
217
Plugin Contributions:
1

Re: Edit Orders v4.0 Support Thread

carlwhat:

why don't you post the code for the Processing and Handling Fee module? and specifically the process method.

the problem is there.

has nothing to do with edit orders, although everyone likes to blame edit orders (me included!!)

Herehow to processing configuration looks

Attachment 19563

11 May 2021, 4:05 PM
#1667
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,069
Plugin Contributions:
56

Re: Edit Orders v4.0 Support Thread

... and as @carlwhat requested, how about posting the code associated with that order-total's process method?

11 May 2021, 4:22 PM
#1668
spanky avatar

spanky

New Zenner

Join Date:
May 2021
Location:
New York
Posts:
16
Plugin Contributions:
0

Re: Edit Orders v4.0 Support Thread

trying to find it

11 May 2021, 4:51 PM
#1669
spanky avatar

spanky

New Zenner

Join Date:
May 2021
Location:
New York
Posts:
16
Plugin Contributions:
0

Re: Edit Orders v4.0 Support Thread

Its way to many characters to post here, should I put it in a google doc?

11 May 2021, 6:08 PM
#1671
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,955
Plugin Contributions:
8

Re: Edit Orders v4.0 Support Thread

Spanky:

https://drive.google.com/file/d/1b2EJBqK6LmJ_WeglKxBeoWtejLB3jrv6/view?usp=sharing

^There is the code

not the right code....

looking for something around here:

includes/modules/order_total/ot_processing_and_handling.php

the name is probably different. but that's the file we need to see...

11 May 2021, 6:11 PM
#1672
spanky avatar

spanky

New Zenner

Join Date:
May 2021
Location:
New York
Posts:
16
Plugin Contributions:
0

Re: Edit Orders v4.0 Support Thread

One moment

11 May 2021, 6:47 PM
#1674
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,955
Plugin Contributions:
8

Re: Edit Orders v4.0 Support Thread

my guess is extraorderfee

12 May 2021, 1:10 PM
#1675
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,069
Plugin Contributions:
56

Re: Edit Orders v4.0 Support Thread

carlwhat:

my guess is extraorderfee
Actually, it turns out that any taxable order-total exhibits this issue. See https://github.com/lat9/edit_orders/issues/198 for the upcoming changes for EO v4.6.0.

Any site experiencing this issue can correct via edit to /YOUR_ADMIN/includes/functions/extra_functions/edit_order_functions.php, removing (or commenting out) the highlighted lines below:

function eo_update_database_order_totals($oID) 
{
    global $db, $order, $eo;

    // Load required modules for order totals if enabled
    if (defined('MODULE_ORDER_TOTAL_INSTALLED') && !empty(MODULE_ORDER_TOTAL_INSTALLED)) {
        $eo->eoLog(PHP_EOL . 'eo_update_database_order_totals, taxes/totals on entry. ' . $eo->eoFormatTaxInfoForLog(true), 'tax');
        
        $eo->tax_updated = false;
        
        $order->info['shipping_tax'] = 0;

        // Load order totals.
        require_once DIR_FS_CATALOG . DIR_WS_CLASSES . 'order_total.php';
        $GLOBALS['order_total_modules'] = new order_total();

        // Load the shopping cart class into the session
        eo_shopping_cart();
[B]        
        // -----
        // Cycle through the order-totals to see if any are currently taxed.  If so, remove the
        // tax from the current order in preparation for its recalculation.
        //
        foreach ($order->totals as $current_total) {
            if (in_array($current_total['class'], ['ot_subtotal', 'ot_tax', 'ot_shipping', 'ot_total', 'ot_misc_cost'])) {
                continue;
            }
            $current_total_tax = $eo->eoGetOrderTotalTax($oID, $current_total['class']);
            $order->info['tax'] -= $current_total_tax;
        }
[/B]
        // Reset the final total (include tax if not already tax-inc)
        // This code causes the order totals to be correctly calculated.
        if (DISPLAY_PRICE_WITH_TAX == 'true') {
            $order->info['total'] = $order->info['subtotal'] + $order->info['shipping_cost'];
        } else {
            $order->info['total'] = $order->info['subtotal'] + $order->info['tax'] + $order->info['shipping_cost'];
        }
12 May 2021, 2:28 PM
#1676
diptimoy avatar

diptimoy

Zen Follower

Join Date:
Apr 2012
Location:
Bardhaman , Bharat
Posts:
217
Plugin Contributions:
1

Re: Edit Orders v4.0 Support Thread

diptimoy:

Herehow to processing configuration looks

Attachment 19563

Spanky:

https://drive.google.com/file/d/1AdQkq5Q2PXv877J3SV8nhzuJzY8S16n7/view?usp=sharing

Any of these? ^

  function process() {
      global $order, $currencies;

      if (MODULE_ORDER_TOTAL_EXTRAORDERFEE_ORDER_FEE == 'true') {

      $pass = true;
      if (MODULE_ORDER_TOTAL_EXTRAORDERFEE_ZONE > 0) {
// bof: check zone and add extra fee
        global $db;
        $pass = false;
        $check_flag = false;
// based on Delivery zone $order->delivery to use Payment zone change to $order->billing in two places
        $check = $db->Execute("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_ORDER_TOTAL_EXTRAORDERFEE_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
        while (!$check->EOF) {
          if ($check->fields['zone_id'] < 1) {
            $check_flag = true;
            break;
          } elseif ($check->fields['zone_id'] == $order->delivery['zone_id']) {
            $check_flag = true;
            break;
          }
          $check->MoveNext();
        }

        if ($check_flag == false) {
          $pass = false;
        } else {
          $pass = true;
        }
// eof: check zone and add extra fee
      }

        if ($pass == true) {
//echo 'TEST EXTRA CHARGE PASS!';
            $tax_address = zen_get_tax_locations();
            $tax = zen_get_tax_rate(MODULE_ORDER_TOTAL_EXTRAORDERFEE_TAX_CLASS, $tax_address['country_id'], $tax_address['zone_id']);
            $tax_description = zen_get_tax_description(MODULE_ORDER_TOTAL_EXTRAORDERFEE_TAX_CLASS, $tax_address['country_id'], $tax_address['zone_id']);

// calculate from flat fee or percentage
            if (substr(MODULE_ORDER_TOTAL_EXTRAORDERFEE_FEE, -1) == '%') {
              //$extra_order_fee = (($order->info['subtotal']+$order->info['shipping_cost'])* (MODULE_ORDER_TOTAL_EXTRAORDERFEE_FEE/100));
              $extra_order_fee =  ($order->info['subtotal'] * (MODULE_ORDER_TOTAL_EXTRAORDERFEE_FEE/100));
      
            } else {
              $extra_order_fee = MODULE_ORDER_TOTAL_EXTRAORDERFEE_FEE;
            }

            $order->info['tax'] += zen_calculate_tax($extra_order_fee, $tax);
            $order->info['tax_groups']["$tax_description"] += zen_calculate_tax($extra_order_fee, $tax);
            $order->info['total'] += $extra_order_fee + zen_calculate_tax($extra_order_fee, $tax);
            if (DISPLAY_PRICE_WITH_TAX == 'true') {
              $extra_order_fee += zen_calculate_tax($extra_order_fee, $tax);
            }

            $this->output[] = array('title' => $this->title . ':',
                                    'text' => $currencies->format($extra_order_fee, true, $order->info['currency'], $order->info['currency_value']),
                                    'value' => $extra_order_fee);
        } else {
//echo 'TEST EXTRA CHARGE NO PASS!';
        }
      }
    }
12 May 2021, 2:28 PM
#1677
spanky avatar

spanky

New Zenner

Join Date:
May 2021
Location:
New York
Posts:
16
Plugin Contributions:
0

Re: Edit Orders v4.0 Support Thread

I actually don't have that file in that directory. Might it be named something else, or go somewhere else?

12 May 2021, 2:29 PM
#1678
diptimoy avatar

diptimoy

Zen Follower

Join Date:
Apr 2012
Location:
Bardhaman , Bharat
Posts:
217
Plugin Contributions:
1

Re: Edit Orders v4.0 Support Thread

@spanky

The file is

ot_extraorderfee.php

<?php
/**
 * ot_total order-total module
 *
 * @package orderTotal
 * @copyright Copyright 2003-2012 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license [URL]http://www.zen-cart.com/license/2_0.txt[/URL] GNU Public License V2.0
 * @version $Id: ot_extraorderfee.php 6101 2012-10-19 10:30:22Z ajeh $
 */
  class ot_extraorderfee {
    var $title, $output;

    function ot_extraorderfee() {
      $this->code = 'ot_extraorderfee';
      $this->title = MODULE_ORDER_TOTAL_EXTRAORDERFEE_TITLE;
      $this->description = MODULE_ORDER_TOTAL_EXTRAORDERFEE_DESCRIPTION;
      $this->sort_order = MODULE_ORDER_TOTAL_EXTRAORDERFEE_SORT_ORDER;

      $this->output = array();
    }

    function process() {
      global $order, $currencies;

      if (MODULE_ORDER_TOTAL_EXTRAORDERFEE_ORDER_FEE == 'true') {

      $pass = true;
      if (MODULE_ORDER_TOTAL_EXTRAORDERFEE_ZONE > 0) {
// bof: check zone and add extra fee
        global $db;
        $pass = false;
        $check_flag = false;
// based on Delivery zone $order->delivery to use Payment zone change to $order->billing in two places
        $check = $db->Execute("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_ORDER_TOTAL_EXTRAORDERFEE_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
        while (!$check->EOF) {
          if ($check->fields['zone_id'] < 1) {
            $check_flag = true;
            break;
          } elseif ($check->fields['zone_id'] == $order->delivery['zone_id']) {
            $check_flag = true;
            break;
          }
          $check->MoveNext();
        }

        if ($check_flag == false) {
          $pass = false;
        } else {
          $pass = true;
        }
// eof: check zone and add extra fee
      }

        if ($pass == true) {
//echo 'TEST EXTRA CHARGE PASS!';
            $tax_address = zen_get_tax_locations();
            $tax = zen_get_tax_rate(MODULE_ORDER_TOTAL_EXTRAORDERFEE_TAX_CLASS, $tax_address['country_id'], $tax_address['zone_id']);
            $tax_description = zen_get_tax_description(MODULE_ORDER_TOTAL_EXTRAORDERFEE_TAX_CLASS, $tax_address['country_id'], $tax_address['zone_id']);

// calculate from flat fee or percentage
            if (substr(MODULE_ORDER_TOTAL_EXTRAORDERFEE_FEE, -1) == '%') {
              //$extra_order_fee = (($order->info['subtotal']+$order->info['shipping_cost'])* (MODULE_ORDER_TOTAL_EXTRAORDERFEE_FEE/100));
              $extra_order_fee =  ($order->info['subtotal'] * (MODULE_ORDER_TOTAL_EXTRAORDERFEE_FEE/100));
      
            } else {
              $extra_order_fee = MODULE_ORDER_TOTAL_EXTRAORDERFEE_FEE;
            }

            $order->info['tax'] += zen_calculate_tax($extra_order_fee, $tax);
            $order->info['tax_groups']["$tax_description"] += zen_calculate_tax($extra_order_fee, $tax);
            $order->info['total'] += $extra_order_fee + zen_calculate_tax($extra_order_fee, $tax);
            if (DISPLAY_PRICE_WITH_TAX == 'true') {
              $extra_order_fee += zen_calculate_tax($extra_order_fee, $tax);
            }

            $this->output[] = array('title' => $this->title . ':',
                                    'text' => $currencies->format($extra_order_fee, true, $order->info['currency'], $order->info['currency_value']),
                                    'value' => $extra_order_fee);
        } else {
//echo 'TEST EXTRA CHARGE NO PASS!';
        }
      }
    }

    function check() {
      global $db;
      if (!isset($this->_check)) {
        $check_query = "select configuration_value
                        from " . TABLE_CONFIGURATION . "
                        where configuration_key = 'MODULE_ORDER_TOTAL_EXTRAORDERFEE_STATUS'";

        $check_query = $db->Execute($check_query);
        $this->_check = $check_query->RecordCount();
      }

      return $this->_check;
    }

    function keys() {
      return array('MODULE_ORDER_TOTAL_EXTRAORDERFEE_STATUS', 'MODULE_ORDER_TOTAL_EXTRAORDERFEE_SORT_ORDER', 'MODULE_ORDER_TOTAL_EXTRAORDERFEE_ORDER_FEE', 'MODULE_ORDER_TOTAL_EXTRAORDERFEE_FEE', 'MODULE_ORDER_TOTAL_EXTRAORDERFEE_TAX_CLASS', 'MODULE_ORDER_TOTAL_EXTRAORDERFEE_ZONE');
    }

    function install() {
      global $db;
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('This module is installed', 'MODULE_ORDER_TOTAL_EXTRAORDERFEE_STATUS', 'true', '', '6', '1','zen_cfg_select_option(array(\'true\'), ', now())");
      $db->Execute("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_EXTRAORDERFEE_SORT_ORDER', '450', 'Sort order of display.', '6', '2', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Allow Extra Order Fee', 'MODULE_ORDER_TOTAL_EXTRAORDERFEE_ORDER_FEE', 'false', 'Do you want to allow Extra order fees?', '6', '3', 'zen_cfg_select_option(array(\'true\', \'false\'), ', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, date_added) values ('Extra Order Fee', 'MODULE_ORDER_TOTAL_EXTRAORDERFEE_FEE', '5.00', 'For Percentage Calculation - include a % Example: 10%<br />For a flat amount just enter the amount - Example: 5 for $5.00', '6', '5', '', now())");
      $db->Execute("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_EXTRAORDERFEE_TAX_CLASS', '0', 'Use the following tax class on the Extra order fee.', '6', '7', 'zen_get_tax_class_title', 'zen_cfg_pull_down_tax_classes(', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Extra Charge Zone', 'MODULE_ORDER_TOTAL_EXTRAORDERFEE_ZONE', '0', 'If a zone is selected, only enable this Extra charge for that zone.', '6', '0', 'zen_get_zone_class_title', 'zen_cfg_pull_down_zone_classes(', now())");
    }

    function remove() {
      global $db;
      $db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key like 'MODULE\_ORDER\_TOTAL\_EXTRAORDERFEE\_%'");

    }
  }
12 May 2021, 2:33 PM
#1679
spanky avatar

spanky

New Zenner

Join Date:
May 2021
Location:
New York
Posts:
16
Plugin Contributions:
0

Re: Edit Orders v4.0 Support Thread

The file that he's saying to edit is what I mean

12 May 2021, 2:41 PM
#1680
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,069
Plugin Contributions:
56

Re: Edit Orders v4.0 Support Thread

@Spanky and @diptimoy, please refer to my response in post #1672.