$discount_amount corrupted in Version 7.11.20

Hello,
I installed the version 7.11.20 and the $ discount_amount variable seems not to work properly in pdf templates, it returns a null value.

@leonardo.bandini

Welcome to the community!

Which module do you mean?

Hi,
thanks for your attention, the issue is in * / modules / AOS_PDF_Templates / *
modifying the php file as below solves the problem, but with the original php file of this release it does not work.

Per favore controllate il file: templateParser.php, questa è la versione corretta:

<?php

class templateParser
{
    static function parse_template($string, $bean_arr)
    {

        foreach ($bean_arr as $bean_name => $bean_id) {

            $focus = BeanFactory::getBean($bean_name, $bean_id);
            $string = templateParser::parse_template_bean($string, $focus->table_name, $focus);

            foreach ($focus->field_defs as $focus_name => $focus_arr) {
                if ($focus_arr['type'] == 'relate') {
                    if (isset($focus_arr['module']) && $focus_arr['module'] != '' && $focus_arr['module'] != 'EmailAddress') {

                        $idName = $focus_arr['id_name'];
                        $relate_focus = BeanFactory::getBean($focus_arr['module'], $focus->$idName);

                        $string = templateParser::parse_template_bean($string, $focus_arr['name'], $relate_focus);
                    }
                }
            }

        }
        return $string;
    }

    function parse_template_bean($string, $key, &$focus)
    {
        global $app_strings, $sugar_config;
        $repl_arr = array();

        foreach ($focus->field_defs as $field_def) {
            if (isset($field_def['name']) && $field_def['name'] != '') {
                $fieldName = $field_def['name'];
                if ($field_def['type'] == 'currency') {
                    $repl_arr[$key . "_" . $fieldName] = currency_format_number($focus->$fieldName, $params = array('currency_symbol' => false));
                } else if (($field_def['type'] == 'radioenum' || $field_def['type'] == 'enum' || $field_def['type'] == 'dynamicenum') && isset($field_def['options'])) {
                    $repl_arr[$key . "_" . $fieldName] = translate($field_def['options'], $focus->module_dir, $focus->$fieldName);
                } else if ($field_def['type'] == 'multienum' && isset($field_def['options'])) {
                    $repl_arr[$key . "_" . $fieldName] = implode(', ', unencodeMultienum($focus->$fieldName));
                } //Fix for Windows Server as it needed to be converted to a string.
                else if ($field_def['type'] == 'int') {
                    $repl_arr[$key . "_" . $fieldName] = strval($focus->$fieldName);
                } else if ($field_def['type'] == 'image') {
                    $secureLink = $sugar_config['site_url'] . '/' . "public/". $focus->id .  '_' . $fieldName;
                    $file_location = $sugar_config['upload_dir'] . '/'  . $focus->id .  '_' . $fieldName;
                    // create a copy with correct extension by mime type
                    if(!file_exists('public')) {
                        sugar_mkdir('public', 0777);
                    }
                    if(!copy($file_location, "public/{$focus->id}".  '_' . "$fieldName")) {
                        $secureLink = $sugar_config['site_url'] . '/'. $file_location;
                    }
                    $link = $secureLink;
                    $repl_arr[$key . "_" . $fieldName] = '<img src="' . $link . '" width="'.$field_def['width'].'" height="'.$field_def['height'].'"/>';
                } else {
                    $repl_arr[$key . "_" . $fieldName] = $focus->$fieldName;
                }
            }
        } // end foreach()

        krsort($repl_arr);
        reset($repl_arr);

        foreach ($repl_arr as $name => $value) {
            if (strpos($name, 'product_discount') > 0) {
                if ($value != '' && $value != '0.00') {
                    if ($repl_arr['aos_products_quotes_discount'] == 'Pct' || $repl_arr['aos_products_quotes_discount'] == 'Percentage' || $repl_arr['aos_products_quotes_discount'] == 'Percent' || $repl_arr['aos_products_quotes_discount'] == 'Percentuale') {
                        $sep = get_number_seperators();
                        $value = rtrim(rtrim(format_number($value), '0'), $sep[1]) . $app_strings['LBL_PERCENTAGE_SYMBOL'];
                    } else {
                        $value = currency_format_number($value, $params = array('currency_symbol' => false));
                    }
                } else {
                    $value = '';
                }
            }
            if ($name === 'aos_products_product_image' && !empty($value)) {
                $value = '<img src="' . $value . '"width="50" height="50"/>';
            }
            if ($name === 'aos_products_quotes_product_qty') {
                $sep = get_number_seperators();
                $value = rtrim(rtrim(format_number($value), '0'), $sep[1]);
            }
            if ($name === 'aos_products_quotes_vat' || strpos($name, 'pct') > 0 || strpos($name, 'percent') > 0 || strpos($name, 'percentage') > 0) {
                $sep = get_number_seperators();
                $value = rtrim(rtrim(format_number($value), '0'), $sep[1]) . $app_strings['LBL_PERCENTAGE_SYMBOL'];
            }
            if (strpos($name, 'date') > 0 || strpos($name, 'expiration') > 0) {
                if ($value != '') {
                    $dt = explode(' ', $value);
                    $value = $dt[0];
                }
            }
            if ($value != '' && is_string($value)) {
                $string = str_replace("\$$name", $value, $string);
            } else if (strpos($name, 'address') > 0) {
                $string = str_replace("\$$name<br />", '', $string);
                $string = str_replace("\$$name <br />", '', $string);
                $string = str_replace("\$$name", '', $string);
            } else {
                $string = str_replace("\$$name", '&nbsp;', $string);
            }


        }

        return $string;
    }
}

?>

@leonardo.bandini

I wrote about module with field $discount_amount. The field is present into modules: AOS_Invoices, AOS_Quotes, AOS_Contracts.

I did not find any problems on my test installation.

I see that your code has some changes but i didn’t analyze them.

were you able to solve this?

Can you share some screenshots for us to analyse the issue?

Hi @cherub-chum,
I originally have 2 SuiteCRM v7.10.27 instances and I just upgraded one of them to v7.11.21 today. I encountered the same problem described by @leonardo.bandini with the v7.11.21 system.


I created a quote with one product line, and entered a 50% discount, and then generated a pdf file using the “Quote Sample” template. In the generated pdf file, the Grand Total bef disc ($total_amt) and the Grand Total ($total_amount) were displayed correctly. However, the Discount ($discount_amount), Subtotal ($subtotal_amount), Tax ($tax_amount) and Shipping ($shipping_amount) were all blank. Please see the uploaded screen capture.

I further tested: When I renamed away the “/modules/AOS_PDF_Templates/templateParser.php” file and copied over the same file from the v7.10.27 instance and re-generated the pdf file, the Discount ($discount_amount), Subtotal ($subtotal_amount), Tax ($tax_amount) and Shipping ($shipping_amount) appeared correctly.

I am new to PHP, and so I could not pinpoint which part of the code is causing the problem.

Thanks.

p/s: I just saw that @leonardo.bandini opened the issue https://github.com/salesagility/SuiteCRM/issues/9213 about 3 months ago in github (which is also new to me). Should I add a “comment” there with the above observations? Thanks.

I compared the lines in the v7.11.21 templateParser.php versus v7.10.27 templateParser.php, without much comprehension of what the code is doing. By trial and error, I observed the following:

  1. if I comment out line 69 to line 72 of v7.11.21 templateParser.php, the Subtotal, Tax and Shipping will appear in the generated pdf. However the discount is still blank.

  2. if I further comment away the “&& isset($repl_arr[‘aos_products_quotes_discount’])” condition in line 129 of of v7.11.21 templateParser.php, the discount will appear in the generated pdf