mod_reception_beryl.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /* Copyright (C) 2018 Quentin Vial-Gouteyron <quentin.vial-gouteyron@atm-consulting.fr>
  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/reception/mod_reception_beryl.php
  20. * \ingroup reception
  21. * \brief File of class to manage shipments numbering rules Beryl
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/core/modules/reception/modules_reception.php';
  24. /**
  25. * Class to manage reception numbering rules Beryl
  26. */
  27. class mod_reception_beryl extends ModelNumRefReception
  28. {
  29. public $version = 'dolibarr';
  30. public $prefix = 'RCP';
  31. public $error = '';
  32. public $nom = 'Beryl';
  33. /**
  34. * Return default description of numbering model
  35. *
  36. * @param Translate $langs Lang object to use for output
  37. * @return string Descriptive text
  38. */
  39. public function info($langs)
  40. {
  41. global $langs;
  42. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  43. }
  44. /**
  45. * Return numbering example
  46. *
  47. * @return string Example
  48. */
  49. public function getExample()
  50. {
  51. return $this->prefix."0501-0001";
  52. }
  53. /**
  54. * Test if existing numbers make problems with numbering
  55. *
  56. * @param Object $object Object we need next value for
  57. * @return boolean false if KO (there is a conflict), true if OK
  58. */
  59. public function canBeActivated($object)
  60. {
  61. global $conf, $langs, $db;
  62. $coyymm = '';
  63. $max = '';
  64. $posindice = strlen($this->prefix) + 6;
  65. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  66. $sql .= " FROM ".MAIN_DB_PREFIX."reception";
  67. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  68. $sql .= " AND entity = ".$conf->entity;
  69. $resql = $db->query($sql);
  70. if ($resql) {
  71. $row = $db->fetch_row($resql);
  72. if ($row) {
  73. $coyymm = substr($row[0], 0, 6);
  74. $max = $row[0];
  75. }
  76. }
  77. if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
  78. $langs->load("errors");
  79. $this->error = $langs->trans('ErrorNumRefModel', $max);
  80. return false;
  81. }
  82. return true;
  83. }
  84. /**
  85. * Return next value
  86. *
  87. * @param Societe $objsoc Third party object
  88. * @param Object $reception Reception object
  89. * @return string Value if OK, 0 if KO
  90. */
  91. public function getNextValue($objsoc, $reception)
  92. {
  93. global $db, $conf;
  94. $posindice = strlen($this->prefix) + 6;
  95. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  96. $sql .= " FROM ".MAIN_DB_PREFIX."reception";
  97. $sql .= " WHERE ref like '".$db->escape($this->prefix)."____-%'";
  98. $sql .= " AND entity = ".$conf->entity;
  99. $resql = $db->query($sql);
  100. if ($resql) {
  101. $obj = $db->fetch_object($resql);
  102. if ($obj) {
  103. $max = intval($obj->max);
  104. } else {
  105. $max = 0;
  106. }
  107. } else {
  108. dol_syslog("mod_reception_beryl::getNextValue", LOG_DEBUG);
  109. return -1;
  110. }
  111. $date = time();
  112. $yymm = strftime("%y%m", $date);
  113. if ($max >= (pow(10, 4) - 1)) {
  114. $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  115. } else {
  116. $num = sprintf("%04s", $max + 1);
  117. }
  118. dol_syslog("mod_reception_beryl::getNextValue return ".$this->prefix.$yymm."-".$num);
  119. return $this->prefix.$yymm."-".$num;
  120. }
  121. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  122. /**
  123. * Return next free value
  124. *
  125. * @param Societe $objsoc Third party object
  126. * @param Object $objforref Shipment object
  127. * @return string Next free value
  128. */
  129. public function reception_get_num($objsoc, $objforref)
  130. {
  131. // phpcs:enable
  132. return $this->getNextValue($objsoc, $objforref);
  133. }
  134. }