mod_pacific.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /* Copyright (C) 2005-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.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 <http://www.gnu.org/licenses/>.
  18. * or see http://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. var $version='dolibarr'; // 'development', 'experimental', 'dolibarr'
  32. var $prefix='FI';
  33. var $error='';
  34. var $nom = 'pacific';
  35. /**
  36. * Return description of numbering module
  37. *
  38. * @return string Text with description
  39. */
  40. function info()
  41. {
  42. global $langs;
  43. return $langs->trans("SimpleNumRefModelDesc",$this->prefix);
  44. }
  45. /**
  46. * Renvoi un exemple de numerotation
  47. *
  48. * @return string Example
  49. */
  50. function getExample()
  51. {
  52. return $this->prefix."0501-0001";
  53. }
  54. /**
  55. * Test si les numeros deja en vigueur dans la base ne provoquent pas de
  56. * de conflits qui empechera cette numerotation de fonctionner.
  57. *
  58. * @return boolean false si conflit, true si ok
  59. */
  60. function canBeActivated()
  61. {
  62. global $langs,$conf,$db;
  63. $langs->load("bills");
  64. $fayymm=''; $max='';
  65. $posindice=8;
  66. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  67. $sql.= " FROM ".MAIN_DB_PREFIX."fichinter";
  68. $sql.= " WHERE ref like '".$this->prefix."____-%'";
  69. $sql.= " WHERE entity = ".$conf->entity;
  70. $resql=$db->query($sql);
  71. if ($resql)
  72. {
  73. $row = $db->fetch_row($resql);
  74. if ($row) { $fayymm = substr($row[0],0,6); $max=$row[0]; }
  75. }
  76. if (! $fayymm || preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i',$fayymm))
  77. {
  78. return true;
  79. }
  80. else
  81. {
  82. $langs->load("errors");
  83. $this->error=$langs->trans('ErrorNumRefModel',$max);
  84. return false;
  85. }
  86. }
  87. /**
  88. * Return next free value
  89. *
  90. * @param Societe $objsoc Object thirdparty
  91. * @param Object $object Object we need next value for
  92. * @return string Value if KO, <0 if KO
  93. */
  94. function getNextValue($objsoc=0,$object='')
  95. {
  96. global $db,$conf;
  97. // D'abord on recupere la valeur max
  98. $posindice=8;
  99. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  100. $sql.= " FROM ".MAIN_DB_PREFIX."fichinter";
  101. $sql.= " WHERE ref LIKE '".$this->prefix."____-%'";
  102. $sql.= " AND entity = ".$conf->entity;
  103. $resql=$db->query($sql);
  104. if ($resql)
  105. {
  106. $obj = $db->fetch_object($resql);
  107. if ($obj) $max = intval($obj->max);
  108. else $max=0;
  109. }
  110. //$date=time();
  111. $date=$object->datec;
  112. $yymm = strftime("%y%m",$date);
  113. if ($max >= (pow(10, 4) - 1)) $num=$max+1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  114. else $num = sprintf("%04s",$max+1);
  115. return $this->prefix.$yymm."-".$num;
  116. }
  117. /**
  118. * Return next free value
  119. *
  120. * @param Societe $objsoc Object third party
  121. * @param Object $objforref Object for number to search
  122. * @return string Next free value
  123. */
  124. function getNumRef($objsoc,$objforref)
  125. {
  126. return $this->getNextValue($objsoc,$objforref);
  127. }
  128. }