Completed
Branch develop (e6f0e7)
by
unknown
24:49
created

list.php ➔ showMyBalance()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 2
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2011	   Dimitri Mouillard	<[email protected]>
3
 * Copyright (C) 2013-2017 Laurent Destailleur	<[email protected]>
4
 * Copyright (C) 2012-2016 Regis Houssin		<[email protected]>
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
/**
21
 *   	\file       htdocs/holiday/list.php
22
 *		\ingroup    holiday
23
 *		\brief      List of holiday
24
 */
25
26
require('../main.inc.php');
27
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
28
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
29
require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
30
require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
31
require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
32
require_once DOL_DOCUMENT_ROOT.'/holiday/common.inc.php';
33
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
34
require_once DOL_DOCUMENT_ROOT.'/core/lib/usergroups.lib.php';
35
36
$langs->load('users');
37
$langs->load('holidays');
38
$langs->load('hrm');
39
40
// Protection if external user
41
if ($user->societe_id > 0) accessforbidden();
42
43
$limit = GETPOST('limit')?GETPOST('limit','int'):$conf->liste_limit;
44
$sortfield = GETPOST("sortfield",'alpha');
45
$sortorder = GETPOST("sortorder",'alpha');
46
$page = GETPOST("page",'int');
47
$page = is_numeric($page) ? $page : 0;
48
$page = $page == -1 ? 0 : $page;
49
50
if (! $sortfield) $sortfield="cp.rowid";
51
if (! $sortorder) $sortorder="DESC";
52
$offset = $limit * $page;
53
$pageprev = $page - 1;
54
$pagenext = $page + 1;
55
56
$id = GETPOST('id','int');
57
58
$sall            = GETPOST('sall', 'alphanohtml');
59
$search_ref      = GETPOST('search_ref');
60
$month_create    = GETPOST('month_create');
61
$year_create     = GETPOST('year_create');
62
$month_start     = GETPOST('month_start');
63
$year_start      = GETPOST('year_start');
64
$month_end       = GETPOST('month_end');
65
$year_end        = GETPOST('year_end');
66
$search_employee = GETPOST('search_employee');
67
$search_valideur = GETPOST('search_valideur');
68
$search_statut   = GETPOST('select_statut');
69
$search_type     = GETPOST('search_type','int');
70
71
// List of fields to search into when doing a "search in all"
72
$fieldstosearchall = array(
73
    'cp.description'=>'Description',
74
    'uu.lastname'=>'EmployeeLastname',
75
    'uu.firstname'=>'EmployeeFirstname'
76
);
77
78
79
80
/*
81
 * Actions
82
 */
83
84
if (GETPOST('button_removefilter_x','alpha') || GETPOST('button_removefilter.x','alpha') || GETPOST('button_removefilter','alpha')) // All tests are required to be compatible with all browsers
85
{
86
	$search_ref="";
87
	$month_create="";
88
	$year_create="";
89
    $month_start="";
90
	$year_start="";
91
	$month_end="";
92
	$year_end="";
93
	$search_employee="";
94
	$search_valideur="";
95
	$search_statut="";
96
	$search_type='';
97
}
98
99
100
101
/*
102
 * View
103
 */
104
105
$holiday = new Holiday($db);
106
$holidaystatic=new Holiday($db);
107
$fuser = new User($db);
108
109
$childids = $user->getAllChildIds();
110
$childids[]=$user->id;
111
112
// Update sold
113
$result = $holiday->updateBalance();
114
115
$max_year = 5;
116
$min_year = 10;
117
$filter='';
118
119
llxHeader('', $langs->trans('CPTitreMenu'));
120
121
$order = $db->order($sortfield,$sortorder).$db->plimit($limit + 1, $offset);
122
123
// Ref
124
if(!empty($search_ref))
125
{
126
    $filter.= " AND cp.rowid = ".$db->escape($search_ref);
127
}
128
129
// Start date
130
if($year_start > 0) {
131
    if($month_start > 0) {
132
    	$filter .= " AND (cp.date_debut BETWEEN '".$db->idate(dol_get_first_day($year_start,$month_start,1))."' AND '".$db->idate(dol_get_last_day($year_start,$month_start,1))."')";
133
    	//$filter.= " AND date_format(cp.date_debut, '%Y-%m') = '$year_start-$month_start'";
134
    } else {
135
    	$filter .= " AND (cp.date_debut BETWEEN '".$db->idate(dol_get_first_day($year_start,1,1))."' AND '".$db->idate(dol_get_last_day($year_start,12,1))."')";
136
    	//$filter.= " AND date_format(cp.date_debut, '%Y') = '$year_start'";
137
    }
138
} else {
139
    if($month_start > 0) {
140
        $filter.= " AND date_format(cp.date_debut, '%m') = '".$db->escape($month_start)."'";
141
    }
142
}
143
144
// End date
145
if($year_end > 0) {
146
    if($month_end > 0) {
147
    	$filter .= " AND (cp.date_fin BETWEEN '".$db->idate(dol_get_first_day($year_end,$month_end,1))."' AND '".$db->idate(dol_get_last_day($year_end,$month_end,1))."')";
148
    	//$filter.= " AND date_format(cp.date_fin, '%Y-%m') = '$year_end-$month_end'";
149
    } else {
150
    	$filter .= " AND (cp.date_fin BETWEEN '".$db->idate(dol_get_first_day($year_end,1,1))."' AND '".$db->idate(dol_get_last_day($year_end,12,1))."')";
151
    	//$filter.= " AND date_format(cp.date_fin, '%Y') = '$year_end'";
152
    }
153
} else {
154
    if($month_end > 0) {
155
        $filter.= " AND date_format(cp.date_fin, '%m') = '".$db->escape($month_end)."'";
156
    }
157
}
158
159
// Create date
160
if($year_create > 0) {
161
    if($month_create > 0) {
162
    	$filter .= " AND (cp.date_create BETWEEN '".$db->idate(dol_get_first_day($year_create,$month_create,1))."' AND '".$db->idate(dol_get_last_day($year_create,$month_create,1))."')";
163
    	//$filter.= " AND date_format(cp.date_create, '%Y-%m') = '$year_create-$month_create'";
164
    } else {
165
    	$filter .= " AND (cp.date_create BETWEEN '".$db->idate(dol_get_first_day($year_create,1,1))."' AND '".$db->idate(dol_get_last_day($year_create,12,1))."')";
166
    	//$filter.= " AND date_format(cp.date_create, '%Y') = '$year_create'";
167
    }
168
} else {
169
    if($month_create > 0) {
170
        $filter.= " AND date_format(cp.date_create, '%m') = '".$db->escape($month_create)."'";
171
    }
172
}
173
174
// Employee
175
if(!empty($search_employee) && $search_employee != -1) {
176
    $filter.= " AND cp.fk_user = '".$db->escape($search_employee)."'\n";
177
}
178
179
// Validator
180
if(!empty($search_valideur) && $search_valideur != -1) {
181
    $filter.= " AND cp.fk_validator = '".$db->escape($search_valideur)."'\n";
182
}
183
184
// Type
185
if (!empty($search_type) && $search_type != -1) {
186
	$filter.= ' AND cp.fk_type IN ('.$db->escape($search_type).')';
187
}
188
189
// Status
190
if(!empty($search_statut) && $search_statut != -1) {
191
    $filter.= " AND cp.statut = '".$db->escape($search_statut)."'\n";
192
}
193
// Search all
194
if (!empty($sall))
195
{
196
	$filter.= natural_search(array_keys($fieldstosearchall), $sall);
197
}
198
199
if (empty($user->rights->holiday->read_all)) $filter.=' AND cp.fk_user IN ('.join(',',$childids).')';
200
201
202
// Récupération de l'ID de l'utilisateur
203
$user_id = $user->id;
204
205
if ($id > 0)
206
{
207
	// Charge utilisateur edite
208
	$fuser->fetch($id, '', '', 1);
209
	$fuser->getrights();
210
	$user_id = $fuser->id;
211
}
212
213
// Récupération des congés payés de l'utilisateur ou de tous les users
214
if (empty($user->rights->holiday->read_all) || $id > 0)
215
{
216
	$result = $holiday->fetchByUser($user_id,$order,$filter);	// Load array $holiday->holiday
217
}
218
else
219
{
220
    $result = $holiday->fetchAll($order,$filter);	// Load array $holiday->holiday
221
}
222
// Si erreur SQL
223
if ($result == '-1')
224
{
225
    print load_fiche_titre($langs->trans('CPTitreMenu'), '', 'title_hrm.png');
226
227
    dol_print_error($db, $langs->trans('Error').' '.$holiday->error);
228
    exit();
229
}
230
231
232
// Show table of vacations
233
234
$var=true;
235
$num = count($holiday->holiday);
236
$form = new Form($db);
237
$formother = new FormOther($db);
238
239
$param='';
240
if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
241
if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
242
if ($optioncss != '') $param.='&optioncss='.$optioncss;
243
244
print '<form method="get" action="'.$_SERVER["PHP_SELF"].'">'."\n";
245
if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
246
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
247
print '<input type="hidden" name="action" value="list">';
248
print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
249
print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
250
print '<input type="hidden" name="page" value="'.$page.'">';
251
if ($id > 0) print '<input type="hidden" name="id" value="'.$id.'">';
252
253
if ($id > 0)
254
{
255
	$title = $langs->trans("User");
256
	$linkback = '<a href="'.DOL_URL_ROOT.'/user/index.php">'.$langs->trans("BackToList").'</a>';
257
	$head = user_prepare_head($fuser);
258
259
	dol_fiche_head($head, 'paidholidays', $title, 0, 'user');
260
261
    dol_banner_tab($fuser,'id',$linkback,$user->rights->user->user->lire || $user->admin);
262
263
	if (empty($conf->global->HOLIDAY_HIDE_BALANCE))
264
	{
265
	    print '<div class="underbanner clearboth"></div>';
266
267
	    print '<br>';
268
269
	    showMyBalance($holiday, $user_id);
270
	}
271
272
	dol_fiche_end();
273
}
274
else
275
{
276
    //print $num;
277
    //print count($holiday->holiday);
278
	print_barre_liste($langs->trans("ListeCP"), $page, $_SERVER["PHP_SELF"], '', $sortfield, $sortorder, "", $num, count($holiday->holiday), 'title_hrm.png', 0, '', '', $limit);
279
280
	if (empty($conf->global->HOLIDAY_HIDE_BALANCE))
281
	{
282
		dol_fiche_head('');
283
284
		showMyBalance($holiday, $user_id);
285
286
		dol_fiche_end();
287
	}
288
}
289
290
291
292
/**
293
 * Show balance of user
294
 *
295
 * @param 	Holiday	$holiday	Object $holiday
296
 * @param	int		$user_id	User id
297
 * @return	string				Html code with balance
298
 */
299
function showMyBalance($holiday, $user_id)
300
{
301
	global $conf, $langs;
302
303
	$alltypeleaves=$holiday->getTypes(1,-1);    // To have labels
304
305
	$out='';
306
	$typeleaves=$holiday->getTypes(1,1);
307
	foreach($typeleaves as $key => $val)
308
	{
309
		$nb_type = $holiday->getCPforUser($user_id, $val['rowid']);
310
		$nb_holiday += $nb_type;
0 ignored issues
show
Bug introduced by
The variable $nb_holiday 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...
311
		$out .= ' - '.$val['label'].': <strong>'.($nb_type?price2num($nb_type):0).'</strong><br>';
312
	}
313
	print $langs->trans('SoldeCPUser', round($nb_holiday,5)).'<br>';
314
	print $out;
315
}
316
317
318
319
if ($id > 0) print '<br>';
320
321
if ($sall)
322
{
323
    foreach($fieldstosearchall as $key => $val) $fieldstosearchall[$key]=$langs->trans($val);
324
    print $langs->trans("FilterOnInto", $sall) . join(', ',$fieldstosearchall);
325
}
326
327
print '<div class="div-table-responsive">';
328
print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
329
330
// Filters
331
print '<tr class="liste_titre_filter">';
332
print '<td class="liste_titre" align="left">';
333
print '<input class="flat" size="4" type="text" name="search_ref" value="'.dol_escape_htmltag($search_ref).'">';
334
print '</td>';
335
336
// Create date
337
print '<td class="liste_titre" align="center">';
338
print '<input class="flat" type="text" size="1" maxlength="2" name="month_create" value="'.dol_escape_htmltag($month_create).'">';
339
$formother->select_year($year_create,'year_create',1, $min_year, 0);
340
print '</td>';
341
342
// User
343
if ($user->rights->holiday->write_all)
344
{
345
    print '<td class="liste_titre maxwidthonsmartphone" align="left">';
346
    print $form->select_dolusers($search_employee,"search_employee",1,"",0,'','',0,0,0,'',0,'','maxwidth200');
347
    print '</td>';
348
}
349
else
350
{
351
    //print '<td class="liste_titre">&nbsp;</td>';
352
    print '<td class="liste_titre maxwidthonsmartphone" align="left">';
353
    print $form->select_dolusers($user->id,"search_employee",1,"",1,'','',0,0,0,'',0,'','maxwidth200');
354
    print '</td>';
355
}
356
357
// Approve
358
if($user->rights->holiday->write_all)
359
{
360
    print '<td class="liste_titre maxwidthonsmartphone" align="left">';
361
362
    $validator = new UserGroup($db);
363
    $excludefilter=$user->admin?'':'u.rowid <> '.$user->id;
364
    $valideurobjects = $validator->listUsersForGroup($excludefilter);
365
    $valideurarray = array();
366
    foreach($valideurobjects as $val) $valideurarray[$val->id]=$val->id;
367
    print $form->select_dolusers($search_valideur,"search_valideur",1,"",0,$valideurarray,'', 0, 0, 0, '', 0, '', 'maxwidth200');
368
    print '</td>';
369
}
370
else
371
{
372
    print '<td class="liste_titre">&nbsp;</td>';
373
}
374
375
// Type
376
print '<td class="liste_titre">';
377
$typeleaves=$holidaystatic->getTypes(1,-1);
378
$arraytypeleaves=array();
379
foreach($typeleaves as $key => $val)
380
{
381
    $labeltoshow = $val['label'];
382
    //$labeltoshow .= ($val['delay'] > 0 ? ' ('.$langs->trans("NoticePeriod").': '.$val['delay'].' '.$langs->trans("days").')':'');
383
    $arraytypeleaves[$val['rowid']]=$labeltoshow;
384
}
385
print $form->selectarray('search_type', $arraytypeleaves, (GETPOST('search_type')?GETPOST('search_type'):''), 1);
386
print '</td>';
387
388
// Duration
389
print '<td class="liste_titre">&nbsp;</td>';
390
391
// Start date
392
print '<td class="liste_titre" align="center">';
393
print '<input class="flat" type="text" size="1" maxlength="2" name="month_start" value="'.dol_escape_htmltag($month_start).'">';
394
$formother->select_year($year_start,'year_start',1, $min_year, $max_year);
395
print '</td>';
396
397
// End date
398
print '<td class="liste_titre" align="center">';
399
print '<input class="flat" type="text" size="1" maxlength="2" name="month_end" value="'.dol_escape_htmltag($month_end).'">';
400
$formother->select_year($year_end,'year_end',1, $min_year, $max_year);
401
print '</td>';
402
403
// Status
404
print '<td class="liste_titre maxwidthonsmartphone maxwidth200" align="right">';
405
$holiday->selectStatutCP($search_statut);
406
print '</td>';
407
408
// Actions
409
print '<td class="liste_titre" align="right">';
410
$searchpicto=$form->showFilterAndCheckAddButtons(0);
411
print $searchpicto;
412
print '</td>';
413
414
print "</tr>\n";
415
416
print '<tr class="liste_titre">';
417
print_liste_field_titre("Ref",$_SERVER["PHP_SELF"],"cp.rowid","",$param,'',$sortfield,$sortorder);
418
print_liste_field_titre("DateCreateCP",$_SERVER["PHP_SELF"],"cp.date_create","",$param,'align="center"',$sortfield,$sortorder);
419
print_liste_field_titre("Employee",$_SERVER["PHP_SELF"],"cp.fk_user","",$param,'',$sortfield,$sortorder);
420
print_liste_field_titre("ValidatorCP",$_SERVER["PHP_SELF"],"cp.fk_validator","",$param,'',$sortfield,$sortorder);
421
print_liste_field_titre("Type",$_SERVER["PHP_SELF"],'','',$param,'',$sortfield,$sortorder);
422
print_liste_field_titre("Duration",$_SERVER["PHP_SELF"],'','',$pram,'align="right"',$sortfield,$sortorder);
423
print_liste_field_titre("DateDebCP",$_SERVER["PHP_SELF"],"cp.date_debut","",$param,'align="center"',$sortfield,$sortorder);
424
print_liste_field_titre("DateFinCP",$_SERVER["PHP_SELF"],"cp.date_fin","",$param,'align="center"',$sortfield,$sortorder);
425
print_liste_field_titre("Status",$_SERVER["PHP_SELF"],"cp.statut","",$param,'align="right"',$sortfield,$sortorder);
426
print_liste_field_titre('',$_SERVER["PHP_SELF"],"",'',$param,'',$sortfield,$sortorder,'maxwidthsearch ');
427
print "</tr>\n";
428
429
// Lines
430
if (! empty($holiday->holiday))
431
{
432
	$userstatic = new User($db);
433
	$approbatorstatic = new User($db);
434
435
	foreach($holiday->holiday as $infos_CP)
436
	{
437
		// User
438
		$userstatic->id=$infos_CP['fk_user'];
439
		$userstatic->lastname=$infos_CP['user_lastname'];
440
		$userstatic->firstname=$infos_CP['user_firstname'];
441
		$userstatic->login=$infos_CP['user_login'];
442
		$userstatic->statut=$infos_CP['user_statut'];
443
		$userstatic->photo=$infos_CP['user_photo'];
444
445
		// Validator
446
		$approbatorstatic->id=$infos_CP['fk_validator'];
447
		$approbatorstatic->lastname=$infos_CP['validator_lastname'];
448
		$approbatorstatic->firstname=$infos_CP['validator_firstname'];
449
		$approbatorstatic->login=$infos_CP['validator_login'];
450
		$approbatorstatic->statut=$infos_CP['validator_statut'];
451
		$approbatorstatic->photo=$infos_CP['validator_photo'];
452
453
		$date = $infos_CP['date_create'];
454
455
		print '<tr class="oddeven">';
456
		print '<td>';
457
		$holidaystatic->id=$infos_CP['rowid'];
458
		$holidaystatic->ref=$infos_CP['rowid'];
459
		print $holidaystatic->getNomUrl(1);
460
		print '</td>';
461
		print '<td style="text-align: center;">'.dol_print_date($date,'day').'</td>';
462
		print '<td>'.$userstatic->getNomUrl(-1, 'leave').'</td>';
463
		print '<td>'.$approbatorstatic->getNomUrl(-1).'</td>';
464
		print '<td>';
465
		$label=$alltypeleaves[$infos_CP['fk_type']]['label'];
466
		print $label?$label:$infos_CP['fk_type'];
467
		print '</td>';
468
		print '<td align="right">';
469
		$nbopenedday=num_open_day($infos_CP['date_debut_gmt'], $infos_CP['date_fin_gmt'], 0, 1, $infos_CP['halfday']);
470
		print $nbopenedday.' '.$langs->trans('DurationDays');
471
		print '</td>';
472
		print '<td align="center">'.dol_print_date($infos_CP['date_debut'],'day').'</td>';
473
		print '<td align="center">'.dol_print_date($infos_CP['date_fin'],'day').'</td>';
474
		print '<td align="right">'.$holidaystatic->LibStatut($infos_CP['statut'],5).'</td>';
475
		print '<td></td>';
476
		print '</tr>'."\n";
477
478
	}
479
}
480
481
// Si il n'y a pas d'enregistrement suite à une recherche
482
if ($result == '2')
483
{
484
    print '<tr>';
485
    print '<td colspan="10" class="opacitymedium">'.$langs->trans('NoRecordFound').'</td>';
486
    print '</tr>';
487
}
488
489
print '</table>';
490
print '</div>';
491
492
print '</form>';
493
494
/*if ($user_id == $user->id)
495
{
496
	print '<br>';
497
	print '<div style="float: right; margin-top: 8px;">';
498
	print '<a href="./card.php?action=request" class="butAction">'.$langs->trans('AddCP').'</a>';
499
	print '</div>';
500
}*/
501
502
llxFooter();
503
504
$db->close();
505