From 630a063283c15a0113b68fae01273ed5bd2f8459 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Wed, 15 Jun 2016 18:21:58 +0200
Subject: [PATCH] FIX Can't create thirdparty or validate invoice if profid is
 mandatory and profid does not exists for other countries

Conflicts:
	htdocs/compta/facture.php
---
 htdocs/compta/facture.php | 29 +++++++++++++++--------------
 htdocs/societe/soc.php    | 23 +++++++++++++----------
 2 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php
index ccfd057ccde..e805d1ecac9 100644
--- a/htdocs/compta/facture.php
+++ b/htdocs/compta/facture.php
@@ -390,21 +390,22 @@ if (empty($reshook))
 
 		// Check parameters
 
-		// Check for mandatory prof id
-		for($i = 1; $i < 6; $i ++)
+		// Check for mandatory prof id (but only if country is than than ours)
+		if ($mysoc->country_id > 0 && $object->thirdparty->country_id == $mysoc->country_id)
 		{
-			$idprof_mandatory = 'SOCIETE_IDPROF' . ($i) . '_INVOICE_MANDATORY';
-			$idprof = 'idprof' . $i;
-			if (! $object->thirdparty->$idprof && ! empty($conf->global->$idprof_mandatory))
-			{
-				if (! $error)
-					$langs->load("errors");
-				$error ++;
-
-				setEventMessage($langs->trans('ErrorProdIdIsMandatory', $langs->transcountry('ProfId' . $i, $object->thirdparty->country_code)), 'errors');
-			}
-		}
-
+    		for ($i = 1; $i <= 6; $i++)
+    		{
+    			$idprof_mandatory = 'SOCIETE_IDPROF' . ($i) . '_INVOICE_MANDATORY';
+    			$idprof = 'idprof' . $i;
+    			if (! $object->thirdparty->$idprof && ! empty($conf->global->$idprof_mandatory))
+    			{
+    				if (! $error) $langs->load("errors");
+    				$error++;
+    				setEventMessages($langs->trans('ErrorProdIdIsMandatory', $langs->transcountry('ProfId' . $i, $object->thirdparty->country_code)), null, 'errors');
+    			}
+    		}
+		}
+		
 		$qualified_for_stock_change = 0;
 		if (empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
 			$qualified_for_stock_change = $object->hasProductsOrServices(2);
diff --git a/htdocs/societe/soc.php b/htdocs/societe/soc.php
index 76680649958..3a5ed38e2d5 100644
--- a/htdocs/societe/soc.php
+++ b/htdocs/societe/soc.php
@@ -363,7 +363,7 @@ if (empty($reshook))
             // Only for companies
 	        if (!($object->particulier || $private))
         	{
-	        	for ($i = 1; $i < 5; $i++)
+	        	for ($i = 1; $i <= 6; $i++)
 	        	{
 	        	    $slabel="idprof".$i;
 	    			$_POST[$slabel]=trim($_POST[$slabel]);
@@ -378,15 +378,18 @@ if (empty($reshook))
 						}
 					}
 
-					$idprof_mandatory ='SOCIETE_IDPROF'.($i).'_MANDATORY';
-
-					if (! $vallabel && ! empty($conf->global->$idprof_mandatory))
-					{
-						$langs->load("errors");
-						$error++;
-						$errors[] = $langs->trans("ErrorProdIdIsMandatory", $langs->transcountry('ProfId'.$i, $object->country_code));
-						$action = (($action=='add'||$action=='create')?'create':'edit');
-					}
+            		// Check for mandatory prof id (but only if country is than than ours)
+					if ($mysoc->country_id > 0 && $object->country_id == $mysoc->country_id)
+            		{
+    					$idprof_mandatory ='SOCIETE_IDPROF'.($i).'_MANDATORY';
+    					if (! $vallabel && ! empty($conf->global->$idprof_mandatory))
+    					{
+    						$langs->load("errors");
+    						$error++;
+    						$errors[] = $langs->trans("ErrorProdIdIsMandatory", $langs->transcountry('ProfId'.$i, $object->country_code));
+    						$action = (($action=='add'||$action=='create')?'create':'edit');
+    					}
+            		}
 	        	}
         	}
         }
-- 
GitLab