From 1e4916bd534ced558e3f51498c0345bda36e3cf2 Mon Sep 17 00:00:00 2001
From: florian HENRY <florian.henry@atm-consulting.fr>
Date: Thu, 30 Jun 2016 16:01:53 +0200
Subject: [PATCH] better way to manamge IBAN/BIC Mandatory

---
 htdocs/compta/bank/class/account.class.php | 49 ++++++++++++++++++++
 htdocs/societe/rib.php                     | 54 ++++++++++++----------
 2 files changed, 79 insertions(+), 24 deletions(-)

diff --git a/htdocs/compta/bank/class/account.class.php b/htdocs/compta/bank/class/account.class.php
index d8f65c08302..0413692beac 100644
--- a/htdocs/compta/bank/class/account.class.php
+++ b/htdocs/compta/bank/class/account.class.php
@@ -1297,6 +1297,55 @@ class Account extends CommonObject
         return 0;
     }
 
+    /**
+     * Return 1 is IBAN is need for UE country
+     *
+     * @return		int        1 yes / 0 No
+     */
+    function needIBAN()
+    {
+    	$country_code=$this->getCountryCode();
+
+    	$country_code_in_EEC=array(
+    			'AT',	// Austria
+    			'BE',	// Belgium
+    			'BG',	// Bulgaria
+    			'CY',	// Cyprus
+    			'CZ',	// Czech republic
+    			'DE',	// Germany
+    			'DK',	// Danemark
+    			'EE',	// Estonia
+    			'ES',	// Spain
+    			'FI',	// Finland
+    			'FR',	// France
+    			'GB',	// United Kingdom
+    			'GR',	// Greece
+    			'HR',   // Croatia
+    			'NL',	// Holland
+    			'HU',	// Hungary
+    			'IE',	// Ireland
+    			'IM',	// Isle of Man - Included in UK
+    			'IT',	// Italy
+    			'LT',	// Lithuania
+    			'LU',	// Luxembourg
+    			'LV',	// Latvia
+    			'MC',	// Monaco - Included in France
+    			'MT',	// Malta
+    			//'NO',	// Norway
+    			'PL',	// Poland
+    			'PT',	// Portugal
+    			'RO',	// Romania
+    			'SE',	// Sweden
+    			'SK',	// Slovakia
+    			'SI',	// Slovenia
+    			'UK',	// United Kingdom
+    			//'CH',	// Switzerland - No. Swizerland in not in EEC
+    	);
+
+    	if (in_array($country_code,$country_code_in_EEC)) return 1; // France, Spain, Gabon, ...
+    	return 0;
+    }
+
     /**
      *	Load miscellaneous information for tab "Info"
      *
diff --git a/htdocs/societe/rib.php b/htdocs/societe/rib.php
index d820058d83b..62e6860adc4 100644
--- a/htdocs/societe/rib.php
+++ b/htdocs/societe/rib.php
@@ -72,17 +72,20 @@ if ($action == 'update' && ! $_POST["cancel"])
 		$action='update';
 		$error++;
 	}
-	if (! GETPOST('iban'))
+	if ($account->needIBAN() == 1)
 	{
-		setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("IBAN")), null, 'errors');
-		$action='update';
-		$error++;
-	}
-	if (! GETPOST('bic'))
-	{
-		setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BIC")), null, 'errors');
-		$action='update';
-		$error++;
+		if (! GETPOST('iban'))
+		{
+			setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("IBAN")), null, 'errors');
+			$action='update';
+			$error++;
+		}
+		if (! GETPOST('bic'))
+		{
+			setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BIC")), null, 'errors');
+			$action='update';
+			$error++;
+		}
 	}
 
     $account->fetch($id);
@@ -141,17 +144,20 @@ if ($action == 'add' && ! $_POST["cancel"])
 		$action='create';
 		$error++;
 	}
-	if (! GETPOST('iban'))
+	if ($account->needIBAN() == 1)
 	{
-		setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("IBAN")), null, 'errors');
-		$action='create';
-		$error++;
-	}
-	if (! GETPOST('bic'))
-	{
-		setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BIC")), null, 'errors');
-		$action='create';
-		$error++;
+		if (! GETPOST('iban'))
+		{
+			setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("IBAN")), null, 'errors');
+			$action='create';
+			$error++;
+		}
+		if (! GETPOST('bic'))
+		{
+			setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BIC")), null, 'errors');
+			$action='create';
+			$error++;
+		}
 	}
 
 	if (! $error)
@@ -489,12 +495,12 @@ if ($socid && $action == 'edit' && $user->rights->societe->creer)
 			$name = 'iban';
 			$size = 30;
 			$content = $account->iban;
-			$require=true;
+			if ($account->needIBAN()) $require=true;
 		} elseif ($val == 'BIC') {
 			$name = 'bic';
 			$size = 12;
 			$content = $account->bic;
-			$require=true;
+			if ($account->needIBAN()) $require=true;
 		}
 
 		print '<tr><td'.($require?' class="fieldrequired" ':'').'>'.$langs->trans($val).'</td>';
@@ -568,10 +574,10 @@ if ($socid && $action == 'create' && $user->rights->societe->creer)
     print '<td><input size="30" type="text" name="bank" value="'.GETPOST('bank').'"></td></tr>';
 
     // IBAN
-    print '<tr><td valign="top" class="fieldrequired">'.$langs->trans("IBAN").'</td>';
+    print '<tr><td valign="top" '.($account->needIBAN()?' class="fieldrequired" ':'').'>'.$langs->trans("IBAN").'</td>';
     print '<td colspan="4"><input size="30" type="text" name="iban" value="'.GETPOST('iban').'"></td></tr>';
 
-    print '<tr><td valign="top" class="fieldrequired">'.$langs->trans("BIC").'</td>';
+    print '<tr><td valign="top" '.($account->needIBAN()?' class="fieldrequired" ':'').'>'.$langs->trans("BIC").'</td>';
     print '<td colspan="4"><input size="12" type="text" name="bic" value="'.GETPOST('bic').'"></td></tr>';
 
     // BBAN
-- 
GitLab