mod_contract_serpis.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. * or see http://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/modules/contract/mod_contract_serpis.php
  20. * \ingroup contract
  21. * \brief File of class to manage contract numbering rules Serpis
  22. */
  23. require_once DOL_DOCUMENT_ROOT .'/core/modules/contract/modules_contract.php';
  24. /**
  25. * Class to manage contract numbering rules Serpis
  26. */
  27. class mod_contract_serpis extends ModelNumRefContracts
  28. {
  29. var $version='dolibarr';
  30. var $prefix='CT';
  31. var $error='';
  32. var $nom='Serpis';
  33. /**
  34. * Return default description of numbering model
  35. *
  36. * @return string text description
  37. */
  38. function info()
  39. {
  40. global $langs;
  41. return $langs->trans("SimpleNumRefModelDesc",$this->prefix);
  42. }
  43. /**
  44. * Return numbering example
  45. *
  46. * @return string Example
  47. */
  48. function getExample()
  49. {
  50. return $this->prefix."0501-0001";
  51. }
  52. /**
  53. * Test if existing numbers make problems with numbering
  54. *
  55. * @return boolean false if conflit, true if ok
  56. */
  57. function canBeActivated()
  58. {
  59. global $conf,$langs,$db;
  60. $coyymm=''; $max='';
  61. $posindice=8;
  62. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  63. $sql.= " FROM ".MAIN_DB_PREFIX."contrat";
  64. $sql.= " WHERE ref LIKE '".$this->prefix."____-%'";
  65. $sql.= " AND entity = ".$conf->entity;
  66. $resql=$db->query($sql);
  67. if ($resql)
  68. {
  69. $row = $db->fetch_row($resql);
  70. if ($row) { $coyymm = substr($row[0],0,6); $max=$row[0]; }
  71. }
  72. if ($coyymm && ! preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i',$coyymm))
  73. {
  74. $langs->load("errors");
  75. $this->error=$langs->trans('ErrorNumRefModel', $max);
  76. return false;
  77. }
  78. return true;
  79. }
  80. /**
  81. * Return next value
  82. *
  83. * @param Societe $objsoc third party object
  84. * @param Object $contract contract object
  85. * @return string Value if OK, 0 if KO
  86. */
  87. function getNextValue($objsoc,$contract)
  88. {
  89. global $db,$conf;
  90. $posindice=8;
  91. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  92. $sql.= " FROM ".MAIN_DB_PREFIX."contrat";
  93. $sql.= " WHERE ref like '".$this->prefix."____-%'";
  94. $sql.= " AND entity = ".$conf->entity;
  95. $resql=$db->query($sql);
  96. if ($resql)
  97. {
  98. $obj = $db->fetch_object($resql);
  99. if ($obj) $max = intval($obj->max);
  100. else $max=0;
  101. }
  102. else
  103. {
  104. dol_syslog("mod_contract_serpis::getNextValue sql=".$sql);
  105. return -1;
  106. }
  107. $date=$contract->date_contrat;
  108. $yymm = strftime("%y%m",$date);
  109. 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
  110. else $num = sprintf("%04s",$max+1);
  111. dol_syslog("mod_contract_serpis::getNextValue return ".$this->prefix.$yymm."-".$num);
  112. return $this->prefix.$yymm."-".$num;
  113. }
  114. /**
  115. * Return next value
  116. *
  117. * @param Societe $objsoc third party object
  118. * @param Object $objforref contract object
  119. * @return string Value if OK, 0 if KO
  120. */
  121. function contract_get_num($objsoc,$objforref)
  122. {
  123. return $this->getNextValue($objsoc,$objforref);
  124. }
  125. }