cabyuser.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. /**
  21. * \file htdocs/compta/stats/cabyuser.php
  22. * \brief Page reporting Salesover by user
  23. */
  24. require '../../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  28. $socid = GETPOST('socid','int');
  29. // Security check
  30. if ($user->societe_id > 0) $socid = $user->societe_id;
  31. if (! empty($conf->comptabilite->enabled)) $result=restrictedArea($user,'compta','','','resultat');
  32. if (! empty($conf->accounting->enabled)) $result=restrictedArea($user,'accounting','','','comptarapport');
  33. // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES')
  34. $modecompta = $conf->global->ACCOUNTING_MODE;
  35. if (GETPOST("modecompta")) $modecompta=GETPOST("modecompta");
  36. $sortorder=isset($_GET["sortorder"])?$_GET["sortorder"]:$_POST["sortorder"];
  37. $sortfield=isset($_GET["sortfield"])?$_GET["sortfield"]:$_POST["sortfield"];
  38. if (! $sortorder) $sortorder="asc";
  39. if (! $sortfield) $sortfield="name";
  40. // Date range
  41. $year=GETPOST("year");
  42. $month=GETPOST("month");
  43. $date_startyear = GETPOST("date_startyear");
  44. $date_startmonth = GETPOST("date_startmonth");
  45. $date_startday = GETPOST("date_startday");
  46. $date_endyear = GETPOST("date_endyear");
  47. $date_endmonth = GETPOST("date_endmonth");
  48. $date_endday = GETPOST("date_endday");
  49. if (empty($year))
  50. {
  51. $year_current = strftime("%Y",dol_now());
  52. $month_current = strftime("%m",dol_now());
  53. $year_start = $year_current;
  54. } else {
  55. $year_current = $year;
  56. $month_current = strftime("%m",dol_now());
  57. $year_start = $year;
  58. }
  59. $date_start=dol_mktime(0,0,0,$_REQUEST["date_startmonth"],$_REQUEST["date_startday"],$_REQUEST["date_startyear"]);
  60. $date_end=dol_mktime(23,59,59,$_REQUEST["date_endmonth"],$_REQUEST["date_endday"],$_REQUEST["date_endyear"]);
  61. // Quarter
  62. if (empty($date_start) || empty($date_end)) // We define date_start and date_end
  63. {
  64. $q=GETPOST("q")?GETPOST("q"):0;
  65. if ($q==0)
  66. {
  67. // We define date_start and date_end
  68. $month_start=GETPOST("month")?GETPOST("month"):($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
  69. $year_end=$year_start;
  70. $month_end=$month_start;
  71. if (! GETPOST("month")) // If month not forced
  72. {
  73. if (! GETPOST('year') && $month_start > $month_current)
  74. {
  75. $year_start--;
  76. $year_end--;
  77. }
  78. $month_end=$month_start-1;
  79. if ($month_end < 1) $month_end=12;
  80. else $year_end++;
  81. }
  82. $date_start=dol_get_first_day($year_start,$month_start,false); $date_end=dol_get_last_day($year_end,$month_end,false);
  83. }
  84. if ($q==1) { $date_start=dol_get_first_day($year_start,1,false); $date_end=dol_get_last_day($year_start,3,false); }
  85. if ($q==2) { $date_start=dol_get_first_day($year_start,4,false); $date_end=dol_get_last_day($year_start,6,false); }
  86. if ($q==3) { $date_start=dol_get_first_day($year_start,7,false); $date_end=dol_get_last_day($year_start,9,false); }
  87. if ($q==4) { $date_start=dol_get_first_day($year_start,10,false); $date_end=dol_get_last_day($year_start,12,false); }
  88. }
  89. else
  90. {
  91. // TODO We define q
  92. }
  93. $commonparams=array();
  94. $commonparams['modecompta']=$modecompta;
  95. $commonparams['sortorder'] = $sortorder;
  96. $commonparams['sortfield'] = $sortfield;
  97. $headerparams = array();
  98. $headerparams['date_startyear'] = $date_startyear;
  99. $headerparams['date_startmonth'] = $date_startmonth;
  100. $headerparams['date_startday'] = $date_startday;
  101. $headerparams['date_endyear'] = $date_endyear;
  102. $headerparams['date_endmonth'] = $date_endmonth;
  103. $headerparams['date_endday'] = $date_endday;
  104. $headerparams['q'] = $q;
  105. $tableparams = array();
  106. $tableparams['search_categ'] = $selected_cat;
  107. $tableparams['subcat'] = ($subcat === true)?'yes':'';
  108. // Adding common parameters
  109. $allparams = array_merge($commonparams, $headerparams, $tableparams);
  110. $headerparams = array_merge($commonparams, $headerparams);
  111. $tableparams = array_merge($commonparams, $tableparams);
  112. foreach($allparams as $key => $value) {
  113. $paramslink .= '&' . $key . '=' . $value;
  114. }
  115. /*
  116. * View
  117. */
  118. llxHeader();
  119. $form=new Form($db);
  120. // Show report header
  121. if ($modecompta=="CREANCES-DETTES") {
  122. $nom=$langs->trans("SalesTurnover").', '.$langs->trans("ByUserAuthorOfInvoice");
  123. $calcmode=$langs->trans("CalcModeDebt");
  124. $calcmode.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&modecompta=RECETTES-DEPENSES">','</a>').')';
  125. $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);
  126. //$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>";
  127. $description=$langs->trans("RulesCADue");
  128. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description.= $langs->trans("DepositsAreNotIncluded");
  129. else $description.= $langs->trans("DepositsAreIncluded");
  130. $builddate=time();
  131. //$exportlink=$langs->trans("NotYetAvailable");
  132. } else {
  133. $nom=$langs->trans("SalesTurnover").', '.$langs->trans("ByUserAuthorOfInvoice");
  134. $calcmode=$langs->trans("CalcModeEngagement");
  135. $calcmode.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&modecompta=CREANCES-DETTES">','</a>').')';
  136. $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);
  137. //$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>";
  138. $description=$langs->trans("RulesCAIn");
  139. $description.= $langs->trans("DepositsAreIncluded");
  140. $builddate=time();
  141. //$exportlink=$langs->trans("NotYetAvailable");
  142. }
  143. $moreparam=array();
  144. if (! empty($modecompta)) $moreparam['modecompta']=$modecompta;
  145. report_header($nom,$nomlink,$period,$periodlink,$description,$builddate,$exportlink,$moreparam,$calcmode);
  146. if (! empty($conf->accounting->enabled))
  147. {
  148. print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);
  149. }
  150. // Show array
  151. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  152. // Extra parameters management
  153. foreach($headerparams as $key => $value)
  154. {
  155. print '<input type="hidden" name="'.$key.'" value="'.$value.'">';
  156. }
  157. $catotal=0;
  158. if ($modecompta == 'CREANCES-DETTES') {
  159. $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";
  160. $sql.= " FROM ".MAIN_DB_PREFIX."user as u";
  161. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.fk_user_author = u.rowid";
  162. $sql.= " WHERE f.fk_statut in (1,2)";
  163. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  164. $sql.= " AND f.type IN (0,1,2,5)";
  165. } else {
  166. $sql.= " AND f.type IN (0,1,2,3,5)";
  167. }
  168. if ($date_start && $date_end) {
  169. $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  170. }
  171. } else {
  172. /*
  173. * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
  174. * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
  175. */
  176. $sql = "SELECT u.rowid as rowid, u.lastname as name, u.firstname as firstname, sum(pf.amount) as amount_ttc";
  177. $sql.= " FROM ".MAIN_DB_PREFIX."user as u" ;
  178. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.fk_user_author = u.rowid ";
  179. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_facture = f.rowid";
  180. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement as p ON p.rowid = pf.fk_paiement";
  181. $sql.= " WHERE 1=1";
  182. if ($date_start && $date_end) {
  183. $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  184. }
  185. }
  186. $sql.= " AND f.entity = ".$conf->entity;
  187. if ($socid) $sql.= " AND f.fk_soc = ".$socid;
  188. $sql .= " GROUP BY u.rowid, u.lastname, u.firstname";
  189. $sql .= " ORDER BY u.rowid";
  190. $result = $db->query($sql);
  191. if ($result) {
  192. $num = $db->num_rows($result);
  193. $i=0;
  194. while ($i < $num) {
  195. $obj = $db->fetch_object($result);
  196. $amount_ht[$obj->rowid] = $obj->amount;
  197. $amount[$obj->rowid] = $obj->amount_ttc;
  198. $name[$obj->rowid] = $obj->name.' '.$obj->firstname;
  199. $catotal_ht+=$obj->amount;
  200. $catotal+=$obj->amount_ttc;
  201. $i++;
  202. }
  203. } else {
  204. dol_print_error($db);
  205. }
  206. // Adding old-version payments, non-bound by "paiement_facture" then without User
  207. if ($modecompta != 'CREANCES-DETTES') {
  208. $sql = "SELECT -1 as rowidx, '' as name, '' as firstname, sum(DISTINCT p.amount) as amount_ttc";
  209. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  210. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  211. $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
  212. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
  213. $sql.= " WHERE pf.rowid IS NULL";
  214. $sql.= " AND p.fk_bank = b.rowid";
  215. $sql.= " AND b.fk_account = ba.rowid";
  216. $sql.= " AND ba.entity IN (".getEntity('bank_account').")";
  217. if ($date_start && $date_end) {
  218. $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  219. }
  220. $sql.= " GROUP BY rowidx, name, firstname";
  221. $sql.= " ORDER BY rowidx";
  222. $result = $db->query($sql);
  223. if ($result) {
  224. $num = $db->num_rows($result);
  225. $i=0;
  226. while ($i < $num) {
  227. $obj = $db->fetch_object($result);
  228. $amount[$obj->rowidx] = $obj->amount_ttc;
  229. $name[$obj->rowidx] = $obj->name.' '.$obj->firstname;
  230. $catotal+=$obj->amount_ttc;
  231. $i++;
  232. }
  233. } else {
  234. dol_print_error($db);
  235. }
  236. }
  237. $morefilter='';
  238. print '<div class="div-table-responsive">';
  239. print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
  240. print "<tr class=\"liste_titre\">";
  241. print_liste_field_titre(
  242. $langs->trans("User"),
  243. $_SERVER["PHP_SELF"],
  244. "name",
  245. "",
  246. $paramslink,
  247. "",
  248. $sortfield,
  249. $sortorder
  250. );
  251. if ($modecompta == 'CREANCES-DETTES') {
  252. print_liste_field_titre(
  253. $langs->trans('AmountHT'),
  254. $_SERVER["PHP_SELF"],
  255. "amount_ht",
  256. "",
  257. $paramslink,
  258. 'align="right"',
  259. $sortfield,
  260. $sortorder
  261. );
  262. } else {
  263. print_liste_field_titre('');
  264. }
  265. print_liste_field_titre(
  266. $langs->trans("AmountTTC"),
  267. $_SERVER["PHP_SELF"],
  268. "amount_ttc",
  269. "",
  270. $paramslink,
  271. 'align="right"',
  272. $sortfield,
  273. $sortorder
  274. );
  275. print_liste_field_titre(
  276. $langs->trans("Percentage"),
  277. $_SERVER["PHP_SELF"],"amount_ttc",
  278. "",
  279. $paramslink,
  280. 'align="right"',
  281. $sortfield,
  282. $sortorder
  283. );
  284. print_liste_field_titre(
  285. $langs->trans("OtherStatistics"),
  286. $_SERVER["PHP_SELF"],
  287. "",
  288. "",
  289. "",
  290. 'align="center" width="20%"'
  291. );
  292. print "</tr>\n";
  293. $var=true;
  294. if (count($amount)) {
  295. $arrayforsort=$name;
  296. // We define arrayforsort
  297. if ($sortfield == 'name' && $sortorder == 'asc') {
  298. asort($name);
  299. $arrayforsort=$name;
  300. }
  301. if ($sortfield == 'name' && $sortorder == 'desc') {
  302. arsort($name);
  303. $arrayforsort=$name;
  304. }
  305. if ($sortfield == 'amount_ht' && $sortorder == 'asc') {
  306. asort($amount_ht);
  307. $arrayforsort=$amount_ht;
  308. }
  309. if ($sortfield == 'amount_ht' && $sortorder == 'desc') {
  310. arsort($amount_ht);
  311. $arrayforsort=$amount_ht;
  312. }
  313. if ($sortfield == 'amount_ttc' && $sortorder == 'asc') {
  314. asort($amount);
  315. $arrayforsort=$amount;
  316. }
  317. if ($sortfield == 'amount_ttc' && $sortorder == 'desc') {
  318. arsort($amount);
  319. $arrayforsort=$amount;
  320. }
  321. $i = 0;
  322. foreach($arrayforsort as $key => $value) {
  323. print '<tr class="oddeven">';
  324. // Third party
  325. $fullname=$name[$key];
  326. if ($key >= 0) {
  327. $linkname='<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$key.'">'.img_object($langs->trans("ShowUser"),'user').' '.$fullname.'</a>';
  328. } else {
  329. $linkname=$langs->trans("PaymentsNotLinkedToUser");
  330. }
  331. print "<td>".$linkname."</td>\n";
  332. // Amount w/o VAT
  333. print '<td align="right">';
  334. if ($modecompta != 'CREANCES-DETTES')
  335. {
  336. if ($key > 0) {
  337. print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid='.$key.'">';
  338. } else {
  339. print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid=-1">';
  340. }
  341. } else {
  342. if ($key > 0) {
  343. print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?userid='.$key.'">';
  344. } else {
  345. print '<a href="#">';
  346. }
  347. print price($amount_ht[$key]);
  348. }
  349. print '</td>';
  350. // Amount with VAT
  351. print '<td align="right">';
  352. if ($modecompta != 'CREANCES-DETTES') {
  353. if ($key > 0) {
  354. print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid='.$key.'">';
  355. } else {
  356. print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid=-1">';
  357. }
  358. } else {
  359. if ($key > 0) {
  360. print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?userid='.$key.'">';
  361. } else {
  362. print '<a href="#">';
  363. }
  364. }
  365. print price($amount[$key]);
  366. print '</td>';
  367. // Percent
  368. print '<td align="right">'.($catotal > 0 ? round(100 * $amount[$key] / $catotal,2).'%' : '&nbsp;').'</td>';
  369. // Other stats
  370. print '<td align="center">';
  371. if (! empty($conf->propal->enabled) && $key>0) {
  372. print '&nbsp;<a href="'.DOL_URL_ROOT.'/comm/propal/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("ProposalStats"),"stats").'</a>&nbsp;';
  373. }
  374. if (! empty($conf->commande->enabled) && $key>0) {
  375. print '&nbsp;<a href="'.DOL_URL_ROOT.'/commande/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("OrderStats"),"stats").'</a>&nbsp;';
  376. }
  377. if (! empty($conf->facture->enabled) && $key>0) {
  378. print '&nbsp;<a href="'.DOL_URL_ROOT.'/compta/facture/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("InvoiceStats"),"stats").'</a>&nbsp;';
  379. }
  380. print '</td>';
  381. print "</tr>\n";
  382. $i++;
  383. }
  384. // Total
  385. print '<tr class="liste_total">';
  386. print '<td>'.$langs->trans("Total").'</td>';
  387. if ($modecompta != 'CREANCES-DETTES') {
  388. print '<td colspan="1"></td>';
  389. } else {
  390. print '<td align="right">'.price($catotal_ht).'</td>';
  391. }
  392. print '<td align="right">'.price($catotal).'</td>';
  393. print '<td>&nbsp;</td>';
  394. print '<td>&nbsp;</td>';
  395. print '</tr>';
  396. $db->free($result);
  397. }
  398. print "</table>";
  399. print '</div>';
  400. print '</form>';
  401. llxFooter();
  402. $db->close();