mod_propale_marbre.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /* Copyright (C) 2005-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2012 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/propale/mod_propale_marbre.php
  21. * \ingroup propale
  22. * \brief File of class to manage commercial proposal numbering rules Marbre
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/modules/propale/modules_propale.php';
  25. /**
  26. * Class to manage business proposition rules Marbre
  27. */
  28. class mod_propale_marbre extends ModeleNumRefPropales
  29. {
  30. /**
  31. * Dolibarr version of the loaded document
  32. * @var string
  33. */
  34. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  35. public $prefix = 'PR';
  36. /**
  37. * @var string Error code (or message)
  38. */
  39. public $error = '';
  40. /**
  41. * @var string Nom du modele
  42. * @deprecated
  43. * @see $name
  44. */
  45. public $nom = 'Marbre';
  46. /**
  47. * @var string model name
  48. */
  49. public $name = 'Marbre';
  50. /**
  51. * Return description of numbering module
  52. *
  53. * @return string Text with description
  54. */
  55. public function info()
  56. {
  57. global $langs;
  58. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  59. }
  60. /**
  61. * Return an example of numbering module values
  62. *
  63. * @return string Example
  64. */
  65. public function getExample()
  66. {
  67. return $this->prefix."0501-0001";
  68. }
  69. /**
  70. * Checks if the numbers already in the database do not
  71. * cause conflicts that would prevent this numbering working.
  72. *
  73. * @return boolean false if conflict, true if ok
  74. */
  75. public function canBeActivated()
  76. {
  77. global $conf, $langs, $db;
  78. $pryymm = '';
  79. $max = '';
  80. $posindice = strlen($this->prefix) + 6;
  81. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  82. $sql .= " FROM ".MAIN_DB_PREFIX."propal";
  83. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  84. $sql .= " AND entity = ".$conf->entity;
  85. $resql = $db->query($sql);
  86. if ($resql) {
  87. $row = $db->fetch_row($resql);
  88. if ($row) {
  89. $pryymm = substr($row[0], 0, 6);
  90. $max = $row[0];
  91. }
  92. }
  93. if (!$pryymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $pryymm)) {
  94. return true;
  95. } else {
  96. $langs->load("errors");
  97. $this->error = $langs->trans('ErrorNumRefModel', $max);
  98. return false;
  99. }
  100. }
  101. /**
  102. * Return next value
  103. *
  104. * @param Societe $objsoc Object third party
  105. * @param Propal $propal Object commercial proposal
  106. * @return string Next value
  107. */
  108. public function getNextValue($objsoc, $propal)
  109. {
  110. global $db, $conf;
  111. // First, we get the max value
  112. $posindice = strlen($this->prefix) + 6;
  113. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
  114. $sql .= " FROM ".MAIN_DB_PREFIX."propal";
  115. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  116. $sql .= " AND entity IN (".getEntity('proposalnumber', 1, $propal).")";
  117. $resql = $db->query($sql);
  118. if ($resql) {
  119. $obj = $db->fetch_object($resql);
  120. if ($obj) {
  121. $max = intval($obj->max);
  122. } else {
  123. $max = 0;
  124. }
  125. } else {
  126. dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
  127. return -1;
  128. }
  129. $date = time();
  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(get_class($this)."::getNextValue return ".$this->prefix.$yymm."-".$num);
  137. return $this->prefix.$yymm."-".$num;
  138. }
  139. /**
  140. * Return next free value
  141. *
  142. * @param Societe $objsoc Object third party
  143. * @param Object $objforref Object for number to search
  144. * @return string Next free value
  145. */
  146. public function getNumRef($objsoc, $objforref)
  147. {
  148. return $this->getNextValue($objsoc, $objforref);
  149. }
  150. }