mod_facture_mars.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. global $conf, $mysoc;
  50. if ((float) $conf->global->MAIN_VERSION_LAST_INSTALL >= 16.0 && $mysoc->country_code != 'FR') {
  51. $this->prefixinvoice = 'IN'; // We use correct standard code "IN = Invoice"
  52. $this->prefixreplacement = 'IR';
  53. $this->prefixdeposit = 'ID';
  54. $this->prefixcreditnote = 'IC';
  55. }
  56. if (!empty($conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX)) {
  57. $this->prefixinvoice = $conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX;
  58. }
  59. }
  60. /**
  61. * Returns the description of the numbering model
  62. *
  63. * @return string Texte descripif
  64. */
  65. public function info()
  66. {
  67. global $langs;
  68. $langs->load("bills");
  69. return $langs->trans('MarsNumRefModelDesc1', $this->prefixinvoice, $this->prefixreplacement, $this->prefixdeposit, $this->prefixcreditnote);
  70. }
  71. /**
  72. * Return an example of numbering
  73. *
  74. * @return string Example
  75. */
  76. public function getExample()
  77. {
  78. return $this->prefixinvoice."0501-0001";
  79. }
  80. /**
  81. * Checks if the numbers already in the database do not
  82. * cause conflicts that would prevent this numbering working.
  83. *
  84. * @return boolean false if conflict, true if ok
  85. */
  86. public function canBeActivated()
  87. {
  88. global $langs, $conf, $db;
  89. $langs->load("bills");
  90. // Check invoice num
  91. $fayymm = '';
  92. $max = '';
  93. $posindice = strlen($this->prefixinvoice) + 6;
  94. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED) as max"; // This is standard SQL
  95. $sql .= " FROM ".MAIN_DB_PREFIX."facture";
  96. $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
  97. $sql .= " AND entity = ".$conf->entity;
  98. $resql = $db->query($sql);
  99. if ($resql) {
  100. $row = $db->fetch_row($resql);
  101. if ($row) {
  102. $fayymm = substr($row[0], 0, 6);
  103. $max = $row[0];
  104. }
  105. }
  106. if ($fayymm && !preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
  107. $langs->load("errors");
  108. $this->error = $langs->trans('ErrorNumRefModel', $max);
  109. return false;
  110. }
  111. // Check credit note num
  112. $fayymm = '';
  113. $posindice = strlen($this->prefixcreditnote) + 6;
  114. $sql = "SELECT MAX(SUBSTRING(ref FROM ".$posindice.")) as max"; // This is standard SQL
  115. $sql .= " FROM ".MAIN_DB_PREFIX."facture";
  116. $sql .= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
  117. $sql .= " AND entity = ".$conf->entity;
  118. $resql = $db->query($sql);
  119. if ($resql) {
  120. $row = $db->fetch_row($resql);
  121. if ($row) {
  122. $fayymm = substr($row[0], 0, 6);
  123. $max = $row[0];
  124. }
  125. }
  126. if ($fayymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
  127. $this->error = $langs->trans('ErrorNumRefModel', $max);
  128. return false;
  129. }
  130. return true;
  131. }
  132. /**
  133. * Return next value not used or last value used
  134. *
  135. * @param Societe $objsoc Object third party
  136. * @param Facture $invoice Object invoice
  137. * @param string $mode 'next' for next value or 'last' for last value
  138. * @return string Value if OK, 0 if KO
  139. */
  140. public function getNextValue($objsoc, $invoice, $mode = 'next')
  141. {
  142. global $db;
  143. $prefix = $this->prefixinvoice;
  144. if ($invoice->type == 1) {
  145. $prefix = $this->prefixreplacement;
  146. } elseif ($invoice->type == 2) {
  147. $prefix = $this->prefixcreditnote;
  148. } elseif ($invoice->type == 3) {
  149. $prefix = $this->prefixdeposit;
  150. }
  151. // First we get the max value
  152. $posindice = strlen($prefix) + 6;
  153. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
  154. $sql .= " FROM ".MAIN_DB_PREFIX."facture";
  155. $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
  156. $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
  157. $resql = $db->query($sql);
  158. dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
  159. if ($resql) {
  160. $obj = $db->fetch_object($resql);
  161. if ($obj) {
  162. $max = intval($obj->max);
  163. } else {
  164. $max = 0;
  165. }
  166. } else {
  167. return -1;
  168. }
  169. if ($mode == 'last') {
  170. if ($max >= (pow(10, 4) - 1)) {
  171. $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
  172. } else {
  173. $num = sprintf("%04s", $max);
  174. }
  175. $ref = '';
  176. $sql = "SELECT ref as ref";
  177. $sql .= " FROM ".MAIN_DB_PREFIX."facture";
  178. $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
  179. $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
  180. $sql .= " ORDER BY ref DESC";
  181. dol_syslog(get_class($this)."::getNextValue", LOG_DEBUG);
  182. $resql = $db->query($sql);
  183. if ($resql) {
  184. $obj = $db->fetch_object($resql);
  185. if ($obj) {
  186. $ref = $obj->ref;
  187. }
  188. } else {
  189. dol_print_error($db);
  190. }
  191. return $ref;
  192. } elseif ($mode == 'next') {
  193. $date = $invoice->date; // This is invoice date (not creation date)
  194. $yymm = strftime("%y%m", $date);
  195. if ($max >= (pow(10, 4) - 1)) {
  196. $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  197. } else {
  198. $num = sprintf("%04s", $max + 1);
  199. }
  200. dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
  201. return $prefix.$yymm."-".$num;
  202. } else {
  203. dol_print_error('', 'Bad parameter for getNextValue');
  204. }
  205. }
  206. /**
  207. * Return next free value
  208. *
  209. * @param Societe $objsoc Object third party
  210. * @param string $objforref Object for number to search
  211. * @param string $mode 'next' for next value or 'last' for last value
  212. * @return string Next free value
  213. */
  214. public function getNumRef($objsoc, $objforref, $mode = 'next')
  215. {
  216. return $this->getNextValue($objsoc, $objforref, $mode);
  217. }
  218. }