clientfourn.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. <?php
  2. /* Copyright (C) 2002-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2012 Cédric Salvador <csalvador@gpcsolutions.fr>
  6. * Copyright (C) 2012-2014 Raphaël Dourseanud <rdoursenaud@gpcsolutions.fr>
  7. * Copyright (C) 2014-2106 Ferran Marcet <fmarcet@2byte.es>
  8. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  9. * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
  10. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  11. * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
  12. * Copyright (C) 2021 Alexandre Spangaro <aspangaro@open-dsi.fr>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  26. */
  27. /**
  28. * \file htdocs/compta/resultat/clientfourn.php
  29. * \ingroup compta, accountancy
  30. * \brief Page reporting
  31. */
  32. // Load Dolibarr environment
  33. require '../../main.inc.php';
  34. require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  38. require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
  39. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  40. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
  41. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancycategory.class.php';
  42. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
  43. // Load translation files required by the page
  44. $langs->loadLangs(array('compta', 'bills', 'donation', 'salaries', 'accountancy', 'loan'));
  45. $date_startmonth = GETPOST('date_startmonth', 'int');
  46. $date_startday = GETPOST('date_startday', 'int');
  47. $date_startyear = GETPOST('date_startyear', 'int');
  48. $date_endmonth = GETPOST('date_endmonth', 'int');
  49. $date_endday = GETPOST('date_endday', 'int');
  50. $date_endyear = GETPOST('date_endyear', 'int');
  51. $showaccountdetail = GETPOST('showaccountdetail', 'aZ09') ? GETPOST('showaccountdetail', 'aZ09') : 'yes';
  52. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  53. $sortfield = GETPOST('sortfield', 'aZ09comma');
  54. $sortorder = GETPOST('sortorder', 'aZ09comma');
  55. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  56. if (empty($page) || $page == -1) {
  57. $page = 0;
  58. } // If $page is not defined, or '' or -1
  59. $offset = $limit * $page;
  60. $pageprev = $page - 1;
  61. $pagenext = $page + 1;
  62. //if (! $sortfield) $sortfield='s.nom, s.rowid';
  63. if (!$sortorder) {
  64. $sortorder = 'ASC';
  65. }
  66. // Date range
  67. $year = GETPOST('year', 'int');
  68. if (empty($year)) {
  69. $year_current = dol_print_date(dol_now(), "%Y");
  70. $month_current = dol_print_date(dol_now(), "%m");
  71. $year_start = $year_current;
  72. } else {
  73. $year_current = $year;
  74. $month_current = dol_print_date(dol_now(), "%m");
  75. $year_start = $year;
  76. }
  77. $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
  78. $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
  79. // We define date_start and date_end
  80. if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
  81. $q = GETPOST("q") ?GETPOST("q") : 0;
  82. if ($q == 0) {
  83. // We define date_start and date_end
  84. $year_end = $year_start;
  85. $month_start = GETPOST("month") ?GETPOST("month") : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
  86. if (!GETPOST('month')) {
  87. if (!GETPOST("year") && $month_start > $month_current) {
  88. $year_start--;
  89. $year_end--;
  90. }
  91. $month_end = $month_start - 1;
  92. if ($month_end < 1) {
  93. $month_end = 12;
  94. } else {
  95. $year_end++;
  96. }
  97. } else {
  98. $month_end = $month_start;
  99. }
  100. $date_start = dol_get_first_day($year_start, $month_start, false);
  101. $date_end = dol_get_last_day($year_end, $month_end, false);
  102. }
  103. if ($q == 1) {
  104. $date_start = dol_get_first_day($year_start, 1, false);
  105. $date_end = dol_get_last_day($year_start, 3, false);
  106. }
  107. if ($q == 2) {
  108. $date_start = dol_get_first_day($year_start, 4, false);
  109. $date_end = dol_get_last_day($year_start, 6, false);
  110. }
  111. if ($q == 3) {
  112. $date_start = dol_get_first_day($year_start, 7, false);
  113. $date_end = dol_get_last_day($year_start, 9, false);
  114. }
  115. if ($q == 4) {
  116. $date_start = dol_get_first_day($year_start, 10, false);
  117. $date_end = dol_get_last_day($year_start, 12, false);
  118. }
  119. }
  120. // $date_start and $date_end are defined. We force $year_start and $nbofyear
  121. $tmps = dol_getdate($date_start);
  122. $year_start = $tmps['year'];
  123. $tmpe = dol_getdate($date_end);
  124. $year_end = $tmpe['year'];
  125. $nbofyear = ($year_end - $year_start) + 1;
  126. //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'));
  127. // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES' or 'BOOKKEEPING')
  128. $modecompta = $conf->global->ACCOUNTING_MODE;
  129. if (isModEnabled('accounting')) {
  130. $modecompta = 'BOOKKEEPING';
  131. }
  132. if (GETPOST("modecompta", 'alpha')) {
  133. $modecompta = GETPOST("modecompta", 'alpha');
  134. }
  135. $AccCat = new AccountancyCategory($db);
  136. // Security check
  137. $socid = GETPOST('socid', 'int');
  138. if ($user->socid > 0) {
  139. $socid = $user->socid;
  140. }
  141. if (isModEnabled('comptabilite')) {
  142. $result = restrictedArea($user, 'compta', '', '', 'resultat');
  143. }
  144. if (isModEnabled('accounting')) {
  145. $result = restrictedArea($user, 'accounting', '', '', 'comptarapport');
  146. }
  147. /*
  148. * View
  149. */
  150. llxHeader();
  151. $form = new Form($db);
  152. $periodlink = '';
  153. $exportlink = '';
  154. $total_ht = 0;
  155. $total_ttc = 0;
  156. // Affiche en-tete de rapport
  157. if ($modecompta == "CREANCES-DETTES") {
  158. $name = $langs->trans("ReportInOut").', '.$langs->trans("ByPredefinedAccountGroups");
  159. $calcmode = $langs->trans("CalcModeDebt");
  160. $calcmode .= '<br>('.$langs->trans("SeeReportInInputOutputMode", '{s1}', '{s2}').')';
  161. $calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=RECETTES-DEPENSES">', '</a>'), $calcmode);
  162. if (isModEnabled('accounting')) {
  163. $calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '{s1}', '{s2}').')';
  164. $calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=BOOKKEEPING">', '</a>'), $calcmode);
  165. }
  166. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  167. $periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($tmps['year'] - 1)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($tmps['year'] + 1)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");
  168. $description = $langs->trans("RulesResultDue");
  169. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  170. $description .= $langs->trans("DepositsAreNotIncluded");
  171. } else {
  172. $description .= $langs->trans("DepositsAreIncluded");
  173. }
  174. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  175. $description .= $langs->trans("SupplierDepositsAreNotIncluded");
  176. }
  177. $builddate = dol_now();
  178. //$exportlink=$langs->trans("NotYetAvailable");
  179. } elseif ($modecompta == "RECETTES-DEPENSES") {
  180. $name = $langs->trans("ReportInOut").', '.$langs->trans("ByPredefinedAccountGroups");
  181. $calcmode = $langs->trans("CalcModeEngagement");
  182. $calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '{s1}', '{s2}').')';
  183. $calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=CREANCES-DETTES">', '</a>'), $calcmode);
  184. if (isModEnabled('accounting')) {
  185. $calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '{s1}', '{s2}').')';
  186. $calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=BOOKKEEPING">', '</a>'), $calcmode);
  187. }
  188. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  189. $periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($tmps['year'] - 1)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($tmps['year'] + 1)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");
  190. $description = $langs->trans("RulesResultInOut");
  191. $builddate = dol_now();
  192. //$exportlink=$langs->trans("NotYetAvailable");
  193. } elseif ($modecompta == "BOOKKEEPING") {
  194. $name = $langs->trans("ReportInOut").', '.$langs->trans("ByPredefinedAccountGroups");
  195. $calcmode = $langs->trans("CalcModeBookkeeping");
  196. $calcmode .= '<br>('.$langs->trans("SeeReportInInputOutputMode", '{s1}', '{s2}').')';
  197. $calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=RECETTES-DEPENSES">', '</a>'), $calcmode);
  198. $calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '{s1}', '{s2}').')';
  199. $calcmode = str_replace(array('{s1}', '{s2}'), array('<a href="'.$_SERVER["PHP_SELF"].'?date_startyear='.$tmps['year'].'&date_startmonth='.$tmps['mon'].'&date_startday='.$tmps['mday'].'&date_endyear='.$tmpe['year'].'&date_endmonth='.$tmpe['mon'].'&date_endday='.$tmpe['mday'].'&modecompta=CREANCES-DETTES">', '</a>'), $calcmode);
  200. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  201. $arraylist = array('no'=>$langs->trans("CustomerCode"), 'yes'=>$langs->trans("AccountWithNonZeroValues"), 'all'=>$langs->trans("All"));
  202. $period .= ' &nbsp; &nbsp; '.$langs->trans("DetailByAccount").' '.$form->selectarray('showaccountdetail', $arraylist, $showaccountdetail, 0);
  203. $periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($tmps['year'] - 1)."&modecompta=".$modecompta."&showaccountdetail=".$showaccountdetail."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($tmps['year'] + 1)."&modecompta=".$modecompta."&showaccountdetail=".$showaccountdetail."'>".img_next()."</a>" : "");
  204. $description = $langs->trans("RulesResultBookkeepingPredefined");
  205. $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")).')';
  206. $builddate = dol_now();
  207. //$exportlink=$langs->trans("NotYetAvailable");
  208. }
  209. $hselected = 'report';
  210. report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink, array('modecompta'=>$modecompta, 'showaccountdetail'=>$showaccountdetail), $calcmode);
  211. if (isModEnabled('accounting') && $modecompta != 'BOOKKEEPING') {
  212. print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);
  213. }
  214. // Show report array
  215. $param = '&modecompta='.urlencode($modecompta).'&showaccountdetail='.urlencode($showaccountdetail);
  216. if ($date_startday) {
  217. $param .= '&date_startday='.$date_startday;
  218. }
  219. if ($date_startmonth) {
  220. $param .= '&date_startmonth='.$date_startmonth;
  221. }
  222. if ($date_startyear) {
  223. $param .= '&date_startyear='.$date_startyear;
  224. }
  225. if ($date_endday) {
  226. $param .= '&date_endday='.$date_endday;
  227. }
  228. if ($date_endmonth) {
  229. $param .= '&date_endmonth='.$date_endmonth;
  230. }
  231. if ($date_endyear) {
  232. $param .= '&date_endyear='.$date_endyear;
  233. }
  234. print '<table class="noborder centpercent">';
  235. print '<tr class="liste_titre">';
  236. if ($modecompta == 'BOOKKEEPING') {
  237. print_liste_field_titre("PredefinedGroups", $_SERVER["PHP_SELF"], 'f.thirdparty_code,f.rowid', '', $param, '', $sortfield, $sortorder, 'width200 ');
  238. } else {
  239. print_liste_field_titre("", $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder, 'width200 ');
  240. }
  241. print_liste_field_titre('');
  242. if ($modecompta == 'BOOKKEEPING') {
  243. print_liste_field_titre("Amount", $_SERVER["PHP_SELF"], 'amount', '', $param, 'class="right"', $sortfield, $sortorder);
  244. } else {
  245. if ($modecompta == 'CREANCES-DETTES') {
  246. print_liste_field_titre("AmountHT", $_SERVER["PHP_SELF"], 'amount_ht', '', $param, 'class="right"', $sortfield, $sortorder);
  247. } else {
  248. print_liste_field_titre(''); // Make 4 columns in total whatever $modecompta is
  249. }
  250. print_liste_field_titre("AmountTTC", $_SERVER["PHP_SELF"], 'amount_ttc', '', $param, 'class="right"', $sortfield, $sortorder);
  251. }
  252. print "</tr>\n";
  253. $total_ht_outcome = $total_ttc_outcome = $total_ht_income = $total_ttc_income = 0;
  254. if ($modecompta == 'BOOKKEEPING') {
  255. $predefinedgroupwhere = "(";
  256. $predefinedgroupwhere .= " (pcg_type = 'EXPENSE')";
  257. $predefinedgroupwhere .= " OR ";
  258. $predefinedgroupwhere .= " (pcg_type = 'INCOME')";
  259. $predefinedgroupwhere .= ")";
  260. $charofaccountstring = $conf->global->CHARTOFACCOUNTS;
  261. $charofaccountstring = dol_getIdFromCode($db, $conf->global->CHARTOFACCOUNTS, 'accounting_system', 'rowid', 'pcg_version');
  262. $sql = "SELECT -1 as socid, aa.pcg_type, SUM(f.credit - f.debit) as amount";
  263. if ($showaccountdetail == 'no') {
  264. $sql .= ", f.thirdparty_code as name";
  265. }
  266. $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as f";
  267. $sql .= ", ".MAIN_DB_PREFIX."accounting_account as aa";
  268. $sql .= " WHERE f.numero_compte = aa.account_number";
  269. $sql .= " AND ".$predefinedgroupwhere;
  270. $sql .= " AND fk_pcg_version = '".$db->escape($charofaccountstring)."'";
  271. $sql .= " AND f.entity = ".$conf->entity;
  272. if (!empty($date_start) && !empty($date_end)) {
  273. $sql .= " AND f.doc_date >= '".$db->idate($date_start)."' AND f.doc_date <= '".$db->idate($date_end)."'";
  274. }
  275. $sql .= " GROUP BY pcg_type";
  276. if ($showaccountdetail == 'no') {
  277. $sql .= ", name, socid"; // group by "accounting group" (INCOME/EXPENSE), then "customer".
  278. }
  279. $sql .= $db->order($sortfield, $sortorder);
  280. $oldpcgtype = '';
  281. dol_syslog("get bookkeeping entries", LOG_DEBUG);
  282. $result = $db->query($sql);
  283. if ($result) {
  284. $num = $db->num_rows($result);
  285. $i = 0;
  286. if ($num > 0) {
  287. while ($i < $num) {
  288. $objp = $db->fetch_object($result);
  289. if ($showaccountdetail == 'no') {
  290. if ($objp->pcg_type != $oldpcgtype) {
  291. print '<tr class="trforbreak"><td colspan="3" class="tdforbreak">'.dol_escape_htmltag($objp->pcg_type).'</td></tr>';
  292. $oldpcgtype = $objp->pcg_type;
  293. }
  294. }
  295. if ($showaccountdetail == 'no') {
  296. print '<tr class="oddeven">';
  297. print '<td></td>';
  298. print '<td>';
  299. print dol_escape_htmltag($objp->pcg_type);
  300. print ($objp->name ? ' ('.dol_escape_htmltag($objp->name).')' : ' ('.$langs->trans("Unknown").')');
  301. print "</td>\n";
  302. print '<td class="right nowraponall"><span class="amount">'.price($objp->amount)."</span></td>\n";
  303. print "</tr>\n";
  304. } else {
  305. print '<tr class="oddeven trforbreak">';
  306. print '<td colspan="2" class="tdforbreak">';
  307. print dol_escape_htmltag($objp->pcg_type);
  308. print "</td>\n";
  309. print '<td class="right nowraponall tdforbreak"><span class="amount">'.price($objp->amount)."</span></td>\n";
  310. print "</tr>\n";
  311. }
  312. $total_ht += (isset($objp->amount) ? $objp->amount : 0);
  313. $total_ttc += (isset($objp->amount) ? $objp->amount : 0);
  314. if ($objp->pcg_type == 'INCOME') {
  315. $total_ht_income += (isset($objp->amount) ? $objp->amount : 0);
  316. $total_ttc_income += (isset($objp->amount) ? $objp->amount : 0);
  317. }
  318. if ($objp->pcg_type == 'EXPENSE') {
  319. $total_ht_outcome -= (isset($objp->amount) ? $objp->amount : 0);
  320. $total_ttc_outcome -= (isset($objp->amount) ? $objp->amount : 0);
  321. }
  322. // Loop on detail of all accounts
  323. // This make 14 calls for each detail of account (NP, N and month m)
  324. if ($showaccountdetail != 'no') {
  325. $tmppredefinedgroupwhere = "pcg_type = '".$db->escape($objp->pcg_type)."'";
  326. $tmppredefinedgroupwhere .= " AND fk_pcg_version = '".$db->escape($charofaccountstring)."'";
  327. //$tmppredefinedgroupwhere .= " AND thirdparty_code = '".$db->escape($objp->name)."'";
  328. // Get cpts of category/group
  329. $cpts = $AccCat->getCptsCat(0, $tmppredefinedgroupwhere);
  330. foreach ($cpts as $j => $cpt) {
  331. $return = $AccCat->getSumDebitCredit($cpt['account_number'], $date_start, $date_end, (empty($cpt['dc']) ? 0 : $cpt['dc']));
  332. if ($return < 0) {
  333. setEventMessages(null, $AccCat->errors, 'errors');
  334. $resultN = 0;
  335. } else {
  336. $resultN = $AccCat->sdc;
  337. }
  338. if ($showaccountdetail == 'all' || $resultN <> 0) {
  339. print '<tr>';
  340. print '<td></td>';
  341. print '<td class="tdoverflowmax200"> &nbsp; &nbsp; '.length_accountg($cpt['account_number']).' - '.$cpt['account_label'].'</td>';
  342. print '<td class="right nowraponall"><span class="amount">'.price($resultN).'</span></td>';
  343. print "</tr>\n";
  344. }
  345. }
  346. }
  347. $i++;
  348. }
  349. } else {
  350. print '<tr><td colspan="3" class="opacitymedium">'.$langs->trans("NoRecordFound").'</td></tr>';
  351. }
  352. } else {
  353. dol_print_error($db);
  354. }
  355. } else {
  356. /*
  357. * Customer invoices
  358. */
  359. print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("CustomersInvoices").'</td></tr>';
  360. if ($modecompta == 'CREANCES-DETTES') {
  361. $sql = "SELECT s.nom as name, s.rowid as socid, sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc";
  362. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  363. $sql .= ", ".MAIN_DB_PREFIX."facture as f";
  364. $sql .= " WHERE f.fk_soc = s.rowid";
  365. $sql .= " AND f.fk_statut IN (1,2)";
  366. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  367. $sql .= " AND f.type IN (0,1,2,5)";
  368. } else {
  369. $sql .= " AND f.type IN (0,1,2,3,5)";
  370. }
  371. if (!empty($date_start) && !empty($date_end)) {
  372. $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  373. }
  374. } elseif ($modecompta == 'RECETTES-DEPENSES') {
  375. /*
  376. * List of payments (old payments are not seen by this query because, on older versions, they were not linked via payment_invoice.
  377. * old versions, they were not linked via payment_invoice. They are added later)
  378. */
  379. $sql = "SELECT s.nom as name, s.rowid as socid, sum(pf.amount) as amount_ttc";
  380. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  381. $sql .= ", ".MAIN_DB_PREFIX."facture as f";
  382. $sql .= ", ".MAIN_DB_PREFIX."paiement_facture as pf";
  383. $sql .= ", ".MAIN_DB_PREFIX."paiement as p";
  384. $sql .= " WHERE p.rowid = pf.fk_paiement";
  385. $sql .= " AND pf.fk_facture = f.rowid";
  386. $sql .= " AND f.fk_soc = s.rowid";
  387. if (!empty($date_start) && !empty($date_end)) {
  388. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  389. }
  390. }
  391. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  392. if ($socid) {
  393. $sql .= " AND f.fk_soc = ".((int) $socid);
  394. }
  395. $sql .= " GROUP BY name, socid";
  396. $sql .= $db->order($sortfield, $sortorder);
  397. dol_syslog("get customer invoices", LOG_DEBUG);
  398. $result = $db->query($sql);
  399. if ($result) {
  400. $num = $db->num_rows($result);
  401. $i = 0;
  402. while ($i < $num) {
  403. $objp = $db->fetch_object($result);
  404. print '<tr class="oddeven">';
  405. print '<td>&nbsp;</td>';
  406. print "<td>".$langs->trans("Bills").' <a href="'.DOL_URL_ROOT.'/compta/facture/list.php?socid='.$objp->socid.'">'.$objp->name."</td>\n";
  407. print '<td class="right">';
  408. if ($modecompta == 'CREANCES-DETTES') {
  409. print '<span class="amount">'.price($objp->amount_ht)."</span>";
  410. }
  411. print "</td>\n";
  412. print '<td class="right"><span class="amount">'.price($objp->amount_ttc)."</span></td>\n";
  413. $total_ht += (isset($objp->amount_ht) ? $objp->amount_ht : 0);
  414. $total_ttc += $objp->amount_ttc;
  415. print "</tr>\n";
  416. $i++;
  417. }
  418. $db->free($result);
  419. } else {
  420. dol_print_error($db);
  421. }
  422. // We add the old customer payments, not linked by payment_invoice
  423. if ($modecompta == 'RECETTES-DEPENSES') {
  424. $sql = "SELECT 'Autres' as name, '0' as idp, sum(p.amount) as amount_ttc";
  425. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  426. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  427. $sql .= ", ".MAIN_DB_PREFIX."paiement as p";
  428. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
  429. $sql .= " WHERE pf.rowid IS NULL";
  430. $sql .= " AND p.fk_bank = b.rowid";
  431. $sql .= " AND b.fk_account = ba.rowid";
  432. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  433. if (!empty($date_start) && !empty($date_end)) {
  434. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  435. }
  436. $sql .= " GROUP BY name, idp";
  437. $sql .= " ORDER BY name";
  438. dol_syslog("get old customer payments not linked to invoices", LOG_DEBUG);
  439. $result = $db->query($sql);
  440. if ($result) {
  441. $num = $db->num_rows($result);
  442. $i = 0;
  443. if ($num) {
  444. while ($i < $num) {
  445. $objp = $db->fetch_object($result);
  446. print '<tr class="oddeven">';
  447. print '<td>&nbsp;</td>';
  448. print "<td>".$langs->trans("Bills")." ".$langs->trans("Other")." (".$langs->trans("PaymentsNotLinkedToInvoice").")\n";
  449. print '<td class="right">';
  450. if ($modecompta == 'CREANCES-DETTES') {
  451. print '<span class="amount">'.price($objp->amount_ht)."</span></td>\n";
  452. }
  453. print '</td>';
  454. print '<td class="right"><span class="amount">'.price($objp->amount_ttc)."</span></td>\n";
  455. $total_ht += (isset($objp->amount_ht) ? $objp->amount_ht : 0);
  456. $total_ttc += $objp->amount_ttc;
  457. print "</tr>\n";
  458. $i++;
  459. }
  460. }
  461. $db->free($result);
  462. } else {
  463. dol_print_error($db);
  464. }
  465. }
  466. if ($total_ttc == 0) {
  467. print '<tr class="oddeven">';
  468. print '<td>&nbsp;</td>';
  469. print '<td colspan="3"><span class="opacitymedium">'.$langs->trans("None").'</span></td>';
  470. print '</tr>';
  471. }
  472. $total_ht_income += $total_ht;
  473. $total_ttc_income += $total_ttc;
  474. print '<tr class="liste_total">';
  475. print '<td></td>';
  476. print '<td></td>';
  477. print '<td class="right">';
  478. if ($modecompta == 'CREANCES-DETTES') {
  479. print price($total_ht);
  480. }
  481. print '</td>';
  482. print '<td class="right">'.price($total_ttc).'</td>';
  483. print '</tr>';
  484. /*
  485. * Donations
  486. */
  487. if (isModEnabled('don')) {
  488. print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("Donations").'</td></tr>';
  489. if ($modecompta == 'CREANCES-DETTES' || $modecompta == 'RECETTES-DEPENSES') {
  490. if ($modecompta == 'CREANCES-DETTES') {
  491. $sql = "SELECT p.societe as name, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";
  492. $sql .= " FROM ".MAIN_DB_PREFIX."don as p";
  493. $sql .= " WHERE p.entity IN (".getEntity('donation').")";
  494. $sql .= " AND fk_statut in (1,2)";
  495. } else {
  496. $sql = "SELECT p.societe as nom, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";
  497. $sql .= " FROM ".MAIN_DB_PREFIX."don as p";
  498. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."payment_donation as pe ON pe.fk_donation = p.rowid";
  499. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as c ON pe.fk_typepayment = c.id";
  500. $sql .= " WHERE p.entity IN (".getEntity('donation').")";
  501. $sql .= " AND fk_statut >= 2";
  502. }
  503. if (!empty($date_start) && !empty($date_end)) {
  504. $sql .= " AND p.datedon >= '".$db->idate($date_start)."' AND p.datedon <= '".$db->idate($date_end)."'";
  505. }
  506. }
  507. $sql .= " GROUP BY p.societe, p.firstname, p.lastname, dm";
  508. $newsortfield = $sortfield;
  509. if ($newsortfield == 's.nom, s.rowid') {
  510. $newsortfield = 'p.societe, p.firstname, p.lastname, dm';
  511. }
  512. if ($newsortfield == 'amount_ht') {
  513. $newsortfield = 'amount';
  514. }
  515. if ($newsortfield == 'amount_ttc') {
  516. $newsortfield = 'amount';
  517. }
  518. $sql .= $db->order($newsortfield, $sortorder);
  519. dol_syslog("get dunning");
  520. $result = $db->query($sql);
  521. $subtotal_ht = 0;
  522. $subtotal_ttc = 0;
  523. if ($result) {
  524. $num = $db->num_rows($result);
  525. $i = 0;
  526. if ($num) {
  527. while ($i < $num) {
  528. $obj = $db->fetch_object($result);
  529. $total_ht += $obj->amount;
  530. $total_ttc += $obj->amount;
  531. $subtotal_ht += $obj->amount;
  532. $subtotal_ttc += $obj->amount;
  533. print '<tr class="oddeven">';
  534. print '<td>&nbsp;</td>';
  535. print "<td>".$langs->trans("Donation")." <a href=\"".DOL_URL_ROOT."/don/list.php?search_company=".$obj->name."&search_name=".$obj->firstname." ".$obj->lastname."\">".$obj->name." ".$obj->firstname." ".$obj->lastname."</a></td>\n";
  536. print '<td class="right">';
  537. if ($modecompta == 'CREANCES-DETTES') {
  538. print '<span class="amount">'.price($obj->amount).'</span>';
  539. }
  540. print '</td>';
  541. print '<td class="right"><span class="amount">'.price($obj->amount).'</span></td>';
  542. print '</tr>';
  543. $i++;
  544. }
  545. } else {
  546. print '<tr class="oddeven"><td>&nbsp;</td>';
  547. print '<td colspan="3"><span class="opacitymedium">'.$langs->trans("None").'</span></td>';
  548. print '</tr>';
  549. }
  550. } else {
  551. dol_print_error($db);
  552. }
  553. $total_ht_income += $subtotal_ht;
  554. $total_ttc_income += $subtotal_ttc;
  555. print '<tr class="liste_total">';
  556. print '<td></td>';
  557. print '<td></td>';
  558. print '<td class="right">';
  559. if ($modecompta == 'CREANCES-DETTES') {
  560. print price($subtotal_ht);
  561. }
  562. print '</td>';
  563. print '<td class="right">'.price($subtotal_ttc).'</td>';
  564. print '</tr>';
  565. }
  566. /*
  567. * Suppliers invoices
  568. */
  569. if ($modecompta == 'CREANCES-DETTES') {
  570. $sql = "SELECT s.nom as name, s.rowid as socid, sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc";
  571. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  572. $sql .= ", ".MAIN_DB_PREFIX."facture_fourn as f";
  573. $sql .= " WHERE f.fk_soc = s.rowid";
  574. $sql .= " AND f.fk_statut IN (1,2)";
  575. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  576. $sql .= " AND f.type IN (0,1,2)";
  577. } else {
  578. $sql .= " AND f.type IN (0,1,2,3)";
  579. }
  580. if (!empty($date_start) && !empty($date_end)) {
  581. $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  582. }
  583. } elseif ($modecompta == 'RECETTES-DEPENSES') {
  584. $sql = "SELECT s.nom as name, s.rowid as socid, sum(pf.amount) as amount_ttc";
  585. $sql .= " FROM ".MAIN_DB_PREFIX."paiementfourn as p";
  586. $sql .= ", ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf";
  587. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f";
  588. $sql .= " ON pf.fk_facturefourn = f.rowid";
  589. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s";
  590. $sql .= " ON f.fk_soc = s.rowid";
  591. $sql .= " WHERE p.rowid = pf.fk_paiementfourn ";
  592. if (!empty($date_start) && !empty($date_end)) {
  593. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  594. }
  595. }
  596. $sql .= " AND f.entity = ".((int) $conf->entity);
  597. if ($socid) {
  598. $sql .= " AND f.fk_soc = ".((int) $socid);
  599. }
  600. $sql .= " GROUP BY name, socid";
  601. $sql .= $db->order($sortfield, $sortorder);
  602. print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("SuppliersInvoices").'</td></tr>';
  603. $subtotal_ht = 0;
  604. $subtotal_ttc = 0;
  605. dol_syslog("get suppliers invoices", LOG_DEBUG);
  606. $result = $db->query($sql);
  607. if ($result) {
  608. $num = $db->num_rows($result);
  609. $i = 0;
  610. if ($num > 0) {
  611. while ($i < $num) {
  612. $objp = $db->fetch_object($result);
  613. print '<tr class="oddeven">';
  614. print '<td>&nbsp;</td>';
  615. print "<td>".$langs->trans("Bills").' <a href="'.DOL_URL_ROOT."/fourn/facture/list.php?socid=".$objp->socid.'">'.$objp->name.'</a></td>'."\n";
  616. print '<td class="right">';
  617. if ($modecompta == 'CREANCES-DETTES') {
  618. print '<span class="amount">'.price(-$objp->amount_ht)."</span>";
  619. }
  620. print "</td>\n";
  621. print '<td class="right"><span class="amount">'.price(-$objp->amount_ttc)."</span></td>\n";
  622. $total_ht -= (isset($objp->amount_ht) ? $objp->amount_ht : 0);
  623. $total_ttc -= $objp->amount_ttc;
  624. $subtotal_ht += (isset($objp->amount_ht) ? $objp->amount_ht : 0);
  625. $subtotal_ttc += $objp->amount_ttc;
  626. print "</tr>\n";
  627. $i++;
  628. }
  629. } else {
  630. print '<tr class="oddeven">';
  631. print '<td>&nbsp;</td>';
  632. print '<td colspan="3"><span class="opacitymedium">'.$langs->trans("None").'</span></td>';
  633. print '</tr>';
  634. }
  635. $db->free($result);
  636. } else {
  637. dol_print_error($db);
  638. }
  639. $total_ht_outcome += $subtotal_ht;
  640. $total_ttc_outcome += $subtotal_ttc;
  641. print '<tr class="liste_total">';
  642. print '<td></td>';
  643. print '<td></td>';
  644. print '<td class="right">';
  645. if ($modecompta == 'CREANCES-DETTES') {
  646. print price(-$subtotal_ht);
  647. }
  648. print '</td>';
  649. print '<td class="right">'.price(-$subtotal_ttc).'</td>';
  650. print '</tr>';
  651. /*
  652. * Social / Fiscal contributions who are not deductible
  653. */
  654. print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("SocialContributionsNondeductibles").'</td></tr>';
  655. if ($modecompta == 'CREANCES-DETTES') {
  656. $sql = "SELECT c.id, c.libelle as label, c.accountancy_code, sum(cs.amount) as amount";
  657. $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  658. $sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";
  659. $sql .= " WHERE cs.fk_type = c.id";
  660. $sql .= " AND c.deductible = 0";
  661. if (!empty($date_start) && !empty($date_end)) {
  662. $sql .= " AND cs.date_ech >= '".$db->idate($date_start)."' AND cs.date_ech <= '".$db->idate($date_end)."'";
  663. }
  664. } elseif ($modecompta == 'RECETTES-DEPENSES') {
  665. $sql = "SELECT c.id, c.libelle as label, c.accountancy_code, sum(p.amount) as amount";
  666. $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  667. $sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";
  668. $sql .= ", ".MAIN_DB_PREFIX."paiementcharge as p";
  669. $sql .= " WHERE p.fk_charge = cs.rowid";
  670. $sql .= " AND cs.fk_type = c.id";
  671. $sql .= " AND c.deductible = 0";
  672. if (!empty($date_start) && !empty($date_end)) {
  673. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  674. }
  675. }
  676. $sql .= " AND cs.entity = ".$conf->entity;
  677. $sql .= " GROUP BY c.libelle, c.id, c.accountancy_code";
  678. $newsortfield = $sortfield;
  679. if ($newsortfield == 's.nom, s.rowid') {
  680. $newsortfield = 'c.libelle, c.id';
  681. }
  682. if ($newsortfield == 'amount_ht') {
  683. $newsortfield = 'amount';
  684. }
  685. if ($newsortfield == 'amount_ttc') {
  686. $newsortfield = 'amount';
  687. }
  688. $sql .= $db->order($newsortfield, $sortorder);
  689. dol_syslog("get social contributions deductible=0", LOG_DEBUG);
  690. $result = $db->query($sql);
  691. $subtotal_ht = 0;
  692. $subtotal_ttc = 0;
  693. if ($result) {
  694. $num = $db->num_rows($result);
  695. $i = 0;
  696. if ($num) {
  697. while ($i < $num) {
  698. $obj = $db->fetch_object($result);
  699. $total_ht -= $obj->amount;
  700. $total_ttc -= $obj->amount;
  701. $subtotal_ht += $obj->amount;
  702. $subtotal_ttc += $obj->amount;
  703. $titletoshow = '';
  704. if ($obj->accountancy_code) {
  705. $titletoshow = $langs->trans("AccountingCode").': '.$obj->accountancy_code;
  706. $tmpaccountingaccount = new AccountingAccount($db);
  707. $tmpaccountingaccount->fetch(0, $obj->accountancy_code, 1);
  708. $titletoshow .= ' - '.$langs->trans("AccountingCategory").': '.$tmpaccountingaccount->pcg_type;
  709. }
  710. print '<tr class="oddeven">';
  711. print '<td>&nbsp;</td>';
  712. print '<td'.($obj->accountancy_code ? ' title="'.dol_escape_htmltag($titletoshow).'"' : '').'>'.dol_escape_htmltag($obj->label).'</td>';
  713. print '<td class="right">';
  714. if ($modecompta == 'CREANCES-DETTES') {
  715. print '<span class="amount">'.price(-$obj->amount).'</span>';
  716. }
  717. print '</td>';
  718. print '<td class="right"><span class="amount">'.price(-$obj->amount).'</span></td>';
  719. print '</tr>';
  720. $i++;
  721. }
  722. } else {
  723. print '<tr class="oddeven">';
  724. print '<td>&nbsp;</td>';
  725. print '<td colspan="3"><span class="opacitymedium">'.$langs->trans("None").'</span></td>';
  726. print '</tr>';
  727. }
  728. } else {
  729. dol_print_error($db);
  730. }
  731. $total_ht_outcome += $subtotal_ht;
  732. $total_ttc_outcome += $subtotal_ttc;
  733. print '<tr class="liste_total">';
  734. print '<td></td>';
  735. print '<td></td>';
  736. print '<td class="right">';
  737. if ($modecompta == 'CREANCES-DETTES') {
  738. print price(-$subtotal_ht);
  739. }
  740. print '</td>';
  741. print '<td class="right">'.price(-$subtotal_ttc).'</td>';
  742. print '</tr>';
  743. /*
  744. * Social / Fiscal contributions who are deductible
  745. */
  746. print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("SocialContributionsDeductibles").'</td></tr>';
  747. if ($modecompta == 'CREANCES-DETTES') {
  748. $sql = "SELECT c.id, c.libelle as label, c.accountancy_code, sum(cs.amount) as amount";
  749. $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  750. $sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";
  751. $sql .= " WHERE cs.fk_type = c.id";
  752. $sql .= " AND c.deductible = 1";
  753. if (!empty($date_start) && !empty($date_end)) {
  754. $sql .= " AND cs.date_ech >= '".$db->idate($date_start)."' AND cs.date_ech <= '".$db->idate($date_end)."'";
  755. }
  756. $sql .= " AND cs.entity = ".$conf->entity;
  757. } elseif ($modecompta == 'RECETTES-DEPENSES') {
  758. $sql = "SELECT c.id, c.libelle as label, c.accountancy_code, sum(p.amount) as amount";
  759. $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  760. $sql .= ", ".MAIN_DB_PREFIX."chargesociales as cs";
  761. $sql .= ", ".MAIN_DB_PREFIX."paiementcharge as p";
  762. $sql .= " WHERE p.fk_charge = cs.rowid";
  763. $sql .= " AND cs.fk_type = c.id";
  764. $sql .= " AND c.deductible = 1";
  765. if (!empty($date_start) && !empty($date_end)) {
  766. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  767. }
  768. $sql .= " AND cs.entity = ".$conf->entity;
  769. }
  770. $sql .= " GROUP BY c.libelle, c.id, c.accountancy_code";
  771. $newsortfield = $sortfield;
  772. if ($newsortfield == 's.nom, s.rowid') {
  773. $newsortfield = 'c.libelle, c.id';
  774. }
  775. if ($newsortfield == 'amount_ht') {
  776. $newsortfield = 'amount';
  777. }
  778. if ($newsortfield == 'amount_ttc') {
  779. $newsortfield = 'amount';
  780. }
  781. $sql .= $db->order($newsortfield, $sortorder);
  782. dol_syslog("get social contributions deductible=1", LOG_DEBUG);
  783. $result = $db->query($sql);
  784. $subtotal_ht = 0;
  785. $subtotal_ttc = 0;
  786. if ($result) {
  787. $num = $db->num_rows($result);
  788. $i = 0;
  789. if ($num) {
  790. while ($i < $num) {
  791. $obj = $db->fetch_object($result);
  792. $total_ht -= $obj->amount;
  793. $total_ttc -= $obj->amount;
  794. $subtotal_ht += $obj->amount;
  795. $subtotal_ttc += $obj->amount;
  796. $titletoshow = '';
  797. if ($obj->accountancy_code) {
  798. $titletoshow = $langs->trans("AccountingCode").': '.$obj->accountancy_code;
  799. $tmpaccountingaccount = new AccountingAccount($db);
  800. $tmpaccountingaccount->fetch(0, $obj->accountancy_code, 1);
  801. $titletoshow .= ' - '.$langs->trans("AccountingCategory").': '.$tmpaccountingaccount->pcg_type;
  802. }
  803. print '<tr class="oddeven">';
  804. print '<td>&nbsp;</td>';
  805. print '<td'.($obj->accountancy_code ? ' title="'.dol_escape_htmltag($titletoshow).'"' : '').'>'.dol_escape_htmltag($obj->label).'</td>';
  806. print '<td class="right">';
  807. if ($modecompta == 'CREANCES-DETTES') {
  808. print '<span class="amount">'.price(-$obj->amount).'</span>';
  809. }
  810. print '</td>';
  811. print '<td class="right"><span class="amount">'.price(-$obj->amount).'</span></td>';
  812. print '</tr>';
  813. $i++;
  814. }
  815. } else {
  816. print '<tr class="oddeven">';
  817. print '<td>&nbsp;</td>';
  818. print '<td colspan="3"><span class="opacitymedium">'.$langs->trans("None").'</span></td>';
  819. print '</tr>';
  820. }
  821. } else {
  822. dol_print_error($db);
  823. }
  824. $total_ht_outcome += $subtotal_ht;
  825. $total_ttc_outcome += $subtotal_ttc;
  826. print '<tr class="liste_total">';
  827. print '<td></td>';
  828. print '<td></td>';
  829. print '<td class="right">';
  830. if ($modecompta == 'CREANCES-DETTES') {
  831. print price(-$subtotal_ht);
  832. }
  833. print '</td>';
  834. print '<td class="right">'.price(-$subtotal_ttc).'</td>';
  835. print '</tr>';
  836. /*
  837. * Salaries
  838. */
  839. if (isModEnabled('salaries')) {
  840. print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("Salaries").'</td></tr>';
  841. if ($modecompta == 'CREANCES-DETTES' || $modecompta == 'RECETTES-DEPENSES') {
  842. if ($modecompta == 'CREANCES-DETTES') {
  843. $column = 's.dateep'; // We use the date of end of period of salary
  844. $sql = "SELECT u.rowid, u.firstname, u.lastname, s.fk_user as fk_user, s.label as label, date_format($column,'%Y-%m') as dm, sum(s.amount) as amount";
  845. $sql .= " FROM ".MAIN_DB_PREFIX."salary as s";
  846. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."user as u ON u.rowid = s.fk_user";
  847. $sql .= " WHERE s.entity IN (".getEntity('salary').")";
  848. if (!empty($date_start) && !empty($date_end)) {
  849. $sql .= " AND $column >= '".$db->idate($date_start)."' AND $column <= '".$db->idate($date_end)."'";
  850. }
  851. $sql .= " GROUP BY u.rowid, u.firstname, u.lastname, s.fk_user, s.label, dm";
  852. } else {
  853. $column = 'p.datep';
  854. $sql = "SELECT u.rowid, u.firstname, u.lastname, s.fk_user as fk_user, p.label as label, date_format($column,'%Y-%m') as dm, sum(p.amount) as amount";
  855. $sql .= " FROM ".MAIN_DB_PREFIX."payment_salary as p";
  856. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."salary as s ON s.rowid = p.fk_salary";
  857. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."user as u ON u.rowid = s.fk_user";
  858. $sql .= " WHERE p.entity IN (".getEntity('payment_salary').")";
  859. if (!empty($date_start) && !empty($date_end)) {
  860. $sql .= " AND $column >= '".$db->idate($date_start)."' AND $column <= '".$db->idate($date_end)."'";
  861. }
  862. $sql .= " GROUP BY u.rowid, u.firstname, u.lastname, s.fk_user, p.label, dm";
  863. }
  864. $newsortfield = $sortfield;
  865. if ($newsortfield == 's.nom, s.rowid') {
  866. $newsortfield = 'u.firstname, u.lastname';
  867. }
  868. if ($newsortfield == 'amount_ht') {
  869. $newsortfield = 'amount';
  870. }
  871. if ($newsortfield == 'amount_ttc') {
  872. $newsortfield = 'amount';
  873. }
  874. $sql .= $db->order($newsortfield, $sortorder);
  875. }
  876. dol_syslog("get salaries");
  877. $result = $db->query($sql);
  878. $subtotal_ht = 0;
  879. $subtotal_ttc = 0;
  880. if ($result) {
  881. $num = $db->num_rows($result);
  882. $i = 0;
  883. if ($num) {
  884. while ($i < $num) {
  885. $obj = $db->fetch_object($result);
  886. $total_ht -= $obj->amount;
  887. $total_ttc -= $obj->amount;
  888. $subtotal_ht += $obj->amount;
  889. $subtotal_ttc += $obj->amount;
  890. print '<tr class="oddeven"><td>&nbsp;</td>';
  891. $userstatic = new User($db);
  892. $userstatic->fetch($obj->fk_user);
  893. print "<td>".$langs->trans("Salary")." <a href=\"".DOL_URL_ROOT."/salaries/list.php?search_user=".urlencode($userstatic->getFullName($langs))."\">".$obj->firstname." ".$obj->lastname."</a></td>\n";
  894. print '<td class="right">';
  895. if ($modecompta == 'CREANCES-DETTES') {
  896. print '<span class="amount">'.price(-$obj->amount).'</span>';
  897. }
  898. print '</td>';
  899. print '<td class="right"><span class="amount">'.price(-$obj->amount).'</span></td>';
  900. print '</tr>';
  901. $i++;
  902. }
  903. } else {
  904. print '<tr class="oddeven">';
  905. print '<td>&nbsp;</td>';
  906. print '<td colspan="3"><span class="opacitymedium">'.$langs->trans("None").'</span></td>';
  907. print '</tr>';
  908. }
  909. } else {
  910. dol_print_error($db);
  911. }
  912. $total_ht_outcome += $subtotal_ht;
  913. $total_ttc_outcome += $subtotal_ttc;
  914. print '<tr class="liste_total">';
  915. print '<td></td>';
  916. print '<td></td>';
  917. print '<td class="right">';
  918. if ($modecompta == 'CREANCES-DETTES') {
  919. print price(-$subtotal_ht);
  920. }
  921. print '</td>';
  922. print '<td class="right">'.price(-$subtotal_ttc).'</td>';
  923. print '</tr>';
  924. }
  925. /*
  926. * Expense report
  927. */
  928. if (isModEnabled('expensereport')) {
  929. if ($modecompta == 'CREANCES-DETTES' || $modecompta == 'RECETTES-DEPENSES') {
  930. $langs->load('trips');
  931. if ($modecompta == 'CREANCES-DETTES') {
  932. $sql = "SELECT p.rowid, p.ref, u.rowid as userid, u.firstname, u.lastname, date_format(date_valid,'%Y-%m') as dm, p.total_ht as amount_ht, p.total_ttc as amount_ttc";
  933. $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as p";
  934. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."user as u ON u.rowid=p.fk_user_author";
  935. $sql .= " WHERE p.entity IN (".getEntity('expensereport').")";
  936. $sql .= " AND p.fk_statut>=5";
  937. $column = 'p.date_valid';
  938. } else {
  939. $sql = "SELECT p.rowid, p.ref, u.rowid as userid, u.firstname, u.lastname, date_format(pe.datep,'%Y-%m') as dm, sum(pe.amount) as amount_ht, sum(pe.amount) as amount_ttc";
  940. $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as p";
  941. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."user as u ON u.rowid=p.fk_user_author";
  942. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."payment_expensereport as pe ON pe.fk_expensereport = p.rowid";
  943. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as c ON pe.fk_typepayment = c.id";
  944. $sql .= " WHERE p.entity IN (".getEntity('expensereport').")";
  945. $sql .= " AND p.fk_statut>=5";
  946. $column = 'pe.datep';
  947. }
  948. if (!empty($date_start) && !empty($date_end)) {
  949. $sql .= " AND $column >= '".$db->idate($date_start)."' AND $column <= '".$db->idate($date_end)."'";
  950. }
  951. if ($modecompta == 'CREANCES-DETTES') {
  952. //No need of GROUP BY
  953. } else {
  954. $sql .= " GROUP BY u.rowid, p.rowid, p.ref, u.firstname, u.lastname, dm";
  955. }
  956. $newsortfield = $sortfield;
  957. if ($newsortfield == 's.nom, s.rowid') {
  958. $newsortfield = 'p.ref';
  959. }
  960. $sql .= $db->order($newsortfield, $sortorder);
  961. }
  962. print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("ExpenseReport").'</td></tr>';
  963. dol_syslog("get expense report outcome");
  964. $result = $db->query($sql);
  965. $subtotal_ht = 0;
  966. $subtotal_ttc = 0;
  967. if ($result) {
  968. $num = $db->num_rows($result);
  969. if ($num) {
  970. while ($obj = $db->fetch_object($result)) {
  971. $total_ht -= $obj->amount_ht;
  972. $total_ttc -= $obj->amount_ttc;
  973. $subtotal_ht += $obj->amount_ht;
  974. $subtotal_ttc += $obj->amount_ttc;
  975. print '<tr class="oddeven">';
  976. print '<td>&nbsp;</td>';
  977. print "<td>".$langs->trans("ExpenseReport")." <a href=\"".DOL_URL_ROOT."/expensereport/list.php?search_user=".$obj->userid."\">".$obj->firstname." ".$obj->lastname."</a></td>\n";
  978. print '<td class="right">';
  979. if ($modecompta == 'CREANCES-DETTES') {
  980. print '<span class="amount">'.price(-$obj->amount_ht).'</span>';
  981. }
  982. print '</td>';
  983. print '<td class="right"><span class="amount">'.price(-$obj->amount_ttc).'</span></td>';
  984. print '</tr>';
  985. }
  986. } else {
  987. print '<tr class="oddeven">';
  988. print '<td>&nbsp;</td>';
  989. print '<td colspan="3"><span class="opacitymedium">'.$langs->trans("None").'</span></td>';
  990. print '</tr>';
  991. }
  992. } else {
  993. dol_print_error($db);
  994. }
  995. $total_ht_outcome += $subtotal_ht;
  996. $total_ttc_outcome += $subtotal_ttc;
  997. print '<tr class="liste_total">';
  998. print '<td></td>';
  999. print '<td></td>';
  1000. print '<td class="right">';
  1001. if ($modecompta == 'CREANCES-DETTES') {
  1002. print price(-$subtotal_ht);
  1003. }
  1004. print '</td>';
  1005. print '<td class="right">'.price(-$subtotal_ttc).'</td>';
  1006. print '</tr>';
  1007. }
  1008. /*
  1009. * Various Payments
  1010. */
  1011. //$conf->global->ACCOUNTING_REPORTS_INCLUDE_VARPAY = 1;
  1012. if (!empty($conf->global->ACCOUNTING_REPORTS_INCLUDE_VARPAY) && isModEnabled("banque") && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES")) {
  1013. $subtotal_ht = 0;
  1014. $subtotal_ttc = 0;
  1015. print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("VariousPayment").'</td></tr>';
  1016. // Debit
  1017. $sql = "SELECT SUM(p.amount) AS amount FROM ".MAIN_DB_PREFIX."payment_various as p";
  1018. $sql .= ' WHERE 1 = 1';
  1019. if (!empty($date_start) && !empty($date_end)) {
  1020. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  1021. }
  1022. $sql .= ' GROUP BY p.sens';
  1023. $sql .= ' ORDER BY p.sens';
  1024. dol_syslog('get various payments', LOG_DEBUG);
  1025. $result = $db->query($sql);
  1026. if ($result) {
  1027. // Debit (payment of suppliers for example)
  1028. $obj = $db->fetch_object($result);
  1029. if (isset($obj->amount)) {
  1030. $subtotal_ht += -$obj->amount;
  1031. $subtotal_ttc += -$obj->amount;
  1032. $total_ht_outcome += $obj->amount;
  1033. $total_ttc_outcome += $obj->amount;
  1034. }
  1035. print '<tr class="oddeven">';
  1036. print '<td>&nbsp;</td>';
  1037. print "<td>".$langs->trans("AccountingDebit")."</td>\n";
  1038. print '<td class="right">';
  1039. if ($modecompta == 'CREANCES-DETTES') {
  1040. print '<span class="amount">'.price(-$obj->amount).'</span>';
  1041. }
  1042. print '</td>';
  1043. print '<td class="right"><span class="amount">'.price(-$obj->amount)."</span></td>\n";
  1044. print "</tr>\n";
  1045. // Credit (payment received from customer for example)
  1046. $obj = $db->fetch_object($result);
  1047. if (isset($obj->amount)) {
  1048. $subtotal_ht += $obj->amount;
  1049. $subtotal_ttc += $obj->amount;
  1050. $total_ht_income += $obj->amount;
  1051. $total_ttc_income += $obj->amount;
  1052. }
  1053. print '<tr class="oddeven"><td>&nbsp;</td>';
  1054. print "<td>".$langs->trans("AccountingCredit")."</td>\n";
  1055. print '<td class="right">';
  1056. if ($modecompta == 'CREANCES-DETTES') {
  1057. print '<span class="amount">'.price($obj->amount).'</span>';
  1058. }
  1059. print '</td>';
  1060. print '<td class="right"><span class="amount">'.price($obj->amount)."</span></td>\n";
  1061. print "</tr>\n";
  1062. // Total
  1063. $total_ht += $subtotal_ht;
  1064. $total_ttc += $subtotal_ttc;
  1065. print '<tr class="liste_total">';
  1066. print '<td></td>';
  1067. print '<td></td>';
  1068. print '<td class="right">';
  1069. if ($modecompta == 'CREANCES-DETTES') {
  1070. print price($subtotal_ht);
  1071. }
  1072. print '</td>';
  1073. print '<td class="right">'.price($subtotal_ttc).'</td>';
  1074. print '</tr>';
  1075. } else {
  1076. dol_print_error($db);
  1077. }
  1078. }
  1079. /*
  1080. * Payment Loan
  1081. */
  1082. if (!empty($conf->global->ACCOUNTING_REPORTS_INCLUDE_LOAN) && isModEnabled('don') && ($modecompta == 'CREANCES-DETTES' || $modecompta == "RECETTES-DEPENSES")) {
  1083. $subtotal_ht = 0;
  1084. $subtotal_ttc = 0;
  1085. print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("PaymentLoan").'</td></tr>';
  1086. $sql = 'SELECT l.rowid as id, l.label AS label, SUM(p.amount_capital + p.amount_insurance + p.amount_interest) as amount FROM '.MAIN_DB_PREFIX.'payment_loan as p';
  1087. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'loan AS l ON l.rowid = p.fk_loan';
  1088. $sql .= ' WHERE 1 = 1';
  1089. if (!empty($date_start) && !empty($date_end)) {
  1090. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  1091. }
  1092. $sql .= ' GROUP BY p.fk_loan';
  1093. $sql .= ' ORDER BY p.fk_loan';
  1094. dol_syslog('get loan payments', LOG_DEBUG);
  1095. $result = $db->query($sql);
  1096. if ($result) {
  1097. require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
  1098. $loan_static = new Loan($db);
  1099. while ($obj = $db->fetch_object($result)) {
  1100. $loan_static->id = $obj->id;
  1101. $loan_static->ref = $obj->id;
  1102. $loan_static->label = $obj->label;
  1103. print '<tr class="oddeven"><td>&nbsp;</td>';
  1104. print "<td>".$loan_static->getNomUrl(1).' - '.$obj->label."</td>\n";
  1105. if ($modecompta == 'CREANCES-DETTES') {
  1106. print '<td class="right"><span class="amount">'.price(-$obj->amount).'</span></td>';
  1107. }
  1108. print '<td class="right"><span class="amount">'.price(-$obj->amount)."</span></td>\n";
  1109. print "</tr>\n";
  1110. $subtotal_ht -= $obj->amount;
  1111. $subtotal_ttc -= $obj->amount;
  1112. }
  1113. $total_ht += $subtotal_ht;
  1114. $total_ttc += $subtotal_ttc;
  1115. $total_ht_income += $subtotal_ht;
  1116. $total_ttc_income += $subtotal_ttc;
  1117. print '<tr class="liste_total">';
  1118. print '<td></td>';
  1119. print '<td></td>';
  1120. print '<td class="right">';
  1121. if ($modecompta == 'CREANCES-DETTES') {
  1122. print price($subtotal_ht);
  1123. }
  1124. print '</td>';
  1125. print '<td class="right">'.price($subtotal_ttc).'</td>';
  1126. print '</tr>';
  1127. } else {
  1128. dol_print_error($db);
  1129. }
  1130. }
  1131. /*
  1132. * VAT
  1133. */
  1134. print '<tr class="trforbreak"><td colspan="4">'.$langs->trans("VAT").'</td></tr>';
  1135. $subtotal_ht = 0;
  1136. $subtotal_ttc = 0;
  1137. if ($conf->tax->enabled && ($modecompta == 'CREANCES-DETTES' || $modecompta == 'RECETTES-DEPENSES')) {
  1138. if ($modecompta == 'CREANCES-DETTES') {
  1139. // VAT to pay
  1140. $amount = 0;
  1141. $sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.total_tva) as amount";
  1142. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  1143. $sql .= " WHERE f.fk_statut IN (1,2)";
  1144. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  1145. $sql .= " AND f.type IN (0,1,2,5)";
  1146. } else {
  1147. $sql .= " AND f.type IN (0,1,2,3,5)";
  1148. }
  1149. if (!empty($date_start) && !empty($date_end)) {
  1150. $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  1151. }
  1152. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  1153. $sql .= " GROUP BY dm";
  1154. $newsortfield = $sortfield;
  1155. if ($newsortfield == 's.nom, s.rowid') {
  1156. $newsortfield = 'dm';
  1157. }
  1158. if ($newsortfield == 'amount_ht') {
  1159. $newsortfield = 'amount';
  1160. }
  1161. if ($newsortfield == 'amount_ttc') {
  1162. $newsortfield = 'amount';
  1163. }
  1164. $sql .= $db->order($newsortfield, $sortorder);
  1165. dol_syslog("get vat to pay", LOG_DEBUG);
  1166. $result = $db->query($sql);
  1167. if ($result) {
  1168. $num = $db->num_rows($result);
  1169. $i = 0;
  1170. if ($num) {
  1171. while ($i < $num) {
  1172. $obj = $db->fetch_object($result);
  1173. $amount -= $obj->amount;
  1174. //$total_ht -= $obj->amount;
  1175. $total_ttc -= $obj->amount;
  1176. //$subtotal_ht -= $obj->amount;
  1177. $subtotal_ttc -= $obj->amount;
  1178. $i++;
  1179. }
  1180. }
  1181. } else {
  1182. dol_print_error($db);
  1183. }
  1184. $total_ht_outcome -= 0;
  1185. $total_ttc_outcome -= $amount;
  1186. print '<tr class="oddeven">';
  1187. print '<td>&nbsp;</td>';
  1188. print "<td>".$langs->trans("VATToPay")."</td>\n";
  1189. print '<td class="right">&nbsp;</td>'."\n";
  1190. print '<td class="right"><span class="amount">'.price($amount)."</span></td>\n";
  1191. print "</tr>\n";
  1192. // VAT to retrieve
  1193. $amount = 0;
  1194. $sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.total_tva) as amount";
  1195. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  1196. $sql .= " WHERE f.fk_statut IN (1,2)";
  1197. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  1198. $sql .= " AND f.type IN (0,1,2)";
  1199. } else {
  1200. $sql .= " AND f.type IN (0,1,2,3)";
  1201. }
  1202. if (!empty($date_start) && !empty($date_end)) {
  1203. $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  1204. }
  1205. $sql .= " AND f.entity = ".$conf->entity;
  1206. $sql .= " GROUP BY dm";
  1207. $newsortfield = $sortfield;
  1208. if ($newsortfield == 's.nom, s.rowid') {
  1209. $newsortfield = 'dm';
  1210. }
  1211. if ($newsortfield == 'amount_ht') {
  1212. $newsortfield = 'amount';
  1213. }
  1214. if ($newsortfield == 'amount_ttc') {
  1215. $newsortfield = 'amount';
  1216. }
  1217. $sql .= $db->order($newsortfield, $sortorder);
  1218. dol_syslog("get vat received back", LOG_DEBUG);
  1219. $result = $db->query($sql);
  1220. if ($result) {
  1221. $num = $db->num_rows($result);
  1222. $i = 0;
  1223. if ($num) {
  1224. while ($i < $num) {
  1225. $obj = $db->fetch_object($result);
  1226. $amount += $obj->amount;
  1227. //$total_ht += $obj->amount;
  1228. $total_ttc += $obj->amount;
  1229. //$subtotal_ht += $obj->amount;
  1230. $subtotal_ttc += $obj->amount;
  1231. $i++;
  1232. }
  1233. }
  1234. } else {
  1235. dol_print_error($db);
  1236. }
  1237. $total_ht_income += 0;
  1238. $total_ttc_income += $amount;
  1239. print '<tr class="oddeven">';
  1240. print '<td>&nbsp;</td>';
  1241. print '<td>'.$langs->trans("VATToCollect")."</td>\n";
  1242. print '<td class="right">&nbsp;</td>'."\n";
  1243. print '<td class="right"><span class="amount">'.price($amount)."</span></td>\n";
  1244. print "</tr>\n";
  1245. } else {
  1246. // VAT really already paid
  1247. $amount = 0;
  1248. $sql = "SELECT date_format(t.datev,'%Y-%m') as dm, sum(t.amount) as amount";
  1249. $sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
  1250. $sql .= " WHERE amount > 0";
  1251. if (!empty($date_start) && !empty($date_end)) {
  1252. $sql .= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";
  1253. }
  1254. $sql .= " AND t.entity = ".$conf->entity;
  1255. $sql .= " GROUP BY dm";
  1256. $newsortfield = $sortfield;
  1257. if ($newsortfield == 's.nom, s.rowid') {
  1258. $newsortfield = 'dm';
  1259. }
  1260. if ($newsortfield == 'amount_ht') {
  1261. $newsortfield = 'amount';
  1262. }
  1263. if ($newsortfield == 'amount_ttc') {
  1264. $newsortfield = 'amount';
  1265. }
  1266. $sql .= $db->order($newsortfield, $sortorder);
  1267. dol_syslog("get vat really paid", LOG_DEBUG);
  1268. $result = $db->query($sql);
  1269. if ($result) {
  1270. $num = $db->num_rows($result);
  1271. $i = 0;
  1272. if ($num) {
  1273. while ($i < $num) {
  1274. $obj = $db->fetch_object($result);
  1275. $amount -= $obj->amount;
  1276. $total_ht -= $obj->amount;
  1277. $total_ttc -= $obj->amount;
  1278. $subtotal_ht -= $obj->amount;
  1279. $subtotal_ttc -= $obj->amount;
  1280. $i++;
  1281. }
  1282. }
  1283. $db->free($result);
  1284. } else {
  1285. dol_print_error($db);
  1286. }
  1287. $total_ht_outcome -= 0;
  1288. $total_ttc_outcome -= $amount;
  1289. print '<tr class="oddeven">';
  1290. print '<td>&nbsp;</td>';
  1291. print "<td>".$langs->trans("VATPaid")."</td>\n";
  1292. print '<td <class="right"></td>'."\n";
  1293. print '<td class="right"><span class="amount">'.price($amount)."</span></td>\n";
  1294. print "</tr>\n";
  1295. // VAT really received
  1296. $amount = 0;
  1297. $sql = "SELECT date_format(t.datev,'%Y-%m') as dm, sum(t.amount) as amount";
  1298. $sql .= " FROM ".MAIN_DB_PREFIX."tva as t";
  1299. $sql .= " WHERE amount < 0";
  1300. if (!empty($date_start) && !empty($date_end)) {
  1301. $sql .= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";
  1302. }
  1303. $sql .= " AND t.entity = ".$conf->entity;
  1304. $sql .= " GROUP BY dm";
  1305. $newsortfield = $sortfield;
  1306. if ($newsortfield == 's.nom, s.rowid') {
  1307. $newsortfield = 'dm';
  1308. }
  1309. if ($newsortfield == 'amount_ht') {
  1310. $newsortfield = 'amount';
  1311. }
  1312. if ($newsortfield == 'amount_ttc') {
  1313. $newsortfield = 'amount';
  1314. }
  1315. $sql .= $db->order($newsortfield, $sortorder);
  1316. dol_syslog("get vat really received back", LOG_DEBUG);
  1317. $result = $db->query($sql);
  1318. if ($result) {
  1319. $num = $db->num_rows($result);
  1320. $i = 0;
  1321. if ($num) {
  1322. while ($i < $num) {
  1323. $obj = $db->fetch_object($result);
  1324. $amount += -$obj->amount;
  1325. $total_ht += -$obj->amount;
  1326. $total_ttc += -$obj->amount;
  1327. $subtotal_ht += -$obj->amount;
  1328. $subtotal_ttc += -$obj->amount;
  1329. $i++;
  1330. }
  1331. }
  1332. $db->free($result);
  1333. } else {
  1334. dol_print_error($db);
  1335. }
  1336. $total_ht_income += 0;
  1337. $total_ttc_income += $amount;
  1338. print '<tr class="oddeven">';
  1339. print '<td>&nbsp;</td>';
  1340. print "<td>".$langs->trans("VATCollected")."</td>\n";
  1341. print '<td class="right"></td>'."\n";
  1342. print '<td class="right"><span class="amount">'.price($amount)."</span></td>\n";
  1343. print "</tr>\n";
  1344. }
  1345. }
  1346. if ($mysoc->tva_assuj != '0') { // Assujetti
  1347. print '<tr class="liste_total">';
  1348. print '<td></td>';
  1349. print '<td></td>';
  1350. print '<td class="right">&nbsp;</td>';
  1351. print '<td class="right">'.price(price2num($subtotal_ttc, 'MT')).'</td>';
  1352. print '</tr>';
  1353. }
  1354. }
  1355. $action = "balanceclient";
  1356. $object = array(&$total_ht, &$total_ttc);
  1357. $parameters["mode"] = $modecompta;
  1358. $parameters["date_start"] = $date_start;
  1359. $parameters["date_end"] = $date_end;
  1360. // Initialize technical object to manage hooks of expenses. Note that conf->hooks_modules contains array array
  1361. $hookmanager->initHooks(array('externalbalance'));
  1362. $reshook = $hookmanager->executeHooks('addBalanceLine', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1363. print $hookmanager->resPrint;
  1364. // Total
  1365. print '<tr>';
  1366. print '<td colspan="'.($modecompta == 'BOOKKEEPING' ? 3 : 4).'">&nbsp;</td>';
  1367. print '</tr>';
  1368. print '<tr class="liste_total"><td class="left" colspan="2">'.$langs->trans("Income").'</td>';
  1369. if ($modecompta == 'CREANCES-DETTES') {
  1370. print '<td class="liste_total right nowraponall">'.price(price2num($total_ht_income, 'MT')).'</td>';
  1371. } elseif ($modecompta == 'RECETTES-DEPENSES') {
  1372. print '<td></td>';
  1373. }
  1374. print '<td class="liste_total right nowraponall">'.price(price2num($total_ttc_income, 'MT')).'</td>';
  1375. print '</tr>';
  1376. print '<tr class="liste_total"><td class="left" colspan="2">'.$langs->trans("Outcome").'</td>';
  1377. if ($modecompta == 'CREANCES-DETTES') {
  1378. print '<td class="liste_total right nowraponall">'.price(price2num(-$total_ht_outcome, 'MT')).'</td>';
  1379. } elseif ($modecompta == 'RECETTES-DEPENSES') {
  1380. print '<td></td>';
  1381. }
  1382. print '<td class="liste_total right nowraponall">'.price(price2num(-$total_ttc_outcome, 'MT')).'</td>';
  1383. print '</tr>';
  1384. print '<tr class="liste_total"><td class="left" colspan="2">'.$langs->trans("Profit").'</td>';
  1385. if ($modecompta == 'CREANCES-DETTES') {
  1386. print '<td class="liste_total right nowraponall">'.price(price2num($total_ht, 'MT')).'</td>';
  1387. } elseif ($modecompta == 'RECETTES-DEPENSES') {
  1388. print '<td></td>';
  1389. }
  1390. print '<td class="liste_total right nowraponall">'.price(price2num($total_ttc, 'MT')).'</td>';
  1391. print '</tr>';
  1392. print "</table>";
  1393. print '<br>';
  1394. // End of page
  1395. llxFooter();
  1396. $db->close();