rebuild_merge_pdf.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #!/usr/bin/env 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=".dol_getmypid()." *****\n";
  49. dol_syslog($script_file." launched with arg ".join(',',$argv));
  50. // Check parameters
  51. if (! isset($argv[1]))
  52. {
  53. usage();
  54. exit(-1);
  55. }
  56. $diroutputpdf=$conf->facture->dir_output . '/temp';
  57. $newlangid='en_EN'; // To force a new lang id
  58. $filter=array();
  59. $regenerate=''; // Ask regenerate (contains name of model to use)
  60. $option='';
  61. $fileprefix='mergedpdf';
  62. foreach ($argv as $key => $value)
  63. {
  64. $found=false;
  65. // Define options
  66. if (preg_match('/^lang=/i',$value))
  67. {
  68. $found=true;
  69. $valarray=explode('=',$value);
  70. $newlangid=$valarray[1];
  71. print 'Use language '.$newlangid.".\n";
  72. }
  73. if (preg_match('/^prefix=/i',$value))
  74. {
  75. $found=true;
  76. $valarray=explode('=',$value);
  77. $fileprefix=$valarray[1];
  78. print 'Use prefix for filename '.$fileprefix.".\n";
  79. }
  80. if (preg_match('/^regenerate=(.*)/i',$value,$reg))
  81. {
  82. if (! in_array($reg[1],array('','0','no')))
  83. {
  84. $found=true;
  85. $regenerate=$reg[1];
  86. print 'Regeneration of PDF is requested with template '.$regenerate."\n";
  87. }
  88. }
  89. if ($value == 'filter=all')
  90. {
  91. $found=true;
  92. $option.=(empty($option)?'':'_').'all';
  93. $filter[]='all';
  94. print 'Rebuild PDF for all invoices'."\n";
  95. }
  96. if ($value == 'filter=date')
  97. {
  98. $found=true;
  99. $option.=(empty($option)?'':'_').'date_'.$argv[$key+1].'_'.$argv[$key+2];
  100. $filter[]='date';
  101. $dateafterdate=dol_stringtotime($argv[$key+1]);
  102. $datebeforedate=dol_stringtotime($argv[$key+2]);
  103. print 'Rebuild PDF for invoices validated between '.dol_print_date($dateafterdate,'day','gmt')." and ".dol_print_date($datebeforedate,'day','gmt').".\n";
  104. }
  105. if ($value == 'filter=payments')
  106. {
  107. $found=true;
  108. $option.=(empty($option)?'':'_').'payments_'.$argv[$key+1].'_'.$argv[$key+2];
  109. $filter[]='payments';
  110. $paymentdateafter=dol_stringtotime($argv[$key+1].'000000');
  111. $paymentdatebefore=dol_stringtotime($argv[$key+2].'235959');
  112. if (empty($paymentdateafter) || empty($paymentdatebefore))
  113. {
  114. print 'Error: Bad date format or value'."\n";
  115. exit(-1);
  116. }
  117. print 'Rebuild PDF for invoices with at least one payment between '.dol_print_date($paymentdateafter,'day','gmt')." and ".dol_print_date($paymentdatebefore,'day','gmt').".\n";
  118. }
  119. if ($value == 'filter=nopayment')
  120. {
  121. $found=true;
  122. $option.=(empty($option)?'':'_').'nopayment';
  123. $filter[]='nopayment';
  124. print 'Rebuild PDF for invoices with no payment done yet.'."\n";
  125. }
  126. if ($value == 'filter=bank')
  127. {
  128. $found=true;
  129. $option.=(empty($option)?'':'_').'bank_'.$argv[$key+1];
  130. $filter[]='bank';
  131. $paymentonbankref=$argv[$key+1];
  132. $bankaccount=new Account($db);
  133. $result=$bankaccount->fetch(0,$paymentonbankref);
  134. if ($result <= 0)
  135. {
  136. print 'Error: Bank account with ref "'.$paymentonbankref.'" not found'."\n";
  137. exit(-1);
  138. }
  139. $paymentonbankid=$bankaccount->id;
  140. print 'Rebuild PDF for invoices with at least one payment on financial account '.$bankaccount->ref."\n";
  141. }
  142. if ($value == 'filter=nodeposit')
  143. {
  144. $found=true;
  145. $option.=(empty($option)?'':'_').'nodeposit';
  146. $filter[]='nodeposit';
  147. print 'Exclude deposit invoices'."\n";
  148. }
  149. if ($value == 'filter=noreplacement')
  150. {
  151. $found=true;
  152. $option.=(empty($option)?'':'_').'noreplacement';
  153. $filter[]='noreplacement';
  154. print 'Exclude replacement invoices'."\n";
  155. }
  156. if ($value == 'filter=nocreditnote')
  157. {
  158. $found=true;
  159. $option.=(empty($option)?'':'_').'nocreditnote';
  160. $filter[]='nocreditnote';
  161. print 'Exclude credit note invoices'."\n";
  162. }
  163. if ($value == 'filter=excludethirdparties')
  164. {
  165. $found=true;
  166. $filter[]='excludethirdparties';
  167. $thirdpartiesid=explode(',',$argv[$key+1]);
  168. print 'Exclude thirdparties with id in list ('.join(',',$thirdpartiesid).").\n";
  169. $option.=(empty($option)?'':'_').'excludethirdparties'.join('-',$thirdpartiesid);
  170. }
  171. if ($value == 'filter=onlythirdparties')
  172. {
  173. $found=true;
  174. $filter[]='onlythirdparties';
  175. $thirdpartiesid=explode(',',$argv[$key+1]);
  176. print 'Only thirdparties with id in list ('.join(',',$thirdpartiesid).").\n";
  177. $option.=(empty($option)?'':'_').'onlythirdparty'.join('-',$thirdpartiesid);
  178. }
  179. if (! $found && preg_match('/filter=/i',$value))
  180. {
  181. usage();
  182. exit(-1);
  183. }
  184. }
  185. // Check if an option and a filter has been provided
  186. if (empty($option) && count($filter) <= 0)
  187. {
  188. usage();
  189. exit(-1);
  190. }
  191. // Check if there is no uncompatible choice
  192. if (in_array('payments',$filter) && in_array('nopayment',$filter))
  193. {
  194. usage();
  195. exit(-1);
  196. }
  197. if (in_array('bank',$filter) && in_array('nopayment',$filter))
  198. {
  199. usage();
  200. exit(-1);
  201. }
  202. // Define SQL and SQL request to select invoices
  203. // Use $filter, $dateafterdate, datebeforedate, $paymentdateafter, $paymentdatebefore
  204. $result=rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, 1, $regenerate, $option, $paymentonbankid, $thirdpartiesid, $fileprefix);
  205. // -------------------- END OF YOUR CODE --------------------
  206. if ($result >= 0)
  207. {
  208. $error=0;
  209. print '--- end ok'."\n";
  210. }
  211. else
  212. {
  213. $error=$result;
  214. print '--- end error code='.$error."\n";
  215. }
  216. $db->close();
  217. exit($error);
  218. /**
  219. * Show usage of script
  220. *
  221. * @return unknown
  222. */
  223. function usage()
  224. {
  225. global $script_file;
  226. print "Rebuild PDF files for some invoices and merge PDF files into one.\n";
  227. print "\n";
  228. print "To build/merge PDF for invoices in a date range:\n";
  229. print "Usage: ".$script_file." filter=date dateafter datebefore\n";
  230. print "To build/merge PDF for invoices with at least one payment in a date range:\n";
  231. print "Usage: ".$script_file." filter=payments dateafter datebefore\n";
  232. print "To build/merge PDF for invoices with at least one payment onto a bank account:\n";
  233. print "Usage: ".$script_file." filter=bank bankref\n";
  234. print "To build/merge PDF for all invoices, use filter=all\n";
  235. print "Usage: ".$script_file." filter=all\n";
  236. print "To build/merge PDF for invoices with no payments, use filter=nopayment\n";
  237. print "Usage: ".$script_file." filter=nopayment\n";
  238. print "To exclude credit notes, use filter=nocreditnote\n";
  239. print "To exclude replacement invoices, use filter=noreplacement\n";
  240. print "To exclude deposit invoices, use filter=nodeposit\n";
  241. print "To exclude some thirdparties, use filter=excludethirdparties id1,id2...\n";
  242. print "To limit to some thirdparties, use filter=onlythirdparties id1,id2...\n";
  243. print "To regenerate existing PDF, use regenerate=templatename\n";
  244. print "To generate invoices in a language, use lang=xx_XX\n";
  245. print "To set prefix of generated file name, use prefix=myfileprefix\n";
  246. print "\n";
  247. print "Example: ".$script_file." filter=payments 20080101 20081231 lang=fr_FR regenerate=crabe\n";
  248. print "Example: ".$script_file." filter=all lang=en_US\n";
  249. print "\n";
  250. print "Note that some filters can be cumulated.\n";
  251. }