mod_holiday_madonna.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * @param Translate $langs Lang object to use for output
  58. * @return string Descriptive text
  59. */
  60. public function info($langs)
  61. {
  62. global $langs;
  63. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  64. }
  65. /**
  66. * Return numbering example
  67. *
  68. * @return string Example
  69. */
  70. public function getExample()
  71. {
  72. return $this->prefix."0501-0001";
  73. }
  74. /**
  75. * Test if existing numbers make problems with numbering
  76. *
  77. * @param Object $object Object we need next value for
  78. * @return boolean false if conflict, true if ok
  79. */
  80. public function canBeActivated($object)
  81. {
  82. global $conf, $langs, $db;
  83. $coyymm = '';
  84. $max = '';
  85. $posindice = strlen($this->prefix) + 6;
  86. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  87. $sql .= " FROM ".MAIN_DB_PREFIX."holiday";
  88. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  89. $sql .= " AND entity = ".$conf->entity;
  90. $resql = $db->query($sql);
  91. if ($resql) {
  92. $row = $db->fetch_row($resql);
  93. if ($row) {
  94. $coyymm = substr($row[0], 0, 6);
  95. $max = $row[0];
  96. }
  97. }
  98. if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
  99. $langs->load("errors");
  100. $this->error = $langs->trans('ErrorNumRefModel', $max);
  101. return false;
  102. }
  103. return true;
  104. }
  105. /**
  106. * Return next value
  107. *
  108. * @param Societe $objsoc third party object
  109. * @param Object $holiday Holiday object
  110. * @return string Value if OK, 0 if KO
  111. */
  112. public function getNextValue($objsoc, $holiday)
  113. {
  114. global $db, $conf;
  115. $posindice = strlen($this->prefix) + 6;
  116. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  117. $sql .= " FROM ".MAIN_DB_PREFIX."holiday";
  118. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  119. $sql .= " AND entity = ".$conf->entity;
  120. $resql = $db->query($sql);
  121. if ($resql) {
  122. $obj = $db->fetch_object($resql);
  123. if ($obj) {
  124. $max = intval($obj->max);
  125. } else {
  126. $max = 0;
  127. }
  128. } else {
  129. dol_syslog("mod_holiday_madonna::getNextValue", LOG_DEBUG);
  130. return -1;
  131. }
  132. $date = $holiday->date_debut;
  133. $yymm = strftime("%y%m", $date);
  134. if ($max >= (pow(10, 4) - 1)) {
  135. $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  136. } else {
  137. $num = sprintf("%04s", $max + 1);
  138. }
  139. dol_syslog("mod_holiday_madonna::getNextValue return ".$this->prefix.$yymm."-".$num);
  140. return $this->prefix.$yymm."-".$num;
  141. }
  142. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  143. /**
  144. * Return next value
  145. *
  146. * @param User $fuser User object
  147. * @param Object $objforref Holiday object
  148. * @return string Value if OK, 0 if KO
  149. */
  150. public function holiday_get_num($fuser, $objforref)
  151. {
  152. // phpcs:enable
  153. return $this->getNextValue($fuser, $objforref);
  154. }
  155. }