invoice: add tax rate in every product line

  • Posts: 86
  • Thank you received: 2
11 years 2 weeks ago #131974

-- HikaShop version -- : 2.2.2
-- Joomla version -- : 2.5.14

Hello I modified the invoice.php in order to display the tax rate. It is working fine when a product is added by the customer, but when I modify an order (add aproduct via backend, selecting one of the existing products/variants) the invoice and shipping invoice show following error message

Error message:

Notice: Undefined property: stdClass::$tax_rate in /homez.428/monpetit/www/administrator/templates/bluestork/html/com_hikashop/order/invoice.php on line 146

A screenshot with the error.


I observed a difference in the database in hikashop_order_product table in order_product_tax_info column when a product / varian is added in frontend or in backen.
When added in Frontend:
a:1:{i:0;O:8:"stdClass":12:{s:11:"taxation_id";s:1...
When added in Backend
a:1:{i:0;O:8:"stdClass":2:{s:11:"tax_namekey";s:7:...
Why the difference? Can I avoid the the difference in the code or Is this normal?

The code I added to the invoice.php
// tax calculation start -
    $tax = " ";
    if (is_array($product->order_product_tax_info)) {
      $tax_rate = $product->order_product_tax_info[0]->tax_rate * 100;
      $tax_amount = $product->order_product_tax_info[0]->tax_amount * $product->order_product_quantity;
      $product_amount = $product->order_product_price * $product->order_product_quantity;

      $tax_total += $tax_amount;
      if ($tax_rate>0) {
        if ($tax_rate == (int)$tax_rate) $tax = sprintf("%d",$tax_rate);
        else $tax = sprintf("%0.2f",$tax_rate);
        $taxes_array[$tax] = @$taxes_array[$tax] + $tax_amount;
        $products_array[$tax] = @$products_array[$tax] + $product_amount + $tax_amount;
        $tax.=" %";
      }
    } // tax

Line 146:
$tax_rate = $product->order_product_tax_info[0]->tax_rate * 100;

Please coul you help me fixing this problem?

Last edit: 11 years 2 weeks ago by scharfet.

Please Log in or Create an account to join the conversation.

  • Posts: 13201
  • Thank you received: 2322
11 years 2 weeks ago #132190

Hi,

When you are adding this product from backend, do you select the correct tax percentage in the dropdown on the right of the field of the option VAT ? That should be added to the order.

Please Log in or Create an account to join the conversation.

  • Posts: 86
  • Thank you received: 2
11 years 2 weeks ago #132241

Hi Xavier,

yes, I added the correct tax percentage from the dropdown list.

I see in the database that the column order_product_tax_info a different code when product is added in backend.
I thik the error occurs because the code added in my invoice.php can not read the tax rate when the tax rate was adden manually from dropdown list.

// tax calculation start -
    $tax = " ";
    if (is_array($product->order_product_tax_info)) {
      $tax_rate = $product->order_product_tax_info[0]->tax_rate * 100;
      $tax_amount = $product->order_product_tax_info[0]->tax_amount * $product->order_product_quantity;
      $product_amount = $product->order_product_price * $product->order_product_quantity;

      $tax_total += $tax_amount;
      if ($tax_rate>0) {
        if ($tax_rate == (int)$tax_rate) $tax = sprintf("%d",$tax_rate);
        else $tax = sprintf("%0.2f",$tax_rate);
        $taxes_array[$tax] = @$taxes_array[$tax] + $tax_amount;
        $products_array[$tax] = @$products_array[$tax] + $product_amount + $tax_amount;
        $tax.=" %";
      }
    } // tax

I hope I do explain myself.

Last edit: 11 years 2 weeks ago by scharfet.

Please Log in or Create an account to join the conversation.

  • Posts: 13201
  • Thank you received: 2322
11 years 2 weeks ago #132363

Hi,

I just tried on my local and yes indeed, there is some missing data in the tax infos.
But as temporary fix, you could do the following thing to get the tax percentage:

$str = $product->order_product_price;
$length = strlen(substr(strrchr($str, "."), 1)) - 1;
$nb = pow(10,$length);
$tax = $product->order_product_tax * $nb / $product->order_product_price *100.'%';

Please Log in or Create an account to join the conversation.

  • Posts: 86
  • Thank you received: 2
11 years 2 weeks ago #132370

Hi Xavier,

I do not know how to implement your code you offered in your last post into the code I have in my invoice.php.
Please could you help me?

Thank you!

PS: Did I understand you right, that the error is caused by an missing code in Hikashop and in the next version of Hikashop it will be fixed? When will the next version come out, in some weeks or some months?

<?php
              $k=0;
              $group = $this->config->get('group_options',0);
        $products_array = $taxes_array = array(); $tax_total = 0; //added
              foreach($this->order->products as $product){
                if($group && $product->order_product_option_parent_id) continue;
    // tax calculation START MODIFIED CODE  =========================================
    $tax = "&nbsp;";
    if (is_array($product->order_product_tax_info)) {
      $tax_rate = $product->order_product_tax_info[0]->tax_rate * 100;
      $tax_amount = $product->order_product_tax_info[0]->tax_amount * $product->order_product_quantity;
      $product_amount = $product->order_product_price * $product->order_product_quantity;

      $tax_total += $tax_amount;
      if ($tax_rate>0) {
        if ($tax_rate == (int)$tax_rate) $tax = sprintf("%d",$tax_rate);
        else $tax = sprintf("%0.2f",$tax_rate);
        $taxes_array[$tax] = @$taxes_array[$tax] + $tax_amount;
        $products_array[$tax] = @$products_array[$tax] + $product_amount + $tax_amount;
        $tax.=" %";
      }
    } // tax END MODIFIED CODE  =========================================
?>

Last edit: 11 years 2 weeks ago by scharfet.

Please Log in or Create an account to join the conversation.

  • Posts: 13201
  • Thank you received: 2322
11 years 2 weeks ago #132445

The code given should be enough to replace yours to display the tax percentage I think.

As I can see in $tax you are just storing the tax percentage, and that's what my code is doing too.

Please Log in or Create an account to join the conversation.

  • Posts: 86
  • Thank you received: 2
11 years 2 weeks ago #132463

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /homez.428/monpetit/www/administrator/templates/bluestork/html/com_hikashop/order/invoice.php on line 150


Code in line 150:
$tax = $product->order_product_tax * $nb / $product->order_product_price *100.'%';

When I subsitute the following code with your code I get the error message which you can see above:
<?php
              $k=0;
              $group = $this->config->get('group_options',0);
        $products_array = $taxes_array = array(); $tax_total = 0; //added
              foreach($this->order->products as $product){
                if($group && $product->order_product_option_parent_id) continue;
    // tax calculation START MODIFIED CODE  =========================================
    $tax = "&nbsp;";
    if (is_array($product->order_product_tax_info)) {
      $tax_rate = $product->order_product_tax_info[0]->tax_rate * 100;
      $tax_amount = $product->order_product_tax_info[0]->tax_amount * $product->order_product_quantity;
      $product_amount = $product->order_product_price * $product->order_product_quantity;

      $tax_total += $tax_amount;
      if ($tax_rate>0) {
        if ($tax_rate == (int)$tax_rate) $tax = sprintf("%d",$tax_rate);
        else $tax = sprintf("%0.2f",$tax_rate);
        $taxes_array[$tax] = @$taxes_array[$tax] + $tax_amount;
        $products_array[$tax] = @$products_array[$tax] + $product_amount + $tax_amount;
        $tax.=" %";
      }
    } // tax END MODIFIED CODE  =========================================
?>


Error ocurrs wen I delete the above code and replace it with the follwoing code:
<?php
$str = $product->order_product_price;
$length = strlen(substr(strrchr($str, "."), 1)) - 1;
$nb = pow(10,$length);
$tax = $product->order_product_tax * $nb / $product->order_product_price *100.'%'; ?>


Each product line has this code:
<?php echo $this->currencyHelper->format($product->order_product_price+$product->order_product_tax,$this->order->order_currency_id); ?>

Thank you for your help.

Last edit: 11 years 2 weeks ago by scharfet.

Please Log in or Create an account to join the conversation.

  • Posts: 13201
  • Thank you received: 2322
11 years 1 week ago #132539

And what about that code:

<?php
$str = $product->order_product_price;
$length = strlen(substr(strrchr($str, "."), 1)) - 1;
$nb = pow(10,$length);
$tax = $product->order_product_tax * $nb / $product->order_product_price * 100; 
$tax .= '%';
?>

Please Log in or Create an account to join the conversation.

Time to create page: 0.080 seconds
Powered by Kunena Forum