casoc.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. <?php
  2. /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerke@telenet.be>
  6. * Copyright (C) 2013 Antoine Iauch <aiauch@gpcsolutions.fr>
  7. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/compta/stats/casoc.php
  24. * \brief Page reporting Turnover (CA) by thirdparty
  25. */
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  34. $langs->load("companies");
  35. $langs->load("categories");
  36. $langs->load("bills");
  37. $langs->load("compta");
  38. // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES')
  39. $modecompta = $conf->global->ACCOUNTING_MODE;
  40. if (GETPOST("modecompta")) $modecompta=GETPOST("modecompta");
  41. $sortorder=isset($_GET["sortorder"])?$_GET["sortorder"]:$_POST["sortorder"];
  42. $sortfield=isset($_GET["sortfield"])?$_GET["sortfield"]:$_POST["sortfield"];
  43. if (! $sortorder) $sortorder="asc";
  44. if (! $sortfield) $sortfield="nom";
  45. $socid = GETPOST('socid','int');
  46. // Category
  47. $selected_cat = (int) GETPOST('search_categ', 'int');
  48. $subcat = false;
  49. if (GETPOST('subcat', 'alpha') === 'yes') {
  50. $subcat = true;
  51. }
  52. // Security check
  53. if ($user->societe_id > 0) $socid = $user->societe_id;
  54. if (! empty($conf->comptabilite->enabled)) $result=restrictedArea($user,'compta','','','resultat');
  55. if (! empty($conf->accounting->enabled)) $result=restrictedArea($user,'accounting','','','comptarapport');
  56. // Date range
  57. $year=GETPOST("year");
  58. $month=GETPOST("month");
  59. $search_societe = GETPOST("search_societe");
  60. $search_zip = GETPOST("search_zip");
  61. $search_town = GETPOST("search_town");
  62. $search_country = GETPOST("search_country");
  63. $date_startyear = GETPOST("date_startyear");
  64. $date_startmonth = GETPOST("date_startmonth");
  65. $date_startday = GETPOST("date_startday");
  66. $date_endyear = GETPOST("date_endyear");
  67. $date_endmonth = GETPOST("date_endmonth");
  68. $date_endday = GETPOST("date_endday");
  69. if (empty($year))
  70. {
  71. $year_current = strftime("%Y",dol_now());
  72. $month_current = strftime("%m",dol_now());
  73. $year_start = $year_current;
  74. } else {
  75. $year_current = $year;
  76. $month_current = strftime("%m",dol_now());
  77. $year_start = $year;
  78. }
  79. $date_start=dol_mktime(0,0,0,$_REQUEST["date_startmonth"],$_REQUEST["date_startday"],$_REQUEST["date_startyear"]);
  80. $date_end=dol_mktime(23,59,59,$_REQUEST["date_endmonth"],$_REQUEST["date_endday"],$_REQUEST["date_endyear"]);
  81. // Quarter
  82. if (empty($date_start) || empty($date_end)) // We define date_start and date_end
  83. {
  84. $q=GETPOST("q")?GETPOST("q"):0;
  85. if ($q==0)
  86. {
  87. // We define date_start and date_end
  88. $month_start=GETPOST("month")?GETPOST("month"):($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
  89. $year_end=$year_start;
  90. $month_end=$month_start;
  91. if (! GETPOST("month")) // If month not forced
  92. {
  93. if (! GETPOST('year') && $month_start > $month_current)
  94. {
  95. $year_start--;
  96. $year_end--;
  97. }
  98. $month_end=$month_start-1;
  99. if ($month_end < 1) $month_end=12;
  100. else $year_end++;
  101. }
  102. $date_start=dol_get_first_day($year_start,$month_start,false); $date_end=dol_get_last_day($year_end,$month_end,false);
  103. }
  104. if ($q==1) { $date_start=dol_get_first_day($year_start,1,false); $date_end=dol_get_last_day($year_start,3,false); }
  105. if ($q==2) { $date_start=dol_get_first_day($year_start,4,false); $date_end=dol_get_last_day($year_start,6,false); }
  106. if ($q==3) { $date_start=dol_get_first_day($year_start,7,false); $date_end=dol_get_last_day($year_start,9,false); }
  107. if ($q==4) { $date_start=dol_get_first_day($year_start,10,false); $date_end=dol_get_last_day($year_start,12,false); }
  108. }
  109. else
  110. {
  111. // TODO We define q
  112. }
  113. $commonparams=array();
  114. $commonparams['modecompta']=$modecompta;
  115. $commonparams['sortorder'] = $sortorder;
  116. $commonparams['sortfield'] = $sortfield;
  117. $headerparams = array();
  118. $headerparams['date_startyear'] = $date_startyear;
  119. $headerparams['date_startmonth'] = $date_startmonth;
  120. $headerparams['date_startday'] = $date_startday;
  121. $headerparams['date_endyear'] = $date_endyear;
  122. $headerparams['date_endmonth'] = $date_endmonth;
  123. $headerparams['date_endday'] = $date_endday;
  124. $headerparams['q'] = $q;
  125. $tableparams = array();
  126. $tableparams['search_categ'] = $selected_cat;
  127. $tableparams['search_societe'] = $search_societe;
  128. $tableparams['search_zip'] = $search_zip;
  129. $tableparams['search_town'] = $search_town;
  130. $tableparams['search_country'] = $search_country;
  131. $tableparams['subcat'] = ($subcat === true)?'yes':'';
  132. // Adding common parameters
  133. $allparams = array_merge($commonparams, $headerparams, $tableparams);
  134. $headerparams = array_merge($commonparams, $headerparams);
  135. $tableparams = array_merge($commonparams, $tableparams);
  136. foreach($allparams as $key => $value) {
  137. $paramslink .= '&' . $key . '=' . $value;
  138. }
  139. /*
  140. * View
  141. */
  142. llxHeader();
  143. $form=new Form($db);
  144. $thirdparty_static=new Societe($db);
  145. $formother = new FormOther($db);
  146. // Show report header
  147. if ($modecompta=="CREANCES-DETTES")
  148. {
  149. $name=$langs->trans("SalesTurnover").', '.$langs->trans("ByThirdParties");
  150. $calcmode=$langs->trans("CalcModeDebt");
  151. $calcmode.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&modecompta=RECETTES-DEPENSES">','</a>').')';
  152. $period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',0,0,0,'',1,0,1);
  153. //$periodlink='<a href="'.$_SERVER["PHP_SELF"].'?year='.($year-1).'&modecompta='.$modecompta.'">'.img_previous().'</a> <a href="'.$_SERVER["PHP_SELF"].'?year='.($year+1).'&modecompta='.$modecompta.'">'.img_next().'</a>';
  154. $description=$langs->trans("RulesCADue");
  155. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description.= $langs->trans("DepositsAreNotIncluded");
  156. else $description.= $langs->trans("DepositsAreIncluded");
  157. $builddate=dol_now();
  158. //$exportlink=$langs->trans("NotYetAvailable");
  159. } else {
  160. $name=$langs->trans("SalesTurnover").', '.$langs->trans("ByThirdParties");
  161. $calcmode=$langs->trans("CalcModeEngagement");
  162. $calcmode.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&modecompta=CREANCES-DETTES">','</a>').')';
  163. $period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',0,0,0,'',1,0,1);
  164. //$periodlink='<a href="'.$_SERVER["PHP_SELF"].'?year='.($year-1).'&modecompta='.$modecompta.'">'.img_previous().'</a> <a href="'.$_SERVER["PHP_SELF"].'?year='.($year+1).'&modecompta='.$modecompta.'">'.img_next().'</a>';
  165. $description=$langs->trans("RulesCAIn");
  166. $description.= $langs->trans("DepositsAreIncluded");
  167. $builddate=dol_now();
  168. //$exportlink=$langs->trans("NotYetAvailable");
  169. }
  170. report_header($name,$namelink,$period,$periodlink,$description,$builddate,$exportlink,$tableparams,$calcmode);
  171. if (! empty($conf->accounting->enabled) && $modecompta != 'BOOKKEEPING')
  172. {
  173. print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);
  174. }
  175. $name=array();
  176. // Show Array
  177. $catotal=0;
  178. if ($modecompta == 'CREANCES-DETTES') {
  179. $sql = "SELECT DISTINCT s.rowid as socid, s.nom as name, s.zip, s.town, s.fk_pays,";
  180. $sql.= " sum(f.total) as amount, sum(f.total_ttc) as amount_ttc";
  181. $sql.= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."societe as s";
  182. if ($selected_cat === -2) // Without any category
  183. {
  184. $sql.= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."categorie_societe as cs ON s.rowid = cs.fk_soc";
  185. }
  186. else if ($selected_cat) // Into a specific category
  187. {
  188. $sql.= ", ".MAIN_DB_PREFIX."categorie as c, ".MAIN_DB_PREFIX."categorie_societe as cs";
  189. }
  190. $sql.= " WHERE f.fk_statut in (1,2)";
  191. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  192. $sql.= " AND f.type IN (0,1,2,5)";
  193. } else {
  194. $sql.= " AND f.type IN (0,1,2,3,5)";
  195. }
  196. $sql.= " AND f.fk_soc = s.rowid";
  197. if ($date_start && $date_end) {
  198. $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  199. }
  200. if ($selected_cat === -2) // Without any category
  201. {
  202. $sql.=" AND cs.fk_soc is null";
  203. }
  204. else if ($selected_cat) { // Into a specific category
  205. $sql.= " AND (c.rowid = ".$selected_cat;
  206. if ($subcat) $sql.=" OR c.fk_parent = " . $selected_cat;
  207. $sql.= ")";
  208. $sql.= " AND cs.fk_categorie = c.rowid AND cs.fk_soc = s.rowid";
  209. }
  210. } else {
  211. /*
  212. * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
  213. * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
  214. */
  215. $sql = "SELECT s.rowid as socid, s.nom as name, s.zip, s.town, s.fk_pays, sum(pf.amount) as amount_ttc";
  216. $sql.= " FROM ".MAIN_DB_PREFIX."facture as f";
  217. $sql.= ", ".MAIN_DB_PREFIX."paiement_facture as pf";
  218. $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
  219. $sql.= ", ".MAIN_DB_PREFIX."societe as s";
  220. if ($selected_cat === -2) // Without any category
  221. {
  222. $sql.= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."categorie_societe as cs ON s.rowid = cs.fk_soc";
  223. }
  224. else if ($selected_cat) // Into a specific category
  225. {
  226. $sql.= ", ".MAIN_DB_PREFIX."categorie as c, ".MAIN_DB_PREFIX."categorie_societe as cs";
  227. }
  228. $sql.= " WHERE p.rowid = pf.fk_paiement";
  229. $sql.= " AND pf.fk_facture = f.rowid";
  230. $sql.= " AND f.fk_soc = s.rowid";
  231. if ($date_start && $date_end) {
  232. $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  233. }
  234. if ($selected_cat === -2) // Without any category
  235. {
  236. $sql.=" AND cs.fk_soc is null";
  237. }
  238. else if ($selected_cat) { // Into a specific category
  239. $sql.= " AND (c.rowid = ".$selected_cat;
  240. if ($subcat) $sql.=" OR c.fk_parent = " . $selected_cat;
  241. $sql.= ")";
  242. $sql.= " AND cs.fk_categorie = c.rowid AND cs.fk_soc = s.rowid";
  243. }
  244. }
  245. if(!empty($search_societe)) $sql.= ' AND s.nom LIKE "%'.$search_societe.'%"';
  246. if(!empty($search_zip)) $sql.= ' AND s.zip LIKE "%'.$search_zip.'%"';
  247. if(!empty($search_town)) $sql.= ' AND s.town LIKE "%'.$search_town.'%"';
  248. if($search_country > 0) $sql.= ' AND s.fk_pays = '.$search_country.'';
  249. $sql.= " AND f.entity = ".$conf->entity;
  250. if ($socid) $sql.= " AND f.fk_soc = ".$socid;
  251. $sql.= " GROUP BY s.rowid, s.nom, s.zip, s.town, s.fk_pays";
  252. $sql.= " ORDER BY s.rowid";
  253. //echo $sql;
  254. dol_syslog("casoc", LOG_DEBUG);
  255. $result = $db->query($sql);
  256. if ($result) {
  257. $num = $db->num_rows($result);
  258. $i=0;
  259. while ($i < $num) {
  260. $obj = $db->fetch_object($result);
  261. $amount_ht[$obj->socid] = $obj->amount;
  262. $amount[$obj->socid] = $obj->amount_ttc;
  263. $name[$obj->socid] = $obj->name.' '.$obj->firstname;
  264. $address_zip[$obj->socid] = $obj->zip;
  265. $address_town[$obj->socid] = $obj->town;
  266. $address_pays[$obj->socid] = getCountry($obj->fk_pays);
  267. $catotal_ht+=$obj->amount;
  268. $catotal+=$obj->amount_ttc;
  269. $i++;
  270. }
  271. } else {
  272. dol_print_error($db);
  273. }
  274. // On ajoute les paiements anciennes version, non lies par paiement_facture
  275. if ($modecompta != 'CREANCES-DETTES') {
  276. $sql = "SELECT '0' as socid, 'Autres' as name, sum(p.amount) as amount_ttc";
  277. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  278. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  279. $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
  280. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
  281. $sql.= " WHERE pf.rowid IS NULL";
  282. $sql.= " AND p.fk_bank = b.rowid";
  283. $sql.= " AND b.fk_account = ba.rowid";
  284. $sql.= " AND ba.entity IN (".getEntity('bank_account').")";
  285. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  286. $sql.= " GROUP BY socid, name";
  287. $sql.= " ORDER BY name";
  288. $result = $db->query($sql);
  289. if ($result) {
  290. $num = $db->num_rows($result);
  291. $i=0;
  292. while ($i < $num) {
  293. $obj = $db->fetch_object($result);
  294. $amount[$obj->rowid] += $obj->amount_ttc;
  295. $name[$obj->rowid] = $obj->name;
  296. $address_zip[$obj->rowid] = $obj->zip;
  297. $address_town[$obj->rowid] = $obj->town;
  298. $address_pays[$obj->rowid] = getCountry($obj->fk_pays);
  299. $catotal+=$obj->amount_ttc;
  300. $i++;
  301. }
  302. } else {
  303. dol_print_error($db);
  304. }
  305. }
  306. // Show array
  307. $i = 0;
  308. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  309. // Extra parameters management
  310. foreach($headerparams as $key => $value)
  311. {
  312. print '<input type="hidden" name="'.$key.'" value="'.$value.'">';
  313. }
  314. $moreforfilter='';
  315. print '<div class="div-table-responsive">';
  316. print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
  317. // Category filter
  318. print '<tr class="liste_titre">';
  319. print '<td>';
  320. print $langs->trans("Category") . ': ' . $formother->select_categories(Categorie::TYPE_CUSTOMER, $selected_cat, 'search_categ', true);
  321. print ' ';
  322. print $langs->trans("SubCats") . '? ';
  323. print '<input type="checkbox" name="subcat" value="yes"';
  324. if ($subcat) {
  325. print ' checked';
  326. }
  327. print'></td>';
  328. print '<td colspan="7" align="right">';
  329. 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")).'">';
  330. print '</td>';
  331. print '</tr>';
  332. print '<tr class="liste_titre">';
  333. print '<td class="liste_titre" align="left">';
  334. print '<input class="flat" size="6" type="text" name="search_societe" value="'.$search_societe.'">';
  335. print '</td>';
  336. print '<td class="liste_titre" align="left">';
  337. print '<input class="flat" size="6" type="text" name="search_zip" value="'.$search_zip.'">';
  338. print '</td>';
  339. print '<td class="liste_titre" align="left">';
  340. print '<input class="flat" size="6" type="text" name="search_town" value="'.$search_town.'">';
  341. print '</td>';
  342. print '<td class="liste_titre" align="left">';
  343. print $form->select_country($search_country, 'search_country');
  344. //print '<input class="flat" size="6" type="text" name="search_country" value="'.$search_country.'">';
  345. print '</td>';
  346. print '<td class="liste_titre">&nbsp;</td>';
  347. print '<td class="liste_titre">&nbsp;</td>';
  348. print '<td class="liste_titre">&nbsp;</td>';
  349. print '<td class="liste_titre">&nbsp;</td>';
  350. print '</tr>';
  351. // Array titles
  352. print "<tr class='liste_titre'>";
  353. print_liste_field_titre(
  354. $langs->trans("Company"),
  355. $_SERVER["PHP_SELF"],
  356. "nom",
  357. "",
  358. $paramslink,
  359. "",
  360. $sortfield,$sortorder
  361. );
  362. print_liste_field_titre(
  363. $langs->trans("Zip"),
  364. $_SERVER["PHP_SELF"],
  365. "zip",
  366. "",
  367. $paramslink,
  368. "",
  369. $sortfield,$sortorder
  370. );
  371. print_liste_field_titre(
  372. $langs->trans("Town"),
  373. $_SERVER["PHP_SELF"],
  374. "town",
  375. "",
  376. $paramslink,
  377. "",
  378. $sortfield,$sortorder
  379. );
  380. print_liste_field_titre(
  381. $langs->trans("Country"),
  382. $_SERVER["PHP_SELF"],
  383. "country",
  384. "",
  385. $paramslink,
  386. "",
  387. $sortfield,$sortorder
  388. );
  389. if ($modecompta == 'CREANCES-DETTES') {
  390. print_liste_field_titre(
  391. $langs->trans('AmountHT'),
  392. $_SERVER["PHP_SELF"],
  393. "amount_ht",
  394. "",
  395. $paramslink,
  396. 'align="right"',
  397. $sortfield,
  398. $sortorder
  399. );
  400. } else {
  401. print_liste_field_titre('');
  402. }
  403. print_liste_field_titre(
  404. $langs->trans("AmountTTC"),
  405. $_SERVER["PHP_SELF"],
  406. "amount_ttc",
  407. "",
  408. $paramslink,
  409. 'align="right"',
  410. $sortfield,
  411. $sortorder
  412. );
  413. print_liste_field_titre(
  414. $langs->trans("Percentage"),
  415. $_SERVER["PHP_SELF"],
  416. "amount_ttc",
  417. "",
  418. $paramslink,
  419. 'align="right"',
  420. $sortfield,
  421. $sortorder
  422. );
  423. print_liste_field_titre(
  424. $langs->trans("OtherStatistics"),
  425. $_SERVER["PHP_SELF"],
  426. "",
  427. "",
  428. "",
  429. 'align="center" width="20%"'
  430. );
  431. print "</tr>\n";
  432. if (count($amount)) {
  433. $arrayforsort=$name;
  434. // Defining array arrayforsort
  435. if ($sortfield == 'nom' && $sortorder == 'asc') {
  436. asort($name);
  437. $arrayforsort=$name;
  438. }
  439. if ($sortfield == 'nom' && $sortorder == 'desc') {
  440. arsort($name);
  441. $arrayforsort=$name;
  442. }
  443. if ($sortfield == 'amount_ht' && $sortorder == 'asc') {
  444. asort($amount_ht);
  445. $arrayforsort=$amount_ht;
  446. }
  447. if ($sortfield == 'amount_ht' && $sortorder == 'desc') {
  448. arsort($amount_ht);
  449. $arrayforsort=$amount_ht;
  450. }
  451. if ($sortfield == 'amount_ttc' && $sortorder == 'asc') {
  452. asort($amount);
  453. $arrayforsort=$amount;
  454. }
  455. if ($sortfield == 'amount_ttc' && $sortorder == 'desc') {
  456. arsort($amount);
  457. $arrayforsort=$amount;
  458. }
  459. if ($sortfield == 'zip' && $sortorder == 'asc') {
  460. asort($address_zip);
  461. $arrayforsort=$address_zip;
  462. }
  463. if ($sortfield == 'zip' && $sortorder == 'desc') {
  464. arsort($address_zip);
  465. $arrayforsort=$address_zip;
  466. }
  467. if ($sortfield == 'town' && $sortorder == 'asc') {
  468. asort($address_town);
  469. $arrayforsort=$address_town;
  470. }
  471. if ($sortfield == 'town' && $sortorder == 'desc') {
  472. arsort($address_town);
  473. $arrayforsort=$address_town;
  474. }
  475. if ($sortfield == 'country' && $sortorder == 'asc') {
  476. asort($address_pays);
  477. $arrayforsort=$address_town;
  478. }
  479. if ($sortfield == 'country' && $sortorder == 'desc') {
  480. arsort($address_pays);
  481. $arrayforsort=$address_town;
  482. }
  483. foreach($arrayforsort as $key=>$value) {
  484. print '<tr class="oddeven">';
  485. // Third party
  486. $fullname=$name[$key];
  487. if ($key > 0) {
  488. $thirdparty_static->id=$key;
  489. $thirdparty_static->name=$fullname;
  490. $thirdparty_static->client=1;
  491. $linkname=$thirdparty_static->getNomUrl(1,'customer');
  492. } else {
  493. $linkname=$langs->trans("PaymentsNotLinkedToInvoice");
  494. }
  495. print "<td>".$linkname."</td>\n";
  496. print '<td>';
  497. print $address_zip[$key];
  498. print '</td>';
  499. print '<td>';
  500. print $address_town[$key];
  501. print '</td>';
  502. print '<td>';
  503. print $address_pays[$key];
  504. print '</td>';
  505. // Amount w/o VAT
  506. print '<td align="right">';
  507. if ($modecompta != 'CREANCES-DETTES') {
  508. if ($key > 0) {
  509. print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?socid='.$key.'">';
  510. } else {
  511. print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?socid=-1">';
  512. }
  513. } else {
  514. if ($key > 0) {
  515. print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?socid='.$key.'">';
  516. } else {
  517. print '<a href="#">';
  518. }
  519. print price($amount_ht[$key]);
  520. }
  521. print '</td>';
  522. // Amount with VAT
  523. print '<td align="right">';
  524. if ($modecompta != 'CREANCES-DETTES') {
  525. if ($key > 0) {
  526. print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?socid='.$key.'">';
  527. } else {
  528. print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?orphelins=1">';
  529. }
  530. } else {
  531. if ($key > 0) {
  532. print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?socid='.$key.'">';
  533. } else {
  534. print '<a href="#">';
  535. }
  536. }
  537. print price($amount[$key]);
  538. print '</a>';
  539. print '</td>';
  540. // Percent;
  541. print '<td align="right">'.($catotal > 0 ? round(100 * $amount[$key] / $catotal, 2).'%' : '&nbsp;').'</td>';
  542. // Other stats
  543. print '<td align="center">';
  544. if (! empty($conf->propal->enabled) && $key>0) {
  545. print '&nbsp;<a href="'.DOL_URL_ROOT.'/comm/propal/stats/index.php?socid='.$key.'">'.img_picto($langs->trans("ProposalStats"),"stats").'</a>&nbsp;';
  546. }
  547. if (! empty($conf->commande->enabled) && $key>0) {
  548. print '&nbsp;<a href="'.DOL_URL_ROOT.'/commande/stats/index.php?socid='.$key.'">'.img_picto($langs->trans("OrderStats"),"stats").'</a>&nbsp;';
  549. }
  550. if (! empty($conf->facture->enabled) && $key>0) {
  551. print '&nbsp;<a href="'.DOL_URL_ROOT.'/compta/facture/stats/index.php?socid='.$key.'">'.img_picto($langs->trans("InvoiceStats"),"stats").'</a>&nbsp;';
  552. }
  553. print '</td>';
  554. print "</tr>\n";
  555. $i++;
  556. }
  557. // Total
  558. print '<tr class="liste_total">';
  559. print '<td>'.$langs->trans("Total").'</td>';
  560. print '<td>&nbsp;</td>';
  561. print '<td>&nbsp;</td>';
  562. print '<td>&nbsp;</td>';
  563. if ($modecompta != 'CREANCES-DETTES') {
  564. print '<td colspan="1"></td>';
  565. } else {
  566. print '<td align="right">'.price($catotal_ht).'</td>';
  567. }
  568. print '<td align="right">'.price($catotal).'</td>';
  569. print '<td>&nbsp;</td>';
  570. print '<td>&nbsp;</td>';
  571. print '</tr>';
  572. $db->free($result);
  573. }
  574. print "</table>";
  575. print "</div>";
  576. print '</form>';
  577. llxFooter();
  578. $db->close();