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