index.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2014-2016 Ferran Marcet <fmarcet@2byte.es>
  6. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
  8. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  9. * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * \file htdocs/compta/resultat/index.php
  26. * \ingroup compta, accountancy
  27. * \brief Page reporting result
  28. */
  29. // Load Dolibarr environment
  30. require '../../main.inc.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  33. // Load translation files required by the page
  34. $langs->loadLangs(array('compta', 'bills', 'donation', 'salaries'));
  35. $date_startmonth = GETPOST('date_startmonth', 'int');
  36. $date_startday = GETPOST('date_startday', 'int');
  37. $date_startyear = GETPOST('date_startyear', 'int');
  38. $date_endmonth = GETPOST('date_endmonth', 'int');
  39. $date_endday = GETPOST('date_endday', 'int');
  40. $date_endyear = GETPOST('date_endyear', 'int');
  41. $nbofyear = 4;
  42. // Date range
  43. $year = GETPOST('year', 'int');
  44. if (empty($year)) {
  45. $year_current = dol_print_date(dol_now(), "%Y");
  46. $month_current = dol_print_date(dol_now(), "%m");
  47. $year_start = $year_current - ($nbofyear - 1);
  48. } else {
  49. $year_current = $year;
  50. $month_current = dol_print_date(dol_now(), "%m");
  51. $year_start = $year - ($nbofyear - 1);
  52. }
  53. $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
  54. $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
  55. // We define date_start and date_end
  56. if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
  57. $q = GETPOST("q") ? GETPOST("q", 'int') : 0;
  58. if ($q == 0) {
  59. // We define date_start and date_end
  60. $year_end = $year_start + ($nbofyear - 1);
  61. $month_start = GETPOST("month") ? GETPOST("month", 'int') : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
  62. if (!GETPOST('month')) {
  63. if (!GETPOST("year") && $month_start > $month_current) {
  64. $year_start--;
  65. $year_end--;
  66. }
  67. $month_end = $month_start - 1;
  68. if ($month_end < 1) {
  69. $month_end = 12;
  70. } else {
  71. $year_end++;
  72. }
  73. } else {
  74. $month_end = $month_start;
  75. }
  76. $date_start = dol_get_first_day($year_start, $month_start, false);
  77. $date_end = dol_get_last_day($year_end, $month_end, false);
  78. }
  79. if ($q == 1) {
  80. $date_start = dol_get_first_day($year_start, 1, false);
  81. $date_end = dol_get_last_day($year_start, 3, false);
  82. }
  83. if ($q == 2) {
  84. $date_start = dol_get_first_day($year_start, 4, false);
  85. $date_end = dol_get_last_day($year_start, 6, false);
  86. }
  87. if ($q == 3) {
  88. $date_start = dol_get_first_day($year_start, 7, false);
  89. $date_end = dol_get_last_day($year_start, 9, false);
  90. }
  91. if ($q == 4) {
  92. $date_start = dol_get_first_day($year_start, 10, false);
  93. $date_end = dol_get_last_day($year_start, 12, false);
  94. }
  95. }
  96. // $date_start and $date_end are defined. We force $year_start and $nbofyear
  97. $tmps = dol_getdate($date_start);
  98. $year_start = $tmps['year'];
  99. $tmpe = dol_getdate($date_end);
  100. $year_end = $tmpe['year'];
  101. $nbofyear = ($year_end - $year_start) + 1;
  102. //var_dump("year_start=".$year_start." year_end=".$year_end." nbofyear=".$nbofyear." date_start=".dol_print_date($date_start, 'dayhour')." date_end=".dol_print_date($date_end, 'dayhour'));
  103. // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES' or 'BOOKKEEPING')
  104. $modecompta = $conf->global->ACCOUNTING_MODE;
  105. if (isModEnabled('accounting')) {
  106. $modecompta = 'BOOKKEEPING';
  107. }
  108. if (GETPOST("modecompta", 'alpha')) {
  109. $modecompta = GETPOST("modecompta", 'alpha');
  110. }
  111. // Security check
  112. $socid = GETPOST('socid', 'int');
  113. if ($user->socid > 0) {
  114. $socid = $user->socid;
  115. }
  116. if (isModEnabled('comptabilite')) {
  117. $result = restrictedArea($user, 'compta', '', '', 'resultat');
  118. }
  119. if (isModEnabled('accounting')) {
  120. $result = restrictedArea($user, 'accounting', '', '', 'comptarapport');
  121. }
  122. /*
  123. * View
  124. */
  125. llxHeader();
  126. $form = new Form($db);
  127. $exportlink = '';
  128. $encaiss = array();
  129. $encaiss_ttc = array();
  130. $decaiss = array();
  131. $decaiss_ttc = array();
  132. // Affiche en-tete du rapport
  133. if ($modecompta == 'CREANCES-DETTES') {
  134. $name = $langs->trans("ReportInOut").', '.$langs->trans("ByYear");
  135. $calcmode = $langs->trans("CalcModeDebt");
  136. $calcmode .= '<br>('.$langs->trans("SeeReportInInputOutputMode", '{s1}', '{s2}').')';
  137. $calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=RECETTES-DEPENSES">', '</a>'), $calcmode);
  138. if (isModEnabled('accounting')) {
  139. $calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '{s1}', '{s2}').')';
  140. $calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=BOOKKEEPING">', '</a>'), $calcmode);
  141. }
  142. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  143. $periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear - 2)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");
  144. $description = $langs->trans("RulesAmountWithTaxExcluded");
  145. $description .= '<br>'.$langs->trans("RulesResultDue");
  146. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  147. $description .= "<br>".$langs->trans("DepositsAreNotIncluded");
  148. } else {
  149. $description .= "<br>".$langs->trans("DepositsAreIncluded");
  150. }
  151. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  152. $description .= $langs->trans("SupplierDepositsAreNotIncluded");
  153. }
  154. $builddate = dol_now();
  155. //$exportlink=$langs->trans("NotYetAvailable");
  156. } elseif ($modecompta == "RECETTES-DEPENSES") {
  157. $name = $langs->trans("ReportInOut").', '.$langs->trans("ByYear");
  158. $calcmode = $langs->trans("CalcModeEngagement");
  159. $calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '{s1}', '{s2}').')';
  160. $calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=CREANCES-DETTES">', '</a>'), $calcmode);
  161. if (isModEnabled('accounting')) {
  162. $calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '{s1}', '{s2}').')';
  163. $calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=BOOKKEEPING">', '</a>'), $calcmode);
  164. }
  165. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  166. $periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear - 2)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");
  167. $description = $langs->trans("RulesAmountWithTaxIncluded");
  168. $description .= '<br>'.$langs->trans("RulesResultInOut");
  169. $builddate = dol_now();
  170. //$exportlink=$langs->trans("NotYetAvailable");
  171. } elseif ($modecompta == "BOOKKEEPING") {
  172. $name = $langs->trans("ReportInOut").', '.$langs->trans("ByYear");
  173. $calcmode = $langs->trans("CalcModeBookkeeping");
  174. $calcmode .= '<br>('.$langs->trans("SeeReportInInputOutputMode", '{s1}', '{s2}').')';
  175. $calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=RECETTES-DEPENSES">', '</a>'), $calcmode);
  176. $calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '{s1}', '{s2}').')';
  177. $calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=CREANCES-DETTES">', '</a>'), $calcmode);
  178. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  179. $periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear - 2)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");
  180. $description = $langs->trans("RulesAmountOnInOutBookkeepingRecord");
  181. $description .= ' ('.$langs->trans("SeePageForSetup", DOL_URL_ROOT.'/accountancy/admin/account.php?mainmenu=accountancy&leftmenu=accountancy_admin', $langs->transnoentitiesnoconv("Accountancy").' / '.$langs->transnoentitiesnoconv("Setup").' / '.$langs->trans("Chartofaccounts")).')';
  182. $builddate = dol_now();
  183. //$exportlink=$langs->trans("NotYetAvailable");
  184. }
  185. $hselected = 'report';
  186. report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink, array('modecompta'=>$modecompta), $calcmode);
  187. if (isModEnabled('accounting') && $modecompta != 'BOOKKEEPING') {
  188. print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);
  189. }
  190. /*
  191. * Factures clients
  192. */
  193. $subtotal_ht = 0;
  194. $subtotal_ttc = 0;
  195. if (isModEnabled('facture') && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES")) {
  196. if ($modecompta == 'CREANCES-DETTES') {
  197. $sql = "SELECT sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc, date_format(f.datef,'%Y-%m') as dm";
  198. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  199. $sql .= ", ".MAIN_DB_PREFIX."facture as f";
  200. $sql .= " WHERE f.fk_soc = s.rowid";
  201. $sql .= " AND f.fk_statut IN (1,2)";
  202. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  203. $sql .= " AND f.type IN (0,1,2,5)";
  204. } else {
  205. $sql .= " AND f.type IN (0,1,2,3,5)";
  206. }
  207. if (!empty($date_start) && !empty($date_end)) {
  208. $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  209. }
  210. } elseif ($modecompta == "RECETTES-DEPENSES") {
  211. /*
  212. * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
  213. * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
  214. */
  215. $sql = "SELECT sum(pf.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
  216. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  217. $sql .= ", ".MAIN_DB_PREFIX."paiement_facture as pf";
  218. $sql .= ", ".MAIN_DB_PREFIX."paiement as p";
  219. $sql .= " WHERE p.rowid = pf.fk_paiement";
  220. $sql .= " AND pf.fk_facture = f.rowid";
  221. if (!empty($date_start) && !empty($date_end)) {
  222. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  223. }
  224. }
  225. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  226. if ($socid) {
  227. $sql .= " AND f.fk_soc = ".((int) $socid);
  228. }
  229. $sql .= " GROUP BY dm";
  230. $sql .= " ORDER BY dm";
  231. //print $sql;
  232. dol_syslog("get customers invoices", LOG_DEBUG);
  233. $result = $db->query($sql);
  234. if ($result) {
  235. $num = $db->num_rows($result);
  236. $i = 0;
  237. while ($i < $num) {
  238. $row = $db->fetch_object($result);
  239. $encaiss[$row->dm] = (isset($row->amount_ht) ? $row->amount_ht : 0);
  240. $encaiss_ttc[$row->dm] = $row->amount_ttc;
  241. $i++;
  242. }
  243. $db->free($result);
  244. } else {
  245. dol_print_error($db);
  246. }
  247. } elseif ($modecompta == "BOOKKEEPING") {
  248. // Nothing from this table
  249. }
  250. if (isModEnabled('facture') && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES")) {
  251. // On ajoute les paiements clients anciennes version, non lies par paiement_facture
  252. if ($modecompta != 'CREANCES-DETTES') {
  253. $sql = "SELECT sum(p.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
  254. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  255. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  256. $sql .= ", ".MAIN_DB_PREFIX."paiement as p";
  257. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
  258. $sql .= " WHERE pf.rowid IS NULL";
  259. $sql .= " AND p.fk_bank = b.rowid";
  260. $sql .= " AND b.fk_account = ba.rowid";
  261. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  262. if (!empty($date_start) && !empty($date_end)) {
  263. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  264. }
  265. $sql .= " GROUP BY dm";
  266. $sql .= " ORDER BY dm";
  267. dol_syslog("get old customers payments not linked to invoices", LOG_DEBUG);
  268. $result = $db->query($sql);
  269. if ($result) {
  270. $num = $db->num_rows($result);
  271. $i = 0;
  272. while ($i < $num) {
  273. $row = $db->fetch_object($result);
  274. if (!isset($encaiss[$row->dm])) {
  275. $encaiss[$row->dm] = 0;
  276. }
  277. $encaiss[$row->dm] += (isset($row->amount_ht) ? $row->amount_ht : 0);
  278. if (!isset($encaiss_ttc[$row->dm])) {
  279. $encaiss_ttc[$row->dm] = 0;
  280. }
  281. $encaiss_ttc[$row->dm] += $row->amount_ttc;
  282. $i++;
  283. }
  284. } else {
  285. dol_print_error($db);
  286. }
  287. } elseif ($modecompta == "RECETTES-DEPENSES") {
  288. // Nothing from this table
  289. }
  290. } elseif ($modecompta == "BOOKKEEPING") {
  291. // Nothing from this table
  292. }
  293. /*
  294. * Frais, factures fournisseurs.
  295. */
  296. $subtotal_ht = 0;
  297. $subtotal_ttc = 0;
  298. if (isModEnabled('facture') && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES")) {
  299. if ($modecompta == 'CREANCES-DETTES') {
  300. $sql = "SELECT sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc, date_format(f.datef,'%Y-%m') as dm";
  301. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  302. $sql .= " WHERE f.fk_statut IN (1,2)";
  303. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  304. $sql .= " AND f.type IN (0,1,2)";
  305. } else {
  306. $sql .= " AND f.type IN (0,1,2,3)";
  307. }
  308. if (!empty($date_start) && !empty($date_end)) {
  309. $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  310. }
  311. } elseif ($modecompta == "RECETTES-DEPENSES") {
  312. $sql = "SELECT sum(pf.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
  313. $sql .= " FROM ".MAIN_DB_PREFIX."paiementfourn as p";
  314. $sql .= ", ".MAIN_DB_PREFIX."facture_fourn as f";
  315. $sql .= ", ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf";
  316. $sql .= " WHERE f.rowid = pf.fk_facturefourn";
  317. $sql .= " AND p.rowid = pf.fk_paiementfourn";
  318. if (!empty($date_start) && !empty($date_end)) {
  319. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  320. }
  321. }
  322. $sql .= " AND f.entity IN (".getEntity('supplier_invoice').")";
  323. if ($socid) {
  324. $sql .= " AND f.fk_soc = ".((int) $socid);
  325. }
  326. $sql .= " GROUP BY dm";
  327. dol_syslog("get suppliers invoices", LOG_DEBUG);
  328. $result = $db->query($sql);
  329. if ($result) {
  330. $num = $db->num_rows($result);
  331. $i = 0;
  332. while ($i < $num) {
  333. $row = $db->fetch_object($result);
  334. if (!isset($decaiss[$row->dm])) {
  335. $decaiss[$row->dm] = 0;
  336. }
  337. $decaiss[$row->dm] = (isset($row->amount_ht) ? $row->amount_ht : 0);
  338. if (!isset($decaiss_ttc[$row->dm])) {
  339. $decaiss_ttc[$row->dm] = 0;
  340. }
  341. $decaiss_ttc[$row->dm] = $row->amount_ttc;
  342. $i++;
  343. }
  344. $db->free($result);
  345. } else {
  346. dol_print_error($db);
  347. }
  348. } elseif ($modecompta == "BOOKKEEPING") {
  349. // Nothing from this table
  350. }
  351. /*
  352. * TVA
  353. */
  354. $subtotal_ht = 0;
  355. $subtotal_ttc = 0;
  356. if (isModEnabled('tax') && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES")) {
  357. if ($modecompta == 'CREANCES-DETTES') {
  358. // TVA collected to pay
  359. $sql = "SELECT sum(f.total_tva) as amount, date_format(f.datef,'%Y-%m') as dm";
  360. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  361. $sql .= " WHERE f.fk_statut IN (1,2)";
  362. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  363. $sql .= " AND f.type IN (0,1,2,5)";
  364. } else {
  365. $sql .= " AND f.type IN (0,1,2,3,5)";
  366. }
  367. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  368. if (!empty($date_start) && !empty($date_end)) {
  369. $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  370. }
  371. $sql .= " GROUP BY dm";
  372. dol_syslog("get vat to pay", LOG_DEBUG);
  373. $result = $db->query($sql);
  374. if ($result) {
  375. $num = $db->num_rows($result);
  376. $i = 0;
  377. if ($num) {
  378. while ($i < $num) {
  379. $obj = $db->fetch_object($result);
  380. /*if (!isset($decaiss[$obj->dm])) {
  381. $decaiss[$obj->dm] = 0;
  382. }
  383. $decaiss[$obj->dm] += $obj->amount;*/
  384. if (!isset($decaiss_ttc[$obj->dm])) {
  385. $decaiss_ttc[$obj->dm] = 0;
  386. }
  387. $decaiss_ttc[$obj->dm] += $obj->amount;
  388. $i++;
  389. }
  390. }
  391. } else {
  392. dol_print_error($db);
  393. }
  394. // TVA paid to get
  395. $sql = "SELECT sum(f.total_tva) as amount, date_format(f.datef,'%Y-%m') as dm";
  396. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  397. $sql .= " WHERE f.fk_statut IN (1,2)";
  398. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  399. $sql .= " AND f.type IN (0,1,2)";
  400. } else {
  401. $sql .= " AND f.type IN (0,1,2,3)";
  402. }
  403. $sql .= " AND f.entity IN (".getEntity('supplier_invoice').")";
  404. if (!empty($date_start) && !empty($date_end)) {
  405. $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  406. }
  407. $sql .= " GROUP BY dm";
  408. dol_syslog("get vat to receive back", LOG_DEBUG);
  409. $result = $db->query($sql);
  410. if ($result) {
  411. $num = $db->num_rows($result);
  412. $i = 0;
  413. if ($num) {
  414. while ($i < $num) {
  415. $obj = $db->fetch_object($result);
  416. /*if (!isset($encaiss[$obj->dm])) {
  417. $encaiss[$obj->dm] = 0;
  418. }
  419. $encaiss[$obj->dm] += $obj->amount;*/
  420. if (!isset($encaiss_ttc[$obj->dm])) {
  421. $encaiss_ttc[$obj->dm] = 0;
  422. }
  423. $encaiss_ttc[$obj->dm] += $obj->amount;
  424. $i++;
  425. }
  426. }
  427. } else {
  428. dol_print_error($db);
  429. }
  430. } elseif ($modecompta == "RECETTES-DEPENSES") {
  431. // TVA really already paid
  432. $sql = "SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm";
  433. $sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
  434. $sql .= " WHERE amount > 0";
  435. $sql .= " AND t.entity IN (".getEntity('vat').")";
  436. if (!empty($date_start) && !empty($date_end)) {
  437. $sql .= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";
  438. }
  439. $sql .= " GROUP BY dm";
  440. dol_syslog("get vat really paid", LOG_DEBUG);
  441. $result = $db->query($sql);
  442. if ($result) {
  443. $num = $db->num_rows($result);
  444. $i = 0;
  445. if ($num) {
  446. while ($i < $num) {
  447. $obj = $db->fetch_object($result);
  448. /*if (!isset($decaiss[$obj->dm])) {
  449. $decaiss[$obj->dm] = 0;
  450. }
  451. $decaiss[$obj->dm] += $obj->amount;*/
  452. if (!isset($decaiss_ttc[$obj->dm])) {
  453. $decaiss_ttc[$obj->dm] = 0;
  454. }
  455. $decaiss_ttc[$obj->dm] += $obj->amount;
  456. $i++;
  457. }
  458. }
  459. } else {
  460. dol_print_error($db);
  461. }
  462. // TVA retrieved
  463. $sql = "SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm";
  464. $sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
  465. $sql .= " WHERE amount < 0";
  466. $sql .= " AND t.entity IN (".getEntity('vat').")";
  467. if (!empty($date_start) && !empty($date_end)) {
  468. $sql .= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";
  469. }
  470. $sql .= " GROUP BY dm";
  471. dol_syslog("get vat really received back", LOG_DEBUG);
  472. $result = $db->query($sql);
  473. if ($result) {
  474. $num = $db->num_rows($result);
  475. $i = 0;
  476. if ($num) {
  477. while ($i < $num) {
  478. $obj = $db->fetch_object($result);
  479. /*if (!isset($encaiss[$obj->dm])) {
  480. $encaiss[$obj->dm] = 0;
  481. }
  482. $encaiss[$obj->dm] += -$obj->amount;*/
  483. if (!isset($encaiss_ttc[$obj->dm])) {
  484. $encaiss_ttc[$obj->dm] = 0;
  485. }
  486. $encaiss_ttc[$obj->dm] += -$obj->amount;
  487. $i++;
  488. }
  489. }
  490. } else {
  491. dol_print_error($db);
  492. }
  493. }
  494. } elseif ($modecompta == "BOOKKEEPING") {
  495. // Nothing from this table
  496. }
  497. /*
  498. * Social contributions
  499. */
  500. $subtotal_ht = 0;
  501. $subtotal_ttc = 0;
  502. if (isModEnabled('tax') && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES")) {
  503. if ($modecompta == 'CREANCES-DETTES') {
  504. $sql = "SELECT c.libelle as nom, date_format(cs.date_ech,'%Y-%m') as dm, sum(cs.amount) as amount";
  505. $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  506. $sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";
  507. $sql .= " WHERE cs.fk_type = c.id";
  508. if (!empty($date_start) && !empty($date_end)) {
  509. $sql .= " AND cs.date_ech >= '".$db->idate($date_start)."' AND cs.date_ech <= '".$db->idate($date_end)."'";
  510. }
  511. } elseif ($modecompta == "RECETTES-DEPENSES") {
  512. $sql = "SELECT c.libelle as nom, date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount";
  513. $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  514. $sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";
  515. $sql .= ", ".MAIN_DB_PREFIX."paiementcharge as p";
  516. $sql .= " WHERE p.fk_charge = cs.rowid";
  517. $sql .= " AND cs.fk_type = c.id";
  518. if (!empty($date_start) && !empty($date_end)) {
  519. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  520. }
  521. }
  522. $sql .= " AND cs.entity IN (".getEntity('social_contributions').")";
  523. $sql .= " GROUP BY c.libelle, dm";
  524. dol_syslog("get social contributions", LOG_DEBUG);
  525. $result = $db->query($sql);
  526. if ($result) {
  527. $num = $db->num_rows($result);
  528. $i = 0;
  529. if ($num) {
  530. while ($i < $num) {
  531. $obj = $db->fetch_object($result);
  532. if (!isset($decaiss[$obj->dm])) {
  533. $decaiss[$obj->dm] = 0;
  534. }
  535. $decaiss[$obj->dm] += $obj->amount;
  536. if (!isset($decaiss_ttc[$obj->dm])) {
  537. $decaiss_ttc[$obj->dm] = 0;
  538. }
  539. $decaiss_ttc[$obj->dm] += $obj->amount;
  540. $i++;
  541. }
  542. }
  543. } else {
  544. dol_print_error($db);
  545. }
  546. } elseif ($modecompta == "BOOKKEEPING") {
  547. // Nothing from this table
  548. }
  549. /*
  550. * Salaries
  551. */
  552. if (isModEnabled('salaries') && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES")) {
  553. if ($modecompta == 'CREANCES-DETTES') {
  554. $column = 's.dateep'; // we use the date of end of period of salary
  555. $sql = "SELECT s.label as nom, date_format(".$column.",'%Y-%m') as dm, sum(s.amount) as amount";
  556. $sql .= " FROM ".MAIN_DB_PREFIX."salary as s";
  557. $sql .= " WHERE s.entity IN (".getEntity('salary').")";
  558. if (!empty($date_start) && !empty($date_end)) {
  559. $sql .= " AND ".$column." >= '".$db->idate($date_start)."' AND ".$column." <= '".$db->idate($date_end)."'";
  560. }
  561. $sql .= " GROUP BY s.label, dm";
  562. }
  563. if ($modecompta == "RECETTES-DEPENSES") {
  564. $column = 'p.datep';
  565. $sql = "SELECT p.label as nom, date_format(".$column.",'%Y-%m') as dm, sum(p.amount) as amount";
  566. $sql .= " FROM ".MAIN_DB_PREFIX."payment_salary as p";
  567. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."salary as s ON p.fk_salary = s.rowid";
  568. $sql .= " WHERE p.entity IN (".getEntity('payment_salary').")";
  569. if (!empty($date_start) && !empty($date_end)) {
  570. $sql .= " AND ".$column." >= '".$db->idate($date_start)."' AND ".$column." <= '".$db->idate($date_end)."'";
  571. }
  572. $sql .= " GROUP BY p.label, dm";
  573. }
  574. $subtotal_ht = 0;
  575. $subtotal_ttc = 0;
  576. dol_syslog("get social salaries payments");
  577. $result = $db->query($sql);
  578. if ($result) {
  579. $num = $db->num_rows($result);
  580. $i = 0;
  581. if ($num) {
  582. while ($i < $num) {
  583. $obj = $db->fetch_object($result);
  584. if (!isset($decaiss[$obj->dm])) {
  585. $decaiss[$obj->dm] = 0;
  586. }
  587. $decaiss[$obj->dm] += $obj->amount;
  588. if (!isset($decaiss_ttc[$obj->dm])) {
  589. $decaiss_ttc[$obj->dm] = 0;
  590. }
  591. $decaiss_ttc[$obj->dm] += $obj->amount;
  592. $i++;
  593. }
  594. }
  595. } else {
  596. dol_print_error($db);
  597. }
  598. } elseif ($modecompta == "BOOKKEEPING") {
  599. // Nothing from this table
  600. }
  601. /*
  602. * Expense reports
  603. */
  604. if (!isModEnabled('expensereport') && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES")) {
  605. $langs->load('trips');
  606. if ($modecompta == 'CREANCES-DETTES') {
  607. $sql = "SELECT date_format(date_valid,'%Y-%m') as dm, sum(p.total_ht) as amount_ht,sum(p.total_ttc) as amount_ttc";
  608. $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as p";
  609. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."user as u ON u.rowid=p.fk_user_author";
  610. $sql .= " WHERE p.entity IN (".getEntity('expensereport').")";
  611. $sql .= " AND p.fk_statut>=5";
  612. $column = 'p.date_valid';
  613. if (!empty($date_start) && !empty($date_end)) {
  614. $sql .= " AND ".$column." >= '".$db->idate($date_start)."' AND ".$column." <= '".$db->idate($date_end)."'";
  615. }
  616. } elseif ($modecompta == 'RECETTES-DEPENSES') {
  617. $sql = "SELECT date_format(pe.datep,'%Y-%m') as dm, sum(p.total_ht) as amount_ht,sum(p.total_ttc) as amount_ttc";
  618. $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as p";
  619. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."user as u ON u.rowid=p.fk_user_author";
  620. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."payment_expensereport as pe ON pe.fk_expensereport = p.rowid";
  621. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as c ON pe.fk_typepayment = c.id";
  622. $sql .= " WHERE p.entity IN (".getEntity('expensereport').")";
  623. $sql .= " AND p.fk_statut>=5";
  624. $column = 'pe.datep';
  625. if (!empty($date_start) && !empty($date_end)) {
  626. $sql .= " AND ".$column." >= '".$db->idate($date_start)."' AND ".$column." <= '".$db->idate($date_end)."'";
  627. }
  628. }
  629. $sql .= " GROUP BY dm";
  630. dol_syslog("get expense report outcome");
  631. $result = $db->query($sql);
  632. $subtotal_ht = 0;
  633. $subtotal_ttc = 0;
  634. if ($result) {
  635. $num = $db->num_rows($result);
  636. if ($num) {
  637. while ($obj = $db->fetch_object($result)) {
  638. if (!isset($decaiss[$obj->dm])) {
  639. $decaiss[$obj->dm] = 0;
  640. }
  641. $decaiss[$obj->dm] += $obj->amount_ht;
  642. if (!isset($decaiss_ttc[$obj->dm])) {
  643. $decaiss_ttc[$obj->dm] = 0;
  644. }
  645. $decaiss_ttc[$obj->dm] += $obj->amount_ttc;
  646. }
  647. }
  648. } else {
  649. dol_print_error($db);
  650. }
  651. } elseif ($modecompta == 'BOOKKEEPING') {
  652. // Nothing from this table
  653. }
  654. /*
  655. * Donation get dunning payments
  656. */
  657. if (isModEnabled('don') && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES")) {
  658. $subtotal_ht = 0;
  659. $subtotal_ttc = 0;
  660. if ($modecompta == 'CREANCES-DETTES') {
  661. $sql = "SELECT p.societe as nom, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";
  662. $sql .= " FROM ".MAIN_DB_PREFIX."don as p";
  663. $sql .= " WHERE p.entity IN (".getEntity('donation').")";
  664. $sql .= " AND fk_statut in (1,2)";
  665. if (!empty($date_start) && !empty($date_end)) {
  666. $sql .= " AND p.datedon >= '".$db->idate($date_start)."' AND p.datedon <= '".$db->idate($date_end)."'";
  667. }
  668. } elseif ($modecompta == 'RECETTES-DEPENSES') {
  669. $sql = "SELECT p.societe as nom, p.firstname, p.lastname, date_format(pe.datep,'%Y-%m') as dm, sum(p.amount) as amount";
  670. $sql .= " FROM ".MAIN_DB_PREFIX."don as p";
  671. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."payment_donation as pe ON pe.fk_donation = p.rowid";
  672. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as c ON pe.fk_typepayment = c.id";
  673. $sql .= " WHERE p.entity IN (".getEntity('donation').")";
  674. $sql .= " AND fk_statut >= 2";
  675. if (!empty($date_start) && !empty($date_end)) {
  676. $sql .= " AND pe.datep >= '".$db->idate($date_start)."' AND pe.datep <= '".$db->idate($date_end)."'";
  677. }
  678. }
  679. $sql .= " GROUP BY p.societe, p.firstname, p.lastname, dm";
  680. dol_syslog("get donation payments");
  681. $result = $db->query($sql);
  682. if ($result) {
  683. $num = $db->num_rows($result);
  684. $i = 0;
  685. if ($num) {
  686. while ($i < $num) {
  687. $obj = $db->fetch_object($result);
  688. if (!isset($encaiss[$obj->dm])) {
  689. $encaiss[$obj->dm] = 0;
  690. }
  691. $encaiss[$obj->dm] += $obj->amount;
  692. if (!isset($encaiss_ttc[$obj->dm])) {
  693. $encaiss_ttc[$obj->dm] = 0;
  694. }
  695. $encaiss_ttc[$obj->dm] += $obj->amount;
  696. $i++;
  697. }
  698. }
  699. } else {
  700. dol_print_error($db);
  701. }
  702. } elseif ($modecompta == 'BOOKKEEPING') {
  703. // Nothing from this table
  704. }
  705. /*
  706. * Various Payments
  707. */
  708. if (!empty($conf->global->ACCOUNTING_REPORTS_INCLUDE_VARPAY) && isModEnabled("banque") && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES")) {
  709. // decaiss
  710. $sql = "SELECT date_format(p.datep, '%Y-%m') AS dm, SUM(p.amount) AS amount FROM ".MAIN_DB_PREFIX."payment_various as p";
  711. $sql .= " WHERE p.entity IN (".getEntity('variouspayment').")";
  712. $sql .= ' AND p.sens = 0';
  713. if (!empty($date_start) && !empty($date_end)) {
  714. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  715. }
  716. $sql .= ' GROUP BY dm';
  717. dol_syslog("get various payments");
  718. $result = $db->query($sql);
  719. if ($result) {
  720. $num = $db->num_rows($result);
  721. $i = 0;
  722. if ($num) {
  723. while ($i < $num) {
  724. $obj = $db->fetch_object($result);
  725. if (!isset($decaiss_ttc[$obj->dm])) {
  726. $decaiss_ttc[$obj->dm] = 0;
  727. }
  728. if (isset($obj->amount)) {
  729. $decaiss_ttc[$obj->dm] += $obj->amount;
  730. }
  731. $i++;
  732. }
  733. }
  734. } else {
  735. dol_print_error($db);
  736. }
  737. // encaiss
  738. $sql = "SELECT date_format(p.datep, '%Y-%m') AS dm, SUM(p.amount) AS amount FROM ".MAIN_DB_PREFIX."payment_various AS p";
  739. $sql .= " WHERE p.entity IN (".getEntity('variouspayment').")";
  740. $sql .= ' AND p.sens = 1';
  741. if (!empty($date_start) && !empty($date_end)) {
  742. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  743. }
  744. $sql .= ' GROUP BY dm';
  745. dol_syslog("get various payments");
  746. $result = $db->query($sql);
  747. if ($result) {
  748. $num = $db->num_rows($result);
  749. $i = 0;
  750. if ($num) {
  751. while ($i < $num) {
  752. $obj = $db->fetch_object($result);
  753. if (!isset($encaiss_ttc[$obj->dm])) {
  754. $encaiss_ttc[$obj->dm] = 0;
  755. }
  756. if (isset($obj->amount)) {
  757. $encaiss_ttc[$obj->dm] += $obj->amount;
  758. }
  759. $i++;
  760. }
  761. }
  762. } else {
  763. dol_print_error($db);
  764. }
  765. }
  766. // Useless with BOOKKEEPING
  767. //elseif ($modecompta == 'BOOKKEEPING') {
  768. //}
  769. /*
  770. * Payment Loan
  771. */
  772. if (!empty($conf->global->ACCOUNTING_REPORTS_INCLUDE_LOAN) && isModEnabled('loan') && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES")) {
  773. $sql = "SELECT date_format(p.datep, '%Y-%m') AS dm, SUM(p.amount_capital + p.amount_insurance + p.amount_interest) AS amount";
  774. $sql .= " FROM ".MAIN_DB_PREFIX."payment_loan AS p, ".MAIN_DB_PREFIX."loan as l";
  775. $sql .= " WHERE l.entity IN (".getEntity('variouspayment').")";
  776. $sql .= " AND p.fk_loan = l.rowid";
  777. if (!empty($date_start) && !empty($date_end)) {
  778. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  779. }
  780. $sql .= ' GROUP BY dm';
  781. dol_syslog("get loan payments");
  782. $result = $db->query($sql);
  783. if ($result) {
  784. $num = $db->num_rows($result);
  785. $i = 0;
  786. if ($num) {
  787. while ($i < $num) {
  788. $obj = $db->fetch_object($result);
  789. if (!isset($decaiss_ttc[$obj->dm])) {
  790. $decaiss_ttc[$obj->dm] = 0;
  791. }
  792. if (isset($obj->amount)) {
  793. $decaiss_ttc[$obj->dm] += $obj->amount;
  794. }
  795. $i++;
  796. }
  797. }
  798. } else {
  799. dol_print_error($db);
  800. }
  801. }
  802. // Useless with BOOKKEEPING
  803. //elseif ($modecompta == 'BOOKKEEPING') {
  804. //}
  805. /*
  806. * Request in mode BOOKKEEPING
  807. */
  808. if (isModEnabled('accounting') && ($modecompta == 'BOOKKEEPING')) {
  809. $predefinedgroupwhere = "(";
  810. $predefinedgroupwhere .= " (aa.pcg_type = 'EXPENSE')";
  811. $predefinedgroupwhere .= " OR ";
  812. $predefinedgroupwhere .= " (aa.pcg_type = 'INCOME')";
  813. $predefinedgroupwhere .= ")";
  814. $charofaccountstring = $conf->global->CHARTOFACCOUNTS;
  815. $charofaccountstring = dol_getIdFromCode($db, $conf->global->CHARTOFACCOUNTS, 'accounting_system', 'rowid', 'pcg_version');
  816. $sql = "SELECT b.doc_ref, b.numero_compte, b.subledger_account, b.subledger_label, aa.pcg_type, date_format(b.doc_date,'%Y-%m') as dm, sum(b.debit) as debit, sum(b.credit) as credit, sum(b.montant) as amount";
  817. $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as b, ".MAIN_DB_PREFIX."accounting_account as aa";
  818. $sql .= " WHERE b.entity = ".$conf->entity;
  819. $sql .= " AND aa.entity = ".$conf->entity;
  820. $sql .= " AND b.numero_compte = aa.account_number";
  821. $sql .= " AND ".$predefinedgroupwhere;
  822. $sql .= " AND fk_pcg_version = '".$db->escape($charofaccountstring)."'";
  823. if (!empty($date_start) && !empty($date_end)) {
  824. $sql .= " AND b.doc_date >= '".$db->idate($date_start)."' AND b.doc_date <= '".$db->idate($date_end)."'";
  825. }
  826. $sql .= " GROUP BY b.doc_ref, b.numero_compte, b.subledger_account, b.subledger_label, pcg_type, dm";
  827. //print $sql;
  828. $subtotal_ht = 0;
  829. $subtotal_ttc = 0;
  830. dol_syslog("get bookkeeping record");
  831. $result = $db->query($sql);
  832. if ($result) {
  833. $num = $db->num_rows($result);
  834. $i = 0;
  835. if ($num) {
  836. while ($i < $num) {
  837. $obj = $db->fetch_object($result);
  838. if ($obj->pcg_type == 'INCOME') {
  839. if (!isset($encaiss[$obj->dm])) {
  840. $encaiss[$obj->dm] = 0; // To avoid warning of var not defined
  841. }
  842. $encaiss[$obj->dm] += $obj->credit;
  843. $encaiss[$obj->dm] -= $obj->debit;
  844. }
  845. if ($obj->pcg_type == 'EXPENSE') {
  846. if (!isset($decaiss[$obj->dm])) {
  847. $decaiss[$obj->dm] = 0; // To avoid warning of var not defined
  848. }
  849. $decaiss[$obj->dm] += $obj->debit;
  850. $decaiss[$obj->dm] -= $obj->credit;
  851. }
  852. // ???
  853. if (!isset($encaiss_ttc[$obj->dm])) {
  854. $encaiss_ttc[$obj->dm] = 0;
  855. }
  856. if (!isset($decaiss_ttc[$obj->dm])) {
  857. $decaiss_ttc[$obj->dm] = 0;
  858. }
  859. $encaiss_ttc[$obj->dm] += 0;
  860. $decaiss_ttc[$obj->dm] += 0;
  861. $i++;
  862. }
  863. }
  864. } else {
  865. dol_print_error($db);
  866. }
  867. }
  868. $action = "balance";
  869. $object = array(&$encaiss, &$encaiss_ttc, &$decaiss, &$decaiss_ttc);
  870. $parameters["mode"] = $modecompta;
  871. // Initialize technical object to manage hooks of expenses. Note that conf->hooks_modules contains array array
  872. $hookmanager->initHooks(array('externalbalance'));
  873. $reshook = $hookmanager->executeHooks('addReportInfo', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  874. /*
  875. * Show result array
  876. */
  877. $totentrees = array();
  878. $totsorties = array();
  879. print '<div class="div-table-responsive">';
  880. print '<table class="tagtable liste">'."\n";
  881. print '<tr class="liste_titre"><td class="liste_titre">&nbsp;</td>';
  882. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  883. print '<td align="center" colspan="2" class="liste_titre borderrightlight">';
  884. print '<a href="clientfourn.php?year='.$annee.'">';
  885. print $annee;
  886. if ($conf->global->SOCIETE_FISCAL_MONTH_START > 1) {
  887. print '-'.($annee + 1);
  888. }
  889. print '</a></td>';
  890. }
  891. print '</tr>';
  892. print '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Month").'</td>';
  893. // Loop on each year to ouput
  894. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  895. print '<td class="liste_titre" align="center">';
  896. $htmlhelp = '';
  897. // if ($modecompta == 'RECETTES-DEPENSES') $htmlhelp=$langs->trans("PurchasesPlusVATEarnedAndDue");
  898. print $form->textwithpicto($langs->trans("Outcome"), $htmlhelp);
  899. print '</td>';
  900. print '<td class="liste_titre" align="center" class="borderrightlight">';
  901. $htmlhelp = '';
  902. // if ($modecompta == 'RECETTES-DEPENSES') $htmlhelp=$langs->trans("SalesPlusVATToRetrieve");
  903. print $form->textwithpicto($langs->trans("Income"), $htmlhelp);
  904. print '</td>';
  905. }
  906. print '</tr>';
  907. // Loop on each month
  908. $nb_mois_decalage = $conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START - 1) : 0;
  909. for ($mois = 1 + $nb_mois_decalage; $mois <= 12 + $nb_mois_decalage; $mois++) {
  910. $mois_modulo = $mois;
  911. if ($mois > 12) {
  912. $mois_modulo = $mois - 12;
  913. }
  914. print '<tr class="oddeven">';
  915. print "<td>".dol_print_date(dol_mktime(12, 0, 0, $mois_modulo, 1, $annee), "%B")."</td>";
  916. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  917. $annee_decalage = $annee;
  918. if ($mois > 12) {
  919. $annee_decalage = $annee + 1;
  920. }
  921. $case = strftime("%Y-%m", dol_mktime(12, 0, 0, $mois_modulo, 1, $annee_decalage));
  922. print '<td class="right">';
  923. if ($modecompta == 'CREANCES-DETTES' || $modecompta == 'BOOKKEEPING') {
  924. if (isset($decaiss[$case]) && $decaiss[$case] != 0) {
  925. print '<a href="clientfourn.php?year='.$annee_decalage.'&month='.$mois_modulo.($modecompta ? '&modecompta='.$modecompta : '').'">'.price(price2num($decaiss[$case], 'MT')).'</a>';
  926. if (!isset($totsorties[$annee])) {
  927. $totsorties[$annee] = 0;
  928. }
  929. $totsorties[$annee] += $decaiss[$case];
  930. }
  931. } else {
  932. if (isset($decaiss_ttc[$case]) && $decaiss_ttc[$case] != 0) {
  933. print '<a href="clientfourn.php?year='.$annee_decalage.'&month='.$mois_modulo.($modecompta ? '&modecompta='.$modecompta : '').'">'.price(price2num($decaiss_ttc[$case], 'MT')).'</a>';
  934. if (!isset($totsorties[$annee])) {
  935. $totsorties[$annee] = 0;
  936. }
  937. $totsorties[$annee] += $decaiss_ttc[$case];
  938. }
  939. }
  940. print "</td>";
  941. print '<td class="borderrightlight nowrap right">';
  942. if ($modecompta == 'CREANCES-DETTES' || $modecompta == 'BOOKKEEPING') {
  943. if (isset($encaiss[$case])) {
  944. print '<a href="clientfourn.php?year='.$annee_decalage.'&month='.$mois_modulo.($modecompta ? '&modecompta='.$modecompta : '').'">'.price(price2num($encaiss[$case], 'MT')).'</a>';
  945. if (!isset($totentrees[$annee])) {
  946. $totentrees[$annee] = 0;
  947. }
  948. $totentrees[$annee] += $encaiss[$case];
  949. }
  950. } else {
  951. if (isset($encaiss_ttc[$case])) {
  952. print '<a href="clientfourn.php?year='.$annee_decalage.'&month='.$mois_modulo.($modecompta ? '&modecompta='.$modecompta : '').'">'.price(price2num($encaiss_ttc[$case], 'MT')).'</a>';
  953. if (!isset($totentrees[$annee])) {
  954. $totentrees[$annee] = 0;
  955. }
  956. $totentrees[$annee] += $encaiss_ttc[$case];
  957. }
  958. }
  959. print "</td>";
  960. }
  961. print '</tr>';
  962. }
  963. // Total
  964. $nbcols = 0;
  965. print '<tr class="liste_total impair"><td>';
  966. if ($modecompta == 'CREANCES-DETTES' || $modecompta == 'BOOKKEEPING') {
  967. print $langs->trans("Total");
  968. } else {
  969. print $langs->trans("TotalTTC");
  970. }
  971. print '</td>';
  972. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  973. $nbcols += 2;
  974. print '<td class="nowrap right">'.(isset($totsorties[$annee]) ?price(price2num($totsorties[$annee], 'MT')) : '&nbsp;').'</td>';
  975. print '<td class="nowrap right" style="border-right: 1px solid #DDD">'.(isset($totentrees[$annee]) ?price(price2num($totentrees[$annee], 'MT')) : '&nbsp;').'</td>';
  976. }
  977. print "</tr>\n";
  978. // Empty line
  979. print '<tr class="impair"><td>&nbsp;</td>';
  980. print '<td colspan="'.$nbcols.'">&nbsp;</td>';
  981. print "</tr>\n";
  982. // Balance
  983. print '<tr class="liste_total"><td>'.$langs->trans("AccountingResult").'</td>';
  984. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  985. print '<td colspan="2" class="borderrightlight right"> ';
  986. if (isset($totentrees[$annee]) || isset($totsorties[$annee])) {
  987. $in = (isset($totentrees[$annee]) ?price2num($totentrees[$annee], 'MT') : 0);
  988. $out = (isset($totsorties[$annee]) ?price2num($totsorties[$annee], 'MT') : 0);
  989. print price(price2num($in - $out, 'MT')).'</td>';
  990. // print '<td>&nbsp;</td>';
  991. }
  992. }
  993. print "</tr>\n";
  994. print "</table>";
  995. print '</div>';
  996. // End of page
  997. llxFooter();
  998. $db->close();