<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Exchange Server">
<!-- converted from rtf -->
<style><!-- .EmailQuote { margin-left: 1pt; padding-left: 4pt; border-left: #800000 2px solid; } --></style>
</head>
<body>
<font face="Calibri, sans-serif" size="2">
<div>Bug: Changing discount type on an invoice does not remove the discount</div>
<div> </div>
<div>Steps to reproduce</div>
<div> </div>
<div>Create an invoice and add a line item</div>
<div>Save the invoice</div>
<div>Edit the invoice</div>
<div>Create a discount for the invoice of 50% (at the invoice level not the line item level)</div>
<div>Save the invoice</div>
<div>Open the invoice</div>
<div>Change the discount to ‘Zero Discount’ </div>
<div>Invoice displays correct amount</div>
<div>Save the invoice</div>
<div> </div>
<div>The invoice now has the percentage reduction still enforced and the discount amount is wrong</div>
<div> </div>
<div>Solution</div>
<div>Line 670 in "include\utils\InventoryUtils.php"</div>
<div> </div>
<div>Change</div>
<div> </div>
<div>if($_REQUEST['discount_type_final'] == 'percentage')</div>
<div>        {</div>
<div>                $updatequery .= " discount_percent=?,";</div>
<div>                array_push($updateparams, $_REQUEST['discount_percentage_final']);</div>
<div>        }</div>
<div>        elseif($_REQUEST['discount_type_final'] == 'amount')</div>
<div>        {</div>
<div>                $discount_amount_final = $_REQUEST['discount_amount_final'];</div>
<div>                $updatequery .= " discount_amount=?,";</div>
<div>                array_push($updateparams, $discount_amount_final);</div>
<div>        }</div>
<div> </div>
<div>To this </div>
<div> </div>
<div>    switch ((isset($_REQUEST['discount_type_final']) ? $_REQUEST['discount_type_final'] : false)) {</div>
<div>        case 'percentage' :</div>
<div>            $updatequery .= " discount_percent=?,";</div>
<div>            array_push($updateparams, (float) $_REQUEST['discount_percentage_final']);</div>
<div>            break;</div>
<div>        case 'amount' :</div>
<div>            $updatequery .= " discount_percent=0, discount_amount=?,";</div>
<div>            array_push($updateparams, (float) $_REQUEST['discount_amount_final']);</div>
<div>            break;</div>
<div>        default :</div>
<div>            $updatequery .= " discount_percent=0, discount_amount=0,";</div>
<div>    }</div>
<div> </div>
<div> </div>
<div>In this small piece of code we see a few issues. These are some of the issues that dog vtiger.</div>
<div> </div>
<div>The original code references the $_REQUEST object directly. </div>
<div>The original code does not test for the variables existence before trying to use it which is bad form as well as a potential source of unexplained errors.</div>
<div>The original code uses ‘==’ as a comparison operator, It should always use ‘===’ unless there is a good reason not to.</div>
<div> </div>
<div>For those that are not aware…</div>
<div> </div>
<div>$cat = ‘cat’;</div>
<div> </div>
<div>0 == $cat <= TRUE</div>
<div>0 === $cat <= FALSE</div>
<div> </div>
<div>== does a type conversion before evaluating the result, (int)’cat’ is 0.</div>
<div>=== and !== compare the type as well as the value, therefore string ‘cat’ is not equal to integer 0.</div>
<div> </div>
<div>This can cause many hard to find bugs</div>
<div> </div>
<div>Chris</div>
<div> </div>
</font>
</body>
</html>