mod_holiday_madonna.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  3. * Copyright (C) 2018 Charlene Benke <charlie@patas-monkey.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/modules/holiday/mod_holiday_madonna.php
  21. * \ingroup contract
  22. * \brief File of class to manage contract numbering rules Serpis
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/modules/holiday/modules_holiday.php';
  25. /**
  26. * Class to manage contract numbering rules madonna
  27. */
  28. class mod_holiday_madonna extends ModelNumRefHolidays
  29. {
  30. /**
  31. * Dolibarr version of the loaded document
  32. * @var string
  33. */
  34. public $version = 'dolibarr';
  35. public $prefix = 'HL';
  36. /**
  37. * @var string Error code (or message)
  38. */
  39. public $error = '';
  40. /**
  41. * @var string Nom du modele
  42. * @deprecated
  43. * @see $name
  44. */
  45. public $nom = 'Madonna';
  46. /**
  47. * @var string model name
  48. */
  49. public $name = 'Madonna';
  50. /**
  51. * @var int Automatic numbering
  52. */
  53. public $code_auto = 1;
  54. /**
  55. * Return default description of numbering model
  56. *
  57. * @return string text description
  58. */
  59. public function info()
  60. {
  61. global $langs;
  62. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  63. }
  64. /**
  65. * Return numbering example
  66. *
  67. * @return string Example
  68. */
  69. public function getExample()
  70. {
  71. return $this->prefix."0501-0001";
  72. }
  73. /**
  74. * Test if existing numbers make problems with numbering
  75. *
  76. * @return boolean false if conflit, true if ok
  77. */
  78. public function canBeActivated()
  79. {
  80. global $conf, $langs, $db;
  81. $coyymm = '';
  82. $max = '';
  83. $posindice = strlen($this->prefix) + 6;
  84. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  85. $sql .= " FROM ".MAIN_DB_PREFIX."holiday";
  86. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  87. $sql .= " AND entity = ".$conf->entity;
  88. $resql = $db->query($sql);
  89. if ($resql) {
  90. $row = $db->fetch_row($resql);
  91. if ($row) {
  92. $coyymm = substr($row[0], 0, 6);
  93. $max = $row[0];
  94. }
  95. }
  96. if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
  97. $langs->load("errors");
  98. $this->error = $langs->trans('ErrorNumRefModel', $max);
  99. return false;
  100. }
  101. return true;
  102. }
  103. /**
  104. * Return next value
  105. *
  106. * @param Societe $objsoc third party object
  107. * @param Object $holiday Holiday object
  108. * @return string Value if OK, 0 if KO
  109. */
  110. public function getNextValue($objsoc, $holiday)
  111. {
  112. global $db, $conf;
  113. $posindice = strlen($this->prefix) + 6;
  114. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  115. $sql .= " FROM ".MAIN_DB_PREFIX."holiday";
  116. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  117. $sql .= " AND entity = ".$conf->entity;
  118. $resql = $db->query($sql);
  119. if ($resql) {
  120. $obj = $db->fetch_object($resql);
  121. if ($obj) {
  122. $max = intval($obj->max);
  123. } else {
  124. $max = 0;
  125. }
  126. } else {
  127. dol_syslog("mod_holiday_madonna::getNextValue", LOG_DEBUG);
  128. return -1;
  129. }
  130. $date = $holiday->date_debut;
  131. $yymm = strftime("%y%m", $date);
  132. if ($max >= (pow(10, 4) - 1)) {
  133. $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  134. } else {
  135. $num = sprintf("%04s", $max + 1);
  136. }
  137. dol_syslog("mod_holiday_madonna::getNextValue return ".$this->prefix.$yymm."-".$num);
  138. return $this->prefix.$yymm."-".$num;
  139. }
  140. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  141. /**
  142. * Return next value
  143. *
  144. * @param User $fuser User object
  145. * @param Object $objforref Holiday object
  146. * @return string Value if OK, 0 if KO
  147. */
  148. public function holiday_get_num($fuser, $objforref)
  149. {
  150. // phpcs:enable
  151. return $this->getNextValue($fuser, $objforref);
  152. }
  153. }