mod_ticket_simple.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /* Copyright (C) 2010-2012 Regis Houssin <regis.houssin@inodbox.com>
  3. * Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  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/ticket/mod_ticket_simple.php
  21. * \ingroup ticket
  22. * \brief File with class to manage the numbering module Simple for ticket references
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/modules/ticket/modules_ticket.php';
  25. /**
  26. * Class to manage the numbering module Simple for ticket references
  27. */
  28. class mod_ticket_simple extends ModeleNumRefTicket
  29. {
  30. /**
  31. * Dolibarr version of the loaded document
  32. * @var string
  33. */
  34. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  35. public $prefix = 'TS';
  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 = 'Simple';
  46. /**
  47. * @var string model name
  48. */
  49. public $name = 'Simple';
  50. /**
  51. * Return description of numbering module
  52. *
  53. * @param Translate $langs Lang object to use for output
  54. * @return string Descriptive text
  55. */
  56. public function info($langs)
  57. {
  58. global $langs;
  59. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  60. }
  61. /**
  62. * Return an example of numbering module values
  63. *
  64. * @return string Example
  65. */
  66. public function getExample()
  67. {
  68. return $this->prefix."0501-0001";
  69. }
  70. /**
  71. * Checks if the numbers already in the database do not
  72. * cause conflicts that would prevent this numbering working.
  73. *
  74. * @param Object $object Object we need next value for
  75. * @return boolean false if conflict, true if ok
  76. */
  77. public function canBeActivated($object)
  78. {
  79. global $conf, $langs, $db;
  80. $coyymm = '';
  81. $max = '';
  82. $posindice = strlen($this->prefix) + 6;
  83. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  84. $sql .= " FROM ".MAIN_DB_PREFIX."ticket";
  85. $search = $this->prefix."____-%";
  86. $sql .= " WHERE ref LIKE '".$db->escape($search)."'";
  87. $sql .= " AND entity = ".$conf->entity;
  88. $resql = $db->query($sql);
  89. if ($resql) {
  90. $row = $db->fetch_row($resql);
  91. if ($row) {
  92. $coyymm = substr($row[0], 0, 6);
  93. $max = $row[0];
  94. }
  95. }
  96. if (!$coyymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $coyymm)) {
  97. return true;
  98. } else {
  99. $langs->load("errors");
  100. $this->error = $langs->trans('ErrorNumRefModel', $max);
  101. return false;
  102. }
  103. }
  104. /**
  105. * Return next value
  106. *
  107. * @param Societe $objsoc Object third party
  108. * @param Ticket $ticket Object ticket
  109. * @return string Value if OK, 0 if KO
  110. */
  111. public function getNextValue($objsoc, $ticket)
  112. {
  113. global $db;
  114. // First, we get the max value
  115. $posindice = strlen($this->prefix) + 6;
  116. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  117. $sql .= " FROM ".MAIN_DB_PREFIX."ticket";
  118. $search = $this->prefix."____-%";
  119. $sql .= " WHERE ref LIKE '".$db->escape($search)."'";
  120. $sql .= " AND entity IN (".getEntity('ticketnumber', 1, $ticket).")";
  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. } else {
  130. dol_syslog("mod_ticket_simple::getNextValue", LOG_DEBUG);
  131. return -1;
  132. }
  133. $date = empty($ticket->datec) ? dol_now() : $ticket->datec;
  134. //$yymm = strftime("%y%m",time());
  135. $yymm = dol_print_date($date, "%y%m");
  136. if ($max >= (pow(10, 4) - 1)) {
  137. $num = $max + 1;
  138. } else {
  139. // If counter > 9999, we do not format on 4 chars, we take number as it is
  140. $num = sprintf("%04s", $max + 1);
  141. }
  142. dol_syslog("mod_ticket_simple::getNextValue return ".$this->prefix.$yymm."-".$num);
  143. return $this->prefix.$yymm."-".$num;
  144. }
  145. }