mod_facture_mars.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /* Copyright (C) 2005-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2018 Regis Houssin <regis.houssin@inodbox.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 <https://www.gnu.org/licenses/>.
  18. * or see https://www.gnu.org/
  19. */
  20. /**
  21. * \file htdocs/core/modules/facture/mod_facture_mars.php
  22. * \ingroup facture
  23. * \brief File containing class for numbering module Mars
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
  26. /**
  27. * Class to manage invoice numbering rules Mars
  28. */
  29. class mod_facture_mars extends ModeleNumRefFactures
  30. {
  31. /**
  32. * Dolibarr version of the loaded document
  33. * @var string
  34. */
  35. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  36. public $prefixinvoice = 'FA';
  37. public $prefixreplacement = 'FR';
  38. public $prefixdeposit = 'AC';
  39. public $prefixcreditnote = 'AV';
  40. /**
  41. * @var string Error code (or message)
  42. */
  43. public $error = '';
  44. /**
  45. * Constructor
  46. */
  47. public function __construct()
  48. {
  49. if (!empty($conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX))
  50. {
  51. $this->prefixinvoice = $conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX;
  52. }
  53. }
  54. /**
  55. * Returns the description of the numbering model
  56. *
  57. * @return string Texte descripif
  58. */
  59. public function info()
  60. {
  61. global $langs;
  62. $langs->load("bills");
  63. return $langs->trans('MarsNumRefModelDesc1', $this->prefixinvoice, $this->prefixreplacement, $this->prefixdeposit, $this->prefixcreditnote);
  64. }
  65. /**
  66. * Return an example of numbering
  67. *
  68. * @return string Example
  69. */
  70. public function getExample()
  71. {
  72. return $this->prefixinvoice."0501-0001";
  73. }
  74. /**
  75. * Checks if the numbers already in the database do not
  76. * cause conflicts that would prevent this numbering working.
  77. *
  78. * @return boolean false if conflict, true if ok
  79. */
  80. public function canBeActivated()
  81. {
  82. global $langs, $conf, $db;
  83. $langs->load("bills");
  84. // Check invoice num
  85. $fayymm = ''; $max = '';
  86. $posindice = strlen($this->prefixinvoice) + 6;
  87. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED) as max"; // This is standard SQL
  88. $sql .= " FROM ".MAIN_DB_PREFIX."facture";
  89. $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
  90. $sql .= " AND entity = ".$conf->entity;
  91. $resql = $db->query($sql);
  92. if ($resql)
  93. {
  94. $row = $db->fetch_row($resql);
  95. if ($row) { $fayymm = substr($row[0], 0, 6); $max = $row[0]; }
  96. }
  97. if ($fayymm && !preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $fayymm))
  98. {
  99. $langs->load("errors");
  100. $this->error = $langs->trans('ErrorNumRefModel', $max);
  101. return false;
  102. }
  103. // Check credit note num
  104. $fayymm = '';
  105. $posindice = strlen($this->prefixcreditnote) + 6;
  106. $sql = "SELECT MAX(SUBSTRING(ref FROM ".$posindice.")) as max"; // This is standard SQL
  107. $sql .= " FROM ".MAIN_DB_PREFIX."facture";
  108. $sql .= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
  109. $sql .= " AND entity = ".$conf->entity;
  110. $resql = $db->query($sql);
  111. if ($resql)
  112. {
  113. $row = $db->fetch_row($resql);
  114. if ($row) { $fayymm = substr($row[0], 0, 6); $max = $row[0]; }
  115. }
  116. if ($fayymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $fayymm))
  117. {
  118. $this->error = $langs->trans('ErrorNumRefModel', $max);
  119. return false;
  120. }
  121. return true;
  122. }
  123. /**
  124. * Return next value not used or last value used
  125. *
  126. * @param Societe $objsoc Object third party
  127. * @param Facture $invoice Object invoice
  128. * @param string $mode 'next' for next value or 'last' for last value
  129. * @return string Value
  130. */
  131. public function getNextValue($objsoc, $invoice, $mode = 'next')
  132. {
  133. global $db;
  134. $prefix = $this->prefixinvoice;
  135. if ($invoice->type == 1) $prefix = $this->prefixreplacement;
  136. elseif ($invoice->type == 2) $prefix = $this->prefixcreditnote;
  137. elseif ($invoice->type == 3) $prefix = $this->prefixdeposit;
  138. // First we get the max value
  139. $posindice = strlen($prefix) + 6;
  140. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
  141. $sql .= " FROM ".MAIN_DB_PREFIX."facture";
  142. $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
  143. $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
  144. $resql = $db->query($sql);
  145. dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
  146. if ($resql)
  147. {
  148. $obj = $db->fetch_object($resql);
  149. if ($obj) $max = intval($obj->max);
  150. else $max = 0;
  151. } else {
  152. return -1;
  153. }
  154. if ($mode == 'last')
  155. {
  156. if ($max >= (pow(10, 4) - 1)) $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
  157. else $num = sprintf("%04s", $max);
  158. $ref = '';
  159. $sql = "SELECT ref as ref";
  160. $sql .= " FROM ".MAIN_DB_PREFIX."facture";
  161. $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
  162. $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
  163. $sql .= " ORDER BY ref DESC";
  164. dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
  165. $resql = $db->query($sql);
  166. if ($resql)
  167. {
  168. $obj = $db->fetch_object($resql);
  169. if ($obj) $ref = $obj->ref;
  170. } else dol_print_error($db);
  171. return $ref;
  172. } elseif ($mode == 'next')
  173. {
  174. $date = $invoice->date; // This is invoice date (not creation date)
  175. $yymm = strftime("%y%m", $date);
  176. 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
  177. else $num = sprintf("%04s", $max + 1);
  178. dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
  179. return $prefix.$yymm."-".$num;
  180. } else dol_print_error('', 'Bad parameter for getNextValue');
  181. }
  182. /**
  183. * Return next free value
  184. *
  185. * @param Societe $objsoc Object third party
  186. * @param string $objforref Object for number to search
  187. * @param string $mode 'next' for next value or 'last' for last value
  188. * @return string Next free value
  189. */
  190. public function getNumRef($objsoc, $objforref, $mode = 'next')
  191. {
  192. return $this->getNextValue($objsoc, $objforref, $mode);
  193. }
  194. }