rebuild_merge_pdf.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #!/usr/bin/php
  2. <?php
  3. /*
  4. * Copyright (C) 2009-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  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 <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file scripts/invoices/rebuild_merge_pdf.php
  21. * \ingroup facture
  22. * \brief Script to rebuild PDF and merge PDF files into one
  23. */
  24. $sapi_type = php_sapi_name();
  25. $script_file = basename(__FILE__);
  26. $path=dirname(__FILE__).'/';
  27. // Test if batch mode
  28. if (substr($sapi_type, 0, 3) == 'cgi') {
  29. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  30. exit(-1);
  31. }
  32. // Include Dolibarr environment
  33. require_once($path."../../htdocs/master.inc.php");
  34. // After this $db is an opened handler to database. We close it at end of file.
  35. require_once(DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php");
  36. require_once(DOL_DOCUMENT_ROOT."/core/modules/facture/modules_facture.php");
  37. require_once(DOL_DOCUMENT_ROOT."/core/lib/date.lib.php");
  38. require_once(DOL_DOCUMENT_ROOT.'/core/lib/invoice2.lib.php');
  39. // Load main language strings
  40. $langs->load("main");
  41. // Global variables
  42. $version=DOL_VERSION;
  43. $error=0;
  44. /*
  45. * Main
  46. */
  47. @set_time_limit(0);
  48. print "***** ".$script_file." (".$version.") pid=".getmypid()." *****\n";
  49. // Check parameters
  50. if (! isset($argv[1]))
  51. {
  52. usage();
  53. exit(-1);
  54. }
  55. $diroutputpdf=$conf->facture->dir_output . '/temp';
  56. $newlangid='en_EN'; // To force a new lang id
  57. $filter=array();
  58. $regenerate=''; // Ask regenerate (contains name of model to use)
  59. $option='';
  60. foreach ($argv as $key => $value)
  61. {
  62. $found=false;
  63. // Define options
  64. if (preg_match('/^lang=/i',$value))
  65. {
  66. $found=true;
  67. $valarray=explode('=',$value);
  68. $newlangid=$valarray[1];
  69. print 'Use language '.$newlangid.".\n";
  70. }
  71. if (preg_match('/^regenerate=(.*)/i',$value,$reg))
  72. {
  73. if (! in_array($reg[1],array('','0','no')))
  74. {
  75. $found=true;
  76. $regenerate=$reg[1];
  77. print 'Regeneration of PDF is requested with template '.$regenerate."\n";
  78. }
  79. }
  80. if ($value == 'filter=all')
  81. {
  82. $found=true;
  83. $option.=(empty($option)?'':'_').'all';
  84. $filter[]='all';
  85. print 'Rebuild PDF for all invoices'."\n";
  86. }
  87. if ($value == 'filter=date')
  88. {
  89. $found=true;
  90. $option.=(empty($option)?'':'_').'date_'.$argv[$key+1].'_'.$argv[$key+2];
  91. $filter[]='date';
  92. $dateafterdate=dol_stringtotime($argv[$key+1]);
  93. $datebeforedate=dol_stringtotime($argv[$key+2]);
  94. print 'Rebuild PDF for invoices validated between '.dol_print_date($dateafterdate,'day')." and ".dol_print_date($datebeforedate,'day').".\n";
  95. }
  96. if ($value == 'filter=payments')
  97. {
  98. $found=true;
  99. $option.=(empty($option)?'':'_').'payments_'.$argv[$key+1].'_'.$argv[$key+2];
  100. $filter[]='payments';
  101. $paymentdateafter=dol_stringtotime($argv[$key+1]);
  102. $paymentdatebefore=dol_stringtotime($argv[$key+2]);
  103. if (empty($paymentdateafter) || empty($paymentdatebefore))
  104. {
  105. print 'Error: Bad date format'."\n";
  106. exit(-1);
  107. }
  108. print 'Rebuild PDF for invoices with at least one payment between '.dol_print_date($paymentdateafter,'day')." and ".dol_print_date($paymentdatebefore,'day').".\n";
  109. }
  110. if ($value == 'filter=nopayment')
  111. {
  112. $found=true;
  113. $option.=(empty($option)?'':'_').'nopayment';
  114. $filter[]='nopayment';
  115. print 'Rebuild PDF for invoices with no payment done yet.'."\n";
  116. }
  117. if ($value == 'filter=bank')
  118. {
  119. $found=true;
  120. $option.=(empty($option)?'':'_').'bank_'.$argv[$key+1];
  121. $filter[]='bank';
  122. $paymentonbankref=$argv[$key+1];
  123. $bankaccount=new Account($db);
  124. $result=$bankaccount->fetch(0,$paymentonbankref);
  125. if ($result <= 0)
  126. {
  127. print 'Error: Bank account with ref "'.$paymentonbankref.'" not found'."\n";
  128. exit(-1);
  129. }
  130. $paymentonbankid=$bankaccount->id;
  131. print 'Rebuild PDF for invoices with at least one payment on financial account '.$bankaccount->ref."\n";
  132. }
  133. if ($value == 'filter=nodeposit')
  134. {
  135. $found=true;
  136. $option.=(empty($option)?'':'_').'nodeposit';
  137. $filter[]='nodeposit';
  138. print 'Exclude deposit invoices'."\n";
  139. }
  140. if ($value == 'filter=noreplacement')
  141. {
  142. $found=true;
  143. $option.=(empty($option)?'':'_').'noreplacement';
  144. $filter[]='noreplacement';
  145. print 'Exclude replacement invoices'."\n";
  146. }
  147. if ($value == 'filter=nocreditnote')
  148. {
  149. $found=true;
  150. $option.=(empty($option)?'':'_').'nocreditnote';
  151. $filter[]='nocreditnote';
  152. print 'Exclude credit note invoices'."\n";
  153. }
  154. if (! $found && preg_match('/filter=/i',$value))
  155. {
  156. usage();
  157. exit(-1);
  158. }
  159. }
  160. // Check if an option and a filter has been provided
  161. if (empty($option) && count($filter) <= 0)
  162. {
  163. usage();
  164. exit(-1);
  165. }
  166. // Check if there is no uncompatible choice
  167. if (in_array('payments',$filter) && in_array('nopayment',$filter))
  168. {
  169. usage();
  170. exit(-1);
  171. }
  172. if (in_array('bank',$filter) && in_array('nopayment',$filter))
  173. {
  174. usage();
  175. exit(-1);
  176. }
  177. // Define SQL and SQL request to select invoices
  178. // Use $filter, $dateafterdate, datebeforedate, $paymentdateafter, $paymentdatebefore
  179. $result=rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, 1, $regenerate, $option, $paymentonbankid);
  180. // -------------------- END OF YOUR CODE --------------------
  181. if ($result >= 0)
  182. {
  183. $error=0;
  184. print '--- end ok'."\n";
  185. }
  186. else
  187. {
  188. $error=$result;
  189. print '--- end error code='.$error."\n";
  190. }
  191. $db->close();
  192. exit($error);
  193. /**
  194. * Show usage of script
  195. *
  196. * @return unknown
  197. */
  198. function usage()
  199. {
  200. global $script_file;
  201. print "Rebuild PDF files for some invoices and merge PDF files into one.\n";
  202. print "\n";
  203. print "To build/merge PDF for invoices in a date range:\n";
  204. print "Usage: ".$script_file." filter=date dateafter datebefore\n";
  205. print "To build/merge PDF for invoices with at least one payment in a date range:\n";
  206. print "Usage: ".$script_file." filter=payments dateafter datebefore\n";
  207. print "To build/merge PDF for invoices with at least one payment onto a bank account:\n";
  208. print "Usage: ".$script_file." filter=bank bankref\n";
  209. print "To build/merge PDF for all invoices, use filter=all\n";
  210. print "Usage: ".$script_file." filter=all\n";
  211. print "To build/merge PDF for invoices with no payments, use filter=nopayment\n";
  212. print "Usage: ".$script_file." filter=nopayment\n";
  213. print "To exclude credit notes, use filter=nocreditnote\n";
  214. print "To exclude replacement invoices, use filter=noreplacement\n";
  215. print "To exclude deposit invoices, use filter=nodeposit\n";
  216. print "To regenerate existing PDF, use regenerate=crabe\n";
  217. print "To generate invoices in a language, use lang=xx_XX\n";
  218. print "\n";
  219. print "Example: ".$script_file." filter=payments 20080101 20081231 lang=fr_FR regenerate=crabe\n";
  220. print "Example: ".$script_file." filter=all lang=en_US\n";
  221. print "\n";
  222. print "Note that some filters can be cumulated.\n";
  223. }
  224. ?>