29 require
'../../main.inc.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/lib/report.lib.php';
31 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/lib/tax.lib.php';
33 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
34 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.form.class.php';
35 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formother.class.php';
36 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
39 $langs->loadLangs(array(
'companies',
'categories',
'bills',
'compta'));
42 $modecompta = $conf->global->ACCOUNTING_MODE;
45 $sortorder = isset($_GET[
"sortorder"]) ? $_GET[
"sortorder"] : $_POST[
"sortorder"];
46 $sortfield = isset($_GET[
"sortfield"]) ? $_GET[
"sortfield"] : $_POST[
"sortfield"];
47 if (!$sortorder) $sortorder =
"asc";
48 if (!$sortfield) $sortfield =
"nom";
50 $socid =
GETPOST(
'socid',
'int');
53 $selected_cat = (int)
GETPOST(
'search_categ',
'int');
55 if (
GETPOST(
'subcat',
'alpha') ===
'yes') {
60 if ($user->socid > 0) $socid = $user->socid;
61 if (!empty($conf->comptabilite->enabled)) $result =
restrictedArea($user,
'compta',
'',
'',
'resultat');
62 if (!empty($conf->accounting->enabled)) $result =
restrictedArea($user,
'accounting',
'',
'',
'comptarapport');
65 $hookmanager->initHooks(array(
'casoclist'));
69 $month =
GETPOST(
"month",
'int');
70 $search_societe =
GETPOST(
"search_societe",
'alpha');
71 $search_zip =
GETPOST(
"search_zip",
'alpha');
72 $search_town =
GETPOST(
"search_town",
'alpha');
73 $search_country =
GETPOST(
"search_country",
'alpha');
74 $date_startyear =
GETPOST(
"date_startyear",
'alpha');
75 $date_startmonth =
GETPOST(
"date_startmonth",
'alpha');
76 $date_startday =
GETPOST(
"date_startday",
'alpha');
77 $date_endyear =
GETPOST(
"date_endyear",
'alpha');
78 $date_endmonth =
GETPOST(
"date_endmonth",
'alpha');
79 $date_endday =
GETPOST(
"date_endday",
'alpha');
82 $year_current = strftime(
"%Y",
dol_now());
83 $month_current = strftime(
"%m",
dol_now());
84 $year_start = $year_current;
86 $year_current = $year;
87 $month_current = strftime(
"%m",
dol_now());
93 if (empty($date_start) || empty($date_end))
99 $month_start =
GETPOST(
"month") ?
GETPOST(
"month") : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
100 $year_end = $year_start;
101 $month_end = $month_start;
104 if (!
GETPOST(
'year') && $month_start > $month_current)
109 $month_end = $month_start - 1;
110 if ($month_end < 1) $month_end = 12;
125 $year_start = $tmps[
'year'];
127 $year_end = $tmpe[
'year'];
128 $nbofyear = ($year_end - $year_start) + 1;
130 $commonparams = array();
131 $commonparams[
'modecompta'] = $modecompta;
132 $commonparams[
'sortorder'] = $sortorder;
133 $commonparams[
'sortfield'] = $sortfield;
135 $headerparams = array();
136 $headerparams[
'date_startyear'] = $date_startyear;
137 $headerparams[
'date_startmonth'] = $date_startmonth;
138 $headerparams[
'date_startday'] = $date_startday;
139 $headerparams[
'date_endyear'] = $date_endyear;
140 $headerparams[
'date_endmonth'] = $date_endmonth;
141 $headerparams[
'date_endday'] = $date_endday;
142 $headerparams[
'q'] = $q;
144 $tableparams = array();
145 $tableparams[
'search_categ'] = $selected_cat;
146 $tableparams[
'search_societe'] = $search_societe;
147 $tableparams[
'search_zip'] = $search_zip;
148 $tableparams[
'search_town'] = $search_town;
149 $tableparams[
'search_country'] = $search_country;
150 $tableparams[
'subcat'] = ($subcat ===
true) ?
'yes' :
'';
153 $allparams = array_merge($commonparams, $headerparams, $tableparams);
154 $headerparams = array_merge($commonparams, $headerparams);
155 $tableparams = array_merge($commonparams, $tableparams);
157 foreach ($allparams as $key => $value) {
158 $paramslink .=
'&'.$key.
'='.$value;
168 $form =
new Form($db);
169 $thirdparty_static =
new Societe($db);
173 if ($modecompta ==
"BOOKKEEPING") $modecompta =
"CREANCES-DETTES";
174 if ($modecompta ==
"BOOKKEEPINGCOLLECTED") $modecompta =
"RECETTES-DEPENSES";
177 if ($modecompta ==
"CREANCES-DETTES")
179 $name = $langs->trans(
"Turnover").
', '.$langs->trans(
"ByThirdParties");
180 $calcmode = $langs->trans(
"CalcModeDebt");
182 $description = $langs->trans(
"RulesCADue");
183 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description .= $langs->trans(
"DepositsAreNotIncluded");
184 else $description .= $langs->trans(
"DepositsAreIncluded");
187 } elseif ($modecompta ==
"RECETTES-DEPENSES")
189 $name = $langs->trans(
"TurnoverCollected").
', '.$langs->trans(
"ByThirdParties");
190 $calcmode = $langs->trans(
"CalcModeEngagement");
192 $description = $langs->trans(
"RulesCAIn");
193 $description .= $langs->trans(
"DepositsAreIncluded");
196 } elseif ($modecompta ==
"BOOKKEEPING")
198 } elseif ($modecompta ==
"BOOKKEEPINGCOLLECTED")
201 $period = $form->selectDate($date_start,
'date_start', 0, 0, 0,
'', 1, 0).
' - '.$form->selectDate($date_end,
'date_end', 0, 0, 0,
'', 1, 0);
202 if ($date_end ==
dol_time_plus_duree($date_start, 1,
'y') - 1) $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>';
203 else $periodlink =
'';
205 report_header($name, $namelink, $period, $periodlink, $description, $builddate, $exportlink, $tableparams, $calcmode);
207 if (!empty($conf->accounting->enabled) && $modecompta !=
'BOOKKEEPING')
217 if ($modecompta ==
'CREANCES-DETTES') {
218 $sql =
"SELECT DISTINCT s.rowid as socid, s.nom as name, s.zip, s.town, s.fk_pays,";
219 $sql .=
" sum(f.total) as amount, sum(f.total_ttc) as amount_ttc";
220 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f, ".MAIN_DB_PREFIX.
"societe as s";
221 if ($selected_cat === -2)
223 $sql .=
" LEFT OUTER JOIN ".MAIN_DB_PREFIX.
"categorie_societe as cs ON s.rowid = cs.fk_soc";
224 } elseif ($selected_cat)
226 $sql .=
", ".MAIN_DB_PREFIX.
"categorie as c, ".MAIN_DB_PREFIX.
"categorie_societe as cs";
228 $sql .=
" WHERE f.fk_statut in (1,2)";
229 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
230 $sql .=
" AND f.type IN (0,1,2,5)";
232 $sql .=
" AND f.type IN (0,1,2,3,5)";
234 $sql .=
" AND f.fk_soc = s.rowid";
235 if ($date_start && $date_end) {
236 $sql .=
" AND f.datef >= '".$db->idate($date_start).
"' AND f.datef <= '".$db->idate($date_end).
"'";
238 if ($selected_cat === -2)
240 $sql .=
" AND cs.fk_soc is null";
241 } elseif ($selected_cat) {
242 $sql .=
" AND (c.rowid = ".$db->escape($selected_cat);
243 if ($subcat) $sql .=
" OR c.fk_parent = ".$db->escape($selected_cat);
245 $sql .=
" AND cs.fk_categorie = c.rowid AND cs.fk_soc = s.rowid";
252 $sql =
"SELECT s.rowid as socid, s.nom as name, s.zip, s.town, s.fk_pays, sum(pf.amount) as amount_ttc";
253 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
254 $sql .=
", ".MAIN_DB_PREFIX.
"paiement_facture as pf";
255 $sql .=
", ".MAIN_DB_PREFIX.
"paiement as p";
256 $sql .=
", ".MAIN_DB_PREFIX.
"societe as s";
257 if ($selected_cat === -2)
259 $sql .=
" LEFT OUTER JOIN ".MAIN_DB_PREFIX.
"categorie_societe as cs ON s.rowid = cs.fk_soc";
260 } elseif ($selected_cat)
262 $sql .=
", ".MAIN_DB_PREFIX.
"categorie as c, ".MAIN_DB_PREFIX.
"categorie_societe as cs";
264 $sql .=
" WHERE p.rowid = pf.fk_paiement";
265 $sql .=
" AND pf.fk_facture = f.rowid";
266 $sql .=
" AND f.fk_soc = s.rowid";
267 if ($date_start && $date_end) {
268 $sql .=
" AND p.datep >= '".$db->idate($date_start).
"' AND p.datep <= '".$db->idate($date_end).
"'";
270 if ($selected_cat === -2)
272 $sql .=
" AND cs.fk_soc is null";
273 } elseif ($selected_cat) {
274 $sql .=
" AND (c.rowid = ".$selected_cat;
275 if ($subcat) $sql .=
" OR c.fk_parent = ".$selected_cat;
277 $sql .=
" AND cs.fk_categorie = c.rowid AND cs.fk_soc = s.rowid";
280 if (!empty($search_societe)) $sql .=
natural_search(
's.nom', $search_societe);
281 if (!empty($search_zip)) $sql .=
natural_search(
's.zip', $search_zip);
282 if (!empty($search_town)) $sql .=
natural_search(
's.town', $search_town);
283 if ($search_country > 0) $sql .=
' AND s.fk_pays = '.$search_country.
'';
284 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
285 if ($socid) $sql .=
" AND f.fk_soc = ".$socid;
286 $sql .=
" GROUP BY s.rowid, s.nom, s.zip, s.town, s.fk_pays";
287 $sql .=
" ORDER BY s.rowid";
293 $result = $db->query($sql);
295 $num = $db->num_rows($result);
298 $obj = $db->fetch_object($result);
299 $amount_ht[$obj->socid] = $obj->amount;
300 $amount[$obj->socid] = $obj->amount_ttc;
301 $name[$obj->socid] = $obj->name.
' '.$obj->firstname;
302 $address_zip[$obj->socid] = $obj->zip;
303 $address_town[$obj->socid] = $obj->town;
304 $address_pays[$obj->socid] =
getCountry($obj->fk_pays);
305 $catotal_ht += $obj->amount;
306 $catotal += $obj->amount_ttc;
314 if ($modecompta !=
'CREANCES-DETTES') {
315 $sql =
"SELECT '0' as socid, 'Autres' as name, sum(p.amount) as amount_ttc";
316 $sql .=
" FROM ".MAIN_DB_PREFIX.
"bank as b";
317 $sql .=
", ".MAIN_DB_PREFIX.
"bank_account as ba";
318 $sql .=
", ".MAIN_DB_PREFIX.
"paiement as p";
319 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiement_facture as pf ON p.rowid = pf.fk_paiement";
320 $sql .=
" WHERE pf.rowid IS NULL";
321 $sql .=
" AND p.fk_bank = b.rowid";
322 $sql .=
" AND b.fk_account = ba.rowid";
323 $sql .=
" AND ba.entity IN (".getEntity(
'bank_account').
")";
324 if ($date_start && $date_end) $sql .=
" AND p.datep >= '".$db->idate($date_start).
"' AND p.datep <= '".$db->idate($date_end).
"'";
325 $sql .=
" GROUP BY socid, name";
326 $sql .=
" ORDER BY name";
328 $result = $db->query($sql);
330 $num = $db->num_rows($result);
333 $obj = $db->fetch_object($result);
334 $amount[$obj->rowid] += $obj->amount_ttc;
335 $name[$obj->rowid] = $obj->name;
336 $address_zip[$obj->rowid] = $obj->zip;
337 $address_town[$obj->rowid] = $obj->town;
338 $address_pays[$obj->rowid] =
getCountry($obj->fk_pays);
339 $catotal += $obj->amount_ttc;
350 print '<form method="POST" action="'.$_SERVER[
"PHP_SELF"].
'">';
351 print '<input type="hidden" name="token" value="'.newToken().
'">'.
"\n";
353 foreach ($headerparams as $key => $value)
355 print '<input type="hidden" name="'.$key.
'" value="'.$value.
'">';
360 print '<div class="div-table-responsive">';
361 print '<table class="tagtable liste'.($moreforfilter ?
" listwithfilterbefore" :
"").
'">'.
"\n";
364 print '<tr class="liste_titre">';
366 print $langs->trans(
"Category").
': '.$formother->select_categories(Categorie::TYPE_CUSTOMER, $selected_cat,
'search_categ',
true);
368 print $langs->trans(
"SubCats").
'? ';
369 print
'<input type="checkbox" name="subcat" value="yes"';
374 print
'<td colspan="7" class="right">';
375 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")).
'">';
379 print
'<tr class="liste_titre">';
380 print
'<td class="liste_titre left">';
381 print
'<input class="flat" size="6" type="text" name="search_societe" value="'.$search_societe.
'">';
383 print
'<td class="liste_titre left">';
384 print
'<input class="flat" size="6" type="text" name="search_zip" value="'.$search_zip.
'">';
386 print
'<td class="liste_titre left">';
387 print
'<input class="flat" size="6" type="text" name="search_town" value="'.$search_town.
'">';
389 print
'<td class="liste_titre left">';
390 print $form->select_country($search_country,
'search_country');
393 print
'<td class="liste_titre"> </td>';
394 print
'<td class="liste_titre"> </td>';
395 print
'<td class="liste_titre"> </td>';
396 print
'<td class="liste_titre"> </td>';
400 print
"<tr class='liste_titre'>";
402 $langs->trans(
"Company"),
408 $sortfield, $sortorder
411 $langs->trans(
"Zip"),
417 $sortfield, $sortorder
420 $langs->trans(
"Town"),
426 $sortfield, $sortorder
429 $langs->trans(
"Country"),
435 $sortfield, $sortorder
437 if ($modecompta ==
'CREANCES-DETTES') {
439 $langs->trans(
'AmountHT'),
452 $langs->trans(
"AmountTTC"),
462 $langs->trans(
"Percentage"),
472 $langs->trans(
"OtherStatistics"),
477 'align="center" width="20%"'
482 if (count($amount)) {
483 $arrayforsort = $name;
485 if ($sortfield ==
'nom' && $sortorder ==
'asc') {
487 $arrayforsort = $name;
489 if ($sortfield ==
'nom' && $sortorder ==
'desc') {
491 $arrayforsort = $name;
493 if ($sortfield ==
'amount_ht' && $sortorder ==
'asc') {
495 $arrayforsort = $amount_ht;
497 if ($sortfield ==
'amount_ht' && $sortorder ==
'desc') {
499 $arrayforsort = $amount_ht;
501 if ($sortfield ==
'amount_ttc' && $sortorder ==
'asc') {
503 $arrayforsort = $amount;
505 if ($sortfield ==
'amount_ttc' && $sortorder ==
'desc') {
507 $arrayforsort = $amount;
509 if ($sortfield ==
'zip' && $sortorder ==
'asc') {
511 $arrayforsort = $address_zip;
513 if ($sortfield ==
'zip' && $sortorder ==
'desc') {
514 arsort($address_zip);
515 $arrayforsort = $address_zip;
517 if ($sortfield ==
'town' && $sortorder ==
'asc') {
518 asort($address_town);
519 $arrayforsort = $address_town;
521 if ($sortfield ==
'town' && $sortorder ==
'desc') {
522 arsort($address_town);
523 $arrayforsort = $address_town;
525 if ($sortfield ==
'country' && $sortorder ==
'asc') {
526 asort($address_pays);
527 $arrayforsort = $address_town;
529 if ($sortfield ==
'country' && $sortorder ==
'desc') {
530 arsort($address_pays);
531 $arrayforsort = $address_town;
534 foreach ($arrayforsort as $key=>$value) {
535 print
'<tr class="oddeven">';
538 $fullname = $name[$key];
540 $thirdparty_static->id = $key;
541 $thirdparty_static->name = $fullname;
542 $thirdparty_static->client = 1;
543 $linkname = $thirdparty_static->getNomUrl(1,
'customer');
545 $linkname = $langs->trans(
"PaymentsNotLinkedToInvoice");
547 print
"<td>".$linkname.
"</td>\n";
550 print $address_zip[$key];
554 print $address_town[$key];
558 print $address_pays[$key];
562 print
'<td class="right">';
563 if ($modecompta !=
'CREANCES-DETTES') {
565 print
'<a href="'.DOL_URL_ROOT.
'/compta/paiement/list.php?socid='.$key.
'">';
567 print
'<a href="'.DOL_URL_ROOT.
'/compta/paiement/list.php?socid=-1">';
571 print
'<a href="'.DOL_URL_ROOT.
'/compta/facture/list.php?socid='.$key.
'">';
573 print
'<a href="#">';
575 print
price($amount_ht[$key]);
580 print
'<td class="right">';
581 if ($modecompta !=
'CREANCES-DETTES') {
583 print
'<a href="'.DOL_URL_ROOT.
'/compta/paiement/list.php?socid='.$key.
'">';
585 print
'<a href="'.DOL_URL_ROOT.
'/compta/paiement/list.php?orphelins=1">';
589 print
'<a href="'.DOL_URL_ROOT.
'/compta/facture/list.php?socid='.$key.
'">';
591 print
'<a href="#">';
594 print
price($amount[$key]);
599 print
'<td class="right">'.($catotal > 0 ? round(100 * $amount[$key] / $catotal, 2).
'%' :
' ').
'</td>';
602 print
'<td class="center">';
603 if (!empty($conf->propal->enabled) && $key > 0) {
604 print
' <a href="'.DOL_URL_ROOT.
'/comm/propal/stats/index.php?socid='.$key.
'">'.
img_picto($langs->trans(
"ProposalStats"),
"stats").
'</a> ';
606 if (!empty($conf->commande->enabled) && $key > 0) {
607 print
' <a href="'.DOL_URL_ROOT.
'/commande/stats/index.php?socid='.$key.
'">'.
img_picto($langs->trans(
"OrderStats"),
"stats").
'</a> ';
609 if (!empty($conf->facture->enabled) && $key > 0) {
610 print
' <a href="'.DOL_URL_ROOT.
'/compta/facture/stats/index.php?socid='.$key.
'">'.
img_picto($langs->trans(
"InvoiceStats"),
"stats").
'</a> ';
618 print
'<tr class="liste_total">';
619 print
'<td>'.$langs->trans(
"Total").
'</td>';
620 print
'<td> </td>';
621 print
'<td> </td>';
622 print
'<td> </td>';
623 if ($modecompta !=
'CREANCES-DETTES') {
626 print
'<td class="right">'.price($catotal_ht).
'</td>';
628 print
'<td class="right">'.price($catotal).
'</td>';
629 print
'<td> </td>';
630 print
'<td> </td>';
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
price($amount, $form=0, $outlangs= '', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code= '')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
Class to manage third parties objects (customers, suppliers, prospects...)
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
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.
natural_search($fields, $value, $mode=0, $nofirstand=0)
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
print $_SERVER["PHP_SELF"]
Edit parameters.
img_next($titlealt= 'default', $moreatt= '')
Show next logo.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
print
Draft customers invoices.
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...
dol_time_plus_duree($time, $duration_value, $duration_unit)
Add a delay to a date.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin= '1', $morecss= '', $textfordropdown= '')
Show information for admin users or standard users.
getCountry($searchkey, $withcode= '', $dbtouse=0, $outputlangs= '', $entconv=1, $searchlabel= '')
Return country label, code or id from an id, code or label.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $keepmoretags= '', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...