FIX: Accountancy - Add control on required fields when we create an account

This commit is contained in:
Alexandre SPANGARO 2021-01-07 06:59:40 +01:00
parent 74ba7e68da
commit 5606542220
2 changed files with 103 additions and 87 deletions

View File

@ -32,7 +32,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
$error = 0; $error = 0;
// Load translation files required by the page // Load translation files required by the page
$langs->loadLangs(array("bills", "accountancy")); $langs->loadLangs(array("bills", "accountancy", "compta"));
$mesg = ''; $mesg = '';
$action = GETPOST('action', 'aZ09'); $action = GETPOST('action', 'aZ09');
@ -41,7 +41,9 @@ $id = GETPOST('id', 'int');
$ref = GETPOST('ref', 'alpha'); $ref = GETPOST('ref', 'alpha');
$rowid = GETPOST('rowid', 'int'); $rowid = GETPOST('rowid', 'int');
$cancel = GETPOST('cancel', 'alpha'); $cancel = GETPOST('cancel', 'alpha');
$accountingaccount = GETPOST('accountingaccount', 'alpha');
$account_number = GETPOST('account_number', 'string');
$label = GETPOST('label', 'alpha');
// Security check // Security check
if ($user->socid > 0) accessforbidden(); if ($user->socid > 0) accessforbidden();
@ -65,6 +67,15 @@ if (GETPOST('cancel', 'alpha'))
if ($action == 'add' && $user->rights->accounting->chartofaccount) if ($action == 'add' && $user->rights->accounting->chartofaccount)
{ {
if (!$cancel) { if (!$cancel) {
if (!$account_number)
{
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountNumber")), null, 'errors');
$action = 'create';
} elseif (!$label)
{
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")), null, 'errors');
$action = 'create';
} else {
$sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS; $sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS;
dol_syslog('accountancy/admin/card.php:: $sql=' . $sql); dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
@ -74,15 +85,13 @@ if ($action == 'add' && $user->rights->accounting->chartofaccount)
// Clean code // Clean code
// To manage zero or not at the end of the accounting account // To manage zero or not at the end of the accounting account
if ($conf->global->ACCOUNTING_MANAGE_ZERO == 1) if ($conf->global->ACCOUNTING_MANAGE_ZERO == 1) {
{ $account_number = $account_number;
$account_number = GETPOST('account_number', 'string');
} else { } else {
$account_number = clean_account(GETPOST('account_number', 'string')); $account_number = clean_account($account_number);
} }
if (GETPOST('account_parent', 'int') <= 0) if (GETPOST('account_parent', 'int') <= 0) {
{
$account_parent = 0; $account_parent = 0;
} else { } else {
$account_parent = GETPOST('account_parent', 'int'); $account_parent = GETPOST('account_parent', 'int');
@ -93,7 +102,7 @@ if ($action == 'add' && $user->rights->accounting->chartofaccount)
$object->account_number = $account_number; $object->account_number = $account_number;
$object->account_parent = $account_parent; $object->account_parent = $account_parent;
$object->account_category = GETPOST('account_category', 'alpha'); $object->account_category = GETPOST('account_category', 'alpha');
$object->label = GETPOST('label', 'alpha'); $object->label = $label;
$object->labelshort = GETPOST('labelshort', 'alpha'); $object->labelshort = GETPOST('labelshort', 'alpha');
$object->active = 1; $object->active = 1;
@ -106,22 +115,30 @@ if ($action == 'add' && $user->rights->accounting->chartofaccount)
$error = 2; $error = 2;
$action = "create"; $action = "create";
setEventMessages($object->error, $object->errors, 'errors'); setEventMessages($object->error, $object->errors, 'errors');
} elseif ($res < 0) } elseif ($res < 0) {
{
$error++; $error++;
setEventMessages($object->error, $object->errors, 'errors'); setEventMessages($object->error, $object->errors, 'errors');
$action = "create"; $action = "create";
} }
if (!$error) if (!$error) {
{
setEventMessages("RecordCreatedSuccessfully", null, 'mesgs'); setEventMessages("RecordCreatedSuccessfully", null, 'mesgs');
$urltogo = $backtopage ? $backtopage : dol_buildpath('/accountancy/admin/account.php', 1); $urltogo = $backtopage ? $backtopage : dol_buildpath('/accountancy/admin/account.php', 1);
header("Location: " . $urltogo); header("Location: " . $urltogo);
exit; exit;
} }
} }
}
} elseif ($action == 'edit' && $user->rights->accounting->chartofaccount) { } elseif ($action == 'edit' && $user->rights->accounting->chartofaccount) {
if (!$cancel) { if (!$cancel) {
if (!$account_number)
{
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountNumber")), null, 'errors');
$action = 'update';
} elseif (!$label)
{
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")), null, 'errors');
$action = 'update';
} else {
$result = $object->fetch($id); $result = $object->fetch($id);
$sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS; $sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS;
@ -133,15 +150,13 @@ if ($action == 'add' && $user->rights->accounting->chartofaccount)
// Clean code // Clean code
// To manage zero or not at the end of the accounting account // To manage zero or not at the end of the accounting account
if ($conf->global->ACCOUNTING_MANAGE_ZERO == 1) if ($conf->global->ACCOUNTING_MANAGE_ZERO == 1) {
{ $account_number = $account_number;
$account_number = GETPOST('account_number', 'string');
} else { } else {
$account_number = clean_account(GETPOST('account_number', 'string')); $account_number = clean_account($account_number);
} }
if (GETPOST('account_parent', 'int') <= 0) if (GETPOST('account_parent', 'int') <= 0) {
{
$account_parent = 0; $account_parent = 0;
} else { } else {
$account_parent = GETPOST('account_parent', 'int'); $account_parent = GETPOST('account_parent', 'int');
@ -152,7 +167,7 @@ if ($action == 'add' && $user->rights->accounting->chartofaccount)
$object->account_number = $account_number; $object->account_number = $account_number;
$object->account_parent = $account_parent; $object->account_parent = $account_parent;
$object->account_category = GETPOST('account_category', 'alpha'); $object->account_category = GETPOST('account_category', 'alpha');
$object->label = GETPOST('label', 'alpha'); $object->label = $label;
$object->labelshort = GETPOST('labelshort', 'alpha'); $object->labelshort = GETPOST('labelshort', 'alpha');
$result = $object->update($user); $result = $object->update($user);
@ -164,6 +179,7 @@ if ($action == 'add' && $user->rights->accounting->chartofaccount)
} else { } else {
$mesg = $object->error; $mesg = $object->error;
} }
}
} else { } else {
$urltogo = $backtopage ? $backtopage : ($_SERVER["PHP_SELF"]."?id=".$id); $urltogo = $backtopage ? $backtopage : ($_SERVER["PHP_SELF"]."?id=".$id);
header("Location: ".$urltogo); header("Location: ".$urltogo);
@ -222,7 +238,7 @@ if ($action == 'create') {
// Account number // Account number
print '<tr><td class="titlefieldcreate"><span class="fieldrequired">'.$langs->trans("AccountNumber").'</span></td>'; print '<tr><td class="titlefieldcreate"><span class="fieldrequired">'.$langs->trans("AccountNumber").'</span></td>';
print '<td><input name="account_number" size="30" value="'.$accountingaccount.'"></td></tr>'; print '<td><input name="account_number" size="30" value="'.$account_number.'"></td></tr>';
// Label // Label
print '<tr><td><span class="fieldrequired">'.$langs->trans("Label").'</span></td>'; print '<tr><td><span class="fieldrequired">'.$langs->trans("Label").'</span></td>';

View File

@ -323,7 +323,7 @@ if ($action != 'export_csv')
$root_account_number = $tmparrayforrootaccount['account_number']; $root_account_number = $tmparrayforrootaccount['account_number'];
if (empty($accountingaccountstatic->label) && $accountingaccountstatic->id > 0) { if (empty($accountingaccountstatic->label) && $accountingaccountstatic->id > 0) {
$link = '<a href="' . DOL_URL_ROOT . '/accountancy/admin/card.php?action=update&token=' . newToken() . '&id=' . $accountingaccountstatic->id . '">' . img_edit() . '</a>'; $link = '<a class="editfielda reposition" href="' . DOL_URL_ROOT . '/accountancy/admin/card.php?action=update&token=' . newToken() . '&id=' . $accountingaccountstatic->id . '">' . img_edit() . '</a>';
} elseif (empty($tmparrayforrootaccount['label'])) { } elseif (empty($tmparrayforrootaccount['label'])) {
$link = '<a href="' . DOL_URL_ROOT . '/accountancy/admin/card.php?action=create&token=' . newToken() . '&accountingaccount=' . length_accountg($line->numero_compte) . '">' . img_edit_add() . '</a>'; $link = '<a href="' . DOL_URL_ROOT . '/accountancy/admin/card.php?action=create&token=' . newToken() . '&accountingaccount=' . length_accountg($line->numero_compte) . '">' . img_edit_add() . '</a>';
} }