*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/**
* \file htdocs/core/customreports.php
* \ingroup core
* \brief Page to make custom reports
*/
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
require_once DOL_DOCUMENT_ROOT."/core/lib/company.lib.php";
require_once DOL_DOCUMENT_ROOT."/core/class/dolgraph.class.php";
require_once DOL_DOCUMENT_ROOT."/core/class/doleditor.class.php";
// Load traductions files requiredby by page
$langs->loadLangs(array("companies", "bills", "other", "exports"));
// Get parameters
$action = GETPOST('action', 'aZ09') ?GETPOST('action', 'aZ09') : 'view'; // The action 'add', 'create', 'edit', 'update', 'view', ...
$massaction = GETPOST('massaction', 'alpha'); // The bulk action (combo box choice into lists)
$mode = GETPOST('mode', 'alpha') ? GETPOST('mode', 'alpha') : 'graph';
$objecttype = GETPOST('objecttype', 'aZ09');
$tabfamily = GETPOST('tabfamily', 'aZ09');
if (empty($objecttype)) $objecttype = 'thirdparty';
$search_filters = GETPOST('search_filters', 'array');
$search_measures = GETPOST('search_measures', 'array');
$search_xaxis = GETPOST('search_xaxis', 'array');
$search_yaxis = GETPOST('search_yaxis', 'array');
$search_graph = GETPOST('search_graph', 'none');
// Load variable for pagination
$limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
$sortfield = GETPOST('sortfield', 'alpha');
$sortorder = GETPOST('sortorder', 'alpha');
$page = GETPOST('page', 'int');
if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) { $page = 0; } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
$offset = $limit * $page;
$pageprev = $page - 1;
$pagenext = $page + 1;
// Protection if external user
if ($user->societe_id > 0)
{
//accessforbidden();
}
$extrafields = new ExtraFields($db);
$diroutputmassaction = $conf->user->dir_temp.'/'.$user->id.'/customreport';
$hookmanager->initHooks(array('customreport')); // Note that conf->hooks_modules contains array
$title = '';
$picto = '';
$head = array();
$object = null;
$ObjectClassName = '';
// Objects available by default
$arrayoftype = array(
'thirdparty' => array('label' => 'ThirdParties', 'ObjectClassName' => 'Societe', 'enabled' => $conf->societe->enabled, 'ClassPath' => DOL_DOCUMENT_ROOT."/societe/class/societe.class.php"),
'contact' => array('label' => 'Contacts', 'ObjectClassName' => 'Contact', 'enabled' => $conf->societe->enabled, 'ClassPath' => DOL_DOCUMENT_ROOT."/contact/class/contact.class.php"),
'contract' => array('label' => 'Contracts', 'ObjectClassName' => 'Contrat', 'enabled' => $conf->contrat->enabled, 'ClassPath' => DOL_DOCUMENT_ROOT."/contrat/class/contrat.class.php", 'langs'=>'contract'),
'invoice' => array('label' => 'Invoices', 'ObjectClassName' => 'Facture', 'enabled' => $conf->facture->enabled, 'ClassPath' => DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php"),
'invoice_template'=>array('label' => 'PredefinedInvoices', 'ObjectClassName' => 'FactureRec', 'enabled' => $conf->facture->enabled, 'ClassPath' => DOL_DOCUMENT_ROOT."/compta/class/facturerec.class.php"),
'bom' => array('label' => 'BOM', 'ObjectClassName' => 'Bom', 'enabled' => $conf->bom->enabled),
'mo' => array('label' => 'MO', 'ObjectClassName' => 'Mo', 'enabled' => $conf->mo->enabled, 'ClassPath' => DOL_DOCUMENT_ROOT."/mrp/class/mo.class.php"),
'ticket' => array('label' => 'Ticket', 'ObjectClassName' => 'Ticket', 'enabled' => $conf->ticket->enabled),
'member' => array('label' => 'Adherent', 'ObjectClassName' => 'Adherent', 'enabled' => $conf->adherent->enabled, 'ClassPath' => DOL_DOCUMENT_ROOT."/adherents/class/adherent.class.php", 'langs'=>'members'),
'cotisation' => array('label' => 'Subscriptions', 'ObjectClassName' => 'Subscription', 'enabled' => $conf->adherent->enabled, 'ClassPath' => DOL_DOCUMENT_ROOT."/adherents/class/subscription.class.php", 'langs'=>'members'),
);
// Complete $arrayoftype
$parameters = array('objecttype'=>$objecttype, 'tabfamily'=>$tabfamily);
$reshook = $hookmanager->executeHooks('loadDataForCustomReports', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
elseif (is_array($hookmanager->resArray)) {
if (! empty($hookmanager->resArray['title'])) { // Add entries for tabs
$title = $hookmanager->resArray['title'];
}
if (! empty($hookmanager->resArray['picto'])) { // Add entries for tabs
$picto = $hookmanager->resArray['picto'];
}
if (! empty($hookmanager->resArray['head'])) { // Add entries for tabs
$head = array_merge($head, $hookmanager->resArray['head']);
}
if (! empty($hookmanager->resArray['arrayoftype'])) { // Add entries from hook
foreach($hookmanager->resArray['arrayoftype'] as $key => $val) {
$arrayoftype[$key] = $val;
}
}
}
if ($objecttype) {
try {
if ($arrayoftype[$objecttype]['langs']) {
$langs->load($arrayoftype[$objecttype]['langs']);
}
if ($arrayoftype[$objecttype]['ClassPath']) {
include_once $arrayoftype[$objecttype]['ClassPath'];
} else {
include_once DOL_DOCUMENT_ROOT."/".$objecttype."/class/".$objecttype.".class.php";
}
$ObjectClassName = $arrayoftype[$objecttype]['ObjectClassName'];
$object = new $ObjectClassName($db);
}
catch(Exception $e) {
print 'Failed to load class for type '.$objecttype;
}
}
// Security check
$socid = 0;
if ($user->socid > 0) // Protection if external user
{
//$socid = $user->socid;
accessforbidden();
}
$result = restrictedArea($user, $object->element, 0, '');
// Fetch optionals attributes and labels
$extrafields->fetch_name_optionals_label($object->table_element);
//$extrafields->fetch_name_optionals_label($object->table_element_line);
$search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
$search_component_params=array('');
/*
* Actions
*/
/*
* View
*/
$form=new Form($db);
llxHeader('', $langs->transnoentitiesnoconv('CustomReports'), '');
// Check parameters
if ($mode == 'graph' && $search_graph == 'bars' && count($search_measures) > 3) {
setEventMessages($langs->trans("GraphInBarsAreLimitedTo3Measures"), null, 'warnings');
$search_graph = 'lines';
}
if ($mode == 'graph' && count($search_xaxis) > 1) {
setEventMessages($langs->trans("OnlyOneFieldForXAxisIsPossible"), null, 'warnings');
$search_xaxis = array(0 => $search_xaxis[0]);
}
//$head = commande_prepare_head(null);
dol_fiche_head($head, 'customreports', $title, -1, $picto);
$tmparray=dol_getdate(dol_now());
$endyear=$tmparray['year'];
$endmonth=$tmparray['mon'];
$datelastday=dol_get_last_day($endyear, $endmonth, 1);
$startyear=$endyear-2;
$param = '';
$arrayofmesures = array('t.count'=>'Count');
$arrayofxaxis = array();
$arrayofyaxis = array();
print '
';
// Generate the SQL request
$sql = '';
if (! empty($search_measures) && ! empty($search_xaxis))
{
$fieldid = 'rowid';
$sql = 'SELECT ';
foreach($search_xaxis as $key => $val) {
if (preg_match('/\-year$/', $val)) {
$tmpval = preg_replace('/\-year$/', '', $val);
$sql .= 'DATE_FORMAT('.$tmpval.", '%Y') as x_".$key.', ';
} elseif (preg_match('/\-month$/', $val)) {
$tmpval = preg_replace('/\-month$/', '', $val);
$sql .= 'DATE_FORMAT('.$tmpval.", '%Y-%m') as x_".$key.', ';
} elseif (preg_match('/\-day$/', $val)) {
$tmpval = preg_replace('/\-day$/', '', $val);
$sql .= 'DATE_FORMAT('.$tmpval.", '%Y-%m-%d') as x_".$key.', ';
}
else $sql .= $val.' as x_'.$key.', ';
}
foreach($search_measures as $key => $val) {
if ($val == 't.count') $sql .= 'COUNT(t.'.$fieldid.') as y_'.$key.', ';
elseif (preg_match('/\-sum$/', $val)) {
$tmpval = preg_replace('/\-sum$/', '', $val);
$sql .= 'SUM('.$db->ifsql($tmpval.' IS NULL', '0', $tmpval).') as y_'.$key.', ';
}
elseif (preg_match('/\-average$/', $val)) {
$tmpval = preg_replace('/\-average$/', '', $val);
$sql .= 'AVG('.$db->ifsql($tmpval.' IS NULL', '0', $tmpval).') as y_'.$key.', ';
}
elseif (preg_match('/\-min$/', $val)) {
$tmpval = preg_replace('/\-min$/', '', $val);
$sql .= 'MIN('.$db->ifsql($tmpval.' IS NULL', '0', $tmpval).') as y_'.$key.', ';
}
elseif (preg_match('/\-max$/', $val)) {
$tmpval = preg_replace('/\-max$/', '', $val);
$sql .= 'MAX('.$db->ifsql($tmpval.' IS NULL', '0', $tmpval).') as y_'.$key.', ';
}
}
$sql = preg_replace('/,\s*$/', '', $sql);
$sql .= ' FROM '.MAIN_DB_PREFIX.$object->table_element.' as t';
// Add measure from extrafields
if ($object->isextrafieldmanaged) {
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.$object->table_element.'_extrafields as te ON te.fk_object = t.'.$fieldid;
}
if ($object->ismultientitymanaged) {
if ($object->ismultientitymanaged == 1) {
// Nothing here
} else {
$tmparray = explode('@', $object->ismultientitymanaged);
$sql .= ' INNER JOIN '.MAIN_DB_PREFIX.$tmparray[1].' as parenttable ON t.'.$tmparray[0].' = parenttable.rowid';
$sql .= ' AND parenttable.entity IN ('.getEntity($tmparray[1]).')';
}
}
$sql .= ' WHERE 1 = 1';
if ($object->ismultientitymanaged == 1) {
$sql .= ' AND entity IN ('.getEntity($object->element).')';
}
foreach($search_filters as $key => $val) {
// TODO
}
$sql .= ' GROUP BY ';
foreach($search_xaxis as $key => $val) {
if (preg_match('/\-year$/', $val)) {
$tmpval = preg_replace('/\-year$/', '', $val);
$sql .= 'DATE_FORMAT('.$tmpval.", '%Y'), ";
} elseif (preg_match('/\-month$/', $val)) {
$tmpval = preg_replace('/\-month$/', '', $val);
$sql .= 'DATE_FORMAT('.$tmpval.", '%Y-%m'), ";
} elseif (preg_match('/\-day$/', $val)) {
$tmpval = preg_replace('/\-day$/', '', $val);
$sql .= 'DATE_FORMAT('.$tmpval.", '%Y-%m-%d'), ";
}
else $sql .= $val.', ';
}
$sql = preg_replace('/,\s*$/', '', $sql);
$sql .= ' ORDER BY ';
foreach($search_xaxis as $key => $val) {
if (preg_match('/\-year$/', $val)) {
$tmpval = preg_replace('/\-year$/', '', $val);
$sql .= 'DATE_FORMAT('.$tmpval.", '%Y'), ";
} elseif (preg_match('/\-month$/', $val)) {
$tmpval = preg_replace('/\-month$/', '', $val);
$sql .= 'DATE_FORMAT('.$tmpval.", '%Y-%m'), ";
} elseif (preg_match('/\-day$/', $val)) {
$tmpval = preg_replace('/\-day$/', '', $val);
$sql .= 'DATE_FORMAT('.$tmpval.", '%Y-%m-%d'), ";
}
else $sql .= $val.', ';
}
$sql = preg_replace('/,\s*$/', '', $sql);
}
$legend=array();
foreach($search_measures as $key => $val) {
$legend[] = $langs->trans($arrayofmesures[$val]);
}
// Execute the SQL request
$totalnbofrecord = 0;
$data = array();
if ($sql) {
$resql = $db->query($sql);
if (! $resql) {
dol_print_error($db);
}
while($obj = $db->fetch_object($resql)) {
// $this->data = array(array(0=>'labelxA',1=>yA1,...,n=>yAn), array('labelxB',yB1,...yBn)); // or when there is n series to show for each x
foreach($search_xaxis as $xkey => $xval) {
$fieldforxkey = 'x_'.$xkey;
$xlabel = $obj->$fieldforxkey;
$xvalwithoutprefix = preg_replace('/^[a-z]+\./', '', $xval);
if (! empty($object->fields[$xvalwithoutprefix]['arrayofkeyval'])) {
$xlabel = $object->fields[$xvalwithoutprefix]['arrayofkeyval'][$obj->$fieldforxkey];
}
$xarray = array(0 => (($xlabel || $xlabel == '0') ? dol_trunc($xlabel, 20, 'middle') : $langs->trans("NotDefined")));
foreach($search_measures as $key => $val) {
$fieldfory = 'y_'.$key;
$xarray[] = $obj->$fieldfory;
}
$data[] = $xarray;
}
}
$totalnbofrecord = count($data);
}
print '';
if ($mode == 'grid') {
// TODO
}
if ($mode == 'graph') {
$WIDTH = '80%';
$HEIGHT = 200;
// Show graph
$px1 = new DolGraph();
$mesg = $px1->isGraphKo();
if (! $mesg)
{
$px1->SetData($data);
unset($data);
$arrayoftypes = array();
foreach($search_measures as $key => $val) {
$arrayoftypes[] = $search_graph;
}
$px1->SetLegend($legend);
$px1->SetMinValue($px1->GetFloorMinValue());
$px1->SetMaxValue($px1->GetCeilMaxValue());
$px1->SetWidth($WIDTH);
$px1->SetHeight($HEIGHT);
$px1->SetYLabel($langs->trans("Y"));
$px1->SetShading(3);
$px1->SetHorizTickIncrement(1);
$px1->SetCssPrefix("cssboxes");
$px1->SetType($arrayoftypes);
$px1->mode='depth';
$px1->SetTitle('');
$dir=$conf->user->dir_temp;
dol_mkdir($dir);
$filenamenb = $dir.'/customreport_'.$object->element.'.png';
$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=user&file=customreport_'.$object->element.'.png';
$px1->draw($filenamenb, $fileurlnb);
print $px1->show($totalnbofrecord ? 0 : $langs->trans("SelectYourGraphOptionsFirst"));
}
}
if ($sql) {
// Show admin info
print '
'.info_admin($langs->trans("SQLUsedForExport").':
'.$sql);
}
print '
';
dol_fiche_end();
// End of page
llxFooter();
$db->close();