Completed
Push — master ( 6969ca...3674b2 )
by Paul
09:12
created

absences_Right::getRightCet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 9
loc 9
rs 9.6666
ccs 0
cts 7
cp 0
crap 6
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
	    $initial_quantity = $quantity;
670
	    
671 11
	    if ($this->quantity_inc_last !== '0000-00-00 00:00:00' && $this->isAccessibleOnMonth())
672 11
	    {
673
	        // Do not increment the first month
674
	        // Do not increment if the full month is not totally available
675
	        
676 10
	        $quantity_inc_month = $this->getQuantityIncMonth($quantity);
677
	        
678 10
	        if (0 !== (int) round(100 * $quantity_inc_month)) {
679
	        
680 9
    	        $quantity += $quantity_inc_month;
681
    	        
682 9
    	        $this->saveQuantityIncMonth($quantity_inc_month);
683
    
684 9
    	        $message = sprintf(
685 9
    	            absences_translate('The quantity of right "%s" has been modified from %s to %s by the monthly update'), 
686 9
                    $this->description,
687 9
                    $initial_quantity,
688
                    $quantity
689 9
    	        );
690
    	        
691 9
    	        $this->addMovement($message, '', 0);
692
    	        
693 9
    	        if (isset($event)) {
694
    	            $event->log('absences', $message);
695
    	        }
696 9
	        }
697 10
	    }
698
	    	
699 11
	    global $babDB;
700
	    	
701 11
	    $babDB->db_query('UPDATE absences_rights SET 
702 11
				quantity_inc_last='.$babDB->quote($limit->getIsoDateTime()).'
703 11
			WHERE id='.$babDB->quote($this->id));
704
	    
705 11
	    if ($this->quantity_inc_last !== '0000-00-00 00:00:00') {
706
    	    // update the modified quantites but not the first month
707
    	    // agentRight have their own isAccessibleOnMonth method because the period can be different
708
    	    
709 10
    	    $modified = $this->getAgentRightIterator();
710 10
    	    $modified->modified_quantity = true;
711
    	    
712 10
    	    foreach ($modified as $agentRight)
713
    	    {
714
    	        /*@var $agentRight absences_AgentRight */
715 8
    	        $agentRight->monthlyQuantityUpdate($event);
716 10
    	    }
717
    	    
718
    	    
719
    	    // UPDATE the current instance
720 10
    	    $this->quantity_inc_last = $limit->getIsoDateTime();
721
    	    
722 10
    	    return true;
723
	    }
724
	    
725
	    // UPDATE the current instance
726 1
	    $this->quantity_inc_last = $limit->getIsoDateTime();
727 1
	    return false;
728
	}
729
	
730
	
731
	
732
	
733
	/**
734
	 * Tester si le droit est accessible en fonction de la periode de validite du droit
735
	 * @see absences_AgentRight::isAccessibleByValidityPeriod()
736
	 * @return bool
737
	 */
738
	public function isAccessibleByValidityPeriod()
739
	{
740
		$access= true;
741
		
742 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...
743
			$access= false;
744
		}
745
		
746 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...
747
			$access= false;
748
		}
749
		
750
		return $access;
751
	}
752
	
753
	/**
754
	 * Test if the vacation right is visible to the end user by testing properties of fixed vacation right
755
	 * if the right is not a fixed vacation right, this method return true
756
	 * 
757
	 * @return bool
758
	 */
759
	public function isAccessibleIfFixed()
760
	{
761
		if ($this->kind != self::FIXED)
762
		{
763
			return true;
764
		}
765
		
766
		// dont't display vacations with fixed dates that are gone
767
		if( $this->date_end_fixed != '0000-00-00 00:00:00' && (bab_mktime($this->date_end_fixed) < mktime())){
768
			return false;
769
		}
770
		
771
		return true;
772
	}
773
	
774
	
775
	/**
776
	 * If the right is displayed as available in the manager list
777
	 * Test if the right is accessible for the user to display information in exported file
778
	 * pour l'export de l'agent et pour l'export des droits
779
	 * 
780
	 * @return bool
781
	 */
782
	public function isAvailable()
783
	{
784
		return ('Y' == $this->active && $this->isAccessibleByValidityPeriod() && $this->isAccessibleIfFixed());
785
	}
786
	
787
	
788
	public function getStatus()
789
	{
790
		if ($this->archived)
791
		{
792
			return absences_translate('Archived');
793
		}
794
		
795
		
796
		if ('Y' == $this->active)
797
		{
798
			return absences_translate('Active');
799
		}
800
		
801
		if ('N' == $this->active)
802
		{
803
			return absences_translate('Disabled for appliquant, usable by managers and delegated managers');
804
		}
805
	}
806
	
807
	
808
	/**
809
	 * Get a period description in text
810
	 * @param string $begin
811
	 * @param string $end
812
	 */
813
	private function periodText($begin, $end)
814
	{
815
		if ('0000-00-00'=== $begin && '0000-00-00' === $end) {
816
			return null;
817
		}
818
		
819
		$d1 = bab_shortDate(bab_mktime($begin), false);
820
		$d2 = bab_shortDate(bab_mktime($end), false);
821
		
822
		if (!$d1) {
823
		    $d1 = absences_translate('??/??/????');
824
		}
825
		
826
		if (!$d2) {
827
		    $d2 = absences_translate('??/??/????');
828
		}
829
		
830
		
831
		return sprintf(absences_translate('the %s and the %s'), $d1, $d2);
832
	}
833
	
834
	
835
	/**
836
	 * 
837
	 * @param string $name
838
	 * @return string
839
	 */
840
	private function getDirFieldLabel($name)
841
	{
842
		$fields = bab_getDirEntry(BAB_REGISTERED_GROUP, BAB_DIR_ENTRY_ID_GROUP);
843
		return $fields[$name]['name'];
844
	}
845
	
846
	
847
	
848
	
849
	/**
850
	 * Get access conditions as a string
851
	 * of null if there are no access conditions
852
	 * @return string
853
	 */
854
	public function getAccessConditions()
855
	{
856
		global $babDB;
857
		$conditions = array();
858
		
859
		
860
		// Disponibilite en fonction de la date de saisie de la demande de conges
861
		if ('0000-00-00' !== $this->date_begin_valid)
862
		{
863
			$period = $this->periodText($this->date_begin_valid, $this->date_end_valid);
864
			$conditions[] = sprintf(absences_translate('To create a vacation request at a date beetween %s.'), $period);
865
		}
866
		
867
		
868
		$rightRules = $this->getRightRule();
869
		
870
		// Disponibilite en fonction de la periode de conges demandee
871
		$res = $rightRules->getInPeriodRes();
872
		if ($babDB->db_num_rows($res) > 0)
873
		{
874
			$s = array();
875
			
876
			while ($arr = $babDB->db_fetch_assoc($res))
877
			{
878
				$d1 = bab_shortDate(bab_mktime($arr['period_start']), false);
879
				$d2 = bab_shortDate(bab_mktime($arr['period_end']), false);
880
				
881
				
882
				switch($arr['right_inperiod'])
883
				{
884
					case 1: $subcond = sprintf(absences_translate('beetween the %s and the %s'), $d1, $d2);	break;
885
					case 2: $subcond = sprintf(absences_translate('before the %s or after the %s'), $d1, $d2);	break;
886
				}
887
				
888
				$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...
889
			}
890
			
891
			$conditions[] = absences_translate('To apply for a vacation period').' '.implode(', ', $s).'.';
892
		}
893
		
894
		// Attribution du droit en fonction des jours demandes et valides
895
896
		if ($rightRules->getRow() && $rightRules->trigger_nbdays_min && $rightRules->trigger_nbdays_max)
897
		{
898
			$text = sprintf(absences_translate('To have requested at least %d days but less than %d days'), $rightRules->trigger_nbdays_min, $rightRules->trigger_nbdays_max);
899
			if ($rightRules->trigger_type)
900
			{
901
				$text .= ' '. sprintf(absences_translate('of type %s'), $rightRules->getType()->name);
902
			}
903
			
904
			$periods = array();
905
			$p1 = $this->periodText($rightRules->trigger_p1_begin, $rightRules->trigger_p1_end);
906
			$p2 = $this->periodText($rightRules->trigger_p2_begin, $rightRules->trigger_p2_end);
907
			
908
			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...
909
				$periods[] = sprintf(absences_translate('beetween %s'), $p1);
910
			}
911
			
912
			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...
913
				$periods[] = sprintf(absences_translate('beetween %s'), $p2);
914
			}
915
			$text .= ' '.implode(' '.absences_translate('or').' ', $periods).'.';
916
			
917
			$conditions[] = $text;
918
		}
919
		
920
		// Disponibilite en fonction d'une date anterieure
921
		
922 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...
923
		{
924
			$f = $this->getDirFieldLabel($this->earlier);
925
			$cond = array();
926
			
927
			if ($this->earlier_begin_valid)
928
			{
929
				$cond[] = sprintf(absences_translate('%d years later "%s"'), $this->earlier_begin_valid, $f);
930
			} else {
931
				$cond[] = sprintf(absences_translate('after "%s"'), $f);
932
			}
933
			
934
			if ($this->earlier_end_valid)
935
			{
936
				$cond[] = sprintf(absences_translate('in the %d years next to "%s"'), $this->earlier_end_valid, $f);
937
			} 
938
			
939
			$conditions[] = absences_translate('To request a period').' '.implode(' '.absences_translate('and').' ', $cond).'.';
940
		}
941
		
942
		
943
		// Disponibilite en fonction d'une date posterieure
944
		
945 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...
946
		{
947
			$f = $this->getDirFieldLabel($this->later);
948
			$cond = array();
949
			
950
			if ($this->later_begin_valid)
951
			{
952
				$cond[] = sprintf(absences_translate('in the %d years preceding "%s"'), $this->later_begin_valid, $f);
953
			} else {
954
				$cond[] = sprintf(absences_translate('after "%s"'), $f);
955
			}
956
			
957
			if ($this->later_end_valid)
958
			{
959
				$cond[] = sprintf(absences_translate('at least %d years before the "%s"'), $this->later_end_valid, $f);
960
			}
961
			
962
			$conditions[] = absences_translate('To request a period').' '.implode(' '.absences_translate('and').' ', $cond).'.';
963
		}
964
		
965
		if($this->delay_before)
966
		{
967
			$conditions[] = sprintf(absences_translate('To request a period at least %s days before.'), $this->delay_before);
968
		}
969
		
970
		
971
		if (0 ===count($conditions))
972
		{
973
			return null;
974
		}
975
		
976
		
977
		$text = absences_translate('The condition to access this vacation right is', 'The conditions to access this vacation right are:', count($conditions));
978
		
979
		foreach($conditions as $c)
980
		{
981
			$text .= " \n".$c;
982
		}
983
		
984
		
985
		return $text;
986
		
987
	}
988
	
989
	
990
	
991
	
992
	/**
993
	 * Agents associated to vacation right
994
	 * @return absences_AgentIterator
995
	 */
996
	public function getAgentIterator()
997
	{
998
		require_once dirname(__FILE__).'/agent.class.php';
999
		$I = new absences_AgentIterator;
1000
		$I->setRight($this);
1001
		
1002
		return $I;
1003
	}
1004
	
1005
	/**
1006
	 * Collections associated to vacation right
1007
	 * @return absences_CollectionIterator
1008
	 */
1009
	public function getCollectionIterator()
1010
	{
1011
		require_once dirname(__FILE__).'/collection.class.php';
1012
		$I = new absences_CollectionIterator;
1013
		$I->setRight($this);
1014
		
1015
		return $I;
1016
	}
1017
	
1018
	
1019
	/**
1020
	 * @return absences_AgentRightStatIterator
1021
	 */
1022 10
	public function getAgentRightIterator()
1023
	{
1024 10
		require_once dirname(__FILE__).'/agent_right.class.php';
1025 10
		$I = new absences_AgentRightStatIterator;
1026 10
		$I->setRight($this);
1027
		
1028 10
		return $I;
1029
	}
1030
	
1031
	
1032
	/**
1033
	 * determiner le nombre de d'agent avec ce droit non solde
1034
	 * @return int
1035
	 */
1036
	public function getAgentUsage()
1037
	{
1038
		$last_available = 0;
1039
		$last_waiting = 0;
1040
		$last_agents = 0;
1041
		$last_agents_iu = 0;
1042
		
1043
		
1044
		foreach($this->getAgentRightIterator() as $agent_right)
1045
		{
1046
			/*@var $agent_right absences_AgentRight */
1047
			
1048
			$waiting = $agent_right->getWaitingQuantity();
1049
			if (0 !== (int) round(100 * $waiting))
1050
			{
1051
				$last_agents_iu++;
1052
			}
1053
			
1054
			$available = $agent_right->getAvailableQuantity();
1055
			if (0 !== (int) round(100 * $available))
1056
			{
1057
				$last_agents++;
1058
				$last_agents_iu++;
1059
			}
1060
			
1061
			
1062
			
1063
			
1064
			$last_waiting += $waiting;
1065
			$last_available += $available; 
1066
				
1067
		}
1068
		
1069
		
1070
		
1071
		return array(
1072
			'available' => absences_quantity($last_available, $this->quantity_unit),
1073
			'waiting' => absences_quantity($last_waiting, $this->quantity_unit),
1074
			'agents' => $last_agents,
1075
			'agents_iu' => $last_agents_iu
1076
		);
1077
	}
1078
	
1079
	
1080
	
1081
	
1082
	
1083
	/**
1084
	 *
1085
	 * @param string $message	Generated message
1086
	 * @param string $comment	Author comment
1087
	 */
1088 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...
1089
	{
1090 9
		require_once dirname(__FILE__).'/movement.class.php';
1091
	
1092 9
		$movement = new absences_Movement();
1093 9
		$movement->message = $message;
1094 9
		$movement->comment = $comment;
1095
		
1096 9
		if (isset($id_author))
1097 9
		{
1098 9
			$movement->id_author = $id_author;
1099 9
		}
1100
		
1101 9
		$movement->setRight($this);
1102 9
		$movement->save();
1103 9
	}
1104
	
1105
	
1106
	
1107
	/**
1108
	 * Tester si tout les beneficiaires du droit on consome tout le solde
1109
	 * @return bool
1110
	 */
1111
	public function isResulted()
1112
	{
1113
		if ($this->cbalance === 'N')
1114
		{
1115
			// soldes negatifs autorises
1116
			// considere comme solde si le droit n'est pas dispo
1117
			return !$this->isAvailable();
1118
		}
1119
		
1120
		
1121
		foreach($this->getAgentRightIterator() as $agent_right)
1122
		{
1123
			if ($agent_right->getAvailableQuantity() > 0)
1124
			{
1125
				// tout n'a pas ete consome
1126
				return false;
1127
			}
1128
		}
1129
		
1130
		// tout a ete consome et le droit n'est pas dispo
1131
		return !$this->isAvailable();
1132
	}
1133
	
1134
	
1135
	
1136
	
1137
	/**
1138
	 * Archiver le droit
1139
	 */
1140
	public function archive()
1141
	{
1142
		global $babDB;
1143
		
1144
		$babDB->db_query('UPDATE absences_rights SET archived='.$babDB->quote(1).' WHERE id='.$babDB->quote($this->id));
1145
	}
1146
	
1147
	
1148
	/**
1149
	 * Method used with bab_Sort
1150
	 */
1151
	public function getSortKey()
1152
	{
1153
		return $this->sortkey;
1154
	}
1155
	
1156
	public function setSortKey($i)
1157
	{
1158
		global $babDB;
1159
		$babDB->db_query('UPDATE absences_rights SET sortkey='.$babDB->quote($i).' WHERE id='.$babDB->quote($this->id));
1160
	}
1161
	
1162
	public function getSortLabel()
1163
	{
1164
		return $this->description;
1165
	}
1166
	
1167
	
1168
	public function getIconClassName()
1169
	{
1170
		bab_functionality::includeOriginal('Icons');
1171
		return Func_Icons::ACTIONS_ARROW_RIGHT;
1172
	}
1173
	
1174
	
1175
	
1176
	
1177
	/**
1178
	 * Insert new right
1179
	 */
1180 8
	public function insert()
1181
	{
1182
		require_once $GLOBALS['babInstallPath'].'utilit/uuid.php';
1183
		global $babDB;
1184
		
1185
		$babDB->db_query("INSERT INTO absences_rights 
1186
			(
1187
				id_creditor,
1188
				kind,
1189
		        createdOn,
1190
				date_entry,
1191
				date_begin,
1192
				date_end,
1193
				quantity,
1194
				quantity_unit,
1195
				quantity_inc_month,
1196
				quantity_inc_max,
1197
				id_type,
1198
				description,
1199
				active,
1200
				cbalance,
1201
				date_begin_valid,
1202
				date_end_valid,
1203
				date_end_fixed,
1204
				date_begin_fixed,
1205
				no_distribution,
1206
				use_in_cet,
1207
				cet_quantity,
1208
				id_rgroup,  
1209
				earlier,
1210
				earlier_begin_valid,
1211
				earlier_end_valid,  
1212
				later,
1213
				later_begin_valid,
1214
				later_end_valid,
1215
				delay_before,
1216
				sync_status,
1217
				sync_update,
1218
				uuid,
1219 8
				archived
1220
			) 
1221
		VALUES 
1222
			(
1223
				".$babDB->quote(bab_getUserId()).",
1224
				".$babDB->quote($this->getKind()).",
1225
		        NOW(),
1226
				NOW(),
1227
				".$babDB->quote($this->date_begin).",
1228
				".$babDB->quote($this->date_end).",
1229
				".$babDB->quote($this->quantity).",
1230
				".$babDB->quote($this->quantity_unit).",
1231
				".$babDB->quote($this->quantity_inc_month).",
1232
				".$babDB->quote($this->quantity_inc_max).",
1233
				".$babDB->quote($this->id_type).",
1234
				".$babDB->quote($this->description).",
1235
				".$babDB->quote($this->active).",
1236
				".$babDB->quote($this->cbalance).",
1237
				".$babDB->quote($this->date_begin_valid).",
1238
				".$babDB->quote($this->date_end_valid).",
1239
				".$babDB->quote($this->date_end_fixed).",
1240
				".$babDB->quote($this->date_begin_fixed).",
1241
				".$babDB->quote($this->no_distribution).",
1242
				".$babDB->quote($this->use_in_cet).",
1243
				".$babDB->quote($this->cet_quantity).",
1244 5
				".$babDB->quote($this->id_rgroup).",
1245
				".$babDB->quote($this->earlier).",
1246
				".$babDB->quote($this->earlier_begin_valid).",
1247
				".$babDB->quote($this->earlier_end_valid).",
1248
				".$babDB->quote($this->later).",
1249
				".$babDB->quote($this->later_begin_valid).",
1250
				".$babDB->quote($this->later_end_valid).",
1251
				".$babDB->quote($this->delay_before).",
1252
				".$babDB->quote($this->sync_status).",
1253
				".$babDB->quote($this->sync_update).",
1254
				".$babDB->quote($this->uuid).",
1255
				".$babDB->quote($this->archived)."
1256
			)
1257
		");
1258
		
1259
		
1260
		$this->id = $babDB->db_insert_id();
1261
	}
1262
	
1263
	
1264
	/**
1265
	 * Update by UUID
1266
	 */
1267
	public function update()
1268
	{
1269
		global $babDB;
1270
		
1271
		$query = 'UPDATE absences_rights SET 
1272
				kind='.$babDB->quote($this->kind).',
1273
				date_entry=NOW(),
1274
				date_begin='.$babDB->quote($this->date_begin).',
1275
				date_end='.$babDB->quote($this->date_end).',
1276
				quantity='.$babDB->quote($this->quantity).',
1277
				quantity_unit='.$babDB->quote($this->quantity_unit).',
1278
				quantity_inc_month='.$babDB->quote($this->quantity_inc_month).',
1279
				quantity_inc_max='.$babDB->quote($this->quantity_inc_max).',
1280
				description='.$babDB->quote($this->description).',
1281
				active='.$babDB->quote($this->active).',
1282
				cbalance='.$babDB->quote($this->cbalance).',
1283
				date_begin_valid='.$babDB->quote($this->date_begin_valid).',
1284
				date_end_valid='.$babDB->quote($this->date_end_valid).',
1285
				date_end_fixed='.$babDB->quote($this->date_end_fixed).',
1286
				date_begin_fixed='.$babDB->quote($this->date_begin_fixed).',
1287
				no_distribution='.$babDB->quote($this->no_distribution).',
1288
				use_in_cet='.$babDB->quote($this->use_in_cet).',
1289
				cet_quantity='.$babDB->quote($this->cet_quantity).',
1290
				earlier='.$babDB->quote($this->earlier).',
1291
				earlier_begin_valid='.$babDB->quote($this->earlier_begin_valid).',
1292
				earlier_end_valid='.$babDB->quote($this->earlier_end_valid).',
1293
				later='.$babDB->quote($this->later).',
1294
				later_begin_valid='.$babDB->quote($this->later_begin_valid).',
1295
				later_end_valid='.$babDB->quote($this->later_end_valid).',
1296
				delay_before='.$babDB->quote($this->delay_before).',
1297
				sync_status='.$babDB->quote($this->sync_status).',
1298
				sync_update='.$babDB->quote($this->sync_update).',
1299
				archived='.$babDB->quote($this->archived).' 
1300
			WHERE uuid='.$babDB->quote($this->uuid).' 
1301
		';
1302
		
1303
		bab_debug($query);
1304
		
1305
		$babDB->db_query($query);
1306
	}
1307
	
1308
	
1309
	/**
1310
	 * 
1311
	 * @return string
1312
	 */
1313
	public function getQuantityAlertTypes()
1314
	{
1315
		$types = explode(',',$this->quantity_alert_types);
1316
		
1317
		if (empty($types))
1318
		{
1319
			return '';
1320
		}
1321
		
1322
		global $babDB;
1323
		
1324
		$res = $babDB->db_query('SELECT name FROM absences_types WHERE id IN('.$babDB->quote($types).')');
1325
		
1326
		$r = array();
1327
		while ($arr = $babDB->db_fetch_assoc($res))
1328
		{
1329
			$r[] = $arr['name'];
1330
		}
1331
		
1332
		return implode(', ', $r);
1333
	}
1334
1335
	
1336
	/**
1337
	 * @return absences_IncrementRight[]
1338
	 */
1339 42
	public function getIncrementIterator()
1340
	{
1341 42
	    require_once dirname(__FILE__).'/increment_right.class.php';
1342 42
	    $I = new absences_IncrementRightIterator;
1343 42
	    $I->setRight($this);
1344 42
	    return $I;
1345
	}
1346
	
1347
	
1348
	/**
1349
	 * Quantitee attribuee sur le droit par la mise a jour mensuelle
1350
	 * a condition qu'il n'y ai pas de quantite specifique pour l'agent
1351
	 * @param string $date     YYYY-MM-DD
1352
	 * @return float
1353
	 */
1354 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...
1355
	{
1356 23
	    $n = 0.0;
1357 23
	    $I = $this->getIncrementIterator();
1358 23
	    $I->upto = $date;
1359
	    
1360 23
	    foreach($I as $d)
1361
	    {
1362 9
	        $n += (float) $d->quantity;
1363 23
	    }
1364
	    
1365 23
	    return $n;
1366
	}
1367
	
1368
	
1369
	
1370
	public function getDynamicConfigurationIterator()
1371
	{
1372
		require_once dirname(__FILE__).'/dynamic_configuration.class.php';
1373
		
1374
		$I = new absences_DynamicConfigurationIterator;
1375
		$I->setRight($this);
1376
		
1377
		return $I;
1378
	}
1379
	
1380
	/**
1381
	 * @return array
1382
	 */
1383
	public function getDynamicTypes()
1384
	{
1385
		global $babDB;
1386
		
1387
		$arr = explode(',', $this->dynconf_types);
1388
		$r = array();
1389
		
1390
		$res = $babDB->db_query('SELECT name FROM absences_types WHERE id IN('.$babDB->quote($arr).')');
1391
		while ($arr = $babDB->db_fetch_assoc($res))
1392
		{
1393
			$r[] = $arr['name'];
1394
		}
1395
		
1396
		return $r;
1397
	}
1398
	
1399
	
1400
	/**
1401
	 * Quantite a ajouter pour le mois
1402
	 * tiens compte de la quantite max
1403
	 * 
1404
	 * @param float $current_quantity		Can be the right quantity or the user right quantity
1405
	 * 
1406
	 * @return float
1407
	 */
1408 10
	public function getQuantityIncMonth($current_quantity)
1409
	{
1410 10
		$add_quantity = (float) $this->quantity_inc_month;
1411 10
		$max = (float) $this->quantity_inc_max;
1412
1413 10
		$textmax = (int) round($max * 100);
1414
		
1415 10
		$new = ($add_quantity + $current_quantity);
1416
		
1417 10
		if ($textmax > 0 && $new > $max)
1418 10
		{
1419
		    
1420 4
			if ($current_quantity >= $max)
1421 4
			{
1422 3
				return 0.0;
1423
			}
1424
			
1425 2
			return ($max - $current_quantity);
1426
		}
1427
		
1428 8
		return $add_quantity;
1429
	}
1430
	
1431
	
1432
	/**
1433
	 * Movements related to the right
1434
	 * @return absences_MovementIterator
1435
	 */
1436
	public function getMovementIterator()
1437
	{
1438
	    require_once dirname(__FILE__).'/movement.class.php';
1439
	
1440
	    $I = new absences_MovementIterator();
1441
	    $I->setRight($this);
1442
	
1443
	    return $I;
1444
	}
1445
	
1446
	
1447
}
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
class absences_RightIterator extends absences_Iterator
1460
{
1461
	
1462
	/**
1463
	 * 
1464
	 * @var int | array
1465
	 */
1466
	public $kind;
1467
	
1468
	/**
1469
	 * les droit pas incrmentees depuis le premier jour du mois
1470
	 * @var ???
1471
	 */
1472
	public $increment;
1473
	
1474
	/**
1475
	 * 
1476
	 * @var bool
1477
	 */
1478
	public $active;
1479
	
1480
	
1481
	/**
1482
	 * 
1483
	 * @var int | array
1484
	 */
1485
	public $sync_status;
1486
	
1487
	
1488
	/**
1489
	 * 
1490
	 * @var int
1491
	 */
1492
	public $archived = 0;
1493
1494
1495
	public function getObject($data)
1496
	{
1497
		$right = new absences_Right($data['id']);
1498
		$right->setRow($data);
1499
1500
1501
		return $right;
1502
	}
1503
1504
	public function executeQuery()
1505
	{
1506
		if(is_null($this->_oResult))
1507
		{
1508
			global $babDB;
1509
1510
			$query = '
1511
			SELECT
1512
				r.*
1513
			FROM
1514
				absences_rights r
1515
			';
1516
			
1517
			$where = array();
1518
1519
			if (isset($this->kind))
1520
			{
1521
				$where[] = 'r.kind IN('.$babDB->quote($this->kind).')';
1522
			}
1523
1524
			if (isset($this->increment))
1525
			{
1526
				require_once $GLOBALS['babInstallPath'].'utilit/dateTime.php';
1527
				$limit = new BAB_DateTime(date('Y'), date('n'), 1);
1528
				
1529
				$where[] = 'r.quantity_inc_last<'.$babDB->quote($limit->getIsoDateTime());
1530
				$where[] = "r.quantity_inc_month>'0.00'";
1531
			}
1532
			
1533
			if (isset($this->active))
1534
			{
1535
				$where[] = 'r.active='.$babDB->quote($this->active ? 'Y' : 'N');
1536
			}
1537
			
1538
			if (isset($this->sync_status))
1539
			{
1540
				$where[] = 'r.sync_status IN('.$babDB->quote($this->sync_status).')';
1541
			}
1542
			
1543
			if (isset($this->archived))
1544
			{
1545
				$where[] = 'r.archived='.$babDB->quote($this->archived);
1546
			}
1547
			
1548
			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...
1549
			{
1550
				$query .= ' WHERE '.implode(' AND ', $where);
1551
			}
1552
			
1553
			bab_debug($query);
1554
1555
			$this->setMySqlResult($this->getDataBaseAdapter()->db_query($query));
1556
		}
1557
	}
1558
}
1559