Fix: Correct vat in report by company

This commit is contained in:
Laurent Destailleur 2009-02-11 21:15:47 +00:00
parent 566d0c7daf
commit caf73a4aff

View File

@ -1,6 +1,6 @@
<?php <?php
/* Copyright (C) 2002-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org> /* Copyright (C) 2002-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -127,7 +127,7 @@ if ($result) {
dolibarr_print_error($db); dolibarr_print_error($db);
} }
// On ajoute les paiements clients anciennes version, non li<EFBFBD>s par paiement_facture // On ajoute les paiements clients anciennes version, non lie par paiement_facture
if ($modecompta != 'CREANCES-DETTES') if ($modecompta != 'CREANCES-DETTES')
{ {
$sql = "SELECT 'Autres' as nom, '0' as idp, sum(p.amount) as amount_ttc"; $sql = "SELECT 'Autres' as nom, '0' as idp, sum(p.amount) as amount_ttc";
@ -254,172 +254,6 @@ print '<td colspan="3" align="right">'.price(-$subtotal_ttc).'</td>';
print '</tr>'; print '</tr>';
/*
* TVA
*/
print '<tr><td colspan="4">'.$langs->trans("VAT").'</td></tr>';
$subtotal_ht = 0;
$subtotal_ttc = 0;
if ($modecompta == 'CREANCES-DETTES')
{
// TVA <20> payer
$amount=0;
$sql = "SELECT sum(f.tva) as amount, date_format(f.datef,'%Y-%m') as dm";
$sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
$sql .= " WHERE f.fk_statut in (1,2)";
if ($year) {
$sql .= " AND f.datef between '".$year."-01-01 00:00:00' and '".$year."-12-31 23:59:59'";
}
$sql .= " GROUP BY dm";
$sql .= " ORDER BY dm DESC";
$result=$db->query($sql);
if ($result) {
$num = $db->num_rows($result);
$var=false;
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $db->fetch_object($result);
$amount = $amount - $obj->amount;
$total_ht = $total_ht - $obj->amount;
$total_ttc = $total_ttc - $obj->amount;
$subtotal_ht = $subtotal_ht - $obj->amount;
$subtotal_ttc = $subtotal_ttc - $obj->amount;
$i++;
}
}
} else {
dolibarr_print_error($db);
}
print "<tr $bc[$var]><td>&nbsp;</td>";
print "<td>".$langs->trans("VATToPay")."</td>\n";
if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price($amount)."</td>\n";
print "<td align=\"right\">".price($amount)."</td>\n";
print "</tr>\n";
// TVA <20> r<>cup<75>rer
$amount=0;
$sql = "SELECT sum(f.total_tva) as amount, date_format(f.datef,'%Y-%m') as dm";
$sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
$sql .= " WHERE f.fk_statut in (1,2)";
if ($year) {
$sql .= " AND f.datef between '".$year."-01-01 00:00:00' and '".$year."-12-31 23:59:59'";
}
$sql .= " GROUP BY dm";
$sql .= " ORDER BY dm DESC";
$result=$db->query($sql);
if ($result) {
$num = $db->num_rows($result);
$var=true;
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $db->fetch_object($result);
$amount = $amount + $obj->amount;
$total_ht = $total_ht + $obj->amount;
$total_ttc = $total_ttc + $obj->amount;
$subtotal_ht = $subtotal_ht + $obj->amount;
$subtotal_ttc = $subtotal_ttc + $obj->amount;
$i++;
}
}
} else {
dolibarr_print_error($db);
}
print "<tr $bc[$var]><td>&nbsp;</td>";
print "<td>".$langs->trans("VATToCollect")."</td>\n";
if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price($amount)."</td>\n";
print "<td align=\"right\">".price($amount)."</td>\n";
print "</tr>\n";
}
else
{
// TVA r<>ellement d<>j<EFBFBD> pay<61>e
$amount=0;
$sql = "SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm";
$sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
$sql .= " WHERE amount > 0";
if ($year) {
$sql .= " AND t.datev between '".$year."-01-01 00:00:00' and '".$year."-12-31 23:59:59'";
}
$sql .= " GROUP BY dm";
$sql .= " ORDER BY dm DESC";
$result=$db->query($sql);
if ($result) {
$num = $db->num_rows($result);
$var=false;
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $db->fetch_object($result);
$amount = $amount - $obj->amount;
$total_ht = $total_ht - $obj->amount;
$total_ttc = $total_ttc - $obj->amount;
$subtotal_ht = $subtotal_ht - $obj->amount;
$subtotal_ttc = $subtotal_ttc - $obj->amount;
$i++;
}
}
$db->free($result);
} else {
dolibarr_print_error($db);
}
print "<tr $bc[$var]><td>&nbsp;</td>";
print "<td>".$langs->trans("VATPayed")."</td>\n";
if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price($amount)."</td>\n";
print "<td align=\"right\">".price($amount)."</td>\n";
print "</tr>\n";
// TVA r<>cup<75>r<EFBFBD>e
$amount=0;
$sql = "SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm";
$sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
$sql .= " WHERE amount < 0";
if ($year) {
$sql .= " AND t.datev between '".$year."-01-01 00:00:00' and '".$year."-12-31 23:59:59'";
}
$sql .= " GROUP BY dm";
$sql .= " ORDER BY dm DESC";
$result=$db->query($sql);
if ($result) {
$num = $db->num_rows($result);
$var=true;
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $db->fetch_object($result);
$amount = $amount + $obj->amount;
$total_ht = $total_ht + $obj->amount;
$total_ttc = $total_ttc + $obj->amount;
$subtotal_ht = $subtotal_ht + $obj->amount;
$subtotal_ttc = $subtotal_ttc + $obj->amount;
$i++;
}
}
$db->free($result);
} else {
dolibarr_print_error($db);
}
print "<tr $bc[$var]><td>&nbsp;</td>";
print "<td>".$langs->trans("VATCollected")."</td>\n";
if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price($amount)."</td>\n";
print "<td align=\"right\">".price($amount)."</td>\n";
print "</tr>\n";
}
print '<tr class="liste_total">';
if ($modecompta == 'CREANCES-DETTES') print '<td colspan="3" align="right">'.price($subtotal_ht).'</td>';
print '<td colspan="3" align="right">'.price($subtotal_ttc).'</td>';
print '</tr>';
/* /*
* Charges sociales non deductibles * Charges sociales non deductibles
@ -471,7 +305,8 @@ if ($result) {
} }
} }
else { else {
print "<tr $bc[$var]><td>&nbsp;</td>"; $var = !$var;
print "<tr $bc[$var]><td>&nbsp;</td>";
print '<td colspan="3">'.$langs->trans("None").'</td>'; print '<td colspan="3">'.$langs->trans("None").'</td>';
print '</tr>'; print '</tr>';
} }
@ -513,7 +348,7 @@ $subtotal_ht = 0;
$subtotal_ttc = 0; $subtotal_ttc = 0;
if ($result) { if ($result) {
$num = $db->num_rows($result); $num = $db->num_rows($result);
$var=false; $var=true;
$i = 0; $i = 0;
if ($num) { if ($num) {
while ($i < $num) { while ($i < $num) {
@ -534,7 +369,8 @@ if ($result) {
} }
} }
else { else {
print "<tr $bc[$var]><td>&nbsp;</td>"; $var = !$var;
print "<tr $bc[$var]><td>&nbsp;</td>";
print '<td colspan="3">'.$langs->trans("None").'</td>'; print '<td colspan="3">'.$langs->trans("None").'</td>';
print '</tr>'; print '</tr>';
} }
@ -546,18 +382,208 @@ if ($modecompta == 'CREANCES-DETTES') print '<td colspan="3" align="right">'.pri
print '<td colspan="3" align="right">'.price(-$subtotal_ttc).'</td>'; print '<td colspan="3" align="right">'.price(-$subtotal_ttc).'</td>';
print '</tr>'; print '</tr>';
if ($mysoc->tva_assuj == 'franchise') // Non assujeti
{
// Total
print '<tr>';
print '<td colspan="4">&nbsp;</td>';
print '</tr>';
print '<tr class="liste_total"><td align="left" colspan="2">'.$langs->trans("Profit").'</td>';
if ($modecompta == 'CREANCES-DETTES') print '<td class="border" align="right">'.price($total_ht).'</td>';
print '<td class="border" align="right">'.price($total_ttc).'</td>';
print '</tr>';
print '<tr>';
print '<td colspan="4">&nbsp;</td>';
print '</tr>';
}
/*
* VAT
*/
print '<tr><td colspan="4">'.$langs->trans("VAT").'</td></tr>';
$subtotal_ht = 0;
$subtotal_ttc = 0;
if ($modecompta == 'CREANCES-DETTES')
{
// TVA a payer
$amount=0;
$sql = "SELECT sum(f.tva) as amount, date_format(f.datef,'%Y-%m') as dm";
$sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
$sql .= " WHERE f.fk_statut in (1,2)";
if ($year) {
$sql .= " AND f.datef between '".$year."-01-01 00:00:00' and '".$year."-12-31 23:59:59'";
}
$sql .= " GROUP BY dm";
$sql .= " ORDER BY dm DESC";
$result=$db->query($sql);
if ($result) {
$num = $db->num_rows($result);
$var=false;
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $db->fetch_object($result);
$amount = $amount - $obj->amount;
$total_ht = $total_ht - $obj->amount;
$total_ttc = $total_ttc - $obj->amount;
$subtotal_ht = $subtotal_ht - $obj->amount;
$subtotal_ttc = $subtotal_ttc - $obj->amount;
$i++;
}
}
} else {
dolibarr_print_error($db);
}
print "<tr $bc[$var]><td>&nbsp;</td>";
print "<td>".$langs->trans("VATToPay")."</td>\n";
if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price($amount)."</td>\n";
print "<td align=\"right\">".price($amount)."</td>\n";
print "</tr>\n";
// TVA a recuperer
$amount=0;
$sql = "SELECT sum(f.total_tva) as amount, date_format(f.datef,'%Y-%m') as dm";
$sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
$sql .= " WHERE f.fk_statut in (1,2)";
if ($year) {
$sql .= " AND f.datef between '".$year."-01-01 00:00:00' and '".$year."-12-31 23:59:59'";
}
$sql .= " GROUP BY dm";
$sql .= " ORDER BY dm DESC";
$result=$db->query($sql);
if ($result) {
$num = $db->num_rows($result);
$var=true;
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $db->fetch_object($result);
$amount = $amount + $obj->amount;
$total_ht = $total_ht + $obj->amount;
$total_ttc = $total_ttc + $obj->amount;
$subtotal_ht = $subtotal_ht + $obj->amount;
$subtotal_ttc = $subtotal_ttc + $obj->amount;
$i++;
}
}
} else {
dolibarr_print_error($db);
}
print "<tr $bc[$var]><td>&nbsp;</td>";
print "<td>".$langs->trans("VATToCollect")."</td>\n";
if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price($amount)."</td>\n";
print "<td align=\"right\">".price($amount)."</td>\n";
print "</tr>\n";
}
else
{
// TVA reellement deja payee
$amount=0;
$sql = "SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm";
$sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
$sql .= " WHERE amount > 0";
if ($year) {
$sql .= " AND t.datev between '".$year."-01-01 00:00:00' and '".$year."-12-31 23:59:59'";
}
$sql .= " GROUP BY dm";
$sql .= " ORDER BY dm DESC";
$result=$db->query($sql);
if ($result) {
$num = $db->num_rows($result);
$var=false;
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $db->fetch_object($result);
$amount = $amount - $obj->amount;
$total_ht = $total_ht - $obj->amount;
$total_ttc = $total_ttc - $obj->amount;
$subtotal_ht = $subtotal_ht - $obj->amount;
$subtotal_ttc = $subtotal_ttc - $obj->amount;
$i++;
}
}
$db->free($result);
} else {
dolibarr_print_error($db);
}
print "<tr $bc[$var]><td>&nbsp;</td>";
print "<td>".$langs->trans("VATPayed")."</td>\n";
if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price($amount)."</td>\n";
print "<td align=\"right\">".price($amount)."</td>\n";
print "</tr>\n";
// TVA recuperee
$amount=0;
$sql = "SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm";
$sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
$sql .= " WHERE amount < 0";
if ($year) {
$sql .= " AND t.datev between '".$year."-01-01 00:00:00' and '".$year."-12-31 23:59:59'";
}
$sql .= " GROUP BY dm";
$sql .= " ORDER BY dm DESC";
$result=$db->query($sql);
if ($result) {
$num = $db->num_rows($result);
$var=true;
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $db->fetch_object($result);
$amount = $amount + $obj->amount;
$total_ht = $total_ht + $obj->amount;
$total_ttc = $total_ttc + $obj->amount;
$subtotal_ht = $subtotal_ht + $obj->amount;
$subtotal_ttc = $subtotal_ttc + $obj->amount;
$i++;
}
}
$db->free($result);
} else {
dolibarr_print_error($db);
}
print "<tr $bc[$var]><td>&nbsp;</td>";
print "<td>".$langs->trans("VATCollected")."</td>\n";
if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price($amount)."</td>\n";
print "<td align=\"right\">".price($amount)."</td>\n";
print "</tr>\n";
}
if ($mysoc->tva_assuj != 'franchise') // Assujeti
{
print '<tr class="liste_total">';
if ($modecompta == 'CREANCES-DETTES') print '<td colspan="3" align="right">'.price($subtotal_ht).'</td>';
print '<td colspan="3" align="right">'.price($subtotal_ttc).'</td>';
print '</tr>';
}
if ($mysoc->tva_assuj != 'franchise') // Assujeti
{
// Total // Total
print '<tr>';
print '<td colspan="4">&nbsp;</td>';
print '</tr>';
print '<tr>'; print '<tr class="liste_total"><td align="left" colspan="2">'.$langs->trans("Profit").'</td>';
print '<td colspan="4">&nbsp;</td>'; if ($modecompta == 'CREANCES-DETTES') print '<td class="border" align="right">'.price($total_ht).'</td>';
print '</tr>'; print '<td class="border" align="right">'.price($total_ttc).'</td>';
print '</tr>';
print '<tr class="liste_total"><td align="left" colspan="2">'.$langs->trans("Profit").'</td>'; }
if ($modecompta == 'CREANCES-DETTES') print '<td class="border" align="right">'.price($total_ht).'</td>';
print '<td class="border" align="right">'.price($total_ttc).'</td>';
print '</tr>';
print "</table>"; print "</table>";
print '<br>'; print '<br>';