Test Failed
Branch develop (71ce82)
by
unknown
46:10
created

payments.lib.php ➔ getOnlinePaymentUrl()   F

Complexity

Conditions 52
Paths > 20000

Size

Total Lines 93
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 52
eloc 58
nc 429496.7295
nop 5
dl 0
loc 93
rs 2.1505
c 0
b 0
f 0

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
/**
3
 * Copyright (C) 2013	Marcos García	<[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 * or see http://www.gnu.org/
18
 */
19
20
/**
21
 * Returns an array with the tabs for the "Payment" section
22
 * It loads tabs from modules looking for the entity payment
23
 *
24
 * @param Paiement $object Current payment object
25
 * @return array Tabs for the payment section
26
 */
27
function payment_prepare_head(Paiement $object) {
28
29
	global $langs, $conf;
30
31
	$h = 0;
32
	$head = array();
33
34
	$head[$h][0] = DOL_URL_ROOT.'/compta/paiement/card.php?id='.$object->id;
35
	$head[$h][1] = $langs->trans("Card");
36
	$head[$h][2] = 'payment';
37
	$h++;
38
39
    // Show more tabs from modules
40
    // Entries must be declared in modules descriptor with line
41
    // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__');   to add new tab
42
    // $this->tabs = array('entity:-tabname);   												to remove a tab
43
    complete_head_from_modules($conf,$langs,$object,$head,$h,'payment');
44
45
	$head[$h][0] = DOL_URL_ROOT.'/compta/paiement/info.php?id='.$object->id;
46
	$head[$h][1] = $langs->trans("Info");
47
	$head[$h][2] = 'info';
48
	$h++;
49
50
	complete_head_from_modules($conf,$langs,$object,$head,$h,'payment', 'remove');
51
52
	return $head;
53
}
54
55
/**
56
 * Returns an array with the tabs for the "Supplier payment" section
57
 * It loads tabs from modules looking for the entity payment_supplier
58
 *
59
 * @param Paiement $object Current payment object
60
 * @return array Tabs for the payment section
61
 */
62
function payment_supplier_prepare_head(Paiement $object) {
63
64
	global $langs, $conf;
65
66
	$h = 0;
67
	$head = array();
68
69
	$head[$h][0] = DOL_URL_ROOT.'/fourn/paiement/card.php?id='.$object->id;
70
	$head[$h][1] = $langs->trans("Card");
71
	$head[$h][2] = 'payment';
72
	$h++;
73
74
    // Show more tabs from modules
75
    // Entries must be declared in modules descriptor with line
76
    // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__');   to add new tab
77
    // $this->tabs = array('entity:-tabname);   												to remove a tab
78
    complete_head_from_modules($conf,$langs,$object,$head,$h,'payment_supplier');
79
80
	$head[$h][0] = DOL_URL_ROOT.'/fourn/paiement/info.php?id='.$object->id;
81
	$head[$h][1] = $langs->trans('Info');
82
	$head[$h][2] = 'info';
83
	$h++;
84
85
	complete_head_from_modules($conf,$langs,$object,$head,$h,'payment_supplier', 'remove');
86
87
	return $head;
88
}
89
90
91
/**
92
 * Return string with full Url
93
 *
94
 * @param   string	$type		Type of URL ('free', 'order', 'invoice', 'contractline', 'membersubscription' ...)
95
 * @param	string	$ref		Ref of object
96
 * @return	string				Url string
97
 */
98
function showOnlinePaymentUrl($type,$ref)
99
{
100
	global $conf, $langs;
101
102
	$langs->load("PAYMENT");
103
	$langs->load("paybox");
104
	$servicename='Online';
105
106
	$out.=img_picto('','object_globe.png').' '.$langs->trans("ToOfferALinkForOnlinePayment",$servicename).'<br>';
0 ignored issues
show
Bug introduced by
The variable $out does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
107
	$url=getOnlinePaymentUrl(0,$type,$ref);
108
	$out.='<input type="text" id="onlinepaymenturl" class="quatrevingtpercent" value="'.$url.'">';
109
	$out.=ajax_autoselect("onlinepaymenturl", 0);
110
	return $out;
111
}
112
113
114
/**
115
 * Return string with full Url
116
 *
117
 * @param   int		$mode		0=True url, 1=Url formated with colors
118
 * @param   string	$type		Type of URL ('free', 'order', 'invoice', 'contractline', 'membersubscription' ...)
119
 * @param	string	$ref		Ref of object
120
 * @param	int		$amount		Amount
121
 * @param	string	$freetag	Free tag
122
 * @return	string				Url string
123
 */
124
function getOnlinePaymentUrl($mode,$type,$ref='',$amount='9.99',$freetag='your_free_tag')
125
{
126
	global $conf;
127
128
	$ref=str_replace(' ','',$ref);
129
130
	if ($type == 'free')
131
	{
132
		$out=DOL_MAIN_URL_ROOT.'/public/payment/newpayment.php?amount='.($mode?'<font color="#666666">':'').$amount.($mode?'</font>':'').'&tag='.($mode?'<font color="#666666">':'').$freetag.($mode?'</font>':'');
133
		if (! empty($conf->global->PAYMENT_SECURITY_TOKEN))
134
		{
135
			if (empty($conf->global->PAYMENT_SECURITY_TOKEN_UNIQUE)) $out.='&securekey='.$conf->global->PAYMENT_SECURITY_TOKEN;
136
			else $out.='&securekey='.dol_hash($conf->global->PAYMENT_SECURITY_TOKEN, 2);
137
		}
138
	}
139
	if ($type == 'order')
140
	{
141
		$out=DOL_MAIN_URL_ROOT.'/public/payment/newpayment.php?source=order&ref='.($mode?'<font color="#666666">':'');
142
		if ($mode == 1) $out.='order_ref';
143
		if ($mode == 0) $out.=urlencode($ref);
144
		$out.=($mode?'</font>':'');
145
		if (! empty($conf->global->PAYMENT_SECURITY_TOKEN))
146
		{
147
			if (empty($conf->global->PAYMENT_SECURITY_TOKEN_UNIQUE)) $out.='&securekey='.$conf->global->PAYMENT_SECURITY_TOKEN;
148
			else
149
			{
150
				$out.='&securekey='.($mode?'<font color="#666666">':'');
151
				if ($mode == 1) $out.="hash('".$conf->global->PAYMENT_SECURITY_TOKEN."' + '".$type."' + order_ref)";
152
				if ($mode == 0) $out.= dol_hash($conf->global->PAYMENT_SECURITY_TOKEN . $type . $ref, 2);
153
				$out.=($mode?'</font>':'');
154
			}
155
		}
156
	}
157
	if ($type == 'invoice')
158
	{
159
		$out=DOL_MAIN_URL_ROOT.'/public/payment/newpayment.php?source=invoice&ref='.($mode?'<font color="#666666">':'');
160
		if ($mode == 1) $out.='invoice_ref';
161
		if ($mode == 0) $out.=urlencode($ref);
162
		$out.=($mode?'</font>':'');
163
		if (! empty($conf->global->PAYMENT_SECURITY_TOKEN))
164
		{
165
			if (empty($conf->global->PAYMENT_SECURITY_TOKEN_UNIQUE)) $out.='&securekey='.$conf->global->PAYMENT_SECURITY_TOKEN;
166
			else
167
			{
168
				$out.='&securekey='.($mode?'<font color="#666666">':'');
169
				if ($mode == 1) $out.="hash('".$conf->global->PAYMENT_SECURITY_TOKEN."' + '".$type."' + invoice_ref)";
170
				if ($mode == 0) $out.= dol_hash($conf->global->PAYMENT_SECURITY_TOKEN . $type . $ref, 2);
171
				$out.=($mode?'</font>':'');
172
			}
173
		}
174
	}
175
	if ($type == 'contractline')
176
	{
177
		$out=DOL_MAIN_URL_ROOT.'/public/payment/newpayment.php?source=contractline&ref='.($mode?'<font color="#666666">':'');
178
		if ($mode == 1) $out.='contractline_ref';
179
		if ($mode == 0) $out.=urlencode($ref);
180
		$out.=($mode?'</font>':'');
181
		if (! empty($conf->global->PAYMENT_SECURITY_TOKEN))
182
		{
183
			if (empty($conf->global->PAYMENT_SECURITY_TOKEN_UNIQUE)) $out.='&securekey='.$conf->global->PAYMENT_SECURITY_TOKEN;
184
			else
185
			{
186
				$out.='&securekey='.($mode?'<font color="#666666">':'');
187
				if ($mode == 1) $out.="hash('".$conf->global->PAYMENT_SECURITY_TOKEN."' + '".$type."' + contractline_ref)";
188
				if ($mode == 0) $out.= dol_hash($conf->global->PAYMENT_SECURITY_TOKEN . $type . $ref, 2);
189
				$out.=($mode?'</font>':'');
190
			}
191
		}
192
	}
193
	if ($type == 'membersubscription')
194
	{
195
		$out=DOL_MAIN_URL_ROOT.'/public/payment/newpayment.php?source=membersubscription&ref='.($mode?'<font color="#666666">':'');
196
		if ($mode == 1) $out.='member_ref';
197
		if ($mode == 0) $out.=urlencode($ref);
198
		$out.=($mode?'</font>':'');
199
		if (! empty($conf->global->PAYMENT_SECURITY_TOKEN))
200
		{
201
			if (empty($conf->global->PAYMENT_SECURITY_TOKEN_UNIQUE)) $out.='&securekey='.$conf->global->PAYMENT_SECURITY_TOKEN;
202
			else
203
			{
204
				$out.='&securekey='.($mode?'<font color="#666666">':'');
205
				if ($mode == 1) $out.="hash('".$conf->global->PAYMENT_SECURITY_TOKEN."' + '".$type."' + member_ref)";
206
				if ($mode == 0) $out.= dol_hash($conf->global->PAYMENT_SECURITY_TOKEN . $type . $ref, 2);
207
				$out.=($mode?'</font>':'');
208
			}
209
		}
210
	}
211
212
	// For multicompany
213
	$out.="&entity=".$conf->entity; // Check the entity because He may be the same reference in several entities
0 ignored issues
show
Bug introduced by
The variable $out does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
214
215
	return $out;
216
}
217
218
219
220
/**
221
 * Show footer of company in HTML pages
222
 *
223
 * @param   Societe		$fromcompany	Third party
224
 * @param   Translate	$langs			Output language
225
 * @param	int			$addformmessage	Add the payment form message
226
 * @param	string		$suffix			Suffix to use on constants
227
 * @param	Object		$object			Object related to payment
228
 * @return	void
229
 */
230
function htmlPrintOnlinePaymentFooter($fromcompany,$langs,$addformmessage=0,$suffix='',$object=null)
231
{
232
    global $conf;
233
234
    // Juridical status
235
    $line1="";
236
    if ($fromcompany->forme_juridique_code)
237
    {
238
        $line1.=($line1?" - ":"").getFormeJuridiqueLabel($fromcompany->forme_juridique_code);
239
    }
240
    // Capital
241
    if ($fromcompany->capital)
242
    {
243
        $line1.=($line1?" - ":"").$langs->transnoentities("CapitalOf",$fromcompany->capital)." ".$langs->transnoentities("Currency".$conf->currency);
244
    }
245
    // Prof Id 1
246
    if ($fromcompany->idprof1 && ($fromcompany->country_code != 'FR' || ! $fromcompany->idprof2))
247
    {
248
        $field=$langs->transcountrynoentities("ProfId1",$fromcompany->country_code);
249
        if (preg_match('/\((.*)\)/i',$field,$reg)) $field=$reg[1];
250
        $line1.=($line1?" - ":"").$field.": ".$fromcompany->idprof1;
251
    }
252
    // Prof Id 2
253
    if ($fromcompany->idprof2)
254
    {
255
        $field=$langs->transcountrynoentities("ProfId2",$fromcompany->country_code);
256
        if (preg_match('/\((.*)\)/i',$field,$reg)) $field=$reg[1];
257
        $line1.=($line1?" - ":"").$field.": ".$fromcompany->idprof2;
258
    }
259
260
    // Second line of company infos
261
    $line2="";
262
    // Prof Id 3
263
    if ($fromcompany->idprof3)
264
    {
265
        $field=$langs->transcountrynoentities("ProfId3",$fromcompany->country_code);
266
        if (preg_match('/\((.*)\)/i',$field,$reg)) $field=$reg[1];
267
        $line2.=($line2?" - ":"").$field.": ".$fromcompany->idprof3;
268
    }
269
    // Prof Id 4
270
    if ($fromcompany->idprof4)
271
    {
272
        $field=$langs->transcountrynoentities("ProfId4",$fromcompany->country_code);
273
        if (preg_match('/\((.*)\)/i',$field,$reg)) $field=$reg[1];
274
        $line2.=($line2?" - ":"").$field.": ".$fromcompany->idprof4;
275
    }
276
    // IntraCommunautary VAT
277
    if ($fromcompany->tva_intra != '')
278
    {
279
        $line2.=($line2?" - ":"").$langs->transnoentities("VATIntraShort").": ".$fromcompany->tva_intra;
280
    }
281
282
    print '<br>';
283
284
    print '<div class="center">'."\n";
285
    if ($addformmessage)
286
    {
287
    	print '<!-- object = '.$object->element.' -->';
288
    	print '<br>';
289
290
    	$parammessageform='ONLINE_PAYMENT_MESSAGE_FORM_'.$suffix;
291
    	if (! empty($conf->global->$parammessageform)) print $langs->transnoentities($conf->global->$parammessageform);
292
    	else if (! empty($conf->global->ONLINE_PAYMENT_MESSAGE_FORM)) print $langs->transnoentities($conf->global->ONLINE_PAYMENT_MESSAGE_FORM);
293
294
    	// Add other message if VAT exists
295
    	if (! empty($object->total_vat) || ! empty($object->total_tva))
296
    	{
297
    		$parammessageform='ONLINE_PAYMENT_MESSAGE_FORMIFVAT_'.$suffix;
298
    		if (! empty($conf->global->$parammessageform)) print $langs->transnoentities($conf->global->$parammessageform);
299
    		else if (! empty($conf->global->ONLINE_PAYMENT_MESSAGE_FORMIFVAT)) print $langs->transnoentities($conf->global->ONLINE_PAYMENT_MESSAGE_FORMIFVAT);
300
    	}
301
    }
302
303
    print '<font style="font-size: 10px;"><br><hr>'."\n";
304
    print $fromcompany->name.'<br>';
305
    print $line1;
306
    if (strlen($line1+$line2) > 50) print '<br>';
307
    else print ' - ';
308
    print $line2;
309
    print '</font></div>'."\n";
310
}
311