Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW: Accountancy - Manage intra-community VAT on supplier invoices FPC22 #24448

Merged
merged 7 commits into from Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion htdocs/accountancy/admin/defaultaccounts.php
@@ -1,7 +1,7 @@
<?php
/* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
* Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2013-2020 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2013-2023 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2014-2015 Ari Elbaz (elarifr) <github@accedinfo.com>
* Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
* Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
Expand Down Expand Up @@ -81,6 +81,10 @@
$list_account[] = 'ACCOUNTING_VAT_BUY_ACCOUNT';
$list_account[] = 'ACCOUNTING_VAT_SOLD_ACCOUNT';
$list_account[] = 'ACCOUNTING_VAT_PAY_ACCOUNT';
if (!empty($conf->global->ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE)) {
$list_account[] = 'ACCOUNTING_VAT_BUY_REVERSE_CHARGES_CREDIT';
$list_account[] = 'ACCOUNTING_VAT_BUY_REVERSE_CHARGES_DEBIT';
}
if (isModEnabled('banque')) {
$list_account[] = 'ACCOUNTING_ACCOUNT_TRANSFER_CASH';
}
Expand Down
41 changes: 40 additions & 1 deletion htdocs/accountancy/admin/index.php
@@ -1,7 +1,7 @@
<?php
/* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
* Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2013-2021 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2013-2023 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2014-2015 Ari Elbaz (elarifr) <github@accedinfo.com>
* Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
* Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
Expand Down Expand Up @@ -275,6 +275,19 @@
}
}

if ($action == 'setenablevatreversecharge') {
$setenablevatreversecharge = GETPOST('value', 'int');
$res = dolibarr_set_const($db, "ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE", $setenablevatreversecharge, 'yesno', 0, '', $conf->entity);
if (!($res > 0)) {
$error++;
}

if (!$error) {
setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
} else {
setEventMessages($langs->trans("Error"), null, 'mesgs');
}
}

/*
* View
Expand Down Expand Up @@ -531,6 +544,32 @@

print '</table>';

// Show specific accountancy FR option
if ($mysoc->country_code == 'FR') {
print '<br>';

// Advanced params
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<td colspan="2">' . $langs->trans('OptionsAdvanced') . '</td>';
print "</tr>\n";

print '<tr class="oddeven">';
print '<td>' . $langs->trans("ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE") . '</td>';
if (!empty($conf->global->ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE)) {
print '<td class="right"><a class="reposition" href="' . $_SERVER['PHP_SELF'] . '?token=' . newToken() . '&action=setenablevatreversecharge&value=0">';
print img_picto($langs->trans("Activated"), 'switch_on');
print '</a></td>';
} else {
print '<td class="right"><a class="reposition" href="' . $_SERVER['PHP_SELF'] . '?token=' . newToken() . '&action=setenablevatreversecharge&value=1">';
print img_picto($langs->trans("Disabled"), 'switch_off');
print '</a></td>';
}
print '</tr>';

print '</table>';
}

print '<div class="center"><input type="submit" class="button button-edit" name="button" value="'.$langs->trans('Modify').'"></div>';

print '</form>';
Expand Down
132 changes: 129 additions & 3 deletions htdocs/accountancy/journal/purchasesjournal.php
Expand Up @@ -3,7 +3,7 @@
* Copyright (C) 2007-2010 Jean Heimburger <jean@tiaris.info>
* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
* Copyright (C) 2013-2021 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2013-2023 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2013-2016 Olivier Geffroy <jeff@jeffinfo.com>
* Copyright (C) 2013-2016 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
Expand Down Expand Up @@ -32,6 +32,7 @@
require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
Expand Down Expand Up @@ -103,9 +104,11 @@
$date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
}

$sql = "SELECT f.rowid, f.ref as ref, f.type, f.datef as df, f.libelle as label, f.ref_supplier, f.date_lim_reglement as dlr, f.close_code,";
$sql = "SELECT f.rowid, f.ref as ref, f.type, f.datef as df, f.libelle as label, f.ref_supplier, f.date_lim_reglement as dlr, f.close_code, f.vat_reverse_charge,";
$sql .= " fd.rowid as fdid, fd.description, fd.product_type, fd.total_ht, fd.tva as total_tva, fd.total_localtax1, fd.total_localtax2, fd.tva_tx, fd.total_ttc, fd.vat_src_code, fd.info_bits,";
$sql .= " s.rowid as socid, s.nom as name, s.fournisseur, s.code_client, s.code_fournisseur,";
$sql .= " p.default_vat_code AS product_buy_default_vat_code, p.tva_tx as product_buy_vat, p.localtax1_tx as product_buy_localvat1, p.localtax2_tx as product_buy_localvat2,";
$sql .= " co.code as country_code, co.label as country_label,";
$sql .= " s.rowid as socid, s.nom as name, s.fournisseur, s.code_client, s.code_fournisseur, s.fk_pays,";
if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
$sql .= " spe.accountancy_code_customer as code_compta,";
$sql .= " spe.accountancy_code_supplier as code_compta_fournisseur,";
Expand All @@ -127,6 +130,7 @@
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = fd.fk_code_ventilation";
$sql .= " JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON f.rowid = fd.fk_facture_fourn";
$sql .= " JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON co.rowid = s.fk_pays ";
if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
}
Expand Down Expand Up @@ -166,12 +170,18 @@
$tablocaltax2 = array();
$tabcompany = array();
$tabother = array();
$tabrctva = array();
$tabrclocaltax1 = array();
$tabrclocaltax2 = array();

$num = $db->num_rows($result);

// Variables
$cptfour = ($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER != "") ? $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER : 'NotDefined';
$cpttva = (!empty($conf->global->ACCOUNTING_VAT_BUY_ACCOUNT)) ? $conf->global->ACCOUNTING_VAT_BUY_ACCOUNT : 'NotDefined';
$rcctva = (!empty($conf->global->ACCOUNTING_VAT_BUY_REVERSE_CHARGES_CREDIT)) ? $conf->global->ACCOUNTING_VAT_BUY_REVERSE_CHARGES_CREDIT : 'NotDefined';
$rcdtva = (!empty($conf->global->ACCOUNTING_VAT_BUY_REVERSE_CHARGES_DEBIT)) ? $conf->global->ACCOUNTING_VAT_BUY_REVERSE_CHARGES_DEBIT : 'NotDefined';
$country_code_in_EEC = getCountriesInEEC(); // This make a database call but there is a cache done into $conf->cache['country_code_in_EEC']

$i = 0;
while ($i < $num) {
Expand Down Expand Up @@ -230,6 +240,53 @@
$tablocaltax2[$obj->rowid][$compta_localtax2] = 0;
}

// VAT Reverse charge
if (($mysoc->country_code == 'FR' || !empty($conf->global->ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE)) && $obj->vat_reverse_charge == 1 && in_array($obj->country_code, $country_code_in_EEC)) {
$rcvatdata = getTaxesFromId($obj->product_buy_vat . ($obj->product_buy_default_vat_code ? ' (' . $obj->product_buy_default_vat_code . ')' : ''), $mysoc, $mysoc, 0);
$rcc_compta_tva = (!empty($vatdata['accountancy_code_vat_reverse_charge_credit']) ? $vatdata['accountancy_code_vat_reverse_charge_credit'] : $rcctva);
$rcd_compta_tva = (!empty($vatdata['accountancy_code_vat_reverse_charge_debit']) ? $vatdata['accountancy_code_vat_reverse_charge_debit'] : $rcdtva);
$rcc_compta_localtax1 = (!empty($vatdata['accountancy_code_vat_reverse_charge_credit']) ? $vatdata['accountancy_code_vat_reverse_charge_credit'] : $rcctva);
$rcd_compta_localtax1 = (!empty($vatdata['accountancy_code_vat_reverse_charge_debit']) ? $vatdata['accountancy_code_vat_reverse_charge_debit'] : $rcdtva);
$rcc_compta_localtax2 = (!empty($vatdata['accountancy_code_vat_reverse_charge_credit']) ? $vatdata['accountancy_code_vat_reverse_charge_credit'] : $rcctva);
$rcd_compta_localtax2 = (!empty($vatdata['accountancy_code_vat_reverse_charge_debit']) ? $vatdata['accountancy_code_vat_reverse_charge_debit'] : $rcdtva);
if (price2num($obj->product_buy_vat) || !empty($obj->product_buy_default_vat_code)) {
$vat_key = vatrate($obj->product_buy_vat) . ($obj->product_buy_default_vat_code ? ' (' . $obj->product_buy_default_vat_code . ')' : '');
$val_value = $vat_key;
$def_tva[$obj->rowid][$rcc_compta_tva][$vat_key] = $val_value;
$def_tva[$obj->rowid][$rcd_compta_tva][$vat_key] = $val_value;
}

if (!isset($tabrctva[$obj->rowid][$rcc_compta_tva])) {
$tabrctva[$obj->rowid][$rcc_compta_tva] = 0;
}
if (!isset($tabrctva[$obj->rowid][$rcd_compta_tva])) {
$tabrctva[$obj->rowid][$rcd_compta_tva] = 0;
}
if (!isset($tabrclocaltax1[$obj->rowid][$rcc_compta_localtax1])) {
$tabrclocaltax1[$obj->rowid][$rcc_compta_localtax1] = 0;
}
if (!isset($tabrclocaltax1[$obj->rowid][$rcd_compta_localtax1])) {
$tabrclocaltax1[$obj->rowid][$rcd_compta_localtax1] = 0;
}
if (!isset($tabrclocaltax2[$obj->rowid][$rcc_compta_localtax2])) {
$tabrclocaltax2[$obj->rowid][$rcc_compta_localtax2] = 0;
}
if (!isset($tabrclocaltax2[$obj->rowid][$rcd_compta_localtax2])) {
$tabrclocaltax2[$obj->rowid][$rcd_compta_localtax2] = 0;
}

$rcvat = (double) price2num($obj->total_ttc * $obj->product_buy_vat / 100, 'MT');
$rclocalvat1 = (double) price2num($obj->total_ttc * $obj->product_buy_localvat1 / 100, 'MT');
$rclocalvat2 = (double) price2num($obj->total_ttc * $obj->product_buy_localvat2 / 100, 'MT');

$tabrctva[$obj->rowid][$rcd_compta_tva] += $rcvat;
$tabrctva[$obj->rowid][$rcc_compta_tva] -= $rcvat;
$tabrclocaltax1[$obj->rowid][$rcd_compta_localtax1] += $rclocalvat1;
$tabrclocaltax1[$obj->rowid][$rcc_compta_localtax1] -= $rclocalvat1;
$tabrclocaltax2[$obj->rowid][$rcd_compta_localtax2] += $rclocalvat2;
$tabrclocaltax2[$obj->rowid][$rcc_compta_localtax2] -= $rclocalvat2;
}

$tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc;
$tabht[$obj->rowid][$compta_prod] += $obj->total_ht;
$tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
Expand Down Expand Up @@ -470,6 +527,29 @@
$arrayofvat = $tablocaltax2;
}

// VAT Reverse charge
if ($mysoc->country_code == 'FR' || !empty($conf->global->ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE)) {
$has_vat = false;
foreach ($arrayofvat[$key] as $k => $mt) {
if ($mt) {
$has_vat = true;
}
}

if (!$has_vat) {
$arrayofvat = $tabrctva;
if ($numtax == 1) {
$arrayofvat = $tabrclocaltax1;
}
if ($numtax == 2) {
$arrayofvat = $tabrclocaltax2;
}
if (!is_array($arrayofvat[$key])) {
$arrayofvat[$key] = array();
}
}
}

foreach ($arrayofvat[$key] as $k => $mt) {
if ($mt) {
$accountingaccount->fetch(null, $k, true); // TODO Use a cache for label
Expand Down Expand Up @@ -720,6 +800,29 @@
$arrayofvat = $tablocaltax2;
}

// VAT Reverse charge
if ($mysoc->country_code == 'FR' || !empty($conf->global->ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE)) {
$has_vat = false;
foreach ($arrayofvat[$key] as $k => $mt) {
if ($mt) {
$has_vat = true;
}
}

if (!$has_vat) {
$arrayofvat = $tabrctva;
if ($numtax == 1) {
$arrayofvat = $tabrclocaltax1;
}
if ($numtax == 2) {
$arrayofvat = $tabrclocaltax2;
}
if (!is_array($arrayofvat[$key])) {
$arrayofvat[$key] = array();
}
}
}

foreach ($arrayofvat[$key] as $k => $mt) {
if ($mt) {
print '"'.$key.'"'.$sep;
Expand Down Expand Up @@ -998,6 +1101,29 @@ function writebookkeeping() {
$arrayofvat = $tablocaltax2;
}

// VAT Reverse charge
if ($mysoc->country_code == 'FR' || !empty($conf->global->ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE)) {
$has_vat = false;
foreach ($arrayofvat[$key] as $k => $mt) {
if ($mt) {
$has_vat = true;
}
}

if (!$has_vat) {
$arrayofvat = $tabrctva;
if ($numtax == 1) {
$arrayofvat = $tabrclocaltax1;
}
if ($numtax == 2) {
$arrayofvat = $tabrclocaltax2;
}
if (!is_array($arrayofvat[$key])) {
$arrayofvat[$key] = array();
}
}
}

foreach ($arrayofvat[$key] as $k => $mt) {
if ($mt) {
print '<tr class="oddeven">';
Expand Down
12 changes: 12 additions & 0 deletions htdocs/fourn/card.php
Expand Up @@ -43,6 +43,9 @@
if (isModEnabled('categorie')) {
require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
}
if (!empty($conf->accounting->enabled)) {
require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
}

// Load translation files required by page
$langs->loadLangs(array(
Expand Down Expand Up @@ -270,6 +273,15 @@
print '</td></tr>';
}

// VAT reverse-charge by default on supplier invoice or not
print '<tr>';
print '<td class="titlefield">';
print $form->textwithpicto($langs->trans('VATReverseChargeByDefault'), $langs->trans('VATReverseChargeByDefaultDesc'));
print '</td><td>';
print '<input type="checkbox" name="vat_reverse_charge" '.($object->vat_reverse_charge == '1' ? ' checked' : '').' disabled>';
print '</td>';
print '</tr>';

// TVA Intra
print '<tr><td class="nowrap">';
//print $langs->trans('VATIntra').'</td><td>';
Expand Down