31 require
'../../main.inc.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/lib/report.lib.php';
33 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
34 require_once DOL_DOCUMENT_ROOT.
'/core/lib/tax.lib.php';
35 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
36 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.form.class.php';
37 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formother.class.php';
38 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
41 $langs->loadLangs(array(
'companies',
'categories',
'bills',
'compta'));
44 $modecompta = $conf->global->ACCOUNTING_MODE;
46 $modecompta =
GETPOST(
"modecompta");
49 $sortorder =
GETPOST(
"sortorder",
'aZ09comma');
50 $sortfield =
GETPOST(
"sortfield",
'aZ09comma');
58 $socid =
GETPOST(
'socid',
'int');
61 $selected_cat = (int)
GETPOST(
'search_categ',
'int');
62 if ($selected_cat == -1) $selected_cat =
'';
64 if (
GETPOST(
'subcat',
'alpha') ===
'yes') {
69 if ($user->socid > 0) {
70 $socid = $user->socid;
76 $result =
restrictedArea($user,
'accounting',
'',
'',
'comptarapport');
80 $hookmanager->initHooks(array(
'casoclist'));
84 $month =
GETPOST(
"month",
'int');
85 $search_societe =
GETPOST(
"search_societe",
'alpha');
86 $search_zip =
GETPOST(
"search_zip",
'alpha');
87 $search_town =
GETPOST(
"search_town",
'alpha');
88 $search_country =
GETPOST(
"search_country",
'alpha');
89 $date_startyear =
GETPOST(
"date_startyear",
'int');
90 $date_startmonth =
GETPOST(
"date_startmonth",
'int');
91 $date_startday =
GETPOST(
"date_startday",
'int');
92 $date_endyear =
GETPOST(
"date_endyear",
'int');
93 $date_endmonth =
GETPOST(
"date_endmonth",
'int');
94 $date_endday =
GETPOST(
"date_endday",
'int');
98 $year_start = $year_current;
100 $year_current = $year;
107 if (empty($date_start) || empty($date_end)) {
111 $month_start =
GETPOST(
"month") ?
GETPOST(
"month") : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
112 $year_end = $year_start;
113 $month_end = $month_start;
115 if (!
GETPOST(
'year') && $month_start > $month_current) {
119 $month_end = $month_start - 1;
120 if ($month_end < 1) {
152 $year_start = $tmps[
'year'];
154 $year_end = $tmpe[
'year'];
155 $nbofyear = ($year_end - $year_start) + 1;
157 $commonparams = array();
158 $commonparams[
'modecompta'] = $modecompta;
159 $commonparams[
'sortorder'] = $sortorder;
160 $commonparams[
'sortfield'] = $sortfield;
162 $headerparams = array();
163 if (!empty($date_startyear)) {
164 $headerparams[
'date_startyear'] = $date_startyear;
166 if (!empty($date_startmonth)) {
167 $headerparams[
'date_startmonth'] = $date_startmonth;
169 if (!empty($date_startday)) {
170 $headerparams[
'date_startday'] = $date_startday;
172 if (!empty($date_endyear)) {
173 $headerparams[
'date_endyear'] = $date_endyear;
175 if (!empty($date_endmonth)) {
176 $headerparams[
'date_endmonth'] = $date_endmonth;
178 if (!empty($date_endday)) {
179 $headerparams[
'date_endday'] = $date_endday;
182 $headerparams[
'q'] = $q;
185 $tableparams = array();
186 $tableparams[
'search_categ'] = $selected_cat;
187 $tableparams[
'search_societe'] = $search_societe;
188 $tableparams[
'search_zip'] = $search_zip;
189 $tableparams[
'search_town'] = $search_town;
190 $tableparams[
'search_country'] = $search_country;
191 $tableparams[
'subcat'] = ($subcat ===
true) ?
'yes' :
'';
194 $allparams = array_merge($commonparams, $headerparams, $tableparams);
195 $headerparams = array_merge($commonparams, $headerparams);
196 $tableparams = array_merge($commonparams, $tableparams);
199 foreach ($allparams as $key => $value) {
200 $paramslink .=
'&'.$key.
'='.$value;
211 $thirdparty_static =
new Societe($db);
215 if ($modecompta ==
"BOOKKEEPING") {
216 $modecompta =
"CREANCES-DETTES";
218 if ($modecompta ==
"BOOKKEEPINGCOLLECTED") {
219 $modecompta =
"RECETTES-DEPENSES";
226 if ($modecompta ==
"CREANCES-DETTES") {
227 $name = $langs->trans(
"Turnover").
', '.$langs->trans(
"ByThirdParties");
228 $calcmode = $langs->trans(
"CalcModeDebt");
230 $description = $langs->trans(
"RulesCADue");
231 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
232 $description .= $langs->trans(
"DepositsAreNotIncluded");
234 $description .= $langs->trans(
"DepositsAreIncluded");
238 } elseif ($modecompta ==
"RECETTES-DEPENSES") {
239 $name = $langs->trans(
"TurnoverCollected").
', '.$langs->trans(
"ByThirdParties");
240 $calcmode = $langs->trans(
"CalcModeEngagement");
242 $description = $langs->trans(
"RulesCAIn");
243 $description .= $langs->trans(
"DepositsAreIncluded");
246 } elseif ($modecompta ==
"BOOKKEEPING") {
247 } elseif ($modecompta ==
"BOOKKEEPINGCOLLECTED") {
249 $period =
$form->selectDate($date_start,
'date_start', 0, 0, 0,
'', 1, 0, 0,
'',
'',
'',
'', 1,
'',
'',
'tzserver');
251 $period .=
$form->selectDate($date_end,
'date_end', 0, 0, 0,
'', 1, 0, 0,
'',
'',
'',
'', 1,
'',
'',
'tzserver');
253 $periodlink =
'<a href="'.$_SERVER[
"PHP_SELF"].
'?year='.($year_start - 1).
'&modecompta='.$modecompta.
'">'.
img_previous().
'</a> <a href="'.$_SERVER[
"PHP_SELF"].
'?year='.($year_start + 1).
'&modecompta='.$modecompta.
'">'.
img_next().
'</a>';
258 report_header($name, $namelink, $period, $periodlink, $description, $builddate, $exportlink, $tableparams, $calcmode);
260 if (
isModEnabled(
'accounting') && $modecompta !=
'BOOKKEEPING') {
261 print
info_admin($langs->trans(
"WarningReportNotReliable"), 0, 0, 1);
271 if ($modecompta ==
'CREANCES-DETTES') {
272 $sql =
"SELECT DISTINCT s.rowid as socid, s.nom as name, s.name_alias, s.zip, s.town, s.fk_pays,";
273 $sql .=
" sum(f.total_ht) as amount, sum(f.total_ttc) as amount_ttc";
274 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f, ".MAIN_DB_PREFIX.
"societe as s";
275 if ($selected_cat === -2) {
276 $sql .=
" LEFT OUTER JOIN ".MAIN_DB_PREFIX.
"categorie_societe as cs ON s.rowid = cs.fk_soc";
277 } elseif ($selected_cat) {
278 $sql .=
", ".MAIN_DB_PREFIX.
"categorie as c, ".MAIN_DB_PREFIX.
"categorie_societe as cs";
280 $sql .=
" WHERE f.fk_statut in (1,2)";
281 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
282 $sql .=
" AND f.type IN (0,1,2,5)";
284 $sql .=
" AND f.type IN (0,1,2,3,5)";
286 $sql .=
" AND f.fk_soc = s.rowid";
287 if ($date_start && $date_end) {
288 $sql .=
" AND f.datef >= '".$db->idate($date_start).
"' AND f.datef <= '".$db->idate($date_end).
"'";
290 if ($selected_cat === -2) {
291 $sql .=
" AND cs.fk_soc is null";
292 } elseif ($selected_cat) {
293 $sql .=
" AND (c.rowid = ".((int) $selected_cat);
295 $sql .=
" OR c.fk_parent = ".((int) $selected_cat);
298 $sql .=
" AND cs.fk_categorie = c.rowid AND cs.fk_soc = s.rowid";
300 } elseif ($modecompta ==
"RECETTES-DEPENSES") {
305 $sql =
"SELECT s.rowid as socid, s.nom as name, s.name_alias, s.zip, s.town, s.fk_pays, sum(pf.amount) as amount_ttc";
306 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
307 $sql .=
", ".MAIN_DB_PREFIX.
"paiement_facture as pf";
308 $sql .=
", ".MAIN_DB_PREFIX.
"paiement as p";
309 $sql .=
", ".MAIN_DB_PREFIX.
"societe as s";
310 if ($selected_cat === -2) {
311 $sql .=
" LEFT OUTER JOIN ".MAIN_DB_PREFIX.
"categorie_societe as cs ON s.rowid = cs.fk_soc";
312 } elseif ($selected_cat) {
313 $sql .=
", ".MAIN_DB_PREFIX.
"categorie as c, ".MAIN_DB_PREFIX.
"categorie_societe as cs";
315 $sql .=
" WHERE p.rowid = pf.fk_paiement";
316 $sql .=
" AND pf.fk_facture = f.rowid";
317 $sql .=
" AND f.fk_soc = s.rowid";
318 if ($date_start && $date_end) {
319 $sql .=
" AND p.datep >= '".$db->idate($date_start).
"' AND p.datep <= '".$db->idate($date_end).
"'";
321 if ($selected_cat === -2) {
322 $sql .=
" AND cs.fk_soc is null";
323 } elseif ($selected_cat) {
324 $sql .=
" AND (c.rowid = ".((int) $selected_cat);
326 $sql .=
" OR c.fk_parent = ".((int) $selected_cat);
329 $sql .=
" AND cs.fk_categorie = c.rowid AND cs.fk_soc = s.rowid";
331 } elseif ($modecompta ==
"BOOKKEEPING") {
332 } elseif ($modecompta ==
"BOOKKEEPINGCOLLECTED") {
334 if (!empty($search_societe)) {
337 if (!empty($search_zip)) {
340 if (!empty($search_town)) {
343 if ($search_country > 0) {
344 $sql .=
' AND s.fk_pays = '.((int) $search_country);
346 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
348 $sql .=
" AND f.fk_soc = ".((int) $socid);
350 $sql .=
" GROUP BY s.rowid, s.nom, s.name_alias, s.zip, s.town, s.fk_pays";
351 $sql .=
" ORDER BY s.rowid";
357 $result = $db->query(
$sql);
359 $num = $db->num_rows($result);
362 $obj = $db->fetch_object($result);
364 $amount_ht[$obj->socid] = (empty($obj->amount) ? 0 : $obj->amount);
365 $amount[$obj->socid] = $obj->amount_ttc;
366 $fullname = $obj->name;
367 if (!empty($obj->name_alias)) {
368 $fullname .=
' ('.$obj->name_alias.
')';
370 $name[$obj->socid] = $fullname;
372 $address_zip[$obj->socid] = $obj->zip;
373 $address_town[$obj->socid] = $obj->town;
374 $address_pays[$obj->socid] =
getCountry($obj->fk_pays);
376 $catotal_ht += (empty($obj->amount) ? 0 : $obj->amount);
377 $catotal += $obj->amount_ttc;
386 if ($modecompta ==
"RECETTES-DEPENSES") {
387 $sql =
"SELECT '0' as socid, 'Autres' as name, sum(p.amount) as amount_ttc";
388 $sql .=
" FROM ".MAIN_DB_PREFIX.
"bank as b";
389 $sql .=
", ".MAIN_DB_PREFIX.
"bank_account as ba";
390 $sql .=
", ".MAIN_DB_PREFIX.
"paiement as p";
391 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiement_facture as pf ON p.rowid = pf.fk_paiement";
392 $sql .=
" WHERE pf.rowid IS NULL";
393 $sql .=
" AND p.fk_bank = b.rowid";
394 $sql .=
" AND b.fk_account = ba.rowid";
395 $sql .=
" AND ba.entity IN (".getEntity(
'bank_account').
")";
396 if ($date_start && $date_end) {
397 $sql .=
" AND p.datep >= '".$db->idate($date_start).
"' AND p.datep <= '".$db->idate($date_end).
"'";
399 $sql .=
" GROUP BY socid, name";
400 $sql .=
" ORDER BY name";
402 $result = $db->query(
$sql);
404 $num = $db->num_rows($result);
407 $obj = $db->fetch_object($result);
409 if (empty($amount[$obj->socid])) {
410 $amount[$obj->socid] = $obj->amount_ttc;
412 $amount[$obj->socid] += $obj->amount_ttc;
415 $name[$obj->socid] = $obj->name;
416 $address_zip[$obj->socid] =
'';
417 $address_town[$obj->socid] =
'';
418 $address_pays[$obj->socid] = 0;
420 $catotal += $obj->amount_ttc;
432 print
'<form method="POST" action="'.$_SERVER[
"PHP_SELF"].
'">';
433 print
'<input type="hidden" name="token" value="'.newToken().
'">'.
"\n";
435 foreach ($headerparams as $key => $value) {
436 print
'<input type="hidden" name="'.$key.
'" value="'.$value.
'">';
441 print
'<div class="div-table-responsive">';
442 print
'<table class="tagtable liste'.($moreforfilter ?
" listwithfilterbefore" :
"").
'">'.
"\n";
445 print
'<tr class="liste_titre">';
446 print
'<td class="nowraponall">';
447 print
img_picto(
'',
'category',
'class="paddingrightonly"');
448 print $formother->select_categories(Categorie::TYPE_CUSTOMER, $selected_cat,
'search_categ', 0, $langs->trans(
"Category"));
450 print $langs->trans(
"SubCats").
'? ';
451 print
'<input type="checkbox" name="subcat" value="yes"';
456 print
'<td colspan="7" class="right">';
457 print
'<input type="image" class="liste_titre" name="button_search" src="'.img_picto($langs->trans(
"Search"),
'search.png',
'',
'', 1).
'" value="'.
dol_escape_htmltag($langs->trans(
"Search")).
'" title="'.
dol_escape_htmltag($langs->trans(
"Search")).
'">';
461 print
'<tr class="liste_titre">';
462 print
'<td class="liste_titre left">';
463 print
'<input class="flat" size="6" type="text" name="search_societe" value="'.dol_escape_htmltag($search_societe).
'">';
465 print
'<td class="liste_titre left">';
466 print
'<input class="flat" size="6" type="text" name="search_zip" value="'.dol_escape_htmltag($search_zip).
'">';
468 print
'<td class="liste_titre left">';
469 print
'<input class="flat" size="6" type="text" name="search_town" value="'.dol_escape_htmltag($search_town).
'">';
471 print
'<td class="liste_titre left">';
472 print
$form->select_country($search_country,
'search_country',
'', 0,
'minwidth100imp maxwidth100');
475 print
'<td class="liste_titre"> </td>';
476 print
'<td class="liste_titre"> </td>';
477 print
'<td class="liste_titre"> </td>';
478 print
'<td class="liste_titre"> </td>';
482 print
"<tr class='liste_titre'>";
484 $langs->trans(
"Company"),
485 $_SERVER[
"PHP_SELF"],
494 $langs->trans(
"Zip"),
495 $_SERVER[
"PHP_SELF"],
504 $langs->trans(
"Town"),
505 $_SERVER[
"PHP_SELF"],
514 $langs->trans(
"Country"),
515 $_SERVER[
"PHP_SELF"],
523 if ($modecompta ==
'CREANCES-DETTES') {
525 $langs->trans(
'AmountHT'),
526 $_SERVER[
"PHP_SELF"],
538 $langs->trans(
"AmountTTC"),
539 $_SERVER[
"PHP_SELF"],
548 $langs->trans(
"Percentage"),
549 $_SERVER[
"PHP_SELF"],
558 $langs->trans(
"OtherStatistics"),
559 $_SERVER[
"PHP_SELF"],
563 'align="center" width="20%"'
568 if (count($amount)) {
569 $arrayforsort = $name;
571 if ($sortfield ==
'nom' && $sortorder ==
'asc') {
573 $arrayforsort = $name;
575 if ($sortfield ==
'nom' && $sortorder ==
'desc') {
577 $arrayforsort = $name;
579 if ($sortfield ==
'amount_ht' && $sortorder ==
'asc') {
581 $arrayforsort = $amount_ht;
583 if ($sortfield ==
'amount_ht' && $sortorder ==
'desc') {
585 $arrayforsort = $amount_ht;
587 if ($sortfield ==
'amount_ttc' && $sortorder ==
'asc') {
589 $arrayforsort = $amount;
591 if ($sortfield ==
'amount_ttc' && $sortorder ==
'desc') {
593 $arrayforsort = $amount;
595 if ($sortfield ==
'zip' && $sortorder ==
'asc') {
597 $arrayforsort = $address_zip;
599 if ($sortfield ==
'zip' && $sortorder ==
'desc') {
600 arsort($address_zip);
601 $arrayforsort = $address_zip;
603 if ($sortfield ==
'town' && $sortorder ==
'asc') {
604 asort($address_town);
605 $arrayforsort = $address_town;
607 if ($sortfield ==
'town' && $sortorder ==
'desc') {
608 arsort($address_town);
609 $arrayforsort = $address_town;
611 if ($sortfield ==
'country' && $sortorder ==
'asc') {
612 asort($address_pays);
613 $arrayforsort = $address_town;
615 if ($sortfield ==
'country' && $sortorder ==
'desc') {
616 arsort($address_pays);
617 $arrayforsort = $address_town;
620 foreach ($arrayforsort as $key => $value) {
621 print
'<tr class="oddeven">';
624 $fullname = $name[$key];
626 $thirdparty_static->id = $key;
627 $thirdparty_static->name = $fullname;
628 $thirdparty_static->client = 1;
629 $linkname = $thirdparty_static->getNomUrl(1,
'customer');
631 $linkname = $langs->trans(
"PaymentsNotLinkedToInvoice");
633 print
"<td>".$linkname.
"</td>\n";
636 print $address_zip[$key];
640 print $address_town[$key];
644 print $address_pays[$key];
648 print
'<td class="right">';
649 if ($modecompta !=
'CREANCES-DETTES') {
651 print
'<a href="'.DOL_URL_ROOT.
'/compta/paiement/list.php?socid='.$key.
'">';
653 print
'<a href="'.DOL_URL_ROOT.
'/compta/paiement/list.php?socid=-1">';
657 print
'<a href="'.DOL_URL_ROOT.
'/compta/facture/list.php?socid='.$key.
'">';
659 print
'<a href="#">';
661 print
price($amount_ht[$key]);
666 print
'<td class="right">';
667 if ($modecompta !=
'CREANCES-DETTES') {
669 print
'<a href="'.DOL_URL_ROOT.
'/compta/paiement/list.php?socid='.$key.
'">';
671 print
'<a href="'.DOL_URL_ROOT.
'/compta/paiement/list.php?orphelins=1">';
675 print
'<a href="'.DOL_URL_ROOT.
'/compta/facture/list.php?socid='.$key.
'">';
677 print
'<a href="#">';
680 print
price($amount[$key]);
685 print
'<td class="right">'.($catotal > 0 ? round(100 * $amount[$key] / $catotal, 2).
'%' :
' ').
'</td>';
688 print
'<td class="center">';
690 print
' <a href="'.DOL_URL_ROOT.
'/comm/propal/stats/index.php?socid='.$key.
'">'.
img_picto($langs->trans(
"ProposalStats"),
"stats").
'</a> ';
693 print
' <a href="'.DOL_URL_ROOT.
'/commande/stats/index.php?socid='.$key.
'">'.
img_picto($langs->trans(
"OrderStats"),
"stats").
'</a> ';
696 print
' <a href="'.DOL_URL_ROOT.
'/compta/facture/stats/index.php?socid='.$key.
'">'.
img_picto($langs->trans(
"InvoiceStats"),
"stats").
'</a> ';
704 print
'<tr class="liste_total">';
705 print
'<td>'.$langs->trans(
"Total").
'</td>';
706 print
'<td> </td>';
707 print
'<td> </td>';
708 print
'<td> </td>';
709 if ($modecompta !=
'CREANCES-DETTES') {
712 print
'<td class="right">'.price($catotal_ht).
'</td>';
714 print
'<td class="right">'.price($catotal).
'</td>';
715 print
'<td> </td>';
716 print
'<td> </td>';