diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index dead4b4beb4..8da52e301e6 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -2358,7 +2358,7 @@ function dol_print_error_email($prefixcode) * @param string $field Field to use for new sorting * @param string $begin ("" by defaut) * @param string $moreparam Add more parameters on sort url links ("" by default) - * @param string $td Options of attribute td ("" by defaut) + * @param string $td Options of attribute td ("" by defaut, example: 'align="center"') * @param string $sortfield Current field used to sort * @param string $sortorder Current sort order * @return void diff --git a/htdocs/langs/en_US/banks.lang b/htdocs/langs/en_US/banks.lang index 3503ff11e62..e4de38e621e 100644 --- a/htdocs/langs/en_US/banks.lang +++ b/htdocs/langs/en_US/banks.lang @@ -154,3 +154,5 @@ DefaultRIB=Default BAN AllRIB=All BAN LabelRIB=BAN Label NoBANRecord=No BAN record +DeleteARib=Delete BAN record +ConfirmDeleteRib=Are you sure you want to delete this BAN record ? diff --git a/htdocs/langs/fr_FR/banks.lang b/htdocs/langs/fr_FR/banks.lang index 29440edd862..4d44e9619eb 100644 --- a/htdocs/langs/fr_FR/banks.lang +++ b/htdocs/langs/fr_FR/banks.lang @@ -154,3 +154,5 @@ DefaultRIB=RIB par défaut AllRIB=Tous les RIB LabelRIB=Nom du RIB NoBANRecord=Aucun RIB enregistré +DeleteARib=Supprimé RIB enregistré +ConfirmDeleteRib=Etes vous sur de vouloir supprimé ce RIB ? diff --git a/htdocs/societe/class/companybankaccount.class.php b/htdocs/societe/class/companybankaccount.class.php index 8b8e3b382c7..fe1726ab345 100644 --- a/htdocs/societe/class/companybankaccount.class.php +++ b/htdocs/societe/class/companybankaccount.class.php @@ -77,13 +77,23 @@ class CompanyBankAccount extends Account { $now=dol_now(); - $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_rib (fk_soc, datec) values ($this->socid, '".$this->db->idate($now)."')"; + // Correct default_rib to be sure to have always one default + $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_rib where fk_soc = ".$this->socid." AND default_rib = 1"; + $result = $this->db->query($sql); + if ($result) + { + $numrows=$this->db->num_rows($result); + if ($this->default_rib && $numrows > 0) $this->default_rib = 0; + if (empty($this->default_rib) && $numrows == 0) $this->default_rib = 1; + } + + $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_rib (fk_soc, datec)"; + $sql.= " VALUES (".$this->socid.", '".$this->db->idate($now)."')"; $resql=$this->db->query($sql); if ($resql) { if ($this->db->affected_rows($resql)) { - $this->default_rib = 1; $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."societe_rib"); return 1; } @@ -120,7 +130,7 @@ class CompanyBankAccount extends Account // return 0; // } - if (!$this->id) { + if (! $this->id) { $this->create(); } @@ -204,6 +214,30 @@ class CompanyBankAccount extends Account } } + /** + * Delete a rib from database + * + * @param User $user User deleting + * @return int <0 if KO, >0 if OK + */ + function delete($user) + { + global $conf; + + $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_rib"; + $sql.= " WHERE rowid = ".$this->id; + + dol_syslog(get_class($this)."::delete sql=".$sql); + $result = $this->db->query($sql); + if ($result) { + return 1; + } + else { + dol_print_error($this->db); + return -1; + } + } + /** * Return RIB * diff --git a/htdocs/societe/rib.php b/htdocs/societe/rib.php index b3432d105a8..819c3b6986c 100644 --- a/htdocs/societe/rib.php +++ b/htdocs/societe/rib.php @@ -31,11 +31,12 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php'; require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php'; $langs->load("companies"); +$langs->load("commercial"); $langs->load("banks"); $langs->load("bills"); // Security check -$socid = isset($_GET["socid"])?$_GET["socid"]:''; +$socid = GETPOST("socid"); if ($user->societe_id) $socid=$user->societe_id; $result = restrictedArea($user, 'societe','',''); @@ -43,17 +44,21 @@ $soc = new Societe($db); $soc->id = $_GET["socid"]; $soc->fetch($_GET["socid"]); +$id=GETPOST("id","int"); +$ribid=GETPOST("ribid","int"); +$action=GETPOST("action"); + /* * Actions */ -if ($_POST["action"] == 'update' && ! $_POST["cancel"]) +if ($action == 'update' && ! $_POST["cancel"]) { // Modification $account = new CompanyBankAccount($db); - $account->fetch($_POST["id"]); + $account->fetch($id); $account->socid = $soc->id; @@ -85,7 +90,7 @@ if ($_POST["action"] == 'update' && ! $_POST["cancel"]) } } -if ($_POST["action"] == 'add' && ! $_POST["cancel"]) +if ($action == 'add' && ! $_POST["cancel"]) { // Ajout $account = new CompanyBankAccount($db); @@ -106,7 +111,7 @@ if ($_POST["action"] == 'add' && ! $_POST["cancel"]) $account->proprio = $_POST["proprio"]; $account->owner_address = $_POST["owner_address"]; - $result = $account->update($user); + $result = $account->update($user); // TODO Use create and include update into create method if (! $result) { $message=$account->error; @@ -120,10 +125,10 @@ if ($_POST["action"] == 'add' && ! $_POST["cancel"]) } } -if ($_GET['action'] == 'setasdefault') +if ($action == 'setasdefault') { $account = new CompanyBankAccount($db); - $res = $account->setAsDefault($_GET['ribid']); + $res = $account->setAsDefault(GETPOST('ribid','int')); if ($res) { $url=DOL_URL_ROOT.'/societe/rib.php?socid='.$soc->id; header('Location: '.$url); @@ -133,10 +138,35 @@ if ($_GET['action'] == 'setasdefault') } } +if ($action == 'confirm_delete' && $_GET['confirm'] == 'yes') +{ + $account = new CompanyBankAccount($db); + if ($account->fetch($ribid?$ribid:$id)) + { + $result = $account->delete($user); + if ($result > 0) + { + $url = $_SERVER['PHP_SELF']."?socid=".$soc->id; + header('Location: '.$url); + exit; + } + else + { + $message = $account->error; + } + } + else + { + $message = $account->error; + } +} + /* * View */ +$form = new Form($db); + llxHeader(); $head=societe_prepare_head2($soc); @@ -144,10 +174,10 @@ $head=societe_prepare_head2($soc); dol_fiche_head($head, 'rib', $langs->trans("ThirdParty"),0,'company'); $account = new CompanyBankAccount($db); -if (!$_GET['id']) +if (! $id) $account->fetch(0,$soc->id); else - $account->fetch($_GET['id']); + $account->fetch($id); if (empty($account->socid)) $account->socid=$soc->id; @@ -158,10 +188,17 @@ if (empty($account->socid)) $account->socid=$soc->id; /* */ /* ************************************************************************** */ -if ($_GET["socid"] && $_GET["action"] != 'edit' && $_GET["action"] != "create") +if ($socid && $action != 'edit' && $action != "create") { + // Confirm delete third party + if ($action == 'delete') + { + print $form->formconfirm($_SERVER["PHP_SELF"]."?socid=".$soc->id."&ribid=".($ribid?$ribid:$id), $langs->trans("DeleteARib"), $langs->trans("ConfirmDeleteRib", $account->getRibLabel()), "confirm_delete", '', 0, 1); + } + print_titre($langs->trans("DefaultRIB")); - print '
| '.$langs->trans("LabelRIB").' | '; print ''.$account->label.' | '; print ''; - foreach ($rib_list as $rib) { + foreach ($rib_list as $rib) + { print " | ||
| '.$rib->label.' | '; print ''.$rib->bank.' | '; @@ -248,16 +286,26 @@ if ($_GET["socid"] && $_GET["action"] != 'edit' && $_GET["action"] != "create") print ''; if (!$rib->default_rib) { print ''; - print img_picto($langs->trans("Disabled"),'switch_off'); + print img_picto($langs->trans("Disabled"),'off'); print ''; } else { - print img_picto($langs->trans("Enabled"),'switch_on'); + print img_picto($langs->trans("Enabled"),'on'); } print ' | '; print ''; - print ''; - print img_picto($langs->trans("Modify"),'edit'); - print ''; + if ($user->rights->societe->creer) + { + print ''; + print img_picto($langs->trans("Modify"),'edit'); + print ''; + + print ' '; + + print ''; + print img_picto($langs->trans("Delete"),'delete'); + print ''; + } + print ' | '; print '|