NEW Add a protection so we can't journalize non balanced transactions

This commit is contained in:
Laurent Destailleur 2017-11-27 17:12:48 +01:00
parent 8954d52e32
commit 533a82187a
10 changed files with 119 additions and 57 deletions

View File

@ -32,25 +32,18 @@ require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
class BookKeeping extends CommonObject
{
/**
*
* @var string Error code (or message)
* @deprecated
*
* @see Accountingbookkeeping::errors
*/
public $error;
/**
*
* @var string[] Error codes (or messages)
*/
public $errors = array ();
/**
*
* @var string Id to identify managed objects
*/
public $element = 'accountingbookkeeping';
/**
*
* @var string Name of table without prefix where object is stored
*/
public $table_element = 'accounting_bookkeeping';
@ -58,13 +51,11 @@ class BookKeeping extends CommonObject
public $entity = 1;
/**
*
* @var BookKeepingLine[] Lines
*/
public $lines = array ();
/**
*
* @var int ID
*/
public $id;

View File

@ -304,7 +304,7 @@ if ($result) {
);
//if ($user->rights->mymodule->supprimer) $arrayofmassactions['predelete']=$langs->trans("Delete");
//if (in_array($massaction, array('presend','predelete'))) $arrayofmassactions=array();
$massactionbutton=$form->selectMassAction('0', $arrayofmassactions, 1);
$massactionbutton=$form->selectMassAction('ventil', $arrayofmassactions, 1);
print '<form action="' . $_SERVER["PHP_SELF"] . '" method="post">' . "\n";
print '<input type="hidden" name="action" value="ventil">';

View File

@ -268,7 +268,7 @@ if ($result) {
);
//if ($user->rights->mymodule->supprimer) $arrayofmassactions['predelete']=$langs->trans("Delete");
//if (in_array($massaction, array('presend','predelete'))) $arrayofmassactions=array();
$massactionbutton=$form->selectMassAction('0', $arrayofmassactions, 1);
$massactionbutton=$form->selectMassAction('ventil', $arrayofmassactions, 1);
print '<form action="' . $_SERVER["PHP_SELF"] . '" method="post">' . "\n";

View File

@ -402,6 +402,9 @@ if (! $error && $action == 'writebookkeeping') {
$errorforline = 0;
$totalcredit = 0;
$totaldebit = 0;
$db->begin();
// Introduce a protection. Total of tabtp must be total of tabbq
@ -460,6 +463,9 @@ if (! $error && $action == 'writebookkeeping') {
$bookkeeping->subledger_account = '';
}
$totaldebit += $bookkeeping->debit;
$totalcredit += $bookkeeping->credit;
$result = $bookkeeping->create($user);
if ($result < 0) {
if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
@ -566,6 +572,9 @@ if (! $error && $action == 'writebookkeeping') {
}
}
$totaldebit += $bookkeeping->debit;
$totalcredit += $bookkeeping->credit;
$result = $bookkeeping->create($user);
if ($result < 0) {
if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
@ -585,6 +594,12 @@ if (! $error && $action == 'writebookkeeping') {
}
}
if ($totaldebit != $totalcredit)
{
$errorforline++;
setEventMessages('Try to insert a non balanced transaction in book. Canceled. Surely a bug.', null, 'errors');
}
if (! $errorforline)
{
$db->commit();

View File

@ -183,6 +183,9 @@ if ($action == 'writebookkeeping') {
{
$errorforline = 0;
$totalcredit = 0;
$totaldebit = 0;
$db->begin();
// Thirdparty
@ -209,6 +212,9 @@ if ($action == 'writebookkeeping') {
$bookkeeping->journal_label = $journal_label;
$bookkeeping->fk_user_author = $user->id;
$totaldebit += $bookkeeping->debit;
$totalcredit += $bookkeeping->credit;
$result = $bookkeeping->create($user);
if ($result < 0) {
if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
@ -255,6 +261,9 @@ if ($action == 'writebookkeeping') {
$bookkeeping->journal_label = $journal_label;
$bookkeeping->fk_user_author = $user->id;
$totaldebit += $bookkeeping->debit;
$totalcredit += $bookkeeping->credit;
$result = $bookkeeping->create($user);
if ($result < 0) {
if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
@ -307,6 +316,9 @@ if ($action == 'writebookkeeping') {
$bookkeeping->journal_label = $journal_label;
$bookkeeping->fk_user_author = $user->id;
$totaldebit += $bookkeeping->debit;
$totalcredit += $bookkeeping->credit;
$result = $bookkeeping->create($user);
if ($result < 0) {
if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
@ -327,6 +339,12 @@ if ($action == 'writebookkeeping') {
}
}
if ($totaldebit != $totalcredit)
{
$errorforline++;
setEventMessages('Try to insert a non balanced transaction in book. Canceled. Surely a bug.', null, 'errors');
}
if (! $errorforline)
{
$db->commit();

View File

@ -55,10 +55,10 @@ $now = dol_now();
// Security check
if ($user->societe_id > 0)
accessforbidden();
$hookmanager->initHooks(array('purchasesjournal'));
$parameters=array();
/*
* Actions
*/
@ -204,8 +204,12 @@ if ($action == 'writebookkeeping') {
$error = 0;
foreach ($tabfac as $key => $val) { // Loop on each invoice
$errorforline = 0;
$totalcredit = 0;
$totaldebit = 0;
$db->begin();
$companystatic = new Societe($db);
@ -251,6 +255,9 @@ if ($action == 'writebookkeeping') {
$bookkeeping->journal_label = $journal_label;
$bookkeeping->fk_user_author = $user->id;
$totaldebit += $bookkeeping->debit;
$totalcredit += $bookkeeping->credit;
$result = $bookkeeping->create($user);
if ($result < 0) {
if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
@ -299,6 +306,9 @@ if ($action == 'writebookkeeping') {
$bookkeeping->journal_label = $journal_label;
$bookkeeping->fk_user_author = $user->id;
$totaldebit += $bookkeeping->debit;
$totalcredit += $bookkeeping->credit;
$result = $bookkeeping->create($user);
if ($result < 0) {
if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
@ -331,46 +341,55 @@ if ($action == 'writebookkeeping') {
if ($numtax == 2) $arrayofvat = $tablocaltax2;
foreach ( $arrayofvat[$key] as $k => $mt ) {
if ($mt) {
$bookkeeping = new BookKeeping($db);
$bookkeeping->doc_date = $val["date"];
$bookkeeping->date_lim_reglement = $val["datereg"];
$bookkeeping->doc_ref = $val["refsologest"];
$bookkeeping->date_create = $now;
$bookkeeping->doc_type = 'supplier_invoice';
$bookkeeping->fk_doc = $key;
$bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add
$bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
$bookkeeping->subledger_account = '';
$bookkeeping->subledger_label = '';
$bookkeeping->numero_compte = $k;
$bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->refsupplier . ' - ' . $langs->trans("VAT").' '.join(', ',$def_tva[$key][$k]) .' %' . ($numtax?' - Localtax '.$numtax:'');
$bookkeeping->montant = $mt;
$bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
$bookkeeping->debit = ($mt > 0) ? $mt : 0;
$bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
$bookkeeping->code_journal = $journal;
$bookkeeping->journal_label = $journal_label;
$bookkeeping->fk_user_author = $user->id;
if ($mt) {
$bookkeeping = new BookKeeping($db);
$bookkeeping->doc_date = $val["date"];
$bookkeeping->date_lim_reglement = $val["datereg"];
$bookkeeping->doc_ref = $val["refsologest"];
$bookkeeping->date_create = $now;
$bookkeeping->doc_type = 'supplier_invoice';
$bookkeeping->fk_doc = $key;
$bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add
$bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
$bookkeeping->subledger_account = '';
$bookkeeping->subledger_label = '';
$bookkeeping->numero_compte = $k;
$bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->refsupplier . ' - ' . $langs->trans("VAT").' '.join(', ',$def_tva[$key][$k]) .' %' . ($numtax?' - Localtax '.$numtax:'');
$bookkeeping->montant = $mt;
$bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
$bookkeeping->debit = ($mt > 0) ? $mt : 0;
$bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
$bookkeeping->code_journal = $journal;
$bookkeeping->journal_label = $journal_label;
$bookkeeping->fk_user_author = $user->id;
$result = $bookkeeping->create($user);
if ($result < 0) {
if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
{
$error++;
$errorforline++;
//setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
}
else
{
$error++;
$errorforline++;
setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
$totaldebit += $bookkeeping->debit;
$totalcredit += $bookkeeping->credit;
$result = $bookkeeping->create($user);
if ($result < 0) {
if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
{
$error++;
$errorforline++;
//setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
}
else
{
$error++;
$errorforline++;
setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
}
}
}
}
}
}
if ($totaldebit != $totalcredit)
{
$errorforline++;
setEventMessages('Try to insert a non balanced transaction in book. Canceled. Surely a bug.', null, 'errors');
}
if (! $errorforline)
@ -686,7 +705,7 @@ if (empty($action) || $action == 'view') {
$accountoshow = length_accountg($k);
if (empty($accountoshow) || $accountoshow == 'NotDefined')
{
print '<span class="error">'.$langs->trans("VATAccountNotDefined").'</span>';
print '<span class="error">'.$langs->trans("VATAccountNotDefined").' ('.$langs->trans("Purchase").')'.'</span>';
}
else print $accountoshow;
print "</td>";

View File

@ -218,6 +218,9 @@ if ($action == 'writebookkeeping') {
$errorforline = 0;
$totalcredit = 0;
$totaldebit = 0;
$db->begin();
$companystatic = new Societe($db);
@ -260,6 +263,9 @@ if ($action == 'writebookkeeping') {
$bookkeeping->journal_label = $journal_label;
$bookkeeping->fk_user_author = $user->id;
$totaldebit += $bookkeeping->debit;
$totalcredit += $bookkeeping->credit;
$result = $bookkeeping->create($user);
if ($result < 0) {
if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
@ -308,6 +314,9 @@ if ($action == 'writebookkeeping') {
$bookkeeping->journal_label = $journal_label;
$bookkeeping->fk_user_author = $user->id;
$totaldebit += $bookkeeping->debit;
$totalcredit += $bookkeeping->credit;
$result = $bookkeeping->create($user);
if ($result < 0) {
if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
@ -343,7 +352,7 @@ if ($action == 'writebookkeeping') {
if ($mt) {
$bookkeeping = new BookKeeping($db);
$bookkeeping->doc_date = $val["date"];
$bookkeeping->date_lim_reglement = $val["datereg"];
$bookkeeping->date_lim_reglement = $val["datereg"];
$bookkeeping->doc_ref = $val["ref"];
$bookkeeping->date_create = $now;
$bookkeeping->doc_type = 'customer_invoice';
@ -362,6 +371,9 @@ if ($action == 'writebookkeeping') {
$bookkeeping->journal_label = $journal_label;
$bookkeeping->fk_user_author = $user->id;
$totaldebit += $bookkeeping->debit;
$totalcredit += $bookkeeping->credit;
$result = $bookkeeping->create($user);
if ($result < 0) {
if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
@ -382,6 +394,12 @@ if ($action == 'writebookkeeping') {
}
}
if ($totaldebit != $totalcredit)
{
$errorforline++;
setEventMessages('Try to insert a non balanced transaction in book. Canceled. Surely a bug.', null, 'errors');
}
if (! $errorforline)
{
$db->commit();
@ -691,7 +709,7 @@ if (empty($action) || $action == 'view') {
$accountoshow = length_accountg($k);
if (empty($accountoshow) || $accountoshow == 'NotDefined')
{
print '<span class="error">'.$langs->trans("VATAccountNotDefined").'</span>';
print '<span class="error">'.$langs->trans("VATAccountNotDefined").' ('.$langs->trans("Sale").')'.'</span>';
}
else print $accountoshow;
print "</td>";

View File

@ -300,7 +300,7 @@ if ($result) {
);
//if ($user->rights->mymodule->supprimer) $arrayofmassactions['predelete']=$langs->trans("Delete");
//if (in_array($massaction, array('presend','predelete'))) $arrayofmassactions=array();
$massactionbutton=$form->selectMassAction('0', $arrayofmassactions, 1);
$massactionbutton=$form->selectMassAction('ventil', $arrayofmassactions, 1);
print '<form action="' . $_SERVER["PHP_SELF"] . '" method="post">' . "\n";
print '<input type="hidden" name="action" value="ventil">';

View File

@ -571,8 +571,9 @@ class Form
$ret.=$hookmanager->resPrint;
$ret.='</select>';
// Warning: if you set submit button to disabled, post using 'Enter' will no more work.
$ret.='<input type="submit" name="confirmmassaction" class="button'.(empty($conf->use_javascript_ajax)?'':' hideobject').' massaction massactionconfirmed" value="'.dol_escape_htmltag($langs->trans("Confirm")).'">';
// Warning: if you set submit button to disabled, post using 'Enter' will no more work if there is no another input submit. So we add a hidden button
$ret.='<input type="submit" name="confirmmassactioninvisible" style="display: none" tabindex="-1">'; // Hidden button BEFORE so it is the one used when we submit with ENTER.
$ret.='<input type="submit" disabled name="confirmmassaction" class="button'.(empty($conf->use_javascript_ajax)?'':' hideobject').' massaction massactionconfirmed" value="'.dol_escape_htmltag($langs->trans("Confirm")).'">';
$ret.='</div>';
if (! empty($conf->use_javascript_ajax))
@ -590,8 +591,8 @@ class Form
if (atleastoneselected || '.$alwaysvisible.')
{
jQuery(".massaction").show();
'.($selected ? 'if (atleastoneselected) jQuery(".massactionselect").val("'.$selected.'");' : '').'
'.($selected ? 'if (! atleastoneselected) jQuery(".massactionselect").val("0");' : '').'
'.($selected ? 'if (atleastoneselected) { jQuery(".massactionselect").val("'.$selected.'"); jQuery(".massactionconfirmed").prop(\'disabled\', false); }' : '').'
'.($selected ? 'if (! atleastoneselected) { jQuery(".massactionselect").val("0"); jQuery(".massactionconfirmed").prop(\'disabled\', true); } ' : '').'
}
else
{
@ -613,7 +614,7 @@ class Form
}
$( this ).closest("form").attr("action", urlform);
console.log("we select a mass action "+massaction+" - "+urlform);
/* Warning: if you set submit button to disabled, post using Enter will no more work
/* Warning: if you set submit button to disabled, post using Enter will no more work if there is no other button */
if ($(this).val() != \'0\')
{
jQuery(".massactionconfirmed").prop(\'disabled\', false);
@ -622,7 +623,6 @@ class Form
{
jQuery(".massactionconfirmed").prop(\'disabled\', true);
}
*/
});
});
</script>

View File

@ -192,6 +192,7 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocalt
if ($type == 1) $apply_tax = true;
break;
}
if ($uselocaltax1_rate && $apply_tax) {
$result[14] = price2num(($tot_sans_remise_wt * (1 + ( $localtax1_rate / 100))) - $tot_sans_remise_wt, 'MT');
$localtaxes[0] += $result[14];