clientfourn.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. <?php
  2. /* Copyright (C) 2002-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.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. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * \file htdocs/compta/resultat/clientfourn.php
  26. * \brief Page reporting
  27. */
  28. require '../../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  34. $langs->load("bills");
  35. $langs->load("donation");
  36. $langs->load("salaries");
  37. $date_startmonth=GETPOST('date_startmonth');
  38. $date_startday=GETPOST('date_startday');
  39. $date_startyear=GETPOST('date_startyear');
  40. $date_endmonth=GETPOST('date_endmonth');
  41. $date_endday=GETPOST('date_endday');
  42. $date_endyear=GETPOST('date_endyear');
  43. // Security check
  44. $socid = GETPOST('socid','int');
  45. if ($user->societe_id > 0) $socid = $user->societe_id;
  46. if (! empty($conf->comptabilite->enabled)) $result=restrictedArea($user,'compta','','','resultat');
  47. if (! empty($conf->accounting->enabled)) $result=restrictedArea($user,'accounting','','','comptarapport');
  48. $limit = GETPOST("limit")?GETPOST("limit","int"):$conf->liste_limit;
  49. $sortfield = GETPOST("sortfield",'alpha');
  50. $sortorder = GETPOST("sortorder",'alpha');
  51. $page = GETPOST("page",'int');
  52. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  53. $offset = $limit * $page;
  54. $pageprev = $page - 1;
  55. $pagenext = $page + 1;
  56. if (! $sortfield) $sortfield='s.nom, s.rowid';
  57. if (! $sortorder) $sortorder='ASC';
  58. // Date range
  59. $year=GETPOST("year");
  60. if (empty($year))
  61. {
  62. $year_current = strftime("%Y",dol_now());
  63. $month_current = strftime("%m",dol_now());
  64. $year_start = $year_current;
  65. } else {
  66. $year_current = $year;
  67. $month_current = strftime("%m",dol_now());
  68. $year_start = $year;
  69. }
  70. $date_start=dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
  71. $date_end=dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
  72. // Quarter
  73. if (empty($date_start) || empty($date_end)) // We define date_start and date_end
  74. {
  75. $q=GETPOST("q")?GETPOST("q"):0;
  76. if ($q==0)
  77. {
  78. // We define date_start and date_end
  79. $year_end=$year_start;
  80. $month_start=GETPOST("month")?GETPOST("month"):($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
  81. if (! GETPOST('month'))
  82. {
  83. if (! GETPOST("year") && $month_start > $month_current)
  84. {
  85. $year_start--;
  86. $year_end--;
  87. }
  88. $month_end=$month_start-1;
  89. if ($month_end < 1) $month_end=12;
  90. else $year_end++;
  91. }
  92. else $month_end=$month_start;
  93. $date_start=dol_get_first_day($year_start,$month_start,false); $date_end=dol_get_last_day($year_end,$month_end,false);
  94. }
  95. if ($q==1) { $date_start=dol_get_first_day($year_start,1,false); $date_end=dol_get_last_day($year_start,3,false); }
  96. if ($q==2) { $date_start=dol_get_first_day($year_start,4,false); $date_end=dol_get_last_day($year_start,6,false); }
  97. if ($q==3) { $date_start=dol_get_first_day($year_start,7,false); $date_end=dol_get_last_day($year_start,9,false); }
  98. if ($q==4) { $date_start=dol_get_first_day($year_start,10,false); $date_end=dol_get_last_day($year_start,12,false); }
  99. }
  100. else
  101. {
  102. // TODO We define q
  103. }
  104. // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES')
  105. $modecompta=(GETPOST("modecompta")?GETPOST("modecompta"):$conf->global->ACCOUNTING_MODE);
  106. /*
  107. * View
  108. */
  109. llxHeader();
  110. $form=new Form($db);
  111. $nomlink='';
  112. $periodlink='';
  113. $exportlink='';
  114. $total_ht=0;
  115. $total_ttc=0;
  116. // Affiche en-tete de rapport
  117. if ($modecompta=="CREANCES-DETTES")
  118. {
  119. $name=$langs->trans("AnnualByCompaniesDueDebtMode");
  120. $calcmode=$langs->trans("CalcModeDebt");
  121. $calcmode.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.(GETPOST("month")>0?'&month='.GETPOST("month"):'').'&modecompta=RECETTES-DEPENSES">','</a>').')';
  122. $period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',0,0,0,'',1,0,1);
  123. //$periodlink='<a href="'.$_SERVER["PHP_SELF"].'?year='.($year-1).'&modecompta='.$modecompta.'">'.img_previous().'</a> <a href="'.$_SERVER["PHP_SELF"].'?year='.($year+1).'&modecompta='.$modecompta.'">'.img_next().'</a>';
  124. $description=$langs->trans("RulesResultDue");
  125. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description.= $langs->trans("DepositsAreNotIncluded");
  126. else $description.= $langs->trans("DepositsAreIncluded");
  127. $builddate=time();
  128. //$exportlink=$langs->trans("NotYetAvailable");
  129. }
  130. else {
  131. $name=$langs->trans("AnnualByCompaniesInputOutputMode");
  132. $calcmode=$langs->trans("CalcModeEngagement");
  133. $calcmode.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.(GETPOST("month")>0?'&month='.GETPOST("month"):'').'&modecompta=CREANCES-DETTES">','</a>').')';
  134. //$period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',1,1,0,'',1,0,1);
  135. $period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',0,0,0,'',1,0,1);
  136. //$periodlink='<a href="'.$_SERVER["PHP_SELF"].'?year='.($year-1).'&modecompta='.$modecompta.'">'.img_previous().'</a> <a href="'.$_SERVER["PHP_SELF"].'?year='.($year+1).'&modecompta='.$modecompta.'">'.img_next().'</a>';
  137. $description=$langs->trans("RulesResultInOut");
  138. $builddate=time();
  139. //$exportlink=$langs->trans("NotYetAvailable");
  140. }
  141. $hselected = 'report';
  142. report_header($name,$nomlink,$period,$periodlink,$description,$builddate,$exportlink,array('modecompta'=>$modecompta),$calcmode);
  143. if (! empty($conf->accounting->enabled))
  144. {
  145. print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);
  146. }
  147. // Show report array
  148. $param='&modecompta='.$modecompta;
  149. if ($date_startday) $param.='&date_startday='.$date_startday;
  150. if ($date_startmonth) $param.='&date_startmonth='.$date_startmonth;
  151. if ($date_startyear) $param.='&date_startyear='.$date_startyear;
  152. if ($date_endday) $param.='&date_endday='.$date_endday;
  153. if ($date_endmonth) $param.='&date_endmonth='.$date_endmonth;
  154. if ($date_endyear) $param.='&date_endyear='.$date_startyear;
  155. print '<table class="noborder" width="100%">';
  156. print '<tr class="liste_titre">';
  157. print_liste_field_titre('');
  158. print_liste_field_titre($langs->trans("Name"), $_SERVER["PHP_SELF"],'s.nom, s.rowid','',$param,'',$sortfield,$sortorder);
  159. if ($modecompta == 'CREANCES-DETTES')
  160. {
  161. print_liste_field_titre($langs->trans("AmountHT"), $_SERVER["PHP_SELF"],'amount_ht','',$param,'align="right"',$sortfield,$sortorder);
  162. }
  163. print_liste_field_titre($langs->trans("AmountTTC"), $_SERVER["PHP_SELF"],'amount_ttc','',$param,'align="right"',$sortfield,$sortorder);
  164. print "</tr>\n";
  165. /*
  166. * Factures clients
  167. */
  168. print '<tr><td colspan="4">'.$langs->trans("CustomersInvoices").'</td></tr>';
  169. if ($modecompta == 'CREANCES-DETTES')
  170. {
  171. $sql = "SELECT s.nom as name, s.rowid as socid, sum(f.total) as amount_ht, sum(f.total_ttc) as amount_ttc";
  172. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  173. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  174. $sql.= " WHERE f.fk_soc = s.rowid";
  175. $sql.= " AND f.fk_statut IN (1,2)";
  176. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))
  177. $sql.= " AND f.type IN (0,1,2,5)";
  178. else
  179. $sql.= " AND f.type IN (0,1,2,3,5)";
  180. if (! empty($date_start) && ! empty($date_end))
  181. $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  182. }
  183. else
  184. {
  185. /*
  186. * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
  187. * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
  188. */
  189. $sql = "SELECT s.nom as name, s.rowid as socid, sum(pf.amount) as amount_ttc";
  190. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  191. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  192. $sql.= ", ".MAIN_DB_PREFIX."paiement_facture as pf";
  193. $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
  194. $sql.= " WHERE p.rowid = pf.fk_paiement";
  195. $sql.= " AND pf.fk_facture = f.rowid";
  196. $sql.= " AND f.fk_soc = s.rowid";
  197. if (! empty($date_start) && ! empty($date_end))
  198. $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  199. }
  200. $sql.= " AND f.entity = ".$conf->entity;
  201. if ($socid) $sql.= " AND f.fk_soc = ".$socid;
  202. $sql.= " GROUP BY s.nom, s.rowid";
  203. $sql.= $db->order($sortfield, $sortorder);
  204. dol_syslog("get customer invoices", LOG_DEBUG);
  205. $result = $db->query($sql);
  206. if ($result) {
  207. $num = $db->num_rows($result);
  208. $i = 0;
  209. while ($i < $num)
  210. {
  211. $objp = $db->fetch_object($result);
  212. print '<tr class="oddeven"><td>&nbsp;</td>';
  213. print "<td>".$langs->trans("Bills").' <a href="'.DOL_URL_ROOT.'/compta/facture/list.php?socid='.$objp->socid.'">'.$objp->name."</td>\n";
  214. if ($modecompta == 'CREANCES-DETTES')
  215. print "<td align=\"right\">".price($objp->amount_ht)."</td>\n";
  216. print "<td align=\"right\">".price($objp->amount_ttc)."</td>\n";
  217. $total_ht += (isset($objp->amount_ht)?$objp->amount_ht:0);
  218. $total_ttc += $objp->amount_ttc;
  219. print "</tr>\n";
  220. $i++;
  221. }
  222. $db->free($result);
  223. } else {
  224. dol_print_error($db);
  225. }
  226. // On ajoute les paiements clients anciennes version, non lie par paiement_facture
  227. if ($modecompta != 'CREANCES-DETTES')
  228. {
  229. $sql = "SELECT 'Autres' as name, '0' as idp, sum(p.amount) as amount_ttc";
  230. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  231. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  232. $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
  233. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
  234. $sql.= " WHERE pf.rowid IS NULL";
  235. $sql.= " AND p.fk_bank = b.rowid";
  236. $sql.= " AND b.fk_account = ba.rowid";
  237. $sql.= " AND ba.entity IN (".getEntity('bank_account').")";
  238. if (! empty($date_start) && ! empty($date_end))
  239. $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  240. $sql.= " GROUP BY name, idp";
  241. $sql.= " ORDER BY name";
  242. dol_syslog("get old customer payments not linked to invoices", LOG_DEBUG);
  243. $result = $db->query($sql);
  244. if ($result) {
  245. $num = $db->num_rows($result);
  246. $i = 0;
  247. if ($num) {
  248. while ($i < $num)
  249. {
  250. $objp = $db->fetch_object($result);
  251. print '<tr class="oddeven"><td>&nbsp;</td>';
  252. print "<td>".$langs->trans("Bills")." ".$langs->trans("Other")." (".$langs->trans("PaymentsNotLinkedToInvoice").")\n";
  253. if ($modecompta == 'CREANCES-DETTES')
  254. print "<td align=\"right\">".price($objp->amount_ht)."</td>\n";
  255. print "<td align=\"right\">".price($objp->amount_ttc)."</td>\n";
  256. $total_ht += (isset($objp->amount_ht)?$objp->amount_ht:0);
  257. $total_ttc += $objp->amount_ttc;
  258. print "</tr>\n";
  259. $i++;
  260. }
  261. }
  262. $db->free($result);
  263. } else {
  264. dol_print_error($db);
  265. }
  266. }
  267. if ($total_ttc == 0)
  268. {
  269. print '<tr class="oddeven"><td>&nbsp;</td>';
  270. print '<td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td>';
  271. print '</tr>';
  272. }
  273. print '<tr class="liste_total">';
  274. if ($modecompta == 'CREANCES-DETTES')
  275. print '<td colspan="3" align="right">'.price($total_ht).'</td>';
  276. print '<td colspan="3" align="right">'.price($total_ttc).'</td>';
  277. print '</tr>';
  278. /*
  279. * Suppliers invoices
  280. */
  281. if ($modecompta == 'CREANCES-DETTES')
  282. {
  283. $sql = "SELECT s.nom as name, s.rowid as socid, sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc";
  284. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  285. $sql.= ", ".MAIN_DB_PREFIX."facture_fourn as f";
  286. $sql.= " WHERE f.fk_soc = s.rowid";
  287. $sql.= " AND f.fk_statut IN (1,2)";
  288. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))
  289. $sql.= " AND f.type IN (0,1,2)";
  290. else
  291. $sql.= " AND f.type IN (0,1,2,3)";
  292. if (! empty($date_start) && ! empty($date_end))
  293. $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  294. }
  295. else
  296. {
  297. $sql = "SELECT s.nom as name, s.rowid as socid, sum(pf.amount) as amount_ttc";
  298. $sql.= " FROM ".MAIN_DB_PREFIX."paiementfourn as p";
  299. $sql.= ", ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf";
  300. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f";
  301. $sql.= " ON pf.fk_facturefourn = f.rowid";
  302. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s";
  303. $sql.= " ON f.fk_soc = s.rowid";
  304. $sql.= " WHERE p.rowid = pf.fk_paiementfourn ";
  305. if (! empty($date_start) && ! empty($date_end))
  306. $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  307. }
  308. $sql.= " AND f.entity = ".$conf->entity;
  309. if ($socid) $sql.= " AND f.fk_soc = ".$socid;
  310. $sql .= " GROUP BY s.nom, s.rowid";
  311. $sql.= $db->order($sortfield, $sortorder);
  312. print '<tr><td colspan="4">'.$langs->trans("SuppliersInvoices").'</td></tr>';
  313. $subtotal_ht = 0;
  314. $subtotal_ttc = 0;
  315. dol_syslog("get suppliers invoices", LOG_DEBUG);
  316. $result = $db->query($sql);
  317. if ($result) {
  318. $num = $db->num_rows($result);
  319. $i = 0;
  320. if ($num > 0)
  321. {
  322. while ($i < $num)
  323. {
  324. $objp = $db->fetch_object($result);
  325. print '<tr class="oddeven"><td>&nbsp;</td>';
  326. print "<td>".$langs->trans("Bills")." <a href=\"".DOL_URL_ROOT."/fourn/facture/list.php?socid=".$objp->socid."\">".$objp->name."</a></td>\n";
  327. if ($modecompta == 'CREANCES-DETTES')
  328. print "<td align=\"right\">".price(-$objp->amount_ht)."</td>\n";
  329. print "<td align=\"right\">".price(-$objp->amount_ttc)."</td>\n";
  330. $total_ht -= (isset($objp->amount_ht)?$objp->amount_ht:0);
  331. $total_ttc -= $objp->amount_ttc;
  332. $subtotal_ht += (isset($objp->amount_ht)?$objp->amount_ht:0);
  333. $subtotal_ttc += $objp->amount_ttc;
  334. print "</tr>\n";
  335. $i++;
  336. }
  337. }
  338. else
  339. {
  340. print '<tr class="oddeven"><td>&nbsp;</td>';
  341. print '<td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td>';
  342. print '</tr>';
  343. }
  344. $db->free($result);
  345. } else {
  346. dol_print_error($db);
  347. }
  348. print '<tr class="liste_total">';
  349. if ($modecompta == 'CREANCES-DETTES')
  350. print '<td colspan="3" align="right">'.price(-$subtotal_ht).'</td>';
  351. print '<td colspan="3" align="right">'.price(-$subtotal_ttc).'</td>';
  352. print '</tr>';
  353. /*
  354. * Charges sociales non deductibles
  355. */
  356. print '<tr><td colspan="4">'.$langs->trans("SocialContributionsNondeductibles").'</td></tr>';
  357. if ($modecompta == 'CREANCES-DETTES')
  358. {
  359. $sql = "SELECT c.id, c.libelle as label, sum(cs.amount) as amount";
  360. $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  361. $sql.= ", ".MAIN_DB_PREFIX."chargesociales as cs";
  362. $sql.= " WHERE cs.fk_type = c.id";
  363. $sql.= " AND c.deductible = 0";
  364. if (! empty($date_start) && ! empty($date_end))
  365. $sql.= " AND cs.date_ech >= '".$db->idate($date_start)."' AND cs.date_ech <= '".$db->idate($date_end)."'";
  366. }
  367. else
  368. {
  369. $sql = "SELECT c.id, c.libelle as label, sum(p.amount) as amount";
  370. $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  371. $sql.= ", ".MAIN_DB_PREFIX."chargesociales as cs";
  372. $sql.= ", ".MAIN_DB_PREFIX."paiementcharge as p";
  373. $sql.= " WHERE p.fk_charge = cs.rowid";
  374. $sql.= " AND cs.fk_type = c.id";
  375. $sql.= " AND c.deductible = 0";
  376. if (! empty($date_start) && ! empty($date_end))
  377. $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  378. }
  379. $sql.= " AND cs.entity = ".$conf->entity;
  380. $sql.= " GROUP BY c.libelle, c.id";
  381. $newsortfield = $sortfield;
  382. if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'c.libelle, c.id';
  383. if ($newsortfield == 'amount_ht') $newsortfield = 'amount';
  384. if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';
  385. $sql.= $db->order($newsortfield, $sortorder);
  386. dol_syslog("get social contributions deductible=0", LOG_DEBUG);
  387. $result=$db->query($sql);
  388. $subtotal_ht = 0;
  389. $subtotal_ttc = 0;
  390. if ($result) {
  391. $num = $db->num_rows($result);
  392. $i = 0;
  393. if ($num) {
  394. while ($i < $num) {
  395. $obj = $db->fetch_object($result);
  396. $total_ht -= $obj->amount;
  397. $total_ttc -= $obj->amount;
  398. $subtotal_ht += $obj->amount;
  399. $subtotal_ttc += $obj->amount;
  400. print '<tr class="oddeven"><td>&nbsp;</td>';
  401. print '<td>'.$obj->label.'</td>';
  402. if ($modecompta == 'CREANCES-DETTES') print '<td align="right">'.price(-$obj->amount).'</td>';
  403. print '<td align="right">'.price(-$obj->amount).'</td>';
  404. print '</tr>';
  405. $i++;
  406. }
  407. }
  408. else {
  409. print '<tr class="oddeven"><td>&nbsp;</td>';
  410. print '<td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td>';
  411. print '</tr>';
  412. }
  413. } else {
  414. dol_print_error($db);
  415. }
  416. print '<tr class="liste_total">';
  417. if ($modecompta == 'CREANCES-DETTES')
  418. print '<td colspan="3" align="right">'.price(-$subtotal_ht).'</td>';
  419. print '<td colspan="3" align="right">'.price(-$subtotal_ttc).'</td>';
  420. print '</tr>';
  421. /*
  422. * Charges sociales deductibles
  423. */
  424. print '<tr><td colspan="4">'.$langs->trans("SocialContributionsDeductibles").'</td></tr>';
  425. if ($modecompta == 'CREANCES-DETTES')
  426. {
  427. $sql = "SELECT c.id, c.libelle as label, sum(cs.amount) as amount";
  428. $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  429. $sql.= ", ".MAIN_DB_PREFIX."chargesociales as cs";
  430. $sql.= " WHERE cs.fk_type = c.id";
  431. $sql.= " AND c.deductible = 1";
  432. if (! empty($date_start) && ! empty($date_end))
  433. $sql.= " AND cs.date_ech >= '".$db->idate($date_start)."' AND cs.date_ech <= '".$db->idate($date_end)."'";
  434. $sql.= " AND cs.entity = ".$conf->entity;
  435. }
  436. else
  437. {
  438. $sql = "SELECT c.id, c.libelle as label, sum(p.amount) as amount";
  439. $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  440. $sql.= ", ".MAIN_DB_PREFIX."chargesociales as cs";
  441. $sql.= ", ".MAIN_DB_PREFIX."paiementcharge as p";
  442. $sql.= " WHERE p.fk_charge = cs.rowid";
  443. $sql.= " AND cs.fk_type = c.id";
  444. $sql.= " AND c.deductible = 1";
  445. if (! empty($date_start) && ! empty($date_end))
  446. $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  447. $sql.= " AND cs.entity = ".$conf->entity;
  448. }
  449. $sql.= " GROUP BY c.libelle, c.id";
  450. $newsortfield = $sortfield;
  451. if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'c.libelle, c.id';
  452. if ($newsortfield == 'amount_ht') $newsortfield = 'amount';
  453. if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';
  454. $sql.= $db->order($newsortfield, $sortorder);
  455. dol_syslog("get social contributions deductible=1", LOG_DEBUG);
  456. $result=$db->query($sql);
  457. $subtotal_ht = 0;
  458. $subtotal_ttc = 0;
  459. if ($result) {
  460. $num = $db->num_rows($result);
  461. $i = 0;
  462. if ($num) {
  463. while ($i < $num) {
  464. $obj = $db->fetch_object($result);
  465. $total_ht -= $obj->amount;
  466. $total_ttc -= $obj->amount;
  467. $subtotal_ht += $obj->amount;
  468. $subtotal_ttc += $obj->amount;
  469. print '<tr class="oddeven"><td>&nbsp;</td>';
  470. print '<td>'.$obj->label.'</td>';
  471. if ($modecompta == 'CREANCES-DETTES')
  472. print '<td align="right">'.price(-$obj->amount).'</td>';
  473. print '<td align="right">'.price(-$obj->amount).'</td>';
  474. print '</tr>';
  475. $i++;
  476. }
  477. }
  478. else {
  479. print '<tr class="oddeven"><td>&nbsp;</td>';
  480. print '<td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td>';
  481. print '</tr>';
  482. }
  483. } else {
  484. dol_print_error($db);
  485. }
  486. print '<tr class="liste_total">';
  487. if ($modecompta == 'CREANCES-DETTES')
  488. print '<td colspan="3" align="right">'.price(-$subtotal_ht).'</td>';
  489. print '<td colspan="3" align="right">'.price(-$subtotal_ttc).'</td>';
  490. print '</tr>';
  491. if ($mysoc->tva_assuj == 'franchise') // Non assujetti
  492. {
  493. // Total
  494. print '<tr>';
  495. print '<td colspan="4">&nbsp;</td>';
  496. print '</tr>';
  497. print '<tr class="liste_total"><td align="left" colspan="2">'.$langs->trans("Profit").'</td>';
  498. if ($modecompta == 'CREANCES-DETTES')
  499. print '<td class="border" align="right">'.price($total_ht).'</td>';
  500. print '<td align="right">'.price($total_ttc).'</td>';
  501. print '</tr>';
  502. print '<tr>';
  503. print '<td colspan="4">&nbsp;</td>';
  504. print '</tr>';
  505. }
  506. /*
  507. * Salaries
  508. */
  509. if (! empty($conf->salaries->enabled))
  510. {
  511. if ($modecompta == 'CREANCES-DETTES') {
  512. $column = 'p.datev';
  513. } else {
  514. $column = 'p.datep';
  515. }
  516. print '<tr><td colspan="4">'.$langs->trans("Salaries").'</td></tr>';
  517. $sql = "SELECT u.rowid, u.firstname, u.lastname, p.fk_user, p.label as label, date_format($column,'%Y-%m') as dm, sum(p.amount) as amount";
  518. $sql.= " FROM ".MAIN_DB_PREFIX."payment_salary as p";
  519. $sql.= " INNER JOIN ".MAIN_DB_PREFIX."user as u ON u.rowid=p.fk_user";
  520. $sql.= " WHERE p.entity = ".$conf->entity;
  521. if (! empty($date_start) && ! empty($date_end))
  522. $sql.= " AND $column >= '".$db->idate($date_start)."' AND $column <= '".$db->idate($date_end)."'";
  523. $sql.= " GROUP BY u.rowid, u.firstname, u.lastname, p.fk_user, p.label, dm";
  524. $newsortfield = $sortfield;
  525. if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'u.firstname, u.lastname';
  526. if ($newsortfield == 'amount_ht') $newsortfield = 'amount';
  527. if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';
  528. $sql.= $db->order($newsortfield, $sortorder);
  529. dol_syslog("get payment salaries");
  530. $result=$db->query($sql);
  531. $subtotal_ht = 0;
  532. $subtotal_ttc = 0;
  533. if ($result)
  534. {
  535. $num = $db->num_rows($result);
  536. $i = 0;
  537. if ($num)
  538. {
  539. while ($i < $num)
  540. {
  541. $obj = $db->fetch_object($result);
  542. $total_ht -= $obj->amount;
  543. $total_ttc -= $obj->amount;
  544. $subtotal_ht += $obj->amount;
  545. $subtotal_ttc += $obj->amount;
  546. print '<tr class="oddeven"><td>&nbsp;</td>';
  547. print "<td>".$langs->trans("Salary")." <a href=\"".DOL_URL_ROOT."/compta/salaries/index.php?filtre=s.fk_user=".$obj->fk_user."\">".$obj->firstname." ".$obj->lastname."</a></td>\n";
  548. if ($modecompta == 'CREANCES-DETTES') print '<td align="right">'.price(-$obj->amount).'</td>';
  549. print '<td align="right">'.price(-$obj->amount).'</td>';
  550. print '</tr>';
  551. $i++;
  552. }
  553. }
  554. else
  555. {
  556. print '<tr class="oddeven"><td>&nbsp;</td>';
  557. print '<td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td>';
  558. print '</tr>';
  559. }
  560. }
  561. else
  562. {
  563. dol_print_error($db);
  564. }
  565. print '<tr class="liste_total">';
  566. if ($modecompta == 'CREANCES-DETTES')
  567. print '<td colspan="3" align="right">'.price(-$subtotal_ht).'</td>';
  568. print '<td colspan="3" align="right">'.price(-$subtotal_ttc).'</td>';
  569. print '</tr>';
  570. }
  571. /*
  572. * Expense
  573. */
  574. if (! empty($conf->expensereport->enabled))
  575. {
  576. $langs->load('trips');
  577. if ($modecompta == 'CREANCES-DETTES') {
  578. $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";
  579. $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as p";
  580. $sql.= " INNER JOIN ".MAIN_DB_PREFIX."user as u ON u.rowid=p.fk_user_author";
  581. $sql.= " WHERE p.entity = ".getEntity('expensereport');
  582. $sql.= " AND p.fk_statut>=5";
  583. $column='p.date_valid';
  584. } else {
  585. $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";
  586. $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as p";
  587. $sql.= " INNER JOIN ".MAIN_DB_PREFIX."user as u ON u.rowid=p.fk_user_author";
  588. $sql.= " INNER JOIN ".MAIN_DB_PREFIX."payment_expensereport as pe ON pe.fk_expensereport = p.rowid";
  589. $sql.= " INNER JOIN ".MAIN_DB_PREFIX."c_paiement as c ON pe.fk_typepayment = c.id";
  590. $sql.= " WHERE p.entity = ".getEntity('expensereport');
  591. $sql.= " AND p.fk_statut>=5";
  592. $column='pe.datep';
  593. }
  594. print '<tr><td colspan="4">'.$langs->trans("ExpenseReport").'</td></tr>';
  595. if (! empty($date_start) && ! empty($date_end))
  596. {
  597. $sql.= " AND $column >= '".$db->idate($date_start)."' AND $column <= '".$db->idate($date_end)."'";
  598. }
  599. $sql.= " GROUP BY u.rowid, p.rowid, p.ref, u.firstname, u.lastname, dm";
  600. $newsortfield = $sortfield;
  601. if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'p.ref';
  602. $sql.= $db->order($newsortfield, $sortorder);
  603. dol_syslog("get expense report outcome");
  604. $result=$db->query($sql);
  605. $subtotal_ht = 0;
  606. $subtotal_ttc = 0;
  607. if ($result)
  608. {
  609. $num = $db->num_rows($result);
  610. if ($num)
  611. {
  612. while ($obj = $db->fetch_object($result))
  613. {
  614. $total_ht -= $obj->amount_ht;
  615. $total_ttc -= $obj->amount_ttc;
  616. $subtotal_ht += $obj->amount_ht;
  617. $subtotal_ttc += $obj->amount_ttc;
  618. print '<tr class="oddeven"><td>&nbsp;</td>';
  619. print "<td>".$langs->trans("ExpenseReport")." <a href=\"".DOL_URL_ROOT."/expensereport/list.php?search_user=".$obj->userid."\">".$obj->firstname." ".$obj->lastname."</a></td>\n";
  620. if ($modecompta == 'CREANCES-DETTES') print '<td align="right">'.price(-$obj->amount_ht).'</td>';
  621. print '<td align="right">'.price(-$obj->amount_ttc).'</td>';
  622. print '</tr>';
  623. }
  624. }
  625. else
  626. {
  627. print '<tr class="oddeven"><td>&nbsp;</td>';
  628. print '<td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td>';
  629. print '</tr>';
  630. }
  631. }
  632. else
  633. {
  634. dol_print_error($db);
  635. }
  636. print '<tr class="liste_total">';
  637. if ($modecompta == 'CREANCES-DETTES')
  638. print '<td colspan="3" align="right">'.price(-$subtotal_ht).'</td>';
  639. print '<td colspan="3" align="right">'.price(-$subtotal_ttc).'</td>';
  640. print '</tr>';
  641. }
  642. /*
  643. * Donations
  644. */
  645. if (! empty($conf->don->enabled))
  646. {
  647. print '<tr><td colspan="4">'.$langs->trans("Donations").'</td></tr>';
  648. if ($modecompta == 'CREANCES-DETTES')
  649. {
  650. $sql = "SELECT p.societe as name, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";
  651. $sql.= " FROM ".MAIN_DB_PREFIX."don as p";
  652. $sql.= " WHERE p.entity = ".$conf->entity;
  653. $sql.= " AND fk_statut in (1,2)";
  654. }
  655. else
  656. {
  657. $sql = "SELECT p.societe as nom, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";
  658. $sql.= " FROM ".MAIN_DB_PREFIX."don as p";
  659. $sql.= " INNER JOIN ".MAIN_DB_PREFIX."payment_donation as pe ON pe.fk_donation = p.rowid";
  660. $sql.= " INNER JOIN ".MAIN_DB_PREFIX."c_paiement as c ON pe.fk_typepayment = c.id";
  661. $sql.= " WHERE p.entity = ".getEntity('donation');
  662. $sql.= " AND fk_statut >= 2";
  663. }
  664. if (! empty($date_start) && ! empty($date_end))
  665. $sql.= " AND p.datedon >= '".$db->idate($date_start)."' AND p.datedon <= '".$db->idate($date_end)."'";
  666. $sql.= " GROUP BY p.societe, p.firstname, p.lastname, dm";
  667. $newsortfield = $sortfield;
  668. if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'p.societe, p.firstname, p.lastname, dm';
  669. if ($newsortfield == 'amount_ht') $newsortfield = 'amount';
  670. if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';
  671. $sql.= $db->order($newsortfield, $sortorder);
  672. dol_syslog("get dunning");
  673. $result=$db->query($sql);
  674. $subtotal_ht = 0;
  675. $subtotal_ttc = 0;
  676. if ($result)
  677. {
  678. $num = $db->num_rows($result);
  679. $i = 0;
  680. if ($num)
  681. {
  682. while ($i < $num)
  683. {
  684. $obj = $db->fetch_object($result);
  685. $total_ht += $obj->amount;
  686. $total_ttc += $obj->amount;
  687. $subtotal_ht += $obj->amount;
  688. $subtotal_ttc += $obj->amount;
  689. print '<tr class="oddeven"><td>&nbsp;</td>';
  690. 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";
  691. if ($modecompta == 'CREANCES-DETTES') print '<td align="right">'.price($obj->amount).'</td>';
  692. print '<td align="right">'.price($obj->amount).'</td>';
  693. print '</tr>';
  694. $i++;
  695. }
  696. }
  697. else
  698. {
  699. print '<tr class="oddeven"><td>&nbsp;</td>';
  700. print '<td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td>';
  701. print '</tr>';
  702. }
  703. }
  704. else
  705. {
  706. dol_print_error($db);
  707. }
  708. print '<tr class="liste_total">';
  709. if ($modecompta == 'CREANCES-DETTES')
  710. print '<td colspan="3" align="right">'.price($subtotal_ht).'</td>';
  711. print '<td colspan="3" align="right">'.price($subtotal_ttc).'</td>';
  712. print '</tr>';
  713. }
  714. /*
  715. * VAT
  716. */
  717. print '<tr><td colspan="4">'.$langs->trans("VAT").'</td></tr>';
  718. $subtotal_ht = 0;
  719. $subtotal_ttc = 0;
  720. if ($modecompta == 'CREANCES-DETTES')
  721. {
  722. // TVA a payer
  723. $amount=0;
  724. $sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.tva) as amount";
  725. $sql.= " FROM ".MAIN_DB_PREFIX."facture as f";
  726. $sql.= " WHERE f.fk_statut IN (1,2)";
  727. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))
  728. $sql.= " AND f.type IN (0,1,2,5)";
  729. else
  730. $sql.= " AND f.type IN (0,1,2,3,5)";
  731. if (! empty($date_start) && ! empty($date_end))
  732. $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  733. $sql.= " AND f.entity = ".$conf->entity;
  734. $sql.= " GROUP BY dm";
  735. $newsortfield = $sortfield;
  736. if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'dm';
  737. if ($newsortfield == 'amount_ht') $newsortfield = 'amount';
  738. if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';
  739. $sql.= $db->order($newsortfield, $sortorder);
  740. dol_syslog("get vat to pay", LOG_DEBUG);
  741. $result=$db->query($sql);
  742. if ($result)
  743. {
  744. $num = $db->num_rows($result);
  745. $i = 0;
  746. if ($num)
  747. {
  748. while ($i < $num)
  749. {
  750. $obj = $db->fetch_object($result);
  751. $amount -= $obj->amount;
  752. //$total_ht -= $obj->amount;
  753. $total_ttc -= $obj->amount;
  754. //$subtotal_ht -= $obj->amount;
  755. $subtotal_ttc -= $obj->amount;
  756. $i++;
  757. }
  758. }
  759. } else {
  760. dol_print_error($db);
  761. }
  762. print '<tr class="oddeven"><td>&nbsp;</td>';
  763. print "<td>".$langs->trans("VATToPay")."</td>\n";
  764. print "<td align=\"right\">&nbsp;</td>\n";
  765. print "<td align=\"right\">".price($amount)."</td>\n";
  766. print "</tr>\n";
  767. // TVA a recuperer
  768. $amount=0;
  769. $sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.total_tva) as amount";
  770. $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  771. $sql.= " WHERE f.fk_statut IN (1,2)";
  772. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))
  773. $sql.= " AND f.type IN (0,1,2)";
  774. else
  775. $sql.= " AND f.type IN (0,1,2,3)";
  776. if (! empty($date_start) && ! empty($date_end))
  777. $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  778. $sql.= " AND f.entity = ".$conf->entity;
  779. $sql.= " GROUP BY dm";
  780. $newsortfield = $sortfield;
  781. if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'dm';
  782. if ($newsortfield == 'amount_ht') $newsortfield = 'amount';
  783. if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';
  784. $sql.= $db->order($newsortfield, $sortorder);
  785. dol_syslog("get vat received back", LOG_DEBUG);
  786. $result=$db->query($sql);
  787. if ($result)
  788. {
  789. $num = $db->num_rows($result);
  790. $i = 0;
  791. if ($num)
  792. {
  793. while ($i < $num)
  794. {
  795. $obj = $db->fetch_object($result);
  796. $amount += $obj->amount;
  797. //$total_ht += $obj->amount;
  798. $total_ttc += $obj->amount;
  799. //$subtotal_ht += $obj->amount;
  800. $subtotal_ttc += $obj->amount;
  801. $i++;
  802. }
  803. }
  804. } else {
  805. dol_print_error($db);
  806. }
  807. print '<tr class="oddeven"><td>&nbsp;</td>';
  808. print "<td>".$langs->trans("VATToCollect")."</td>\n";
  809. print "<td align=\"right\">&nbsp;</td>\n";
  810. print "<td align=\"right\">".price($amount)."</td>\n";
  811. print "</tr>\n";
  812. }
  813. else
  814. {
  815. // VAT really already paid
  816. $amount=0;
  817. $sql = "SELECT date_format(t.datev,'%Y-%m') as dm, sum(t.amount) as amount";
  818. $sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
  819. $sql.= " WHERE amount > 0";
  820. if (! empty($date_start) && ! empty($date_end))
  821. $sql.= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";
  822. $sql.= " AND t.entity = ".$conf->entity;
  823. $sql.= " GROUP BY dm";
  824. $newsortfield = $sortfield;
  825. if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'dm';
  826. if ($newsortfield == 'amount_ht') $newsortfield = 'amount';
  827. if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';
  828. $sql.= $db->order($newsortfield, $sortorder);
  829. dol_syslog("get vat really paid", LOG_DEBUG);
  830. $result=$db->query($sql);
  831. if ($result) {
  832. $num = $db->num_rows($result);
  833. $i = 0;
  834. if ($num) {
  835. while ($i < $num) {
  836. $obj = $db->fetch_object($result);
  837. $amount -= $obj->amount;
  838. $total_ht -= $obj->amount;
  839. $total_ttc -= $obj->amount;
  840. $subtotal_ht -= $obj->amount;
  841. $subtotal_ttc -= $obj->amount;
  842. $i++;
  843. }
  844. }
  845. $db->free($result);
  846. } else {
  847. dol_print_error($db);
  848. }
  849. print '<tr class="oddeven"><td>&nbsp;</td>';
  850. print "<td>".$langs->trans("VATPaid")."</td>\n";
  851. if ($modecompta == 'CREANCES-DETTES')
  852. print "<td align=\"right\">".price($amount)."</td>\n";
  853. print "<td align=\"right\">".price($amount)."</td>\n";
  854. print "</tr>\n";
  855. // VAT really received
  856. $amount=0;
  857. $sql = "SELECT date_format(t.datev,'%Y-%m') as dm, sum(t.amount) as amount";
  858. $sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
  859. $sql.= " WHERE amount < 0";
  860. if (! empty($date_start) && ! empty($date_end))
  861. $sql.= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";
  862. $sql.= " AND t.entity = ".$conf->entity;
  863. $sql.= " GROUP BY dm";
  864. $newsortfield = $sortfield;
  865. if ($newsortfield == 's.nom, s.rowid') $newsortfield = 'dm';
  866. if ($newsortfield == 'amount_ht') $newsortfield = 'amount';
  867. if ($newsortfield == 'amount_ttc') $newsortfield = 'amount';
  868. $sql.= $db->order($newsortfield, $sortorder);
  869. dol_syslog("get vat really received back", LOG_DEBUG);
  870. $result=$db->query($sql);
  871. if ($result) {
  872. $num = $db->num_rows($result);
  873. $i = 0;
  874. if ($num) {
  875. while ($i < $num) {
  876. $obj = $db->fetch_object($result);
  877. $amount += $obj->amount;
  878. $total_ht += $obj->amount;
  879. $total_ttc += $obj->amount;
  880. $subtotal_ht += $obj->amount;
  881. $subtotal_ttc += $obj->amount;
  882. $i++;
  883. }
  884. }
  885. $db->free($result);
  886. }
  887. else
  888. {
  889. dol_print_error($db);
  890. }
  891. print '<tr class="oddeven"><td>&nbsp;</td>';
  892. print "<td>".$langs->trans("VATCollected")."</td>\n";
  893. if ($modecompta == 'CREANCES-DETTES')
  894. print "<td align=\"right\">".price($amount)."</td>\n";
  895. print "<td align=\"right\">".price($amount)."</td>\n";
  896. print "</tr>\n";
  897. }
  898. if ($mysoc->tva_assuj != 'franchise') // Assujetti
  899. {
  900. print '<tr class="liste_total">';
  901. if ($modecompta == 'CREANCES-DETTES')
  902. print '<td colspan="3" align="right">&nbsp;</td>';
  903. print '<td colspan="3" align="right">'.price(price2num($subtotal_ttc,'MT')).'</td>';
  904. print '</tr>';
  905. }
  906. $action = "balanceclient";
  907. $object = array(&$total_ht, &$total_ttc);
  908. $parameters["mode"] = $modecompta;
  909. $parameters["date_start"] = $date_start;
  910. $parameters["date_end"] = $date_end;
  911. $parameters["bc"] = $bc;
  912. // Initialize technical object to manage hooks of expenses. Note that conf->hooks_modules contains array array
  913. $hookmanager->initHooks(array('externalbalance'));
  914. $reshook=$hookmanager->executeHooks('addBalanceLine',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  915. print $hookmanager->resPrint;
  916. if ($mysoc->tva_assuj != 'franchise') // Assujetti
  917. {
  918. // Total
  919. print '<tr>';
  920. print '<td colspan="4">&nbsp;</td>';
  921. print '</tr>';
  922. print '<tr class="liste_total"><td align="left" colspan="2">'.$langs->trans("Profit").'</td>';
  923. if ($modecompta == 'CREANCES-DETTES')
  924. print '<td class="liste_total" align="right">'.price(price2num($total_ht,'MT')).'</td>';
  925. print '<td class="liste_total" align="right">'.price(price2num($total_ttc,'MT')).'</td>';
  926. print '</tr>';
  927. }
  928. print "</table>";
  929. print '<br>';
  930. llxFooter();
  931. $db->close();