diff --git a/htdocs/compta/tva/quadri.php b/htdocs/compta/tva/quadri.php new file mode 100644 index 00000000000..d6dc95b5bd5 --- /dev/null +++ b/htdocs/compta/tva/quadri.php @@ -0,0 +1,313 @@ + + * Copyright (C) 2004 Éric Seigne + * Copyright (C) 2004-2006 Laurent Destailleur + * Copyright (C) 2006 Yannick Warnier + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ + * $Source$ + * + */ + +/** + \file htdocs/compta/tva/quadri.php + \ingroup compta + \brief Trimestrial page + \version $Revision$ + @todo deal with recurrent invoices as well +*/ + +require("./pre.inc.php"); +require("../../tva.class.php"); + +$year=$_GET["year"]; +if ($year == 0 ) +{ + $year_current = strftime("%Y",time()); + $year_start = $year_current; +} else { + $year_current = $year; + $year_start = $year; +} + + + +/** + * Gets VAT to collect for the given month of the given year + * + * The function gets the VAT in split results, as the VAT declaration asks + * to report the amounts for different VAT rates as different lines. + * This function also accounts recurrent invoices + * @param object Database handler object + * @param integer Year + * @param integer Year quarter (1-4) + */ +function tva_coll($db,$y,$q) +{ + global $conf; + if ($conf->compta->mode == "CREANCES-DETTES") + { + // if vat payed on due invoices + $sql = "SELECT d.fk_facture as facid, f.facnumber as facnum, d.tva_taux as rate, d.total_ht as totalht, d.total_tva as amount"; + $sql.= " FROM ".MAIN_DB_PREFIX."facture as f, "; + $sql.= MAIN_DB_PREFIX."facturedet as d " ; + $sql.= " WHERE "; + $sql.= " f.fk_statut = 1 "; + $sql.= " AND f.rowid = d.fk_facture "; + $sql.= " AND date_format(f.datef,'%Y') = ".$y; + $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3)." AND date_format(f.datef,'%m') <= ".($q*3).")"; + $sql.= " ORDER BY rate, facid"; + + } + else + { + // if vat payed on paiments + } + + $resql = $db->query($sql); + + if ($resql) + { + $list = array(); + $rate = -1; + while($assoc = $db->fetch_array($resql)) + { + if($assoc['rate'] != $rate){ //new rate + $list[$assoc['rate']]['totalht'] = $assoc['totalht']; + $list[$assoc['rate']]['vat'] = $assoc['amount']; + $list[$assoc['rate']]['facid'][] = $assoc['facid']; + $list[$assoc['rate']]['facnum'][] = $assoc['facnum']; + }else{ + $list[$assoc['rate']]['totalht'] += $assoc['totalht']; + $list[$assoc['rate']]['vat'] += $assoc['amount']; + if(!in_array($assoc['facid'],$list[$assoc['rate']]['facid'])){ + $list[$assoc['rate']]['facid'][] = $assoc['facid']; + $list[$assoc['rate']]['facnum'][] = $assoc['facnum']; + } + } + $rate = $assoc['rate']; + } + return $list; + } + else + { + dolibarr_print_error($db); + } +} + + +/** + * Gets VAT to pay for the given month of the given year + * + * The function gets the VAT in split results, as the VAT declaration asks + * to report the amounts for different VAT rates as different lines. + * @param object Database handler object + * @param integer Year + * @param integer Year quarter (1-4) + */ +function tva_paye($db, $y,$q) +{ + global $conf; + + if ($conf->compta->mode == "CREANCES-DETTES") + { + // Si on paye la tva sur les factures dues (non brouillon) + $sql = "SELECT d.fk_facture_fourn as facid, f.facnumber as facnum, d.tva_taux as rate, d.total_ht as totalht, d.tva as amount"; + $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn as f, "; + $sql.= MAIN_DB_PREFIX."facture_fourn_det as d " ; + $sql.= " WHERE "; + $sql.= " f.fk_statut = 1 "; + $sql.= " AND f.rowid = d.fk_facture_fourn "; + $sql.= " AND date_format(f.datef,'%Y') = ".$y; + $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3)." AND date_format(f.datef,'%m') <= ".($q*3).")"; + $sql.= " ORDER BY rate, facid "; + } + else + { + // Si on paye la tva sur les payments + } + + $resql = $db->query($sql); + if ($resql) + { + $list = array(); + $rate = -1; + while($assoc = $db->fetch_array($resql)) + { + if($assoc['rate'] != $rate){ //new rate + $list[$assoc['rate']]['totalht'] = $assoc['totalht']; + $list[$assoc['rate']]['vat'] = $assoc['amount']; + $list[$assoc['rate']]['facid'][] = $assoc['facid']; + $list[$assoc['rate']]['facnum'][] = $assoc['facnum']; + }else{ + $list[$assoc['rate']]['totalht'] += $assoc['totalht']; + $list[$assoc['rate']]['vat'] += $assoc['amount']; + if(!in_array($assoc['facid'],$list[$assoc['rate']]['facid'])){ + $list[$assoc['rate']]['facid'][] = $assoc['facid']; + $list[$assoc['rate']]['facnum'][] = $assoc['facnum']; + } + } + $rate = $assoc['rate']; + } + return $list; + + } + else + { + dolibarr_print_error($db); + } +} + +/** + * Main script + */ + +llxHeader(); + +$textprevyear="".img_previous().""; +$textnextyear=" ".img_next().""; + +print_fiche_titre($langs->trans("VAT"),"$textprevyear ".$langs->trans("Year")." $year_start $textnextyear"); + + +echo ''; +echo ''; + +echo ''; + +print "
'; +print_fiche_titre($langs->trans("VATSummary")); +echo '
"; +print ""; +print ""; +print ""; +print ""; +print ""; +print ""; +print ""; +print ""; +print ""; +print "\n"; + +if ($conf->compta->mode == "CREANCES-DETTES") +{ + $y = $year_current ; + + + $total = 0; $subtotal = 0; + $i=0; + $subtot_coll_total = 0; + $subtot_coll_vat = 0; + $subtot_paye_total = 0; + $subtot_paye_vat = 0; + for ($q = 1 ; $q <= 4 ; $q++ ) + { + print ""; + $var=true; + + $x_coll = tva_coll($db, $y, $q); + $x_paye = tva_paye($db, $y, $q); + $x_both = array(); + //now, from these two arrays, get another array with one rate per line + foreach(array_keys($x_coll) as $my_coll_rate){ + $x_both[$my_coll_rate]['coll']['totalht'] = $x_coll[$my_coll_rate]['totalht']; + $x_both[$my_coll_rate]['coll']['vat'] = $x_coll[$my_coll_rate]['vat']; + $x_both[$my_coll_rate]['paye']['totalht'] = 0; + $x_both[$my_coll_rate]['paye']['vat'] = 0; + $x_both[$my_coll_rate]['coll']['links'] = ''; + foreach($x_coll[$my_coll_rate]['facid'] as $id=>$dummy){ + $x_both[$my_coll_rate]['coll']['links'] .= '..'.substr($x_coll[$my_coll_rate]['facnum'][$id],-2).' '; + } + } + foreach(array_keys($x_paye) as $my_paye_rate){ + $x_both[$my_paye_rate]['paye']['totalht'] = $x_paye[$my_paye_rate]['totalht']; + $x_both[$my_paye_rate]['paye']['vat'] = $x_paye[$my_paye_rate]['vat']; + if(!isset($x_both[$my_paye_rate]['coll']['totalht'])){ + $x_both[$my_paye_rate]['coll']['total_ht'] = 0; + $x_both[$my_paye_rate]['coll']['vat'] = 0; + } + $x_both[$my_paye_rate]['paye']['links'] = ''; + foreach($x_paye[$my_paye_rate]['facid'] as $id=>$dummy){ + $x_both[$my_paye_rate]['paye']['links'] .= '..'.substr($x_paye[$my_paye_rate]['facnum'][$id],-2).' '; + } + } + //now we have an array (x_both) indexed by rates for coll and paye + + $x_coll_sum = 0; + $x_coll_ht = 0; + $x_paye_sum = 0; + $x_paye_ht = 0; + foreach($x_both as $rate => $both){ + $var=!$var; + print ""; + print ""; + print ""; + print ""; + print ""; + print ""; + print ""; + print ""; + print ""; + print ""; + $x_coll_sum += $both['coll']['vat']; + $x_paye_sum += $both['paye']['vat']; + $subtot_coll_total += $both['coll']['totalht']; + $subtot_coll_vat += $both['coll']['vat']; + $subtot_paye_total += $both['paye']['totalht']; + $subtot_paye_vat += $both['paye']['vat']; + } + + $diff = $x_coll_sum - $x_paye_sum; + $total = $total + $diff; + $subtotal = $subtotal + $diff; + + $var=!$var; + print ""; + print ''; + print "\n"; + print "\n"; + + $i++; + } + print '' . + '' . + '' . + '' . + ''; + '' . + '' . + ''; + ''; + print ''; + +} +else +{ + print ''; + print ''; +} + +print '
".$langs->trans("Year")." $year_current".$langs->trans("Income")."".$langs->trans("VATToPay")."".$langs->trans("Invoices")."".$langs->trans("Outcome")."".$langs->trans("VATToCollect")."".$langs->trans("Invoices")."".$langs->trans("TotalToPay")."
".$langs->trans("Quadri")." $q (".strftime("%b %Y",mktime(0,0,0,(($q-1)*3)+1,1,$y)).' - '.strftime("%b %Y",mktime(0,0,0,($q*3),1,$y)).")
$rate%".price($both['coll']['totalht'])."".price($both['coll']['vat'])."".$both['coll']['links']."".price($both['paye']['totalht'])."".price($both['paye']['vat'])."".$both['paye']['links']."
".price($diff)."
'.$langs->trans("Total").':'.price($subtot_coll_total).''.price($subtot_coll_vat).''.price($subtot_paye_total).''.price($subtot_paye_vat).''.price($total).'' . + '
'.$langs->trans("FeatureNotYetAvailable").'
'.$langs->trans("FeatureIsSupportedInInOutModeOnly").'
'; +echo ''; +echo ''; + + +$db->close(); + +llxFooter('$Date$ - $Revision$'); +?>