* Copyright (C) 2004 Eric Seigne * Copyright (C) 2004-2008 Laurent Destailleur * Copyright (C) 2006 Yannick Warnier * Copyright (C) 2005-2009 Regis Houssin * * 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 3 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, see . */ /** * \file htdocs/compta/tva/quadri.php * \ingroup tax * \brief Trimestrial page * TODO Deal with recurrent invoices as well */ require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/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; } // Security check $socid = isset($_GET["socid"])?$_GET["socid"]:''; if ($user->societe_id) $socid=$user->societe_id; $result = restrictedArea($user, 'tax', '', '', 'charges'); /** * 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 DoliDB $db Database handler * @param int $y Year * @param int $q Year quarter (1-4) * @return array */ function tva_coll($db,$y,$q) { global $conf; if ($conf->global->ACCOUNTING_MODE == "CREANCES-DETTES") { // if vat paid on due invoices $sql = "SELECT d.fk_facture as facid, f.facnumber as facnum, d.tva_tx 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.= ", ".MAIN_DB_PREFIX."societe as s"; $sql.= " WHERE f.fk_soc = s.rowid"; $sql.= " AND f.entity = ".$conf->entity; $sql.= " AND f.fk_statut in (1,2)"; $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); $sql.= " AND date_format(f.datef,'%m') <= ".($q*3).")"; $sql.= " ORDER BY rate, facid"; } else { // if vat paid 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 { dol_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 DoliDB $db Database handler object * @param int $y Year * @param int $q Year quarter (1-4) * @return array */ function tva_paye($db, $y,$q) { global $conf; if ($conf->global->ACCOUNTING_MODE == "CREANCES-DETTES") { // Si on paye la tva sur les factures dues (non brouillon) $sql = "SELECT d.fk_facture_fourn as facid, f.ref_supplier as facnum, d.tva_tx 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.= ", ".MAIN_DB_PREFIX."societe as s"; $sql.= " WHERE f.fk_soc = s.rowid"; $sql.= " AND f.entity = ".$conf->entity; $sql.= " AND f.fk_statut = 1 "; $sql.= " AND f.rowid = d.fk_facture_fourn "; $sql.= " AND date_format(f.datef,'%Y') = '".$y."'"; $sql.= " AND (round(date_format(f.datef,'%m')) > ".(($q-1)*3); $sql.= " AND round(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 { dol_print_error($db); } } /** * View */ 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->global->ACCOUNTING_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 ''; print ''; print ''; print ''; print ''; print ''; print ''; 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 (".dol_print_date(dol_mktime(0,0,0,(($q-1)*3)+1,1,$y),"%b %Y").' - '.dol_print_date(dol_mktime(0,0,0,($q*3),1,$y),"%b %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).''; print '
'.$langs->trans("FeatureNotYetAvailable").'
'.$langs->trans("FeatureIsSupportedInInOutModeOnly").'
'; echo ''; echo ''; $db->close(); llxFooter();