From 8b3faf46db34bd9f225dcbd508e6a4c632eeb27b Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Tue, 13 Jan 2015 15:38:59 +0100
Subject: [PATCH] Add PMP into selection of buy price.

---
 htdocs/core/tpl/objectline_create.tpl.php | 64 +++++++++++++++++------
 htdocs/fourn/ajax/getSupplierPrices.php   |  7 ++-
 2 files changed, 54 insertions(+), 17 deletions(-)

diff --git a/htdocs/core/tpl/objectline_create.tpl.php b/htdocs/core/tpl/objectline_create.tpl.php
index 6037f6c77c9..a3cac080ab5 100644
--- a/htdocs/core/tpl/objectline_create.tpl.php
+++ b/htdocs/core/tpl/objectline_create.tpl.php
@@ -448,41 +448,73 @@ jQuery(document).ready(function() {
 		setforpredef();
 		jQuery('#trlinefordates').show();
 	});
+
+	/* Sur changemenr de produit, on recharge la liste des prix fournisseur */
 	$("#idprod, #idprodfournprice").change(function()
 	{
 		setforpredef();
 		jQuery('#trlinefordates').show();
 
-		<?php if (! empty($usemargins) && $user->rights->margins->creer) { ?>
+		<?php
+		if (! empty($usemargins) && $user->rights->margins->creer)
+		{
+			$langs->load('stocks');
+			?>
 
 		/* Code for margin */
   		$("#fournprice_predef options").remove();
 		$("#fournprice_predef").hide();
 		$("#buying_price").val("").show();
+		/* Call post to load content of combo list fournprice_predef */
   		$.post('<?php echo DOL_URL_ROOT; ?>/fourn/ajax/getSupplierPrices.php', { 'idprod': $(this).val() }, function(data) {
 	    	if (data && data.length > 0)
 	    	{
     	  		var options = '';
+    	  		var defaultkey = '';
+    	  		var defaultprice = '';
 	      		var i = 0;
 	      		$(data).each(function() {
-	        		i++;
-	        		options += '<option value="'+this.id+'" price="'+this.price+'"';
-	        		if (i == 1) {
-	          			options += ' selected';
-	          			$("#buying_price").val(this.price);
-	        		}
-	        		options += '>'+this.label+'</option>';
+	      			if (this.id != 'pmpprice')
+		      		{
+		        		i++;
+		        		options += '<option value="'+this.id+'" price="'+this.price+'"';
+		        		if (this.price > 0 && i == 1) { defaultkey = this.id; defaultprice = this.price; }
+		        		options += '>'+this.label+'</option>';
+		      		}
+	      			if (this.id == 'pmpprice')
+	      			{
+		      			if (this.price > 0 && 1 == 1) { defaultkey = this.id; defaultprice = this.price; }
+	    	      		options += '<option value="'+this.id+'" price="'+this.price+'">'+this.label+'</option>';
+	      			}
 	      		});
-	      		options += '<option value=""><?php echo $langs->trans("InputPrice"); ?></option>';
-	      		$("#buying_price").hide();
+	      		options += '<option value="inputprice" price="'+defaultprice+'"><?php echo $langs->trans("InputPrice"); ?></option>';
+
+				/* alert(defaultkey+' '+defaultprice); */
 	      		$("#fournprice_predef").html(options).show();
+	      		$("#fournprice_predef").val(defaultkey);
+
+	      		/* At loading, no product are yet selected, so we hide field of buying_price */
+	      		$("#buying_price").hide();
+
+	      		/* Define default price at loading */
+	      		var defaultprice = $("#fournprice_predef").find('option:selected').attr("price");
+	      		$("#buying_price").val(defaultprice);
+
 	      		$("#fournprice_predef").change(function() {
-	        		var selval = $(this).find('option:selected').attr("price");
-	        		if (selval)
-	          			$("#buying_price").val(selval).hide();
-	        		else
-	          			$('#buying_price').show();
-	      		});
+	      			/* Hide field buying_price according to choice into list (if 'inputprice' or not) */
+					var linevalue=$(this).find('option:selected').val();
+	        		var pricevalue = $(this).find('option:selected').attr("price");
+	        		if (linevalue != 'inputprice' && linevalue != 'pmpprice') {
+	          			$("#buying_price").val(pricevalue).hide();	/* We set value then hide field */
+	        		}
+	        		if (linevalue == 'inputprice') {
+		          		$('#buying_price').show();
+	        		}
+	        		if (linevalue == 'pmpprice') {
+	        			$("#buying_price").val(pricevalue);
+		          		$('#buying_price').hide();
+	        		}
+				});
 	    	}
 	  	},
 	  	'json');
diff --git a/htdocs/fourn/ajax/getSupplierPrices.php b/htdocs/fourn/ajax/getSupplierPrices.php
index cdff85ccde0..907e34f8d35 100755
--- a/htdocs/fourn/ajax/getSupplierPrices.php
+++ b/htdocs/fourn/ajax/getSupplierPrices.php
@@ -17,7 +17,7 @@
 
 /**
  *	\file       /htdocs/fourn/ajax/getSupplierPrices.php
- *	\brief      File to return Ajax response on get supplier prices
+ *	\brief      File to return an Ajax response to get a supplier prices
  */
 
 if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal
@@ -119,6 +119,11 @@ if (! empty($idprod))
 			$db->free($result);
 		}
 	}
+
+	// Add price for pmp
+	$price=123;
+	$prices[] = array("id" => 'pmpprice', "price" => $price, "label" => $langs->trans("PMPValueShort").': '.price($price,0,$langs,0,0,-1,$conf->currency), "title" => $langs->trans("PMPValueShort").': '.price($price,0,$langs,0,0,-1,$conf->currency));
+
 }
 
 echo json_encode($prices);
-- 
GitLab