mod_facture_mars.php 7.2 KB

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