mod_contract_serpis.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  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/contract/mod_contract_serpis.php
  20. * \ingroup contract
  21. * \brief File of class to manage contract numbering rules Serpis
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/core/modules/contract/modules_contract.php';
  24. /**
  25. * Class to manage contract numbering rules Serpis
  26. */
  27. class mod_contract_serpis extends ModelNumRefContracts
  28. {
  29. /**
  30. * Dolibarr version of the loaded document
  31. * @var string
  32. */
  33. public $version = 'dolibarr';
  34. public $prefix = 'CT';
  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 = 'Serpis';
  45. /**
  46. * @var string model name
  47. */
  48. public $name = 'Serpis';
  49. /**
  50. * @var int Automatic numbering
  51. */
  52. public $code_auto = 1;
  53. /**
  54. * Return default description of numbering model
  55. *
  56. * @param Translate $langs Lang object to use for output
  57. * @return string Descriptive text
  58. */
  59. public function info($langs)
  60. {
  61. global $langs;
  62. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  63. }
  64. /**
  65. * Return numbering example
  66. *
  67. * @return string Example
  68. */
  69. public function getExample()
  70. {
  71. return $this->prefix."0501-0001";
  72. }
  73. /**
  74. * Test if existing numbers make problems with numbering
  75. *
  76. * @param Object $object Object we need next value for
  77. * @return boolean false if conflict, true if ok
  78. */
  79. public function canBeActivated($object)
  80. {
  81. global $conf, $langs, $db;
  82. $coyymm = '';
  83. $max = '';
  84. $posindice = strlen($this->prefix) + 6;
  85. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  86. $sql .= " FROM ".MAIN_DB_PREFIX."contrat";
  87. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  88. $sql .= " AND entity = ".$conf->entity;
  89. $resql = $db->query($sql);
  90. if ($resql) {
  91. $row = $db->fetch_row($resql);
  92. if ($row) {
  93. $coyymm = substr($row[0], 0, 6);
  94. $max = $row[0];
  95. }
  96. }
  97. if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
  98. $langs->load("errors");
  99. $this->error = $langs->trans('ErrorNumRefModel', $max);
  100. return false;
  101. }
  102. return true;
  103. }
  104. /**
  105. * Return next value
  106. *
  107. * @param Societe $objsoc third party object
  108. * @param Object $contract contract object
  109. * @return string Value if OK, 0 if KO
  110. */
  111. public function getNextValue($objsoc, $contract)
  112. {
  113. global $db, $conf;
  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."contrat";
  117. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  118. $sql .= " AND entity = ".$conf->entity;
  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_contract_serpis::getNextValue", LOG_DEBUG);
  129. return -1;
  130. }
  131. $date = $contract->date_contrat;
  132. $yymm = strftime("%y%m", $date);
  133. if ($max >= (pow(10, 4) - 1)) {
  134. $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  135. } else {
  136. $num = sprintf("%04s", $max + 1);
  137. }
  138. dol_syslog("mod_contract_serpis::getNextValue return ".$this->prefix.$yymm."-".$num);
  139. return $this->prefix.$yymm."-".$num;
  140. }
  141. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  142. /**
  143. * Return next value
  144. *
  145. * @param Societe $objsoc third party object
  146. * @param Object $objforref contract object
  147. * @return string Value if OK, 0 if KO
  148. */
  149. public function contract_get_num($objsoc, $objforref)
  150. {
  151. // phpcs:enable
  152. return $this->getNextValue($objsoc, $objforref);
  153. }
  154. }