clientfourn.php 56 KB

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