Opencart - Reward points does not substract tax

872 views Asked by At

By default, Opencart reward points does not seem to work on products with tax. If I buy a product with points it substracts the product's price but without tax so it I am left with VAT to pay for that "free" product. In this way you cannot get free products with reward points and the invoice is wrongly generated.

I am trying to edit the model/total/reward.php to include the product's tax ! I found a snippet of the fix but it is for an old version and it doesn't seem to work with mine 1.5.5.1.

Can someone take a look on it please ?

Here is the snippet found

    <?php
class ModelTotalReward extends Model {
   public function getTotal(&$total_data, &$total, &$taxes) {
      if (isset($this->session->data['reward'])) {
         $this->language->load('total/reward');

         $points = $this->customer->getRewardPoints();

         if ($this->session->data['reward'] <= $points) {
            $discount_total = 0;

            $points_total = 0;

            foreach ($this->cart->getProducts() as $product) {
               if ($product['points']) {
                  $points_total += $product['points'];
               }
            }   

            $points = min($points, $points_total);

            foreach ($this->cart->getProducts() as $product) {
               $discount = 0;

               if ($product['points']) {
                  $discount = $product['total'] * ($this->session->data['reward'] / $points_total);

                  if ($product['tax_class_id']) {
                     $taxes[$product['tax_class_id']] -= ($product['total'] / 100 * $this->tax->getRates($product['tax_class_id'])) - (($product['total'] - $discount) / 100 * $this->tax->getRates($product['tax_class_id']));
                  }
               }

               $discount_total += $discount;
            }

            $total_data[] = array(
               'code'       => 'reward',
                 'title'      => sprintf($this->language->get('text_reward'), $this->session->data['reward']),
                'text'       => $this->currency->format(-$discount_total),
                 'value'      => -$discount_total,
               'sort_order' => $this->config->get('reward_sort_order')
               );

            $total -= $discount_total;
         }
      }
   }

   public function confirm($order_info, $order_total) {
      $this->language->load('total/reward');

      $points = 0;

      $start = strpos($order_total['title'], '(') + 1;
      $end = strrpos($order_total['title'], ')');

      if ($start && $end) { 
         $points = substr($order_total['title'], $start, $end - $start);
      }   

      if ($points) {
         $this->db->query("INSERT INTO " . DB_PREFIX . "customer_reward SET customer_id = '" . (int)$order_info['customer_id'] . "', order_id = '" . (int)$order_info['order_id'] . "', description = '" . $this->db->escape(sprintf($this->language->get('text_order_id'), (int)$order_info['order_id'])) . "', points = '" . (float)-$points . "', date_added = NOW()");            
      }
   }      
}
?>

and this is the original model/total/reward.php

    <?php
class ModelTotalReward extends Model {
   public function getTotal(&$total_data, &$total, &$taxes) {
      if (isset($this->session->data['reward'])) {
         $this->language->load('total/reward');

         $points = $this->customer->getRewardPoints();

         if ($this->session->data['reward'] <= $points) {
            $discount_total = 0;

            $points_total = 0;

            foreach ($this->cart->getProducts() as $product) {
               if ($product['points']) {
                  $points_total += $product['points'];
               }
            }   

            $points = min($points, $points_total);

            foreach ($this->cart->getProducts() as $product) {
               $discount = 0;

               if ($product['points']) {
                  $discount = $product['total'] * ($this->session->data['reward'] / $points_total);

                  if ($product['tax_class_id']) {
                     $tax_rates = $this->tax->getRates($product['total'] - ($product['total'] - $discount), $product['tax_class_id']);

                     foreach ($tax_rates as $tax_rate) {
                        if ($tax_rate['type'] == 'P') {
                           $taxes[$tax_rate['tax_rate_id']] -= $tax_rate['amount'];
                        }
                     }   
                  }
               }

               $discount_total += $discount;
            }

            $total_data[] = array(
               'code'       => 'reward',
                 'title'      => sprintf($this->language->get('text_reward'), $this->session->data['reward']),
                'text'       => $this->currency->format(-$discount_total),
                 'value'      => -$discount_total,
               'sort_order' => $this->config->get('reward_sort_order')
               );

            $total -= $discount_total;
         }
      }
   }

   public function confirm($order_info, $order_total) {
      $this->language->load('total/reward');

      $points = 0;

      $start = strpos($order_total['title'], '(') + 1;
      $end = strrpos($order_total['title'], ')');

      if ($start && $end) { 
         $points = substr($order_total['title'], $start, $end - $start);
      }   

      if ($points) {
         $this->db->query("INSERT INTO " . DB_PREFIX . "customer_reward SET customer_id = '" . (int)$order_info['customer_id'] . "', description = '" . $this->db->escape(sprintf($this->language->get('text_order_id'), (int)$order_info['order_id'])) . "', points = '" . (float)-$points . "', date_added = NOW()");            
      }
   }      
}
?>

Thank You !

1

There are 1 answers

0
Stoyan Berov On BEST ANSWER

I had the same difficulty, just fixed it with some code I found on the OpenCart forum. In model/total/reward.php:

      foreach ($this->cart->getProducts() as $product) {
           $discount = 0;

      if ($product['points']) {
              $discount = $product['total'] * ($this->session->data['reward'] / $points_total);

        /* TAX HACK */
        $tr = $this->tax->getRates($product['total'] - ($product['total'] - $discount), $product['tax_class_id']);
        foreach ($tr as $t) {
          $discount += $t['amount'];
        }
        /* TAX HACK */

              if ($product['tax_class_id']) {
                 $tax_rates = $this->tax->getRates($product['total'] - ($product['total'] - $discount), $product['tax_class_id']);

                 foreach ($tax_rates as $tax_rate) {
                    if ($tax_rate['type'] == 'P') {
                       $taxes[$tax_rate['tax_rate_id']] -= $tax_rate['amount'];
                    }
                 }   
              }
           }

           $discount_total += $discount;
        }

Let me know if that worked for you, too. Cheers!