dolibarr  9.0.0
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@inodbox.com>
5  * Copyright (C) 2013 Antoine Iauch <aiauch@gpcsolutions.fr>
6  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
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 
27 require '../../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
31 
32 // Load translation files required by the page
33 $langs->load("accountancy");
34 
35 $socid = GETPOST('socid','int');
36 
37 // Security check
38 if ($user->societe_id > 0) $socid = $user->societe_id;
39 if (! empty($conf->comptabilite->enabled)) $result=restrictedArea($user,'compta','','','resultat');
40 if (! empty($conf->accounting->enabled)) $result=restrictedArea($user,'accounting','','','comptarapport');
41 
42 // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES')
43 $modecompta = $conf->global->ACCOUNTING_MODE;
44 if (GETPOST("modecompta")) $modecompta=GETPOST("modecompta");
45 
46 $sortorder=isset($_GET["sortorder"])?$_GET["sortorder"]:$_POST["sortorder"];
47 $sortfield=isset($_GET["sortfield"])?$_GET["sortfield"]:$_POST["sortfield"];
48 if (! $sortorder) $sortorder="asc";
49 if (! $sortfield) $sortfield="name";
50 
51 // Date range
52 $year=GETPOST("year");
53 $month=GETPOST("month");
54 $date_startyear = GETPOST("date_startyear");
55 $date_startmonth = GETPOST("date_startmonth");
56 $date_startday = GETPOST("date_startday");
57 $date_endyear = GETPOST("date_endyear");
58 $date_endmonth = GETPOST("date_endmonth");
59 $date_endday = GETPOST("date_endday");
60 if (empty($year))
61 {
62  $year_current = strftime("%Y",dol_now());
63  $month_current = strftime("%m",dol_now());
64  $year_start = $year_current;
65 } else {
66  $year_current = $year;
67  $month_current = strftime("%m",dol_now());
68  $year_start = $year;
69 }
70 $date_start=dol_mktime(0,0,0,$_REQUEST["date_startmonth"],$_REQUEST["date_startday"],$_REQUEST["date_startyear"]);
71 $date_end=dol_mktime(23,59,59,$_REQUEST["date_endmonth"],$_REQUEST["date_endday"],$_REQUEST["date_endyear"]);
72 // Quarter
73 if (empty($date_start) || empty($date_end)) // We define date_start and date_end
74 {
75  $q=GETPOST("q")?GETPOST("q"):0;
76  if ($q==0)
77  {
78  // We define date_start and date_end
79  $month_start=GETPOST("month")?GETPOST("month"):($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
80  $year_end=$year_start;
81  $month_end=$month_start;
82  if (! GETPOST("month")) // If month not forced
83  {
84  if (! GETPOST('year') && $month_start > $month_current)
85  {
86  $year_start--;
87  $year_end--;
88  }
89  $month_end=$month_start-1;
90  if ($month_end < 1) $month_end=12;
91  else $year_end++;
92  }
93  $date_start=dol_get_first_day($year_start,$month_start,false); $date_end=dol_get_last_day($year_end,$month_end,false);
94  }
95  if ($q==1) { $date_start=dol_get_first_day($year_start,1,false); $date_end=dol_get_last_day($year_start,3,false); }
96  if ($q==2) { $date_start=dol_get_first_day($year_start,4,false); $date_end=dol_get_last_day($year_start,6,false); }
97  if ($q==3) { $date_start=dol_get_first_day($year_start,7,false); $date_end=dol_get_last_day($year_start,9,false); }
98  if ($q==4) { $date_start=dol_get_first_day($year_start,10,false); $date_end=dol_get_last_day($year_start,12,false); }
99 }
100 else
101 {
102  // TODO We define q
103 }
104 // $date_start and $date_end are defined. We force $year_start and $nbofyear
105 $tmps=dol_getdate($date_start);
106 $year_start = $tmps['year'];
107 $tmpe=dol_getdate($date_end);
108 $year_end = $tmpe['year'];
109 $nbofyear = ($year_end - $year_start) + 1;
110 
111 $commonparams=array();
112 $commonparams['modecompta']=$modecompta;
113 $commonparams['sortorder'] = $sortorder;
114 $commonparams['sortfield'] = $sortfield;
115 
116 $headerparams = array();
117 $headerparams['date_startyear'] = $date_startyear;
118 $headerparams['date_startmonth'] = $date_startmonth;
119 $headerparams['date_startday'] = $date_startday;
120 $headerparams['date_endyear'] = $date_endyear;
121 $headerparams['date_endmonth'] = $date_endmonth;
122 $headerparams['date_endday'] = $date_endday;
123 $headerparams['q'] = $q;
124 
125 $tableparams = array();
126 $tableparams['search_categ'] = $selected_cat;
127 $tableparams['subcat'] = ($subcat === true)?'yes':'';
128 
129 // Adding common parameters
130 $allparams = array_merge($commonparams, $headerparams, $tableparams);
131 $headerparams = array_merge($commonparams, $headerparams);
132 $tableparams = array_merge($commonparams, $tableparams);
133 
134 foreach($allparams as $key => $value) {
135  $paramslink .= '&' . $key . '=' . $value;
136 }
137 
138 /*
139  * View
140  */
141 
142 llxHeader();
143 
144 $form=new Form($db);
145 
146 // TODO Report from bookkeeping not yet available, so we switch on report on business events
147 if ($modecompta=="BOOKKEEPING") $modecompta="CREANCES-DETTES";
148 if ($modecompta=="BOOKKEEPINGCOLLECTED") $modecompta="RECETTES-DEPENSES";
149 
150 // Show report header
151 if ($modecompta=="CREANCES-DETTES") {
152  $name=$langs->trans("Turnover").', '.$langs->trans("ByUserAuthorOfInvoice");
153  $calcmode=$langs->trans("CalcModeDebt");
154  //$calcmode.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year_start.'&modecompta=RECETTES-DEPENSES">','</a>').')';
155  $description=$langs->trans("RulesCADue");
156  if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description.= $langs->trans("DepositsAreNotIncluded");
157  else $description.= $langs->trans("DepositsAreIncluded");
158  $builddate=dol_now();
159  //$exportlink=$langs->trans("NotYetAvailable");
160 }
161 else if ($modecompta=="RECETTES-DEPENSES")
162 {
163  $name=$langs->trans("TurnoverCollected").', '.$langs->trans("ByUserAuthorOfInvoice");
164  $calcmode=$langs->trans("CalcModeEngagement");
165  //$calcmode.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year_start.'&modecompta=CREANCES-DETTES">','</a>').')';
166  $description=$langs->trans("RulesCAIn");
167  $description.= $langs->trans("DepositsAreIncluded");
168  $builddate=dol_now();
169  //$exportlink=$langs->trans("NotYetAvailable");
170 }
171 else if ($modecompta=="BOOKKEEPING")
172 {
173 
174 
175 }
176 else if ($modecompta=="BOOKKEEPINGCOLLECTED")
177 {
178 
179 
180 }
181 $period=$form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
182 if ($date_end == dol_time_plus_duree($date_start, 1, 'y') - 1) $periodlink='<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_start-1).'&modecompta='.$modecompta.'">'.img_previous().'</a> <a href="'.$_SERVER["PHP_SELF"].'?year='.($year_start+1).'&modecompta='.$modecompta.'">'.img_next().'</a>';
183 else $periodlink = '';
184 
185 $moreparam=array();
186 if (! empty($modecompta)) $moreparam['modecompta']=$modecompta;
187 
188 report_header($name,$namelink,$period,$periodlink,$description,$builddate,$exportlink,$moreparam,$calcmode);
189 
190 if (! empty($conf->accounting->enabled) && $modecompta != 'BOOKKEEPING')
191 {
192  print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);
193 }
194 
195 
196 $name=array();
197 
198 // Show array
199 print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
200 // Extra parameters management
201 foreach($headerparams as $key => $value)
202 {
203  print '<input type="hidden" name="'.$key.'" value="'.$value.'">';
204 }
205 
206 $catotal=0;
207 if ($modecompta == 'CREANCES-DETTES') {
208  $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";
209  $sql.= " FROM ".MAIN_DB_PREFIX."user as u";
210  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.fk_user_author = u.rowid";
211  $sql.= " WHERE f.fk_statut in (1,2)";
212  if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
213  $sql.= " AND f.type IN (0,1,2,5)";
214  } else {
215  $sql.= " AND f.type IN (0,1,2,3,5)";
216  }
217  if ($date_start && $date_end) {
218  $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
219  }
220 } else {
221  /*
222  * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
223  * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
224  */
225  $sql = "SELECT u.rowid as rowid, u.lastname as name, u.firstname as firstname, sum(pf.amount) as amount_ttc";
226  $sql.= " FROM ".MAIN_DB_PREFIX."user as u" ;
227  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.fk_user_author = u.rowid ";
228  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_facture = f.rowid";
229  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement as p ON p.rowid = pf.fk_paiement";
230  $sql.= " WHERE 1=1";
231  if ($date_start && $date_end) {
232  $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
233  }
234 }
235 $sql.= " AND f.entity = ".$conf->entity;
236 if ($socid) $sql.= " AND f.fk_soc = ".$socid;
237 $sql .= " GROUP BY u.rowid, u.lastname, u.firstname";
238 $sql .= " ORDER BY u.rowid";
239 
240 $result = $db->query($sql);
241 if ($result) {
242  $num = $db->num_rows($result);
243  $i=0;
244  while ($i < $num) {
245  $obj = $db->fetch_object($result);
246  $amount_ht[$obj->rowid] = $obj->amount;
247  $amount[$obj->rowid] = $obj->amount_ttc;
248  $name[$obj->rowid] = $obj->name.' '.$obj->firstname;
249  $catotal_ht+=$obj->amount;
250  $catotal+=$obj->amount_ttc;
251  $i++;
252  }
253 } else {
254  dol_print_error($db);
255 }
256 
257 // Adding old-version payments, non-bound by "paiement_facture" then without User
258 if ($modecompta != 'CREANCES-DETTES') {
259  $sql = "SELECT -1 as rowidx, '' as name, '' as firstname, sum(DISTINCT p.amount) as amount_ttc";
260  $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
261  $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
262  $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
263  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
264  $sql.= " WHERE pf.rowid IS NULL";
265  $sql.= " AND p.fk_bank = b.rowid";
266  $sql.= " AND b.fk_account = ba.rowid";
267  $sql.= " AND ba.entity IN (".getEntity('bank_account').")";
268  if ($date_start && $date_end) {
269  $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
270  }
271  $sql.= " GROUP BY rowidx, name, firstname";
272  $sql.= " ORDER BY rowidx";
273 
274  $result = $db->query($sql);
275  if ($result) {
276  $num = $db->num_rows($result);
277  $i=0;
278  while ($i < $num) {
279  $obj = $db->fetch_object($result);
280  $amount[$obj->rowidx] = $obj->amount_ttc;
281  $name[$obj->rowidx] = $obj->name.' '.$obj->firstname;
282  $catotal+=$obj->amount_ttc;
283  $i++;
284  }
285  } else {
286  dol_print_error($db);
287  }
288 }
289 
290 $morefilter='';
291 
292 print '<div class="div-table-responsive">';
293 print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
294 
295 print "<tr class=\"liste_titre\">";
297  $langs->trans("User"),
298  $_SERVER["PHP_SELF"],
299  "name",
300  "",
301  $paramslink,
302  "",
303  $sortfield,
304  $sortorder
305  );
306 if ($modecompta == 'CREANCES-DETTES') {
308  $langs->trans('AmountHT'),
309  $_SERVER["PHP_SELF"],
310  "amount_ht",
311  "",
312  $paramslink,
313  'align="right"',
314  $sortfield,
315  $sortorder
316  );
317  } else {
319 }
321  $langs->trans("AmountTTC"),
322  $_SERVER["PHP_SELF"],
323  "amount_ttc",
324  "",
325  $paramslink,
326  'align="right"',
327  $sortfield,
328  $sortorder
329  );
331  $langs->trans("Percentage"),
332  $_SERVER["PHP_SELF"],"amount_ttc",
333  "",
334  $paramslink,
335  'align="right"',
336  $sortfield,
337  $sortorder
338  );
340  $langs->trans("OtherStatistics"),
341  $_SERVER["PHP_SELF"],
342  "",
343  "",
344  "",
345  'align="center" width="20%"'
346  );
347 print "</tr>\n";
348 
349 if (count($amount)) {
350  $arrayforsort=$name;
351 
352  // We define arrayforsort
353  if ($sortfield == 'name' && $sortorder == 'asc') {
354  asort($name);
355  $arrayforsort=$name;
356  }
357  if ($sortfield == 'name' && $sortorder == 'desc') {
358  arsort($name);
359  $arrayforsort=$name;
360  }
361  if ($sortfield == 'amount_ht' && $sortorder == 'asc') {
362  asort($amount_ht);
363  $arrayforsort=$amount_ht;
364  }
365  if ($sortfield == 'amount_ht' && $sortorder == 'desc') {
366  arsort($amount_ht);
367  $arrayforsort=$amount_ht;
368  }
369  if ($sortfield == 'amount_ttc' && $sortorder == 'asc') {
370  asort($amount);
371  $arrayforsort=$amount;
372  }
373  if ($sortfield == 'amount_ttc' && $sortorder == 'desc') {
374  arsort($amount);
375  $arrayforsort=$amount;
376  }
377 
378  $i = 0;
379 
380  foreach($arrayforsort as $key => $value) {
381 
382  print '<tr class="oddeven">';
383 
384  // Third party
385  $fullname=$name[$key];
386  if ($key >= 0) {
387  $linkname='<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$key.'">'.img_object($langs->trans("ShowUser"),'user').' '.$fullname.'</a>';
388  } else {
389  $linkname=$langs->trans("PaymentsNotLinkedToUser");
390  }
391  print "<td>".$linkname."</td>\n";
392 
393  // Amount w/o VAT
394  print '<td align="right">';
395  if ($modecompta != 'CREANCES-DETTES')
396  {
397  if ($key > 0) {
398  print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid='.$key.'">';
399  } else {
400  print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid=-1">';
401  }
402  } else {
403  if ($key > 0) {
404  print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?userid='.$key.'">';
405  } else {
406  print '<a href="#">';
407  }
408  print price($amount_ht[$key]);
409  }
410  print '</td>';
411 
412  // Amount with VAT
413  print '<td align="right">';
414  if ($modecompta != 'CREANCES-DETTES') {
415  if ($key > 0) {
416  print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid='.$key.'">';
417  } else {
418  print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid=-1">';
419  }
420  } else {
421  if ($key > 0) {
422  print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?userid='.$key.'">';
423  } else {
424  print '<a href="#">';
425  }
426  }
427  print price($amount[$key]);
428  print '</td>';
429 
430  // Percent
431  print '<td align="right">'.($catotal > 0 ? round(100 * $amount[$key] / $catotal,2).'%' : '&nbsp;').'</td>';
432 
433  // Other stats
434  print '<td align="center">';
435  if (! empty($conf->propal->enabled) && $key>0) {
436  print '&nbsp;<a href="'.DOL_URL_ROOT.'/comm/propal/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("ProposalStats"),"stats").'</a>&nbsp;';
437  }
438  if (! empty($conf->commande->enabled) && $key>0) {
439  print '&nbsp;<a href="'.DOL_URL_ROOT.'/commande/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("OrderStats"),"stats").'</a>&nbsp;';
440  }
441  if (! empty($conf->facture->enabled) && $key>0) {
442  print '&nbsp;<a href="'.DOL_URL_ROOT.'/compta/facture/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("InvoiceStats"),"stats").'</a>&nbsp;';
443  }
444  print '</td>';
445  print "</tr>\n";
446  $i++;
447  }
448 
449  // Total
450  print '<tr class="liste_total">';
451  print '<td>'.$langs->trans("Total").'</td>';
452  if ($modecompta != 'CREANCES-DETTES') {
453  print '<td colspan="1"></td>';
454  } else {
455  print '<td align="right">'.price($catotal_ht).'</td>';
456  }
457  print '<td align="right">'.price($catotal).'</td>';
458  print '<td>&nbsp;</td>';
459  print '<td>&nbsp;</td>';
460  print '</tr>';
461 
462  $db->free($result);
463 }
464 
465 print "</table>";
466 print '</div>';
467 print '</form>';
468 
469 // End of page
470 llxFooter();
471 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
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:453
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
img_next($titlealt='default', $moreatt='')
Show next logo.
Class to manage generation of HTML components Only common components must be here.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='')
Show information for admin users or standard users.
llxHeader()
Empty header.
Definition: wrapper.php:44
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:467
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="")
Show title line of an array.
dol_getdate($timestamp, $fast=false)
Return an array with locale date info.
img_previous($titlealt='default', $moreatt='')
Show previous logo.
report_header($reportname, $notused, $period, $periodlink, $description, $builddate, $exportlink='', $moreparam=array(), $calcmode='', $varlink='')
Show header of a report.
Definition: report.lib.php:41
dol_time_plus_duree($time, $duration_value, $duration_unit)
Add a delay to a date.
Definition: date.lib.php:116
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)