From 533a82187a5ac820c488c33198138792e8f9b772 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 27 Nov 2017 17:12:48 +0100 Subject: [PATCH] NEW Add a protection so we can't journalize non balanced transactions --- .../accountancy/class/bookkeeping.class.php | 9 -- htdocs/accountancy/customer/list.php | 2 +- htdocs/accountancy/expensereport/list.php | 2 +- htdocs/accountancy/journal/bankjournal.php | 15 +++ .../journal/expensereportsjournal.php | 18 ++++ .../accountancy/journal/purchasesjournal.php | 93 +++++++++++-------- htdocs/accountancy/journal/sellsjournal.php | 22 ++++- htdocs/accountancy/supplier/list.php | 2 +- htdocs/core/class/html.form.class.php | 12 +-- htdocs/core/lib/price.lib.php | 1 + 10 files changed, 119 insertions(+), 57 deletions(-) diff --git a/htdocs/accountancy/class/bookkeeping.class.php b/htdocs/accountancy/class/bookkeeping.class.php index bb6f6949cbb..3e600755330 100644 --- a/htdocs/accountancy/class/bookkeeping.class.php +++ b/htdocs/accountancy/class/bookkeeping.class.php @@ -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; diff --git a/htdocs/accountancy/customer/list.php b/htdocs/accountancy/customer/list.php index 220c0f7cd8a..193dd88b81f 100644 --- a/htdocs/accountancy/customer/list.php +++ b/htdocs/accountancy/customer/list.php @@ -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 '
' . "\n"; print ''; diff --git a/htdocs/accountancy/expensereport/list.php b/htdocs/accountancy/expensereport/list.php index b17e5acc9e2..7a35fe0f725 100644 --- a/htdocs/accountancy/expensereport/list.php +++ b/htdocs/accountancy/expensereport/list.php @@ -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 '' . "\n"; diff --git a/htdocs/accountancy/journal/bankjournal.php b/htdocs/accountancy/journal/bankjournal.php index bc7dfa3d7bf..8fb68b24665 100644 --- a/htdocs/accountancy/journal/bankjournal.php +++ b/htdocs/accountancy/journal/bankjournal.php @@ -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(); diff --git a/htdocs/accountancy/journal/expensereportsjournal.php b/htdocs/accountancy/journal/expensereportsjournal.php index c73286a8bdd..524115e9d67 100644 --- a/htdocs/accountancy/journal/expensereportsjournal.php +++ b/htdocs/accountancy/journal/expensereportsjournal.php @@ -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(); diff --git a/htdocs/accountancy/journal/purchasesjournal.php b/htdocs/accountancy/journal/purchasesjournal.php index ac9955608c1..cd490dceb2b 100644 --- a/htdocs/accountancy/journal/purchasesjournal.php +++ b/htdocs/accountancy/journal/purchasesjournal.php @@ -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 ''.$langs->trans("VATAccountNotDefined").''; + print ''.$langs->trans("VATAccountNotDefined").' ('.$langs->trans("Purchase").')'.''; } else print $accountoshow; print ""; diff --git a/htdocs/accountancy/journal/sellsjournal.php b/htdocs/accountancy/journal/sellsjournal.php index 42fbb7ff681..4f61a246c89 100644 --- a/htdocs/accountancy/journal/sellsjournal.php +++ b/htdocs/accountancy/journal/sellsjournal.php @@ -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 ''.$langs->trans("VATAccountNotDefined").''; + print ''.$langs->trans("VATAccountNotDefined").' ('.$langs->trans("Sale").')'.''; } else print $accountoshow; print ""; diff --git a/htdocs/accountancy/supplier/list.php b/htdocs/accountancy/supplier/list.php index 611a19f3653..c95cb3f2c3d 100644 --- a/htdocs/accountancy/supplier/list.php +++ b/htdocs/accountancy/supplier/list.php @@ -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 '' . "\n"; print ''; diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 7671fecfd87..1e24300a384 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -571,8 +571,9 @@ class Form $ret.=$hookmanager->resPrint; $ret.=''; - // Warning: if you set submit button to disabled, post using 'Enter' will no more work. - $ret.=''; + // 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.=''; // Hidden button BEFORE so it is the one used when we submit with ENTER. + $ret.=''; $ret.=''; 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); } - */ }); }); diff --git a/htdocs/core/lib/price.lib.php b/htdocs/core/lib/price.lib.php index 0cb1f755450..ba84876c93c 100644 --- a/htdocs/core/lib/price.lib.php +++ b/htdocs/core/lib/price.lib.php @@ -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];