dolibarr  7.0.0-beta
quadri.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
4  * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
5  * Copyright (C) 2006 Yannick Warnier <ywarnier@beeznest.org>
6  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
29 require '../../main.inc.php';
30 require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
31 
32 $year = GETPOST('year', 'int');
33 if ($year == 0 )
34 {
35  $year_current = strftime("%Y",time());
36  $year_start = $year_current;
37 } else {
38  $year_current = $year;
39  $year_start = $year;
40 }
41 
42 // Security check
43 $socid = isset($_GET["socid"])?$_GET["socid"]:'';
44 if ($user->societe_id) $socid=$user->societe_id;
45 $result = restrictedArea($user, 'tax', '', '', 'charges');
46 
47 
59 function tva_coll($db,$y,$q)
60 {
61  global $conf;
62 
63  if ($conf->global->ACCOUNTING_MODE == "CREANCES-DETTES")
64  {
65  // if vat paid on due invoices
66  $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";
67  $sql.= " FROM ".MAIN_DB_PREFIX."facture as f";
68  $sql.= ", ".MAIN_DB_PREFIX."facturedet as d" ;
69  $sql.= ", ".MAIN_DB_PREFIX."societe as s";
70  $sql.= " WHERE f.fk_soc = s.rowid";
71  $sql.= " AND f.entity = ".$conf->entity;
72  $sql.= " AND f.fk_statut in (1,2)";
73  $sql.= " AND f.rowid = d.fk_facture ";
74  $sql.= " AND date_format(f.datef,'%Y') = '".$y."'";
75  $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3);
76  $sql.= " AND date_format(f.datef,'%m') <= ".($q*3).")";
77  $sql.= " ORDER BY rate, facid";
78 
79  }
80  else
81  {
82  // if vat paid on paiments
83  }
84 
85  $resql = $db->query($sql);
86 
87  if ($resql)
88  {
89  $list = array();
90  $rate = -1;
91  while($assoc = $db->fetch_array($resql))
92  {
93  if($assoc['rate'] != $rate){ //new rate
94  $list[$assoc['rate']]['totalht'] = $assoc['totalht'];
95  $list[$assoc['rate']]['vat'] = $assoc['amount'];
96  $list[$assoc['rate']]['facid'][] = $assoc['facid'];
97  $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
98  }else{
99  $list[$assoc['rate']]['totalht'] += $assoc['totalht'];
100  $list[$assoc['rate']]['vat'] += $assoc['amount'];
101  if(!in_array($assoc['facid'],$list[$assoc['rate']]['facid'])){
102  $list[$assoc['rate']]['facid'][] = $assoc['facid'];
103  $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
104  }
105  }
106  $rate = $assoc['rate'];
107  }
108  return $list;
109  }
110  else
111  {
112  dol_print_error($db);
113  }
114 }
115 
116 
127 function tva_paye($db, $y,$q)
128 {
129  global $conf;
130 
131  if ($conf->global->ACCOUNTING_MODE == "CREANCES-DETTES")
132  {
133  // Si on paye la tva sur les factures dues (non brouillon)
134  $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";
135  $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
136  $sql.= ", ".MAIN_DB_PREFIX."facture_fourn_det as d" ;
137  $sql.= ", ".MAIN_DB_PREFIX."societe as s";
138  $sql.= " WHERE f.fk_soc = s.rowid";
139  $sql.= " AND f.entity = ".$conf->entity;
140  $sql.= " AND f.fk_statut = 1 ";
141  $sql.= " AND f.rowid = d.fk_facture_fourn ";
142  $sql.= " AND date_format(f.datef,'%Y') = '".$y."'";
143  $sql.= " AND (round(date_format(f.datef,'%m')) > ".(($q-1)*3);
144  $sql.= " AND round(date_format(f.datef,'%m')) <= ".($q*3).")";
145  $sql.= " ORDER BY rate, facid ";
146  }
147  else
148  {
149  // Si on paye la tva sur les payments
150  }
151 
152  $resql = $db->query($sql);
153  if ($resql)
154  {
155  $list = array();
156  $rate = -1;
157  while($assoc = $db->fetch_array($resql))
158  {
159  if($assoc['rate'] != $rate){ //new rate
160  $list[$assoc['rate']]['totalht'] = $assoc['totalht'];
161  $list[$assoc['rate']]['vat'] = $assoc['amount'];
162  $list[$assoc['rate']]['facid'][] = $assoc['facid'];
163  $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
164  }else{
165  $list[$assoc['rate']]['totalht'] += $assoc['totalht'];
166  $list[$assoc['rate']]['vat'] += $assoc['amount'];
167  if(!in_array($assoc['facid'],$list[$assoc['rate']]['facid'])){
168  $list[$assoc['rate']]['facid'][] = $assoc['facid'];
169  $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
170  }
171  }
172  $rate = $assoc['rate'];
173  }
174  return $list;
175 
176  }
177  else
178  {
179  dol_print_error($db);
180  }
181 }
182 
183 
188 llxHeader();
189 
190 $textprevyear="<a href=\"quadri.php?year=" . ($year_current-1) . "\">".img_previous()."</a>";
191 $textnextyear=" <a href=\"quadri.php?year=" . ($year_current+1) . "\">".img_next()."</a>";
192 
193 print load_fiche_titre($langs->trans("VAT"),"$textprevyear ".$langs->trans("Year")." $year_start $textnextyear");
194 
195 
196 echo '<table width="100%">';
197 echo '<tr><td>';
198 print load_fiche_titre($langs->trans("VATSummary"));
199 echo '</td></tr>';
200 
201 echo '<tr>';
202 
203 print "<table class=\"noborder\" width=\"100%\">";
204 print "<tr class=\"liste_titre\">";
205 print "<td width=\"20%\">".$langs->trans("Year")." $year_current</td>";
206 print "<td align=\"right\">".$langs->trans("Income")."</td>";
207 print "<td align=\"right\">".$langs->trans("VATToPay")."</td>";
208 print "<td align=\"right\">".$langs->trans("Invoices")."</td>";
209 print "<td align=\"right\">".$langs->trans("Outcome")."</td>";
210 print "<td align=\"right\">".$langs->trans("VATToCollect")."</td>";
211 print "<td align=\"right\">".$langs->trans("Invoices")."</td>";
212 print "<td align=\"right\">".$langs->trans("TotalToPay")."</td>";
213 print "</tr>\n";
214 
215 if ($conf->global->ACCOUNTING_MODE == "CREANCES-DETTES")
216 {
217  $y = $year_current;
218 
219  $total = 0; $subtotal = 0;
220  $i=0;
221  $subtot_coll_total = 0;
222  $subtot_coll_vat = 0;
223  $subtot_paye_total = 0;
224  $subtot_paye_vat = 0;
225  for ($q = 1 ; $q <= 4 ; $q++) {
226  print "<tr class=\"liste_titre\"><td colspan=\"8\">".$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").")</td></tr>";
227  $var=true;
228 
229  $x_coll = tva_coll($db, $y, $q);
230  $x_paye = tva_paye($db, $y, $q);
231  $x_both = array();
232  //now, from these two arrays, get another array with one rate per line
233  foreach(array_keys($x_coll) as $my_coll_rate){
234  $x_both[$my_coll_rate]['coll']['totalht'] = $x_coll[$my_coll_rate]['totalht'];
235  $x_both[$my_coll_rate]['coll']['vat'] = $x_coll[$my_coll_rate]['vat'];
236  $x_both[$my_coll_rate]['paye']['totalht'] = 0;
237  $x_both[$my_coll_rate]['paye']['vat'] = 0;
238  $x_both[$my_coll_rate]['coll']['links'] = '';
239  foreach($x_coll[$my_coll_rate]['facid'] as $id=>$dummy){
240  $x_both[$my_coll_rate]['coll']['links'] .= '<a href="'.DOL_URL_ROOT.'/compta/facture/card.php?facid='.$x_coll[$my_coll_rate]['facid'][$id].'" title="'.$x_coll[$my_coll_rate]['facnum'][$id].'">..'.substr($x_coll[$my_coll_rate]['facnum'][$id],-2).'</a> ';
241  }
242  }
243  foreach(array_keys($x_paye) as $my_paye_rate){
244  $x_both[$my_paye_rate]['paye']['totalht'] = $x_paye[$my_paye_rate]['totalht'];
245  $x_both[$my_paye_rate]['paye']['vat'] = $x_paye[$my_paye_rate]['vat'];
246  if(!isset($x_both[$my_paye_rate]['coll']['totalht'])){
247  $x_both[$my_paye_rate]['coll']['total_ht'] = 0;
248  $x_both[$my_paye_rate]['coll']['vat'] = 0;
249  }
250  $x_both[$my_paye_rate]['paye']['links'] = '';
251  foreach($x_paye[$my_paye_rate]['facid'] as $id=>$dummy){
252  $x_both[$my_paye_rate]['paye']['links'] .= '<a href="../../fourn/facture/card.php?facid='.$x_paye[$my_paye_rate]['facid'][$id].'" title="'.$x_paye[$my_paye_rate]['facnum'][$id].'">..'.substr($x_paye[$my_paye_rate]['facnum'][$id],-2).'</a> ';
253  }
254  }
255  //now we have an array (x_both) indexed by rates for coll and paye
256 
257  $x_coll_sum = 0;
258  $x_coll_ht = 0;
259  $x_paye_sum = 0;
260  $x_paye_ht = 0;
261  foreach($x_both as $rate => $both){
262 
263  print '<tr class="oddeven">';
264  print "<td>$rate%</td>";
265  print "<td class=\"nowrap\" align=\"right\">".price($both['coll']['totalht'])."</td>";
266  print "<td class=\"nowrap\" align=\"right\">".price($both['coll']['vat'])."</td>";
267  print "<td align=\"right\">".$both['coll']['links']."</td>";
268  print "<td class=\"nowrap\" align=\"right\">".price($both['paye']['totalht'])."</td>";
269  print "<td class=\"nowrap\" align=\"right\">".price($both['paye']['vat'])."</td>";
270  print "<td align=\"right\">".$both['paye']['links']."</td>";
271  print "<td></td>";
272  print "</tr>";
273  $x_coll_sum += $both['coll']['vat'];
274  $x_paye_sum += $both['paye']['vat'];
275  $subtot_coll_total += $both['coll']['totalht'];
276  $subtot_coll_vat += $both['coll']['vat'];
277  $subtot_paye_total += $both['paye']['totalht'];
278  $subtot_paye_vat += $both['paye']['vat'];
279  }
280 
281  $diff = $x_coll_sum - $x_paye_sum;
282  $total = $total + $diff;
283  $subtotal = $subtotal + $diff;
284 
285 
286  print '<tr class="oddeven">';
287  print '<td colspan="7"></td>';
288  print "<td class=\"nowrap\" align=\"right\">".price($diff)."</td>\n";
289  print "</tr>\n";
290 
291  $i++;
292  }
293  print '<tr class="liste_total">';
294  print '<td align="right">'.$langs->trans("Total").':</td>';
295  print '<td class="nowrap" align="right">'.price($subtot_coll_total).'</td>';
296  print '<td class="nowrap" align="right">'.price($subtot_coll_vat).'</td>';
297  print '<td></td>';
298  print '<td class="nowrap" align="right">'.price($subtot_paye_total).'</td>';
299  print '<td class="nowrap" align="right">'.price($subtot_paye_vat).'</td>';
300  print '<td></td>';
301  print '<td class="nowrap" align="right"><b>'.price($total).'</b>';
302  print '</td>';
303  print '</tr>';
304 
305 }
306 else
307 {
308  print '<tr><td colspan="5">'.$langs->trans("FeatureNotYetAvailable").'</td></tr>';
309  print '<tr><td colspan="5">'.$langs->trans("FeatureIsSupportedInInOutModeOnly").'</td></tr>';
310 }
311 
312 print '</table>';
313 echo '</td></tr>';
314 echo '</table>';
315 
316 llxFooter();
317 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
tva_coll($db, $y, $q)
Gets VAT to collect for the given month of the given year The function gets the VAT in split results...
Definition: quadri.php:59
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
img_previous($titlealt= 'default', $moreatt='')
Show previous logo.
img_next($titlealt= 'default', $moreatt='')
Show next logo.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
tva_paye($db, $y, $q)
Gets VAT to pay for the given month of the given year The function gets the VAT in split results...
Definition: quadri.php:127
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id=0, $morecssontable='', $morehtmlcenter='')
Load a title with picto.
llxHeader()
Empty header.
Definition: wrapper.php:46
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
print
Draft customers invoices.
Definition: index.php:91
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $objcanvas=null)
Check permissions of a user to show a page and an object.