2016-07-30 16:41:10 +02:00
#!/usr/bin/env php
< ? php
2016-07-30 17:28:18 +02:00
/* Copyright ( C ) 2016 Laurent Destailleur < eldy @ users . sourceforge . net >
2016-07-30 16:41:10 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2021-11-09 03:01:48 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2016-07-30 16:41:10 +02:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-06-23 15:33:04 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2019-09-23 21:55:30 +02:00
* or see https :// www . gnu . org /
2018-07-26 20:19:52 +02:00
*
2016-07-30 16:41:10 +02:00
* Get a distant dump file and load it into a mysql database
*/
$sapi_type = php_sapi_name ();
$script_file = basename ( __FILE__ );
2023-12-04 10:24:06 +01:00
$path = dirname ( __FILE__ ) . '/' ;
2016-07-30 16:41:10 +02:00
// Test if batch mode
if ( substr ( $sapi_type , 0 , 3 ) == 'cgi' ) {
echo " Error: You are using PHP for CGI. To execute " . $script_file . " from command line, you must use PHP for CLI mode. \n " ;
exit ;
}
// Global variables
2023-12-04 10:24:06 +01:00
$error = 0 ;
2016-07-30 16:41:10 +02:00
2023-12-04 10:24:06 +01:00
$confirm = isset ( $argv [ 1 ]) ? $argv [ 1 ] : '' ;
2016-07-30 16:41:10 +02:00
// Include Dolibarr environment
2023-12-04 10:24:06 +01:00
$res = 0 ;
if ( ! $res && file_exists ( $path . " ../../master.inc.php " )) {
$res = @ include $path . " ../../master.inc.php " ;
2021-03-01 00:19:52 +01:00
}
2023-12-04 10:24:06 +01:00
if ( ! $res && file_exists ( $path . " ../../htdocs/master.inc.php " )) {
$res = @ include $path . " ../../htdocs/master.inc.php " ;
2021-03-01 00:19:52 +01:00
}
2023-12-04 10:24:06 +01:00
if ( ! $res && file_exists ( " ../master.inc.php " )) {
$res = @ include " ../master.inc.php " ;
2021-03-01 00:19:52 +01:00
}
2023-12-04 10:24:06 +01:00
if ( ! $res && file_exists ( " ../../master.inc.php " )) {
$res = @ include " ../../master.inc.php " ;
2021-03-01 00:19:52 +01:00
}
2023-12-04 10:24:06 +01:00
if ( ! $res && file_exists ( " ../../../master.inc.php " )) {
$res = @ include " ../../../master.inc.php " ;
2021-03-01 00:19:52 +01:00
}
2023-12-04 10:24:06 +01:00
if ( ! $res && preg_match ( '/\/nltechno([^\/]*)\//' , $_SERVER [ " PHP_SELF " ], $reg )) {
$res = @ include $path . " ../../../dolibarr " . $reg [ 1 ] . " /htdocs/master.inc.php " ; // Used on dev env only
2021-03-01 00:19:52 +01:00
}
2023-12-04 10:24:06 +01:00
if ( ! $res && preg_match ( '/\/nltechno([^\/]*)\//' , $_SERVER [ " PHP_SELF " ], $reg )) {
$res = @ include " ../../../dolibarr " . $reg [ 1 ] . " /htdocs/master.inc.php " ; // Used on dev env only
2021-03-01 00:19:52 +01:00
}
2023-12-04 10:24:06 +01:00
if ( ! $res ) {
2021-03-01 00:19:52 +01:00
die ( " Failed to include master.inc.php file \n " );
}
2018-07-26 11:57:25 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2016-07-30 16:41:10 +02:00
/*
* Main
*/
2020-03-30 11:39:02 +02:00
print " ***** " . $script_file . " ***** \n " ;
print " Update dates to current year for database name = " . $db -> database_name . " \n " ;
2021-03-01 00:19:52 +01:00
if ( empty ( $confirm )) {
2016-07-30 16:41:10 +02:00
print " Usage: $script_file confirm \n " ;
print " Return code: 0 if success, <>0 if error \n " ;
exit ( - 1 );
}
2023-12-04 10:24:06 +01:00
$tmp = dol_getdate ( dol_now ());
2016-07-30 16:41:10 +02:00
2023-12-04 10:24:06 +01:00
$tables = array (
'propal' => array ( 0 => 'datep' , 1 => 'fin_validite' , 2 => 'date_valid' , 3 => 'date_cloture' ),
'commande' => array ( 0 => 'date_commande' , 1 => 'date_valid' , 2 => 'date_cloture' ),
'facture' => array ( 0 => 'datec' , 1 => 'datef' , 2 => 'date_valid' , 3 => 'date_lim_reglement' ),
'paiement' => array ( 0 => 'datep' ),
'bank' => array ( 0 => 'datev' , 1 => 'dateo' ),
'commande_fournisseur' => array ( 0 => 'date_commande' , 1 => 'date_valid' , 3 => 'date_creation' , 4 => 'date_approve' , 5 => 'date_approve2' , 6 => 'date_livraison' ),
'supplier_proposal' => array ( 0 => 'datec' , 1 => 'date_valid' , 2 => 'date_cloture' ),
'expensereport' => array ( 0 => 'date_debut' , 1 => 'date_fin' , 2 => 'date_create' , 3 => 'date_valid' , 4 => 'date_approve' , 5 => 'date_refuse' , 6 => 'date_cancel' ),
'holiday' => array ( 0 => 'date_debut' , 1 => 'date_fin' , 2 => 'date_create' , 3 => 'date_valid' , 5 => 'date_refuse' , 6 => 'date_cancel' ),
'ticket' => array ( 0 => 'datec' , 1 => 'date_read' , 2 => 'date_close' )
2016-07-30 17:28:18 +02:00
);
2016-07-30 16:41:10 +02:00
2023-12-04 10:24:06 +01:00
$year = 2010 ;
$currentyear = $tmp [ 'year' ];
2021-03-01 00:19:52 +01:00
while ( $year <= $currentyear ) {
//$year=2021;
2023-12-04 10:24:06 +01:00
$delta1 = ( $currentyear - $year );
$delta2 = ( $currentyear - $year - 1 );
2021-03-01 00:19:52 +01:00
//$delta=-1;
if ( $delta1 ) {
foreach ( $tables as $tablekey => $tableval ) {
print " Correct " . $tablekey . " for year " . $year . " and move them to current year " . $currentyear . " " ;
2023-12-04 10:24:06 +01:00
$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) " ;
2021-03-01 00:19:52 +01:00
//$sql="select rowid from ".MAIN_DB_PREFIX.$tablekey." where ".$tableval[0]." between '".$year."-01-01' and '".$year."-12-31' and ".$tableval[0]." > NOW()";
$resql = $db -> query ( $sql );
if ( $resql ) {
$num = $db -> num_rows ( $resql );
2023-12-04 10:24:06 +01:00
$i = 0 ;
2021-03-01 00:19:52 +01:00
while ( $i < $num ) {
2023-12-04 10:24:06 +01:00
$obj = $db -> fetch_object ( $resql );
2021-03-01 00:19:52 +01:00
if ( $obj ) {
print " . " ;
2023-12-04 10:24:06 +01:00
$sql2 = " UPDATE " . MAIN_DB_PREFIX . $tablekey . " set " ;
$j = 0 ;
2021-03-01 00:19:52 +01:00
foreach ( $tableval as $field ) {
if ( $j ) {
2023-12-04 10:24:06 +01:00
$sql2 .= " , " ;
2021-03-01 00:19:52 +01:00
}
2023-12-04 10:24:06 +01:00
$sql2 .= $field . " = " . $db -> ifsql ( " DATE_ADD( " . $field . " , INTERVAL " . $delta1 . " YEAR) > NOW() " , " DATE_ADD( " . $field . " , INTERVAL " . $delta2 . " YEAR) " , " DATE_ADD( " . $field . " , INTERVAL " . $delta1 . " YEAR) " );
2021-03-01 00:19:52 +01:00
$j ++ ;
}
2023-12-04 10:24:06 +01:00
$sql2 .= " WHERE rowid = " . $obj -> rowid ;
2021-03-01 00:19:52 +01:00
//print $sql2."\n";
$resql2 = $db -> query ( $sql2 );
2023-12-04 10:24:06 +01:00
if ( ! $resql2 ) {
2021-03-01 00:19:52 +01:00
dol_print_error ( $db );
}
}
$i ++ ;
}
} else {
dol_print_error ( $db );
}
print " \n " ;
}
}
$year ++ ;
2016-07-30 16:41:10 +02:00
}
print " \n " ;
exit ( 0 );