mod_project_simple.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /* Copyright (C) 2010-2012 Regis Houssin <regis.houssin@inodbox.com>
  3. * Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  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/project/mod_project_simple.php
  21. * \ingroup project
  22. * \brief File with class to manage the numbering module Simple for project references
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/modules/project/modules_project.php';
  25. /**
  26. * Class to manage the numbering module Simple for project references
  27. */
  28. class mod_project_simple extends ModeleNumRefProjects
  29. {
  30. /**
  31. * Dolibarr version of the loaded document
  32. * @var string
  33. */
  34. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  35. public $prefix = 'PJ';
  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 = 'Simple';
  46. /**
  47. * @var string model name
  48. */
  49. public $name = 'Simple';
  50. /**
  51. * Return description of numbering module
  52. *
  53. * @param Translate $langs Lang object to use for output
  54. * @return string Descriptive text
  55. */
  56. public function info($langs)
  57. {
  58. global $langs;
  59. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  60. }
  61. /**
  62. * Return an example of numbering module values
  63. *
  64. * @return string Example
  65. */
  66. public function getExample()
  67. {
  68. return $this->prefix."0501-0001";
  69. }
  70. /**
  71. * Checks if the numbers already in the database do not
  72. * cause conflicts that would prevent this numbering working.
  73. *
  74. * @param Object $object Object we need next value for
  75. * @return boolean false if KO (there is a conflict), true if OK
  76. */
  77. public function canBeActivated($object)
  78. {
  79. global $conf, $langs, $db;
  80. $coyymm = '';
  81. $max = '';
  82. $posindice = strlen($this->prefix) + 6;
  83. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  84. $sql .= " FROM ".MAIN_DB_PREFIX."projet";
  85. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  86. $sql .= " AND entity = ".$conf->entity;
  87. $resql = $db->query($sql);
  88. if ($resql) {
  89. $row = $db->fetch_row($resql);
  90. if ($row) {
  91. $coyymm = substr($row[0], 0, 6);
  92. $max = $row[0];
  93. }
  94. }
  95. if (!$coyymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
  96. return true;
  97. } else {
  98. $langs->load("errors");
  99. $this->error = $langs->trans('ErrorNumRefModel', $max);
  100. return false;
  101. }
  102. }
  103. /**
  104. * Return next value
  105. *
  106. * @param Societe $objsoc Object third party
  107. * @param Project $project Object project
  108. * @return string Value if OK, 0 if KO
  109. */
  110. public function getNextValue($objsoc, $project)
  111. {
  112. global $db;
  113. // First, we get the max value
  114. $posindice = strlen($this->prefix) + 6;
  115. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  116. $sql .= " FROM ".MAIN_DB_PREFIX."projet";
  117. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  118. $sql .= " AND entity IN (".getEntity('projectnumber', 1, $project).")";
  119. $resql = $db->query($sql);
  120. if ($resql) {
  121. $obj = $db->fetch_object($resql);
  122. if ($obj) {
  123. $max = intval($obj->max);
  124. } else {
  125. $max = 0;
  126. }
  127. } else {
  128. dol_syslog("mod_project_simple::getNextValue", LOG_DEBUG);
  129. return -1;
  130. }
  131. $date = (empty($project->date_c) ? dol_now() : $project->date_c);
  132. //$yymm = strftime("%y%m",time());
  133. //$yymm = strftime("%y%m", $date);
  134. $yymm = dol_print_date($date, "%y%m");
  135. if ($max >= (pow(10, 4) - 1)) {
  136. $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  137. } else {
  138. $num = sprintf("%04s", $max + 1);
  139. }
  140. dol_syslog("mod_project_simple::getNextValue return ".$this->prefix.$yymm."-".$num);
  141. return $this->prefix.$yymm."-".$num;
  142. }
  143. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  144. /**
  145. * Return next reference not yet used as a reference
  146. *
  147. * @param Societe $objsoc Object third party
  148. * @param Project $project Object project
  149. * @return string Next not used reference
  150. */
  151. public function project_get_num($objsoc = 0, $project = '')
  152. {
  153. // phpcs:enable
  154. return $this->getNextValue($objsoc, $project);
  155. }
  156. }