Thomas Nielsen
Thomas Nielsen

Reputation: 613

Magento system log errors that I cannot get rid off

I'm having two problems in my system.log that I'm having some problems resolving. The site seems to run fine, but I would very much like to remove these ongoing errors from the log.


Notice: Undefined variable: order in /var/www/... on line 17

// line 17
$merchantnumber = $standard->getConfigData('merchantnumber', $order ? $order->getStoreId() : null);

Warning: Division by zero in /var/www/... on line 267

// line 267
"vat" => (float)round((string)((round($order->getBaseShippingInclTax(),2)-round($order->getBaseShippingAmount(),2))/round((string)$order->getBaseShippingAmount(),2))*100, 2)

UPDATE

// line 258-281 
                $items = $order->getAllItems();
    foreach ($items as $itemId => $item)
    {
        $invoice["lines"][] = array
        (
            "id" => $item->getSku(),
            "description" => $item->getName(),
            "quantity" => round($item->getQtyOrdered(), 0),
            "price" => $item->getBasePrice()*100,
            "vat" => (float)round((string)((round($item->getBasePriceInclTax(),2)-round($item->getBasePrice(),2))/round((string)$item->getBasePrice(),2))*100, 2)
        );
    }

    $invoice["lines"][] = array
    (
        "id" => $order->getShippingMethod(),
        "description" => $order->getShippingDescription(),
        "quantity" => 1,
        "price" => $order->getBaseShippingAmount()*100,
        "vat" => (float)round((string)((round($order->getBaseShippingInclTax(),2)-round($order->getBaseShippingAmount(),2))/round((string)$order->getBaseShippingAmount(),2))*100, 2)
    );

    return json_encode($invoice);
}

I've posted the wrong code before sorry, I was confused when looking throug the errorlog because the same (Devider) error appeared on both the Item and Order part.

Upvotes: 0

Views: 1374

Answers (3)

LSerni
LSerni

Reputation: 57418

Unfortunately Magento code seems to often rely on PHP's tolerance for logic errors which are considered notices.

 $merchantnumber = $standard->getConfigData('merchantnumber', $order ?
   $order->getStoreId() : null);

If $order is not set we want to use NULL instead of the order's store Id:

 $merchantnumber = $standard->getConfigData('merchantnumber',
   isset($order) ? $order->getStoreId() : null);

In the second file:

UPDATE check out if you're referring to $order or $item. If it's in a loop, it is probably the latter.

"vat" => (float)round((string)((round($item->getBaseShippingInclTax(),2)-round($item->getBaseShippingAmount(),2))/round((string)$item->getBaseShippingAmount(),2))*100, 2)

the VAT% is back-calculated from ShippingInclTax (which is ShippingAmount + VAT). Only, if getBaseShippingAmount() is zero, the calculation crashes.

To cope with that we ought to have:

 'vat' => (0 == round($item->getBaseShippingAmount(),2)) ? 0 :
          (float)round((string)(
 (round($item->getBaseShippingInclTax(),2)
 -round($item->getBaseShippingAmount (),2)
 )/round((string)$item->getBaseShippingAmount(),2)
 )*100,2),

...I'm not really happy with all those rounds, but they probably are there so that printed accounts "check" up to the last decimal and avoid strange results such as 10.33 + 10.33 + 9.33 = 30.00 instead of 29.99.

I would write

 'vat' => (0 == $item->getBaseShippingAmount()) ? 0
        : round(
             100.0*(
               $item->getBaseShippingInclTax()/$item->getBaseShippingAmount()
               -1.0
             )
          ,2),

but even if it is mathematically more sound, I'm afraid that the results might not "match" with what Magento prints elsewhere.

If you're using $order in a loop (meaning you've got the same value whatever the item), you'd be better advised in calculating VAT before the loop and then use it inside.

Upvotes: 2

alexkorep
alexkorep

Reputation: 531

In the first case you can try changing that line to

$merchantnumber = $standard->getConfigData('merchantnumber', (isset($order) && $order) ? $order->getStoreId() : null);

In the second case I cannot tell exactly what value should go to "vat" in case when $order->getBaseShippingAmount() is 0. I suspect you need to do something like

if (!$order->getBaseShippingAmount()) {
   return;
}

to be added somewhere above line 267, but it's hard to tell for sure without seeing the code.

Upvotes: 2

Emil Vikström
Emil Vikström

Reputation: 91983

A notice is not an error. You can either choose to ignore it, or you can check if the order variable exists using isset:

 ...er', isset($order) ? $...

Division by zero should always be avoided because the answer is usually undefined (it is mathematically undefined at least). Check that the denominator is not zero before you try to do the division.

Upvotes: 1

Related Questions