mod_chequereceipt_mint.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. * @param Translate $langs Lang object to use for output
  44. * @return string Descriptive text
  45. */
  46. public function info($langs)
  47. {
  48. global $langs;
  49. return $langs->trans("SimpleNumRefModelDesc", $this->prefix);
  50. }
  51. /**
  52. * Return an example of numbering
  53. *
  54. * @return string Example
  55. */
  56. public function getExample()
  57. {
  58. return $this->prefix."0501-0001";
  59. }
  60. /**
  61. * Checks if the numbers already in the database do not
  62. * cause conflicts that would prevent this numbering working.
  63. *
  64. * @param Object $object Object we need next value for
  65. * @return boolean false if conflict, true if ok
  66. */
  67. public function canBeActivated($object)
  68. {
  69. global $conf, $langs, $db;
  70. $payyymm = '';
  71. $max = '';
  72. $posindice = strlen($this->prefix) + 6;
  73. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  74. $sql .= " FROM ".MAIN_DB_PREFIX."bordereau_cheque";
  75. $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
  76. $sql .= " AND entity = ".$conf->entity;
  77. $resql = $db->query($sql);
  78. if ($resql) {
  79. $row = $db->fetch_row($resql);
  80. if ($row) {
  81. $payyymm = substr($row[0], 0, 6);
  82. $max = $row[0];
  83. }
  84. }
  85. if ($payyymm && !preg_match('/'.$this->prefix.'[0-9][0-9][0-9][0-9]/i', $payyymm)) {
  86. $langs->load("errors");
  87. $this->error = $langs->trans('ErrorNumRefModel', $max);
  88. return false;
  89. }
  90. return true;
  91. }
  92. /**
  93. * Return next free value
  94. *
  95. * @param Societe $objsoc Object thirdparty
  96. * @param Object $object Object we need next value for
  97. * @return string Value if KO, <0 if KO
  98. */
  99. public function getNextValue($objsoc, $object)
  100. {
  101. global $db, $conf;
  102. // First, we get the max value
  103. $posindice = strlen($this->prefix) + 6;
  104. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
  105. $sql .= " FROM ".MAIN_DB_PREFIX."bordereau_cheque";
  106. $sql .= " WHERE ref like '".$db->escape($this->prefix)."____-%'";
  107. $sql .= " AND entity = ".$conf->entity;
  108. $resql = $db->query($sql);
  109. if ($resql) {
  110. $obj = $db->fetch_object($resql);
  111. if ($obj) {
  112. $max = intval($obj->max);
  113. } else {
  114. $max = 0;
  115. }
  116. } else {
  117. dol_syslog(__METHOD__, LOG_DEBUG);
  118. return -1;
  119. }
  120. //$date=time();
  121. $date = $object->date_bordereau;
  122. $yymm = strftime("%y%m", $date);
  123. if ($max >= (pow(10, 4) - 1)) {
  124. $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  125. } else {
  126. $num = sprintf("%04s", $max + 1);
  127. }
  128. dol_syslog(__METHOD__." return ".$this->prefix.$yymm."-".$num);
  129. return $this->prefix.$yymm."-".$num;
  130. }
  131. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  132. /**
  133. * Return next free value
  134. *
  135. * @param Societe $objsoc Object third party
  136. * @param string $objforref Object for number to search
  137. * @return string Next free value
  138. */
  139. public function chequereceipt_get_num($objsoc, $objforref)
  140. {
  141. // phpcs:enable
  142. return $this->getNextValue($objsoc, $objforref);
  143. }
  144. }