dolibarr  7.0.0-beta
cabyuser.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
5  * Copyright (C) 2013 Antoine Iauch <aiauch@gpcsolutions.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
26 require '../../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
30 
31 $langs->load("accountancy");
32 
33 $socid = GETPOST('socid','int');
34 
35 // Security check
36 if ($user->societe_id > 0) $socid = $user->societe_id;
37 if (! empty($conf->comptabilite->enabled)) $result=restrictedArea($user,'compta','','','resultat');
38 if (! empty($conf->accounting->enabled)) $result=restrictedArea($user,'accounting','','','comptarapport');
39 
40 // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES')
41 $modecompta = $conf->global->ACCOUNTING_MODE;
42 if (GETPOST("modecompta")) $modecompta=GETPOST("modecompta");
43 
44 $sortorder=isset($_GET["sortorder"])?$_GET["sortorder"]:$_POST["sortorder"];
45 $sortfield=isset($_GET["sortfield"])?$_GET["sortfield"]:$_POST["sortfield"];
46 if (! $sortorder) $sortorder="asc";
47 if (! $sortfield) $sortfield="name";
48 
49 // Date range
50 $year=GETPOST("year");
51 $month=GETPOST("month");
52 $date_startyear = GETPOST("date_startyear");
53 $date_startmonth = GETPOST("date_startmonth");
54 $date_startday = GETPOST("date_startday");
55 $date_endyear = GETPOST("date_endyear");
56 $date_endmonth = GETPOST("date_endmonth");
57 $date_endday = GETPOST("date_endday");
58 if (empty($year))
59 {
60  $year_current = strftime("%Y",dol_now());
61  $month_current = strftime("%m",dol_now());
62  $year_start = $year_current;
63 } else {
64  $year_current = $year;
65  $month_current = strftime("%m",dol_now());
66  $year_start = $year;
67 }
68 $date_start=dol_mktime(0,0,0,$_REQUEST["date_startmonth"],$_REQUEST["date_startday"],$_REQUEST["date_startyear"]);
69 $date_end=dol_mktime(23,59,59,$_REQUEST["date_endmonth"],$_REQUEST["date_endday"],$_REQUEST["date_endyear"]);
70 // Quarter
71 if (empty($date_start) || empty($date_end)) // We define date_start and date_end
72 {
73  $q=GETPOST("q")?GETPOST("q"):0;
74  if ($q==0)
75  {
76  // We define date_start and date_end
77  $month_start=GETPOST("month")?GETPOST("month"):($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
78  $year_end=$year_start;
79  $month_end=$month_start;
80  if (! GETPOST("month")) // If month not forced
81  {
82  if (! GETPOST('year') && $month_start > $month_current)
83  {
84  $year_start--;
85  $year_end--;
86  }
87  $month_end=$month_start-1;
88  if ($month_end < 1) $month_end=12;
89  else $year_end++;
90  }
91  $date_start=dol_get_first_day($year_start,$month_start,false); $date_end=dol_get_last_day($year_end,$month_end,false);
92  }
93  if ($q==1) { $date_start=dol_get_first_day($year_start,1,false); $date_end=dol_get_last_day($year_start,3,false); }
94  if ($q==2) { $date_start=dol_get_first_day($year_start,4,false); $date_end=dol_get_last_day($year_start,6,false); }
95  if ($q==3) { $date_start=dol_get_first_day($year_start,7,false); $date_end=dol_get_last_day($year_start,9,false); }
96  if ($q==4) { $date_start=dol_get_first_day($year_start,10,false); $date_end=dol_get_last_day($year_start,12,false); }
97 }
98 else
99 {
100  // TODO We define q
101 }
102 
103 $commonparams=array();
104 $commonparams['modecompta']=$modecompta;
105 $commonparams['sortorder'] = $sortorder;
106 $commonparams['sortfield'] = $sortfield;
107 
108 $headerparams = array();
109 $headerparams['date_startyear'] = $date_startyear;
110 $headerparams['date_startmonth'] = $date_startmonth;
111 $headerparams['date_startday'] = $date_startday;
112 $headerparams['date_endyear'] = $date_endyear;
113 $headerparams['date_endmonth'] = $date_endmonth;
114 $headerparams['date_endday'] = $date_endday;
115 $headerparams['q'] = $q;
116 
117 $tableparams = array();
118 $tableparams['search_categ'] = $selected_cat;
119 $tableparams['subcat'] = ($subcat === true)?'yes':'';
120 
121 // Adding common parameters
122 $allparams = array_merge($commonparams, $headerparams, $tableparams);
123 $headerparams = array_merge($commonparams, $headerparams);
124 $tableparams = array_merge($commonparams, $tableparams);
125 
126 foreach($allparams as $key => $value) {
127  $paramslink .= '&' . $key . '=' . $value;
128 }
129 
130 /*
131  * View
132  */
133 
134 llxHeader();
135 
136 $form=new Form($db);
137 
138 // Show report header
139 if ($modecompta=="CREANCES-DETTES") {
140  $name=$langs->trans("SalesTurnover").', '.$langs->trans("ByUserAuthorOfInvoice");
141  $calcmode=$langs->trans("CalcModeDebt");
142  $calcmode.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&modecompta=RECETTES-DEPENSES">','</a>').')';
143  $period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',0,0,0,'',1,0,1);
144  //$periodlink="<a href='".$_SERVER["PHP_SELF"]."?year=".($year-1)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year+1)."&modecompta=".$modecompta."'>".img_next()."</a>";
145  $description=$langs->trans("RulesCADue");
146  if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description.= $langs->trans("DepositsAreNotIncluded");
147  else $description.= $langs->trans("DepositsAreIncluded");
148  $builddate=dol_now();
149  //$exportlink=$langs->trans("NotYetAvailable");
150 } else {
151  $name=$langs->trans("SalesTurnover").', '.$langs->trans("ByUserAuthorOfInvoice");
152  $calcmode=$langs->trans("CalcModeEngagement");
153  $calcmode.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&modecompta=CREANCES-DETTES">','</a>').')';
154  $period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',0,0,0,'',1,0,1);
155  //$periodlink="<a href='".$_SERVER["PHP_SELF"]."?year=".($year-1)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year+1)."&modecompta=".$modecompta."'>".img_next()."</a>";
156  $description=$langs->trans("RulesCAIn");
157  $description.= $langs->trans("DepositsAreIncluded");
158  $builddate=dol_now();
159  //$exportlink=$langs->trans("NotYetAvailable");
160 }
161 $moreparam=array();
162 if (! empty($modecompta)) $moreparam['modecompta']=$modecompta;
163 
164 report_header($name,$namelink,$period,$periodlink,$description,$builddate,$exportlink,$moreparam,$calcmode);
165 
166 if (! empty($conf->accounting->enabled) && $modecompta != 'BOOKKEEPING')
167 {
168  print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);
169 }
170 
171 
172 $name=array();
173 
174 // Show array
175 print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
176 // Extra parameters management
177 foreach($headerparams as $key => $value)
178 {
179  print '<input type="hidden" name="'.$key.'" value="'.$value.'">';
180 }
181 
182 $catotal=0;
183 if ($modecompta == 'CREANCES-DETTES') {
184  $sql = "SELECT u.rowid as rowid, u.lastname as name, u.firstname as firstname, sum(f.total) as amount, sum(f.total_ttc) as amount_ttc";
185  $sql.= " FROM ".MAIN_DB_PREFIX."user as u";
186  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.fk_user_author = u.rowid";
187  $sql.= " WHERE f.fk_statut in (1,2)";
188  if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
189  $sql.= " AND f.type IN (0,1,2,5)";
190  } else {
191  $sql.= " AND f.type IN (0,1,2,3,5)";
192  }
193  if ($date_start && $date_end) {
194  $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
195  }
196 } else {
197  /*
198  * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
199  * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
200  */
201  $sql = "SELECT u.rowid as rowid, u.lastname as name, u.firstname as firstname, sum(pf.amount) as amount_ttc";
202  $sql.= " FROM ".MAIN_DB_PREFIX."user as u" ;
203  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.fk_user_author = u.rowid ";
204  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_facture = f.rowid";
205  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement as p ON p.rowid = pf.fk_paiement";
206  $sql.= " WHERE 1=1";
207  if ($date_start && $date_end) {
208  $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
209  }
210 }
211 $sql.= " AND f.entity = ".$conf->entity;
212 if ($socid) $sql.= " AND f.fk_soc = ".$socid;
213 $sql .= " GROUP BY u.rowid, u.lastname, u.firstname";
214 $sql .= " ORDER BY u.rowid";
215 
216 $result = $db->query($sql);
217 if ($result) {
218  $num = $db->num_rows($result);
219  $i=0;
220  while ($i < $num) {
221  $obj = $db->fetch_object($result);
222  $amount_ht[$obj->rowid] = $obj->amount;
223  $amount[$obj->rowid] = $obj->amount_ttc;
224  $name[$obj->rowid] = $obj->name.' '.$obj->firstname;
225  $catotal_ht+=$obj->amount;
226  $catotal+=$obj->amount_ttc;
227  $i++;
228  }
229 } else {
230  dol_print_error($db);
231 }
232 
233 // Adding old-version payments, non-bound by "paiement_facture" then without User
234 if ($modecompta != 'CREANCES-DETTES') {
235  $sql = "SELECT -1 as rowidx, '' as name, '' as firstname, sum(DISTINCT p.amount) as amount_ttc";
236  $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
237  $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
238  $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
239  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
240  $sql.= " WHERE pf.rowid IS NULL";
241  $sql.= " AND p.fk_bank = b.rowid";
242  $sql.= " AND b.fk_account = ba.rowid";
243  $sql.= " AND ba.entity IN (".getEntity('bank_account').")";
244  if ($date_start && $date_end) {
245  $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
246  }
247  $sql.= " GROUP BY rowidx, name, firstname";
248  $sql.= " ORDER BY rowidx";
249 
250  $result = $db->query($sql);
251  if ($result) {
252  $num = $db->num_rows($result);
253  $i=0;
254  while ($i < $num) {
255  $obj = $db->fetch_object($result);
256  $amount[$obj->rowidx] = $obj->amount_ttc;
257  $name[$obj->rowidx] = $obj->name.' '.$obj->firstname;
258  $catotal+=$obj->amount_ttc;
259  $i++;
260  }
261  } else {
262  dol_print_error($db);
263  }
264 }
265 
266 $morefilter='';
267 
268 print '<div class="div-table-responsive">';
269 print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
270 
271 print "<tr class=\"liste_titre\">";
273  $langs->trans("User"),
274  $_SERVER["PHP_SELF"],
275  "name",
276  "",
277  $paramslink,
278  "",
279  $sortfield,
280  $sortorder
281  );
282 if ($modecompta == 'CREANCES-DETTES') {
284  $langs->trans('AmountHT'),
285  $_SERVER["PHP_SELF"],
286  "amount_ht",
287  "",
288  $paramslink,
289  'align="right"',
290  $sortfield,
291  $sortorder
292  );
293  } else {
295 }
297  $langs->trans("AmountTTC"),
298  $_SERVER["PHP_SELF"],
299  "amount_ttc",
300  "",
301  $paramslink,
302  'align="right"',
303  $sortfield,
304  $sortorder
305  );
307  $langs->trans("Percentage"),
308  $_SERVER["PHP_SELF"],"amount_ttc",
309  "",
310  $paramslink,
311  'align="right"',
312  $sortfield,
313  $sortorder
314  );
316  $langs->trans("OtherStatistics"),
317  $_SERVER["PHP_SELF"],
318  "",
319  "",
320  "",
321  'align="center" width="20%"'
322  );
323 print "</tr>\n";
324 $var=true;
325 
326 if (count($amount)) {
327  $arrayforsort=$name;
328 
329  // We define arrayforsort
330  if ($sortfield == 'name' && $sortorder == 'asc') {
331  asort($name);
332  $arrayforsort=$name;
333  }
334  if ($sortfield == 'name' && $sortorder == 'desc') {
335  arsort($name);
336  $arrayforsort=$name;
337  }
338  if ($sortfield == 'amount_ht' && $sortorder == 'asc') {
339  asort($amount_ht);
340  $arrayforsort=$amount_ht;
341  }
342  if ($sortfield == 'amount_ht' && $sortorder == 'desc') {
343  arsort($amount_ht);
344  $arrayforsort=$amount_ht;
345  }
346  if ($sortfield == 'amount_ttc' && $sortorder == 'asc') {
347  asort($amount);
348  $arrayforsort=$amount;
349  }
350  if ($sortfield == 'amount_ttc' && $sortorder == 'desc') {
351  arsort($amount);
352  $arrayforsort=$amount;
353  }
354 
355  $i = 0;
356 
357  foreach($arrayforsort as $key => $value) {
358 
359  print '<tr class="oddeven">';
360 
361  // Third party
362  $fullname=$name[$key];
363  if ($key >= 0) {
364  $linkname='<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$key.'">'.img_object($langs->trans("ShowUser"),'user').' '.$fullname.'</a>';
365  } else {
366  $linkname=$langs->trans("PaymentsNotLinkedToUser");
367  }
368  print "<td>".$linkname."</td>\n";
369 
370  // Amount w/o VAT
371  print '<td align="right">';
372  if ($modecompta != 'CREANCES-DETTES')
373  {
374  if ($key > 0) {
375  print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid='.$key.'">';
376  } else {
377  print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid=-1">';
378  }
379  } else {
380  if ($key > 0) {
381  print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?userid='.$key.'">';
382  } else {
383  print '<a href="#">';
384  }
385  print price($amount_ht[$key]);
386  }
387  print '</td>';
388 
389  // Amount with VAT
390  print '<td align="right">';
391  if ($modecompta != 'CREANCES-DETTES') {
392  if ($key > 0) {
393  print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid='.$key.'">';
394  } else {
395  print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid=-1">';
396  }
397  } else {
398  if ($key > 0) {
399  print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?userid='.$key.'">';
400  } else {
401  print '<a href="#">';
402  }
403  }
404  print price($amount[$key]);
405  print '</td>';
406 
407  // Percent
408  print '<td align="right">'.($catotal > 0 ? round(100 * $amount[$key] / $catotal,2).'%' : '&nbsp;').'</td>';
409 
410  // Other stats
411  print '<td align="center">';
412  if (! empty($conf->propal->enabled) && $key>0) {
413  print '&nbsp;<a href="'.DOL_URL_ROOT.'/comm/propal/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("ProposalStats"),"stats").'</a>&nbsp;';
414  }
415  if (! empty($conf->commande->enabled) && $key>0) {
416  print '&nbsp;<a href="'.DOL_URL_ROOT.'/commande/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("OrderStats"),"stats").'</a>&nbsp;';
417  }
418  if (! empty($conf->facture->enabled) && $key>0) {
419  print '&nbsp;<a href="'.DOL_URL_ROOT.'/compta/facture/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("InvoiceStats"),"stats").'</a>&nbsp;';
420  }
421  print '</td>';
422  print "</tr>\n";
423  $i++;
424  }
425 
426  // Total
427  print '<tr class="liste_total">';
428  print '<td>'.$langs->trans("Total").'</td>';
429  if ($modecompta != 'CREANCES-DETTES') {
430  print '<td colspan="1"></td>';
431  } else {
432  print '<td align="right">'.price($catotal_ht).'</td>';
433  }
434  print '<td align="right">'.price($catotal).'</td>';
435  print '<td>&nbsp;</td>';
436  print '<td>&nbsp;</td>';
437  print '</tr>';
438 
439  $db->free($result);
440 }
441 
442 print "</table>";
443 print '</div>';
444 print '</form>';
445 
446 
447 llxFooter();
448 
449 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
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...
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:445
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
if(empty($reshook)) $form
View.
Definition: perms.php:103
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage generation of HTML components Only common components must be here.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1')
Show information for admin users or standard users.
llxHeader()
Empty header.
Definition: wrapper.php:46
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_now($mode='gmt')
Return date for now.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:459
print
Draft customers invoices.
Definition: index.php:91
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="")
Show title line of an array.
report_header($reportname, $notused, $period, $periodlink, $description, $builddate, $exportlink='', $moreparam=array(), $calcmode='', $varlink='')
Show header of a report.
Definition: report.lib.php:41
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.