mod_delivery_jade.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2010 Regis Houssin <regis.houssin@inodbox.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. * or see https://www.gnu.org/
  19. */
  20. /**
  21. * \file htdocs/core/modules/delivery/mod_delivery_jade.php
  22. * \ingroup delivery
  23. * \brief Fichier contenant la classe du modele de numerotation de reference de bon de livraison Jade
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/core/modules/delivery/modules_delivery.php';
  26. /**
  27. * \class mod_delivery_jade
  28. * \brief Classe du modele de numerotation de reference de bon de livraison Jade
  29. */
  30. class mod_delivery_jade extends ModeleNumRefDeliveryOrder
  31. {
  32. /**
  33. * Dolibarr version of the loaded document
  34. * @var string
  35. */
  36. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  37. /**
  38. * @var string Error message
  39. */
  40. public $error = '';
  41. /**
  42. * @var string Nom du modele
  43. * @deprecated
  44. * @see $name
  45. */
  46. public $nom = 'Jade';
  47. /**
  48. * @var string model name
  49. */
  50. public $name = 'Jade';
  51. public $prefix = 'BL';
  52. /**
  53. * Returns the description of the numbering model
  54. *
  55. * @param Translate $langs Lang object to use for output
  56. * @return string Descriptive text
  57. */
  58. public function info($langs)
  59. {
  60. global $langs;
  61. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  62. }
  63. /**
  64. * Return an example of numbering
  65. *
  66. * @return string Example
  67. */
  68. public function getExample()
  69. {
  70. return $this->prefix."0501-0001";
  71. }
  72. /**
  73. * Checks if the numbers already in the database do not
  74. * cause conflicts that would prevent this numbering working.
  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 $langs, $conf, $db;
  82. $langs->load("bills");
  83. // Check invoice num
  84. $fayymm = '';
  85. $max = '';
  86. $posindice = strlen($this->prefix) + 6;
  87. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
  88. $sql .= " FROM ".MAIN_DB_PREFIX."delivery";
  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. $fayymm = substr($row[0], 0, 6);
  96. $max = $row[0];
  97. }
  98. }
  99. if ($fayymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
  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"; // This is standard SQL
  119. $sql .= " FROM ".MAIN_DB_PREFIX."delivery";
  120. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  121. $sql .= " AND entity = ".$conf->entity;
  122. $resql = $db->query($sql);
  123. dol_syslog("mod_delivery_jade::getNextValue", LOG_DEBUG);
  124. if ($resql) {
  125. $obj = $db->fetch_object($resql);
  126. if ($obj) {
  127. $max = intval($obj->max);
  128. } else {
  129. $max = 0;
  130. }
  131. } else {
  132. return -1;
  133. }
  134. $date = $object->date_delivery;
  135. if (empty($date)) {
  136. $date = dol_now();
  137. }
  138. $yymm = strftime("%y%m", $date);
  139. if ($max >= (pow(10, 4) - 1)) {
  140. $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  141. } else {
  142. $num = sprintf("%04s", $max + 1);
  143. }
  144. dol_syslog("mod_delivery_jade::getNextValue return ".$this->prefix.$yymm."-".$num);
  145. return $this->prefix.$yymm."-".$num;
  146. }
  147. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  148. /**
  149. * Return next free ref
  150. *
  151. * @param Societe $objsoc Object thirdparty
  152. * @param Object $object Object livraison
  153. * @return string Descriptive text
  154. */
  155. public function delivery_get_num($objsoc = 0, $object = '')
  156. {
  157. // phpcs:enable
  158. return $this->getNextValue($objsoc, $object);
  159. }
  160. }