mod_member_simple.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /* Copyright (C) 2021 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/modules/member/mod_member_simple.php
  20. * \ingroup member
  21. * \brief File with class to manage the numbering module Simple for member references
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/core/modules/member/modules_member.class.php';
  24. /**
  25. * Class to manage the numbering module Simple for member references
  26. */
  27. class mod_member_simple extends ModeleNumRefMembers
  28. {
  29. /**
  30. * Dolibarr version of the loaded document
  31. * @var string
  32. */
  33. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  34. public $prefix = 'MEM';
  35. /**
  36. * @var string Error code (or message)
  37. */
  38. public $error = '';
  39. /**
  40. * @var string Nom du modele
  41. * @deprecated
  42. * @see $name
  43. */
  44. public $nom = 'Simple';
  45. /**
  46. * @var string model name
  47. */
  48. public $name = 'Simple';
  49. /**
  50. * Return description of numbering module
  51. *
  52. * @return string Text with description
  53. */
  54. public function info()
  55. {
  56. global $langs;
  57. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  58. }
  59. /**
  60. * Return an example of numbering module values
  61. *
  62. * @return string Example
  63. */
  64. public function getExample()
  65. {
  66. return $this->prefix."0501-0001";
  67. }
  68. /**
  69. * Checks if the numbers already in the database do not
  70. * cause conflicts that would prevent this numbering working.
  71. *
  72. * @return boolean false if conflict, true if ok
  73. */
  74. public function canBeActivated()
  75. {
  76. global $conf, $langs, $db;
  77. $coyymm = '';
  78. $max = '';
  79. $posindice = strlen($this->prefix) + 6;
  80. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  81. $sql .= " FROM ".MAIN_DB_PREFIX."adherent";
  82. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  83. $sql .= " AND entity = ".$conf->entity;
  84. $resql = $db->query($sql);
  85. if ($resql) {
  86. $row = $db->fetch_row($resql);
  87. if ($row) {
  88. $coyymm = substr($row[0], 0, 6);
  89. $max = $row[0];
  90. }
  91. }
  92. if (!$coyymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
  93. return true;
  94. } else {
  95. $langs->load("errors");
  96. $this->error = $langs->trans('ErrorNumRefModel', $max);
  97. return false;
  98. }
  99. }
  100. /**
  101. * Return next value
  102. *
  103. * @param Societe $objsoc Object third party
  104. * @param Object $object Object we need next value for
  105. * @return string Value if OK, 0 if KO
  106. */
  107. public function getNextValue($objsoc, $object)
  108. {
  109. global $db, $conf;
  110. /*
  111. // First, we get the max value
  112. $posindice = strlen($this->prefix) + 6;
  113. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  114. $sql .= " FROM ".MAIN_DB_PREFIX."adherent";
  115. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  116. $sql .= " AND entity = ".$conf->entity;
  117. $resql = $db->query($sql);
  118. if ($resql) {
  119. $obj = $db->fetch_object($resql);
  120. if ($obj) {
  121. $max = intval($obj->max);
  122. } else {
  123. $max = 0;
  124. }
  125. } else {
  126. dol_syslog("mod_member_simple::getNextValue", LOG_DEBUG);
  127. return -1;
  128. }
  129. $date = empty($object->date_c) ? dol_now() : $object->date_c;
  130. //$yymm = strftime("%y%m",time());
  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_member_simple::getNextValue return ".$this->prefix.$yymm."-".$num);
  138. return $this->prefix.$yymm."-".$num;
  139. */
  140. // For the moment, the ref of a member is the rowid
  141. $sql = "SELECT MAX(rowid) as max";
  142. $sql .= " FROM ".MAIN_DB_PREFIX."adherent";
  143. $resql = $db->query($sql);
  144. if ($resql) {
  145. $obj = $db->fetch_object($resql);
  146. if ($obj) {
  147. $max = intval($obj->max);
  148. } else {
  149. $max = 0;
  150. }
  151. } else {
  152. dol_syslog("mod_member_simple::getNextValue", LOG_DEBUG);
  153. return -1;
  154. }
  155. return ($max + 1);
  156. }
  157. }