mod_asset_standard.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /* Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2022 Frédéric France <frederic.france@netlogic.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. * or see https://www.gnu.org/
  19. */
  20. /**
  21. * \file htdocs/core/modules/asset/mod_asset_standard.php
  22. * \ingroup asset
  23. * \brief File of class to manage Asset numbering rules standard
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/core/modules/asset/modules_asset.php';
  26. /**
  27. * Class to manage the Standard numbering rule for Asset
  28. */
  29. class mod_asset_standard extends ModeleNumRefAsset
  30. {
  31. /**
  32. * Dolibarr version of the loaded document
  33. * @var string
  34. */
  35. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  36. public $prefix = 'ASSET';
  37. /**
  38. * @var string Error code (or message)
  39. */
  40. public $error = '';
  41. /**
  42. * @var string name
  43. */
  44. public $name = 'standard';
  45. /**
  46. * Return description of numbering module
  47. *
  48. * @param Translate $langs Lang object to use for output
  49. * @return string Descriptive text
  50. */
  51. public function info($langs)
  52. {
  53. global $langs;
  54. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  55. }
  56. /**
  57. * Return an example of numbering
  58. *
  59. * @return string Example
  60. */
  61. public function getExample()
  62. {
  63. return $this->prefix."0501-0001";
  64. }
  65. /**
  66. * Checks if the numbers already in the database do not
  67. * cause conflicts that would prevent this numbering working.
  68. *
  69. * @param Object $object Object we need next value for
  70. * @return boolean false if conflict, true if ok
  71. */
  72. public function canBeActivated($object)
  73. {
  74. global $conf, $langs, $db;
  75. $coyymm = '';
  76. $max = '';
  77. $posindice = strlen($this->prefix) + 6;
  78. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  79. $sql .= " FROM ".MAIN_DB_PREFIX."asset_asset";
  80. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  81. if ($object->ismultientitymanaged == 1) {
  82. $sql .= " AND entity = ".$conf->entity;
  83. } elseif ($object->ismultientitymanaged == 2) {
  84. // TODO
  85. }
  86. $resql = $db->query($sql);
  87. if ($resql) {
  88. $row = $db->fetch_row($resql);
  89. if ($row) {
  90. $coyymm = substr($row[0], 0, 6); $max = $row[0];
  91. }
  92. }
  93. if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
  94. $langs->load("errors");
  95. $this->error = $langs->trans('ErrorNumRefModel', $max);
  96. return false;
  97. }
  98. return true;
  99. }
  100. /**
  101. * Return next free value
  102. *
  103. * @param Object $object Object we need next value for
  104. * @return string Value if KO, <0 if KO
  105. */
  106. public function getNextValue($object)
  107. {
  108. global $db, $conf;
  109. // first we get the max value
  110. $posindice = strlen($this->prefix) + 6;
  111. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  112. $sql .= " FROM ".MAIN_DB_PREFIX."asset";
  113. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  114. if ($object->ismultientitymanaged == 1) {
  115. $sql .= " AND entity = ".$conf->entity;
  116. } elseif ($object->ismultientitymanaged == 2) {
  117. // TODO
  118. }
  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_asset_standard::getNextValue", LOG_DEBUG);
  129. return -1;
  130. }
  131. //$date=time();
  132. $date = $object->date_creation;
  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_asset_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
  140. return $this->prefix.$yymm."-".$num;
  141. }
  142. }