PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/compta/resultat/index.php

https://github.com/asterix14/dolibarr
PHP | 517 lines | 389 code | 66 blank | 62 comment | 74 complexity | 199055931d64e86cce304bc516f1b9ed MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 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@dolibarr.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/compta/resultat/index.php
  21. * \brief Page reporting resultat
  22. */
  23. require('../../main.inc.php');
  24. require_once(DOL_DOCUMENT_ROOT."/core/lib/report.lib.php");
  25. $year_start=isset($_GET["year_start"])?$_GET["year_start"]:$_POST["year_start"];
  26. $year_current = strftime("%Y",time());
  27. $nbofyear=4;
  28. if (! $year_start) {
  29. $year_start = $year_current - ($nbofyear-1);
  30. $year_end = $year_current;
  31. }
  32. else {
  33. $year_end=$year_start + ($nbofyear-1);
  34. }
  35. // Security check
  36. $socid = isset($_REQUEST["socid"])?$_REQUEST["socid"]:'';
  37. if ($user->societe_id > 0) $socid = $user->societe_id;
  38. if (!$user->rights->compta->resultat->lire && !$user->rights->accounting->comptarapport->lire)
  39. accessforbidden();
  40. // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES')
  41. $modecompta = $conf->global->COMPTA_MODE;
  42. if ($_GET["modecompta"]) $modecompta=$_GET["modecompta"];
  43. /*
  44. * View
  45. */
  46. llxHeader();
  47. $form=new Form($db);
  48. // Affiche en-tete du rapport
  49. if ($modecompta=="CREANCES-DETTES")
  50. {
  51. $nom=$langs->trans("AnnualSummaryDueDebtMode");
  52. $nom.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=RECETTES-DEPENSES">','</a>').')';
  53. $period="$year_start - $year_end";
  54. $periodlink=($year_start?"<a href='".$_SERVER["PHP_SELF"]."?year_start=".($year_start-1)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year_start=".($year_start+1)."&modecompta=".$modecompta."'>".img_next()."</a>":"");
  55. $description=$langs->trans("RulesResultDue");
  56. $builddate=time();
  57. //$exportlink=$langs->trans("NotYetAvailable");
  58. }
  59. else {
  60. $nom=$langs->trans("AnnualSummaryInputOutputMode");
  61. $nom.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=CREANCES-DETTES">','</a>').')';
  62. $period="$year_start - $year_end";
  63. $periodlink=($year_start?"<a href='".$_SERVER["PHP_SELF"]."?year_start=".($year_start-1)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year_start=".($year_start+1)."&modecompta=".$modecompta."'>".img_next()."</a>":"");
  64. $description=$langs->trans("RulesResultInOut");
  65. $builddate=time();
  66. //$exportlink=$langs->trans("NotYetAvailable");
  67. }
  68. report_header($nom,$nomlink,$period,$periodlink,$description,$builddate,$exportlink);
  69. /*
  70. * Factures clients
  71. */
  72. $subtotal_ht = 0;
  73. $subtotal_ttc = 0;
  74. if ($modecompta == 'CREANCES-DETTES') {
  75. $sql = "SELECT sum(f.total) as amount_ht, sum(f.total_ttc) as amount_ttc, date_format(f.datef,'%Y-%m') as dm";
  76. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  77. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  78. $sql.= " WHERE f.fk_soc = s.rowid";
  79. $sql.= " AND f.fk_statut in (1,2)";
  80. } else {
  81. /*
  82. * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
  83. * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
  84. */
  85. $sql = "SELECT sum(pf.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
  86. $sql.= " FROM ".MAIN_DB_PREFIX."facture as f";
  87. $sql.= ", ".MAIN_DB_PREFIX."paiement_facture as pf";
  88. $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
  89. $sql.= " WHERE p.rowid = pf.fk_paiement";
  90. $sql.= " AND pf.fk_facture = f.rowid";
  91. }
  92. $sql.= " AND f.entity = ".$conf->entity;
  93. if ($socid) $sql.= " AND f.fk_soc = $socid";
  94. $sql.= " GROUP BY dm";
  95. $sql.= " ORDER BY dm";
  96. //print $sql;
  97. dol_syslog("get customers invoices sql=".$sql);
  98. $result=$db->query($sql);
  99. if ($result)
  100. {
  101. $num = $db->num_rows($result);
  102. $i = 0;
  103. while ($i < $num)
  104. {
  105. $row = $db->fetch_object($result);
  106. $encaiss[$row->dm] = $row->amount_ht;
  107. $encaiss_ttc[$row->dm] = $row->amount_ttc;
  108. $i++;
  109. }
  110. $db->free($result);
  111. }
  112. else {
  113. dol_print_error($db);
  114. }
  115. // On ajoute les paiements clients anciennes version, non lies par paiement_facture
  116. if ($modecompta != 'CREANCES-DETTES') {
  117. $sql = "SELECT sum(p.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
  118. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  119. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  120. $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
  121. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
  122. $sql.= " WHERE pf.rowid IS NULL";
  123. $sql.= " AND p.fk_bank = b.rowid";
  124. $sql.= " AND b.fk_account = ba.rowid";
  125. $sql.= " AND ba.entity = ".$conf->entity;
  126. $sql.= " GROUP BY dm";
  127. $sql.= " ORDER BY dm";
  128. dol_syslog("get old customers payments not linked to invoices sql=".$sql);
  129. $result = $db->query($sql);
  130. if ($result) {
  131. $num = $db->num_rows($result);
  132. $i = 0;
  133. while ($i < $num)
  134. {
  135. $row = $db->fetch_object($result);
  136. $encaiss[$row->dm] += $row->amount_ht;
  137. $encaiss_ttc[$row->dm] += $row->amount_ttc;
  138. $i++;
  139. }
  140. }
  141. else {
  142. dol_print_error($db);
  143. }
  144. }
  145. /*
  146. * Frais, factures fournisseurs.
  147. */
  148. $subtotal_ht = 0;
  149. $subtotal_ttc = 0;
  150. if ($modecompta == 'CREANCES-DETTES') {
  151. $sql = "SELECT sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc, date_format(f.datef,'%Y-%m') as dm";
  152. $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  153. $sql.= " WHERE f.fk_statut IN (1,2)";
  154. } else {
  155. $sql = "SELECT sum(pf.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
  156. $sql.= " FROM ".MAIN_DB_PREFIX."paiementfourn as p";
  157. $sql.= ", ".MAIN_DB_PREFIX."facture_fourn as f";
  158. $sql.= ", ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf";
  159. $sql.= " WHERE f.rowid = pf.fk_facturefourn";
  160. $sql.= " AND p.rowid = pf.fk_paiementfourn";
  161. }
  162. $sql.= " AND f.entity = ".$conf->entity;
  163. if ($socid) $sql.= " AND f.fk_soc = ".$socid;
  164. $sql.= " GROUP BY dm";
  165. dol_syslog("get suppliers invoices sql=".$sql);
  166. $result=$db->query($sql);
  167. if ($result)
  168. {
  169. $num = $db->num_rows($result);
  170. $i = 0;
  171. while ($i < $num)
  172. {
  173. $row = $db->fetch_object($result);
  174. $decaiss[$row->dm] = $row->amount_ht;
  175. $decaiss_ttc[$row->dm] = $row->amount_ttc;
  176. $i++;
  177. }
  178. $db->free($result);
  179. }
  180. else {
  181. dol_print_error($db);
  182. }
  183. /*
  184. * TVA
  185. */
  186. $subtotal_ht = 0;
  187. $subtotal_ttc = 0;
  188. if ($modecompta == 'CREANCES-DETTES') {
  189. // TVA a payer
  190. $sql = "SELECT sum(f.tva) as amount, date_format(f.datef,'%Y-%m') as dm";
  191. $sql.= " FROM ".MAIN_DB_PREFIX."facture as f";
  192. $sql.= " WHERE f.fk_statut IN (1,2)";
  193. $sql.= " AND f.entity = ".$conf->entity;
  194. $sql.= " GROUP BY dm DESC";
  195. dol_syslog("get vat to pay sql=".$sql);
  196. $result=$db->query($sql);
  197. if ($result) {
  198. $num = $db->num_rows($result);
  199. $var=false;
  200. $i = 0;
  201. if ($num) {
  202. while ($i < $num) {
  203. $obj = $db->fetch_object($result);
  204. $decaiss[$obj->dm] += $obj->amount;
  205. $decaiss_ttc[$obj->dm] += $obj->amount;
  206. $i++;
  207. }
  208. }
  209. } else {
  210. dol_print_error($db);
  211. }
  212. // TVA a recuperer
  213. $sql = "SELECT sum(f.total_tva) as amount, date_format(f.datef,'%Y-%m') as dm";
  214. $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  215. $sql.= " WHERE f.fk_statut IN (1,2)";
  216. $sql.= " AND f.entity = ".$conf->entity;
  217. $sql.= " GROUP BY dm";
  218. dol_syslog("get vat to receive back sql=".$sql);
  219. $result=$db->query($sql);
  220. if ($result) {
  221. $num = $db->num_rows($result);
  222. $var=false;
  223. $i = 0;
  224. if ($num) {
  225. while ($i < $num) {
  226. $obj = $db->fetch_object($result);
  227. $encaiss[$obj->dm] += $obj->amount;
  228. $encaiss_ttc[$obj->dm] += $obj->amount;
  229. $i++;
  230. }
  231. }
  232. } else {
  233. dol_print_error($db);
  234. }
  235. }
  236. else {
  237. // TVA reellement deja payee
  238. $sql = "SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm";
  239. $sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
  240. $sql.= " WHERE amount > 0";
  241. $sql.= " AND t.entity = ".$conf->entity;
  242. $sql.= " GROUP BY dm";
  243. dol_syslog("get vat really paid sql=".$sql);
  244. $result=$db->query($sql);
  245. if ($result) {
  246. $num = $db->num_rows($result);
  247. $var=false;
  248. $i = 0;
  249. if ($num) {
  250. while ($i < $num) {
  251. $obj = $db->fetch_object($result);
  252. $decaiss[$obj->dm] += $obj->amount;
  253. $decaiss_ttc[$obj->dm] += $obj->amount;
  254. $i++;
  255. }
  256. }
  257. } else {
  258. dol_print_error($db);
  259. }
  260. // TVA recuperee
  261. $sql = "SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm";
  262. $sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
  263. $sql.= " WHERE amount < 0";
  264. $sql.= " AND t.entity = ".$conf->entity;
  265. $sql.= " GROUP BY dm";
  266. dol_syslog("get vat really received back sql=".$sql);
  267. $result=$db->query($sql);
  268. if ($result) {
  269. $num = $db->num_rows($result);
  270. $var=false;
  271. $i = 0;
  272. if ($num) {
  273. while ($i < $num) {
  274. $obj = $db->fetch_object($result);
  275. $encaiss[$obj->dm] += $obj->amount;
  276. $encaiss_ttc[$obj->dm] += $obj->amount;
  277. $i++;
  278. }
  279. }
  280. } else {
  281. dol_print_error($db);
  282. }
  283. }
  284. /*
  285. * Charges sociales non deductibles
  286. */
  287. $subtotal_ht = 0;
  288. $subtotal_ttc = 0;
  289. if ($modecompta == 'CREANCES-DETTES') {
  290. $sql = "SELECT c.libelle as nom, date_format(s.date_ech,'%Y-%m') as dm, sum(s.amount) as amount_ht, sum(s.amount) as amount_ttc";
  291. $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  292. $sql.= ", ".MAIN_DB_PREFIX."chargesociales as s";
  293. $sql.= " WHERE s.fk_type = c.id";
  294. $sql.= " AND c.deductible = 0";
  295. }
  296. else {
  297. $sql = "SELECT c.libelle as nom, date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount_ht, sum(p.amount) as amount_ttc";
  298. $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  299. $sql.= ", ".MAIN_DB_PREFIX."chargesociales as s";
  300. $sql.= ", ".MAIN_DB_PREFIX."paiementcharge as p";
  301. $sql.= " WHERE p.fk_charge = s.rowid";
  302. $sql.= " AND s.fk_type = c.id";
  303. $sql.= " AND c.deductible = 0";
  304. }
  305. $sql.= " AND s.entity = ".$conf->entity;
  306. $sql.= " GROUP BY c.libelle, dm";
  307. dol_syslog("get social contributions deductible=0 sql=".$sql);
  308. $result=$db->query($sql);
  309. if ($result) {
  310. $num = $db->num_rows($result);
  311. $var=false;
  312. $i = 0;
  313. if ($num) {
  314. while ($i < $num) {
  315. $obj = $db->fetch_object($result);
  316. $decaiss[$obj->dm] += $obj->amount_ht;
  317. $decaiss_ttc[$obj->dm] += $obj->amount_ttc;
  318. $i++;
  319. }
  320. }
  321. } else {
  322. dol_print_error($db);
  323. }
  324. /*
  325. * Charges sociales deductibles
  326. */
  327. $subtotal_ht = 0;
  328. $subtotal_ttc = 0;
  329. if ($modecompta == 'CREANCES-DETTES')
  330. {
  331. $sql = "SELECT c.libelle as nom, date_format(s.date_ech,'%Y-%m') as dm, sum(s.amount) as amount_ht, sum(s.amount) as amount_ttc";
  332. $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  333. $sql.= ", ".MAIN_DB_PREFIX."chargesociales as s";
  334. $sql.= " WHERE s.fk_type = c.id";
  335. $sql.= " AND c.deductible = 1";
  336. }
  337. else
  338. {
  339. $sql = "SELECT c.libelle as nom, date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount_ht, sum(p.amount) as amount_ttc";
  340. $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  341. $sql.= ", ".MAIN_DB_PREFIX."chargesociales as s";
  342. $sql.= ", ".MAIN_DB_PREFIX."paiementcharge as p";
  343. $sql.= " WHERE p.fk_charge = s.rowid";
  344. $sql.= " AND s.fk_type = c.id";
  345. $sql.= " AND c.deductible = 1";
  346. }
  347. $sql.= " AND s.entity = ".$conf->entity;
  348. $sql.= " GROUP BY c.libelle, dm";
  349. dol_syslog("get social contributions paid deductible=1 sql=".$sql);
  350. $result=$db->query($sql);
  351. if ($result) {
  352. $num = $db->num_rows($result);
  353. $var=false;
  354. $i = 0;
  355. if ($num) {
  356. while ($i < $num) {
  357. $obj = $db->fetch_object($result);
  358. $decaiss[$obj->dm] += $obj->amount_ht;
  359. $decaiss_ttc[$obj->dm] += $obj->amount_ttc;
  360. $i++;
  361. }
  362. }
  363. } else {
  364. dol_print_error($db);
  365. }
  366. /*
  367. * Show result array
  368. */
  369. $totentrees=array();
  370. $totsorties=array();
  371. print '<table class="noborder" width="100%">';
  372. print '<tr class="liste_titre"><td rowspan=2>'.$langs->trans("Month").'</td>';
  373. for ($annee = $year_start ; $annee <= $year_end ; $annee++)
  374. {
  375. print '<td align="center" colspan="2">';
  376. print '<a href="clientfourn.php?year='.$annee.'">';
  377. print $annee;
  378. if ($conf->global->SOCIETE_FISCAL_MONTH_START > 1) print '-'.($annee+1);
  379. print '</a></td>';
  380. }
  381. print '</tr>';
  382. print '<tr class="liste_titre">';
  383. for ($annee = $year_start ; $annee <= $year_end ; $annee++)
  384. {
  385. print '<td align="right">'.$langs->trans("Outcome").'</td>';
  386. print '<td align="right">'.$langs->trans("Income").'</td>';
  387. }
  388. print '</tr>';
  389. $var=True;
  390. // Loop on each month
  391. $nb_mois_decalage = $conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START-1):0;
  392. for ($mois = 1+$nb_mois_decalage ; $mois <= 12+$nb_mois_decalage ; $mois++)
  393. {
  394. $mois_modulo = $mois;
  395. if($mois>12) {$mois_modulo = $mois-12;}
  396. $var=!$var;
  397. print '<tr '.$bc[$var].'>';
  398. print "<td>".dol_print_date(dol_mktime(12,0,0,$mois_modulo,1,$annee),"%B")."</td>";
  399. for ($annee = $year_start ; $annee <= $year_end ; $annee++)
  400. {
  401. $annee_decalage=$annee;
  402. if($mois>12) {$annee_decalage=$annee+1;}
  403. $case = strftime("%Y-%m",dol_mktime(12,0,0,$mois_modulo,1,$annee_decalage));
  404. print '<td align="right">&nbsp;';
  405. if ($decaiss_ttc[$case] != 0)
  406. {
  407. print '<a href="clientfourn.php?year='.$annee_decalage.'&month='.$mois_modulo.'">'.price($decaiss_ttc[$case]).'</a>';
  408. $totsorties[$annee]+=$decaiss_ttc[$case];
  409. }
  410. print "</td>";
  411. print '<td align="right">&nbsp;';
  412. if ($encaiss_ttc[$case] != 0)
  413. {
  414. print '<a href="clientfourn.php?year='.$annee_decalage.'&month='.$mois_modulo.'">'.price($encaiss_ttc[$case]).'</a>';
  415. $totentrees[$annee]+=$encaiss_ttc[$case];
  416. }
  417. print "</td>";
  418. }
  419. print '</tr>';
  420. }
  421. // Total
  422. $var=!$var;
  423. $nbcols=0;
  424. print '<tr class="liste_total"><td>'.$langs->trans("TotalTTC").'</td>';
  425. for ($annee = $year_start ; $annee <= $year_end ; $annee++)
  426. {
  427. $nbcols+=2;
  428. print '<td align="right">'.(isset($totsorties[$annee])?price($totsorties[$annee]):'&nbsp;').'</td>';
  429. print '<td align="right">'.(isset($totentrees[$annee])?price($totentrees[$annee]):'&nbsp;').'</td>';
  430. }
  431. print "</tr>\n";
  432. // Empty line
  433. print '<tr><td>&nbsp;</td>';
  434. print '<td colspan="'.$nbcols.'">&nbsp;</td>';
  435. print "</tr>\n";
  436. // Balance
  437. $var=!$var;
  438. print '<tr class="liste_total"><td>'.$langs->trans("Profit").'</td>';
  439. for ($annee = $year_start ; $annee <= $year_end ; $annee++)
  440. {
  441. print '<td align="right" colspan="2"> ';
  442. if (isset($totentrees[$annee]) || isset($totsorties[$annee])) {
  443. print price($totentrees[$annee]-$totsorties[$annee]).'</td>';
  444. // print '<td>&nbsp;</td>';
  445. }
  446. }
  447. print "</tr>\n";
  448. print "</table>";
  449. $db->close();
  450. llxFooter();
  451. ?>