From a258663175b08a237c621e7125efc7ad2c15758b Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 9 Jul 2016 14:58:51 +0200
Subject: [PATCH] Fix edition of status were loosing old percent. We show it to
 allow user to keep it if required.

---
 htdocs/core/class/html.formprojet.class.php   |  2 +-
 .../install/mysql/migration/3.9.0-4.0.0.sql   |  1 +
 htdocs/langs/en_US/main.lang                  |  1 +
 htdocs/projet/card.php                        | 21 ++++++++++++++-----
 4 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/htdocs/core/class/html.formprojet.class.php b/htdocs/core/class/html.formprojet.class.php
index d2ad0a6f1ab..0eca137f6d3 100644
--- a/htdocs/core/class/html.formprojet.class.php
+++ b/htdocs/core/class/html.formprojet.class.php
@@ -569,7 +569,7 @@ class FormProjets
 				{
 					$obj = $this->db->fetch_object($resql);
 
-					$sellist .='<option value="'.$obj->rowid.'" defaultpercent="'.$obj->percent.'"';
+					$sellist .='<option value="'.$obj->rowid.'" defaultpercent="'.$obj->percent.'" elemcode="'.$obj->code.'"';
 					if ($obj->rowid == $preselected) $sellist .= ' selected="selected"';
 					$sellist .= '>';
 					if ($useshortlabel)
diff --git a/htdocs/install/mysql/migration/3.9.0-4.0.0.sql b/htdocs/install/mysql/migration/3.9.0-4.0.0.sql
index 8fc8ecb5df3..f58dda0a74f 100644
--- a/htdocs/install/mysql/migration/3.9.0-4.0.0.sql
+++ b/htdocs/install/mysql/migration/3.9.0-4.0.0.sql
@@ -488,6 +488,7 @@ ALTER TABLE llx_import_model MODIFY COLUMN type varchar(50);
 
 UPDATE llx_projet set fk_opp_status = NULL where fk_opp_status = -1;
 UPDATE llx_c_lead_status set code = 'WON' where code = 'WIN';
+UPDATE llx_c_lead_status set percent = 100 where code = 'WON';
 
 
 CREATE TABLE llx_oauth_token (
diff --git a/htdocs/langs/en_US/main.lang b/htdocs/langs/en_US/main.lang
index b90d02f8f77..e0b21059d4e 100644
--- a/htdocs/langs/en_US/main.lang
+++ b/htdocs/langs/en_US/main.lang
@@ -84,6 +84,7 @@ SeeAbove=See above
 HomeArea=Home area
 LastConnexion=Last connection
 PreviousConnexion=Previous connection
+PreviousValue=Previous value
 ConnectedOnMultiCompany=Connected on environment
 ConnectedSince=Connected since
 AuthenticationMode=Authentification mode
diff --git a/htdocs/projet/card.php b/htdocs/projet/card.php
index d3895b0000e..62a5a8fd3f1 100644
--- a/htdocs/projet/card.php
+++ b/htdocs/projet/card.php
@@ -710,7 +710,9 @@ else
 
 		    // Opportunity probability
 		    print '<tr><td>'.$langs->trans("OpportunityProbability").'</td>';
-		    print '<td><input size="5" type="text" id="opp_percent" name="opp_percent" value="'.(isset($_POST['opp_percent'])?GETPOST('opp_percent'):(strcmp($object->opp_percent,'')?price($object->opp_percent,0,$langs,1,0):'')).'"> %</td>';
+		    print '<td><input size="5" type="text" id="opp_percent" name="opp_percent" value="'.(isset($_POST['opp_percent'])?GETPOST('opp_percent'):(strcmp($object->opp_percent,'')?price($object->opp_percent,0,$langs,1,0):'')).'"> %';
+            print '<span id="oldopppercent"></span>';
+		    print '</td>';
 		    print '</tr>';
 		    
 		    // Opportunity amount
@@ -837,14 +839,23 @@ else
     print '</form>';
 
     // Change probability from status
-    print '<script type="text/javascript" language="javascript">
+    if (! empty($conf->use_javascript_ajax))
+    {
+        print '<script type="text/javascript" language="javascript">
         jQuery(document).ready(function() {
         	function change_percent()
         	{
                 var element = jQuery("#opp_status option:selected");
                 var defaultpercent = element.attr("defaultpercent");
-                /*if (jQuery("#opp_percent_not_set").val() == "") */
-                jQuery("#opp_percent").val(defaultpercent);
+                var elemcode = element.attr("elemcode");
+                /* Change percent of default percent of new status is higher */
+                if (parseFloat(jQuery("#opp_percent").val()) != parseFloat(defaultpercent)
+                    )
+                {
+                    if (! jQuery("#oldopppercent").text()) jQuery("#oldopppercent").text(\' - '.dol_escape_js($langs->trans("PreviousValue")).': \'+jQuery("#opp_percent").val()+\' %\');
+                    jQuery("#opp_percent").val(defaultpercent);
+                    
+                }
         	}
         	/*init_myfunc();*/
         	jQuery("#opp_status").change(function() {
@@ -852,7 +863,7 @@ else
         	});
         });
         </script>';
-    
+    }    
     
     /*
      * Boutons actions
-- 
GitLab