updatedemo.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  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. * Get a distant dump file and load it into a mysql database
  20. */
  21. $sapi_type = php_sapi_name();
  22. $script_file = basename(__FILE__);
  23. $path=dirname(__FILE__).'/';
  24. // Test if batch mode
  25. if (substr($sapi_type, 0, 3) == 'cgi') {
  26. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  27. exit;
  28. }
  29. // Global variables
  30. $error=0;
  31. $confirm=isset($argv[1])?$argv[1]:'';
  32. // Include Dolibarr environment
  33. $res=0;
  34. if (! $res && file_exists($path."../../master.inc.php")) {
  35. $res=@include $path."../../master.inc.php";
  36. }
  37. if (! $res && file_exists($path."../../htdocs/master.inc.php")) {
  38. $res=@include $path."../../htdocs/master.inc.php";
  39. }
  40. if (! $res && file_exists("../master.inc.php")) {
  41. $res=@include "../master.inc.php";
  42. }
  43. if (! $res && file_exists("../../master.inc.php")) {
  44. $res=@include "../../master.inc.php";
  45. }
  46. if (! $res && file_exists("../../../master.inc.php")) {
  47. $res=@include "../../../master.inc.php";
  48. }
  49. if (! $res && preg_match('/\/nltechno([^\/]*)\//', $_SERVER["PHP_SELF"], $reg)) {
  50. $res=@include $path."../../../dolibarr".$reg[1]."/htdocs/master.inc.php"; // Used on dev env only
  51. }
  52. if (! $res && preg_match('/\/nltechno([^\/]*)\//', $_SERVER["PHP_SELF"], $reg)) {
  53. $res=@include "../../../dolibarr".$reg[1]."/htdocs/master.inc.php"; // Used on dev env only
  54. }
  55. if (! $res) {
  56. die("Failed to include master.inc.php file\n");
  57. }
  58. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  59. /*
  60. * Main
  61. */
  62. print "***** ".$script_file." *****\n";
  63. print "Update dates to current year for database name = ".$db->database_name."\n";
  64. if (empty($confirm)) {
  65. print "Usage: $script_file confirm\n";
  66. print "Return code: 0 if success, <>0 if error\n";
  67. exit(-1);
  68. }
  69. $tmp=dol_getdate(dol_now());
  70. $tables=array(
  71. 'propal'=>array(0=>'datep', 1=>'fin_validite', 2=>'date_valid', 3=>'date_cloture'),
  72. 'commande'=>array(0=>'date_commande', 1=>'date_valid', 2=>'date_cloture'),
  73. 'facture'=>array(0=>'datec', 0=>'datef', 1=>'date_valid', 2=>'date_lim_reglement'),
  74. 'paiement'=>array(0=>'datep'),
  75. 'bank'=>array(0=>'datev', 1=>'dateo'),
  76. 'commande_fournisseur'=>array(0=>'date_commande', 1=>'date_valid', 3=>'date_creation', 4=>'date_approve', 5=>'date_approve2', 6=>'date_livraison'),
  77. 'supplier_proposal'=>array(0=>'datec', 1=>'date_valid', 2=>'date_cloture'),
  78. 'expensereport'=>array(0=>'date_debut', 1=>'date_fin', 2=>'date_create', 3=>'date_valid', 4=>'date_approve', 5=>'date_refuse', 6=>'date_cancel'),
  79. 'holiday'=>array(0=>'date_debut', 1=>'date_fin', 2=>'date_create', 3=>'date_valid', 5=>'date_refuse', 6=>'date_cancel'),
  80. 'ticket'=>array(0=>'datec', 1=>'date_read', 2=>'date_close')
  81. );
  82. $year=2010;
  83. $currentyear=$tmp['year'];
  84. while ($year <= $currentyear) {
  85. //$year=2021;
  86. $delta1=($currentyear - $year);
  87. $delta2=($currentyear - $year - 1);
  88. //$delta=-1;
  89. if ($delta1) {
  90. foreach ($tables as $tablekey => $tableval) {
  91. print "Correct ".$tablekey." for year ".$year." and move them to current year ".$currentyear." ";
  92. $sql="select rowid from ".MAIN_DB_PREFIX.$tablekey." where ".$tableval[0]." between '".$year."-01-01' and '".$year."-12-31' and ".$tableval[0]." < DATE_ADD(NOW(), INTERVAL -1 YEAR)";
  93. //$sql="select rowid from ".MAIN_DB_PREFIX.$tablekey." where ".$tableval[0]." between '".$year."-01-01' and '".$year."-12-31' and ".$tableval[0]." > NOW()";
  94. $resql = $db->query($sql);
  95. if ($resql) {
  96. $num = $db->num_rows($resql);
  97. $i=0;
  98. while ($i < $num) {
  99. $obj=$db->fetch_object($resql);
  100. if ($obj) {
  101. print ".";
  102. $sql2="UPDATE ".MAIN_DB_PREFIX.$tablekey." set ";
  103. $j=0;
  104. foreach ($tableval as $field) {
  105. if ($j) {
  106. $sql2.=", ";
  107. }
  108. $sql2.= $field." = ".$db->ifsql("DATE_ADD(".$field.", INTERVAL ".$delta1." YEAR) > NOW()", "DATE_ADD(".$field.", INTERVAL ".$delta2." YEAR)", "DATE_ADD(".$field.", INTERVAL ".$delta1." YEAR)");
  109. $j++;
  110. }
  111. $sql2.=" WHERE rowid = ".$obj->rowid;
  112. //print $sql2."\n";
  113. $resql2 = $db->query($sql2);
  114. if (! $resql2) {
  115. dol_print_error($db);
  116. }
  117. }
  118. $i++;
  119. }
  120. } else {
  121. dol_print_error($db);
  122. }
  123. print "\n";
  124. }
  125. }
  126. $year++;
  127. }
  128. print "\n";
  129. exit(0);