updatedemo.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 2 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")) $res=@include $path."../../master.inc.php";
  35. if (! $res && file_exists($path."../../htdocs/master.inc.php")) $res=@include $path."../../htdocs/master.inc.php";
  36. if (! $res && file_exists("../master.inc.php")) $res=@include "../master.inc.php";
  37. if (! $res && file_exists("../../master.inc.php")) $res=@include "../../master.inc.php";
  38. if (! $res && file_exists("../../../master.inc.php")) $res=@include "../../../master.inc.php";
  39. if (! $res && preg_match('/\/nltechno([^\/]*)\//', $_SERVER["PHP_SELF"], $reg)) $res=@include $path."../../../dolibarr".$reg[1]."/htdocs/master.inc.php"; // Used on dev env only
  40. if (! $res && preg_match('/\/nltechno([^\/]*)\//', $_SERVER["PHP_SELF"], $reg)) $res=@include "../../../dolibarr".$reg[1]."/htdocs/master.inc.php"; // Used on dev env only
  41. if (! $res) die("Failed to include master.inc.php file\n");
  42. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  43. /*
  44. * Main
  45. */
  46. if (empty($confirm))
  47. {
  48. print "Usage: $script_file confirm\n";
  49. print "Return code: 0 if success, <>0 if error\n";
  50. exit(-1);
  51. }
  52. $tmp=dol_getdate(dol_now());
  53. $tables=array(
  54. 'propal'=>array(0=>'datep', 1=>'fin_validite', 2=>'date_valid', 3=>'date_cloture'),
  55. 'commande'=>array(0=>'date_commande', 1=>'date_valid', 2=>'date_cloture'),
  56. 'facture'=>array(0=>'datef', 1=>'date_valid', 2=>'date_lim_reglement'),
  57. 'paiement'=>array(0=>'datep'),
  58. 'bank'=>array(0=>'datev', 1=>'dateo'),
  59. 'commande_fournisseur'=>array(0=>'date_commande', 1=>'date_valid', 3=>'date_creation', 4=>'date_approve', 5=>'date_approve2', 6=>'date_livraison'),
  60. 'supplier_proposal'=>array(0=>'datec', 1=>'date_valid', 2=>'date_cloture')
  61. );
  62. $year=2010;
  63. $currentyear=$tmp['year'];
  64. while ($year <= $currentyear)
  65. {
  66. //$year=2021;
  67. $delta=($currentyear - $year);
  68. //$delta=-1;
  69. if ($delta)
  70. {
  71. foreach($tables as $tablekey => $tableval)
  72. {
  73. print "\nCorrect ".$tablekey." for year ".$year." and move them to current year ".$currentyear." ";
  74. $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)";
  75. //$sql="select rowid from ".MAIN_DB_PREFIX.$tablekey." where ".$tableval[0]." between '".$year."-01-01' and '".$year."-12-31' and ".$tableval[0]." > NOW()";
  76. $resql = $db->query($sql);
  77. if ($resql)
  78. {
  79. $num = $db->num_rows($resql);
  80. $i=0;
  81. while ($i < $num)
  82. {
  83. $obj=$db->fetch_object($resql);
  84. if ($obj)
  85. {
  86. print ".";
  87. $sql2="UPDATE ".MAIN_DB_PREFIX.$tablekey." set ";
  88. $j=0;
  89. foreach($tableval as $field)
  90. {
  91. if ($j) $sql2.=", ";
  92. $sql2.= $field." = DATE_ADD(".$field.", INTERVAL ".$delta." YEAR)";
  93. $j++;
  94. }
  95. $sql2.=" WHERE rowid = ".$obj->rowid;
  96. //print $sql2."\n";
  97. $resql2 = $db->query($sql2);
  98. if (! $resql2) dol_print_error($db);
  99. }
  100. $i++;
  101. }
  102. }
  103. else dol_print_error($db);
  104. }
  105. }
  106. $year++;
  107. }
  108. print "\n";
  109. exit(0);