customreports.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. <?php
  2. /* Copyright (C) 2020 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. *
  17. * Note: This tool can be included into a list page with :
  18. * define('USE_CUSTOM_REPORT_AS_INCLUDE', 1);
  19. * include DOL_DOCUMENT_ROOT.'/core/customreports.php';
  20. */
  21. /**
  22. * \file htdocs/core/customreports.php
  23. * \ingroup core
  24. * \brief Page to make custom reports
  25. */
  26. if (!defined('USE_CUSTOM_REPORT_AS_INCLUDE')) {
  27. require '../main.inc.php';
  28. // Get parameters
  29. $action = GETPOST('action', 'aZ09') ?GETPOST('action', 'aZ09') : 'view'; // The action 'add', 'create', 'edit', 'update', 'view', ...
  30. $massaction = GETPOST('massaction', 'alpha'); // The bulk action (combo box choice into lists)
  31. $mode = GETPOST('mode', 'alpha') ? GETPOST('mode', 'alpha') : 'graph';
  32. $objecttype = GETPOST('objecttype', 'aZ09');
  33. $tabfamily = GETPOST('tabfamily', 'aZ09');
  34. if (empty($objecttype)) {
  35. $objecttype = 'thirdparty';
  36. }
  37. $search_measures = GETPOST('search_measures', 'array');
  38. //$search_xaxis = GETPOST('search_xaxis', 'array');
  39. if (GETPOST('search_xaxis', 'alpha') && GETPOST('search_xaxis', 'alpha') != '-1') {
  40. $search_xaxis = array(GETPOST('search_xaxis', 'alpha'));
  41. } else {
  42. $search_xaxis = array();
  43. }
  44. //$search_groupby = GETPOST('search_groupby', 'array');
  45. if (GETPOST('search_groupby', 'alpha') && GETPOST('search_groupby', 'alpha') != '-1') {
  46. $search_groupby = array(GETPOST('search_groupby', 'alpha'));
  47. } else {
  48. $search_groupby = array();
  49. }
  50. $search_yaxis = GETPOST('search_yaxis', 'array');
  51. $search_graph = GETPOST('search_graph', 'restricthtml');
  52. // Load variable for pagination
  53. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  54. $sortfield = GETPOST('sortfield', 'aZ09comma');
  55. $sortorder = GETPOST('sortorder', 'aZ09comma');
  56. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  57. if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
  58. $page = 0;
  59. } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
  60. $offset = $limit * $page;
  61. $pageprev = $page - 1;
  62. $pagenext = $page + 1;
  63. $diroutputmassaction = $conf->user->dir_temp.'/'.$user->id.'/customreport';
  64. }
  65. require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
  66. require_once DOL_DOCUMENT_ROOT."/core/lib/company.lib.php";
  67. require_once DOL_DOCUMENT_ROOT."/core/class/dolgraph.class.php";
  68. require_once DOL_DOCUMENT_ROOT."/core/class/doleditor.class.php";
  69. require_once DOL_DOCUMENT_ROOT."/core/class/html.formother.class.php";
  70. // Load traductions files requiredby by page
  71. $langs->loadLangs(array("companies", "other", "exports", "sendings"));
  72. $extrafields = new ExtraFields($db);
  73. $hookmanager->initHooks(array('customreport')); // Note that conf->hooks_modules contains array
  74. $title = '';
  75. $picto = '';
  76. $head = array();
  77. $object = null;
  78. $ObjectClassName = '';
  79. // Objects available by default
  80. $arrayoftype = array(
  81. 'thirdparty' => array('label' => 'ThirdParties', 'ObjectClassName' => 'Societe', 'enabled' => isModEnabled('societe'), 'ClassPath' => "/societe/class/societe.class.php"),
  82. 'contact' => array('label' => 'Contacts', 'ObjectClassName' => 'Contact', 'enabled' => isModEnabled('societ'), 'ClassPath' => "/contact/class/contact.class.php"),
  83. 'proposal' => array('label' => 'Proposals', 'ObjectClassName' => 'Propal', 'enabled' => isModEnabled('propal'), 'ClassPath' => "/comm/propal/class/propal.class.php"),
  84. 'order' => array('label' => 'Orders', 'ObjectClassName' => 'Commande', 'enabled' => isModEnabled('commande'), 'ClassPath' => "/commande/class/commande.class.php"),
  85. 'invoice' => array('label' => 'Invoices', 'ObjectClassName' => 'Facture', 'enabled' => isModEnabled('facture'), 'ClassPath' => "/compta/facture/class/facture.class.php"),
  86. 'invoice_template'=>array('label' => 'PredefinedInvoices', 'ObjectClassName' => 'FactureRec', 'enabled' => isModEnabled('facture'), 'ClassPath' => "/compta/class/facturerec.class.php", 'langs'=>'bills'),
  87. 'contract' => array('label' => 'Contracts', 'ObjectClassName' => 'Contrat', 'enabled' => isModEnabled('contrat'), 'ClassPath' => "/contrat/class/contrat.class.php", 'langs'=>'contracts'),
  88. 'contractdet' => array('label' => 'ContractLines', 'ObjectClassName' => 'ContratLigne', 'enabled' => isModEnabled('contrat'), 'ClassPath' => "/contrat/class/contrat.class.php", 'langs'=>'contracts'),
  89. 'bom' => array('label' => 'BOM', 'ObjectClassName' => 'Bom', 'enabled' => isModEnabled('bom')),
  90. 'mo' => array('label' => 'MO', 'ObjectClassName' => 'Mo', 'enabled' => isModEnabled('mrp'), 'ClassPath' => "/mrp/class/mo.class.php"),
  91. 'ticket' => array('label' => 'Ticket', 'ObjectClassName' => 'Ticket', 'enabled' => isModEnabled('ticket')),
  92. 'member' => array('label' => 'Adherent', 'ObjectClassName' => 'Adherent', 'enabled' => isModEnabled('adherent'), 'ClassPath' => "/adherents/class/adherent.class.php", 'langs'=>'members'),
  93. 'cotisation' => array('label' => 'Subscriptions', 'ObjectClassName' => 'Subscription', 'enabled' => isModEnabled('adherent'), 'ClassPath' => "/adherents/class/subscription.class.php", 'langs'=>'members'),
  94. );
  95. // Complete $arrayoftype by external modules
  96. $parameters = array('objecttype'=>$objecttype, 'tabfamily'=>$tabfamily);
  97. $reshook = $hookmanager->executeHooks('loadDataForCustomReports', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  98. if ($reshook < 0) {
  99. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  100. } elseif (is_array($hookmanager->resArray)) {
  101. if (!empty($hookmanager->resArray['title'])) { // Add entries for tabs
  102. $title = $hookmanager->resArray['title'];
  103. }
  104. if (!empty($hookmanager->resArray['picto'])) { // Add entries for tabs
  105. $picto = $hookmanager->resArray['picto'];
  106. }
  107. if (!empty($hookmanager->resArray['head'])) { // Add entries for tabs
  108. $head = array_merge($head, $hookmanager->resArray['head']);
  109. }
  110. if (!empty($hookmanager->resArray['arrayoftype'])) { // Add entries from hook
  111. foreach ($hookmanager->resArray['arrayoftype'] as $key => $val) {
  112. $arrayoftype[$key] = $val;
  113. }
  114. }
  115. }
  116. if ($objecttype) {
  117. try {
  118. if (!empty($arrayoftype[$objecttype]['ClassPath'])) {
  119. dol_include_once($arrayoftype[$objecttype]['ClassPath']);
  120. } else {
  121. dol_include_once("/".$objecttype."/class/".$objecttype.".class.php");
  122. }
  123. $ObjectClassName = $arrayoftype[$objecttype]['ObjectClassName'];
  124. $object = new $ObjectClassName($db);
  125. } catch (Exception $e) {
  126. print 'Failed to load class for type '.$objecttype;
  127. }
  128. }
  129. // Security check
  130. $socid = 0;
  131. if ($user->socid > 0) { // Protection if external user
  132. //$socid = $user->socid;
  133. accessforbidden();
  134. }
  135. // Fetch optionals attributes and labels
  136. $extrafields->fetch_name_optionals_label($object->table_element);
  137. //$extrafields->fetch_name_optionals_label($object->table_element_line);
  138. $search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
  139. $search_component_params = array('');
  140. $search_component_params_hidden = GETPOST('search_component_params_hidden', 'alphanohtml');
  141. // For the case we enter a criteria manually, the search_component_params_input will be defined and must be used in priority
  142. if (GETPOST('search_component_params_input', 'alphanohtml')) {
  143. $search_component_params_hidden = GETPOST('search_component_params_input', 'alphanohtml');
  144. }
  145. $MAXUNIQUEVALFORGROUP = 20;
  146. $MAXMEASURESINBARGRAPH = 20;
  147. $YYYY = substr($langs->trans("Year"), 0, 1).substr($langs->trans("Year"), 0, 1).substr($langs->trans("Year"), 0, 1).substr($langs->trans("Year"), 0, 1);
  148. $MM = substr($langs->trans("Month"), 0, 1).substr($langs->trans("Month"), 0, 1);
  149. $DD = substr($langs->trans("Day"), 0, 1).substr($langs->trans("Day"), 0, 1);
  150. $HH = substr($langs->trans("Hour"), 0, 1).substr($langs->trans("Hour"), 0, 1);
  151. $MI = substr($langs->trans("Minute"), 0, 1).substr($langs->trans("Minute"), 0, 1);
  152. $SS = substr($langs->trans("Second"), 0, 1).substr($langs->trans("Second"), 0, 1);
  153. $arrayofmesures = array();
  154. $arrayofxaxis = array();
  155. $arrayofgroupby = array();
  156. $arrayofyaxis = array();
  157. $arrayofvaluesforgroupby = array();
  158. $features = $object->element;
  159. if (!empty($object->element_for_permission)) {
  160. $features = $object->element_for_permission;
  161. }
  162. restrictedArea($user, $features, 0, '');
  163. $error = 0;
  164. /*
  165. * Actions
  166. */
  167. // None
  168. /*
  169. * View
  170. */
  171. $form = new Form($db);
  172. $formother = new FormOther($db);
  173. if (!defined('USE_CUSTOM_REPORT_AS_INCLUDE')) {
  174. llxHeader('', $langs->transnoentitiesnoconv('CustomReports'), '');
  175. print dol_get_fiche_head($head, 'customreports', $title, -1, $picto);
  176. }
  177. $newarrayoftype = array();
  178. foreach ($arrayoftype as $key => $val) {
  179. if (dol_eval($val['enabled'], 1, 1, '1')) {
  180. $newarrayoftype[$key] = $arrayoftype[$key];
  181. }
  182. if (!empty($val['langs'])) {
  183. $langs->load($val['langs']);
  184. }
  185. }
  186. $count = 0;
  187. $arrayofmesures = fillArrayOfMeasures($object, 't', $langs->trans($newarrayoftype[$objecttype]['label']), $arrayofmesures, 0, $count);
  188. $arrayofmesures = dol_sort_array($arrayofmesures, 'position', 'asc', 0, 0, 1);
  189. $count = 0;
  190. $arrayofxaxis = fillArrayOfXAxis($object, 't', $langs->trans($newarrayoftype[$objecttype]['label']), $arrayofxaxis, 0, $count);
  191. $arrayofxaxis = dol_sort_array($arrayofxaxis, 'position', 'asc', 0, 0, 1);
  192. $count = 0;
  193. $arrayofgroupby = fillArrayOfGroupBy($object, 't', $langs->trans($newarrayoftype[$objecttype]['label']), $arrayofgroupby, 0, $count);
  194. $arrayofgroupby = dol_sort_array($arrayofgroupby, 'position', 'asc', 0, 0, 1);
  195. // Check parameters
  196. if ($action == 'viewgraph') {
  197. if (!count($search_measures)) {
  198. setEventMessages($langs->trans("AtLeastOneMeasureIsRequired"), null, 'warnings');
  199. } elseif ($mode == 'graph' && count($search_xaxis) > 1) {
  200. setEventMessages($langs->trans("OnlyOneFieldForXAxisIsPossible"), null, 'warnings');
  201. $search_xaxis = array(0 => $search_xaxis[0]);
  202. }
  203. if (count($search_groupby) >= 2) {
  204. setEventMessages($langs->trans("ErrorOnlyOneFieldForGroupByIsPossible"), null, 'warnings');
  205. $search_groupby = array(0 => $search_groupby[0]);
  206. }
  207. if (!count($search_xaxis)) {
  208. setEventMessages($langs->trans("AtLeastOneXAxisIsRequired"), null, 'warnings');
  209. } elseif ($mode == 'graph' && $search_graph == 'bars' && count($search_measures) > $MAXMEASURESINBARGRAPH) {
  210. $langs->load("errors");
  211. setEventMessages($langs->trans("GraphInBarsAreLimitedToNMeasures", $MAXMEASURESINBARGRAPH), null, 'warnings');
  212. $search_graph = 'lines';
  213. }
  214. }
  215. // Get all possible values of fields when a 'group by' is set, and save this into $arrayofvaluesforgroupby
  216. // $arrayofvaluesforgroupby will be used to forge lael of each grouped series
  217. if (is_array($search_groupby) && count($search_groupby)) {
  218. foreach ($search_groupby as $gkey => $gval) {
  219. $gvalwithoutprefix = preg_replace('/^[a-z]+\./', '', $gval);
  220. if (preg_match('/\-year$/', $search_groupby[$gkey])) {
  221. $tmpval = preg_replace('/\-year$/', '', $search_groupby[$gkey]);
  222. $fieldtocount .= 'DATE_FORMAT('.$tmpval.", '%Y')";
  223. } elseif (preg_match('/\-month$/', $search_groupby[$gkey])) {
  224. $tmpval = preg_replace('/\-month$/', '', $search_groupby[$gkey]);
  225. $fieldtocount .= 'DATE_FORMAT('.$tmpval.", '%Y-%m')";
  226. } elseif (preg_match('/\-day$/', $search_groupby[$gkey])) {
  227. $tmpval = preg_replace('/\-day$/', '', $search_groupby[$gkey]);
  228. $fieldtocount .= 'DATE_FORMAT('.$tmpval.", '%Y-%m-%d')";
  229. } else {
  230. $fieldtocount = $search_groupby[$gkey];
  231. }
  232. $sql = "SELECT DISTINCT ".$fieldtocount." as val";
  233. if (strpos($fieldtocount, 'te.') === 0) {
  234. $sql .= " FROM ".MAIN_DB_PREFIX.$object->table_element."_extrafields as te";
  235. } else {
  236. $tabletouse = $object->table_element;
  237. $tablealiastouse = 't';
  238. if (!empty($arrayofgroupby[$gval])) {
  239. $tmpval = explode('.', $gval);
  240. $tabletouse = $arrayofgroupby[$gval]['table'];
  241. $tablealiastouse = $tmpval[0];
  242. }
  243. $sql .= " FROM ".MAIN_DB_PREFIX.$tabletouse." as ".$tablealiastouse;
  244. }
  245. // Add the where here
  246. /*
  247. $sqlfilters = GETPOST('search_component_params_hidden', 'alphanohtml');
  248. if ($sqlfilters) {
  249. $errormessage = '';
  250. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  251. }*/
  252. $sql .= " LIMIT ".((int) ($MAXUNIQUEVALFORGROUP + 1));
  253. //print $sql;
  254. $resql = $db->query($sql);
  255. if (!$resql) {
  256. dol_print_error($db);
  257. }
  258. while ($obj = $db->fetch_object($resql)) {
  259. if (is_null($obj->val)) {
  260. $keytouse = '__NULL__';
  261. $valuetranslated = $langs->transnoentitiesnoconv("NotDefined");
  262. } elseif ($obj->val === '') {
  263. $keytouse = '';
  264. $valuetranslated = $langs->transnoentitiesnoconv("Empty");
  265. } else {
  266. $keytouse = (string) $obj->val;
  267. $valuetranslated = $obj->val;
  268. }
  269. $regs = array();
  270. if (!empty($object->fields[$gvalwithoutprefix]['arrayofkeyval'])) {
  271. $valuetranslated = $object->fields[$gvalwithoutprefix]['arrayofkeyval'][$obj->val];
  272. if (is_null($valuetranslated)) {
  273. $valuetranslated = $langs->transnoentitiesnoconv("UndefinedKey");
  274. }
  275. $valuetranslated = $langs->trans($valuetranslated);
  276. } elseif (preg_match('/integer:([^:]+):([^:]+)$/', $object->fields[$gvalwithoutprefix]['type'], $regs)) {
  277. $classname = $regs[1];
  278. $classpath = $regs[2];
  279. dol_include_once($classpath);
  280. if (class_exists($classname)) {
  281. $tmpobject = new $classname($db);
  282. $tmpobject->fetch($obj->val);
  283. foreach ($tmpobject->fields as $fieldkey => $field) {
  284. if ($field['showoncombobox']) {
  285. $valuetranslated = $tmpobject->$fieldkey;
  286. //if ($valuetranslated == '-') $valuetranslated = $langs->transnoentitiesnoconv("Unknown")
  287. break;
  288. }
  289. }
  290. //$valuetranslated = $tmpobject->ref.'eee';
  291. }
  292. }
  293. $arrayofvaluesforgroupby['g_'.$gkey][$keytouse] = $valuetranslated;
  294. }
  295. // Add also the possible NULL value if field is a parent field that is not a strict join
  296. $tmpfield = explode('.', $gval);
  297. if ($tmpfield[0] != 't' || (is_array($object->fields[$tmpfield[1]]) && empty($object->fields[$tmpfield[1]]['notnull']))) {
  298. dol_syslog("The group by field ".$gval." may be null (because field is null or it is a left join), so we add __NULL__ entry in list of possible values");
  299. //var_dump($gval); var_dump($object->fields);
  300. $arrayofvaluesforgroupby['g_'.$gkey]['__NULL__'] = $langs->transnoentitiesnoconv("NotDefined");
  301. }
  302. asort($arrayofvaluesforgroupby['g_'.$gkey]);
  303. // Add a protection/error to refuse the request if number of differentr values for the group by is higher than $MAXUNIQUEVALFORGROUP
  304. if (count($arrayofvaluesforgroupby['g_'.$gkey]) > $MAXUNIQUEVALFORGROUP) {
  305. $langs->load("errors");
  306. if (strpos($fieldtocount, 'te.') === 0) { // This is an extrafield
  307. //if (!empty($extrafields->attributes[$object->table_element]['langfile'][$gvalwithoutprefix])) {
  308. // $langs->load($extrafields->attributes[$object->table_element]['langfile'][$gvalwithoutprefix]);
  309. //}
  310. $keyforlabeloffield = $extrafields->attributes[$object->table_element]['label'][$gvalwithoutprefix];
  311. $labeloffield = $langs->transnoentitiesnoconv($keyforlabeloffield);
  312. } elseif (strpos($fieldtocount, 't__') === 0) { // This is a field of a foreign key
  313. $reg = array();
  314. if (preg_match('/^(.*)\.(.*)/', $gvalwithoutprefix, $reg)) {
  315. $gvalwithoutprefix = preg_replace('/\..*$/', '', $gvalwithoutprefix);
  316. $gvalwithoutprefix = preg_replace('/t__/', '', $gvalwithoutprefix);
  317. $keyforlabeloffield = $object->fields[$gvalwithoutprefix]['label'];
  318. $labeloffield = $langs->transnoentitiesnoconv($keyforlabeloffield).'-'.$reg[2];
  319. } else {
  320. $labeloffield = $langs->transnoentitiesnoconv($keyforlabeloffield);
  321. }
  322. } else { // This is a common field
  323. $reg = array();
  324. if (preg_match('/^(.*)\-(year|month|day)/', $gvalwithoutprefix, $reg)) {
  325. $gvalwithoutprefix = preg_replace('/\-(year|month|day)/', '', $gvalwithoutprefix);
  326. $keyforlabeloffield = $object->fields[$gvalwithoutprefix]['label'];
  327. $labeloffield = $langs->transnoentitiesnoconv($keyforlabeloffield).'-'.$reg[2];
  328. } else {
  329. $keyforlabeloffield = $object->fields[$gvalwithoutprefix]['label'];
  330. $labeloffield = $langs->transnoentitiesnoconv($keyforlabeloffield);
  331. }
  332. }
  333. //var_dump($gkey.' '.$gval.' '.$gvalwithoutprefix.' '.$fieldtocount.' '.$keyforlabeloffield);
  334. //var_dump($object->fields);
  335. setEventMessages($langs->trans("ErrorTooManyDifferentValueForSelectedGroupBy", $MAXUNIQUEVALFORGROUP, $labeloffield), null, 'warnings');
  336. $search_groupby = array();
  337. }
  338. $db->free($resql);
  339. }
  340. }
  341. //var_dump($arrayofvaluesforgroupby);exit;
  342. $tmparray = dol_getdate(dol_now());
  343. $endyear = $tmparray['year'];
  344. $endmonth = $tmparray['mon'];
  345. $datelastday = dol_get_last_day($endyear, $endmonth, 1);
  346. $startyear = $endyear - 2;
  347. $param = '';
  348. print '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
  349. print '<input type="hidden" name="token" value="'.newToken().'">';
  350. print '<input type="hidden" name="action" value="viewgraph">';
  351. print '<input type="hidden" name="tabfamily" value="'.$tabfamily.'">';
  352. $viewmode = '';
  353. $viewmode .= '<div class="divadvancedsearchfield">';
  354. $arrayofgraphs = array('bars' => 'Bars', 'lines' => 'Lines'); // also 'pies'
  355. $viewmode .= '<div class="inline-block opacitymedium"><span class="fas fa-chart-area paddingright" title="'.$langs->trans("Graph").'"></span>'.$langs->trans("Graph").'</div> ';
  356. $viewmode .= $form->selectarray('search_graph', $arrayofgraphs, $search_graph, 0, 0, 0, 'minwidth100', 1);
  357. $viewmode .= '</div>';
  358. $num = 0;
  359. $massactionbutton = '';
  360. $nav = '';
  361. $newcardbutton = '';
  362. $limit = 0;
  363. print_barre_liste('', $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, -1, 'object_action', 0, $nav.'<span class="marginleftonly"></span>'.$newcardbutton, '', $limit, 1, 0, 1, $viewmode);
  364. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  365. // Select object
  366. print '<div class="divadvancedsearchfield center floatnone">';
  367. print '<div class="inline-block"><span class="opacitymedium">'.$langs->trans("StatisticsOn").'</span></div> ';
  368. print $form->selectarray('objecttype', $newarrayoftype, $objecttype, 0, 0, 0, '', 1, 0, 0, '', 'minwidth200', 1);
  369. if (empty($conf->use_javascript_ajax)) {
  370. print '<input type="submit" class="button buttongen button-save nomargintop" name="changeobjecttype" value="'.$langs->trans("Refresh").'">';
  371. } else {
  372. print '<!-- js code to reload page with good object type -->
  373. <script type="text/javascript">
  374. jQuery(document).ready(function() {
  375. jQuery("#objecttype").change(function() {
  376. console.log("Reload for "+jQuery("#objecttype").val());
  377. location.href = "'.$_SERVER["PHP_SELF"].'?objecttype="+jQuery("#objecttype").val()+"'.($tabfamily ? '&tabfamily='.urlencode($tabfamily) : '').(GETPOST('show_search_component_params_hidden', 'int') ? '&show_search_component_params_hidden='.((int) GETPOST('show_search_component_params_hidden', 'int')) : '').'";
  378. });
  379. });
  380. </script>';
  381. }
  382. print '</div><div class="clearboth"></div>';
  383. // Add Filter (you can use param &show_search_component_params_hidden=1 for debug)
  384. print '<div class="divadvancedsearchfield quatrevingtpercent">';
  385. print $form->searchComponent(array($object->element => $object->fields), $search_component_params, array(), $search_component_params_hidden);
  386. print '</div>';
  387. // Add measures into array
  388. $count = 0;
  389. //var_dump($arrayofmesures);
  390. print '<div class="divadvancedsearchfield clearboth">';
  391. print '<div class="inline-block"><span class="fas fa-ruler-combined paddingright pictofixedwidth" title="'.dol_escape_htmltag($langs->trans("Measures")).'"></span><span class="fas fa-caret-left caretleftaxis" title="'.dol_escape_htmltag($langs->trans("Measures")).'"></span></div>';
  392. $simplearrayofmesures = array();
  393. foreach ($arrayofmesures as $key => $val) {
  394. $simplearrayofmesures[$key] = $arrayofmesures[$key]['label'];
  395. }
  396. print $form->multiselectarray('search_measures', $simplearrayofmesures, $search_measures, 0, 0, 'minwidth300', 1, 0, '', '', $langs->trans("Measures")); // Fill the array $arrayofmeasures with possible fields
  397. print '</div>';
  398. // XAxis
  399. $count = 0;
  400. print '<div class="divadvancedsearchfield">';
  401. print '<div class="inline-block"><span class="fas fa-ruler-combined paddingright pictofixedwidth" title="'.dol_escape_htmltag($langs->trans("XAxis")).'"></span><span class="fas fa-caret-down caretdownaxis" title="'.dol_escape_htmltag($langs->trans("XAxis")).'"></span></div>';
  402. //var_dump($arrayofxaxis);
  403. print $formother->selectXAxisField($object, $search_xaxis, $arrayofxaxis, $langs->trans("XAxis"), 'minwidth300 maxwidth400'); // Fill the array $arrayofxaxis with possible fields
  404. print '</div>';
  405. // Group by
  406. $count = 0;
  407. print '<div class="divadvancedsearchfield">';
  408. print '<div class="inline-block opacitymedium"><span class="fas fa-ruler-horizontal paddingright pictofixedwidth" title="'.dol_escape_htmltag($langs->trans("GroupBy")).'"></span></div>';
  409. print $formother->selectGroupByField($object, $search_groupby, $arrayofgroupby, 'minwidth250 maxwidth300', $langs->trans("GroupBy")); // Fill the array $arrayofgroupby with possible fields
  410. print '</div>';
  411. if ($mode == 'grid') {
  412. // YAxis
  413. print '<div class="divadvancedsearchfield">';
  414. foreach ($object->fields as $key => $val) {
  415. if (empty($val['measure']) && (!isset($val['enabled']) || dol_eval($val['enabled'], 1, 1, '1'))) {
  416. if (in_array($key, array('id', 'rowid', 'entity', 'last_main_doc', 'extraparams'))) {
  417. continue;
  418. }
  419. if (preg_match('/^fk_/', $key)) {
  420. continue;
  421. }
  422. if (in_array($val['type'], array('html', 'text'))) {
  423. continue;
  424. }
  425. if (in_array($val['type'], array('timestamp', 'date', 'datetime'))) {
  426. $arrayofyaxis['t.'.$key.'-year'] = array(
  427. 'label' => $langs->trans($val['label']).' ('.$YYYY.')',
  428. 'position' => $val['position'],
  429. 'table' => $object->table_element
  430. );
  431. $arrayofyaxis['t.'.$key.'-month'] = array(
  432. 'label' => $langs->trans($val['label']).' ('.$YYYY.'-'.$MM.')',
  433. 'position' => $val['position'],
  434. 'table' => $object->table_element
  435. );
  436. $arrayofyaxis['t.'.$key.'-day'] = array(
  437. 'label' => $langs->trans($val['label']).' ('.$YYYY.'-'.$MM.'-'.$DD.')',
  438. 'position' => $val['position'],
  439. 'table' => $object->table_element
  440. );
  441. } else {
  442. $arrayofyaxis['t.'.$key] = array(
  443. 'label' => $val['label'],
  444. 'position' => (int) $val['position'],
  445. 'table' => $object->table_element
  446. );
  447. }
  448. }
  449. }
  450. // Add measure from extrafields
  451. if ($object->isextrafieldmanaged) {
  452. foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) {
  453. if (!empty($extrafields->attributes[$object->table_element]['totalizable'][$key]) && (!isset($extrafields->attributes[$object->table_element]['enabled'][$key]) || dol_eval($extrafields->attributes[$object->table_element]['enabled'][$key], 1, 1, '1'))) {
  454. $arrayofyaxis['te.'.$key] = array(
  455. 'label' => $extrafields->attributes[$object->table_element]['label'][$key],
  456. 'position' => (int) $extrafields->attributes[$object->table_element]['pos'][$key],
  457. 'table' => $object->table_element
  458. );
  459. }
  460. }
  461. }
  462. $arrayofyaxis = dol_sort_array($arrayofyaxis, 'position');
  463. $arrayofyaxislabel = array();
  464. foreach ($arrayofyaxis as $key => $val) {
  465. $arrayofyaxislabel[$key] = $val['label'];
  466. }
  467. print '<div class="inline-block opacitymedium"><span class="fas fa-ruler-vertical paddingright" title="'.$langs->trans("YAxis").'"></span>'.$langs->trans("YAxis").'</div> ';
  468. print $form->multiselectarray('search_yaxis', $arrayofyaxislabel, $search_yaxis, 0, 0, 'minwidth100', 1);
  469. print '</div>';
  470. }
  471. if ($mode == 'graph') {
  472. //
  473. }
  474. print '<div class="divadvancedsearchfield">';
  475. print '<input type="submit" class="button buttongen button-save nomargintop" value="'.$langs->trans("Refresh").'">';
  476. print '</div>';
  477. print '</div>';
  478. print '</form>';
  479. // Generate the SQL request
  480. $sql = '';
  481. if (!empty($search_measures) && !empty($search_xaxis)) {
  482. $fieldid = 'rowid';
  483. $sql = "SELECT ";
  484. foreach ($search_xaxis as $key => $val) {
  485. if (preg_match('/\-year$/', $val)) {
  486. $tmpval = preg_replace('/\-year$/', '', $val);
  487. $sql .= "DATE_FORMAT(".$tmpval.", '%Y') as x_".$key.', ';
  488. } elseif (preg_match('/\-month$/', $val)) {
  489. $tmpval = preg_replace('/\-month$/', '', $val);
  490. $sql .= "DATE_FORMAT(".$tmpval.", '%Y-%m') as x_".$key.', ';
  491. } elseif (preg_match('/\-day$/', $val)) {
  492. $tmpval = preg_replace('/\-day$/', '', $val);
  493. $sql .= "DATE_FORMAT(".$tmpval.", '%Y-%m-%d') as x_".$key.', ';
  494. } else {
  495. $sql .= $val." as x_".$key.", ";
  496. }
  497. }
  498. foreach ($search_groupby as $key => $val) {
  499. if (preg_match('/\-year$/', $val)) {
  500. $tmpval = preg_replace('/\-year$/', '', $val);
  501. $sql .= "DATE_FORMAT(".$tmpval.", '%Y') as g_".$key.', ';
  502. } elseif (preg_match('/\-month$/', $val)) {
  503. $tmpval = preg_replace('/\-month$/', '', $val);
  504. $sql .= "DATE_FORMAT(".$tmpval.", '%Y-%m') as g_".$key.', ';
  505. } elseif (preg_match('/\-day$/', $val)) {
  506. $tmpval = preg_replace('/\-day$/', '', $val);
  507. $sql .= "DATE_FORMAT(".$tmpval.", '%Y-%m-%d') as g_".$key.', ';
  508. } else {
  509. $sql .= $val." as g_".$key.", ";
  510. }
  511. }
  512. foreach ($search_measures as $key => $val) {
  513. if ($val == 't.count') {
  514. $sql .= "COUNT(t.".$fieldid.") as y_".$key.', ';
  515. } elseif (preg_match('/\-sum$/', $val)) {
  516. $tmpval = preg_replace('/\-sum$/', '', $val);
  517. $sql .= "SUM(".$db->ifsql($tmpval.' IS NULL', '0', $tmpval).") as y_".$key.", ";
  518. } elseif (preg_match('/\-average$/', $val)) {
  519. $tmpval = preg_replace('/\-average$/', '', $val);
  520. $sql .= "AVG(".$db->ifsql($tmpval.' IS NULL', '0', $tmpval).") as y_".$key.", ";
  521. } elseif (preg_match('/\-min$/', $val)) {
  522. $tmpval = preg_replace('/\-min$/', '', $val);
  523. $sql .= "MIN(".$db->ifsql($tmpval.' IS NULL', '0', $tmpval).") as y_".$key.", ";
  524. } elseif (preg_match('/\-max$/', $val)) {
  525. $tmpval = preg_replace('/\-max$/', '', $val);
  526. $sql .= "MAX(".$db->ifsql($tmpval.' IS NULL', '0', $tmpval).") as y_".$key.", ";
  527. }
  528. }
  529. $sql = preg_replace('/,\s*$/', '', $sql);
  530. $sql .= " FROM ".MAIN_DB_PREFIX.$object->table_element." as t";
  531. // Add measure from extrafields
  532. if ($object->isextrafieldmanaged) {
  533. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$object->table_element."_extrafields as te ON te.fk_object = t.".$fieldid;
  534. }
  535. // Add table for link for multientity
  536. if ($object->ismultientitymanaged) { // 0=No test on entity, 1=Test with field entity, 'field@table'=Test with link by field@table
  537. if ($object->ismultientitymanaged == 1) {
  538. // No table to add here
  539. } else {
  540. $tmparray = explode('@', $object->ismultientitymanaged);
  541. $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$tmparray[1]." as parenttableforentity ON t.".$tmparray[0]." = parenttableforentity.rowid";
  542. $sql .= " AND parenttableforentity.entity IN (".getEntity($tmparray[1]).")";
  543. }
  544. }
  545. $listoftablesalreadyadded = array($object->table_element => $object->table_element);
  546. // Add LEFT JOIN for all parent tables mentionned into the Xaxis
  547. //var_dump($arrayofxaxis); var_dump($search_xaxis);
  548. foreach ($search_xaxis as $key => $val) {
  549. if (!empty($arrayofxaxis[$val])) {
  550. $tmpval = explode('.', $val);
  551. //var_dump($arrayofxaxis[$val]['table']);
  552. if (! in_array($arrayofxaxis[$val]['table'], $listoftablesalreadyadded)) { // We do not add join for main table already added
  553. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$arrayofxaxis[$val]['table']." as ".$db->sanitize($tmpval[0])." ON t.".str_replace('t__', '', $db->sanitize($tmpval[0]))." = ".$db->sanitize($tmpval[0]).".rowid";
  554. $listoftablesalreadyadded[$arrayofxaxis[$val]['table']] = $arrayofxaxis[$val]['table'];
  555. }
  556. } else {
  557. dol_print_error($db, 'Found a key into search_xaxis not found into arrayofxaxis');
  558. }
  559. }
  560. // Add LEFT JOIN for all parent tables mentionned into the Group by
  561. //var_dump($arrayofgroupby); var_dump($search_groupby);
  562. foreach ($search_groupby as $key => $val) {
  563. if (!empty($arrayofgroupby[$val])) {
  564. $tmpval = explode('.', $val);
  565. //var_dump($arrayofxaxis[$val]['table']);
  566. if (! in_array($arrayofgroupby[$val]['table'], $listoftablesalreadyadded)) { // We do not add join for main table already added
  567. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$arrayofgroupby[$val]['table']." as ".$tmpval[0]." ON t.".str_replace('t__', '', $tmpval[0])." = ".$tmpval[0].".rowid";
  568. $listoftablesalreadyadded[$arrayofgroupby[$val]['table']] = $arrayofgroupby[$val]['table'];
  569. }
  570. } else {
  571. dol_print_error($db, 'Found a key into search_groupby not found into arrayofgroupby');
  572. }
  573. }
  574. // Add LEFT JOIN for all parent tables mentionned into the Yaxis
  575. //var_dump($arrayofgroupby); var_dump($search_groupby);
  576. foreach ($search_measures as $key => $val) {
  577. if (!empty($arrayofmesures[$val])) {
  578. $tmpval = explode('.', $val);
  579. //var_dump($arrayofxaxis[$val]['table']);
  580. if (! in_array($arrayofmesures[$val]['table'], $listoftablesalreadyadded)) { // We do not add join for main table already added
  581. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$arrayofmesures[$val]['table']." as ".$tmpval[0]." ON t.".str_replace('t__', '', $tmpval[0])." = ".$tmpval[0].".rowid";
  582. $listoftablesalreadyadded[$arrayofmesures[$val]['table']] = $arrayofmesures[$val]['table'];
  583. }
  584. } else {
  585. dol_print_error($db, 'Found a key into search_measures not found into arrayofmesures');
  586. }
  587. }
  588. $sql .= " WHERE 1 = 1";
  589. if ($object->ismultientitymanaged == 1) { // 0=No test on entity, 1=Test with field entity, 'field@table'=Test with link by field@table
  590. $sql .= " AND t.entity IN (".getEntity($object->element).")";
  591. }
  592. // Add the where here
  593. $sqlfilters = $search_component_params_hidden;
  594. if ($sqlfilters) {
  595. $errormessage = '';
  596. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  597. if ($errormessage) {
  598. print dol_escape_htmltag($errormessage);
  599. }
  600. }
  601. $sql .= " GROUP BY ";
  602. foreach ($search_xaxis as $key => $val) {
  603. if (preg_match('/\-year$/', $val)) {
  604. $tmpval = preg_replace('/\-year$/', '', $val);
  605. $sql .= "DATE_FORMAT(".$tmpval.", '%Y'), ";
  606. } elseif (preg_match('/\-month$/', $val)) {
  607. $tmpval = preg_replace('/\-month$/', '', $val);
  608. $sql .= "DATE_FORMAT(".$tmpval.", '%Y-%m'), ";
  609. } elseif (preg_match('/\-day$/', $val)) {
  610. $tmpval = preg_replace('/\-day$/', '', $val);
  611. $sql .= "DATE_FORMAT(".$tmpval.", '%Y-%m-%d'), ";
  612. } else {
  613. $sql .= $val.", ";
  614. }
  615. }
  616. foreach ($search_groupby as $key => $val) {
  617. if (preg_match('/\-year$/', $val)) {
  618. $tmpval = preg_replace('/\-year$/', '', $val);
  619. $sql .= "DATE_FORMAT(".$tmpval.", '%Y'), ";
  620. } elseif (preg_match('/\-month$/', $val)) {
  621. $tmpval = preg_replace('/\-month$/', '', $val);
  622. $sql .= "DATE_FORMAT(".$tmpval.", '%Y-%m'), ";
  623. } elseif (preg_match('/\-day$/', $val)) {
  624. $tmpval = preg_replace('/\-day$/', '', $val);
  625. $sql .= "DATE_FORMAT(".$tmpval.", '%Y-%m-%d'), ";
  626. } else {
  627. $sql .= $val.', ';
  628. }
  629. }
  630. $sql = preg_replace('/,\s*$/', '', $sql);
  631. $sql .= ' ORDER BY ';
  632. foreach ($search_xaxis as $key => $val) {
  633. if (preg_match('/\-year$/', $val)) {
  634. $tmpval = preg_replace('/\-year$/', '', $val);
  635. $sql .= "DATE_FORMAT(".$tmpval.", '%Y'), ";
  636. } elseif (preg_match('/\-month$/', $val)) {
  637. $tmpval = preg_replace('/\-month$/', '', $val);
  638. $sql .= "DATE_FORMAT(".$tmpval.", '%Y-%m'), ";
  639. } elseif (preg_match('/\-day$/', $val)) {
  640. $tmpval = preg_replace('/\-day$/', '', $val);
  641. $sql .= "DATE_FORMAT(".$tmpval.", '%Y-%m-%d'), ";
  642. } else {
  643. $sql .= $val.', ';
  644. }
  645. }
  646. foreach ($search_groupby as $key => $val) {
  647. if (preg_match('/\-year$/', $val)) {
  648. $tmpval = preg_replace('/\-year$/', '', $val);
  649. $sql .= "DATE_FORMAT(".$tmpval.", '%Y'), ";
  650. } elseif (preg_match('/\-month$/', $val)) {
  651. $tmpval = preg_replace('/\-month$/', '', $val);
  652. $sql .= "DATE_FORMAT(".$tmpval.", '%Y-%m'), ";
  653. } elseif (preg_match('/\-day$/', $val)) {
  654. $tmpval = preg_replace('/\-day$/', '', $val);
  655. $sql .= "DATE_FORMAT(".$tmpval.", '%Y-%m-%d'), ";
  656. } else {
  657. $sql .= $val.', ';
  658. }
  659. }
  660. $sql = preg_replace('/,\s*$/', '', $sql);
  661. }
  662. //print $sql;
  663. $legend = array();
  664. foreach ($search_measures as $key => $val) {
  665. $legend[] = $langs->trans($arrayofmesures[$val]['label']);
  666. }
  667. $useagroupby = (is_array($search_groupby) && count($search_groupby));
  668. //var_dump($useagroupby);
  669. //var_dump($arrayofvaluesforgroupby);
  670. // Execute the SQL request
  671. $totalnbofrecord = 0;
  672. $data = array();
  673. if ($sql) {
  674. $resql = $db->query($sql);
  675. if (!$resql) {
  676. dol_print_error($db);
  677. }
  678. $ifetch = 0;
  679. $xi = 0;
  680. $oldlabeltouse = '';
  681. while ($obj = $db->fetch_object($resql)) {
  682. $ifetch++;
  683. if ($useagroupby) {
  684. $xval = $search_xaxis[0];
  685. $fieldforxkey = 'x_0';
  686. $xlabel = $obj->$fieldforxkey;
  687. $xvalwithoutprefix = preg_replace('/^[a-z]+\./', '', $xval);
  688. // Define $xlabel
  689. if (!empty($object->fields[$xvalwithoutprefix]['arrayofkeyval'])) {
  690. $xlabel = $object->fields[$xvalwithoutprefix]['arrayofkeyval'][$obj->$fieldforxkey];
  691. }
  692. $labeltouse = (($xlabel || $xlabel == '0') ? dol_trunc($xlabel, 20, 'middle') : ($xlabel === '' ? $langs->transnoentitiesnoconv("Empty") : $langs->transnoentitiesnoconv("NotDefined")));
  693. if ($oldlabeltouse && ($labeltouse != $oldlabeltouse)) {
  694. $xi++; // Increase $xi
  695. }
  696. //var_dump($labeltouse.' '.$oldlabeltouse.' '.$xi);
  697. $oldlabeltouse = $labeltouse;
  698. /* Example of value for $arrayofvaluesforgroupby
  699. * array (size=1)
  700. * 'g_0' =>
  701. * array (size=6)
  702. * 0 => string '0' (length=1)
  703. * '' => string 'Empty' (length=5)
  704. * '__NULL__' => string 'Not defined' (length=11)
  705. * 'done' => string 'done' (length=4)
  706. * 'processing' => string 'processing' (length=10)
  707. * 'undeployed' => string 'undeployed' (length=10)
  708. */
  709. foreach ($search_measures as $key => $val) {
  710. $gi = 0;
  711. foreach ($search_groupby as $gkey) {
  712. //var_dump('*** Fetch #'.$ifetch.' for labeltouse='.$labeltouse.' measure number '.$key.' and group g_'.$gi);
  713. //var_dump($arrayofvaluesforgroupby);
  714. foreach ($arrayofvaluesforgroupby['g_'.$gi] as $gvaluepossiblekey => $gvaluepossiblelabel) {
  715. $ykeysuffix = $gvaluepossiblelabel;
  716. $gvalwithoutprefix = preg_replace('/^[a-z]+\./', '', $gval);
  717. $fieldfory = 'y_'.$key;
  718. $fieldforg = 'g_'.$gi;
  719. $fieldforybis = 'y_'.$key.'_'.$ykeysuffix;
  720. //var_dump('gvaluepossiblekey='.$gvaluepossiblekey.' gvaluepossiblelabel='.$gvaluepossiblelabel.' ykeysuffix='.$ykeysuffix.' gval='.$gval.' gvalwithoutsuffix='.$gvalwithoutprefix);
  721. //var_dump('fieldforg='.$fieldforg.' obj->$fieldforg='.$obj->$fieldforg.' fieldfory='.$fieldfory.' obj->$fieldfory='.$obj->$fieldfory.' fieldforybis='.$fieldforybis);
  722. if (!is_array($data[$xi])) {
  723. $data[$xi] = array();
  724. }
  725. if (!array_key_exists('label', $data[$xi])) {
  726. $data[$xi] = array();
  727. $data[$xi]['label'] = $labeltouse;
  728. }
  729. $objfieldforg = $obj->$fieldforg;
  730. if (is_null($objfieldforg)) {
  731. $objfieldforg = '__NULL__';
  732. }
  733. if ($gvaluepossiblekey == '0') { // $gvaluepossiblekey can have type int or string. So we create a special if, used when value is '0'
  734. //var_dump($objfieldforg.' == \'0\' -> '.($objfieldforg == '0'));
  735. if ($objfieldforg == '0') {
  736. // The record we fetch is for this group
  737. $data[$xi][$fieldforybis] = $obj->$fieldfory;
  738. } elseif (!isset($data[$xi][$fieldforybis])) {
  739. // The record we fetch is not for this group
  740. $data[$xi][$fieldforybis] = '0';
  741. }
  742. } else {
  743. //var_dump((string) $objfieldforg.' === '.(string) $gvaluepossiblekey.' -> '.((string) $objfieldforg === (string) $gvaluepossiblekey));
  744. if ((string) $objfieldforg === (string) $gvaluepossiblekey) {
  745. // The record we fetch is for this group
  746. $data[$xi][$fieldforybis] = $obj->$fieldfory;
  747. } elseif (!isset($data[$xi][$fieldforybis])) {
  748. // The record we fetch is not for this group
  749. $data[$xi][$fieldforybis] = '0';
  750. }
  751. }
  752. }
  753. //var_dump($data[$xi]);
  754. $gi++;
  755. }
  756. }
  757. } else { // No group by
  758. $xval = $search_xaxis[0];
  759. $fieldforxkey = 'x_0';
  760. $xlabel = $obj->$fieldforxkey;
  761. $xvalwithoutprefix = preg_replace('/^[a-z]+\./', '', $xval);
  762. // Define $xlabel
  763. if (!empty($object->fields[$xvalwithoutprefix]['arrayofkeyval'])) {
  764. $xlabel = $object->fields[$xvalwithoutprefix]['arrayofkeyval'][$obj->$fieldforxkey];
  765. }
  766. $labeltouse = (($xlabel || $xlabel == '0') ? dol_trunc($xlabel, 20, 'middle') : ($xlabel === '' ? $langs->trans("Empty") : $langs->trans("NotDefined")));
  767. $xarrayforallseries = array('label' => $labeltouse);
  768. foreach ($search_measures as $key => $val) {
  769. $fieldfory = 'y_'.$key;
  770. $xarrayforallseries[$fieldfory] = $obj->$fieldfory;
  771. }
  772. $data[$xi] = $xarrayforallseries;
  773. $xi++;
  774. }
  775. }
  776. $totalnbofrecord = count($data);
  777. }
  778. //var_dump($data);
  779. print '<div class="customreportsoutput'.($totalnbofrecord ? '' : ' customreportsoutputnotdata').'">';
  780. if ($mode == 'grid') {
  781. // TODO
  782. }
  783. if ($mode == 'graph') {
  784. $WIDTH = '80%';
  785. $HEIGHT = 200;
  786. // Show graph
  787. $px1 = new DolGraph();
  788. $mesg = $px1->isGraphKo();
  789. if (!$mesg) {
  790. //var_dump($legend);
  791. //var_dump($data);
  792. $px1->SetData($data);
  793. unset($data);
  794. $arrayoftypes = array();
  795. foreach ($search_measures as $key => $val) {
  796. $arrayoftypes[] = $search_graph;
  797. }
  798. $px1->SetLegend($legend);
  799. $px1->SetMinValue($px1->GetFloorMinValue());
  800. $px1->SetMaxValue($px1->GetCeilMaxValue());
  801. $px1->SetWidth($WIDTH);
  802. $px1->SetHeight($HEIGHT);
  803. $px1->SetYLabel($langs->trans("Y"));
  804. $px1->SetShading(3);
  805. $px1->SetHorizTickIncrement(1);
  806. $px1->SetCssPrefix("cssboxes");
  807. $px1->SetType($arrayoftypes);
  808. $px1->mode = 'depth';
  809. $px1->SetTitle('');
  810. $dir = $conf->user->dir_temp;
  811. dol_mkdir($dir);
  812. $filenamenb = $dir.'/customreport_'.$object->element.'.png';
  813. $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=user&file=customreport_'.$object->element.'.png';
  814. $px1->draw($filenamenb, $fileurlnb);
  815. $texttoshow = $langs->trans("NoRecordFound");
  816. if (!GETPOSTISSET('search_measures') || !GETPOSTISSET('search_xaxis')) {
  817. $texttoshow = $langs->trans("SelectYourGraphOptionsFirst");
  818. }
  819. print $px1->show($totalnbofrecord ? 0 : $texttoshow);
  820. }
  821. }
  822. if ($sql) {
  823. // Show admin info
  824. print '<br>'.info_admin($langs->trans("SQLUsedForExport").':<br> '.$sql, 0, 0, 1, '', 'TechnicalInformation');
  825. }
  826. print '<div>';
  827. if (!defined('USE_CUSTOM_REPORT_AS_INCLUDE')) {
  828. print dol_get_fiche_end();
  829. }
  830. // End of page
  831. llxFooter();
  832. $db->close();
  833. /**
  834. * Fill arrayofmesures for an object
  835. *
  836. * @param mixed $object Any object
  837. * @param string $tablealias Alias of table
  838. * @param string $labelofobject Label of object
  839. * @param array $arrayofmesures Array of mesures already filled
  840. * @param int $level Level
  841. * @param int $count Count
  842. * @return array Array of mesures
  843. */
  844. function fillArrayOfMeasures($object, $tablealias, $labelofobject, &$arrayofmesures, $level = 0, &$count = 0)
  845. {
  846. global $langs, $extrafields, $db;
  847. if ($level > 10) { // Protection against infinite loop
  848. return $arrayofmesures;
  849. }
  850. if ($level == 0) {
  851. // Add the count of record only for the main/first level object. Parents are necessarly unique for each record.
  852. $arrayofmesures[$tablealias.'.count'] = array(
  853. 'label' => img_picto('', $object->picto, 'class="pictofixedwidth"').' '.$labelofobject.': Count',
  854. 'position' => 0,
  855. 'table' => $object->table_element
  856. );
  857. }
  858. // Add main fields of object
  859. foreach ($object->fields as $key => $val) {
  860. if (!empty($val['isameasure']) && (!isset($val['enabled']) || dol_eval($val['enabled'], 1, 1, '1'))) {
  861. $position = (empty($val['position']) ? 0 : intVal($val['position']));
  862. $arrayofmesures[$tablealias.'.'.$key.'-sum'] = array(
  863. 'label' => img_picto('', $object->picto, 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($val['label']).' <span class="opacitymedium">('.$langs->trans("Sum").')</span>',
  864. 'position' => ($position + ($count * 100000)).'.1',
  865. 'table' => $object->table_element
  866. );
  867. $arrayofmesures[$tablealias.'.'.$key.'-average'] = array(
  868. 'label' => img_picto('', $object->picto, 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($val['label']).' <span class="opacitymedium">('.$langs->trans("Average").')</span>',
  869. 'position' => ($position + ($count * 100000)).'.2',
  870. 'table' => $object->table_element
  871. );
  872. $arrayofmesures[$tablealias.'.'.$key.'-min'] = array(
  873. 'label' => img_picto('', $object->picto, 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($val['label']).' <span class="opacitymedium">('.$langs->trans("Minimum").')</span>',
  874. 'position' => ($position + ($count * 100000)).'.3',
  875. 'table' => $object->table_element
  876. );
  877. $arrayofmesures[$tablealias.'.'.$key.'-max'] = array(
  878. 'label' => img_picto('', $object->picto, 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($val['label']).' <span class="opacitymedium">('.$langs->trans("Maximum").')</span>',
  879. 'position' => ($position + ($count * 100000)).'.4',
  880. 'table' => $object->table_element
  881. );
  882. }
  883. }
  884. // Add extrafields to Measures
  885. if (!empty($object->isextrafieldmanaged)) {
  886. foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) {
  887. if (!empty($extrafields->attributes[$object->table_element]['totalizable'][$key]) && (!isset($extrafields->attributes[$object->table_element]['enabled'][$key]) || dol_eval($extrafields->attributes[$object->table_element]['enabled'][$key], 1, 1, '1'))) {
  888. $position = (!empty($val['position']) ? $val['position'] : 0);
  889. $arrayofmesures[$tablealias.'e.'.$key.'-sum'] = array(
  890. 'label' => img_picto('', $object->picto, 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($extrafields->attributes[$object->table_element]['label'][$key]).' <span class="opacitymedium">('.$langs->trans("Sum").')</span>',
  891. 'position' => ($position+($count * 100000)).'.1',
  892. 'table' => $object->table_element
  893. );
  894. $arrayofmesures[$tablealias.'e.'.$key.'-average'] = array(
  895. 'label' => img_picto('', $object->picto, 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($extrafields->attributes[$object->table_element]['label'][$key]).' <span class="opacitymedium">('.$langs->trans("Average").')</span>',
  896. 'position' => ($position+($count * 100000)).'.2',
  897. 'table' => $object->table_element
  898. );
  899. $arrayofmesures[$tablealias.'e.'.$key.'-min'] = array(
  900. 'label' => img_picto('', $object->picto, 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($extrafields->attributes[$object->table_element]['label'][$key]).' <span class="opacitymedium">('.$langs->trans("Minimum").')</span>',
  901. 'position' => ($position+($count * 100000)).'.3',
  902. 'table' => $object->table_element
  903. );
  904. $arrayofmesures[$tablealias.'e.'.$key.'-max'] = array(
  905. 'label' => img_picto('', $object->picto, 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($extrafields->attributes[$object->table_element]['label'][$key]).' <span class="opacitymedium">('.$langs->trans("Maximum").')</span>',
  906. 'position' => ($position+($count * 100000)).'.4',
  907. 'table' => $object->table_element
  908. );
  909. }
  910. }
  911. }
  912. // Add fields for parent objects
  913. foreach ($object->fields as $key => $val) {
  914. if (preg_match('/^[^:]+:[^:]+:/', $val['type'])) {
  915. $tmptype = explode(':', $val['type'], 4);
  916. if ($tmptype[0] == 'integer' && $tmptype[1] && $tmptype[2]) {
  917. $newobject = $tmptype[1];
  918. dol_include_once($tmptype[2]);
  919. if (class_exists($newobject)) {
  920. $tmpobject = new $newobject($db);
  921. //var_dump($key); var_dump($tmpobject->element); var_dump($val['label']); var_dump($tmptype); var_dump('t-'.$key);
  922. $count++;
  923. $arrayofmesures = fillArrayOfMeasures($tmpobject, $tablealias.'__'.$key, $langs->trans($val['label']), $arrayofmesures, $level + 1, $count);
  924. } else {
  925. print 'For property '.$object->element.'->'.$key.', type="'.$val['type'].'": Failed to find class '.$newobject." in file ".$tmptype[2]."<br>\n";
  926. }
  927. }
  928. }
  929. }
  930. return $arrayofmesures;
  931. }
  932. /**
  933. * Fill arrayofmesures for an object
  934. *
  935. * @param mixed $object Any object
  936. * @param string $tablealias Alias of table ('t' for example)
  937. * @param string $labelofobject Label of object
  938. * @param array $arrayofxaxis Array of xaxis already filled
  939. * @param int $level Level
  940. * @param int $count Count
  941. * @return array Array of xaxis
  942. */
  943. function fillArrayOfXAxis($object, $tablealias, $labelofobject, &$arrayofxaxis, $level = 0, &$count = 0)
  944. {
  945. global $langs, $extrafields, $db;
  946. if ($level > 10) { // Protection against infinite loop
  947. return $arrayofxaxis;
  948. }
  949. if ($level >= 2) {
  950. return $arrayofxaxis;
  951. }
  952. $YYYY = substr($langs->trans("Year"), 0, 1).substr($langs->trans("Year"), 0, 1).substr($langs->trans("Year"), 0, 1).substr($langs->trans("Year"), 0, 1);
  953. $MM = substr($langs->trans("Month"), 0, 1).substr($langs->trans("Month"), 0, 1);
  954. $DD = substr($langs->trans("Day"), 0, 1).substr($langs->trans("Day"), 0, 1);
  955. $HH = substr($langs->trans("Hour"), 0, 1).substr($langs->trans("Hour"), 0, 1);
  956. $MI = substr($langs->trans("Minute"), 0, 1).substr($langs->trans("Minute"), 0, 1);
  957. $SS = substr($langs->trans("Second"), 0, 1).substr($langs->trans("Second"), 0, 1);
  958. // Add main fields of object
  959. foreach ($object->fields as $key => $val) {
  960. if (empty($val['measure'])) {
  961. if (in_array($key, array(
  962. 'id', 'ref_ext', 'rowid', 'entity', 'last_main_doc', 'logo', 'logo_squarred', 'extraparams',
  963. 'parent', 'photo', 'socialnetworks', 'webservices_url', 'webservices_key'))) {
  964. continue;
  965. }
  966. if (isset($val['enabled']) && !dol_eval($val['enabled'], 1, 1, '1')) {
  967. continue;
  968. }
  969. if (isset($val['visible']) && !dol_eval($val['visible'], 1, 1, '1')) {
  970. continue;
  971. }
  972. if (preg_match('/^fk_/', $key) && !preg_match('/^fk_statu/', $key)) {
  973. continue;
  974. }
  975. if (preg_match('/^pass/', $key)) {
  976. continue;
  977. }
  978. if (in_array($val['type'], array('html', 'text'))) {
  979. continue;
  980. }
  981. if (in_array($val['type'], array('timestamp', 'date', 'datetime'))) {
  982. $position = (empty($val['position']) ? 0 : intVal($val['position']));
  983. $arrayofxaxis[$tablealias.'.'.$key.'-year'] = array(
  984. 'label' => img_picto('', $object->picto, 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($val['label']).' <span class="opacitymedium">('.$YYYY.')</span>',
  985. 'position' => ($position + ($count * 100000)).'.1',
  986. 'table' => $object->table_element
  987. );
  988. $arrayofxaxis[$tablealias.'.'.$key.'-month'] = array(
  989. 'label' => img_picto('', $object->picto, 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($val['label']).' <span class="opacitymedium">('.$YYYY.'-'.$MM.')</span>',
  990. 'position' => ($position + ($count * 100000)).'.2',
  991. 'table' => $object->table_element
  992. );
  993. $arrayofxaxis[$tablealias.'.'.$key.'-day'] = array(
  994. 'label' => img_picto('', $object->picto, 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($val['label']).' <span class="opacitymedium">('.$YYYY.'-'.$MM.'-'.$DD.')</span>',
  995. 'position' => ($position + ($count * 100000)).'.3',
  996. 'table' => $object->table_element
  997. );
  998. } else {
  999. $position = (empty($val['position']) ? 0 : intVal($val['position']));
  1000. $arrayofxaxis[$tablealias.'.'.$key] = array(
  1001. 'label' => img_picto('', $object->picto, 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($val['label']),
  1002. 'position' => ($position + ($count * 100000)),
  1003. 'table' => $object->table_element
  1004. );
  1005. }
  1006. }
  1007. }
  1008. // Add extrafields to X-Axis
  1009. if (!empty($object->isextrafieldmanaged)) {
  1010. foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) {
  1011. if ($extrafields->attributes[$object->table_element]['type'][$key] == 'separate') {
  1012. continue;
  1013. }
  1014. if (!empty($extrafields->attributes[$object->table_element]['totalizable'][$key])) {
  1015. continue;
  1016. }
  1017. $arrayofxaxis[$tablealias.'e.'.$key] = array(
  1018. 'label' => img_picto('', $object->picto, 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($extrafields->attributes[$object->table_element]['label'][$key]),
  1019. 'position' => 1000 + (int) $extrafields->attributes[$object->table_element]['pos'][$key] + ($count * 100000),
  1020. 'table' => $object->table_element
  1021. );
  1022. }
  1023. }
  1024. // Add fields for parent objects
  1025. foreach ($object->fields as $key => $val) {
  1026. if (preg_match('/^[^:]+:[^:]+:/', $val['type'])) {
  1027. $tmptype = explode(':', $val['type'], 4);
  1028. if ($tmptype[0] == 'integer' && $tmptype[1] && $tmptype[2]) {
  1029. $newobject = $tmptype[1];
  1030. dol_include_once($tmptype[2]);
  1031. if (class_exists($newobject)) {
  1032. $tmpobject = new $newobject($db);
  1033. //var_dump($key); var_dump($tmpobject->element); var_dump($val['label']); var_dump($tmptype); var_dump('t-'.$key);
  1034. $count++;
  1035. $arrayofxaxis = fillArrayOfXAxis($tmpobject, $tablealias.'__'.$key, $langs->trans($val['label']), $arrayofxaxis, $level + 1, $count);
  1036. } else {
  1037. print 'For property '.$object->element.'->'.$key.', type="'.$val['type'].'": Failed to find class '.$newobject." in file ".$tmptype[2]."<br>\n";
  1038. }
  1039. }
  1040. }
  1041. }
  1042. return $arrayofxaxis;
  1043. }
  1044. /**
  1045. * Fill arrayofgrupby for an object
  1046. *
  1047. * @param mixed $object Any object
  1048. * @param string $tablealias Alias of table
  1049. * @param string $labelofobject Label of object
  1050. * @param array $arrayofgroupby Array of groupby already filled
  1051. * @param int $level Level
  1052. * @param int $count Count
  1053. * @return array Array of groupby
  1054. */
  1055. function fillArrayOfGroupBy($object, $tablealias, $labelofobject, &$arrayofgroupby, $level = 0, &$count = 0)
  1056. {
  1057. global $langs, $extrafields, $db;
  1058. if ($level > 10) { // Protection against infinite loop
  1059. return $arrayofgroupby;
  1060. }
  1061. if ($level >= 2) {
  1062. return $arrayofgroupby;
  1063. }
  1064. $YYYY = substr($langs->trans("Year"), 0, 1).substr($langs->trans("Year"), 0, 1).substr($langs->trans("Year"), 0, 1).substr($langs->trans("Year"), 0, 1);
  1065. $MM = substr($langs->trans("Month"), 0, 1).substr($langs->trans("Month"), 0, 1);
  1066. $DD = substr($langs->trans("Day"), 0, 1).substr($langs->trans("Day"), 0, 1);
  1067. $HH = substr($langs->trans("Hour"), 0, 1).substr($langs->trans("Hour"), 0, 1);
  1068. $MI = substr($langs->trans("Minute"), 0, 1).substr($langs->trans("Minute"), 0, 1);
  1069. $SS = substr($langs->trans("Second"), 0, 1).substr($langs->trans("Second"), 0, 1);
  1070. // Add main fields of object
  1071. foreach ($object->fields as $key => $val) {
  1072. if (empty($val['isameasure'])) {
  1073. if (in_array($key, array(
  1074. 'id', 'ref_ext', 'rowid', 'entity', 'last_main_doc', 'logo', 'logo_squarred', 'extraparams',
  1075. 'parent', 'photo', 'socialnetworks', 'webservices_url', 'webservices_key'))) {
  1076. continue;
  1077. }
  1078. if (isset($val['enabled']) && !dol_eval($val['enabled'], 1, 1, '1')) {
  1079. continue;
  1080. }
  1081. if (isset($val['visible']) && !dol_eval($val['visible'], 1, 1, '1')) {
  1082. continue;
  1083. }
  1084. if (preg_match('/^fk_/', $key) && !preg_match('/^fk_statu/', $key)) {
  1085. continue;
  1086. }
  1087. if (preg_match('/^pass/', $key)) {
  1088. continue;
  1089. }
  1090. if (in_array($val['type'], array('html', 'text'))) {
  1091. continue;
  1092. }
  1093. if (in_array($val['type'], array('timestamp', 'date', 'datetime'))) {
  1094. $position = (empty($val['position']) ? 0 : intVal($val['position']));
  1095. $arrayofgroupby[$tablealias.'.'.$key.'-year'] = array(
  1096. 'label' => img_picto('', $object->picto,
  1097. 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($val['label']).' <span class="opacitymedium">('.$YYYY.')</span>', 'position' => ($position + ($count * 100000)).'.1',
  1098. 'table' => $object->table_element
  1099. );
  1100. $arrayofgroupby[$tablealias.'.'.$key.'-month'] = array(
  1101. 'label' => img_picto('', $object->picto,
  1102. 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($val['label']).' <span class="opacitymedium">('.$YYYY.'-'.$MM.')</span>', 'position' => ($position + ($count * 100000)).'.2',
  1103. 'table' => $object->table_element
  1104. );
  1105. $arrayofgroupby[$tablealias.'.'.$key.'-day'] = array(
  1106. 'label' => img_picto('', $object->picto,
  1107. 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($val['label']).' <span class="opacitymedium">('.$YYYY.'-'.$MM.'-'.$DD.')</span>', 'position' => ($position + ($count * 100000)).'.3',
  1108. 'table' => $object->table_element
  1109. );
  1110. } else {
  1111. $position = (empty($val['position']) ? 0 : intVal($val['position']));
  1112. $arrayofgroupby[$tablealias.'.'.$key] = array(
  1113. 'label' => img_picto('', $object->picto,
  1114. 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($val['label']), 'position' => ($position + ($count * 100000)),
  1115. 'table' => $object->table_element
  1116. );
  1117. }
  1118. }
  1119. }
  1120. // Add extrafields to Group by
  1121. if (!empty($object->isextrafieldmanaged)) {
  1122. foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) {
  1123. if ($extrafields->attributes[$object->table_element]['type'][$key] == 'separate') {
  1124. continue;
  1125. }
  1126. if (!empty($extrafields->attributes[$object->table_element]['totalizable'][$key])) {
  1127. continue;
  1128. }
  1129. $arrayofgroupby[$tablealias.'e.'.$key] = array(
  1130. 'label' => img_picto('', $object->picto,
  1131. 'class="pictofixedwidth"').' '.$labelofobject.': '.$langs->trans($extrafields->attributes[$object->table_element]['label'][$key]), 'position' => 1000 + (int) $extrafields->attributes[$object->table_element]['pos'][$key] + ($count * 100000),
  1132. 'table' => $object->table_element
  1133. );
  1134. }
  1135. }
  1136. // Add fields for parent objects
  1137. foreach ($object->fields as $key => $val) {
  1138. if (preg_match('/^[^:]+:[^:]+:/', $val['type'])) {
  1139. $tmptype = explode(':', $val['type'], 4);
  1140. if ($tmptype[0] == 'integer' && $tmptype[1] && $tmptype[2]) {
  1141. $newobject = $tmptype[1];
  1142. dol_include_once($tmptype[2]);
  1143. if (class_exists($newobject)) {
  1144. $tmpobject = new $newobject($db);
  1145. //var_dump($key); var_dump($tmpobject->element); var_dump($val['label']); var_dump($tmptype); var_dump('t-'.$key);
  1146. $count++;
  1147. $arrayofgroupby = fillArrayOfGroupBy($tmpobject, $tablealias.'__'.$key, $langs->trans($val['label']), $arrayofgroupby, $level + 1, $count);
  1148. } else {
  1149. print 'For property '.$object->element.'->'.$key.', type="'.$val['type'].'": Failed to find class '.$newobject." in file ".$tmptype[2]."<br>\n";
  1150. }
  1151. }
  1152. }
  1153. }
  1154. return $arrayofgroupby;
  1155. }