mod_asset_standard.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. *
  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/asset/mod_asset_standard.php
  21. * \ingroup asset
  22. * \brief File of class to manage Asset numbering rules standard
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/modules/asset/modules_asset.php';
  25. /**
  26. * Class to manage customer order numbering rules standard
  27. */
  28. class mod_asset_standard extends ModeleNumRefAsset
  29. {
  30. /**
  31. * Dolibarr version of the loaded document
  32. * @var string
  33. */
  34. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  35. public $prefix = 'ASSET';
  36. /**
  37. * @var string Error code (or message)
  38. */
  39. public $error = '';
  40. /**
  41. * @var string name
  42. */
  43. public $name = 'standard';
  44. /**
  45. * Return description of numbering module
  46. *
  47. * @return string Text with description
  48. */
  49. public function info()
  50. {
  51. global $langs;
  52. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  53. }
  54. /**
  55. * Return an example of numbering
  56. *
  57. * @return string Example
  58. */
  59. public function getExample()
  60. {
  61. return $this->prefix."0501-0001";
  62. }
  63. /**
  64. * Checks if the numbers already in the database do not
  65. * cause conflicts that would prevent this numbering working.
  66. *
  67. * @param Object $object Object we need next value for
  68. * @return boolean false if conflict, true if ok
  69. */
  70. public function canBeActivated($object)
  71. {
  72. global $conf, $langs, $db;
  73. $coyymm = ''; $max = '';
  74. $posindice = strlen($this->prefix) + 6;
  75. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  76. $sql .= " FROM ".MAIN_DB_PREFIX."asset_asset";
  77. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  78. if ($object->ismultientitymanaged == 1) {
  79. $sql .= " AND entity = ".$conf->entity;
  80. } elseif ($object->ismultientitymanaged == 2) {
  81. // TODO
  82. }
  83. $resql = $db->query($sql);
  84. if ($resql) {
  85. $row = $db->fetch_row($resql);
  86. if ($row) {
  87. $coyymm = substr($row[0], 0, 6); $max = $row[0];
  88. }
  89. }
  90. if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
  91. $langs->load("errors");
  92. $this->error = $langs->trans('ErrorNumRefModel', $max);
  93. return false;
  94. }
  95. return true;
  96. }
  97. /**
  98. * Return next free value
  99. *
  100. * @param Object $object Object we need next value for
  101. * @return string Value if KO, <0 if KO
  102. */
  103. public function getNextValue($object)
  104. {
  105. global $db, $conf;
  106. // first we get the max value
  107. $posindice = strlen($this->prefix) + 6;
  108. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  109. $sql .= " FROM ".MAIN_DB_PREFIX."asset_asset";
  110. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  111. if ($object->ismultientitymanaged == 1) {
  112. $sql .= " AND entity = ".$conf->entity;
  113. } elseif ($object->ismultientitymanaged == 2) {
  114. // TODO
  115. }
  116. $resql = $db->query($sql);
  117. if ($resql) {
  118. $obj = $db->fetch_object($resql);
  119. if ($obj) {
  120. $max = intval($obj->max);
  121. } else {
  122. $max = 0;
  123. }
  124. } else {
  125. dol_syslog("mod_asset_standard::getNextValue", LOG_DEBUG);
  126. return -1;
  127. }
  128. //$date=time();
  129. $date = $object->date_creation;
  130. $yymm = strftime("%y%m", $date);
  131. if ($max >= (pow(10, 4) - 1)) {
  132. $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  133. } else {
  134. $num = sprintf("%04s", $max + 1);
  135. }
  136. dol_syslog("mod_asset_standard::getNextValue return ".$this->prefix.$yymm."-".$num);
  137. return $this->prefix.$yymm."-".$num;
  138. }
  139. }