PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/compta/resultat/clientfourn.php

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