mod_reception_beryl.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. * @return string text description
  37. */
  38. public function info()
  39. {
  40. global $langs;
  41. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  42. }
  43. /**
  44. * Return numbering example
  45. *
  46. * @return string Example
  47. */
  48. public function getExample()
  49. {
  50. return $this->prefix."0501-0001";
  51. }
  52. /**
  53. * Test if existing numbers make problems with numbering
  54. *
  55. * @return boolean false if conflit, true if ok
  56. */
  57. public function canBeActivated()
  58. {
  59. global $conf, $langs, $db;
  60. $coyymm = '';
  61. $max = '';
  62. $posindice = strlen($this->prefix) + 6;
  63. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  64. $sql .= " FROM ".MAIN_DB_PREFIX."reception";
  65. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  66. $sql .= " AND entity = ".$conf->entity;
  67. $resql = $db->query($sql);
  68. if ($resql) {
  69. $row = $db->fetch_row($resql);
  70. if ($row) {
  71. $coyymm = substr($row[0], 0, 6);
  72. $max = $row[0];
  73. }
  74. }
  75. if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
  76. $langs->load("errors");
  77. $this->error = $langs->trans('ErrorNumRefModel', $max);
  78. return false;
  79. }
  80. return true;
  81. }
  82. /**
  83. * Return next value
  84. *
  85. * @param Societe $objsoc Third party object
  86. * @param Object $reception Reception object
  87. * @return string Value if OK, 0 if KO
  88. */
  89. public function getNextValue($objsoc, $reception)
  90. {
  91. global $db, $conf;
  92. $posindice = strlen($this->prefix) + 6;
  93. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  94. $sql .= " FROM ".MAIN_DB_PREFIX."reception";
  95. $sql .= " WHERE ref like '".$db->escape($this->prefix)."____-%'";
  96. $sql .= " AND entity = ".$conf->entity;
  97. $resql = $db->query($sql);
  98. if ($resql) {
  99. $obj = $db->fetch_object($resql);
  100. if ($obj) {
  101. $max = intval($obj->max);
  102. } else {
  103. $max = 0;
  104. }
  105. } else {
  106. dol_syslog("mod_reception_beryl::getNextValue", LOG_DEBUG);
  107. return -1;
  108. }
  109. $date = time();
  110. $yymm = strftime("%y%m", $date);
  111. if ($max >= (pow(10, 4) - 1)) {
  112. $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  113. } else {
  114. $num = sprintf("%04s", $max + 1);
  115. }
  116. dol_syslog("mod_reception_beryl::getNextValue return ".$this->prefix.$yymm."-".$num);
  117. return $this->prefix.$yymm."-".$num;
  118. }
  119. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  120. /**
  121. * Return next free value
  122. *
  123. * @param Societe $objsoc Third party object
  124. * @param Object $objforref Shipment object
  125. * @return string Next free value
  126. */
  127. public function reception_get_num($objsoc, $objforref)
  128. {
  129. // phpcs:enable
  130. return $this->getNextValue($objsoc, $objforref);
  131. }
  132. }