PageRenderTime 25ms CodeModel.GetById 6ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/compta/stats/casoc.php

https://bitbucket.org/speedealing/speedealing
PHP | 319 lines | 235 code | 35 blank | 49 comment | 75 complexity | 45970f470235586f21e9206682fb43eb MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerke@telenet.be>
  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/casoc.php
  22. * \brief Page reporting CA par societe
  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. $langs->load("companies");
  29. // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES')
  30. $modecompta = $conf->global->COMPTA_MODE;
  31. if (GETPOST("modecompta")) $modecompta=GETPOST("modecompta");
  32. $sortorder=isset($_GET["sortorder"])?$_GET["sortorder"]:$_POST["sortorder"];
  33. $sortfield=isset($_GET["sortfield"])?$_GET["sortfield"]:$_POST["sortfield"];
  34. if (! $sortorder) $sortorder="asc";
  35. if (! $sortfield) $sortfield="nom";
  36. // Security check
  37. $socid = isset($_REQUEST["socid"])?$_REQUEST["socid"]:'';
  38. if ($user->societe_id > 0) $socid = $user->societe_id;
  39. if (!$user->rights->compta->resultat->lire && !$user->rights->accounting->comptarapport->lire) accessforbidden();
  40. // Date range
  41. $year=GETPOST("year");
  42. $month=GETPOST("month");
  43. if (empty($year))
  44. {
  45. $year_current = strftime("%Y",dol_now());
  46. $month_current = strftime("%m",dol_now());
  47. $year_start = $year_current;
  48. } else {
  49. $year_current = $year;
  50. $month_current = strftime("%m",dol_now());
  51. $year_start = $year;
  52. }
  53. $date_start=dol_mktime(0,0,0,$_REQUEST["date_startmonth"],$_REQUEST["date_startday"],$_REQUEST["date_startyear"]);
  54. $date_end=dol_mktime(23,59,59,$_REQUEST["date_endmonth"],$_REQUEST["date_endday"],$_REQUEST["date_endyear"]);
  55. // Quarter
  56. if (empty($date_start) || empty($date_end)) // We define date_start and date_end
  57. {
  58. $q=GETPOST("q")?GETPOST("q"):0;
  59. if ($q==0)
  60. {
  61. // We define date_start and date_end
  62. $month_start=GETPOST("month")?GETPOST("month"):($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
  63. $year_end=$year_start;
  64. $month_end=$month_start;
  65. if (! GETPOST("month")) // If month not forced
  66. {
  67. if (! GETPOST('year') && $month_start > $month_current)
  68. {
  69. $year_start--;
  70. $year_end--;
  71. }
  72. $month_end=$month_start-1;
  73. if ($month_end < 1) $month_end=12;
  74. else $year_end++;
  75. }
  76. $date_start=dol_get_first_day($year_start,$month_start,false); $date_end=dol_get_last_day($year_end,$month_end,false);
  77. }
  78. if ($q==1) { $date_start=dol_get_first_day($year_start,1,false); $date_end=dol_get_last_day($year_start,3,false); }
  79. if ($q==2) { $date_start=dol_get_first_day($year_start,4,false); $date_end=dol_get_last_day($year_start,6,false); }
  80. if ($q==3) { $date_start=dol_get_first_day($year_start,7,false); $date_end=dol_get_last_day($year_start,9,false); }
  81. if ($q==4) { $date_start=dol_get_first_day($year_start,10,false); $date_end=dol_get_last_day($year_start,12,false); }
  82. }
  83. else
  84. {
  85. // TODO We define q
  86. }
  87. /*
  88. * View
  89. */
  90. llxHeader();
  91. $form=new Form($db);
  92. $thirdparty_static=new Societe($db);
  93. // Affiche en-tete de rapport
  94. if ($modecompta=="CREANCES-DETTES")
  95. {
  96. $nom=$langs->trans("SalesTurnover").', '.$langs->trans("ByThirdParties");
  97. $nom.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&modecompta=RECETTES-DEPENSES">','</a>').')';
  98. $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);
  99. //$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>';
  100. $description=$langs->trans("RulesCADue");
  101. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description.= $langs->trans("DepositsAreNotIncluded");
  102. else $description.= $langs->trans("DepositsAreIncluded");
  103. $builddate=time();
  104. //$exportlink=$langs->trans("NotYetAvailable");
  105. }
  106. else {
  107. $nom=$langs->trans("SalesTurnover").', '.$langs->trans("ByThirdParties");
  108. $nom.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&modecompta=CREANCES-DETTES">','</a>').')';
  109. $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);
  110. //$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>';
  111. $description=$langs->trans("RulesCAIn");
  112. $description.= $langs->trans("DepositsAreIncluded");
  113. $builddate=time();
  114. //$exportlink=$langs->trans("NotYetAvailable");
  115. }
  116. $moreparam=array();
  117. if (! empty($modecompta)) $moreparam['modecompta']=$modecompta;
  118. report_header($nom,$nomlink,$period,$periodlink,$description,$builddate,$exportlink,$moreparam);
  119. // Charge tableau
  120. $catotal=0;
  121. if ($modecompta == 'CREANCES-DETTES')
  122. {
  123. $sql = "SELECT s.rowid as socid, s.nom as name, sum(f.total) as amount, sum(f.total_ttc) as amount_ttc";
  124. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  125. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  126. $sql.= " WHERE f.fk_statut in (1,2)";
  127. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2)";
  128. else $sql.= " AND f.type IN (0,1,2,3)";
  129. $sql.= " AND f.fk_soc = s.rowid";
  130. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  131. }
  132. else
  133. {
  134. /*
  135. * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
  136. * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
  137. */
  138. $sql = "SELECT s.rowid as socid, s.nom as name, sum(pf.amount) as amount_ttc";
  139. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  140. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  141. $sql.= ", ".MAIN_DB_PREFIX."paiement_facture as pf";
  142. $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
  143. $sql .= " WHERE p.rowid = pf.fk_paiement";
  144. $sql.= " AND pf.fk_facture = f.rowid";
  145. $sql.= " AND f.fk_soc = s.rowid";
  146. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  147. }
  148. $sql.= " AND f.entity = ".$conf->entity;
  149. if ($socid) $sql.= " AND f.fk_soc = ".$socid;
  150. $sql.= " GROUP BY s.rowid, s.nom";
  151. $sql.= " ORDER BY s.rowid";
  152. $result = $db->query($sql);
  153. if ($result)
  154. {
  155. $num = $db->num_rows($result);
  156. $i=0;
  157. while ($i < $num)
  158. {
  159. $obj = $db->fetch_object($result);
  160. $amount[$obj->socid] += $obj->amount_ttc;
  161. $name[$obj->socid] = $obj->name;
  162. $catotal+=$obj->amount_ttc;
  163. $i++;
  164. }
  165. }
  166. else {
  167. dol_print_error($db);
  168. }
  169. // On ajoute les paiements anciennes version, non lies par paiement_facture
  170. if ($modecompta != 'CREANCES-DETTES')
  171. {
  172. $sql = "SELECT '0' as socid, 'Autres' as name, sum(p.amount) as amount_ttc";
  173. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  174. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  175. $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
  176. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
  177. $sql.= " WHERE pf.rowid IS NULL";
  178. $sql.= " AND p.fk_bank = b.rowid";
  179. $sql.= " AND b.fk_account = ba.rowid";
  180. $sql.= " AND ba.entity = ".$conf->entity;
  181. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  182. $sql.= " GROUP BY socid, name";
  183. $sql.= " ORDER BY name";
  184. $result = $db->query($sql);
  185. if ($result)
  186. {
  187. $num = $db->num_rows($result);
  188. $i=0;
  189. while ($i < $num)
  190. {
  191. $obj = $db->fetch_object($result);
  192. $amount[$obj->rowid] += $obj->amount_ttc;
  193. $name[$obj->rowid] = $obj->name;
  194. $catotal+=$obj->amount_ttc;
  195. $i++;
  196. }
  197. }
  198. else {
  199. dol_print_error($db);
  200. }
  201. }
  202. // Show array
  203. $i = 0;
  204. print "<table class=\"noborder\" width=\"100%\">";
  205. print "<tr class=\"liste_titre\">";
  206. print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"nom","",'&amp;year='.($year).'&modecompta='.$modecompta,"",$sortfield,$sortorder);
  207. print_liste_field_titre($langs->trans("AmountTTC"),$_SERVER["PHP_SELF"],"amount_ttc","",'&amp;year='.($year).'&modecompta='.$modecompta,'align="right"',$sortfield,$sortorder);
  208. print_liste_field_titre($langs->trans("Percentage"),$_SERVER["PHP_SELF"],"amount_ttc","",'&amp;year='.($year).'&modecompta='.$modecompta,'align="right"',$sortfield,$sortorder);
  209. print_liste_field_titre($langs->trans("OtherStatistics"),$_SERVER["PHP_SELF"],"","","",'align="center" width="20%"');
  210. print "</tr>\n";
  211. $var=true;
  212. if (count($amount))
  213. {
  214. $arrayforsort=$name;
  215. // On definit tableau arrayforsort
  216. if ($sortfield == 'nom' && $sortorder == 'asc') {
  217. asort($name);
  218. $arrayforsort=$name;
  219. }
  220. if ($sortfield == 'nom' && $sortorder == 'desc') {
  221. arsort($name);
  222. $arrayforsort=$name;
  223. }
  224. if ($sortfield == 'amount_ttc' && $sortorder == 'asc') {
  225. asort($amount);
  226. $arrayforsort=$amount;
  227. }
  228. if ($sortfield == 'amount_ttc' && $sortorder == 'desc') {
  229. arsort($amount);
  230. $arrayforsort=$amount;
  231. }
  232. foreach($arrayforsort as $key=>$value)
  233. {
  234. $var=!$var;
  235. print "<tr ".$bc[$var].">";
  236. // Third party
  237. $fullname=$name[$key];
  238. if ($key > 0) {
  239. $thirdparty_static->id=$key;
  240. $thirdparty_static->name=$fullname;
  241. $thirdparty_static->client=1;
  242. $linkname=$thirdparty_static->getNomUrl(1,'customer');
  243. }
  244. else {
  245. $linkname=$langs->trans("PaymentsNotLinkedToInvoice");
  246. }
  247. print "<td>".$linkname."</td>\n";
  248. // Amount
  249. print '<td align="right">';
  250. if ($modecompta != 'CREANCES-DETTES')
  251. {
  252. if ($key > 0) print '<a href="'.DOL_URL_ROOT.'/compta/paiement/liste.php?socid='.$key.'">';
  253. else print '<a href="'.DOL_URL_ROOT.'/compta/paiement/liste.php?orphelins=1">';
  254. }
  255. else
  256. {
  257. if ($key > 0) print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?socid='.$key.'">';
  258. else print '<a href="#">';
  259. }
  260. print price($amount[$key]);
  261. print '</a>';
  262. print '</td>';
  263. // Percent;
  264. print '<td align="right">'.($catotal > 0 ? round(100 * $amount[$key] / $catotal, 2).'%' : '&nbsp;').'</td>';
  265. // Other stats
  266. print '<td align="center">';
  267. if (! empty($conf->propal->enabled) && $key>0) print '&nbsp;<a href="'.DOL_URL_ROOT.'/comm/propal/stats/index.php?socid='.$key.'">'.img_picto($langs->trans("ProposalStats"),"stats").'</a>&nbsp;';
  268. if (! empty($conf->commande->enabled) && $key>0) print '&nbsp;<a href="'.DOL_URL_ROOT.'/commande/stats/index.php?socid='.$key.'">'.img_picto($langs->trans("OrderStats"),"stats").'</a>&nbsp;';
  269. if (! empty($conf->facture->enabled) && $key>0) print '&nbsp;<a href="'.DOL_URL_ROOT.'/compta/facture/stats/index.php?socid='.$key.'">'.img_picto($langs->trans("InvoiceStats"),"stats").'</a>&nbsp;';
  270. print '</td>';
  271. print "</tr>\n";
  272. $i++;
  273. }
  274. // Total
  275. print '<tr class="liste_total"><td>'.$langs->trans("Total").'</td><td align="right">'.price($catotal).'</td><td>&nbsp;</td>';
  276. print '<td>&nbsp;</td>';
  277. print '</tr>';
  278. $db->free($result);
  279. }
  280. print "</table>";
  281. print '<br>';
  282. llxFooter();
  283. $db->close();
  284. ?>