mod_facture_terre.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /* Copyright (C) 2005-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2015 Regis Houssin <regis.houssin@inodbox.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/modules/facture/mod_facture_terre.php
  21. * \ingroup facture
  22. * \brief File containing class for numbering module Terre
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
  25. /**
  26. * \class mod_facture_terre
  27. * \brief Class of numbering module Terre for invoices
  28. */
  29. class mod_facture_terre extends ModeleNumRefFactures
  30. {
  31. /**
  32. * Dolibarr version of the loaded document 'development', 'experimental', 'dolibarr'
  33. * @var string
  34. */
  35. public $version = 'dolibarr';
  36. /**
  37. * Prefix for invoices
  38. * @var string
  39. */
  40. public $prefixinvoice = 'FA';
  41. /**
  42. * Prefix for replacement invoices
  43. * @var string
  44. */
  45. public $prefixreplacement = 'FA';
  46. /**
  47. * Prefix for credit note
  48. * @var string
  49. */
  50. public $prefixcreditnote = 'AV';
  51. /**
  52. * Prefix for deposit
  53. * @var string
  54. */
  55. public $prefixdeposit = 'AC';
  56. /**
  57. * @var string Error code (or message)
  58. */
  59. public $error = '';
  60. /**
  61. * Constructor
  62. */
  63. public function __construct()
  64. {
  65. global $conf, $mysoc;
  66. if ((float) $conf->global->MAIN_VERSION_LAST_INSTALL >= 16.0 && $mysoc->country_code != 'FR') {
  67. $this->prefixinvoice = 'IN'; // We use correct standard code "IN = Invoice"
  68. $this->prefixreplacement = 'IR';
  69. $this->prefixdeposit = 'ID';
  70. $this->prefixcreditnote = 'IC';
  71. }
  72. if (!empty($conf->global->INVOICE_NUMBERING_TERRE_FORCE_PREFIX)) {
  73. $this->prefixinvoice = $conf->global->INVOICE_NUMBERING_TERRE_FORCE_PREFIX;
  74. }
  75. }
  76. /**
  77. * Returns the description of the numbering model
  78. *
  79. * @param Translate $langs Lang object to use for output
  80. * @return string Descriptive text
  81. */
  82. public function info($langs)
  83. {
  84. global $langs;
  85. $langs->load("bills");
  86. return $langs->trans('TerreNumRefModelDesc1', $this->prefixinvoice, $this->prefixcreditnote, $this->prefixdeposit);
  87. }
  88. /**
  89. * Return an example of numbering
  90. *
  91. * @return string Example
  92. */
  93. public function getExample()
  94. {
  95. return $this->prefixinvoice."0501-0001";
  96. }
  97. /**
  98. * Checks if the numbers already in the database do not
  99. * cause conflicts that would prevent this numbering working.
  100. *
  101. * @param Object $object Object we need next value for
  102. * @return boolean false if conflict, true if ok
  103. */
  104. public function canBeActivated($object)
  105. {
  106. global $langs, $conf, $db;
  107. $langs->load("bills");
  108. // Check invoice num
  109. $fayymm = '';
  110. $max = '';
  111. $posindice = strlen($this->prefixinvoice) + 6;
  112. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
  113. $sql .= " FROM ".MAIN_DB_PREFIX."facture";
  114. $sql .= " WHERE ref LIKE '".$db->escape($this->prefixinvoice)."____-%'";
  115. $sql .= " AND entity = ".$conf->entity;
  116. $resql = $db->query($sql);
  117. if ($resql) {
  118. $row = $db->fetch_row($resql);
  119. if ($row) {
  120. $fayymm = substr($row[0], 0, 6);
  121. $max = $row[0];
  122. }
  123. }
  124. if ($fayymm && !preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
  125. $langs->load("errors");
  126. $this->error = $langs->trans('ErrorNumRefModel', $max);
  127. return false;
  128. }
  129. // Check credit note num
  130. $fayymm = '';
  131. $posindice = strlen($this->prefixcreditnote) + 6;
  132. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
  133. $sql .= " FROM ".MAIN_DB_PREFIX."facture";
  134. $sql .= " WHERE ref LIKE '".$db->escape($this->prefixcreditnote)."____-%'";
  135. $sql .= " AND entity = ".$conf->entity;
  136. $resql = $db->query($sql);
  137. if ($resql) {
  138. $row = $db->fetch_row($resql);
  139. if ($row) {
  140. $fayymm = substr($row[0], 0, 6);
  141. $max = $row[0];
  142. }
  143. }
  144. if ($fayymm && !preg_match('/'.$this->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
  145. $this->error = $langs->trans('ErrorNumRefModel', $max);
  146. return false;
  147. }
  148. // Check deposit num
  149. $fayymm = '';
  150. $posindice = strlen($this->prefixdeposit) + 6;
  151. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
  152. $sql .= " FROM ".MAIN_DB_PREFIX."facture";
  153. $sql .= " WHERE ref LIKE '".$db->escape($this->prefixdeposit)."____-%'";
  154. $sql .= " AND entity = ".$conf->entity;
  155. $resql = $db->query($sql);
  156. if ($resql) {
  157. $row = $db->fetch_row($resql);
  158. if ($row) {
  159. $fayymm = substr($row[0], 0, 6);
  160. $max = $row[0];
  161. }
  162. }
  163. if ($fayymm && !preg_match('/'.$this->prefixdeposit.'[0-9][0-9][0-9][0-9]/i', $fayymm)) {
  164. $this->error = $langs->trans('ErrorNumRefModel', $max);
  165. return false;
  166. }
  167. return true;
  168. }
  169. /**
  170. * Return next value not used or last value used.
  171. * Note to increase perf of this numbering engine, you can create a calculated column and modify request to use this field instead for select:
  172. * ALTER TABLE llx_facture ADD COLUMN calculated_numrefonly INTEGER AS (CASE SUBSTRING(ref FROM 1 FOR 2) WHEN 'FA' THEN CAST(SUBSTRING(ref FROM 10) AS SIGNED) ELSE 0 END) PERSISTENT;
  173. * ALTER TABLE llx_facture ADD INDEX calculated_numrefonly_idx (calculated_numrefonly);
  174. *
  175. * @param Societe $objsoc Object third party
  176. * @param Facture $invoice Object invoice
  177. * @param string $mode 'next' for next value or 'last' for last value
  178. * @return string Next ref value or last ref if $mode is 'last', <= 0 if KO
  179. */
  180. public function getNextValue($objsoc, $invoice, $mode = 'next')
  181. {
  182. global $db;
  183. dol_syslog(get_class($this)."::getNextValue mode=".$mode, LOG_DEBUG);
  184. $prefix = $this->prefixinvoice;
  185. if ($invoice->type == 2) {
  186. $prefix = $this->prefixcreditnote;
  187. } elseif ($invoice->type == 3) {
  188. $prefix = $this->prefixdeposit;
  189. }
  190. // First we get the max value
  191. $posindice = strlen($prefix) + 6;
  192. $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
  193. $sql .= " FROM ".MAIN_DB_PREFIX."facture";
  194. $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-%'";
  195. $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
  196. $resql = $db->query($sql);
  197. if ($resql) {
  198. $obj = $db->fetch_object($resql);
  199. if ($obj) {
  200. $max = intval($obj->max);
  201. } else {
  202. $max = 0;
  203. }
  204. } else {
  205. return -1;
  206. }
  207. if ($mode == 'last') {
  208. if ($max >= (pow(10, 4) - 1)) {
  209. $num = $max; // If counter > 9999, we do not format on 4 chars, we take number as it is
  210. } else {
  211. $num = sprintf("%04s", $max);
  212. }
  213. $ref = '';
  214. $sql = "SELECT ref as ref";
  215. $sql .= " FROM ".MAIN_DB_PREFIX."facture";
  216. $sql .= " WHERE ref LIKE '".$db->escape($prefix)."____-".$num."'";
  217. $sql .= " AND entity IN (".getEntity('invoicenumber', 1, $invoice).")";
  218. $sql .= " ORDER BY ref DESC";
  219. $resql = $db->query($sql);
  220. if ($resql) {
  221. $obj = $db->fetch_object($resql);
  222. if ($obj) {
  223. $ref = $obj->ref;
  224. }
  225. } else {
  226. dol_print_error($db);
  227. }
  228. return $ref;
  229. } elseif ($mode == 'next') {
  230. $date = $invoice->date; // This is invoice date (not creation date)
  231. $yymm = strftime("%y%m", $date);
  232. if ($max >= (pow(10, 4) - 1)) {
  233. $num = $max + 1; // If counter > 9999, we do not format on 4 chars, we take number as it is
  234. } else {
  235. $num = sprintf("%04s", $max + 1);
  236. }
  237. dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
  238. return $prefix.$yymm."-".$num;
  239. } else {
  240. dol_print_error('', 'Bad parameter for getNextValue');
  241. }
  242. return 0;
  243. }
  244. /**
  245. * Return next free value
  246. *
  247. * @param Societe $objsoc Object third party
  248. * @param string $objforref Object for number to search
  249. * @param string $mode 'next' for next value or 'last' for last value
  250. * @return string Next free value
  251. */
  252. public function getNumRef($objsoc, $objforref, $mode = 'next')
  253. {
  254. return $this->getNextValue($objsoc, $objforref, $mode);
  255. }
  256. }