Completed
Push — master ( d63371...9ce9eb )
by Alexis
21s queued 19s
created

_updateLineNC()   F

Complexity

Conditions 19
Paths 800

Size

Total Lines 80
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 19
eloc 44
c 1
b 0
f 0
nc 800
nop 4
dl 0
loc 80
rs 2.5

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
/* <one line to give the program's name and a brief idea of what it does.>
3
 * Copyright (C) 2015 ATM Consulting <[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
 */
18
19
/**
20
 *	\file		lib/importdevis.lib.php
21
 *	\ingroup	importdevis
22
 *	\brief		This file is an example module library
23
 *				Put some comments here
24
 */
25
26
function subtotalAdminPrepareHead()
27
{
28
    global $langs, $conf;
29
30
    $langs->load("subtotal@subtotal");
31
32
    $h = 0;
33
    $head = array();
34
35
    $head[$h][0] = dol_buildpath("/subtotal/admin/subtotal_setup.php", 1);
0 ignored issues
show
Bug introduced by
The function dol_buildpath was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
    $head[$h][0] = /** @scrutinizer ignore-call */ dol_buildpath("/subtotal/admin/subtotal_setup.php", 1);
Loading history...
36
    $head[$h][1] = $langs->trans("Parameters");
37
    $head[$h][2] = 'settings';
38
    $h++;
39
    $head[$h][0] = dol_buildpath("/subtotal/admin/subtotal_about.php", 1);
40
    $head[$h][1] = $langs->trans("About");
41
    $head[$h][2] = 'about';
42
    $h++;
43
44
    complete_head_from_modules($conf, $langs, $object, $head, $h, 'subtotal', $showLabel=false);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $object seems to be never defined.
Loading history...
Bug introduced by
The function complete_head_from_modules was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
    /** @scrutinizer ignore-call */ 
45
    complete_head_from_modules($conf, $langs, $object, $head, $h, 'subtotal', $showLabel=false);
Loading history...
45
46
    return $head;
47
}
48
49
function getHtmlSelectTitle(&$object, $showLabel=false)
50
{
51
	global $langs;
52
	
53
	require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php';
0 ignored issues
show
Bug introduced by
The constant DOL_DOCUMENT_ROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
54
	dol_include_once('/subtotal/class/subtotal.class.php');
0 ignored issues
show
Bug introduced by
The function dol_include_once was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
	/** @scrutinizer ignore-call */ 
55
 dol_include_once('/subtotal/class/subtotal.class.php');
Loading history...
55
	$TTitle = TSubtotal::getAllTitleFromDocument($object);
56
	$html = '';
57
	if ($showLabel) $html.= '<label for="under_title">'.$langs->trans('subtotalLabelForUnderTitle').'</label>';
58
	$html.= '<select onChange="$(\'select[name=under_title]\').val(this.value);" name="under_title" class="under_title minwidth200"><option value="-1"></option>';
59
	
60
	$nbsp = '&nbsp;';
61
	foreach ($TTitle as &$line)
62
	{
63
		$str = str_repeat($nbsp, ($line->qty - 1) * 3);
64
		$html .= '<option value="'.$line->rang.'">'.$str.(!empty($line->label) ? $line->label : dol_trunc($line->desc, 30)).'</option>';
0 ignored issues
show
Bug introduced by
The function dol_trunc was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
		$html .= '<option value="'.$line->rang.'">'.$str.(!empty($line->label) ? $line->label : /** @scrutinizer ignore-call */ dol_trunc($line->desc, 30)).'</option>';
Loading history...
65
	}
66
	
67
	$html .= '</select>';
68
	return $html;
69
}
70
71
function getTFreeText()
72
{
73
	global $db,$conf;
74
	
75
	$TFreeText = array();
76
	
77
	$sql = 'SELECT rowid, label, content, active, entity FROM '.MAIN_DB_PREFIX.'c_subtotal_free_text WHERE active = 1 AND entity = '.$conf->entity.' ORDER BY label';
0 ignored issues
show
Bug introduced by
The constant MAIN_DB_PREFIX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
78
	$resql = $db->query($sql);
79
	
80
	if ($resql)
81
	{
82
		while ($row = $db->fetch_object($resql))
83
		{
84
			$TFreeText[$row->rowid] = $row;
85
		}
86
	}
87
	
88
	return $TFreeText;
89
}
90
91
function getHtmlSelectFreeText($withEmpty=true)
92
{
93
	global $langs;
94
	
95
	$TFreeText = getTFreeText();
96
	$html = '<label for="free_text">'.$langs->trans('subtotalLabelForFreeText').'</label>';
97
	$html.= '<select onChange="getTFreeText($(this));" name="free_text" class="minwidth200">';
98
	if ($withEmpty) $html.= '<option value=""></option>';
99
100
	$TFreeTextContents = array();
101
	foreach ($TFreeText as $id => $tab)
102
	{
103
		$html.= '<option value="'.$id.'">'.$tab->label.'</option>';
104
		$TFreeTextContents[$id] = $tab->content;
105
	}
106
107
	$html .= '</select>';
108
109
	$html .= '<script type="text/javascript">';
110
	$html .= 'function getTFreeText(select) {';
111
	$html .= ' var TFreeText = '.json_encode($TFreeTextContents).';';
112
	$html .= ' var id = select.val();';
113
	$html .= ' if (id in TFreeText) {';
114
	$html .= '  var content = TFreeText[id];';
115
	$html .= '  if (typeof CKEDITOR == "object" && typeof CKEDITOR.instances != "undefined" && "sub-total-title" in CKEDITOR.instances) {';
116
	$html .= '   var editor = CKEDITOR.instances["sub-total-title"];';
117
	$html .= '   editor.setData(content);';
118
	$html .= '  } else {';
119
	$html .= '   $("#sub-total-title").val(content);';
120
	$html .= '  }';
121
	$html .= ' }';
122
	$html .= '}';
123
	$html .= '</script>';
124
125
	return $html;
126
}
127
128
function _updateSubtotalLine(&$object, &$line)
129
{
130
	$label = GETPOST('line-title');
0 ignored issues
show
Bug introduced by
The function GETPOST was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
	$label = /** @scrutinizer ignore-call */ GETPOST('line-title');
Loading history...
131
	$description = ($line->qty>90) ? '' : GETPOST('line-description');
132
	$pagebreak = (int) GETPOST('line-pagebreak');
133
134
	$level = GETPOST('subtotal_level', 'int');
135
	if (!empty($level))
136
	{
137
		if ($line->qty > 90) $line->qty = 100 - $level; // Si on edit une ligne sous-total
138
		else $line->qty = $level;
139
	}
140
	
141
	$res = TSubtotal::doUpdateLine($object, $line->id, $description, 0, $line->qty, 0, '', '', 0, 9, 0, 0, 'HT', $pagebreak, 0, 1, null, 0, $label, TSubtotal::$module_number, $line->array_options);
142
143
	return $res;
144
}
145
146
function _updateSubtotalBloc($object, $line)
147
{
148
	global $conf,$langs;
149
	
150
	$subtotal_tva_tx = $subtotal_tva_tx_init = GETPOST('subtotal_tva_tx', 'int');
0 ignored issues
show
Bug introduced by
The function GETPOST was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

150
	$subtotal_tva_tx = $subtotal_tva_tx_init = /** @scrutinizer ignore-call */ GETPOST('subtotal_tva_tx', 'int');
Loading history...
151
	$subtotal_progress = $subtotal_progress_init = GETPOST('subtotal_progress', 'int');
152
	$array_options = $line->array_options;
153
	$showBlockExtrafields = GETPOST('showBlockExtrafields');
154
	
155
	if ($subtotal_tva_tx != '' || $subtotal_progress != '' || (!empty($showBlockExtrafields) && !empty($array_options)))
156
	{
157
		$error_progress = $nb_progress_update = $nb_progress_not_updated = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $nb_progress_update is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $error_progress is dead and can be removed.
Loading history...
158
		$TLine = TSubtotal::getLinesFromTitleId($object, $line->id);
159
		foreach ($TLine as &$line)
0 ignored issues
show
introduced by
$line is overwriting one of the parameters of this function.
Loading history...
160
		{
161
			if (!TSubtotal::isModSubtotalLine($line))
162
			{
163
				$subtotal_tva_tx = $subtotal_tva_tx_init; // ré-init car la variable peut évoluer
164
					
165
				if (!empty($showBlockExtrafields)) $line->array_options = $array_options;
166
				if ($subtotal_tva_tx == '') $subtotal_tva_tx = $line->tva_tx;
167
				if ($object->element == 'facture' && !empty($conf->global->INVOICE_USE_SITUATION) && $object->type == Facture::TYPE_SITUATION)
0 ignored issues
show
Bug introduced by
The type Facture was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
168
				{
169
					$subtotal_progress = $subtotal_progress_init;
170
					if ($subtotal_progress == '') $subtotal_progress = $line->situation_percent;
171
					else
172
					{
173
						$prev_percent = $line->get_prev_progress($object->id);
174
						if ($subtotal_progress < $prev_percent)
175
						{
176
							$nb_progress_not_updated++;
177
							$subtotal_progress = $line->situation_percent;
178
						}
179
					}
180
				}
181
				
182
				$res = TSubtotal::doUpdateLine($object, $line->id, $line->desc, $line->subprice, $line->qty, $line->remise_percent, $line->date_start, $line->date_end, $subtotal_tva_tx, $line->product_type, $line->localtax1_tx, $line->localtax2_tx, 'HT', $line->info_bits, $line->fk_parent_line, $line->skip_update_total, $line->fk_fournprice, $line->pa_ht, $line->label, $line->special_code, $line->array_options, $subtotal_progress, $line->fk_unit);
183
184
				if ($res > 0) $success_updated_line++;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $success_updated_line seems to be never defined.
Loading history...
185
				else $error_updated_line++;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $error_updated_line seems to be never defined.
Loading history...
186
			}
187
		}
188
189
		if ($nb_progress_not_updated > 0) setEventMessage($langs->trans('subtotal_nb_progress_not_updated', $nb_progress_not_updated), 'warnings');
0 ignored issues
show
Bug introduced by
The function setEventMessage was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

189
		if ($nb_progress_not_updated > 0) /** @scrutinizer ignore-call */ setEventMessage($langs->trans('subtotal_nb_progress_not_updated', $nb_progress_not_updated), 'warnings');
Loading history...
190
		
191
		if ($success_updated_line > 0) setEventMessage($langs->trans('subtotal_success_updated_line', $success_updated_line));
192
		if ($error_updated_line > 0)
193
		{
194
			setEventMessage($langs->trans('subtotal_error_updated_line', $error_updated_line), 'errors');
195
			return -$error_updated_line;
196
		}
197
		
198
		return $success_updated_line;
199
	}
200
	
201
	return 0;
202
}
203
204
function _createExtraComprisNonCompris()
205
{
206
	global $db;
207
	
208
	dol_include_once('/core/class/extrafields.class.php');
0 ignored issues
show
Bug introduced by
The function dol_include_once was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

208
	/** @scrutinizer ignore-call */ 
209
 dol_include_once('/core/class/extrafields.class.php');
Loading history...
209
	
210
	$extra = new ExtraFields($db); // propaldet, commandedet, facturedet
0 ignored issues
show
Bug introduced by
The type ExtraFields was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
211
	$extra->addExtraField('subtotal_nc', 'Non compris', 'varchar', 0, 255, 'propaldet', 0, 0, '', unserialize('a:1:{s:7:"options";a:1:{s:0:"";N;}}'), 0, '', 0, 1);
212
	$extra->addExtraField('subtotal_nc', 'Non compris', 'varchar', 0, 255, 'commandedet', 0, 0, '', unserialize('a:1:{s:7:"options";a:1:{s:0:"";N;}}'), 0, '', 0, 1);
213
	$extra->addExtraField('subtotal_nc', 'Non compris', 'varchar', 0, 255, 'facturedet', 0, 0, '', unserialize('a:1:{s:7:"options";a:1:{s:0:"";N;}}'), 0, '', 0, 1);
214
	$extra->addExtraField('subtotal_nc', 'Non compris', 'varchar', 0, 255, 'supplier_proposaldet', 0, 0, '', unserialize('a:1:{s:7:"options";a:1:{s:0:"";N;}}'), 0, '', 0, 1);
215
	$extra->addExtraField('subtotal_nc', 'Non compris', 'varchar', 0, 255, 'commande_fournisseurdet', 0, 0, '', unserialize('a:1:{s:7:"options";a:1:{s:0:"";N;}}'), 0, '', 0, 1);
216
	$extra->addExtraField('subtotal_nc', 'Non compris', 'varchar', 0, 255, 'facture_fourn_det', 0, 0, '', unserialize('a:1:{s:7:"options";a:1:{s:0:"";N;}}'), 0, '', 0, 1);
217
}
218
219
220
	
221
/**
222
 * Maj du bloc pour forcer le total_tva et total_ht à 0 et recalculer le total du document
223
 * 
224
 * @param	$lineid			= title lineid
0 ignored issues
show
Documentation Bug introduced by
The doc comment = at position 0 could not be parsed: Unknown type name '=' at position 0 in =.
Loading history...
225
 * @param	$subtotal_nc	0 = "Compris" prise en compte des totaux des lignes; 1 = "Non compris" non prise en compte des totaux du bloc; null = update de toutes les lignes 
226
 */
227
function _updateLineNC($element, $elementid, $lineid, $subtotal_nc=null)
228
{
229
	global $db,$langs,$tmp_object_nc;
230
	
231
	$error = 0;
232
	if (empty($element)) $error++;
233
	
234
	if (!$error)
235
	{
236
		if (!empty($tmp_object_nc) && $tmp_object_nc->element == $element && $tmp_object_nc->id == $elementid)
237
		{
238
			$object = $tmp_object_nc;
239
		}
240
		else
241
		{
242
			$classname = ucfirst($element);
243
			
244
			switch ($element) {
245
			    case 'supplier_proposal':
246
			        $classname = 'SupplierProposal';
247
			        break;
248
			        
249
			    case 'order_supplier':
250
			        $classname = 'CommandeFournisseur';
251
			        break;
252
			        
253
			    case 'invoice_supplier':
254
			        $classname = 'FactureFournisseur';
255
			        break;
256
			}
257
			
258
			$object = new $classname($db); // Propal | Commande | Facture
259
			$res = $object->fetch($elementid);
260
			if ($res < 0) $error++;
261
			else $tmp_object_nc = $object;
262
		}
263
	}
264
	
265
	if (!$error)
266
	{
267
		foreach ($object->lines as &$l)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $object does not seem to be defined for all execution paths leading up to this point.
Loading history...
268
		{
269
			if($l->id == $lineid) {
270
				$line = $l;
271
				break;
272
			}
273
		}
274
		
275
		if (!empty($line))
276
		{
277
			$db->begin();
278
			
279
			if(TSubtotal::isModSubtotalLine($line))
280
			{
281
				if(TSubtotal::isTitle($line)) {
282
					// Update le contenu du titre (ainsi que le titre lui même)
283
					$TTitleBlock = TSubtotal::getLinesFromTitleId($object, $lineid, true);
284
					foreach($TTitleBlock as &$line_block)
285
					{
286
						$res = doUpdate($object, $line_block, $subtotal_nc);
0 ignored issues
show
Unused Code introduced by
The assignment to $res is dead and can be removed.
Loading history...
287
					}
288
				}
289
			}
290
			else
291
			{
292
				$res = doUpdate($object, $line, $subtotal_nc);
293
			}
294
			
295
			$res = $object->update_price(1);
296
			if ($res <= 0) $error++;
297
			
298
			if (!$error)
299
			{
300
				setEventMessage($langs->trans('subtotal_update_nc_success'));
0 ignored issues
show
Bug introduced by
The function setEventMessage was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

300
				/** @scrutinizer ignore-call */ 
301
    setEventMessage($langs->trans('subtotal_update_nc_success'));
Loading history...
301
				$db->commit();
302
			}
303
			else
304
			{
305
				setEventMessage($langs->trans('subtotal_update_nc_error'), 'errors');
306
				$db->rollback();
307
			}
308
		}
309
	}
310
}
311
312
function doUpdate(&$object, &$line, $subtotal_nc)
313
{
314
	global $user;
315
	
316
	if (TSubtotal::isFreeText($line) || TSubtotal::isSubtotal($line)) return 1;
317
	// Update extrafield et total
318
	if(! empty($subtotal_nc)) {
319
		$line->total_ht = $line->total_tva = $line->total_ttc = $line->total_localtax1 = $line->total_localtax2 = 
320
			$line->multicurrency_total_ht = $line->multicurrency_total_tva = $line->multicurrency_total_ttc = 0;
321
322
		$line->array_options['options_subtotal_nc'] = 1;
323
324
		if ($line->element == 'propaldet') $res = $line->update();
325
		else $res = $line->update($user);
326
	}
327
	else {
328
		$line->array_options['options_subtotal_nc'] = 0;
329
		$res = TSubtotal::doUpdateLine($object, $line->id, $line->desc, $line->subprice, $line->qty, $line->remise_percent, $line->date_start, $line->date_end, $line->tva_tx, $line->product_type, $line->localtax1_tx, $line->localtax2_tx, 'HT', $line->info_bits, $line->fk_parent_line, $line->skip_update_total, $line->fk_fournprice, $line->pa_ht, $line->label, $line->special_code, $line->array_options, $line->situation_percent, $line->fk_unit);
330
	}
331
	
332
	return $res;
333
}
334
335
function _updateLine($element, $elementid, $lineid)
336
{
337
	_updateLineNC($element, $elementid, $lineid);
338
}
339