mod_pacific.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /* Copyright (C) 2005-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
  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/fichinter/mod_pacific.php
  22. * \ingroup fiche intervention
  23. * \brief File with Pacific numbering module for interventions
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/core/modules/fichinter/modules_fichinter.php';
  26. /**
  27. * Class to manage numbering of intervention cards with rule Pacific.
  28. */
  29. class mod_pacific extends ModeleNumRefFicheinter
  30. {
  31. /**
  32. * Dolibarr version of the loaded document
  33. * @var string
  34. */
  35. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  36. public $prefix = 'FI';
  37. /**
  38. * @var string Error code (or message)
  39. */
  40. public $error = '';
  41. /**
  42. * @var string Nom du modele
  43. * @deprecated
  44. * @see $name
  45. */
  46. public $nom = 'pacific';
  47. /**
  48. * @var string model name
  49. */
  50. public $name = 'pacific';
  51. /**
  52. * Return description of numbering module
  53. *
  54. * @param Translate $langs Lang object to use for output
  55. * @return string Descriptive text
  56. */
  57. public function info($langs)
  58. {
  59. global $langs;
  60. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  61. }
  62. /**
  63. * Return an example of numbering
  64. *
  65. * @return string Example
  66. */
  67. public function getExample()
  68. {
  69. return $this->prefix."0501-0001";
  70. }
  71. /**
  72. * Checks if the numbers already in the database do not
  73. * cause conflicts that would prevent this numbering working.
  74. *
  75. * @param Object $object Object we need next value for
  76. * @return boolean false if conflict, true if ok
  77. */
  78. public function canBeActivated($object)
  79. {
  80. global $langs, $conf, $db;
  81. $langs->load("bills");
  82. $fayymm = '';
  83. $max = '';
  84. $posindice = strlen($this->prefix) + 6;
  85. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  86. $sql .= " FROM ".MAIN_DB_PREFIX."fichinter";
  87. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  88. $sql .= " WHERE entity = ".$conf->entity;
  89. $resql = $db->query($sql);
  90. if ($resql) {
  91. $row = $db->fetch_row($resql);
  92. if ($row) {
  93. $fayymm = substr($row[0], 0, 6);
  94. $max = $row[0];
  95. }
  96. }
  97. if (!$fayymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
  98. return true;
  99. } else {
  100. $langs->load("errors");
  101. $this->error = $langs->trans('ErrorNumRefModel', $max);
  102. return false;
  103. }
  104. }
  105. /**
  106. * Return next free value
  107. *
  108. * @param Societe $objsoc Object thirdparty
  109. * @param Object $object Object we need next value for
  110. * @return string Value if KO, <0 if KO
  111. */
  112. public function getNextValue($objsoc = 0, $object = '')
  113. {
  114. global $db, $conf;
  115. // First, we get the max value
  116. $posindice = strlen($this->prefix) + 6;
  117. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  118. $sql .= " FROM ".MAIN_DB_PREFIX."fichinter";
  119. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  120. $sql .= " AND entity = ".$conf->entity;
  121. $resql = $db->query($sql);
  122. if ($resql) {
  123. $obj = $db->fetch_object($resql);
  124. if ($obj) {
  125. $max = intval($obj->max);
  126. } else {
  127. $max = 0;
  128. }
  129. }
  130. //$date=time();
  131. $date = $object->datec;
  132. $yymm = strftime("%y%m", $date);
  133. if ($max >= (pow(10, 4) - 1)) {
  134. $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  135. } else {
  136. $num = sprintf("%04s", $max + 1);
  137. }
  138. return $this->prefix.$yymm."-".$num;
  139. }
  140. /**
  141. * Return next free value
  142. *
  143. * @param Societe $objsoc Object third party
  144. * @param Object $objforref Object for number to search
  145. * @return string Next free value
  146. */
  147. public function getNumRef($objsoc, $objforref)
  148. {
  149. return $this->getNextValue($objsoc, $objforref);
  150. }
  151. }