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