PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/compta/resultat/clientfourn.php

https://github.com/asterix14/dolibarr
PHP | 692 lines | 536 code | 79 blank | 77 comment | 144 complexity | c09193529fc76bf8fe1ed84f216432ec MD5 | raw file
Possible License(s): LGPL-2.0
  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-2009 Regis Houssin <regis@dolibarr.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/compta/resultat/clientfourn.php
  21. * \brief Page reporting
  22. */
  23. require('../../main.inc.php');
  24. require_once(DOL_DOCUMENT_ROOT."/compta/tva/class/tva.class.php");
  25. require_once(DOL_DOCUMENT_ROOT."/compta/sociales/class/chargesociales.class.php");
  26. require_once(DOL_DOCUMENT_ROOT."/core/lib/report.lib.php");
  27. require_once(DOL_DOCUMENT_ROOT."/core/lib/tax.lib.php");
  28. require_once(DOL_DOCUMENT_ROOT."/core/lib/date.lib.php");
  29. $langs->load("bills");
  30. // Security check
  31. $socid = GETPOST("socid");
  32. if ($user->societe_id > 0) $socid = $user->societe_id;
  33. if (!$user->rights->compta->resultat->lire && !$user->rights->accounting->comptarapport->lire)
  34. accessforbidden();
  35. // Date range
  36. $year=GETPOST("year");
  37. if (empty($year))
  38. {
  39. $year_current = strftime("%Y",dol_now());
  40. $month_current = strftime("%m",dol_now());
  41. $year_start = $year_current;
  42. } else {
  43. $year_current = $year;
  44. $month_current = strftime("%m",dol_now());
  45. $year_start = $year;
  46. }
  47. $date_start=dol_mktime(0,0,0,$_REQUEST["date_startmonth"],$_REQUEST["date_startday"],$_REQUEST["date_startyear"]); // Date for local PHP server
  48. $date_end=dol_mktime(23,59,59,$_REQUEST["date_endmonth"],$_REQUEST["date_endday"],$_REQUEST["date_endyear"]); // Date for local PHP server
  49. // Quarter
  50. if (empty($date_start) || empty($date_end)) // We define date_start and date_end
  51. {
  52. $q=GETPOST("q")?GETPOST("q"):0;
  53. if ($q==0)
  54. {
  55. // We define date_start and date_end
  56. $year_end=$year_start;
  57. $month_start=GETPOST("month")?GETPOST("month"):($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
  58. if (! GETPOST('month'))
  59. {
  60. if (! GETPOST("year") && $month_start > $month_current)
  61. {
  62. $year_start--;
  63. $year_end--;
  64. }
  65. $month_end=$month_start-1;
  66. if ($month_end < 1) $month_end=12;
  67. else $year_end++;
  68. }
  69. else $month_end=$month_start;
  70. $date_start=dol_get_first_day($year_start,$month_start,false); $date_end=dol_get_last_day($year_end,$month_end,false);
  71. }
  72. if ($q==1) { $date_start=dol_get_first_day($year_start,1,false); $date_end=dol_get_last_day($year_start,3,false); }
  73. if ($q==2) { $date_start=dol_get_first_day($year_start,4,false); $date_end=dol_get_last_day($year_start,6,false); }
  74. if ($q==3) { $date_start=dol_get_first_day($year_start,7,false); $date_end=dol_get_last_day($year_start,9,false); }
  75. if ($q==4) { $date_start=dol_get_first_day($year_start,10,false); $date_end=dol_get_last_day($year_start,12,false); }
  76. }
  77. else
  78. {
  79. // TODO We define q
  80. }
  81. // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES')
  82. $modecompta = $conf->global->COMPTA_MODE;
  83. if ($_GET["modecompta"]) $modecompta=$_GET["modecompta"];
  84. /*
  85. * View
  86. */
  87. llxHeader();
  88. $form=new Form($db);
  89. // Affiche en-tete de rapport
  90. if ($modecompta=="CREANCES-DETTES")
  91. {
  92. $nom=$langs->trans("AnnualByCompaniesDueDebtMode");
  93. $nom.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.(GETPOST("month")>0?'&month='.GETPOST("month"):'').'&modecompta=RECETTES-DEPENSES">','</a>').')';
  94. $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);
  95. //$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>';
  96. $description=$langs->trans("RulesResultDue");
  97. $builddate=time();
  98. //$exportlink=$langs->trans("NotYetAvailable");
  99. }
  100. else {
  101. $nom=$langs->trans("AnnualByCompaniesInputOutputMode");
  102. $nom.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.(GETPOST("month")>0?'&month='.GETPOST("month"):'').'&modecompta=CREANCES-DETTES">','</a>').')';
  103. //$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);
  104. $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);
  105. //$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>';
  106. $description=$langs->trans("RulesResultInOut");
  107. $builddate=time();
  108. //$exportlink=$langs->trans("NotYetAvailable");
  109. }
  110. report_header($nom,$nomlink,$period,$periodlink,$description,$builddate,$exportlink);
  111. // Show report array
  112. print '<table class="noborder" width="100%">';
  113. print '<tr class="liste_titre">';
  114. print '<td width="10%">&nbsp;</td><td>&nbsp;</td>';
  115. if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".$langs->trans("AmountHT")."</td>";
  116. print "<td align=\"right\">".$langs->trans("AmountTTC")."</td>";
  117. print "</tr>\n";
  118. /*
  119. * Factures clients
  120. */
  121. print '<tr><td colspan="4">'.$langs->trans("CustomersInvoices").'</td></tr>';
  122. if ($modecompta == 'CREANCES-DETTES') {
  123. $sql = "SELECT s.nom, s.rowid as socid, sum(f.total) as amount_ht, sum(f.total_ttc) as amount_ttc";
  124. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  125. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  126. $sql.= " WHERE f.fk_soc = s.rowid";
  127. $sql.= " AND f.fk_statut in (1,2)";
  128. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  129. } else {
  130. /*
  131. * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
  132. * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
  133. */
  134. $sql = "SELECT s.nom as nom, s.rowid as socid, sum(pf.amount) as amount_ttc";
  135. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  136. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  137. $sql.= ", ".MAIN_DB_PREFIX."paiement_facture as pf";
  138. $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
  139. $sql.= " WHERE p.rowid = pf.fk_paiement";
  140. $sql.= " AND pf.fk_facture = f.rowid";
  141. $sql.= " AND f.fk_soc = s.rowid";
  142. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  143. }
  144. $sql.= " AND f.entity = ".$conf->entity;
  145. if ($socid) $sql.= " AND f.fk_soc = ".$socid;
  146. $sql.= " GROUP BY s.nom, s.rowid";
  147. $sql.= " ORDER BY s.nom";
  148. /*
  149. print dol_print_date($date_end,'dayhour',true).'<br>';
  150. print $db->idate($date_end).'<br>';
  151. print adodb_get_gmt_diff();
  152. print adodb_date('Y-d-m H:i',$date_end,true).'<br>';
  153. print adodb_date('Y-d-m H:i',$date_end,false).'<br>';
  154. $date_end=0;
  155. print dol_print_date($date_end,'dayhour',true).'<br>';
  156. print $db->idate($date_end).'<br>';
  157. print adodb_gmstrftime('%Y-%d-%m %H:%M',$date_end).'<br>';
  158. print $sql;
  159. */
  160. dol_syslog("get customer invoices sql=".$sql);
  161. $result = $db->query($sql);
  162. if ($result) {
  163. $num = $db->num_rows($result);
  164. $i = 0;
  165. $var=true;
  166. while ($i < $num)
  167. {
  168. $objp = $db->fetch_object($result);
  169. $var=!$var;
  170. print "<tr $bc[$var]><td>&nbsp;</td>";
  171. print "<td>".$langs->trans("Bills")." <a href=\"../facture.php?socid=".$objp->socid."\">$objp->nom</td>\n";
  172. if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price($objp->amount_ht)."</td>\n";
  173. print "<td align=\"right\">".price($objp->amount_ttc)."</td>\n";
  174. $total_ht = $total_ht + $objp->amount_ht;
  175. $total_ttc = $total_ttc + $objp->amount_ttc;
  176. print "</tr>\n";
  177. $i++;
  178. }
  179. $db->free($result);
  180. } else {
  181. dol_print_error($db);
  182. }
  183. // On ajoute les paiements clients anciennes version, non lie par paiement_facture
  184. if ($modecompta != 'CREANCES-DETTES')
  185. {
  186. $sql = "SELECT 'Autres' as nom, '0' as idp, sum(p.amount) as amount_ttc";
  187. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  188. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  189. $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
  190. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
  191. $sql.= " WHERE pf.rowid IS NULL";
  192. $sql.= " AND p.fk_bank = b.rowid";
  193. $sql.= " AND b.fk_account = ba.rowid";
  194. $sql.= " AND ba.entity = ".$conf->entity;
  195. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  196. $sql.= " GROUP BY nom, idp";
  197. $sql.= " ORDER BY nom";
  198. dol_syslog("get old customer payments not linked to invoices sql=".$sql);
  199. $result = $db->query($sql);
  200. if ($result) {
  201. $num = $db->num_rows($result);
  202. $i = 0;
  203. if ($num) {
  204. while ($i < $num)
  205. {
  206. $objp = $db->fetch_object($result);
  207. $var=!$var;
  208. print "<tr $bc[$var]><td>&nbsp;</td>";
  209. print "<td>".$langs->trans("Bills")." ".$langs->trans("Other")." (".$langs->trans("PaymentsNotLinkedToInvoice").")\n";
  210. if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price($objp->amount_ht)."</td>\n";
  211. print "<td align=\"right\">".price($objp->amount_ttc)."</td>\n";
  212. $total_ht = $total_ht + $objp->amount_ht;
  213. $total_ttc = $total_ttc + $objp->amount_ttc;
  214. print "</tr>\n";
  215. $i++;
  216. }
  217. }
  218. $db->free($result);
  219. } else {
  220. dol_print_error($db);
  221. }
  222. }
  223. if ($total_ttc == 0)
  224. {
  225. $var=!$var;
  226. print "<tr $bc[$var]><td>&nbsp;</td>";
  227. print '<td colspan="3">'.$langs->trans("None").'</td>';
  228. print '</tr>';
  229. }
  230. print '<tr class="liste_total">';
  231. if ($modecompta == 'CREANCES-DETTES') print '<td colspan="3" align="right">'.price($total_ht).'</td>';
  232. print '<td colspan="3" align="right">'.price($total_ttc).'</td>';
  233. print '</tr>';
  234. /*
  235. * Frais, factures fournisseurs.
  236. */
  237. if ($modecompta == 'CREANCES-DETTES')
  238. {
  239. $sql = "SELECT s.nom, s.rowid as socid, date_format(f.datef,'%Y-%m') as dm, sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc";
  240. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  241. $sql.= ", ".MAIN_DB_PREFIX."facture_fourn as f";
  242. $sql.= " WHERE f.fk_soc = s.rowid";
  243. $sql.= " AND f.fk_statut in (1,2)";
  244. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  245. } else {
  246. $sql = "SELECT s.nom, s.rowid as socid, date_format(p.datep,'%Y-%m') as dm, sum(pf.amount) as amount_ttc";
  247. $sql .= " FROM ".MAIN_DB_PREFIX."paiementfourn as p";
  248. $sql.= ", ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf";
  249. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f";
  250. $sql .= " ON pf.fk_facturefourn = f.rowid";
  251. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s";
  252. $sql .= " ON f.fk_soc = s.rowid";
  253. $sql .= " WHERE p.rowid = pf.fk_paiementfourn ";
  254. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  255. }
  256. $sql.= " AND f.entity = ".$conf->entity;
  257. if ($socid) $sql.= " AND f.fk_soc = ".$socid;
  258. $sql .= " GROUP BY s.nom, s.rowid, dm";
  259. $sql .= " ORDER BY s.nom, s.rowid";
  260. print '<tr><td colspan="4">'.$langs->trans("SuppliersInvoices").'</td></tr>';
  261. $subtotal_ht = 0;
  262. $subtotal_ttc = 0;
  263. dol_syslog("get suppliers invoices sql=".$sql);
  264. $result = $db->query($sql);
  265. if ($result) {
  266. $num = $db->num_rows($result);
  267. $i = 0;
  268. $var=true;
  269. if ($num > 0) {
  270. while ($i < $num) {
  271. $objp = $db->fetch_object($result);
  272. $var=!$var;
  273. print "<tr $bc[$var]><td>&nbsp;</td>";
  274. print "<td>".$langs->trans("Bills")." <a href=\"".DOL_URL_ROOT."/fourn/facture/index.php?socid=".$objp->socid."\">".$objp->nom."</a></td>\n";
  275. if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price(-$objp->amount_ht)."</td>\n";
  276. print "<td align=\"right\">".price(-$objp->amount_ttc)."</td>\n";
  277. $total_ht = $total_ht - $objp->amount_ht;
  278. $total_ttc = $total_ttc - $objp->amount_ttc;
  279. $subtotal_ht = $subtotal_ht + $objp->amount_ht;
  280. $subtotal_ttc = $subtotal_ttc + $objp->amount_ttc;
  281. print "</tr>\n";
  282. $i++;
  283. }
  284. }
  285. else {
  286. $var=!$var;
  287. print "<tr $bc[$var]><td>&nbsp;</td>";
  288. print '<td colspan="3">'.$langs->trans("None").'</td>';
  289. print '</tr>';
  290. }
  291. $db->free($result);
  292. } else {
  293. dol_print_error($db);
  294. }
  295. print '<tr class="liste_total">';
  296. if ($modecompta == 'CREANCES-DETTES') print '<td colspan="3" align="right">'.price(-$subtotal_ht).'</td>';
  297. print '<td colspan="3" align="right">'.price(-$subtotal_ttc).'</td>';
  298. print '</tr>';
  299. /*
  300. * Charges sociales non deductibles
  301. */
  302. print '<tr><td colspan="4">'.$langs->trans("SocialContributions").'</td></tr>';
  303. if ($modecompta == 'CREANCES-DETTES') {
  304. $sql = "SELECT c.libelle as nom, sum(s.amount) as amount";
  305. $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  306. $sql.= ", ".MAIN_DB_PREFIX."chargesociales as s";
  307. $sql.= " WHERE s.fk_type = c.id";
  308. $sql.= " AND c.deductible = 0";
  309. if ($date_start && $date_end) $sql.= " AND s.date_ech >= '".$db->idate($date_start)."' AND s.date_ech <= '".$db->idate($date_end)."'";
  310. }
  311. else {
  312. $sql = "SELECT c.libelle as nom, sum(p.amount) as amount";
  313. $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  314. $sql.= ", ".MAIN_DB_PREFIX."chargesociales as s";
  315. $sql.= ", ".MAIN_DB_PREFIX."paiementcharge as p";
  316. $sql.= " WHERE p.fk_charge = s.rowid";
  317. $sql.= " AND s.fk_type = c.id";
  318. $sql.= " AND c.deductible = 0";
  319. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  320. }
  321. $sql.= " AND s.entity = ".$conf->entity;
  322. $sql.= " GROUP BY c.libelle";
  323. dol_syslog("get social contributions deductible=0 sql=".$sql);
  324. $result=$db->query($sql);
  325. $subtotal_ht = 0;
  326. $subtotal_ttc = 0;
  327. if ($result) {
  328. $num = $db->num_rows($result);
  329. $var=true;
  330. $i = 0;
  331. if ($num) {
  332. while ($i < $num) {
  333. $obj = $db->fetch_object($result);
  334. $total_ht = $total_ht - $obj->amount;
  335. $total_ttc = $total_ttc - $obj->amount;
  336. $subtotal_ht = $subtotal_ht + $obj->amount;
  337. $subtotal_ttc = $subtotal_ttc + $obj->amount;
  338. $var = !$var;
  339. print "<tr $bc[$var]><td>&nbsp;</td>";
  340. print '<td>'.$obj->nom.'</td>';
  341. if ($modecompta == 'CREANCES-DETTES') print '<td align="right">'.price(-$obj->amount).'</td>';
  342. print '<td align="right">'.price(-$obj->amount).'</td>';
  343. print '</tr>';
  344. $i++;
  345. }
  346. }
  347. else {
  348. $var = !$var;
  349. print "<tr $bc[$var]><td>&nbsp;</td>";
  350. print '<td colspan="3">'.$langs->trans("None").'</td>';
  351. print '</tr>';
  352. }
  353. } else {
  354. dol_print_error($db);
  355. }
  356. print '<tr class="liste_total">';
  357. if ($modecompta == 'CREANCES-DETTES') print '<td colspan="3" align="right">'.price(-$subtotal_ht).'</td>';
  358. print '<td colspan="3" align="right">'.price(-$subtotal_ttc).'</td>';
  359. print '</tr>';
  360. /*
  361. * Charges sociales deductibles
  362. */
  363. print '<tr><td colspan="4">'.$langs->trans("SocialContributions").'</td></tr>';
  364. if ($modecompta == 'CREANCES-DETTES') {
  365. $sql = "SELECT c.libelle as nom, sum(s.amount) as amount";
  366. $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  367. $sql.= ", ".MAIN_DB_PREFIX."chargesociales as s";
  368. $sql.= " WHERE s.fk_type = c.id";
  369. $sql.= " AND c.deductible = 1";
  370. if ($date_start && $date_end) $sql.= " AND s.date_ech >= '".$db->idate($date_start)."' AND s.date_ech <= '".$db->idate($date_end)."'";
  371. $sql.= " AND s.entity = ".$conf->entity;
  372. $sql.= " GROUP BY c.libelle DESC";
  373. }
  374. else {
  375. $sql = "SELECT c.libelle as nom, sum(p.amount) as amount";
  376. $sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  377. $sql.= ", ".MAIN_DB_PREFIX."chargesociales as s";
  378. $sql.= ", ".MAIN_DB_PREFIX."paiementcharge as p";
  379. $sql .= " WHERE p.fk_charge = s.rowid";
  380. $sql.= " AND s.fk_type = c.id";
  381. $sql.= " AND c.deductible = 1";
  382. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  383. $sql.= " AND s.entity = ".$conf->entity;
  384. $sql.= " GROUP BY c.libelle";
  385. }
  386. dol_syslog("get social contributions deductible=1 sql=".$sql);
  387. $result=$db->query($sql);
  388. $subtotal_ht = 0;
  389. $subtotal_ttc = 0;
  390. if ($result) {
  391. $num = $db->num_rows($result);
  392. $var=true;
  393. $i = 0;
  394. if ($num) {
  395. while ($i < $num) {
  396. $obj = $db->fetch_object($result);
  397. $total_ht = $total_ht - $obj->amount;
  398. $total_ttc = $total_ttc - $obj->amount;
  399. $subtotal_ht = $subtotal_ht + $obj->amount;
  400. $subtotal_ttc = $subtotal_ttc + $obj->amount;
  401. $var = !$var;
  402. print "<tr $bc[$var]><td>&nbsp;</td>";
  403. print '<td>'.$obj->nom.'</td>';
  404. if ($modecompta == 'CREANCES-DETTES') print '<td align="right">'.price(-$obj->amount).'</td>';
  405. print '<td align="right">'.price(-$obj->amount).'</td>';
  406. print '</tr>';
  407. $i++;
  408. }
  409. }
  410. else {
  411. $var = !$var;
  412. print "<tr $bc[$var]><td>&nbsp;</td>";
  413. print '<td colspan="3">'.$langs->trans("None").'</td>';
  414. print '</tr>';
  415. }
  416. } else {
  417. dol_print_error($db);
  418. }
  419. print '<tr class="liste_total">';
  420. if ($modecompta == 'CREANCES-DETTES') print '<td colspan="3" align="right">'.price(-$subtotal_ht).'</td>';
  421. print '<td colspan="3" align="right">'.price(-$subtotal_ttc).'</td>';
  422. print '</tr>';
  423. if ($mysoc->tva_assuj == 'franchise') // Non assujeti
  424. {
  425. // Total
  426. print '<tr>';
  427. print '<td colspan="4">&nbsp;</td>';
  428. print '</tr>';
  429. print '<tr class="liste_total"><td align="left" colspan="2">'.$langs->trans("Profit").'</td>';
  430. if ($modecompta == 'CREANCES-DETTES') print '<td class="border" align="right">'.price($total_ht).'</td>';
  431. print '<td align="right">'.price($total_ttc).'</td>';
  432. print '</tr>';
  433. print '<tr>';
  434. print '<td colspan="4">&nbsp;</td>';
  435. print '</tr>';
  436. }
  437. /*
  438. * VAT
  439. */
  440. print '<tr><td colspan="4">'.$langs->trans("VAT").'</td></tr>';
  441. $subtotal_ht = 0;
  442. $subtotal_ttc = 0;
  443. if ($modecompta == 'CREANCES-DETTES')
  444. {
  445. // TVA a payer
  446. $amount=0;
  447. $sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.tva) as amount";
  448. $sql.= " FROM ".MAIN_DB_PREFIX."facture as f";
  449. $sql.= " WHERE f.fk_statut in (1,2)";
  450. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  451. $sql.= " AND f.entity = ".$conf->entity;
  452. $sql.= " GROUP BY dm";
  453. $sql.= " ORDER BY dm DESC";
  454. dol_syslog("get vat to pay sql=".$sql);
  455. $result=$db->query($sql);
  456. if ($result) {
  457. $num = $db->num_rows($result);
  458. $var=false;
  459. $i = 0;
  460. if ($num) {
  461. while ($i < $num) {
  462. $obj = $db->fetch_object($result);
  463. $amount = $amount - $obj->amount;
  464. $total_ht = $total_ht - $obj->amount;
  465. $total_ttc = $total_ttc - $obj->amount;
  466. $subtotal_ht = $subtotal_ht - $obj->amount;
  467. $subtotal_ttc = $subtotal_ttc - $obj->amount;
  468. $i++;
  469. }
  470. }
  471. } else {
  472. dol_print_error($db);
  473. }
  474. print "<tr $bc[$var]><td>&nbsp;</td>";
  475. print "<td>".$langs->trans("VATToPay")."</td>\n";
  476. if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price($amount)."</td>\n";
  477. print "<td align=\"right\">".price($amount)."</td>\n";
  478. print "</tr>\n";
  479. // TVA a recuperer
  480. $amount=0;
  481. $sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.total_tva) as amount";
  482. $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  483. $sql.= " WHERE f.fk_statut in (1,2)";
  484. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  485. $sql.= " AND f.entity = ".$conf->entity;
  486. $sql.= " GROUP BY dm";
  487. $sql.= " ORDER BY dm DESC";
  488. dol_syslog("get vat received back sql=".$sql);
  489. $result=$db->query($sql);
  490. if ($result) {
  491. $num = $db->num_rows($result);
  492. $var=true;
  493. $i = 0;
  494. if ($num) {
  495. while ($i < $num) {
  496. $obj = $db->fetch_object($result);
  497. $amount = $amount + $obj->amount;
  498. $total_ht = $total_ht + $obj->amount;
  499. $total_ttc = $total_ttc + $obj->amount;
  500. $subtotal_ht = $subtotal_ht + $obj->amount;
  501. $subtotal_ttc = $subtotal_ttc + $obj->amount;
  502. $i++;
  503. }
  504. }
  505. } else {
  506. dol_print_error($db);
  507. }
  508. print "<tr $bc[$var]><td>&nbsp;</td>";
  509. print "<td>".$langs->trans("VATToCollect")."</td>\n";
  510. if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price($amount)."</td>\n";
  511. print "<td align=\"right\">".price($amount)."</td>\n";
  512. print "</tr>\n";
  513. }
  514. else
  515. {
  516. // TVA reellement deja payee
  517. $amount=0;
  518. $sql = "SELECT date_format(t.datev,'%Y-%m') as dm, sum(t.amount) as amount";
  519. $sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
  520. $sql.= " WHERE amount > 0";
  521. if ($date_start && $date_end) $sql.= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";
  522. $sql.= " AND t.entity = ".$conf->entity;
  523. $sql.= " GROUP BY dm";
  524. $sql.= " ORDER BY dm DESC";
  525. dol_syslog("get vat really paid sql=".$sql);
  526. $result=$db->query($sql);
  527. if ($result) {
  528. $num = $db->num_rows($result);
  529. $var=false;
  530. $i = 0;
  531. if ($num) {
  532. while ($i < $num) {
  533. $obj = $db->fetch_object($result);
  534. $amount = $amount - $obj->amount;
  535. $total_ht = $total_ht - $obj->amount;
  536. $total_ttc = $total_ttc - $obj->amount;
  537. $subtotal_ht = $subtotal_ht - $obj->amount;
  538. $subtotal_ttc = $subtotal_ttc - $obj->amount;
  539. $i++;
  540. }
  541. }
  542. $db->free($result);
  543. } else {
  544. dol_print_error($db);
  545. }
  546. print "<tr $bc[$var]><td>&nbsp;</td>";
  547. print "<td>".$langs->trans("VATPaid")."</td>\n";
  548. if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price($amount)."</td>\n";
  549. print "<td align=\"right\">".price($amount)."</td>\n";
  550. print "</tr>\n";
  551. // TVA recuperee
  552. $amount=0;
  553. $sql = "SELECT date_format(t.datev,'%Y-%m') as dm, sum(t.amount) as amount";
  554. $sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
  555. $sql.= " WHERE amount < 0";
  556. if ($date_start && $date_end) $sql.= " AND t.datev >= '".$db->idate($date_start)."' AND t.datev <= '".$db->idate($date_end)."'";
  557. $sql.= " AND t.entity = ".$conf->entity;
  558. $sql.= " GROUP BY dm";
  559. $sql.= " ORDER BY dm DESC";
  560. dol_syslog("get vat really received back sql=".$sql);
  561. $result=$db->query($sql);
  562. if ($result) {
  563. $num = $db->num_rows($result);
  564. $var=true;
  565. $i = 0;
  566. if ($num) {
  567. while ($i < $num) {
  568. $obj = $db->fetch_object($result);
  569. $amount = $amount + $obj->amount;
  570. $total_ht = $total_ht + $obj->amount;
  571. $total_ttc = $total_ttc + $obj->amount;
  572. $subtotal_ht = $subtotal_ht + $obj->amount;
  573. $subtotal_ttc = $subtotal_ttc + $obj->amount;
  574. $i++;
  575. }
  576. }
  577. $db->free($result);
  578. }
  579. else
  580. {
  581. dol_print_error($db);
  582. }
  583. print "<tr $bc[$var]><td>&nbsp;</td>";
  584. print "<td>".$langs->trans("VATCollected")."</td>\n";
  585. if ($modecompta == 'CREANCES-DETTES') print "<td align=\"right\">".price($amount)."</td>\n";
  586. print "<td align=\"right\">".price($amount)."</td>\n";
  587. print "</tr>\n";
  588. }
  589. if ($mysoc->tva_assuj != 'franchise') // Assujeti
  590. {
  591. print '<tr class="liste_total">';
  592. if ($modecompta == 'CREANCES-DETTES') print '<td colspan="3" align="right">'.price(price2num($subtotal_ht,'MT')).'</td>';
  593. print '<td colspan="3" align="right">'.price(price2num($subtotal_ttc,'MT')).'</td>';
  594. print '</tr>';
  595. }
  596. if ($mysoc->tva_assuj != 'franchise') // Assujeti
  597. {
  598. // Total
  599. print '<tr>';
  600. print '<td colspan="4">&nbsp;</td>';
  601. print '</tr>';
  602. print '<tr class="liste_total"><td align="left" colspan="2">'.$langs->trans("Profit").'</td>';
  603. if ($modecompta == 'CREANCES-DETTES') print '<td class="liste_total" align="right">'.price(price2num($total_ht,'MT')).'</td>';
  604. print '<td class="liste_total" align="right">'.price(price2num($total_ttc,'MT')).'</td>';
  605. print '</tr>';
  606. }
  607. print "</table>";
  608. print '<br>';
  609. llxFooter();
  610. $db->close();
  611. ?>