Completed
Push — master ( 80924f...e83060 )
by Paul
04:24
created

absences_Right::getOrCreateReport()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 60
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 24
c 1
b 0
f 1
nc 6
nop 0
dl 0
loc 60
rs 8.6961
ccs 0
cts 27
cp 0
crap 42

How to fix   Long Method   

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
 * OVIDENTIA http://www.ovidentia.org                                   *
4
 ************************************************************************
5
 * Copyright (c) 2003 by CANTICO ( http://www.cantico.fr )              *
6
 *                                                                      *
7
 * This file is part of Ovidentia.                                      *
8
 *                                                                      *
9
 * Ovidentia is free software; you can redistribute it and/or modify    *
10
 * it under the terms of the GNU General Public License as published by *
11
 * the Free Software Foundation; either version 2, or (at your option)  *
12
 * any later version.													*
13
 *																		*
14
 * This program is distributed in the hope that it will be useful, but  *
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of			*
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.					*
17
 * See the  GNU General Public License for more details.				*
18
 *																		*
19
 * You should have received a copy of the GNU General Public License	*
20
 * along with this program; if not, write to the Free Software			*
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,*
22
 * USA.																	*
23
************************************************************************/
24
25
require_once dirname(__FILE__).'/record.class.php';
26
require_once dirname(__FILE__).'/type.class.php';
27
require_once dirname(__FILE__).'/rgroup.class.php';
28
29
/**
30
 * Abscence Right
31
 *
32
 * 
33
 * 
34
 * @property int 		$id_creditor
35
 * @property int 		$kind
36
 * @property string 	$date_entry
37
 * @property string 	$createdOn
38
 * @property string 	$date_begin
39
 * @property string 	$date_end
40
 * @property float 		$quantity
41
 * @property string 	$quantity_unit
42
 * @property float 		$quantity_inc_month
43
 * @property float 		$quantity_inc_max
44
 * @property string		$quantity_inc_last
45
 * @property int 		$id_type
46
 * @property string 	$description
47
 * @property string 	$active
48
 * @property string 	$cbalance
49
 * @property string 	$date_begin_valid
50
 * @property string 	$date_end_valid
51
 * @property string 	$date_end_fixed
52
 * @property string 	$date_begin_fixed
53
 * @property int 		$no_distribution
54
 * @property int 		$id_rgroup
55
 * @property string 	$earlier
56
 * @property int 		$earlier_begin_valid
57
 * @property int 		$earlier_end_valid
58
 * @property string 	$later
59
 * @property int 		$later_begin_valid
60
 * @property int 		$later_end_valid
61
 * @property int		$delay_before
62
 * @property int		$use_in_cet
63
 * @property float		$cet_quantity
64
 * @property int		$id_report_type
65
 * @property string 	$date_end_report
66
 * @property string		$description_report
67
 * @property int		$id_reported_from
68
 * @property int		$sync_status
69
 * @property string		$sync_update
70
 * @property string		$uuid
71
 * @property int		$archived
72
 * @property int		$sortkey
73
 * @property int		$require_approval
74
 * 
75
 * @property int		$quantity_alert_days
76
 * @property int		$quantity_alert_types
77
 * @property string		$quantity_alert_begin
78
 * @property string		$quantity_alert_end
79
 * 
80
 * @property int		$dynconf_types
81
 * @property string		$dynconf_begin
82
 * @property string		$dynconf_end
83
 * 
84
 * @property string     $renewal_uid
85
 * @property int        $renewal_parent     Right ID of the right in the previous period
86
 * 
87
 */
88
class absences_Right extends absences_Record implements absences_RightSort 
89
{
90
	/**
91
	 * Genre du droit default
92
	 * @var int
93
	 */
94
	const REGULAR 	= 1;
95
	
96
	/**
97
	 * Genre du droit a date fixe
98
	 * @var int
99
	 */
100
	const FIXED		= 2;
101
	
102
	/**
103
	 * Genre du droit compte epargne temps
104
	 * @var int
105
	 */
106
	const CET		= 4;
107
	
108
	/**
109
	 * Ajout periodique de solde sur le droit
110
	 * @var int
111
	 */
112
	const INCREMENT = 8;
113
114
	
115
	/**
116
	 * Droit a recuperation (cree apres une approbation de declaration de jours travailles donnant droit a recuperation)
117
	 * @var int
118
	 */
119
	const RECOVERY	= 16;
120
	
121
	/**
122
	 * Droit de report (cree automatiquement)
123
	 * @var int
124
	 */
125
	const REPORT	= 32;
126
	
127
	
128
	
129
	
130
	
131
	const SYNC_CLIENT 		= 1;
132
	const SYNC_CLIENT_END 	= 2;
133
	const SYNC_CLIENT_ERROR	= 3;
134
	
135
	const SYNC_SERVER 		= 8;
136
	
137
	
138
	
139
	
140
	/**
141
	 * 
142
	 * @var absences_Type
143
	 */
144
	private $type;
145
	
146
	
147
	/**
148
	 * @var absences_Rgroup
149
	 */
150
	private $rgroup;
151
	
152
	
153
	/**
154
	 * 
155
	 * @var absences_RightRule
156
	 */
157
	private $right_rule;
158
	
159
	/**
160
	 * 
161
	 * @var absences_RightCet
162
	 */
163
	private $right_cet;
164
	
165
	
166
	/**
167
	 * List of unsaved inperiod rules
168
	 * @var array
169
	 */
170
	private $inperiod = array();
171
	
172
	/**
173
	 *
174
	 * @var absences_Right
175
	 */
176
	private $reported_from;
177
	
178
	/**
179
	 *
180
	 * @var absences_Right
181
	 */
182
	private $report;
183
	
184
	
185
	/**
186
	 * 
187
	 * @var string
188
	 */
189
	private $_uuid;
190
	
191
	
192 84
	public function __construct($id)
193
	{
194 84
		$this->id = $id;
195 84
	}
196
	
197
	
198
	/**
199
	 * 
200
	 * @param string $uuid
201
	 * @return absences_Right
202
	 */
203
	public static function getByUuid($uuid)
204
	{
205
		$right = new absences_Right(null);
206
		$right->_uuid = $uuid;
207
		
208
		return $right;
209
	}
210
	
211
	/**
212
	 * Table row as an array
213
	 * @return array
214
	 */
215 72
	public function getRow()
216
	{
217 72
		if (null === $this->row)
218 72
		{
219 46
			global $babDB;
220
			
221
			$query = '
222
				SELECT 
223
					r.*
224
				FROM absences_rights r	
225 46
				WHERE ';
226
			
227 46
			if (isset($this->id))
228 46
			{
229 46
				$query .= 'r.id='.$babDB->quote($this->id);
230 46
			} elseif (isset($this->_uuid))
231
			{
232
				$query .= 'r.uuid='.$babDB->quote($this->_uuid);
233
			} else {
234 9
				return false;
235
			}
236
			
237 46
			$res = $babDB->db_query($query);
238
			
239 46
			$this->setRow($babDB->db_fetch_assoc($res));
240 46
		}
241
		
242 72
		return $this->row;
243
	}
244
245
	
246
	/**
247
	 * La nature du droit
248
	 * @return int
249
	 */
250 14
	public function getKind()
251
	{
252 14
		return (int) $this->kind;
253
	}
254
	
255
	/**
256
	 * @return string
257
	 */
258 9
	public function getKindLabel()
259 9
	{
260
		$arr = absences_kinds();
261
		$kind = $this->getKind();
262
		
263
		if (!isset($arr[$kind])) {
264
		    return '';
265
		}
266
		
267
		return $arr[$kind];
268 9
	}
269
	
270
	/**
271
	 * @return int
272
	 */
273 9
	public function getSyncStatus()
274 9
	{
275
		return (int) $this->sync_status;
276
	}
277
	
278
	/**
279
	 * @return string
280
	 */
281
	public function getSyncStatusLabel()
282
	{
283
		$arr = absences_syncStatus();
284
		return $arr[$this->getSyncStatus()];
285
	}
286
	
287
	
288
	/**
289
	 *
290
	 * @param absences_Type $type
291
	 * @return absences_Right
292
	 */
293 9
	public function setType(absences_Type $type)
294
	{
295
		$this->type = $type;
296
		return $this;
297
	}
298
	
299
	
300
	/**
301
	 * @return absences_Type
302
	 */
303 10
	public function getType()
304
	{
305 1
		if (!isset($this->type))
306 1
		{
307 10
			$row = $this->getRow();
308 1
			$this->type = new absences_Type($row['id_type']);
309 10
		}
310
	
311 1
		return $this->type;
312
	}
313
	
314
	
315
	
316
	/**
317
	 *
318
	 * @param absences_Rgroup $rgroup
319
	 * @return absences_Right
320
	 */
321
	public function setRgroup(absences_Rgroup $rgroup)
322
	{
323
		$this->rgroup = $rgroup;
324
		return $this;
325
	}
326
	
327
	
328
	/**
329
	 * @return absences_Rgroup
330
	 */
331 9 View Code Duplication
	public function getRgroup()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
332
	{
333
		if (!isset($this->rgroup))
334
		{
335
			$row = $this->getRow();
336
			if (empty($row['id_rgroup']))
337
			{
338
				return null;
339
			}
340
			$this->rgroup = new absences_Rgroup($row['id_rgroup']);
341
		}
342
	
343 9
		return $this->rgroup;
344 9
	}
345
	
346
	/**
347
	 * 
348
	 * @return string
349
	 */
350 9 View Code Duplication
	public function getRgroupLabel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
351
	{
352
		$rgroup = $this->getRgroup();
353
		
354
		if (null === $rgroup || !$rgroup->getRow())
355
		{
356
			return null;
357
		}
358
		
359
		return $rgroup->name;
360
	}
361
	
362
	
363
	
364
	/**
365
	 *
366
	 * @return int
367
	 */
368 View Code Duplication
	public function getRgroupSortkey()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
369
	{
370
		$rgroup = $this->getRgroup();
371
	
372
		if (null === $rgroup || !$rgroup->getRow())
373
		{
374
			return null;
375
		}
376
	
377
		return $rgroup->sortkey;
378
	}
379
	
380
	
381
	/**
382
	 * @return string
383
	 */
384 View Code Duplication
	public function getUnitLabel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
385
	{
386
		switch($this->quantity_unit)
387
		{
388
			case 'D':
389
				return absences_translate('day(s)');
390
	
391
			case 'H':
392
				return absences_translate('hour(s)');
393
		}
394
	
395
		return '';
396
	}
397
	
398
	/**
399
	 * Start date year
400
	 * @return int
401
	 */
402 View Code Duplication
	public function getYear()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
403
	{
404
		$year = (int) substr($this->date_begin, 0, 4);
405
		
406
		if (0 === $year)
407
		{
408
			return null;
409
		}
410
		
411
		return $year;
412
	}
413
	
414
	
415
	/**
416
	 * 
417
	 * @param absences_RightRule $RightRule
418
	 */
419
	public function setRightRule(absences_RightRule $RightRule)
420
	{
421
		$this->rightrule = $RightRule;
0 ignored issues
show
Bug introduced by
The property rightrule does not seem to exist. Did you mean right_rule?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
422
		return $this;
423
	}
424
	
425
	/**
426
	 * @return absences_RightRule
427
	 */
428 10 View Code Duplication
	public function getRightRule()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
429
	{
430 10
		if (!isset($this->right_rule))
431 10
		{
432 10
			require_once dirname(__FILE__).'/right_rule.class.php';
433 10
			$this->right_rule = absences_RightRule::getFromRight($this->id);	
434 10
		}
435 10
		return $this->right_rule;
436
	}
437
	
438
	
439
	
440
	
441
	/**
442
	 *
443
	 * @param absences_RightCet $RightCet
444
	 */
445
	public function setRightCet(absences_RightCet $RightCet)
446
	{
447
		$this->right_cet = $RightCet;
448
		return $this;
449
	}
450
	
451
	/**
452
	 * @return absences_RightCet
453
	 */
454 View Code Duplication
	public function getRightCet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
455
	{
456
		if (!isset($this->right_cet))
457
		{
458
			require_once dirname(__FILE__).'/right_cet.class.php';
459
			$this->right_cet = absences_RightCet::getFromRight($this->id);
460
		}
461
		return $this->right_cet;
462
	}
463
	
464
	
465
	
466
	/**
467
	 * 
468
	 * @param absences_RightInPeriod $inperiod
469
	 */
470
	public function addInPeriod(absences_RightInPeriod $inperiod)
471
	{
472
		$this->inperiod[] = $inperiod;
473
		$inperiod->setRight($this);
474
		return $this;
475
	}
476
	
477
	
478
	/**
479
	 * @return multitype:absences_RightInPeriod
0 ignored issues
show
Documentation introduced by
The doc-type multitype:absences_RightInPeriod could not be parsed: Unknown type name "multitype:absences_RightInPeriod" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
480
	 */
481
	public function getInperiodRules()
482
	{
483
		return $this->inperiod;
484
	}
485
	
486
	
487
	/**
488
	 * Get the source right of report or false if the right is not a report
489
	 * @return absences_Right | false
490
	 */
491
	public function getReportedFrom()
492
	{
493
		if (!isset($this->reported_from))
494
		{
495
			if (self::REPORT !== $this->getKind() || empty($this->id_reported_from))
496
			{
497
				$this->reported_from = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object<absences_Right> of property $reported_from.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
498
			} else {
499
			
500
				$this->reported_from = new absences_Right($this->id_reported_from);
501
			}
502
		}
503
		
504
		return $this->reported_from;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->reported_from; of type false|absences_Right adds false to the return on line 504 which is incompatible with the return type documented by absences_Right::getReportedFrom of type absences_Right. It seems like you forgot to handle an error condition.
Loading history...
505
	}
506
	
507
	
508
	/**
509
	 * Get the report right if exists
510
	 * @return absences_Right | false
511
	 */
512
	public function getReport()
513
	{
514
		if (!isset($this->report))
515
		{
516
			global $babDB;
517
			
518
			$res = $babDB->db_query('SELECT * FROM absences_rights WHERE id_reported_from='.$babDB->quote($this->id));
519
			if (0 === $babDB->db_num_rows($res))
520
			{
521
				$this->report = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object<absences_Right> of property $report.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
522
			} else {
523
				
524
				$row = $babDB->db_fetch_assoc($res);
525
				$right = new absences_Right($row['id']);
526
				$right->setRow($row);
527
				
528
				$this->report = $right;
529
			}
530
		}
531
		
532
		return $this->report;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->report; of type false|absences_Right adds false to the return on line 532 which is incompatible with the return type documented by absences_Right::getReport of type absences_Right. It seems like you forgot to handle an error condition.
Loading history...
533
	}
534
	
535
	
536
	/**
537
	 * Get the report right or create it if not exists
538
	 * @return absences_Right | null
539
	 */
540
	public function getOrCreateReport()
541
	{
542
		$right = $this->getReport();
543
		
544
		if (false !== $right)
545
		{
546
			return $right;
547
		}
548
		
549
		
550
		if (empty($this->date_end_report) || '0000-00-00'===$this->date_end_report)
551
		{
552
			return null;
553
		}
554
		
555
		
556
		$description = empty($this->description_report) ? $this->description : $this->description_report;
557
		$id_type = empty($this->id_report_type) ? $this->id_type : $this->id_report_type;
558
		
559
		global $babDB;
560
		
561
		$babDB->db_query('INSERT INTO absences_rights (
562
				kind, 
563
				description, 
564
		        createdOn,
565
				date_entry, 
566
				date_begin, 
567
				date_end,
568
				date_begin_valid,
569
				date_end_valid,
570
				quantity_unit,
571
				id_type,
572
				cbalance,
573
				use_in_cet,
574
				id_reported_from
575
			) 
576
				VALUES 
577
			(
578
				'.$babDB->quote(self::REPORT).',
579
				'.$babDB->quote($description).',
580
		        NOW(),
581
				NOW(),
582
				'.$babDB->quote($this->date_begin).',
583
				'.$babDB->quote($this->date_end_report).',
584
				'.$babDB->quote($this->date_begin_valid).',
585
				'.$babDB->quote($this->date_end_report).',
586
				'.$babDB->quote($this->quantity_unit).',
587
				'.$babDB->quote($id_type).',
588
				'.$babDB->quote('N').',
589
				'.$babDB->quote('0').',
590
				'.$babDB->quote($this->id).'
591
			)');
592
		
593
		
594
		$id_report = $babDB->db_insert_id();
595
		
596
		$this->report = new absences_Right($id_report);
597
		
598
		return $this->report;
599
	}
600
	
601
	
602
	
603
	/**
604
	 * Tester si le droit est disponible dans le mois en cours (en fonction de la periode de conges demandee)
605
	 * si cette methode renvoi true, il n'y a pas de fin de validite dans le mois en cours sauf si validoperlap est actif
606
	 *
607
	 * si on ce base sur cette methode pour determiner si un droit doit etre incremente dans le mois
608
	 *  - le droit sera incremente si pas de fin de validite dans le mois
609
	 *  - le droit sera incremente si le chevauchement est active et que la fin de validite est dans le mois
610
	 *
611
	 *
612
	 * @return bool
613
	 */
614 10 View Code Duplication
	public function isAccessibleOnMonth()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
615
	{
616 10
		require_once $GLOBALS['babInstallPath'].'utilit/dateTime.php';
617 10
		$begin = new BAB_DateTime(date('Y'), date('n'), 1);
618 10
		$end = new BAB_DateTime(date('Y'), date('n'), date('t'), 23, 59, 59);
619
		
620 10
		$rightRule = $this->getRightRule();
621
	
622 10
		return $rightRule->isAccessibleOnPeriod($begin->getTimeStamp(), $end->getTimeStamp());
623
	}
624
	
625
	
626
	
627 9 View Code Duplication
	protected function saveQuantityIncMonth($quantity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
628
	{
629 9
	    require_once dirname(__FILE__).'/increment_right.class.php';
630
	
631 9
	    $increment = new absences_IncrementRight();
632 9
	    $increment->id_right = $this->id;
633 9
	    $increment->quantity = $quantity;
634 9
	    $increment->createdOn = date('Y-m-d H:i:s');
635 9
	    $increment->monthkey = date('Ym');
636
	
637 9
	    $increment->saveOrUpdate();
638 9
	}
639
	
640
	
641
	
642
	
643
	/**
644
	 * Increment quantity for the month if not allready done
645
	 * return true if quantity has been modified
646
	 *
647
	 * @param LibTimer_eventHourly $event optional event if the action is done via a background task
648
	 *
649
	 * @return bool
650
	 */
651 14
	public function monthlyQuantityUpdate(LibTimer_eventHourly $event = null)
652
	{
653 14
	    if ($this->getKind() !== absences_Right::INCREMENT || $this->quantity_inc_month<='0.00')
654 14
	    {
655
	        return false;
656
	    }
657
658 14
	    require_once $GLOBALS['babInstallPath'].'utilit/dateTime.php';
659 14
	    $limit = new BAB_DateTime(date('Y'), date('n'), 1);
660
661 14
	    if ($this->quantity_inc_last >= $limit->getIsoDateTime())
662 14
	    {
663 3
	        return false;
664
	    }
665
666 11
	    $quantity = (float) $this->quantity;
667 11
	    $quantity += $this->getIncrementQuantity();
668
	    
669 11
	    if ($this->quantity_inc_last !== '0000-00-00 00:00:00' && $this->isAccessibleOnMonth())
670 11
	    {
671
	        // Do not increment the first month
672
	        // Do not increment if the full month is not totally available
673
	        
674 10
	        $quantity_inc_month = $this->getQuantityIncMonth($quantity);
675
	        
676 10
	        if (0 !== (int) round(100 * $quantity_inc_month)) {
677
	        
678 9
    	        $quantity += $quantity_inc_month;
679
    	        
680 9
    	        $this->saveQuantityIncMonth($quantity_inc_month);
681
    
682 9
    	        $message = sprintf(
683 9
    	            absences_translate('The quantity of right "%s" has been modified from %s to %s by the monthly update'), 
684 9
                    $this->description,
685 9
                    $this->quantity,
686
                    $quantity
687 9
    	        );
688
    	        
689 9
    	        $this->addMovement($message, '', 0);
690
    	        
691 9
    	        if (isset($event)) {
692
    	            $event->log('absences', $message);
693
    	        }
694 9
	        }
695 10
	    }
696
	    	
697 11
	    global $babDB;
698
	    	
699 11
	    $babDB->db_query('UPDATE absences_rights SET 
700 11
				quantity_inc_last='.$babDB->quote($limit->getIsoDateTime()).'
701 11
			WHERE id='.$babDB->quote($this->id));
702
	    
703 11
	    if ($this->quantity_inc_last !== '0000-00-00 00:00:00') {
704
    	    // update the modified quantites but not the first month
705
    	    // agentRight have their own isAccessibleOnMonth method because the period can be different
706
    	    
707 10
    	    $modified = $this->getAgentRightIterator();
708 10
    	    $modified->modified_quantity = true;
709
    	    
710 10
    	    foreach ($modified as $agentRight)
711
    	    {
712
    	        /*@var $agentRight absences_AgentRight */
713 8
    	        $agentRight->monthlyQuantityUpdate($event);
714 10
    	    }
715
    	    
716
    	    
717
    	    // UPDATE the current instance
718 10
    	    $this->quantity_inc_last = $limit->getIsoDateTime();
719
    	    
720 10
    	    return true;
721
	    }
722
	    
723
	    // UPDATE the current instance
724 1
	    $this->quantity_inc_last = $limit->getIsoDateTime();
725 1
	    return false;
726
	}
727
	
728
	
729
	
730
	
731
	/**
732
	 * Tester si le droit est accessible en fonction de la periode de validite du droit
733
	 * @see absences_AgentRight::isAccessibleByValidityPeriod()
734
	 * @return bool
735
	 */
736
	public function isAccessibleByValidityPeriod()
737
	{
738
		$access= true;
739
		
740 View Code Duplication
		if( $this->date_begin_valid != '0000-00-00' && (bab_mktime($this->date_begin_valid." 00:00:00") > mktime())){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
741
			$access= false;
742
		}
743
		
744 View Code Duplication
		if( $this->date_end_valid != '0000-00-00' && (bab_mktime($this->date_end_valid." 23:59:59") < mktime())){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
745
			$access= false;
746
		}
747
		
748
		return $access;
749
	}
750
	
751
	/**
752
	 * Test if the vacation right is visible to the end user by testing properties of fixed vacation right
753
	 * if the right is not a fixed vacation right, this method return true
754
	 * 
755
	 * @return bool
756
	 */
757
	public function isAccessibleIfFixed()
758
	{
759
		if ($this->kind != self::FIXED)
760
		{
761
			return true;
762
		}
763
		
764
		// dont't display vacations with fixed dates that are gone
765
		if( $this->date_end_fixed != '0000-00-00 00:00:00' && (bab_mktime($this->date_end_fixed) < mktime())){
766
			return false;
767
		}
768
		
769
		return true;
770
	}
771
	
772
	
773
	/**
774
	 * If the right is displayed as available in the manager list
775
	 * Test if the right is accessible for the user to display information in exported file
776
	 * pour l'export de l'agent et pour l'export des droits
777
	 * 
778
	 * @return bool
779
	 */
780
	public function isAvailable()
781
	{
782
		return ('Y' == $this->active && $this->isAccessibleByValidityPeriod() && $this->isAccessibleIfFixed());
783
	}
784
	
785
	
786
	public function getStatus()
787
	{
788
		if ($this->archived)
789
		{
790
			return absences_translate('Archived');
791
		}
792
		
793
		
794
		if ('Y' == $this->active)
795
		{
796
			return absences_translate('Active');
797
		}
798
		
799
		if ('N' == $this->active)
800
		{
801
			return absences_translate('Disabled for appliquant, usable by managers and delegated managers');
802
		}
803
	}
804
	
805
	
806
	/**
807
	 * Get a period description in text
808
	 * @param string $begin
809
	 * @param string $end
810
	 */
811
	private function periodText($begin, $end)
812
	{
813
		if ('0000-00-00'=== $begin && '0000-00-00' === $end) {
814
			return null;
815
		}
816
		
817
		$d1 = bab_shortDate(bab_mktime($begin), false);
818
		$d2 = bab_shortDate(bab_mktime($end), false);
819
		
820
		if (!$d1) {
821
		    $d1 = absences_translate('??/??/????');
822
		}
823
		
824
		if (!$d2) {
825
		    $d2 = absences_translate('??/??/????');
826
		}
827
		
828
		
829
		return sprintf(absences_translate('the %s and the %s'), $d1, $d2);
830
	}
831
	
832
	
833
	/**
834
	 * 
835
	 * @param string $name
836
	 * @return string
837
	 */
838
	private function getDirFieldLabel($name)
839
	{
840
		$fields = bab_getDirEntry(BAB_REGISTERED_GROUP, BAB_DIR_ENTRY_ID_GROUP);
841
		return $fields[$name]['name'];
842
	}
843
	
844
	
845
	
846
	
847
	/**
848
	 * Get access conditions as a string
849
	 * of null if there are no access conditions
850
	 * @return string
851
	 */
852
	public function getAccessConditions()
853
	{
854
		global $babDB;
855
		$conditions = array();
856
		
857
		
858
		// Disponibilite en fonction de la date de saisie de la demande de conges
859
		if ('0000-00-00' !== $this->date_begin_valid)
860
		{
861
			$period = $this->periodText($this->date_begin_valid, $this->date_end_valid);
862
			$conditions[] = sprintf(absences_translate('To create a vacation request at a date beetween %s.'), $period);
863
		}
864
		
865
		
866
		$rightRules = $this->getRightRule();
867
		
868
		// Disponibilite en fonction de la periode de conges demandee
869
		$res = $rightRules->getInPeriodRes();
870
		if ($babDB->db_num_rows($res) > 0)
871
		{
872
			$s = array();
873
			
874
			while ($arr = $babDB->db_fetch_assoc($res))
875
			{
876
				$d1 = bab_shortDate(bab_mktime($arr['period_start']), false);
877
				$d2 = bab_shortDate(bab_mktime($arr['period_end']), false);
878
				
879
				
880
				switch($arr['right_inperiod'])
881
				{
882
					case 1: $subcond = sprintf(absences_translate('beetween the %s and the %s'), $d1, $d2);	break;
883
					case 2: $subcond = sprintf(absences_translate('before the %s or after the %s'), $d1, $d2);	break;
884
				}
885
				
886
				$s[] = $subcond;
0 ignored issues
show
Bug introduced by
The variable $subcond 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...
887
			}
888
			
889
			$conditions[] = absences_translate('To apply for a vacation period').' '.implode(', ', $s).'.';
890
		}
891
		
892
		// Attribution du droit en fonction des jours demandes et valides
893
894
		if ($rightRules->getRow() && $rightRules->trigger_nbdays_min && $rightRules->trigger_nbdays_max)
895
		{
896
			$text = sprintf(absences_translate('To have requested at least %d days but less than %d days'), $rightRules->trigger_nbdays_min, $rightRules->trigger_nbdays_max);
897
			if ($rightRules->trigger_type)
898
			{
899
				$text .= ' '. sprintf(absences_translate('of type %s'), $rightRules->getType()->name);
900
			}
901
			
902
			$periods = array();
903
			$p1 = $this->periodText($rightRules->trigger_p1_begin, $rightRules->trigger_p1_end);
904
			$p2 = $this->periodText($rightRules->trigger_p2_begin, $rightRules->trigger_p2_end);
905
			
906
			if ($p1) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $p1 of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
907
				$periods[] = sprintf(absences_translate('beetween %s'), $p1);
908
			}
909
			
910
			if ($p2) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $p2 of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
911
				$periods[] = sprintf(absences_translate('beetween %s'), $p2);
912
			}
913
			$text .= ' '.implode(' '.absences_translate('or').' ', $periods).'.';
914
			
915
			$conditions[] = $text;
916
		}
917
		
918
		// Disponibilite en fonction d'une date anterieure
919
		
920 View Code Duplication
		if ($this->earlier)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
921
		{
922
			$f = $this->getDirFieldLabel($this->earlier);
923
			$cond = array();
924
			
925
			if ($this->earlier_begin_valid)
926
			{
927
				$cond[] = sprintf(absences_translate('%d years later "%s"'), $this->earlier_begin_valid, $f);
928
			} else {
929
				$cond[] = sprintf(absences_translate('after "%s"'), $f);
930
			}
931
			
932
			if ($this->earlier_end_valid)
933
			{
934
				$cond[] = sprintf(absences_translate('in the %d years next to "%s"'), $this->earlier_end_valid, $f);
935
			} 
936
			
937
			$conditions[] = absences_translate('To request a period').' '.implode(' '.absences_translate('and').' ', $cond).'.';
938
		}
939
		
940
		
941
		// Disponibilite en fonction d'une date posterieure
942
		
943 View Code Duplication
		if ($this->later)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
944
		{
945
			$f = $this->getDirFieldLabel($this->later);
946
			$cond = array();
947
			
948
			if ($this->later_begin_valid)
949
			{
950
				$cond[] = sprintf(absences_translate('in the %d years preceding "%s"'), $this->later_begin_valid, $f);
951
			} else {
952
				$cond[] = sprintf(absences_translate('after "%s"'), $f);
953
			}
954
			
955
			if ($this->later_end_valid)
956
			{
957
				$cond[] = sprintf(absences_translate('at least %d years before the "%s"'), $this->later_end_valid, $f);
958
			}
959
			
960
			$conditions[] = absences_translate('To request a period').' '.implode(' '.absences_translate('and').' ', $cond).'.';
961
		}
962
		
963
		if($this->delay_before)
964
		{
965
			$conditions[] = sprintf(absences_translate('To request a period at least %s days before.'), $this->delay_before);
966
		}
967
		
968
		
969
		if (0 ===count($conditions))
970
		{
971
			return null;
972
		}
973
		
974
		
975
		$text = absences_translate('The condition to access this vacation right is', 'The conditions to access this vacation right are:', count($conditions));
976
		
977
		foreach($conditions as $c)
978
		{
979
			$text .= " \n".$c;
980
		}
981
		
982
		
983
		return $text;
984
		
985
	}
986
	
987
	
988
	
989
	
990
	/**
991
	 * Agents associated to vacation right
992
	 * @return absences_AgentIterator
993
	 */
994
	public function getAgentIterator()
995
	{
996
		require_once dirname(__FILE__).'/agent.class.php';
997
		$I = new absences_AgentIterator;
998
		$I->setRight($this);
999
		
1000
		return $I;
1001
	}
1002
	
1003
	/**
1004
	 * Collections associated to vacation right
1005
	 * @return absences_CollectionIterator
1006
	 */
1007
	public function getCollectionIterator()
1008
	{
1009
		require_once dirname(__FILE__).'/collection.class.php';
1010
		$I = new absences_CollectionIterator;
1011
		$I->setRight($this);
1012
		
1013
		return $I;
1014
	}
1015
	
1016
	
1017
	/**
1018
	 * @return absences_AgentRightStatIterator
1019
	 */
1020 10
	public function getAgentRightIterator()
1021
	{
1022 10
		require_once dirname(__FILE__).'/agent_right.class.php';
1023 10
		$I = new absences_AgentRightStatIterator;
1024 10
		$I->setRight($this);
1025
		
1026 10
		return $I;
1027
	}
1028
	
1029
	
1030
	/**
1031
	 * determiner le nombre de d'agent avec ce droit non solde
1032
	 * @return int
1033
	 */
1034
	public function getAgentUsage()
1035
	{
1036
		$last_available = 0;
1037
		$last_waiting = 0;
1038
		$last_agents = 0;
1039
		$last_agents_iu = 0;
1040
		
1041
		
1042
		foreach($this->getAgentRightIterator() as $agent_right)
1043
		{
1044
			/*@var $agent_right absences_AgentRight */
1045
			
1046
			$waiting = $agent_right->getWaitingQuantity();
1047
			if (0 !== (int) round(100 * $waiting))
1048
			{
1049
				$last_agents_iu++;
1050
			}
1051
			
1052
			$available = $agent_right->getAvailableQuantity();
1053
			if (0 !== (int) round(100 * $available))
1054
			{
1055
				$last_agents++;
1056
				$last_agents_iu++;
1057
			}
1058
			
1059
			
1060
			
1061
			
1062
			$last_waiting += $waiting;
1063
			$last_available += $available; 
1064
				
1065
		}
1066
		
1067
		
1068
		
1069
		return array(
1070
			'available' => absences_quantity($last_available, $this->quantity_unit),
1071
			'waiting' => absences_quantity($last_waiting, $this->quantity_unit),
1072
			'agents' => $last_agents,
1073
			'agents_iu' => $last_agents_iu
1074
		);
1075
	}
1076
	
1077
	
1078
	
1079
	
1080
	
1081
	/**
1082
	 *
1083
	 * @param string $message	Generated message
1084
	 * @param string $comment	Author comment
1085
	 */
1086 9 View Code Duplication
	public function addMovement($message, $comment = '', $id_author = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1087
	{
1088 9
		require_once dirname(__FILE__).'/movement.class.php';
1089
	
1090 9
		$movement = new absences_Movement();
1091 9
		$movement->message = $message;
1092 9
		$movement->comment = $comment;
1093
		
1094 9
		if (isset($id_author))
1095 9
		{
1096 9
			$movement->id_author = $id_author;
1097 9
		}
1098
		
1099 9
		$movement->setRight($this);
1100 9
		$movement->save();
1101 9
	}
1102
	
1103
	
1104
	
1105
	/**
1106
	 * Tester si tout les beneficiaires du droit on consome tout le solde
1107
	 * @return bool
1108
	 */
1109
	public function isResulted()
1110
	{
1111
		if ($this->cbalance === 'N')
1112
		{
1113
			// soldes negatifs autorises
1114
			// considere comme solde si le droit n'est pas dispo
1115
			return !$this->isAvailable();
1116
		}
1117
		
1118
		
1119
		foreach($this->getAgentRightIterator() as $agent_right)
1120
		{
1121
			if ($agent_right->getAvailableQuantity() > 0)
1122
			{
1123
				// tout n'a pas ete consome
1124
				return false;
1125
			}
1126
		}
1127
		
1128
		// tout a ete consome et le droit n'est pas dispo
1129
		return !$this->isAvailable();
1130
	}
1131
	
1132
	
1133
	
1134
	
1135
	/**
1136
	 * Archiver le droit
1137
	 */
1138
	public function archive()
1139
	{
1140
		global $babDB;
1141
		
1142
		$babDB->db_query('UPDATE absences_rights SET archived='.$babDB->quote(1).' WHERE id='.$babDB->quote($this->id));
1143
	}
1144
	
1145
	
1146
	/**
1147
	 * Method used with bab_Sort
1148
	 */
1149
	public function getSortKey()
1150
	{
1151
		return $this->sortkey;
1152
	}
1153
	
1154
	public function setSortKey($i)
1155
	{
1156
		global $babDB;
1157
		$babDB->db_query('UPDATE absences_rights SET sortkey='.$babDB->quote($i).' WHERE id='.$babDB->quote($this->id));
1158
	}
1159
	
1160
	public function getSortLabel()
1161
	{
1162
		return $this->description;
1163
	}
1164
	
1165
	
1166
	public function getIconClassName()
1167
	{
1168
		bab_functionality::includeOriginal('Icons');
1169
		return Func_Icons::ACTIONS_ARROW_RIGHT;
1170
	}
1171
	
1172
	
1173
	
1174
	
1175
	/**
1176
	 * Insert new right
1177
	 */
1178 8
	public function insert()
1179
	{
1180
		require_once $GLOBALS['babInstallPath'].'utilit/uuid.php';
1181
		global $babDB;
1182
		
1183
		$babDB->db_query("INSERT INTO absences_rights 
1184
			(
1185
				id_creditor,
1186
				kind,
1187
		        createdOn,
1188
				date_entry,
1189
				date_begin,
1190
				date_end,
1191
				quantity,
1192
				quantity_unit,
1193
				quantity_inc_month,
1194
				quantity_inc_max,
1195
				id_type,
1196
				description,
1197
				active,
1198
				cbalance,
1199
				date_begin_valid,
1200
				date_end_valid,
1201
				date_end_fixed,
1202
				date_begin_fixed,
1203
				no_distribution,
1204
				use_in_cet,
1205
				cet_quantity,
1206
				id_rgroup,  
1207
				earlier,
1208
				earlier_begin_valid,
1209
				earlier_end_valid,  
1210
				later,
1211
				later_begin_valid,
1212
				later_end_valid,
1213
				delay_before,
1214
				sync_status,
1215
				sync_update,
1216
				uuid,
1217
				archived
1218
			) 
1219 8
		VALUES 
1220
			(
1221
				".$babDB->quote(bab_getUserId()).",
1222
				".$babDB->quote($this->getKind()).",
1223
		        NOW(),
1224
				NOW(),
1225
				".$babDB->quote($this->date_begin).",
1226
				".$babDB->quote($this->date_end).",
1227
				".$babDB->quote($this->quantity).",
1228
				".$babDB->quote($this->quantity_unit).",
1229
				".$babDB->quote($this->quantity_inc_month).",
1230 8
				".$babDB->quote($this->quantity_inc_max).",
1231
				".$babDB->quote($this->id_type).",
1232
				".$babDB->quote($this->description).",
1233
				".$babDB->quote($this->active).",
1234
				".$babDB->quote($this->cbalance).",
1235
				".$babDB->quote($this->date_begin_valid).",
1236
				".$babDB->quote($this->date_end_valid).",
1237
				".$babDB->quote($this->date_end_fixed).",
1238
				".$babDB->quote($this->date_begin_fixed).",
1239
				".$babDB->quote($this->no_distribution).",
1240
				".$babDB->quote($this->use_in_cet).",
1241
				".$babDB->quote($this->cet_quantity).",
1242
				".$babDB->quote($this->id_rgroup).",
1243
				".$babDB->quote($this->earlier).",
1244
				".$babDB->quote($this->earlier_begin_valid).",
1245 5
				".$babDB->quote($this->earlier_end_valid).",
1246
				".$babDB->quote($this->later).",
1247
				".$babDB->quote($this->later_begin_valid).",
1248
				".$babDB->quote($this->later_end_valid).",
1249
				".$babDB->quote($this->delay_before).",
1250
				".$babDB->quote($this->sync_status).",
1251
				".$babDB->quote($this->sync_update).",
1252
				".$babDB->quote($this->uuid).",
1253
				".$babDB->quote($this->archived)."
1254
			)
1255
		");
1256
		
1257
		
1258
		$this->id = $babDB->db_insert_id();
1259
	}
1260
	
1261
	
1262
	/**
1263
	 * Update by UUID
1264
	 */
1265
	public function update()
1266
	{
1267
		global $babDB;
1268
		
1269
		$query = 'UPDATE absences_rights SET 
1270
				kind='.$babDB->quote($this->kind).',
1271
				date_entry=NOW(),
1272
				date_begin='.$babDB->quote($this->date_begin).',
1273
				date_end='.$babDB->quote($this->date_end).',
1274
				quantity='.$babDB->quote($this->quantity).',
1275
				quantity_unit='.$babDB->quote($this->quantity_unit).',
1276
				quantity_inc_month='.$babDB->quote($this->quantity_inc_month).',
1277
				quantity_inc_max='.$babDB->quote($this->quantity_inc_max).',
1278
				description='.$babDB->quote($this->description).',
1279
				active='.$babDB->quote($this->active).',
1280
				cbalance='.$babDB->quote($this->cbalance).',
1281
				date_begin_valid='.$babDB->quote($this->date_begin_valid).',
1282
				date_end_valid='.$babDB->quote($this->date_end_valid).',
1283
				date_end_fixed='.$babDB->quote($this->date_end_fixed).',
1284
				date_begin_fixed='.$babDB->quote($this->date_begin_fixed).',
1285
				no_distribution='.$babDB->quote($this->no_distribution).',
1286
				use_in_cet='.$babDB->quote($this->use_in_cet).',
1287
				cet_quantity='.$babDB->quote($this->cet_quantity).',
1288
				earlier='.$babDB->quote($this->earlier).',
1289
				earlier_begin_valid='.$babDB->quote($this->earlier_begin_valid).',
1290
				earlier_end_valid='.$babDB->quote($this->earlier_end_valid).',
1291
				later='.$babDB->quote($this->later).',
1292
				later_begin_valid='.$babDB->quote($this->later_begin_valid).',
1293
				later_end_valid='.$babDB->quote($this->later_end_valid).',
1294
				delay_before='.$babDB->quote($this->delay_before).',
1295
				sync_status='.$babDB->quote($this->sync_status).',
1296
				sync_update='.$babDB->quote($this->sync_update).',
1297
				archived='.$babDB->quote($this->archived).' 
1298
			WHERE uuid='.$babDB->quote($this->uuid).' 
1299
		';
1300
		
1301
		bab_debug($query);
1302
		
1303
		$babDB->db_query($query);
1304
	}
1305
	
1306
	
1307
	/**
1308
	 * 
1309
	 * @return string
1310
	 */
1311
	public function getQuantityAlertTypes()
1312
	{
1313
		$types = explode(',',$this->quantity_alert_types);
1314
		
1315
		if (empty($types))
1316
		{
1317
			return '';
1318
		}
1319
		
1320
		global $babDB;
1321
		
1322
		$res = $babDB->db_query('SELECT name FROM absences_types WHERE id IN('.$babDB->quote($types).')');
1323
		
1324
		$r = array();
1325
		while ($arr = $babDB->db_fetch_assoc($res))
1326
		{
1327
			$r[] = $arr['name'];
1328
		}
1329
		
1330
		return implode(', ', $r);
1331
	}
1332
1333
	
1334
	/**
1335
	 * @return absences_IncrementRight[]
1336
	 */
1337 42
	public function getIncrementIterator()
1338
	{
1339 42
	    require_once dirname(__FILE__).'/increment_right.class.php';
1340 42
	    $I = new absences_IncrementRightIterator;
1341 42
	    $I->setRight($this);
1342 42
	    return $I;
1343
	}
1344
	
1345
	
1346
	/**
1347
	 * Quantitee attribuee sur le droit par la mise a jour mensuelle
1348
	 * a condition qu'il n'y ai pas de quantite specifique pour l'agent
1349
	 * @param string $date     YYYY-MM-DD
1350
	 * @return float
1351
	 */
1352 23 View Code Duplication
	public function getIncrementQuantity($date = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1353
	{
1354 23
	    $n = 0.0;
1355 23
	    $I = $this->getIncrementIterator();
1356 23
	    $I->upto = $date;
1357
	    
1358 23
	    foreach($I as $d)
1359
	    {
1360 9
	        $n += (float) $d->quantity;
1361 23
	    }
1362
	    
1363 23
	    return $n;
1364
	}
1365
	
1366
	
1367
	
1368
	public function getDynamicConfigurationIterator()
1369
	{
1370
		require_once dirname(__FILE__).'/dynamic_configuration.class.php';
1371
		
1372
		$I = new absences_DynamicConfigurationIterator;
1373
		$I->setRight($this);
1374
		
1375
		return $I;
1376
	}
1377
	
1378
	/**
1379
	 * @return array
1380
	 */
1381
	public function getDynamicTypes()
1382
	{
1383
		global $babDB;
1384
		
1385
		$arr = explode(',', $this->dynconf_types);
1386
		$r = array();
1387
		
1388
		$res = $babDB->db_query('SELECT name FROM absences_types WHERE id IN('.$babDB->quote($arr).')');
1389
		while ($arr = $babDB->db_fetch_assoc($res))
1390
		{
1391
			$r[] = $arr['name'];
1392
		}
1393
		
1394
		return $r;
1395
	}
1396
	
1397
	
1398
	/**
1399
	 * Quantite a ajouter pour le mois
1400
	 * tiens compte de la quantite max
1401
	 * 
1402
	 * @param float $current_quantity		Can be the right quantity or the user right quantity
1403
	 * 
1404
	 * @return float
1405
	 */
1406 10
	public function getQuantityIncMonth($current_quantity)
1407
	{
1408 10
		$add_quantity = (float) $this->quantity_inc_month;
1409 10
		$max = (float) $this->quantity_inc_max;
1410
1411 10
		$textmax = (int) round($max * 100);
1412
		
1413 10
		$new = ($add_quantity + $current_quantity);
1414
		
1415 10
		if ($textmax > 0 && $new > $max)
1416 10
		{
1417
		    
1418 4
			if ($current_quantity >= $max)
1419 4
			{
1420 3
				return 0.0;
1421
			}
1422
			
1423 2
			return ($max - $current_quantity);
1424
		}
1425
		
1426 8
		return $add_quantity;
1427
	}
1428
	
1429
	
1430
	/**
1431
	 * Movements related to the right
1432
	 * @return absences_MovementIterator
1433
	 */
1434
	public function getMovementIterator()
1435
	{
1436
	    require_once dirname(__FILE__).'/movement.class.php';
1437
	
1438
	    $I = new absences_MovementIterator();
1439
	    $I->setRight($this);
1440
	
1441
	    return $I;
1442
	}
1443
	
1444
	
1445
}
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
class absences_RightIterator extends absences_Iterator
1458
{
1459
	
1460
	/**
1461
	 * 
1462
	 * @var int | array
1463
	 */
1464
	public $kind;
1465
	
1466
	/**
1467
	 * les droit pas incrmentees depuis le premier jour du mois
1468
	 * @var ???
1469
	 */
1470
	public $increment;
1471
	
1472
	/**
1473
	 * 
1474
	 * @var bool
1475
	 */
1476
	public $active;
1477
	
1478
	
1479
	/**
1480
	 * 
1481
	 * @var int | array
1482
	 */
1483
	public $sync_status;
1484
	
1485
	
1486
	/**
1487
	 * 
1488
	 * @var int
1489
	 */
1490
	public $archived = 0;
1491
1492
1493
	public function getObject($data)
1494
	{
1495
		$right = new absences_Right($data['id']);
1496
		$right->setRow($data);
1497
1498
1499
		return $right;
1500
	}
1501
1502
	public function executeQuery()
1503
	{
1504
		if(is_null($this->_oResult))
1505
		{
1506
			global $babDB;
1507
1508
			$query = '
1509
			SELECT
1510
				r.*
1511
			FROM
1512
				absences_rights r
1513
			';
1514
			
1515
			$where = array();
1516
1517
			if (isset($this->kind))
1518
			{
1519
				$where[] = 'r.kind IN('.$babDB->quote($this->kind).')';
1520
			}
1521
1522
			if (isset($this->increment))
1523
			{
1524
				require_once $GLOBALS['babInstallPath'].'utilit/dateTime.php';
1525
				$limit = new BAB_DateTime(date('Y'), date('n'), 1);
1526
				
1527
				$where[] = 'r.quantity_inc_last<'.$babDB->quote($limit->getIsoDateTime());
1528
				$where[] = "r.quantity_inc_month>'0.00'";
1529
			}
1530
			
1531
			if (isset($this->active))
1532
			{
1533
				$where[] = 'r.active='.$babDB->quote($this->active ? 'Y' : 'N');
1534
			}
1535
			
1536
			if (isset($this->sync_status))
1537
			{
1538
				$where[] = 'r.sync_status IN('.$babDB->quote($this->sync_status).')';
1539
			}
1540
			
1541
			if (isset($this->archived))
1542
			{
1543
				$where[] = 'r.archived='.$babDB->quote($this->archived);
1544
			}
1545
			
1546
			if ($where)
0 ignored issues
show
Bug Best Practice introduced by
The expression $where of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1547
			{
1548
				$query .= ' WHERE '.implode(' AND ', $where);
1549
			}
1550
			
1551
			bab_debug($query);
1552
1553
			$this->setMySqlResult($this->getDataBaseAdapter()->db_query($query));
1554
		}
1555
	}
1556
}
1557