SugarpdfHelper.php ➔ format_number_sugarpdf()   F
last analyzed

Complexity

Conditions 30
Paths 4800

Size

Total Lines 98
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 30
Metric Value
cc 30
eloc 61
nc 4800
nop 4
dl 0
loc 98
ccs 2
cts 2
cp 1
crap 30
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2 1
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
/*********************************************************************************
4
 * SugarCRM Community Edition is a customer relationship management program developed by
5
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6
7
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
8
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
9
 *
10
 * This program is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU Affero General Public License version 3 as published by the
12
 * Free Software Foundation with the addition of the following permission added
13
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
14
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
15
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
16
 *
17
 * This program is distributed in the hope that it will be useful, but WITHOUT
18
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
20
 * details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License along with
23
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
24
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
 * 02110-1301 USA.
26
 *
27
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
28
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
29
 *
30
 * The interactive user interfaces in modified source and object code versions
31
 * of this program must display Appropriate Legal Notices, as required under
32
 * Section 5 of the GNU Affero General Public License version 3.
33
 *
34
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
35
 * these Appropriate Legal Notices must retain the display of the "Powered by
36
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
37
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
38
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
39
 ********************************************************************************/
40
41
42 1
    function wrapTD($html, $options){
43
        return wrapTag("td",$html, $options);
44
    }
45
46 1
    function wrapTR($html, $options){
47
        return wrapTag("tr",$html, $options);
48
    }
49
50 1
    function wrapTable($html, $options){
51
        return wrapTag("table",$html, $options);
52
    }
53
54 1
    function wrapB($html){
55
        return "<b>".$html."</b>";
56
    }
57
58 1
    function wrapI($html){
59
        return "<i>".$html."</i>";
60
    }
61 1
    function wrapTag($tag, $html, $options){
62
        // Wrap the tags defined in the options array (like b, i, font... tags)
63
        if(!empty($options)){
64
            foreach($options as $k=>$v){
65
                if(is_array($v)){
66
                    $html = wrapTag($k, "$html", $v);
67
                }
68
            }
69
        }
70
        // wrap the HTML content with the passed tag
71
        $return = "<$tag ";
72
        if(!empty($options)){
73
            foreach($options as $k=>$v){
74
                if(!is_array($v)){
75
                    $return .= " $k=".'"'.$v.'"';
76
                }
77
            }
78
        }
79
        return $return.">".$html."</$tag>";
80
    }
81
82
    /**
83
     * This function prepare a string to be ready for the PDF printing.
84
     * @param $string
85
     * @return string
86
     */
87 1
    function prepare_string($string){
88
        global $locale;
89
        $string = html_entity_decode($string, ENT_QUOTES);
90
        // return $locale->translateCharset($string, 'UTF-8', $locale->getExportCharset());
91
        return $string;
92
    }
93
     /**
94
     * Copy of format_number() from currency with fix for sugarpdf.
95
     * @return String formatted currency value
96
     * @see modules/Currencies/Currency.php
97
     */
98 1
    function format_number_sugarpdf($amount, $round = null, $decimals = null, $params = array()) {
99
        global $app_strings, $current_user, $sugar_config, $locale;
100
        static $current_users_currency = null;
101
        static $last_override_currency = null;
102
        static $override_currency_id = null;
103
        static $currency;
104
105
        $seps = get_number_seperators();
106
        $num_grp_sep = $seps[0];
107
        $dec_sep = $seps[1];
108
109
        // cn: bug 8522 - sig digits not honored in pdfs
110
        if(is_null($decimals)) {
111
            $decimals = $locale->getPrecision();
112
        }
113
        if(is_null($round)) {
114
            $round = $locale->getPrecision();
115
        }
116
117
        // only create a currency object if we need it
118
        if((!empty($params['currency_symbol']) && $params['currency_symbol']) ||
119
          (!empty($params['convert']) && $params['convert']) ||
120
          (!empty($params['currency_id']))) {
121
                // if we have an override currency_id
122
                if(!empty($params['currency_id'])) {
123
                    if($override_currency_id != $params['currency_id']) {
124
                        $override_currency_id = $params['currency_id'];
125
                        $currency = new Currency();
126
                        $currency->retrieve($override_currency_id);
127
                        $last_override_currency = $currency;
128
                    } else {
129
                        $currency = $last_override_currency;
130
                    }
131
132
                } elseif(!isset($current_users_currency)) { // else use current user's
133
                    $current_users_currency = new Currency();
134
                    if($current_user->getPreference('currency')) $current_users_currency->retrieve($current_user->getPreference('currency'));
135
                    else $current_users_currency->retrieve('-99'); // use default if none set
136
                    $currency = $current_users_currency;
137
                }
138
        }
139
        if(!empty($params['convert']) && $params['convert']) {
140
            $amount = $currency->convertFromDollar($amount, 6);
141
        }
142
143
        if(!empty($params['currency_symbol']) && $params['currency_symbol']) {
144
            if(!empty($params['symbol_override'])) {
145
                $symbol = $params['symbol_override'];
146
            }
147
148
            // BEGIN SUGARPDF
149
            /*elseif(!empty($params['type']) && $params['type'] == 'pdf') {
150
                $symbol = $currency->getPdfCurrencySymbol();
151
                $symbol_space = false;
152
            }*/
153
            elseif(!empty($params['type']) && $params['type'] == 'sugarpdf') {
154
                $symbol = $currency->symbol;
155
                $symbol_space = false;
156
            }
157
            // END SUGARPDF
158
159
            else {
160
                if(empty($currency->symbol))
161
                    $symbol = $currency->getDefaultCurrencySymbol();
162
                else
163
                    $symbol = $currency->symbol;
164
                $symbol_space = true;
165
            }
166
        } else {
167
            $symbol = '';
168
        }
169
170
        if(isset($params['charset_convert'])) {
171
            $symbol = $locale->translateCharset($symbol, 'UTF-8', $locale->getExportCharset());
172
        }
173
174
        if(empty($params['human'])) {
175
           $amount = number_format(round($amount, $round), $decimals, $dec_sep, $num_grp_sep);
176
           $amount = format_place_symbol($amount, $symbol,(empty($params['symbol_space']) ? false : true));
177
        } else {
178
            // If amount is more greater than a thousand(positive or negative)
179
            if(strpos($amount, '.') > 0) {
180
               $checkAmount = strlen(substr($amount, 0, strpos($amount, '.')));
181
            }
182
183
            if($checkAmount >= 1000 || $checkAmount <= -1000) {
184
                $amount = round(($amount / 1000), 0);
185
                $amount = $amount . 'k';
186
                $amount = format_place_symbol($amount, $symbol,(empty($params['symbol_space']) ? false : true));
187
            } else {
188
                $amount = format_place_symbol($amount, $symbol,(empty($params['symbol_space']) ? false : true));
189
            }
190
        }
191
192
        if(!empty($params['percentage']) && $params['percentage']) $amount .= $app_strings['LBL_PERCENTAGE_SYMBOL'];
193
        return $amount;
194
195 1
    } //end function format_number
196
?>
197