mod_commande_marbre.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/commande/mod_commande_marbre.php
  21. * \ingroup commande
  22. * \brief File of class to manage Sales Order numbering rules Marbre
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/modules/commande/modules_commande.php';
  25. /**
  26. * Class to manage Sales Order numbering rules Marbre
  27. */
  28. class mod_commande_marbre extends ModeleNumRefCommandes
  29. {
  30. /**
  31. * Dolibarr version of the loaded document
  32. * @var string
  33. */
  34. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  35. public $prefix = 'CO';
  36. /**
  37. * @var string Error code (or message)
  38. */
  39. public $error = '';
  40. /**
  41. * @var string name
  42. */
  43. public $name = 'Marbre';
  44. /**
  45. * Constructor
  46. */
  47. public function __construct()
  48. {
  49. global $conf, $mysoc;
  50. if ((float) $conf->global->MAIN_VERSION_LAST_INSTALL >= 16.0 && $mysoc->country_code != 'FR') {
  51. $this->prefix = 'SO'; // We use correct standard code "SO = Sale Order"
  52. }
  53. }
  54. /**
  55. * Return description of numbering module
  56. *
  57. * @param Translate $langs Lang object to use for output
  58. * @return string Descriptive text
  59. */
  60. public function info($langs)
  61. {
  62. global $langs;
  63. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  64. }
  65. /**
  66. * Return an example of numbering
  67. *
  68. * @return string Example
  69. */
  70. public function getExample()
  71. {
  72. return $this->prefix."0501-0001";
  73. }
  74. /**
  75. * Checks if the numbers already in the database do not
  76. * cause conflicts that would prevent this numbering working.
  77. *
  78. * @param Object $object Object we need next value for
  79. * @return boolean false if conflict, true if ok
  80. */
  81. public function canBeActivated($object)
  82. {
  83. global $conf, $langs, $db;
  84. $coyymm = '';
  85. $max = '';
  86. $posindice = strlen($this->prefix) + 6;
  87. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  88. $sql .= " FROM ".MAIN_DB_PREFIX."commande";
  89. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  90. $sql .= " AND entity = ".$conf->entity;
  91. $resql = $db->query($sql);
  92. if ($resql) {
  93. $row = $db->fetch_row($resql);
  94. if ($row) {
  95. $coyymm = substr($row[0], 0, 6);
  96. $max = $row[0];
  97. }
  98. }
  99. if ($coyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
  100. $langs->load("errors");
  101. $this->error = $langs->trans('ErrorNumRefModel', $max);
  102. return false;
  103. }
  104. return true;
  105. }
  106. /**
  107. * Return next free value
  108. *
  109. * @param Societe $objsoc Object thirdparty
  110. * @param Object $object Object we need next value for
  111. * @return string Value if KO, <0 if KO
  112. */
  113. public function getNextValue($objsoc, $object)
  114. {
  115. global $db, $conf;
  116. // First, we get the max value
  117. $posindice = strlen($this->prefix) + 6;
  118. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  119. $sql .= " FROM ".MAIN_DB_PREFIX."commande";
  120. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  121. $sql .= " AND entity IN (".getEntity('ordernumber', 1, $object).")";
  122. $resql = $db->query($sql);
  123. if ($resql) {
  124. $obj = $db->fetch_object($resql);
  125. if ($obj) {
  126. $max = intval($obj->max);
  127. } else {
  128. $max = 0;
  129. }
  130. } else {
  131. dol_syslog("mod_commande_marbre::getNextValue", LOG_DEBUG);
  132. return -1;
  133. }
  134. //$date=time();
  135. $date = $object->date;
  136. $yymm = strftime("%y%m", $date);
  137. if ($max >= (pow(10, 4) - 1)) {
  138. $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  139. } else {
  140. $num = sprintf("%04s", $max + 1);
  141. }
  142. dol_syslog("mod_commande_marbre::getNextValue return ".$this->prefix.$yymm."-".$num);
  143. return $this->prefix.$yymm."-".$num;
  144. }
  145. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  146. /**
  147. * Return next free value
  148. *
  149. * @param Societe $objsoc Object third party
  150. * @param string $objforref Object for number to search
  151. * @return string Next free value
  152. */
  153. public function commande_get_num($objsoc, $objforref)
  154. {
  155. // phpcs:enable
  156. return $this->getNextValue($objsoc, $objforref);
  157. }
  158. }