From caf73a4affc544911afdf9f0afd85baf79c40beb Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 11 Feb 2009 21:15:47 +0000 Subject: [PATCH] Fix: Correct vat in report by company --- htdocs/compta/resultat/clientfourn.php | 422 +++++++++++++------------ 1 file changed, 224 insertions(+), 198 deletions(-) diff --git a/htdocs/compta/resultat/clientfourn.php b/htdocs/compta/resultat/clientfourn.php index b8b1a6e9a80..15947c88d87 100644 --- a/htdocs/compta/resultat/clientfourn.php +++ b/htdocs/compta/resultat/clientfourn.php @@ -1,6 +1,6 @@ - * Copyright (C) 2004-2008 Laurent Destailleur + * Copyright (C) 2004-2009 Laurent Destailleur * * 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 @@ -82,7 +82,7 @@ print ' '; */ print 'Facturation clients'; -if ($modecompta == 'CREANCES-DETTES') { +if ($modecompta == 'CREANCES-DETTES') { $sql = "SELECT s.nom, s.rowid as socid, sum(f.total) as amount_ht, sum(f.total_ttc) as amount_ttc"; $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."facture as f"; $sql .= " WHERE f.fk_soc = s.rowid AND f.fk_statut in (1,2)"; @@ -96,7 +96,7 @@ if ($modecompta == 'CREANCES-DETTES') { $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."paiement_facture as pf, ".MAIN_DB_PREFIX."paiement as p"; $sql .= " WHERE p.rowid = pf.fk_paiement AND pf.fk_facture = f.rowid AND f.fk_soc = s.rowid"; if ($year) $sql .= " AND p.datep between '".$year."-01-01 00:00:00' and '".$year."-12-31 23:59:59'"; -} +} if ($socid) $sql .= " AND f.fk_soc = $socid"; $sql .= " GROUP BY nom"; $sql .= " ORDER BY nom"; @@ -110,13 +110,13 @@ if ($result) { { $objp = $db->fetch_object($result); $var=!$var; - + print " "; print "".$langs->trans("Bills")." socid."\">$objp->nom\n"; - + if ($modecompta == 'CREANCES-DETTES') print "".price($objp->amount_ht)."\n"; print "".price($objp->amount_ttc)."\n"; - + $total_ht = $total_ht + $objp->amount_ht; $total_ttc = $total_ttc + $objp->amount_ttc; print "\n"; @@ -127,9 +127,9 @@ if ($result) { dolibarr_print_error($db); } -// On ajoute les paiements clients anciennes version, non li�s par paiement_facture +// On ajoute les paiements clients anciennes version, non lie par paiement_facture if ($modecompta != 'CREANCES-DETTES') -{ +{ $sql = "SELECT 'Autres' as nom, '0' as idp, sum(p.amount) as amount_ttc"; $sql .= " FROM ".MAIN_DB_PREFIX."paiement as p"; $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement"; @@ -147,13 +147,13 @@ if ($modecompta != 'CREANCES-DETTES') { $objp = $db->fetch_object($result); $var=!$var; - + print " "; print "".$langs->trans("Bills")." ".$langs->trans("Other")." (".$langs->trans("PaymentsNotLinkedToInvoice").")\n"; - + if ($modecompta == 'CREANCES-DETTES') print "".price($objp->amount_ht)."\n"; print "".price($objp->amount_ttc)."\n"; - + $total_ht = $total_ht + $objp->amount_ht; $total_ttc = $total_ttc + $objp->amount_ttc; print "\n"; @@ -184,7 +184,7 @@ print ''; * Frais, factures fournisseurs. */ if ($modecompta == 'CREANCES-DETTES') -{ +{ $sql = "SELECT s.nom, s.rowid as socid, sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc, date_format(f.datef,'%Y-%m') as dm"; $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture_fourn as f"; $sql .= " WHERE f.fk_soc = s.rowid AND f.fk_statut in (1,2)"; @@ -222,13 +222,13 @@ if ($result) { while ($i < $num) { $objp = $db->fetch_object($result); $var=!$var; - + print " "; print "".$langs->trans("Bills")." socid."\">".$objp->nom."\n"; - + if ($modecompta == 'CREANCES-DETTES') print "".price(-$objp->amount_ht)."\n"; print "".price(-$objp->amount_ttc)."\n"; - + $total_ht = $total_ht - $objp->amount_ht; $total_ttc = $total_ttc - $objp->amount_ttc; $subtotal_ht = $subtotal_ht + $objp->amount_ht; @@ -254,172 +254,6 @@ print ''.price(-$subtotal_ttc).''; print ''; -/* - * TVA - */ -print ''.$langs->trans("VAT").''; -$subtotal_ht = 0; -$subtotal_ttc = 0; - -if ($modecompta == 'CREANCES-DETTES') -{ - // TVA � 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 " "; - print "".$langs->trans("VATToPay")."\n"; - if ($modecompta == 'CREANCES-DETTES') print "".price($amount)."\n"; - print "".price($amount)."\n"; - print "\n"; - - // TVA � r�cup�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 " "; - print "".$langs->trans("VATToCollect")."\n"; - if ($modecompta == 'CREANCES-DETTES') print "".price($amount)."\n"; - print "".price($amount)."\n"; - print "\n"; -} -else -{ - // TVA r�ellement d�j� pay�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 " "; - print "".$langs->trans("VATPayed")."\n"; - if ($modecompta == 'CREANCES-DETTES') print "".price($amount)."\n"; - print "".price($amount)."\n"; - print "\n"; - - // TVA r�cup�r�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 " "; - print "".$langs->trans("VATCollected")."\n"; - if ($modecompta == 'CREANCES-DETTES') print "".price($amount)."\n"; - print "".price($amount)."\n"; - print "\n"; -} -print ''; -if ($modecompta == 'CREANCES-DETTES') print ''.price($subtotal_ht).''; -print ''.price($subtotal_ttc).''; -print ''; - /* * Charges sociales non deductibles @@ -452,15 +286,15 @@ if ($result) { $num = $db->num_rows($result); $var=true; $i = 0; - if ($num) { + if ($num) { while ($i < $num) { $obj = $db->fetch_object($result); - + $total_ht = $total_ht - $obj->amount; $total_ttc = $total_ttc - $obj->amount; $subtotal_ht = $subtotal_ht + $obj->amount; $subtotal_ttc = $subtotal_ttc + $obj->amount; - + $var = !$var; print " "; print ''.$obj->nom.''; @@ -471,7 +305,8 @@ if ($result) { } } else { - print " "; + $var = !$var; + print " "; print ''.$langs->trans("None").''; print ''; } @@ -513,17 +348,17 @@ $subtotal_ht = 0; $subtotal_ttc = 0; if ($result) { $num = $db->num_rows($result); - $var=false; + $var=true; $i = 0; if ($num) { while ($i < $num) { $obj = $db->fetch_object($result); - + $total_ht = $total_ht - $obj->amount; $total_ttc = $total_ttc - $obj->amount; $subtotal_ht = $subtotal_ht + $obj->amount; $subtotal_ttc = $subtotal_ttc + $obj->amount; - + $var = !$var; print " "; print ''.$obj->nom.''; @@ -534,7 +369,8 @@ if ($result) { } } else { - print " "; + $var = !$var; + print " "; print ''.$langs->trans("None").''; print ''; } @@ -546,18 +382,208 @@ if ($modecompta == 'CREANCES-DETTES') print ''.pri print ''.price(-$subtotal_ttc).''; print ''; +if ($mysoc->tva_assuj == 'franchise') // Non assujeti +{ + // Total + print ''; + print ' '; + print ''; + print ''.$langs->trans("Profit").''; + if ($modecompta == 'CREANCES-DETTES') print ''.price($total_ht).''; + print ''.price($total_ttc).''; + print ''; + + print ''; + print ' '; + print ''; +} + + +/* + * VAT + */ +print ''.$langs->trans("VAT").''; +$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 " "; + print "".$langs->trans("VATToPay")."\n"; + if ($modecompta == 'CREANCES-DETTES') print "".price($amount)."\n"; + print "".price($amount)."\n"; + print "\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 " "; + print "".$langs->trans("VATToCollect")."\n"; + if ($modecompta == 'CREANCES-DETTES') print "".price($amount)."\n"; + print "".price($amount)."\n"; + print "\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 " "; + print "".$langs->trans("VATPayed")."\n"; + if ($modecompta == 'CREANCES-DETTES') print "".price($amount)."\n"; + print "".price($amount)."\n"; + print "\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 " "; + print "".$langs->trans("VATCollected")."\n"; + if ($modecompta == 'CREANCES-DETTES') print "".price($amount)."\n"; + print "".price($amount)."\n"; + print "\n"; +} + + +if ($mysoc->tva_assuj != 'franchise') // Assujeti +{ + print ''; + if ($modecompta == 'CREANCES-DETTES') print ''.price($subtotal_ht).''; + print ''.price($subtotal_ttc).''; + print ''; +} + + +if ($mysoc->tva_assuj != 'franchise') // Assujeti +{ // Total + print ''; + print ' '; + print ''; -print ''; -print ' '; -print ''; - -print ''.$langs->trans("Profit").''; -if ($modecompta == 'CREANCES-DETTES') print ''.price($total_ht).''; -print ''.price($total_ttc).''; -print ''; - + print ''.$langs->trans("Profit").''; + if ($modecompta == 'CREANCES-DETTES') print ''.price($total_ht).''; + print ''.price($total_ttc).''; + print ''; +} print ""; print '
';