Skip to content

Commit

Permalink
Fix journalization protection
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Feb 12, 2018
1 parent 734bd4d commit 09dfd5e
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 129 deletions.
2 changes: 1 addition & 1 deletion htdocs/accountancy/bookkeeping/list.php
Expand Up @@ -730,7 +730,7 @@
print "</table>";
print '</div>';

// TODO Replace this with mass action
// TODO Replace this with mass delete action
print '<div class="tabsAction tabsActionNoBottom">' . "\n";
print '<a class="butActionDelete" name="button_delmvt" href="'.$_SERVER["PHP_SELF"].'?action=delbookkeepingyear'.($param?'&'.$param:'').'">' . $langs->trans("DelBookKeeping") . '</a>';
print '</div>';
Expand Down
4 changes: 2 additions & 2 deletions htdocs/accountancy/customer/lines.php
Expand Up @@ -163,7 +163,7 @@
/*
* Customer Invoice lines
*/
$sql = "SELECT f.rowid, f.facnumber, f.type, f.datef, f.ref_client,";
$sql = "SELECT f.rowid as facid, f.facnumber, f.type, f.datef, f.ref_client,";
$sql .= " fd.rowid, fd.description, fd.product_type, fd.total_ht, fd.total_tva, fd.tva_tx, fd.vat_src_code, fd.total_ttc,";
$sql .= " s.rowid as socid, s.nom as name, s.code_compta, s.code_client,";
$sql .= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.accountancy_code_sell, aa.rowid as fk_compte, aa.account_number, aa.label as label_compte,";
Expand Down Expand Up @@ -333,7 +333,7 @@
$codecompta = length_accountg($objp->account_number) . ' - ' . $objp->label_compte;

$facture_static->ref = $objp->facnumber;
$facture_static->id = $objp->rowid;
$facture_static->id = $objp->facid;

$product_static->ref = $objp->product_ref;
$product_static->id = $objp->product_id;
Expand Down
2 changes: 1 addition & 1 deletion htdocs/accountancy/journal/bankjournal.php
Expand Up @@ -634,7 +634,7 @@
}
}

if ($totaldebit != $totalcredit)
if (price2num($totaldebit) != price2num($totalcredit))
{
$error++;
$errorforline++;
Expand Down
3 changes: 2 additions & 1 deletion htdocs/accountancy/journal/expensereportsjournal.php
Expand Up @@ -344,7 +344,8 @@
}
}

if ($totaldebit != $totalcredit)
// Protection against a bug on line before
if (price2num($totaldebit) != price2num($totalcredit))
{
$error++;
$errorforline++;
Expand Down
164 changes: 94 additions & 70 deletions htdocs/accountancy/journal/purchasesjournal.php
Expand Up @@ -222,6 +222,8 @@
$companystatic = new Societe($db);
$invoicestatic = new FactureFournisseur($db);

$errorforinvoice = array();

foreach ($tabfac as $key => $val) { // Loop on each invoice

$errorforline = 0;
Expand Down Expand Up @@ -257,17 +259,18 @@
if ($alreadydispatched) $replacedinvoice = 2;
}

// If not already into bookkeeping, we won't add it, if yes, add the counterpart ???.
// If not already into bookkeeping, we won't add it. If yes, do nothing (should not happen because creating replacement not possible if invoice is accounted)
if ($replacedinvoice == 1)
{
$db->rollback();
continue;
}

// Thirdparty
if (! $errorforline)
{
foreach ( $tabttc[$key] as $k => $mt ) {
if ($mt) {
//if ($mt) {
$bookkeeping = new BookKeeping($db);
$bookkeeping->doc_date = $val["date"];
$bookkeeping->date_lim_reglement = $val["datereg"];
Expand Down Expand Up @@ -298,24 +301,26 @@
{
$error++;
$errorforline++;
$errorforinvoice[$key]=1;
//setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
}
else
{
$error++;
$errorforline++;
$errorforinvoice[$key]=1;
setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
}
}
}
//}
}
}

// Product / Service
if (! $errorforline)
{
foreach ( $tabht[$key] as $k => $mt ) {
if ($mt) {
//if ($mt) {
// get compte id and label
$accountingaccount = new AccountingAccount($db);
if ($accountingaccount->fetch(null, $k, true)) {
Expand Down Expand Up @@ -349,17 +354,19 @@
{
$error++;
$errorforline++;
$errorforinvoice[$key]=1;
//setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
}
else
{
$error++;
$errorforline++;
$errorforinvoice[$key]=1;
setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
}
}
}
}
//}
}
}

Expand Down Expand Up @@ -406,12 +413,14 @@
{
$error++;
$errorforline++;
$errorforinvoice[$key]=1;
//setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
}
else
{
$error++;
$errorforline++;
$errorforinvoice[$key]=1;
setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
}
}
Expand Down Expand Up @@ -456,26 +465,35 @@
{
$error++;
$errorforline++;
$errorforinvoice[$key]=1;
//setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
}
else
{
$error++;
$errorforline++;
$errorforinvoice[$key]=1;
setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
}
}
}
}
}

if ($totaldebit != $totalcredit)
// Protection against a bug on line before
if (price2num($totaldebit) != price2num($totalcredit))
{
$error++;
$errorforline++;
$errorforinvoice[$key]=1;
setEventMessages('Try to insert a non balanced transaction in book for '.$invoicestatic->ref.'. Canceled. Surely a bug.', null, 'errors');
}

// Check totaldebit is also same than total of invoice. If not, some record are not yet ready to be journalized




if (! $errorforline)
{
$db->commit();
Expand Down Expand Up @@ -567,34 +585,36 @@
if ($alreadydispatched) $replacedinvoice = 2;
}

// If not already into bookkeeping, we won't add it, if yes, add the counterpart ???.
// If not already into bookkeeping, we won't add it. If yes, do nothing (should not happen because creating replacement not possible if invoice is accounted)
if ($replacedinvoice == 1)
{
continue;
}

// Third party
foreach ( $tabttc[$key] as $k => $mt ) {
print '"' . $key . '"' . $sep;
print '"' . $date . '"' . $sep;
print '"' . $val["refsologest"] . '"' . $sep;
print '"' . utf8_decode ( dol_trunc($companystatic->name, 32) ). '"' . $sep;
print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
print '"' . $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER . '"' . $sep;
print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
print '"' . $langs->trans("Code_tiers") . '"' . $sep;
print '"' . utf8_decode ( dol_trunc($companystatic->name, 16) ) . ' - ' . $val["refsuppliersologest"] . ' - ' . $langs->trans("Code_tiers") . '"' . $sep;
print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep;
print '"' . ($mt >= 0 ? price($mt) : '') . '"'. $sep;
print '"' . $journal . '"' ;
print "\n";
//if ($mt) {
print '"' . $key . '"' . $sep;
print '"' . $date . '"' . $sep;
print '"' . $val["refsologest"] . '"' . $sep;
print '"' . utf8_decode ( dol_trunc($companystatic->name, 32) ). '"' . $sep;
print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
print '"' . $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER . '"' . $sep;
print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
print '"' . $langs->trans("Code_tiers") . '"' . $sep;
print '"' . utf8_decode ( dol_trunc($companystatic->name, 16) ) . ' - ' . $val["refsuppliersologest"] . ' - ' . $langs->trans("Code_tiers") . '"' . $sep;
print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep;
print '"' . ($mt >= 0 ? price($mt) : '') . '"'. $sep;
print '"' . $journal . '"' ;
print "\n";
//}
}

// Product / Service
foreach ( $tabht[$key] as $k => $mt ) {
$accountingaccount = new AccountingAccount($db);
$accountingaccount->fetch(null, $k, true);
if ($mt) {
//if ($mt) {
print '"' . $key . '"' . $sep;
print '"' . $date . '"' . $sep;
print '"' . $val["refsologest"] . '"' . $sep;
Expand All @@ -608,7 +628,7 @@
print '"' . ($mt < 0 ? price(- $mt) : '') . '"'. $sep;
print '"' . $journal . '"' ;
print "\n";
}
//}
}

// VAT
Expand Down Expand Up @@ -787,45 +807,48 @@ function writebookkeeping() {
print '<td align="right"></td>';
print '<td align="right"></td>';
print "</tr>";

continue;
}

// Third party
foreach ( $tabttc[$key] as $k => $mt ) {
print '<tr class="oddeven">';
print "<td><!-- Thirdparty --></td>";
print "<td>" . $date . "</td>";
print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
// Account
print "<td>";
$accountoshow = length_accounta($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER);
if (empty($accountoshow) || $accountoshow == 'NotDefined')
{
print '<span class="error">'.$langs->trans("MainAccountForCustomersNotDefined").'</span>';
}
else print $accountoshow;
print '</td>';
// Subledger account
print "<td>";
$accountoshow = length_accounta($k);
if (empty($accountoshow) || $accountoshow == 'NotDefined')
{
print '<span class="error">'.$langs->trans("ThirdpartyAccountNotDefined").'</span>';
}
else print $accountoshow;
print '</td>';
print "<td>" . $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("SubledgerAccount") . "</td>";
print '<td align="right">' . ($mt < 0 ? - price(- $mt) : '') . "</td>";
print '<td align="right">' . ($mt >= 0 ? price($mt) : '') . "</td>";
print "</tr>";
//if ($mt) {
print '<tr class="oddeven">';
print "<td><!-- Thirdparty --></td>";
print "<td>" . $date . "</td>";
print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
// Account
print "<td>";
$accountoshow = length_accounta($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER);
if (empty($accountoshow) || $accountoshow == 'NotDefined')
{
print '<span class="error">'.$langs->trans("MainAccountForCustomersNotDefined").'</span>';
}
else print $accountoshow;
print '</td>';
// Subledger account
print "<td>";
$accountoshow = length_accounta($k);
if (empty($accountoshow) || $accountoshow == 'NotDefined')
{
print '<span class="error">'.$langs->trans("ThirdpartyAccountNotDefined").'</span>';
}
else print $accountoshow;
print '</td>';
print "<td>" . $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("SubledgerAccount") . "</td>";
print '<td align="right">' . ($mt < 0 ? - price(- $mt) : '') . "</td>";
print '<td align="right">' . ($mt >= 0 ? price($mt) : '') . "</td>";
print "</tr>";
//}
}

// Product / Service
foreach ( $tabht[$key] as $k => $mt ) {
$accountingaccount = new AccountingAccount($db);
$accountingaccount->fetch(null, $k, true);

if ($mt) {
//if ($mt) {
print '<tr class="oddeven">';
print "<td><!-- Product --></td>";
print "<td>" . $date . "</td>";
Expand All @@ -848,7 +871,7 @@ function writebookkeeping() {
print '<td align="right">' . ($mt >= 0 ? price($mt) : '') . "</td>";
print '<td align="right">' . ($mt < 0 ? price(- $mt) : '') . "</td>";
print "</tr>";
}
//}
}

// VAT
Expand Down Expand Up @@ -890,27 +913,28 @@ function writebookkeeping() {
if (is_array($tabother[$key]))
{
foreach ( $tabother[$key] as $k => $mt ) {

print '<tr class="oddeven">';
print "<td><!-- VAT counterpart NPR --></td>";
print "<td>" . $date . "</td>";
print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
// Account
print "<td>";
$accountoshow = length_accountg($k);
if (empty($accountoshow) || $accountoshow == 'NotDefined')
{
print '<span class="error">'.$langs->trans("VATAccountNotDefined").' ('.$langs->trans("NPR counterpart").'). Set ACCOUNTING_COUNTERPART_VAT_NPR to the subvention account'.'</span>';
if ($mt) {
print '<tr class="oddeven">';
print "<td><!-- VAT counterpart NPR --></td>";
print "<td>" . $date . "</td>";
print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
// Account
print "<td>";
$accountoshow = length_accountg($k);
if (empty($accountoshow) || $accountoshow == 'NotDefined')
{
print '<span class="error">'.$langs->trans("VATAccountNotDefined").' ('.$langs->trans("NPR counterpart").'). Set ACCOUNTING_COUNTERPART_VAT_NPR to the subvention account'.'</span>';
}
else print $accountoshow;
print '</td>';
// Subledger account
print "<td>";
print '</td>';
print "<td>" . $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("VAT") . " NPR (counterpart)</td>";
print '<td align="right">' . ($mt < 0 ? - price(- $mt) : '') . "</td>";
print '<td align="right">' . ($mt >= 0 ? price($mt) : '') . "</td>";
print "</tr>";
}
else print $accountoshow;
print '</td>';
// Subledger account
print "<td>";
print '</td>';
print "<td>" . $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("VAT") . " NPR (counterpart)</td>";
print '<td align="right">' . ($mt < 0 ? - price(- $mt) : '') . "</td>";
print '<td align="right">' . ($mt >= 0 ? price($mt) : '') . "</td>";
print "</tr>";
}
}
}
Expand Down

0 comments on commit 09dfd5e

Please sign in to comment.