diff --git a/htdocs/accountancy/journal/purchasesjournal.php b/htdocs/accountancy/journal/purchasesjournal.php index c8b0f297f26..a07fbd3a96d 100644 --- a/htdocs/accountancy/journal/purchasesjournal.php +++ b/htdocs/accountancy/journal/purchasesjournal.php @@ -132,6 +132,7 @@ if ($result) { $tablocaltax1 = array (); $tablocaltax2 = array (); $tabcompany = array (); + $tabother = array(); $num = $db->num_rows($result); @@ -158,6 +159,7 @@ if ($result) { $compta_tva = (! empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva); $compta_localtax1 = (! empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva); $compta_localtax2 = (! empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva); + $compta_counterpart_tva_npr = (! empty($conf->global->ACCOUNTING_COUNTERPART_VAT_NPR)) ? $conf->global->ACCOUNTING_COUNTERPART_VAT_NPR : 'NotDefined'; // Define array to display all VAT rates that use this accounting account $compta_tva if (price2num($obj->tva_tx) || ! empty($obj->vat_src_code)) @@ -165,6 +167,9 @@ if ($result) { $def_tva[$obj->rowid][$compta_tva][vatrate($obj->tva_tx).($obj->vat_src_code?' ('.$obj->vat_src_code.')':'')]=(vatrate($obj->tva_tx).($obj->vat_src_code?' ('.$obj->vat_src_code.')':'')); } + $line = new SupplierInvoiceLine($db); + $line->fetch($obj->fdid); + $tabfac[$obj->rowid]["date"] = $db->jdate($obj->df); $tabfac[$obj->rowid]["datereg"] = $db->jdate($obj->dlr); $tabfac[$obj->rowid]["ref"] = $obj->ref_supplier . ' (' . $obj->ref . ')'; @@ -185,6 +190,10 @@ if ($result) { $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc; $tabht[$obj->rowid][$compta_prod] += $obj->total_ht; $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva; + if (! empty($line->tva_npr)) // Add an entry for counterpart + { + $tabother[$obj->rowid][$compta_counterpart_tva_npr] += $obj->total_tva; + } $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1; $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2; $tabcompany[$obj->rowid] = array ( @@ -192,9 +201,9 @@ if ($result) { 'name' => $obj->name, 'code_fournisseur' => $obj->code_fournisseur, 'code_compta_fournisseur' => $compta_soc - ); + ); - $i ++; + $i++; } } else { dol_print_error($db); @@ -388,6 +397,55 @@ if ($action == 'writebookkeeping') { } } + // Counterpart of VAT for VAT NPR + // var_dump($tabother); + if (! $errorforline) + { + foreach ( $tabother[$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").' NPR'; + $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; + + $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) { $error++; @@ -535,6 +593,25 @@ if ($action == 'exportcsv') { print "\n"; } } + + // VAT counterpart for NPR + foreach ( $tabother[$key] as $k => $mt ) { + 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 '"' . length_accounta(html_entity_decode($k)) . '"' . $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("VAT") . ' NPR"' . $sep; + print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep; + print '"' . ($mt >= 0 ? price($mt) : '') . '"'. $sep; + print '"' . $journal . '"' ; + print "\n"; + } + } } } } @@ -701,31 +778,59 @@ if (empty($action) || $action == 'view') { foreach ( $arrayofvat[$key] as $k => $mt ) { if ($mt) { - print '