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