mod_facture_terre.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /* Copyright (C) 2005-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.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 <http://www.gnu.org/licenses/>.
  17. * or see http://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. /** \class mod_facture_terre
  26. * \brief Classe du modele de numerotation de reference de facture Terre
  27. */
  28. class mod_facture_terre extends ModeleNumRefFactures
  29. {
  30. var $version='dolibarr'; // 'development', 'experimental', 'dolibarr'
  31. var $prefixinvoice='FA';
  32. var $prefixcreditnote='AV';
  33. var $prefixdeposit='AC';
  34. var $error='';
  35. /**
  36. * Renvoi la description du modele de numerotation
  37. *
  38. * @return string Texte descripif
  39. */
  40. function info()
  41. {
  42. global $langs;
  43. $langs->load("bills");
  44. return $langs->trans('TerreNumRefModelDesc1',$this->prefixinvoice,$this->prefixcreditnote,$this->prefixdeposit);
  45. }
  46. /**
  47. * Renvoi un exemple de numerotation
  48. *
  49. * @return string Example
  50. */
  51. function getExample()
  52. {
  53. return $this->prefixinvoice."0501-0001";
  54. }
  55. /**
  56. * Test si les numeros deja en vigueur dans la base ne provoquent pas de
  57. * de conflits qui empechera cette numerotation de fonctionner.
  58. *
  59. * @return boolean false si conflit, true si ok
  60. */
  61. function canBeActivated()
  62. {
  63. global $langs,$conf,$db;
  64. $langs->load("bills");
  65. // Check invoice num
  66. $fayymm=''; $max='';
  67. $posindice=8;
  68. $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
  69. $sql.= " FROM ".MAIN_DB_PREFIX."facture";
  70. $sql.= " WHERE facnumber LIKE '".$this->prefixinvoice."____-%'";
  71. $sql.= " AND entity = ".$conf->entity;
  72. $resql=$db->query($sql);
  73. if ($resql)
  74. {
  75. $row = $db->fetch_row($resql);
  76. if ($row) { $fayymm = substr($row[0],0,6); $max=$row[0]; }
  77. }
  78. if ($fayymm && ! preg_match('/'.$this->prefixinvoice.'[0-9][0-9][0-9][0-9]/i',$fayymm))
  79. {
  80. $langs->load("errors");
  81. $this->error=$langs->trans('ErrorNumRefModel',$max);
  82. return false;
  83. }
  84. // Check credit note num
  85. $fayymm='';
  86. $posindice=8;
  87. $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
  88. $sql.= " FROM ".MAIN_DB_PREFIX."facture";
  89. $sql.= " WHERE facnumber LIKE '".$this->prefixcreditnote."____-%'";
  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->prefixcreditnote.'[0-9][0-9][0-9][0-9]/i',$fayymm))
  98. {
  99. $this->error=$langs->trans('ErrorNumRefModel',$max);
  100. return false;
  101. }
  102. // Check deposit num
  103. $fayymm='';
  104. $posindice=8;
  105. $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
  106. $sql.= " FROM ".MAIN_DB_PREFIX."facture";
  107. $sql.= " WHERE facnumber LIKE '".$this->prefixdeposit."____-%'";
  108. $sql.= " AND entity = ".$conf->entity;
  109. $resql=$db->query($sql);
  110. if ($resql)
  111. {
  112. $row = $db->fetch_row($resql);
  113. if ($row) { $fayymm = substr($row[0],0,6); $max=$row[0]; }
  114. }
  115. if ($fayymm && ! preg_match('/'.$this->prefixdeposit.'[0-9][0-9][0-9][0-9]/i',$fayymm))
  116. {
  117. $this->error=$langs->trans('ErrorNumRefModel',$max);
  118. return false;
  119. }
  120. return true;
  121. }
  122. /**
  123. * Return next value not used or last value used
  124. *
  125. * @param Societe $objsoc Object third party
  126. * @param Facture $facture Object invoice
  127. * @param string $mode 'next' for next value or 'last' for last value
  128. * @return string Value
  129. */
  130. function getNextValue($objsoc,$facture,$mode='next')
  131. {
  132. global $db,$conf;
  133. if ($facture->type == 2) $prefix=$this->prefixcreditnote;
  134. else if ($facture->type == 3) $prefix=$this->prefixdeposit;
  135. else $prefix=$this->prefixinvoice;
  136. // D'abord on recupere la valeur max
  137. $posindice=8;
  138. $sql = "SELECT MAX(CAST(SUBSTRING(facnumber FROM ".$posindice.") AS SIGNED)) as max"; // This is standard SQL
  139. $sql.= " FROM ".MAIN_DB_PREFIX."facture";
  140. $sql.= " WHERE facnumber LIKE '".$prefix."____-%'";
  141. $sql.= " AND entity = ".$conf->entity;
  142. $resql=$db->query($sql);
  143. dol_syslog(get_class($this)."::getNextValue sql=".$sql);
  144. if ($resql)
  145. {
  146. $obj = $db->fetch_object($resql);
  147. if ($obj) $max = intval($obj->max);
  148. else $max=0;
  149. }
  150. else
  151. {
  152. dol_syslog(get_class($this)."::getNextValue sql=".$sql, LOG_ERR);
  153. return -1;
  154. }
  155. if ($mode == 'last')
  156. {
  157. if ($max >= (pow(10, 4) - 1)) $num=$max; // If counter > 9999, we do not format on 4 chars, we take number as it is
  158. else $num = sprintf("%04s",$max);
  159. $ref='';
  160. $sql = "SELECT facnumber as ref";
  161. $sql.= " FROM ".MAIN_DB_PREFIX."facture";
  162. $sql.= " WHERE facnumber LIKE '".$prefix."____-".$num."'";
  163. $sql.= " AND entity = ".$conf->entity;
  164. dol_syslog(get_class($this)."::getNextValue sql=".$sql);
  165. $resql=$db->query($sql);
  166. if ($resql)
  167. {
  168. $obj = $db->fetch_object($resql);
  169. if ($obj) $ref = $obj->ref;
  170. }
  171. else dol_print_error($db);
  172. return $ref;
  173. }
  174. else if ($mode == 'next')
  175. {
  176. $date=$facture->date; // This is invoice date (not creation date)
  177. $yymm = strftime("%y%m",$date);
  178. 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
  179. else $num = sprintf("%04s",$max+1);
  180. dol_syslog(get_class($this)."::getNextValue return ".$prefix.$yymm."-".$num);
  181. return $prefix.$yymm."-".$num;
  182. }
  183. else dol_print_error('','Bad parameter for getNextValue');
  184. }
  185. /**
  186. * Return next free value
  187. *
  188. * @param Societe $objsoc Object third party
  189. * @param string $objforref Object for number to search
  190. * @param string $mode 'next' for next value or 'last' for last value
  191. * @return string Next free value
  192. */
  193. function getNumRef($objsoc,$objforref,$mode='next')
  194. {
  195. return $this->getNextValue($objsoc,$objforref,$mode);
  196. }
  197. }