From 857de646071d19ac51011923da1b95763bb6924b Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 30 Jul 2016 16:41:10 +0200
Subject: [PATCH] Fix multicurrency

---
 dev/initdemo/updatedemo.php       | 165 ++++++++++++++++++++++++++++++
 htdocs/comm/propal/card.php       |   2 +-
 htdocs/commande/card.php          |   2 +-
 htdocs/compta/facture.php         |   2 +-
 htdocs/fourn/commande/card.php    |   2 +-
 htdocs/fourn/facture/card.php     |   2 +-
 htdocs/langs/en_US/main.lang      |   1 +
 htdocs/supplier_proposal/card.php |   4 +-
 8 files changed, 173 insertions(+), 7 deletions(-)
 create mode 100755 dev/initdemo/updatedemo.php

diff --git a/dev/initdemo/updatedemo.php b/dev/initdemo/updatedemo.php
new file mode 100755
index 00000000000..feabbe9ac6c
--- /dev/null
+++ b/dev/initdemo/updatedemo.php
@@ -0,0 +1,165 @@
+#!/usr/bin/env php
+<?php
+/* Copyright (C) 2012 Laurent Destailleur	<eldy@users.sourceforge.net>
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * or see http://www.gnu.org/
+ * 
+ * Get a distant dump file and load it into a mysql database
+ */
+
+$sapi_type = php_sapi_name();
+$script_file = basename(__FILE__);
+$path=dirname(__FILE__).'/';
+
+// 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
+$error=0;
+
+$confirm=isset($argv[1])?$argv[1]:'';
+
+// Include Dolibarr environment
+$res=0;
+if (! $res && file_exists($path."../../master.inc.php")) $res=@include($path."../../master.inc.php");
+if (! $res && file_exists($path."../../htdocs/master.inc.php")) $res=@include($path."../../htdocs/master.inc.php");
+if (! $res && file_exists("../master.inc.php")) $res=@include("../master.inc.php");
+if (! $res && file_exists("../../master.inc.php")) $res=@include("../../master.inc.php");
+if (! $res && file_exists("../../../master.inc.php")) $res=@include("../../../master.inc.php");
+if (! $res && preg_match('/\/nltechno([^\/]*)\//',$_SERVER["PHP_SELF"],$reg)) $res=@include($path."../../../dolibarr".$reg[1]."/htdocs/master.inc.php"); // Used on dev env only
+if (! $res && preg_match('/\/nltechno([^\/]*)\//',$_SERVER["PHP_SELF"],$reg)) $res=@include("../../../dolibarr".$reg[1]."/htdocs/master.inc.php"); // Used on dev env only
+if (! $res) die ("Failed to include master.inc.php file\n");
+include_once(DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php');
+
+
+/*
+ *	Main
+ */
+
+if (empty($confirm))
+{
+	print "Usage: $script_file confirm\n";
+	print "Return code: 0 if success, <>0 if error\n";
+	exit(-1);
+}
+
+
+$tmp=dol_getdate(dol_now());
+
+
+
+$year=2010;
+$currentyear=$tmp['year'];
+while ($year < ($currentyear - 1))      // We want to keep 2 years of data 
+{
+    $delta=($currentyear - $year);
+    
+    print "Correct proposal for year ".$year." and move them to current year ".$currentyear."\n"; 
+    $sql="select rowid from ".MAIN_DB_PREFIX."propal where datep between '".$year."-01-01' and '".$year."-12-31'";
+    $resql = $db->query($sql);
+    if ($resql)
+    {
+        $num = $db->num_rows($resql);
+        $i=0;
+        while ($i < $num)
+        {
+            $obj=$db->fetch_object($resql);
+            if ($obj)
+            {
+                print ".";
+            
+                $sql2="UPDATE ".MAIN_DB_PREFIX."propal set ";
+                $sql2.= "datep        = DATE_ADD(datep,        INTERVAL ".$delta." YEAR),"; 
+                $sql2.= "fin_validite = DATE_ADD(fin_validite, INTERVAL ".$delta." YEAR),";
+                $sql2.= "date_valid   = DATE_ADD(date_valid,   INTERVAL ".$delta." YEAR),";
+                $sql2.= "date_cloture = DATE_ADD(date_cloture, INTERVAL ".$delta." YEAR)";
+                $sql2.=" WHERE rowid = ".$obj->rowid;
+                //print $sql2."\n";
+                
+                $resql2 = $db->query($sql2);
+                if (! $resql2) dol_print_error($db);
+            }            
+            $i++;
+        }
+    }
+    else dol_print_error($db);
+
+    print "Correct order for year ".$year." and move them to current year ".$currentyear."\n";
+    $sql="select rowid from ".MAIN_DB_PREFIX."commande where date_commande between '".$year."-01-01' and '".$year."-12-31'";
+    $resql = $db->query($sql);
+    if ($resql)
+    {
+        $num = $db->num_rows($resql);
+        $i=0;
+        while ($i < $num)
+        {
+            $obj=$db->fetch_object($resql);
+            if ($obj)
+            {
+                print ".";
+    
+                $sql2="UPDATE ".MAIN_DB_PREFIX."commande set ";
+                $sql2.= "date_commande = DATE_ADD(date_commande,        INTERVAL ".$delta." YEAR),";
+                $sql2.= "date_valid    = DATE_ADD(date_valid,   INTERVAL ".$delta." YEAR),";
+                $sql2.= "date_cloture  = DATE_ADD(date_cloture, INTERVAL ".$delta." YEAR)";
+                $sql2.=" WHERE rowid = ".$obj->rowid;
+                //print $sql2."\n";
+    
+                $resql2 = $db->query($sql2);
+                if (! $resql2) dol_print_error($db);
+            }
+            $i++;
+        }
+    }
+    else dol_print_error($db);
+    
+    print "Correct invoice for year ".$year." and move them to current year ".$currentyear."\n";
+    $sql="select rowid from ".MAIN_DB_PREFIX."facture where datef between '".$year."-01-01' and '".$year."-12-31'";
+    $resql = $db->query($sql);
+    if ($resql)
+    {
+        $num = $db->num_rows($resql);
+        $i=0;
+        while ($i < $num)
+        {
+            $obj=$db->fetch_object($resql);
+            if ($obj)
+            {
+                print ".";
+    
+                $sql2="UPDATE ".MAIN_DB_PREFIX."facture set ";
+                $sql2.= "datef        = DATE_ADD(datef,        INTERVAL ".$delta." YEAR),";
+                $sql2.= "date_valid   = DATE_ADD(date_valid,   INTERVAL ".$delta." YEAR),";
+                $sql2.= "date_lim_reglement = DATE_ADD(date_lim_reglement,   INTERVAL ".$delta." YEAR)";
+                $sql2.=" WHERE rowid = ".$obj->rowid;
+                //print $sql2."\n";
+    
+                $resql2 = $db->query($sql2);
+                if (! $resql2) dol_print_error($db);
+            }
+            $i++;
+        }
+    }
+    else dol_print_error($db);
+    
+    $year++;
+}
+
+print "\n";
+
+exit(0);
diff --git a/htdocs/comm/propal/card.php b/htdocs/comm/propal/card.php
index 2f040b4610d..b09ce43af96 100644
--- a/htdocs/comm/propal/card.php
+++ b/htdocs/comm/propal/card.php
@@ -1939,7 +1939,7 @@ if ($action == 'create')
 		print '<tr>';
 		print '<td width="25%">';
 		print '<table class="nobordernopadding" width="100%"><tr><td>';
-		print fieldLabel('Rate','multicurrency_tx');
+		print fieldLabel('CurrencyRate','multicurrency_tx');
 		print '</td>';
 		if ($action != 'editmulticurrencyrate' && ! empty($object->brouillon))
 			print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmulticurrencyrate&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1) . '</a></td>';
diff --git a/htdocs/commande/card.php b/htdocs/commande/card.php
index 81f101359f8..bf1925ed061 100644
--- a/htdocs/commande/card.php
+++ b/htdocs/commande/card.php
@@ -2208,7 +2208,7 @@ if ($action == 'create' && $user->rights->commande->creer)
 			print '<tr>';
 			print '<td width="25%">';
 			print '<table class="nobordernopadding" width="100%"><tr><td>';
-			print fieldLabel('Rate','multicurrency_tx');
+			print fieldLabel('CurrencyRate','multicurrency_tx');
 			print '</td>';
 			if ($action != 'editmulticurrencyrate' && ! empty($object->brouillon))
 				print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmulticurrencyrate&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1) . '</a></td>';
diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php
index 5af6240fa9e..f9c02522a87 100644
--- a/htdocs/compta/facture.php
+++ b/htdocs/compta/facture.php
@@ -3559,7 +3559,7 @@ else if ($id > 0 || ! empty($ref))
 		print '<tr>';
 		print '<td>';
 		print '<table class="nobordernopadding" width="100%"><tr><td>';
-		print fieldLabel('Rate','multicurrency_tx');
+		print fieldLabel('CurrencyRate','multicurrency_tx');
 		print '</td>';
 		if ($action != 'editmulticurrencyrate' && ! empty($object->brouillon))
 			print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmulticurrencyrate&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1) . '</a></td>';
diff --git a/htdocs/fourn/commande/card.php b/htdocs/fourn/commande/card.php
index fa93be5020d..500b240fa16 100644
--- a/htdocs/fourn/commande/card.php
+++ b/htdocs/fourn/commande/card.php
@@ -1865,7 +1865,7 @@ elseif (! empty($object->id))
 		print '<tr>';
 		print '<td width="25%">';
 		print '<table class="nobordernopadding" width="100%"><tr><td>';
-		print fieldLabel('Rate','multicurrency_tx');
+		print fieldLabel('CurrencyRate','multicurrency_tx');
 		print '</td>';
 		if ($action != 'editmulticurrencyrate' && ! empty($object->brouillon))
 			print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmulticurrencyrate&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1) . '</a></td>';
diff --git a/htdocs/fourn/facture/card.php b/htdocs/fourn/facture/card.php
index 280664ea421..1e8e05d81e5 100644
--- a/htdocs/fourn/facture/card.php
+++ b/htdocs/fourn/facture/card.php
@@ -1901,7 +1901,7 @@ else
 			print '<tr>';
 			print '<td>';
 			print '<table class="nobordernopadding" width="100%"><tr><td>';
-			print fieldLabel('Rate','multicurrency_tx');
+			print fieldLabel('CurrencyRate','multicurrency_tx');
 			print '</td>';
 			if ($action != 'editmulticurrencyrate' && ! empty($object->brouillon))
 				print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmulticurrencyrate&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1) . '</a></td>';
diff --git a/htdocs/langs/en_US/main.lang b/htdocs/langs/en_US/main.lang
index e0b21059d4e..54b49de9ec3 100644
--- a/htdocs/langs/en_US/main.lang
+++ b/htdocs/langs/en_US/main.lang
@@ -278,6 +278,7 @@ MonthOfDay=Month of the day
 HourShort=H
 MinuteShort=mn
 Rate=Rate
+CurrencyRate=Currency conversion rate
 UseLocalTax=Include tax
 Bytes=Bytes
 KiloBytes=Kilobytes
diff --git a/htdocs/supplier_proposal/card.php b/htdocs/supplier_proposal/card.php
index f56daac29ec..481981bdabd 100644
--- a/htdocs/supplier_proposal/card.php
+++ b/htdocs/supplier_proposal/card.php
@@ -1488,7 +1488,7 @@ if ($action == 'create')
 	}
 
 	// Multicurrency
-	if ( empty($conf->multicurrency->enabled))
+	if (! empty($conf->multicurrency->enabled))
 	{
 		// Multicurrency code
 		print '<tr>';
@@ -1511,7 +1511,7 @@ if ($action == 'create')
 		print '<tr>';
 		print '<td>';
 		print '<table class="nobordernopadding" width="100%"><tr><td>';
-		print fieldLabel('Rate','multicurrency_tx');
+		print fieldLabel('CurrencyRate','multicurrency_tx');
 		print '</td>';
 		if ($action != 'editmulticurrencyrate' && ! empty($object->brouillon))
 			print '<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=editmulticurrencyrate&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1) . '</a></td>';
-- 
GitLab