mod_chequereceipt_mint.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /* Copyright (C) 2015 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 <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/modules/cheque/mod_chequereceipt_mint.php
  20. * \ingroup cheque
  21. * \brief File containing class for numbering module Mint
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/core/modules/cheque/modules_chequereceipts.php';
  24. /**
  25. * Class to manage cheque receipts numbering rules Mint
  26. */
  27. class mod_chequereceipt_mint extends ModeleNumRefChequeReceipts
  28. {
  29. /**
  30. * Dolibarr version of the loaded document
  31. * @var string
  32. */
  33. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  34. public $prefix = 'CHK';
  35. /**
  36. * @var string Error code (or message)
  37. */
  38. public $error = '';
  39. public $name = 'Mint';
  40. /**
  41. * Return description of numbering module
  42. *
  43. * @return string Text with description
  44. */
  45. public function info()
  46. {
  47. global $langs;
  48. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  49. }
  50. /**
  51. * Return an example of numbering
  52. *
  53. * @return string Example
  54. */
  55. public function getExample()
  56. {
  57. return $this->prefix."0501-0001";
  58. }
  59. /**
  60. * Checks if the numbers already in the database do not
  61. * cause conflicts that would prevent this numbering working.
  62. *
  63. * @return boolean false if conflict, true if ok
  64. */
  65. public function canBeActivated()
  66. {
  67. global $conf, $langs, $db;
  68. $payyymm = '';
  69. $max = '';
  70. $posindice = strlen($this->prefix) + 6;
  71. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  72. $sql .= " FROM ".MAIN_DB_PREFIX."bordereau_cheque";
  73. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  74. $sql .= " AND entity = ".$conf->entity;
  75. $resql = $db->query($sql);
  76. if ($resql) {
  77. $row = $db->fetch_row($resql);
  78. if ($row) {
  79. $payyymm = substr($row[0], 0, 6);
  80. $max = $row[0];
  81. }
  82. }
  83. if ($payyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $payyymm)) {
  84. $langs->load("errors");
  85. $this->error = $langs->trans('ErrorNumRefModel', $max);
  86. return false;
  87. }
  88. return true;
  89. }
  90. /**
  91. * Return next free value
  92. *
  93. * @param Societe $objsoc Object thirdparty
  94. * @param Object $object Object we need next value for
  95. * @return string Value if KO, <0 if KO
  96. */
  97. public function getNextValue($objsoc, $object)
  98. {
  99. global $db, $conf;
  100. // First, we get the max value
  101. $posindice = strlen($this->prefix) + 6;
  102. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  103. $sql .= " FROM ".MAIN_DB_PREFIX."bordereau_cheque";
  104. $sql .= " WHERE ref like '".$db->escape($this->prefix)."____-%'";
  105. $sql .= " AND entity = ".$conf->entity;
  106. $resql = $db->query($sql);
  107. if ($resql) {
  108. $obj = $db->fetch_object($resql);
  109. if ($obj) {
  110. $max = intval($obj->max);
  111. } else {
  112. $max = 0;
  113. }
  114. } else {
  115. dol_syslog(__METHOD__, LOG_DEBUG);
  116. return -1;
  117. }
  118. //$date=time();
  119. $date = $object->date_bordereau;
  120. $yymm = strftime("%y%m", $date);
  121. if ($max >= (pow(10, 4) - 1)) {
  122. $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  123. } else {
  124. $num = sprintf("%04s", $max + 1);
  125. }
  126. dol_syslog(__METHOD__." return ".$this->prefix.$yymm."-".$num);
  127. return $this->prefix.$yymm."-".$num;
  128. }
  129. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  130. /**
  131. * Return next free value
  132. *
  133. * @param Societe $objsoc Object third party
  134. * @param string $objforref Object for number to search
  135. * @return string Next free value
  136. */
  137. public function chequereceipt_get_num($objsoc, $objforref)
  138. {
  139. // phpcs:enable
  140. return $this->getNextValue($objsoc, $objforref);
  141. }
  142. }