diff --git a/htdocs/product/stats/facture.php b/htdocs/product/stats/facture.php index 4110781298d..366681eeea8 100644 --- a/htdocs/product/stats/facture.php +++ b/htdocs/product/stats/facture.php @@ -48,6 +48,12 @@ if (!empty($user->socid)) { // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context $hookmanager->initHooks(array('productstatsinvoice')); +$extrafields = new ExtraFields($db); + +// Fetch optionals attributes and labels +$extrafields->fetch_name_optionals_label('facture'); + +$search_array_options = $extrafields->getOptionalsFromPost('facture', '', 'search_'); $showmessage = GETPOST('showmessage'); @@ -160,12 +166,32 @@ if ($id > 0 || !empty($ref)) { if (!$user->hasRight('societe', 'client', 'voir') && !$socid) { $sql .= ", sc.fk_soc, sc.fk_user "; } + // Add fields from extrafields + if (!empty($extrafields->attributes['facture']['label'])) { + foreach ($extrafields->attributes['facture']['label'] as $key => $val) { + $sql .= ($extrafields->attributes['facture']['type'][$key] != 'separate' ? ", ef.".$key." as options_".$key : ''); + } + } + // Add fields from hooks + $parameters = array(); + $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters, $object, $action); // Note that $action and $object may have been modified by hook + $sql .= $hookmanager->resPrint; + $sql = preg_replace('/,\s*$/', '', $sql); + $sql .= " FROM ".MAIN_DB_PREFIX."societe as s"; $sql .= ", ".MAIN_DB_PREFIX."facture as f"; + if (isset($extrafields->attributes['facture']['label']) && is_array($extrafields->attributes['facture']['label']) && count($extrafields->attributes['facture']['label'])) { + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.'facture'."_extrafields as ef on (f.rowid = ef.fk_object)"; + } $sql .= ", ".MAIN_DB_PREFIX."facturedet as d"; if (!$user->hasRight('societe', 'client', 'voir') && !$socid) { $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; } + // Add table from hooks + $parameters = array(); + $reshook = $hookmanager->executeHooks('printFieldListFrom', $parameters, $object); // Note that $action and $object may have been modified by hook + $sql .= $hookmanager->resPrint; + $sql .= " WHERE f.fk_soc = s.rowid"; $sql .= " AND f.entity IN (".getEntity('invoice').")"; $sql .= " AND d.fk_facture = f.rowid"; @@ -182,6 +208,18 @@ if ($id > 0 || !empty($ref)) { if ($socid) { $sql .= " AND f.fk_soc = ".((int) $socid); } + // Add where from extra fields + include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php'; + // Add where from hooks + $parameters = array(); + $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $object, $action); // Note that $action and $object may have been modified by hook + $sql .= $hookmanager->resPrint; + + // Add HAVING from hooks + $parameters = array(); + $reshook = $hookmanager->executeHooks('printFieldListHaving', $parameters, $object, $action); // Note that $action and $object may have been modified by hook + $sql .= empty($hookmanager->resPrint) ? "" : " HAVING 1=1 ".$hookmanager->resPrint; + $sql .= $db->order($sortfield, $sortorder); // Calcul total qty and amount for global if full scan list @@ -213,6 +251,13 @@ if ($id > 0 || !empty($ref)) { $option .= '&search_year='.urlencode($search_year); } + // Add $param from extra fields + include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_param.tpl.php'; + // Add $param from hooks + $parameters = array(); + $reshook = $hookmanager->executeHooks('printFieldListSearchParam', $parameters, $object, $action); // Note that $action and $object may have been modified by hook + $option .= $hookmanager->resPrint; + print '