From 887b3d49ea848d722077111f3f401750d3d7feb3 Mon Sep 17 00:00:00 2001 From: Maxime Nicolas <71704827+Maxime-UwU@users.noreply.github.com> Date: Thu, 16 Mar 2023 12:15:19 +0100 Subject: [PATCH 1/2] update interbank transfer --- htdocs/compta/bank/transfer.php | 326 +++++++++++++++++++------------- 1 file changed, 197 insertions(+), 129 deletions(-) diff --git a/htdocs/compta/bank/transfer.php b/htdocs/compta/bank/transfer.php index d896c3e6069..7a6ba0dc037 100644 --- a/htdocs/compta/bank/transfer.php +++ b/htdocs/compta/bank/transfer.php @@ -6,6 +6,8 @@ * Copyright (C) 2015 Jean-François Ferry * Copyright (C) 2015 Marcos García * Copyright (C) 2018-2021 Frédéric France + * Copyright (C) 2023 Maxime Nicolas + * Copyright (C) 2023 Benjamin GREMBI * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,7 +37,6 @@ require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; // Load translation files required by the page $langs->loadLangs(array('banks', 'categories', 'multicurrency')); - $socid = 0; if ($user->socid > 0) { $socid = $user->socid; @@ -45,7 +46,6 @@ if (!$user->rights->banque->transfer) { } $action = GETPOST('action', 'aZ09'); -$error = 0; $hookmanager->initHooks(array('banktransfer')); @@ -60,114 +60,141 @@ if ($reshook < 0) { setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); } if ($action == 'add') { - $langs->load("errors"); + $langs->load('errors'); + $i = 1; - $dateo = dol_mktime(12, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int')); - $label = GETPOST('label', 'alpha'); - $amount = price2num(GETPOST('amount', 'alpha'), 'MT', 2); - $amountto = price2num(GETPOST('amountto', 'alpha'), 'MT', 2); + while($i < 20){ - if (!$label) { - $error++; - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Description")), null, 'errors'); + $dateo[$i] = dol_mktime(12, 0, 0, GETPOST($i.'_month', 'int'), GETPOST($i.'_day', 'int'), GETPOST($i.'_year', 'int')); + $label[$i] = GETPOST($i.'_label', 'alpha'); + $amount[$i] = intval(price2num(GETPOST($i.'_amount', 'alpha'), 'MT', 2)); + $amountto[$i] = price2num(GETPOST($i.'_amountto', 'alpha'), 'MT', 2); + $accountfrom[$i] = intval(GETPOST($i.'_account_from', 'int')); + $accountto[$i] = intval(GETPOST($i.'_account_to', 'int')); + $type[$i] = GETPOST($i.'_type', 'int'); + $errori[$i] = 0; + + $tabnum[$i] = 0; + if (!empty($label[$i]) || !empty($type[$i]) || !($amount[$i] <= 0) || !($accountfrom[$i] < 0) || !($accountto[$i] < 0)) { + $tabnum[$i] = 1; + } + $i++; } - if (!$amount) { - $error++; - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")), null, 'errors'); - } - if (!GETPOST('account_from', 'int')) { - $error++; - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("TransferFrom")), null, 'errors'); - } - if (!GETPOST('account_to', 'int')) { - $error++; - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("TransferTo")), null, 'errors'); - } - if (!$error) { - require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; - $accountfrom = new Account($db); - $accountfrom->fetch(GETPOST('account_from', 'int')); - - $accountto = new Account($db); - $accountto->fetch(GETPOST('account_to', 'int')); - - if ($accountto->currency_code == $accountfrom->currency_code) { - $amountto = $amount; - } else { - if (!$amountto) { - $error++; - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AmountTo")), null, 'errors'); + $n = 1; + while($n < 20){ + if ($tabnum[$n] === 1) { + if ($accountfrom[$n] < 0) { + $errori[$n]++; + setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' .$langs->transnoentities("TransferFrom")), null, 'errors'); } - } - if ($amountto < 0) { - $error++; - setEventMessages($langs->trans("AmountMustBePositive"), null, 'errors'); - } - - if ($accountto->id == $accountfrom->id) { - $error++; - setEventMessages($langs->trans("ErrorFromToAccountsMustDiffers"), null, 'errors'); - } - - if (empty($error)) { - $db->begin(); - - $bank_line_id_from = 0; - $bank_line_id_to = 0; - $result = 0; - - // By default, electronic transfert from bank to bank - $typefrom = 'PRE'; - $typeto = 'VIR'; - if ($accountto->courant == Account::TYPE_CASH || $accountfrom->courant == Account::TYPE_CASH) { - // This is transfer of change - $typefrom = 'LIQ'; - $typeto = 'LIQ'; + if ($accountto[$n] < 0) { + $errori[$n]++; + setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' .$langs->transnoentities("TransferTo")), null, 'errors'); + } + if (!$type[$n]){ + $errori[$n]++; + setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' .$langs->transnoentities("Type")), null, 'errors'); + } + if (!$dateo[$n]){ + $errori[$n]++; + setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' .$langs->transnoentities("Date")), null, 'errors'); } - if (!$error) { - $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, price2num(-1 * $amount), '', '', $user); + if (!($label[$n])) { + $errori[$n]++; + setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' . $langs->transnoentities("Description")), null, 'errors'); } - if (!($bank_line_id_from > 0)) { - $error++; - } - if (!$error) { - $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, $amountto, '', '', $user); - } - if (!($bank_line_id_to > 0)) { - $error++; + if (!($amount[$n])) { + $errori[$n]++; + setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' .$langs->transnoentities("Amount")), null, 'errors'); } - if (!$error) { - $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert'); - } - if (!($result > 0)) { - $error++; - } - if (!$error) { - $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert'); - } - if (!($result > 0)) { - $error++; + if (!$errori[$n]) { + require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; + + $accountfrom = new Account($db); + $accountfrom->fetch(GETPOST($n.'_account_from', 'int')); + + $accountto = new Account($db); + $accountto->fetch(GETPOST($n.'_account_to', 'int')); + + if ($accountto->currency_code == $accountfrom->currency_code) { + $amountto[$n] = $amount[$n]; + } else { + if (!$amountto[$n]) { + $error[$n]++; + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AmountTo")).' #'.$n, null, 'errors'); + } + } + if ($amountto[$n] < 0) { + $errori[$n]++; + setEventMessages($langs->trans("AmountMustBePositive").' #'.$n, null, 'errors'); + } + + if ($accountto->id == $accountfrom->id) { + $errori[$n]++; + setEventMessages($langs->trans("ErrorFromToAccountsMustDiffers").' #'.$n, null, 'errors'); + } } - if (!$error) { - $mesgs = $langs->trans("TransferFromToDone", '{s1}', '{s2}', $amount, $langs->transnoentitiesnoconv("Currency".$conf->currency)); - $mesgs = str_replace('{s1}', ''.$accountfrom->label.'', $mesgs); - $mesgs = str_replace('{s2}', ''.$accountto->label.'', $mesgs); - setEventMessages($mesgs, null, 'mesgs'); - $db->commit(); - } else { - setEventMessages($accountfrom->error.' '.$accountto->error, null, 'errors'); - $db->rollback(); + if ($errori[$n] == 0) { + $db->begin(); + + $bank_line_id_from = 0; + $bank_line_id_to = 0; + $result = 0; + + // By default, electronic transfert from bank to bank + $typefrom = $type[$n]; + $typeto = $type[$n]; + if ($accountto->courant == Account::TYPE_CASH || $accountfrom->courant == Account::TYPE_CASH) { + // This is transfer of change + $typefrom = 'LIQ'; + $typeto = 'LIQ'; + } + + if (!$errori[$n]) { + $bank_line_id_from = $accountfrom->addline($dateo[$n], $typefrom, $label[$n], price2num(-1 * $amount[$n]), '', '', $user); + } + if (!($bank_line_id_from > 0)) { + $errori[$n]++; + } + if (!$errori[$n]) { + $bank_line_id_to = $accountto->addline($dateo[$n], $typeto, $label[$n], $amountto[$n], '', '', $user); + } + if (!($bank_line_id_to > 0)) { + $errori[$n]++; + } + + if (!$errori[$n]) { + $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert'); + } + if (!($result > 0)) { + $errori++; + } + if (!$errori[$n]) { + $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert'); + } + if (!($result > 0)) { + $errori[$n]++; + } + if (!$errori[$n]) { + $mesgs = $langs->trans("TransferFromToDone", '{s1}', '{s2}', $amount[$n], $langs->transnoentitiesnoconv("Currency".$conf->currency)); + $mesgs = str_replace('{s1}', ''.$accountfrom->label.'', $mesgs); + $mesgs = str_replace('{s2}', ''.$accountto->label.'', $mesgs); + setEventMessages($mesgs, null, 'mesgs'); + $db->commit(); + } else { + setEventMessages($accountfrom->error.' '.$accountto->error, null, 'errors'); + $db->rollback(); + } } - } + } + $n++; } } - - /* * View */ @@ -179,6 +206,7 @@ llxHeader('', $title, $help_url); print ' + '; // End of page llxFooter(); + $db->close(); From 6dc5e3a709bc8e796d9a500ca2ea4c98c8313df2 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 16 Mar 2023 11:30:43 +0000 Subject: [PATCH 2/2] Fixing style errors. --- htdocs/compta/bank/transfer.php | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/htdocs/compta/bank/transfer.php b/htdocs/compta/bank/transfer.php index 7a6ba0dc037..9f5d4facf9e 100644 --- a/htdocs/compta/bank/transfer.php +++ b/htdocs/compta/bank/transfer.php @@ -63,8 +63,7 @@ if ($action == 'add') { $langs->load('errors'); $i = 1; - while($i < 20){ - + while ($i < 20) { $dateo[$i] = dol_mktime(12, 0, 0, GETPOST($i.'_month', 'int'), GETPOST($i.'_day', 'int'), GETPOST($i.'_year', 'int')); $label[$i] = GETPOST($i.'_label', 'alpha'); $amount[$i] = intval(price2num(GETPOST($i.'_amount', 'alpha'), 'MT', 2)); @@ -77,12 +76,12 @@ if ($action == 'add') { $tabnum[$i] = 0; if (!empty($label[$i]) || !empty($type[$i]) || !($amount[$i] <= 0) || !($accountfrom[$i] < 0) || !($accountto[$i] < 0)) { $tabnum[$i] = 1; - } + } $i++; } $n = 1; - while($n < 20){ + while ($n < 20) { if ($tabnum[$n] === 1) { if ($accountfrom[$n] < 0) { $errori[$n]++; @@ -92,11 +91,11 @@ if ($action == 'add') { $errori[$n]++; setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' .$langs->transnoentities("TransferTo")), null, 'errors'); } - if (!$type[$n]){ + if (!$type[$n]) { $errori[$n]++; setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' .$langs->transnoentities("Type")), null, 'errors'); } - if (!$dateo[$n]){ + if (!$dateo[$n]) { $errori[$n]++; setEventMessages($langs->trans("ErrorFieldRequired", '#'.$n. ' ' .$langs->transnoentities("Date")), null, 'errors'); } @@ -112,13 +111,13 @@ if ($action == 'add') { if (!$errori[$n]) { require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; - + $accountfrom = new Account($db); $accountfrom->fetch(GETPOST($n.'_account_from', 'int')); - + $accountto = new Account($db); $accountto->fetch(GETPOST($n.'_account_to', 'int')); - + if ($accountto->currency_code == $accountfrom->currency_code) { $amountto[$n] = $amount[$n]; } else { @@ -131,7 +130,7 @@ if ($action == 'add') { $errori[$n]++; setEventMessages($langs->trans("AmountMustBePositive").' #'.$n, null, 'errors'); } - + if ($accountto->id == $accountfrom->id) { $errori[$n]++; setEventMessages($langs->trans("ErrorFromToAccountsMustDiffers").' #'.$n, null, 'errors'); @@ -140,11 +139,11 @@ if ($action == 'add') { if ($errori[$n] == 0) { $db->begin(); - + $bank_line_id_from = 0; $bank_line_id_to = 0; $result = 0; - + // By default, electronic transfert from bank to bank $typefrom = $type[$n]; $typeto = $type[$n]; @@ -153,7 +152,7 @@ if ($action == 'add') { $typefrom = 'LIQ'; $typeto = 'LIQ'; } - + if (!$errori[$n]) { $bank_line_id_from = $accountfrom->addline($dateo[$n], $typefrom, $label[$n], price2num(-1 * $amount[$n]), '', '', $user); } @@ -166,7 +165,7 @@ if ($action == 'add') { if (!($bank_line_id_to > 0)) { $errori[$n]++; } - + if (!$errori[$n]) { $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert'); } @@ -190,7 +189,7 @@ if ($action == 'add') { $db->rollback(); } } - } + } $n++; } } @@ -297,14 +296,13 @@ print ''; print ''; -for($i = 1 ; $i< 20; $i++){ +for ($i = 1 ; $i< 20; $i++) { $label = ''; $amount = ''; if ($errori[$i]) { $label = GETPOST($i.'_label', 'alpha'); $amount = GETPOST($i.'_amount', 'alpha'); - } if ($i == 1) {