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;
// Load translation files required by the page
$langs->loadLangs(array("bills", "accountancy"));
$langs->loadLangs(array("bills", "accountancy", "compta"));
$mesg = '';
$action = GETPOST('action', 'aZ09');
@ -41,7 +41,9 @@ $id = GETPOST('id', 'int');
$ref = GETPOST('ref', 'alpha');
$rowid = GETPOST('rowid', 'int');
$cancel = GETPOST('cancel', 'alpha');
$accountingaccount = GETPOST('accountingaccount', 'alpha');
$account_number = GETPOST('account_number', 'string');
$label = GETPOST('label', 'alpha');
// Security check
if ($user->socid > 0) accessforbidden();
@ -65,104 +67,118 @@ if (GETPOST('cancel', 'alpha'))
if ($action == 'add' && $user->rights->accounting->chartofaccount)
{
if (!$cancel) {
$sql = 'SELECT pcg_version FROM '.MAIN_DB_PREFIX.'accounting_system WHERE rowid='.$conf->global->CHARTOFACCOUNTS;
dol_syslog('accountancy/admin/card.php:: $sql='.$sql);
$result = $db->query($sql);
$obj = $db->fetch_object($result);
// Clean code
// To manage zero or not at the end of the accounting account
if ($conf->global->ACCOUNTING_MANAGE_ZERO == 1)
if (!$account_number)
{
$account_number = GETPOST('account_number', 'string');
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 {
$account_number = clean_account(GETPOST('account_number', 'string'));
}
$sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS;
if (GETPOST('account_parent', 'int') <= 0)
{
$account_parent = 0;
} else {
$account_parent = GETPOST('account_parent', 'int');
}
dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
$result = $db->query($sql);
$obj = $db->fetch_object($result);
$object->fk_pcg_version = $obj->pcg_version;
$object->pcg_type = GETPOST('pcg_type', 'alpha');
$object->account_number = $account_number;
$object->account_parent = $account_parent;
$object->account_category = GETPOST('account_category', 'alpha');
$object->label = GETPOST('label', 'alpha');
$object->labelshort = GETPOST('labelshort', 'alpha');
$object->active = 1;
// Clean code
$res = $object->create($user);
if ($res == - 3) {
$error = 1;
$action = "create";
setEventMessages($object->error, $object->errors, 'errors');
} elseif ($res == - 4) {
$error = 2;
$action = "create";
setEventMessages($object->error, $object->errors, 'errors');
} elseif ($res < 0)
{
$error++;
setEventMessages($object->error, $object->errors, 'errors');
$action = "create";
}
if (!$error)
{
setEventMessages("RecordCreatedSuccessfully", null, 'mesgs');
$urltogo = $backtopage ? $backtopage : dol_buildpath('/accountancy/admin/account.php', 1);
header("Location: ".$urltogo);
exit;
// To manage zero or not at the end of the accounting account
if ($conf->global->ACCOUNTING_MANAGE_ZERO == 1) {
$account_number = $account_number;
} else {
$account_number = clean_account($account_number);
}
if (GETPOST('account_parent', 'int') <= 0) {
$account_parent = 0;
} else {
$account_parent = GETPOST('account_parent', 'int');
}
$object->fk_pcg_version = $obj->pcg_version;
$object->pcg_type = GETPOST('pcg_type', 'alpha');
$object->account_number = $account_number;
$object->account_parent = $account_parent;
$object->account_category = GETPOST('account_category', 'alpha');
$object->label = $label;
$object->labelshort = GETPOST('labelshort', 'alpha');
$object->active = 1;
$res = $object->create($user);
if ($res == -3) {
$error = 1;
$action = "create";
setEventMessages($object->error, $object->errors, 'errors');
} elseif ($res == -4) {
$error = 2;
$action = "create";
setEventMessages($object->error, $object->errors, 'errors');
} elseif ($res < 0) {
$error++;
setEventMessages($object->error, $object->errors, 'errors');
$action = "create";
}
if (!$error) {
setEventMessages("RecordCreatedSuccessfully", null, 'mesgs');
$urltogo = $backtopage ? $backtopage : dol_buildpath('/accountancy/admin/account.php', 1);
header("Location: " . $urltogo);
exit;
}
}
}
} elseif ($action == 'edit' && $user->rights->accounting->chartofaccount) {
if (!$cancel) {
$result = $object->fetch($id);
$sql = 'SELECT pcg_version FROM '.MAIN_DB_PREFIX.'accounting_system WHERE rowid='.$conf->global->CHARTOFACCOUNTS;
dol_syslog('accountancy/admin/card.php:: $sql='.$sql);
$result2 = $db->query($sql);
$obj = $db->fetch_object($result2);
// Clean code
// To manage zero or not at the end of the accounting account
if ($conf->global->ACCOUNTING_MANAGE_ZERO == 1)
if (!$account_number)
{
$account_number = GETPOST('account_number', 'string');
} else {
$account_number = clean_account(GETPOST('account_number', 'string'));
}
if (GETPOST('account_parent', 'int') <= 0)
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountNumber")), null, 'errors');
$action = 'update';
} elseif (!$label)
{
$account_parent = 0;
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")), null, 'errors');
$action = 'update';
} else {
$account_parent = GETPOST('account_parent', 'int');
}
$result = $object->fetch($id);
$object->fk_pcg_version = $obj->pcg_version;
$object->pcg_type = GETPOST('pcg_type', 'alpha');
$object->account_number = $account_number;
$object->account_parent = $account_parent;
$object->account_category = GETPOST('account_category', 'alpha');
$object->label = GETPOST('label', 'alpha');
$object->labelshort = GETPOST('labelshort', 'alpha');
$sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS;
$result = $object->update($user);
dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
$result2 = $db->query($sql);
$obj = $db->fetch_object($result2);
if ($result > 0) {
$urltogo = $backtopage ? $backtopage : ($_SERVER["PHP_SELF"]."?id=".$id);
header("Location: ".$urltogo);
exit();
} else {
$mesg = $object->error;
// Clean code
// To manage zero or not at the end of the accounting account
if ($conf->global->ACCOUNTING_MANAGE_ZERO == 1) {
$account_number = $account_number;
} else {
$account_number = clean_account($account_number);
}
if (GETPOST('account_parent', 'int') <= 0) {
$account_parent = 0;
} else {
$account_parent = GETPOST('account_parent', 'int');
}
$object->fk_pcg_version = $obj->pcg_version;
$object->pcg_type = GETPOST('pcg_type', 'alpha');
$object->account_number = $account_number;
$object->account_parent = $account_parent;
$object->account_category = GETPOST('account_category', 'alpha');
$object->label = $label;
$object->labelshort = GETPOST('labelshort', 'alpha');
$result = $object->update($user);
if ($result > 0) {
$urltogo = $backtopage ? $backtopage : ($_SERVER["PHP_SELF"] . "?id=" . $id);
header("Location: " . $urltogo);
exit();
} else {
$mesg = $object->error;
}
}
} else {
$urltogo = $backtopage ? $backtopage : ($_SERVER["PHP_SELF"]."?id=".$id);
@ -222,7 +238,7 @@ if ($action == 'create') {
// Account number
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
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'];
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'])) {
$link = '<a href="' . DOL_URL_ROOT . '/accountancy/admin/card.php?action=create&token=' . newToken() . '&accountingaccount=' . length_accountg($line->numero_compte) . '">' . img_edit_add() . '</a>';
}