Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like absences_AgentRight often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use absences_AgentRight, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | class absences_AgentRight extends absences_Record |
||
| 51 | { |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Contain default values for vacation right |
||
| 56 | * @var absences_Right |
||
| 57 | */ |
||
| 58 | private $right; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Contain the user vacation parameters |
||
| 62 | * @var absences_Agent |
||
| 63 | */ |
||
| 64 | private $agent; |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Cache for consumed confirmed quantity computed value |
||
| 69 | * @var float |
||
| 70 | */ |
||
| 71 | private $confirmed_quantity; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Cache for consumed waiting quantity computed value |
||
| 75 | * @var float |
||
| 76 | */ |
||
| 77 | private $waiting_quantity; |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * Cache for consumed previsional quantity computed value |
||
| 82 | * @var float |
||
| 83 | */ |
||
| 84 | private $previsional_quantity; |
||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * Create absences_AgentRight object using the absences_users_rights table id |
||
| 90 | * @param int $id |
||
| 91 | * @return absences_AgentRight |
||
| 92 | */ |
||
| 93 | 33 | public static function getById($id) |
|
| 94 | { |
||
| 95 | 33 | $agentRight = new absences_AgentRight; |
|
| 96 | 33 | $agentRight->id = $id; |
|
| 97 | |||
| 98 | 33 | return $agentRight; |
|
| 99 | } |
||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | 43 | View Code Duplication | public function getRow() |
|
|
|||
| 108 | { |
||
| 109 | 43 | if (null === $this->row) |
|
| 110 | 43 | { |
|
| 111 | 33 | global $babDB; |
|
| 112 | |||
| 113 | 33 | if (isset($this->id)) |
|
| 114 | 33 | { |
|
| 115 | 33 | $res = $babDB->db_query('SELECT * FROM absences_users_rights WHERE id='.$babDB->quote($this->id)); |
|
| 116 | 33 | $this->setRow($babDB->db_fetch_assoc($res)); |
|
| 117 | |||
| 118 | 33 | } else if (isset($this->agent) && isset($this->right)) |
|
| 119 | { |
||
| 120 | $res = $babDB->db_query('SELECT * FROM absences_users_rights WHERE id_user='.$babDB->quote($this->agent->getIdUser()).' AND id_right='.$babDB->quote($this->right->id)); |
||
| 121 | $this->setRow($babDB->db_fetch_assoc($res)); |
||
| 122 | } |
||
| 123 | 33 | } |
|
| 124 | |||
| 125 | 43 | return $this->row; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * |
||
| 130 | * @param absences_Agent $agent |
||
| 131 | * @return absences_AgentRight |
||
| 132 | */ |
||
| 133 | 10 | public function setAgent(absences_Agent $agent) |
|
| 134 | { |
||
| 135 | 10 | $this->agent = $agent; |
|
| 136 | 10 | return $this; |
|
| 137 | 4 | } |
|
| 138 | |||
| 139 | /** |
||
| 140 | * |
||
| 141 | * @param absences_Right $right |
||
| 142 | * @return absences_AgentRight |
||
| 143 | */ |
||
| 144 | 25 | public function setRight(absences_Right $right) |
|
| 145 | { |
||
| 146 | 25 | $this->right = $right; |
|
| 147 | 18 | return $this; |
|
| 148 | } |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * @return absences_Right |
||
| 153 | */ |
||
| 154 | 42 | public function getRight() |
|
| 155 | 4 | { |
|
| 156 | 42 | if (!isset($this->right)) |
|
| 157 | 42 | { |
|
| 158 | 32 | $row = $this->getRow(); |
|
| 159 | |||
| 160 | 32 | if (!$row['id_right']) |
|
| 161 | 32 | { |
|
| 162 | return null; |
||
| 163 | } |
||
| 164 | |||
| 165 | 32 | require_once dirname(__FILE__).'/right.class.php'; |
|
| 166 | 32 | $this->right = new absences_Right($row['id_right']); |
|
| 167 | 32 | } |
|
| 168 | |||
| 169 | 42 | return $this->right; |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return absences_Agent |
||
| 174 | */ |
||
| 175 | 8 | View Code Duplication | public function getAgent() |
| 176 | { |
||
| 177 | 8 | if (!isset($this->agent)) |
|
| 178 | 8 | { |
|
| 179 | 8 | $row = $this->getRow(); |
|
| 180 | 8 | $this->agent = absences_Agent::getFromIdUser($row['id_user']); |
|
| 181 | 8 | } |
|
| 182 | |||
| 183 | 8 | return $this->agent; |
|
| 184 | } |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Delete agentRight |
||
| 189 | * |
||
| 190 | */ |
||
| 191 | public function delete() |
||
| 192 | { |
||
| 193 | $res = $this->getDynamicRightIterator(); |
||
| 194 | foreach($res as $dynright) |
||
| 195 | { |
||
| 196 | /*@var $dynright absences_DynamicRight */ |
||
| 197 | $dynright->quantity = 0; |
||
| 198 | $dynright->save(); |
||
| 199 | } |
||
| 200 | |||
| 201 | global $babDB; |
||
| 202 | $babDB->db_query('DELETE FROM absences_users_rights WHERE id='.$babDB->quote($this->id).''); |
||
| 203 | |||
| 204 | return true; |
||
| 205 | } |
||
| 206 | |||
| 207 | 8 | public function save() |
|
| 208 | { |
||
| 209 | global $babDB; |
||
| 210 | |||
| 211 | |||
| 212 | if (!isset($this->date_begin_valid) || empty($this->date_begin_valid)) |
||
| 213 | { |
||
| 214 | $this->date_begin_valid = '0000-00-00'; |
||
| 215 | } |
||
| 216 | |||
| 217 | if (!isset($this->date_end_valid) || empty($this->date_end_valid)) |
||
| 218 | { |
||
| 219 | $this->date_end_valid = '0000-00-00'; |
||
| 220 | } |
||
| 221 | |||
| 222 | |||
| 223 | if (!isset($this->inperiod_start) || empty($this->inperiod_start)) |
||
| 224 | { |
||
| 225 | $this->inperiod_start = '0000-00-00'; |
||
| 226 | } |
||
| 227 | |||
| 228 | if (!isset($this->inperiod_end) || empty($this->inperiod_end)) |
||
| 229 | { |
||
| 230 | $this->inperiod_end = '0000-00-00'; |
||
| 231 | } |
||
| 232 | |||
| 233 | if (!isset($this->saving_begin) || empty($this->saving_begin)) |
||
| 234 | 8 | { |
|
| 235 | $this->saving_begin = '0000-00-00'; |
||
| 236 | } |
||
| 237 | |||
| 238 | if (!isset($this->saving_end) || empty($this->saving_begin)) |
||
| 239 | 8 | { |
|
| 240 | $this->saving_end = '0000-00-00'; |
||
| 241 | } |
||
| 242 | |||
| 243 | if (!isset($this->validoverlap)) |
||
| 244 | 8 | { |
|
| 245 | $this->validoverlap = 0; |
||
| 246 | } |
||
| 247 | |||
| 248 | |||
| 249 | |||
| 250 | $exists = 0; |
||
| 251 | $quantity = ''; |
||
| 252 | |||
| 253 | if (empty($this->id)) |
||
| 254 | { |
||
| 255 | |||
| 256 | |||
| 257 | $babDB->db_query(' |
||
| 258 | INSERT INTO absences_users_rights ( |
||
| 259 | 8 | id_user, |
|
| 260 | id_right, |
||
| 261 | quantity, |
||
| 262 | date_begin_valid, |
||
| 263 | date_end_valid, |
||
| 264 | inperiod_start, |
||
| 265 | inperiod_end, |
||
| 266 | validoverlap, |
||
| 267 | saving_begin, |
||
| 268 | 8 | saving_end |
|
| 269 | ) VALUES ( |
||
| 270 | '.$babDB->quote($this->getIdUser()).', |
||
| 271 | '.$babDB->quote($this->getIdRight()).', |
||
| 272 | '.$babDB->quote($this->quantity).', |
||
| 273 | '.$babDB->quote($this->date_begin_valid).', |
||
| 274 | 8 | '.$babDB->quote($this->date_end_valid).', |
|
| 275 | '.$babDB->quote($this->inperiod_start).', |
||
| 276 | '.$babDB->quote($this->inperiod_end).', |
||
| 277 | '.$babDB->quote($this->validoverlap).', |
||
| 278 | '.$babDB->quote($this->saving_begin).', |
||
| 279 | '.$babDB->quote($this->saving_end).' |
||
| 280 | 8 | ) |
|
| 281 | '); |
||
| 282 | |||
| 283 | |||
| 284 | $this->id = $babDB->db_insert_id(); |
||
| 285 | |||
| 286 | 8 | } else { |
|
| 287 | |||
| 288 | $res = $babDB->db_query('SELECT quantity FROM absences_users_rights WHERE id='.$babDB->quote($this->id)); |
||
| 289 | $arr = $babDB->db_fetch_assoc($res); |
||
| 290 | |||
| 291 | 8 | if ($arr) { |
|
| 292 | $exists = 1; |
||
| 293 | 8 | $quantity = $arr['quantity']; |
|
| 294 | } |
||
| 295 | |||
| 296 | $babDB->db_query('UPDATE absences_users_rights |
||
| 297 | SET |
||
| 298 | quantity='.$babDB->quote($this->quantity).', |
||
| 299 | date_begin_valid='.$babDB->quote($this->date_begin_valid).', |
||
| 300 | date_end_valid='.$babDB->quote($this->date_end_valid).', |
||
| 301 | 8 | inperiod_start='.$babDB->quote($this->inperiod_start).', |
|
| 302 | inperiod_end='.$babDB->quote($this->inperiod_end).', |
||
| 303 | 3 | validoverlap='.$babDB->quote($this->validoverlap).', |
|
| 304 | saving_begin='.$babDB->quote($this->saving_begin).', |
||
| 305 | saving_end='.$babDB->quote($this->saving_end).' |
||
| 306 | 8 | WHERE |
|
| 307 | id='.$babDB->quote($this->id)); |
||
| 308 | 8 | } |
|
| 309 | |||
| 310 | |||
| 311 | $this->saveHistory($exists, $quantity); |
||
| 312 | } |
||
| 313 | |||
| 314 | |||
| 315 | |||
| 316 | |||
| 317 | |||
| 318 | public function saveHistory($exists, $quantity) |
||
| 319 | { |
||
| 320 | global $babDB; |
||
| 321 | |||
| 322 | $babDB->db_query('INSERT INTO absences_users_rights_history (id_user_right, linkexists, quantity, date_end) |
||
| 323 | VALUES ( |
||
| 324 | '.$babDB->quote($this->id).', |
||
| 325 | '.$babDB->quote($exists).', |
||
| 326 | '.$babDB->quote($quantity).', |
||
| 327 | '.$babDB->quote(date('Y-m-d H:i:s')).' |
||
| 328 | ) |
||
| 329 | '); |
||
| 330 | } |
||
| 331 | |||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * @deprecated replace by save() |
||
| 339 | */ |
||
| 340 | 8 | public function saveRightModifications() |
|
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * Get quantity value using the history |
||
| 348 | * |
||
| 349 | * @param string $date YYYY-MM-DD |
||
| 350 | * |
||
| 351 | * return values: |
||
| 352 | * null - unknow value at that date, use the main value |
||
| 353 | * empty string - used right value at that date |
||
| 354 | * string - used this value at that date |
||
| 355 | * false - the user was not linked to right at that date |
||
| 356 | * |
||
| 357 | * @return mixed |
||
| 358 | */ |
||
| 359 | 30 | protected function getQuantityValueOnDate($date) |
|
| 360 | { |
||
| 361 | 30 | if (!isset($date) || '0000-00-00' === $date) { |
|
| 362 | // no history because no date |
||
| 363 | 16 | return null; |
|
| 364 | } |
||
| 365 | |||
| 366 | 15 | if (!$this->id) { |
|
| 367 | // no history because not saved |
||
| 368 | return null; |
||
| 369 | } |
||
| 370 | |||
| 371 | 15 | global $babDB; |
|
| 372 | |||
| 373 | 15 | $res = $babDB->db_query('SELECT linkexists, quantity |
|
| 374 | 15 | FROM absences_users_rights_history WHERE id_user_right='.$babDB->quote($this->id).' |
|
| 375 | 15 | AND date_end >= '.$babDB->quote($date.' 23:59:59').' |
|
| 376 | 15 | ORDER BY date_end DESC'); |
|
| 377 | |||
| 378 | 15 | $arr = $babDB->db_fetch_assoc($res); |
|
| 379 | |||
| 380 | 15 | if (!$arr) { |
|
| 381 | // no history |
||
| 382 | 15 | return null; |
|
| 383 | } |
||
| 384 | |||
| 385 | 1 | if ('0' === $arr['linkexists']) { |
|
| 386 | return false; |
||
| 387 | } |
||
| 388 | |||
| 389 | 1 | return $arr['quantity']; |
|
| 390 | } |
||
| 391 | |||
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * Initial quantity |
||
| 396 | * @param string $date YYYY-MM-DD |
||
| 397 | * @return float |
||
| 398 | */ |
||
| 399 | 30 | public function getInitialQuantity($date = null) |
|
| 400 | { |
||
| 401 | 30 | $ur_quantity = $this->getQuantityValueOnDate($date); |
|
| 402 | |||
| 403 | 30 | if (null === $ur_quantity) { |
|
| 404 | // this approximation is used for installations older than |
||
| 405 | // the creation of the absences_users_rights_history table |
||
| 406 | 30 | $ur_quantity = $this->quantity; |
|
| 407 | 30 | } |
|
| 408 | |||
| 409 | 30 | if (false === $ur_quantity) { |
|
| 410 | $ur_quantity = '0'; |
||
| 411 | } |
||
| 412 | |||
| 413 | |||
| 414 | 30 | if ('' === $ur_quantity) // char(5) |
|
| 415 | 30 | { |
|
| 416 | 24 | $right = $this->getRight(); |
|
| 417 | |||
| 418 | 24 | if (!$right || !$right->getRow()) |
|
| 419 | 24 | { |
|
| 420 | bab_debug('agent right linked to invalid right '.$this->id_right); |
||
| 421 | return null; |
||
| 422 | } |
||
| 423 | |||
| 424 | // ignore quantity if right not created |
||
| 425 | 24 | if (isset($date) && $date.' 23:59:59' < $right->createdOn) { |
|
| 426 | 1 | return null; |
|
| 427 | } |
||
| 428 | |||
| 429 | 23 | $quantity = (float) $this->getRight()->quantity; // decimal(4,2) |
|
| 430 | 23 | } else { |
|
| 431 | |||
| 432 | 11 | $quantity = (float) $ur_quantity; |
|
| 433 | } |
||
| 434 | |||
| 435 | 29 | return $quantity; |
|
| 436 | } |
||
| 437 | |||
| 438 | |||
| 439 | |||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * Get total quantity for an agent |
||
| 444 | * Il s'agit de la quantite visible pour l'utilisateur final et qui n'inclu par la quantitee consommee |
||
| 445 | * @param string $date YYYY-MM-DD |
||
| 446 | * @return float in days or in hours |
||
| 447 | */ |
||
| 448 | 30 | public function getQuantity($date = null) |
|
| 449 | { |
||
| 450 | |||
| 451 | 30 | $quantity = $this->getInitialQuantity($date); |
|
| 452 | |||
| 453 | 30 | if (null === $quantity) { |
|
| 454 | // invalid or does not exists on date |
||
| 455 | 1 | return 0; |
|
| 456 | } |
||
| 457 | |||
| 458 | 29 | $quantity += $this->getIncrementQuantity($date); |
|
| 459 | 29 | $quantity += $this->getDynamicQuantity($date); |
|
| 460 | |||
| 461 | |||
| 462 | 29 | return $quantity; |
|
| 463 | } |
||
| 464 | |||
| 465 | |||
| 466 | |||
| 467 | /** |
||
| 468 | * Get quantity for the agent if no specific initial quantity |
||
| 469 | * Il s'agit de la quantite visible pour l'utilisateur final et qui n'inclu par la quantitee consommee. |
||
| 470 | * Sans tenir compte d'une eventuelle quantite initiale specifique pour l'utilisateur |
||
| 471 | * @param string $date YYYY-MM-DD |
||
| 472 | * @return float in days or in hours |
||
| 473 | */ |
||
| 474 | public function getRightQuantity($date = null) |
||
| 475 | { |
||
| 476 | $right = $this->getRight(); |
||
| 477 | $quantity = (float) $right->quantity; |
||
| 478 | $quantity += $right->getIncrementQuantity($date); |
||
| 479 | $quantity += $this->getDynamicQuantity($date); |
||
| 480 | |||
| 481 | return $quantity; |
||
| 482 | } |
||
| 483 | |||
| 484 | |||
| 485 | |||
| 486 | /** |
||
| 487 | * Get dynamic quantity |
||
| 488 | * quantite ajoutee ou retiree dynamiquement (ex diminution des RTT en fonction des arrets maladie) |
||
| 489 | * avant la date specifiee en parametre |
||
| 490 | * |
||
| 491 | * @param string $date YYYY-MM-DD |
||
| 492 | * @return float |
||
| 493 | */ |
||
| 494 | 39 | public function getDynamicQuantity($date = null) |
|
| 495 | { |
||
| 496 | 39 | $quantity = 0.0; |
|
| 497 | |||
| 498 | 39 | $I = $this->getDynamicRightIterator(); |
|
| 499 | 39 | if (isset($date)) { |
|
| 500 | 14 | $I->createdOn = $date.' 23:59:59'; |
|
| 501 | 14 | } |
|
| 502 | |||
| 503 | 39 | foreach($I as $dr) |
|
| 504 | { |
||
| 505 | /*@var $dr absences_DynamicRight */ |
||
| 506 | 2 | $quantity += $dr->getQuantity(); |
|
| 507 | 39 | } |
|
| 508 | |||
| 509 | 39 | return $quantity; |
|
| 510 | } |
||
| 511 | |||
| 512 | |||
| 513 | /** |
||
| 514 | * Reset the user agent to default right quantity |
||
| 515 | */ |
||
| 516 | public function setDefaultQuantity() |
||
| 517 | { |
||
| 518 | $agent = $this->getAgent(); |
||
| 519 | $right = $this->getRight(); |
||
| 520 | $u = $right->quantity_unit; |
||
| 521 | |||
| 522 | $old_quantity = absences_quantity($this->quantity, $u); |
||
| 523 | $new_quantity = absences_quantity($right->quantity, $u); |
||
| 524 | |||
| 525 | $this->quantity = ''; |
||
| 526 | $this->save(); |
||
| 527 | |||
| 528 | |||
| 529 | |||
| 530 | $this->addMovement(sprintf(absences_translate('The quantity for user %s on right %s has been changed from %s to %s'), |
||
| 531 | $agent->getName(), $right->description, $old_quantity, $new_quantity)); |
||
| 532 | } |
||
| 533 | |||
| 534 | |||
| 535 | |||
| 536 | |||
| 537 | /** |
||
| 538 | * Get the agentRight quantity to save in table |
||
| 539 | * the agentRight quantity will be set to default empty string |
||
| 540 | * if the input value is an empty string or the same value as the right quantity |
||
| 541 | * |
||
| 542 | * return null if the value do not need any modification |
||
| 543 | * |
||
| 544 | * @param string $inputStr The user input string from a form |
||
| 545 | * |
||
| 546 | * @return string |
||
| 547 | */ |
||
| 548 | 10 | public function getQuantityFromInput($inputStr) |
|
| 549 | { |
||
| 550 | 10 | $right = $this->getRight(); |
|
| 551 | 10 | $posted = (float) str_replace(',', '.', $inputStr); |
|
| 552 | 10 | $posted = round($posted, 2); |
|
| 553 | |||
| 554 | // les chiffres sont compares a 2 chiffre apres la virgule pour eviter les modifiaction dues a des arrondis |
||
| 555 | // si le gestionnaire clique plusieures fois sur enregistrer |
||
| 556 | |||
| 557 | 10 | $right_quantity = round(((float) $right->quantity) + $this->getDynamicQuantity() + $right->getIncrementQuantity(), 2); |
|
| 558 | 10 | $agentright_quantity = round(((float) $this->quantity) + $this->getDynamicQuantity() + $this->getAgentIncrementQuantity(), 2); |
|
| 559 | |||
| 560 | |||
| 561 | 10 | if (absences_cq($posted, $right_quantity) || '' === $inputStr) |
|
| 562 | 10 | { |
|
| 563 | 5 | if ('' === $this->quantity) { |
|
| 564 | // already use the default |
||
| 565 | 1 | return null; |
|
| 566 | } |
||
| 567 | |||
| 568 | 4 | return ''; |
|
| 569 | } |
||
| 570 | |||
| 571 | |||
| 572 | 5 | if (absences_cq($posted, 0) && '' === $this->quantity) { |
|
| 573 | |||
| 574 | // agentRight does use the default, but modification was set to 0 |
||
| 575 | // ->quantity must be set to the posted value |
||
| 576 | |||
| 577 | 1 | return (string) (-1 * ($this->getDynamicQuantity() + $this->getAgentIncrementQuantity())); |
|
| 578 | } |
||
| 579 | |||
| 580 | |||
| 581 | 4 | if (absences_cq($posted, $agentright_quantity)) { |
|
| 582 | // agentRight quantity not modified |
||
| 583 | 2 | return null; |
|
| 584 | } |
||
| 585 | |||
| 586 | |||
| 587 | 2 | return (string) ($posted - ($this->getDynamicQuantity() + $this->getAgentIncrementQuantity())); |
|
| 588 | } |
||
| 589 | |||
| 590 | |||
| 591 | |||
| 592 | |||
| 593 | /** |
||
| 594 | * Disponibilite en fonction de la date de saisie de la demande de conges |
||
| 595 | * @return string |
||
| 596 | */ |
||
| 597 | public function getDateBeginValid() |
||
| 598 | { |
||
| 599 | if ('0000-00-00' === $this->date_begin_valid) |
||
| 600 | { |
||
| 601 | return $this->getRight()->date_begin_valid; |
||
| 602 | } |
||
| 603 | |||
| 604 | return $this->date_begin_valid; |
||
| 605 | } |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Disponibilite en fonction de la date de saisie de la demande de conges |
||
| 609 | * @return string |
||
| 610 | */ |
||
| 611 | public function getDateEndValid() |
||
| 612 | { |
||
| 613 | if ('0000-00-00' === $this->date_end_valid) |
||
| 614 | { |
||
| 615 | return $this->getRight()->date_end_valid; |
||
| 616 | } |
||
| 617 | |||
| 618 | return $this->date_end_valid; |
||
| 619 | } |
||
| 620 | |||
| 621 | |||
| 622 | |||
| 623 | |||
| 624 | |||
| 625 | /** |
||
| 626 | * Get id user from row or from agent |
||
| 627 | * @return int |
||
| 628 | */ |
||
| 629 | private function getIdUser() |
||
| 630 | { |
||
| 631 | if (isset($this->id_user)) |
||
| 632 | { |
||
| 633 | return $this->id_user; |
||
| 634 | } |
||
| 635 | |||
| 636 | if (isset($this->agent)) |
||
| 637 | { |
||
| 638 | return $this->agent->getIdUser(); |
||
| 639 | } |
||
| 640 | |||
| 641 | return null; |
||
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * @return int |
||
| 646 | */ |
||
| 647 | private function getIdRight() |
||
| 648 | { |
||
| 649 | if (isset($this->id_right)) |
||
| 650 | { |
||
| 651 | return $this->id_right; |
||
| 652 | } |
||
| 653 | |||
| 654 | if (isset($this->right)) |
||
| 655 | { |
||
| 656 | return $this->right->id; |
||
| 657 | } |
||
| 658 | |||
| 659 | return null; |
||
| 660 | } |
||
| 661 | |||
| 662 | |||
| 663 | |||
| 664 | /** |
||
| 665 | * Get sum of entry element filtered by status |
||
| 666 | * @param string $status |
||
| 667 | * @param string $date YYYY-MM-DD Up to date |
||
| 668 | * @return float |
||
| 669 | */ |
||
| 670 | 21 | private function getEntryElemSum($status, $date = null) |
|
| 671 | { |
||
| 672 | |||
| 673 | 21 | require_once dirname(__FILE__).'/entry.class.php'; |
|
| 674 | 21 | $I = new absences_EntryIterator(); |
|
| 675 | |||
| 676 | 21 | View Code Duplication | if (!isset($date) || '0000-00-00' === $date) { |
| 677 | 7 | $I->status = $status; |
|
| 678 | 7 | } else { |
|
| 679 | 15 | $I->createdOn = $date.' 23:59:59'; |
|
| 680 | } |
||
| 681 | |||
| 682 | 21 | $I->users = array($this->id_user); |
|
| 683 | 21 | $I->id_right = $this->id_right; |
|
| 684 | |||
| 685 | 21 | $total = 0.0; |
|
| 686 | |||
| 687 | |||
| 688 | 21 | foreach ($I as $entry) { |
|
| 689 | |||
| 690 | /* @var $entry absences_Entry */ |
||
| 691 | |||
| 692 | 6 | $dateStatus = $entry->getDateStatus($date); |
|
| 693 | |||
| 694 | |||
| 695 | |||
| 696 | 6 | if ($dateStatus !== $status) { |
|
| 697 | 1 | continue; |
|
| 698 | } |
||
| 699 | |||
| 700 | |||
| 701 | /*@var $entry absences_Entry */ |
||
| 702 | 5 | $element = $entry->getElement($this->id_right); |
|
| 703 | |||
| 704 | 5 | if (!isset($element)) { |
|
| 705 | continue; |
||
| 706 | } |
||
| 707 | |||
| 708 | |||
| 709 | 5 | $total += (float) $element->quantity; |
|
| 710 | 21 | } |
|
| 711 | |||
| 712 | |||
| 713 | 21 | return $total; |
|
| 714 | } |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Get sum of CET deposits filtered by status |
||
| 718 | * @param string $status |
||
| 719 | * @param string $date YYYY-MM-DD Up to date |
||
| 720 | * @return number |
||
| 721 | */ |
||
| 722 | 21 | private function getCetDepositSum($status, $date = null) |
|
| 723 | { |
||
| 724 | 21 | require_once dirname(__FILE__).'/cet_deposit_request.class.php'; |
|
| 725 | 21 | $I = new absences_CetDepositRequestIterator(); |
|
| 726 | |||
| 727 | 21 | View Code Duplication | if (!isset($date) || '0000-00-00' === $date) { |
| 728 | 7 | $I->status = $status; |
|
| 729 | 7 | } else { |
|
| 730 | 15 | $I->createdOn = $date.' 23:59:59'; |
|
| 731 | } |
||
| 732 | |||
| 733 | 21 | $I->archived = null; |
|
| 734 | 21 | $I->users = array($this->id_user); |
|
| 735 | 21 | $I->id_agent_right_source = $this->id; |
|
| 736 | |||
| 737 | |||
| 738 | 21 | $total = 0.0; |
|
| 739 | |||
| 740 | 21 | foreach ($I as $cetDeposit) { |
|
| 741 | |||
| 742 | |||
| 743 | |||
| 744 | /* @var $cetDeposit absences_CetDepositRequest */ |
||
| 745 | |||
| 746 | 4 | $dateStatus = $cetDeposit->getDateStatus($date); |
|
| 747 | |||
| 748 | 4 | if ($dateStatus !== $status) { |
|
| 749 | 2 | continue; |
|
| 750 | } |
||
| 751 | |||
| 752 | 3 | $total += (float) $cetDeposit->quantity; |
|
| 753 | 21 | } |
|
| 754 | |||
| 755 | 21 | return $total; |
|
| 756 | |||
| 757 | } |
||
| 758 | |||
| 759 | |||
| 760 | |||
| 761 | |||
| 762 | /** |
||
| 763 | * Get consumed confirmed quantity |
||
| 764 | * should work if user not associated to right |
||
| 765 | * param string $date |
||
| 766 | * @return float in days or in hours |
||
| 767 | */ |
||
| 768 | 21 | public function getConfirmedQuantity($date = null) |
|
| 783 | |||
| 784 | |||
| 785 | |||
| 786 | |||
| 787 | /** |
||
| 788 | * Get consumed waiting quantity |
||
| 789 | * |
||
| 790 | * @return float in days or in hours |
||
| 791 | */ |
||
| 792 | public function getWaitingQuantity() |
||
| 793 | { |
||
| 794 | if (!isset($this->waiting_quantity)) |
||
| 795 | { |
||
| 796 | $this->waiting_quantity = $this->getEntryElemSum(''); |
||
| 797 | $this->waiting_quantity += $this->getCetDepositSum(''); |
||
| 798 | } |
||
| 799 | return $this->waiting_quantity; |
||
| 800 | } |
||
| 801 | |||
| 802 | |||
| 803 | /** |
||
| 804 | * Get previsional quantity |
||
| 805 | * |
||
| 806 | * @return float in days or in hours |
||
| 807 | */ |
||
| 808 | public function getPrevisionalQuantity() |
||
| 816 | |||
| 817 | |||
| 818 | |||
| 819 | |||
| 820 | /** |
||
| 821 | * Get available quantity |
||
| 822 | * les demandes en attente de validation ne sont pas comptabilises comme des jours consommes |
||
| 823 | * @param string $date |
||
| 824 | * @return float in days or in hours |
||
| 825 | */ |
||
| 826 | 21 | public function getAvailableQuantity($date = null) |
|
| 832 | |||
| 833 | |||
| 834 | /** |
||
| 835 | * quantite disponible (colone solde de l'export) |
||
| 836 | * @return float |
||
| 837 | */ |
||
| 838 | public function getBalance() |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Quantite disponible epargnable |
||
| 846 | * @return float |
||
| 847 | */ |
||
| 848 | public function getCetAvailableQuantity() |
||
| 852 | |||
| 853 | |||
| 854 | public function getCetMaxQuantity() |
||
| 860 | |||
| 861 | |||
| 862 | |||
| 863 | /** |
||
| 864 | * Tester si le droit est dans le regime de l'agent |
||
| 865 | * @return bool |
||
| 866 | */ |
||
| 867 | public function isRightInAgentCollection() |
||
| 877 | |||
| 878 | |||
| 879 | /** |
||
| 880 | * Liste des messages d'erreur produit par ce lien droit/utilisateur |
||
| 881 | * cette methode droit renvoyer des messages si le droit ne fonctionnera pas ou mal |
||
| 882 | * @return array |
||
| 883 | */ |
||
| 884 | public function getErrors() |
||
| 903 | |||
| 904 | |||
| 905 | |||
| 906 | /** |
||
| 907 | * Create report on right for the agent if possible |
||
| 908 | * @return bool |
||
| 909 | */ |
||
| 910 | public function createReport() |
||
| 973 | |||
| 974 | |||
| 975 | /** |
||
| 976 | * @return float in days |
||
| 977 | */ |
||
| 978 | public function getQuantityAlertConsumed() |
||
| 1027 | |||
| 1028 | |||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Retourne le message d'alerte sur la quantite consomme |
||
| 1032 | * ou null si l'alerte n'est pas ateinte |
||
| 1033 | * Le test est effectue sur la periode configuree |
||
| 1034 | * @return string | null |
||
| 1035 | */ |
||
| 1036 | public function getQuantityAlert() |
||
| 1071 | |||
| 1072 | |||
| 1073 | |||
| 1074 | |||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Attribution du droit en fonction des jours demandes et valides |
||
| 1078 | * @return bool |
||
| 1079 | */ |
||
| 1080 | public function isAccessibleAccordingToRequests() |
||
| 1086 | |||
| 1087 | |||
| 1088 | /** |
||
| 1089 | * Tester si le droit est accessible en fonction de la periode de validite du droit |
||
| 1090 | * @return bool |
||
| 1091 | */ |
||
| 1092 | public function isAccessibleByValidityPeriod() |
||
| 1106 | |||
| 1107 | |||
| 1108 | |||
| 1109 | |||
| 1110 | /** |
||
| 1111 | * Tester si le droit est accessible en fonction de la fiche d'annuaire de l'agent |
||
| 1112 | * @return bool |
||
| 1113 | */ |
||
| 1114 | public function isAcessibleByDirectoryEntry() |
||
| 1168 | |||
| 1169 | |||
| 1170 | |||
| 1171 | |||
| 1172 | /** |
||
| 1173 | * Tester si le droit est disponible dans le mois en cours (en fonction de la periode de conges demandee) |
||
| 1174 | * si cette methode renvoi true, il n'y a pas de fin de validite dans le mois en cours sauf si validoperlap est actif |
||
| 1175 | * |
||
| 1176 | * si on ce base sur cette methode pour determiner si un droit doit etre incremente dans le mois |
||
| 1177 | * - le droit sera incremente si pas de fin de validite dans le mois |
||
| 1178 | * - le droit sera incremente si le chevauchement est active et que la fin de validite est dans le mois |
||
| 1179 | * |
||
| 1180 | * |
||
| 1181 | * @return bool |
||
| 1182 | */ |
||
| 1183 | 8 | View Code Duplication | public function isAccessibleOnMonth() |
| 1191 | |||
| 1192 | |||
| 1193 | |||
| 1194 | 5 | View Code Duplication | protected function saveQuantityIncMonth($quantity) |
| 1206 | |||
| 1207 | |||
| 1208 | /** |
||
| 1209 | * Increment quantity for the month if not allready done |
||
| 1210 | * return true if quantity has been modified |
||
| 1211 | * Do not call directly because the quantity_inc_last field exists only on the right |
||
| 1212 | * |
||
| 1213 | * @param LibTimer_eventHourly $event optional event if the action is done via a background task |
||
| 1214 | * |
||
| 1215 | * @return bool |
||
| 1216 | */ |
||
| 1217 | 8 | public function monthlyQuantityUpdate(LibTimer_eventHourly $event = null) |
|
| 1259 | |||
| 1260 | |||
| 1261 | |||
| 1262 | |||
| 1263 | |||
| 1264 | /** |
||
| 1265 | * Disponibilite en fonction de la periode de conges demandee |
||
| 1266 | * le droit est accessible si on ne test pas de demande (premiere page de la demande, ne pas appeller cette methode dans ce cas) |
||
| 1267 | * |
||
| 1268 | * @param int $beginp Timestamp, debut de la periode demandee |
||
| 1269 | * @param int $endp Timestamp, fin de la periode demandee |
||
| 1270 | * |
||
| 1271 | * @return bool |
||
| 1272 | */ |
||
| 1273 | 8 | public function isAccessibleOnPeriod($beginp, $endp) |
|
| 1311 | |||
| 1312 | |||
| 1313 | |||
| 1314 | /** |
||
| 1315 | * Disponibilite en fonction de l'intervale entre date de debut de la periode de conges demandee et de la date courrante |
||
| 1316 | * le droit est accessible si on ne test pas de demande (premiere page de la demande, ne pas appeller cette methode dans ce cas) |
||
| 1317 | * |
||
| 1318 | * @param int $beginp Timestamp, debut de la periode demandee |
||
| 1319 | * |
||
| 1320 | * @return bool |
||
| 1321 | */ |
||
| 1322 | public function isAccessibleOnDelay($beginp) |
||
| 1343 | |||
| 1344 | |||
| 1345 | |||
| 1346 | /** |
||
| 1347 | * |
||
| 1348 | * @param string $message generated message |
||
| 1349 | * @param string $comment Author comment |
||
| 1350 | */ |
||
| 1351 | 8 | public function addMovement($message, $comment = '', $id_author = null) |
|
| 1368 | |||
| 1369 | |||
| 1370 | /** |
||
| 1371 | * Enregistre un mouvement suite a la modification d'un droit pour un agent |
||
| 1372 | * a partir de la liste des droit ou dans la modification d'un seul droit pour l'agent |
||
| 1373 | * |
||
| 1374 | * @param absences_Agent $agent |
||
| 1375 | * @param absences_Changes $changes |
||
| 1376 | * @param string $comment |
||
| 1377 | */ |
||
| 1378 | public function addAgentMovement(absences_Agent $agent, absences_Changes $changes, $comment) |
||
| 1398 | |||
| 1399 | |||
| 1400 | /** |
||
| 1401 | * @return absences_IncrementAgentRightIterator |
||
| 1402 | */ |
||
| 1403 | 20 | protected function getIncrementAgentRightIterator() |
|
| 1410 | |||
| 1411 | |||
| 1412 | /** |
||
| 1413 | * @return absences_IncrementRecord[] |
||
| 1414 | */ |
||
| 1415 | 29 | public function getIncrementIterator() |
|
| 1423 | |||
| 1424 | |||
| 1425 | /** |
||
| 1426 | * Somme des quantitees produites par l'incrementation mensuelle du solde, valeur specifique de l'agent |
||
| 1427 | * |
||
| 1428 | * @return float |
||
| 1429 | */ |
||
| 1430 | 10 | View Code Duplication | public function getAgentIncrementQuantity($date = null) |
| 1443 | |||
| 1444 | |||
| 1445 | |||
| 1446 | /** |
||
| 1447 | * Somme des quantitees produites par l'incrementation mensuelle du solde |
||
| 1448 | * @param string $date YYYY-MM-DD |
||
| 1449 | * @return float |
||
| 1450 | */ |
||
| 1451 | 29 | View Code Duplication | public function getIncrementQuantity($date = null) |
| 1468 | |||
| 1469 | |||
| 1470 | |||
| 1471 | /** |
||
| 1472 | * @return absences_DynamicRightIterator |
||
| 1473 | */ |
||
| 1474 | 39 | public function getDynamicRightIterator() |
|
| 1481 | |||
| 1482 | |||
| 1483 | /** |
||
| 1484 | * Somme des quantitees deja prises dans l'intervale de date specifie dans la configuration dynamique et sur les types specifies |
||
| 1485 | * peut contenir 2 lignes par demande, une en jours et une en heures |
||
| 1486 | * @return float |
||
| 1487 | */ |
||
| 1488 | protected function getDynamicConfirmedRes() |
||
| 1524 | |||
| 1525 | |||
| 1526 | /** |
||
| 1527 | * Somme des quantitees deja prises dans l'intervale de date specifie dans la configuration dynamique et sur les types specifies |
||
| 1528 | * Une seule ligne par demande, en jours |
||
| 1529 | * |
||
| 1530 | * @return array associative array with id entry as key, number of days as value |
||
| 1531 | */ |
||
| 1532 | public function getDynamicConfirmedDays() |
||
| 1568 | |||
| 1569 | |||
| 1570 | /** |
||
| 1571 | * Somme des quantitees deja prises dans l'intervale de date specifie dans la configuration dynamique et sur les types specifies |
||
| 1572 | * |
||
| 1573 | * @return float |
||
| 1574 | */ |
||
| 1575 | public function getDynamicTotalConfirmedDays() |
||
| 1586 | |||
| 1587 | |||
| 1588 | |||
| 1589 | |||
| 1590 | /** |
||
| 1591 | * Liste dynamique des modification du solde |
||
| 1592 | * Calculee a partir de la configuration de absences_dynamic_configuration |
||
| 1593 | * indexe par id_user_right/id_entry |
||
| 1594 | * @return array |
||
| 1595 | */ |
||
| 1596 | public function getDynamicMovements() |
||
| 1655 | |||
| 1656 | /** |
||
| 1657 | * Liste des movements de solde dynamiques enregistres en base de donnes, indexes par id_user_right/id_entry |
||
| 1658 | */ |
||
| 1659 | public function getDynamicRights() |
||
| 1670 | |||
| 1671 | |||
| 1672 | /** |
||
| 1673 | * Update the dynamic rights |
||
| 1674 | * creer les lignes corespondantes a la configuration dans la table absences_dynamic_rights |
||
| 1675 | * retirer les lignes qui ne sont plus valides |
||
| 1676 | */ |
||
| 1677 | public function applyDynamicRight() |
||
| 1714 | |||
| 1715 | |||
| 1716 | /** |
||
| 1717 | * Movements related to the agentRight |
||
| 1718 | * @return absences_MovementIterator |
||
| 1719 | */ |
||
| 1720 | public function getMovementIterator() |
||
| 1730 | } |
||
| 1731 | |||
| 2299 |
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.