rebuild_merge_pdf.php 8.7 KB

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