From 2dd907a8e17baf4b402b3ec0969167ba9800bb61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 26 Jul 2018 09:41:44 +0200 Subject: [PATCH] extrafield totalizable --- htdocs/comm/propal/list.php | 31 ++++++++++++------ htdocs/core/actions_extrafields.inc.php | 10 ++++-- htdocs/core/class/extrafields.class.php | 32 ++++++++++++++----- htdocs/core/tpl/admin_extrafields_add.tpl.php | 4 +++ .../core/tpl/admin_extrafields_edit.tpl.php | 4 +++ .../core/tpl/admin_extrafields_view.tpl.php | 3 ++ .../tpl/extrafields_list_print_fields.tpl.php | 8 +++++ .../install/mysql/migration/8.0.0-9.0.0.sql | 1 + .../install/mysql/tables/llx_extrafields.sql | 1 + htdocs/langs/en_US/main.lang | 2 ++ 10 files changed, 76 insertions(+), 20 deletions(-) diff --git a/htdocs/comm/propal/list.php b/htdocs/comm/propal/list.php index 1a23c0b8728..1e00e48f36e 100644 --- a/htdocs/comm/propal/list.php +++ b/htdocs/comm/propal/list.php @@ -988,27 +988,40 @@ if ($resql) } // Show total line - if (isset($totalarray['totalhtfield']) + if (isset($totalarray['totalhtfield']) || isset($totalarray['totalvatfield']) || isset($totalarray['totalttcfield']) || isset($totalarray['totalamfield']) || isset($totalarray['totalrtpfield']) + || isset($totalarray['totalizable']) ) { print ''; $i=0; while ($i < $totalarray['nbfield']) { - $i++; - if ($i == 1) - { + $i++; + if ($i == 1) + { if ($num < $limit && empty($offset)) print ''.$langs->trans("Total").''; else print ''.$langs->trans("Totalforthispage").''; - } - elseif ($totalarray['totalhtfield'] == $i) print ''.price($totalarray['totalht']).''; - elseif ($totalarray['totalvatfield'] == $i) print ''.price($totalarray['totalvat']).''; - elseif ($totalarray['totalttcfield'] == $i) print ''.price($totalarray['totalttc']).''; - else print ''; + } + elseif ($totalarray['totalhtfield'] == $i) print ''.price($totalarray['totalht']).''; + elseif ($totalarray['totalvatfield'] == $i) print ''.price($totalarray['totalvat']).''; + elseif ($totalarray['totalttcfield'] == $i) print ''.price($totalarray['totalttc']).''; + elseif ($totalarray['totalizable']) { + $printed = false; + foreach ($totalarray['totalizable'] as $totalizable) { + if ($totalizable['pos']==$i && ! $printed) { + print ''.price($totalizable['total']).''; + $printed = true; + } + } + if (! $printed) { + print ''; + } + } + else print ''; } print ''; } diff --git a/htdocs/core/actions_extrafields.inc.php b/htdocs/core/actions_extrafields.inc.php index 599b847b49e..6b6d4b7a176 100644 --- a/htdocs/core/actions_extrafields.inc.php +++ b/htdocs/core/actions_extrafields.inc.php @@ -181,7 +181,9 @@ if ($action == 'add') GETPOST('help','alpha'), GETPOST('computed_value','alpha'), (GETPOST('entitycurrentorall', 'alpha')?0:''), - GETPOST('langfile', 'alpha') + GETPOST('langfile', 'alpha'), + 1, + (GETPOST('totalizable', 'alpha')?1:0) ); if ($result > 0) { @@ -331,7 +333,7 @@ if ($action == 'update') $visibility = GETPOST('list', 'alpha'); if ($type == 'separate') $visibility=3; - $result=$extrafields->update( + $result=$extrafields->update( GETPOST('attrname', 'alpha'), GETPOST('label', 'alpha'), $type, @@ -348,7 +350,9 @@ if ($action == 'update') GETPOST('default_value','alpha'), GETPOST('computed_value','alpha'), (GETPOST('entitycurrentorall', 'alpha')?0:''), - GETPOST('langfile') + GETPOST('langfile'), + 1, + (GETPOST('totalizable', 'alpha')?1:0) ); if ($result > 0) { diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index cd3e6c90fb5..9d8d718993b 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -9,6 +9,7 @@ * Copyright (C) 2015 Charles-Fr BENKE * Copyright (C) 2016 Raphaël Doursenaud * Copyright (C) 2017 Nicolas ZABOURI + * Copyright (C) 2018 Frédéric France * * 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 @@ -516,14 +517,15 @@ class ExtraFields * @param string $entity Entity of extrafields * @param string $langfile Language file * @param string $enabled Condition to have the field enabled or not + * @param int $totalizable Is extrafield totalizable on list * @return int >0 if OK, <=0 if KO */ - function update($attrname, $label, $type, $length, $elementtype, $unique=0, $required=0, $pos=0, $param='', $alwayseditable=0, $perms='', $list='', $help='', $default='', $computed='', $entity='', $langfile='', $enabled='1') + function update($attrname, $label, $type, $length, $elementtype, $unique=0, $required=0, $pos=0, $param='', $alwayseditable=0, $perms='', $list='', $help='', $default='', $computed='', $entity='', $langfile='', $enabled='1', $totalizable=0) { if ($elementtype == 'thirdparty') $elementtype='societe'; if ($elementtype == 'contact') $elementtype='socpeople'; - $table=$elementtype.'_extrafields'; + $table=$elementtype.'_extrafields'; if ($elementtype == 'categorie') $table='categories_extrafields'; if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname)) @@ -568,7 +570,7 @@ class ExtraFields { if ($label) { - $result=$this->update_label($attrname,$label,$type,$length,$elementtype,$unique,$required,$pos,$param,$alwayseditable,$perms,$list,$help,$default,$computed,$entity,$langfile,$enabled); + $result=$this->update_label($attrname,$label,$type,$length,$elementtype,$unique,$required,$pos,$param,$alwayseditable,$perms,$list,$help,$default,$computed,$entity,$langfile,$enabled, $totalizable); } if ($result > 0) { @@ -625,12 +627,13 @@ class ExtraFields * @param string $entity Entity of extrafields * @param string $langfile Language file * @param string $enabled Condition to have the field enabled or not - * @return int <=0 if KO, >0 if OK + * @param int $totalizable Is extrafield totalizable on list + * @return int <=0 if KO, >0 if OK */ - private function update_label($attrname,$label,$type,$size,$elementtype,$unique=0,$required=0,$pos=0,$param='',$alwayseditable=0,$perms='',$list='0',$help='',$default='',$computed='',$entity='',$langfile='',$enabled='1') + private function update_label($attrname,$label,$type,$size,$elementtype,$unique=0,$required=0,$pos=0,$param='',$alwayseditable=0,$perms='',$list='0',$help='',$default='',$computed='',$entity='',$langfile='',$enabled='1', $totalizable=0) { global $conf, $user; - dol_syslog(get_class($this)."::update_label ".$attrname.", ".$label.", ".$type.", ".$size.", ".$elementtype.", ".$unique.", ".$required.", ".$pos.", ".$alwayseditable.", ".$perms.", ".$list.", ".$default.", ".$computed.", ".$entity.", ".$langfile.", ".$enabled); + dol_syslog(get_class($this)."::update_label ".$attrname.", ".$label.", ".$type.", ".$size.", ".$elementtype.", ".$unique.", ".$required.", ".$pos.", ".$alwayseditable.", ".$perms.", ".$list.", ".$default.", ".$computed.", ".$entity.", ".$langfile.", ".$enabled.", ".$totalizable); // Clean parameters if ($elementtype == 'thirdparty') $elementtype='societe'; @@ -638,6 +641,9 @@ class ExtraFields if (empty($pos)) $pos=0; if (empty($list)) $list='0'; + if (empty($totalizable)) { + $totalizable = 0; + } if (empty($required)) $required=0; if (empty($unique)) $unique=0; if (empty($alwayseditable)) $alwayseditable=0; @@ -681,6 +687,7 @@ class ExtraFields $sql.= " alwayseditable,"; $sql.= " param,"; $sql.= " list,"; + $sql.= " totalizable,"; $sql.= " fielddefault,"; $sql.= " fieldcomputed,"; $sql.= " fk_user_author,"; @@ -703,6 +710,7 @@ class ExtraFields $sql.= " '".$this->db->escape($alwayseditable)."',"; $sql.= " '".$this->db->escape($params)."',"; $sql.= " '".$this->db->escape($list)."', "; + $sql.= " ".$totalizable.","; $sql.= " ".(($default!='')?"'".$this->db->escape($default)."'":"null").","; $sql.= " ".($computed?"'".$this->db->escape($computed)."'":"null").","; $sql .= " " . $user->id . ","; @@ -773,7 +781,7 @@ class ExtraFields // We should not have several time this log. If we have, there is some optimization to do by calling a simple $object->fetch_optionals() that include cache management. dol_syslog("fetch_name_optionals_label elementtype=".$elementtype); - $sql = "SELECT rowid,name,label,type,size,elementtype,fieldunique,fieldrequired,param,pos,alwayseditable,perms,langs,list,fielddefault,fieldcomputed,entity,enabled,help"; + $sql = "SELECT rowid,name,label,type,size,elementtype,fieldunique,fieldrequired,param,pos,alwayseditable,perms,langs,list,totalizable,fielddefault,fieldcomputed,entity,enabled,help"; $sql.= " FROM ".MAIN_DB_PREFIX."extrafields"; $sql.= " WHERE entity IN (0,".$conf->entity.")"; if ($elementtype) $sql.= " AND elementtype = '".$elementtype."'"; // Filed with object->table_element @@ -807,6 +815,7 @@ class ExtraFields $this->attribute_perms[$tab->name]=(strlen($tab->perms) == 0 ? 1 : $tab->perms); $this->attribute_langfile[$tab->name]=$tab->langs; $this->attribute_list[$tab->name]=$tab->list; + $this->attribute_totalizable[$tab->name]=$tab->totalizable; $this->attribute_entityid[$tab->name]=$tab->entity; $this->attribute_entitylabel[$tab->name]=(empty($labelmulticompany[$tab->entity])?'Entity'.$tab->entity:$labelmulticompany[$tab->entity]); @@ -825,6 +834,7 @@ class ExtraFields $this->attributes[$tab->elementtype]['perms'][$tab->name]=(strlen($tab->perms) == 0 ? 1 : $tab->perms); $this->attributes[$tab->elementtype]['langfile'][$tab->name]=$tab->langs; $this->attributes[$tab->elementtype]['list'][$tab->name]=$tab->list; + $this->attributes[$tab->elementtype]['totalizable'][$tab->name]=$tab->totalizable; $this->attributes[$tab->elementtype]['entityid'][$tab->name]=$tab->entity; $this->attributes[$tab->elementtype]['entitylabel'][$tab->name]=(empty($labelmulticompany[$tab->entity])?'Entity'.$tab->entity:$labelmulticompany[$tab->entity]); $this->attributes[$tab->elementtype]['enabled'][$tab->name]=$tab->enabled; @@ -886,6 +896,7 @@ class ExtraFields $perms=dol_eval($this->attributes[$extrafieldsobjectkey]['perms'][$key], 1); $langfile=$this->attributes[$extrafieldsobjectkey]['langfile'][$key]; $list=dol_eval($this->attributes[$extrafieldsobjectkey]['list'][$key], 1); + $totalizable=$this->attributes[$extrafieldsobjectkey]['totalizable'][$key]; $help=$this->attributes[$extrafieldsobjectkey]['help'][$key]; $hidden=(empty($list) ? 1 : 0); // If empty, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller) } @@ -902,6 +913,7 @@ class ExtraFields $param=$this->attribute_param[$key]; $langfile=$this->attribute_langfile[$key]; $list=$this->attribute_list[$key]; + $totalizable=$this->attribute_totalizable[$key]; $hidden=(empty($list) ? 1 : 0); // If empty, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller) } @@ -1753,7 +1765,7 @@ class ExtraFields $align=''; - if ($type == 'date') + if ($type == 'date') { $align="center"; } @@ -1781,6 +1793,10 @@ class ExtraFields { $align="center"; } + elseif ($type == 'price') + { + $align="right"; + } return $align; } diff --git a/htdocs/core/tpl/admin_extrafields_add.tpl.php b/htdocs/core/tpl/admin_extrafields_add.tpl.php index e80685154fc..2c971c7a553 100644 --- a/htdocs/core/tpl/admin_extrafields_add.tpl.php +++ b/htdocs/core/tpl/admin_extrafields_add.tpl.php @@ -2,6 +2,7 @@ /* Copyright (C) 2010-2017 Laurent Destailleur * Copyright (C) 2012 Regis Houssin * Copyright (C) 2016 Charlie Benke + * Copyright (C) 2018 Frédéric France * * 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 @@ -52,6 +53,7 @@ $langs->load("modulebuilder"); var required = jQuery("#required"); var alwayseditable = jQuery("#alwayseditable"); var list = jQuery("#list"); + var totalizable = jQuery("#totalizable"); load("modulebuilder"); textwithpicto($langs->trans("Visibility"), $langs->trans("VisibleDesc")); ?> + +trans("Totalizable"); ?>> textwithpicto($langs->trans("HelpOnTooltip"), $langs->trans("HelpOnTooltipDesc")); ?> multicompany->enabled) { ?> diff --git a/htdocs/core/tpl/admin_extrafields_edit.tpl.php b/htdocs/core/tpl/admin_extrafields_edit.tpl.php index cb5ef26bc57..d4dc2776fde 100644 --- a/htdocs/core/tpl/admin_extrafields_edit.tpl.php +++ b/htdocs/core/tpl/admin_extrafields_edit.tpl.php @@ -1,6 +1,7 @@ * Copyright (C) 2012 Regis Houssin + * Copyright (C) 2018 Frédéric France * * 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 @@ -51,6 +52,7 @@ $langs->load("modulebuilder"); var required = jQuery("#required"); var alwayseditable = jQuery("#alwayseditable"); var list = jQuery("#list"); + var totalizable = jQuery("#totalizable"); attributes[$elementtype]['param'][$attrname]; $perms=$extrafields->attributes[$elementtype]['perms'][$attrname]; $langfile=$extrafields->attributes[$elementtype]['langfile'][$attrname]; $list=$extrafields->attributes[$elementtype]['list'][$attrname]; +$totalizable = $extrafields->attributes[$elementtype]['totalizable'][$attrname]; $help=$extrafields->attributes[$elementtype]['help'][$attrname]; $entitycurrentorall=$extrafields->attributes[$elementtype]['entityid'][$attrname]; @@ -251,6 +254,7 @@ else trans("AlwaysEditable"); ?>> textwithpicto($langs->trans("Visibility"), $langs->trans("VisibleDesc")); ?> +textwithpicto($langs->trans("Totalizable"), $langs->trans("TotalizableDesc")); ?>> textwithpicto($langs->trans("HelpOnTooltip"), $langs->trans("HelpOnTooltipDesc")); ?> multicompany->enabled) { ?> diff --git a/htdocs/core/tpl/admin_extrafields_view.tpl.php b/htdocs/core/tpl/admin_extrafields_view.tpl.php index a03c06f2d1d..ebe7a49877d 100644 --- a/htdocs/core/tpl/admin_extrafields_view.tpl.php +++ b/htdocs/core/tpl/admin_extrafields_view.tpl.php @@ -1,6 +1,7 @@ * Copyright (C) 2012-2017 Regis Houssin + * Copyright (C) 2018 Frédéric France * * 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 @@ -61,6 +62,7 @@ print ''.$langs->trans("Unique").''; print ''.$langs->trans("Required").''; print ''.$langs->trans("AlwaysEditable").''; print ''.$form->textwithpicto($langs->trans("Visible"), $langs->trans("VisibleDesc")).''; +print ''.$form->textwithpicto($langs->trans("Totalizable"), $langs->trans("TotalizableDesc")).''; if ($conf->multicompany->enabled) { print ''.$langs->trans("Entities").''; } @@ -88,6 +90,7 @@ if (is_array($extrafields->attributes[$elementtype]['type']) && count($extrafiel print ''.yn($extrafields->attributes[$elementtype]['required'][$key])."\n"; print ''.yn($extrafields->attributes[$elementtype]['alwayseditable'][$key])."\n"; print ''.$extrafields->attributes[$elementtype]['list'][$key]."\n"; + print ''.yn($extrafields->attributes[$elementtype]['totalizable'][$key])."\n"; if (! empty($conf->multicompany->enabled)) { print ''.($extrafields->attributes[$elementtype]['entityid'][$key]==0?$langs->trans("All"):$extrafields->attributes[$elementtype]['entitylabel'][$key]).''; } diff --git a/htdocs/core/tpl/extrafields_list_print_fields.tpl.php b/htdocs/core/tpl/extrafields_list_print_fields.tpl.php index e59f50501ab..b506bda0793 100644 --- a/htdocs/core/tpl/extrafields_list_print_fields.tpl.php +++ b/htdocs/core/tpl/extrafields_list_print_fields.tpl.php @@ -40,6 +40,14 @@ if (! empty($extrafieldsobjectkey)) // $extrafieldsobject is the $object->table_ print $extrafields->showOutputField($key, $value, '', $extrafieldsobjectkey); print ''; if (! $i) $totalarray['nbfield']++; + + if ($extrafields->attributes[$extrafieldsobjectkey]['totalizable'][$key]) { + if (! $i) { + // we keep position for the first line + $totalarray['totalizable'][$key]['pos'] = $totalarray['nbfield']; + } + $totalarray['totalizable'][$key]['total'] += $obj->$tmpkey; + } if (! empty($val['isameasure'])) { if (! $i) $totalarray['pos'][$totalarray['nbfield']]='ef.'.$tmpkey; diff --git a/htdocs/install/mysql/migration/8.0.0-9.0.0.sql b/htdocs/install/mysql/migration/8.0.0-9.0.0.sql index 87ada98390b..82d58c40192 100644 --- a/htdocs/install/mysql/migration/8.0.0-9.0.0.sql +++ b/htdocs/install/mysql/migration/8.0.0-9.0.0.sql @@ -30,6 +30,7 @@ ALTER TABLE llx_extrafields ADD COLUMN help text NULL; +ALTER TABLE llx_extrafields ADD COLUMN totalizable boolean DEFAULT FALSE after list; ALTER TABLE llx_user ADD COLUMN dateemploymentend date after dateemployment; diff --git a/htdocs/install/mysql/tables/llx_extrafields.sql b/htdocs/install/mysql/tables/llx_extrafields.sql index 1a85fc28a5a..9f37383957a 100644 --- a/htdocs/install/mysql/tables/llx_extrafields.sql +++ b/htdocs/install/mysql/tables/llx_extrafields.sql @@ -36,6 +36,7 @@ create table llx_extrafields alwayseditable integer DEFAULT 0, -- 1 if field can be edited whatever is element status param text, -- extra parameters to define possible values of field list varchar(255) DEFAULT '1', -- visibility of field. 0=Never visible, 1=Visible on list and forms, 2=Visible on list only. Using a negative value means field is not shown by default on list but can be selected for viewing + totalizable boolean DEFAULT FALSE, -- is extrafield totalizable on list langs varchar(64), -- example: fileofmymodule@mymodule help text, -- to store help tooltip fk_user_author integer, -- user making creation diff --git a/htdocs/langs/en_US/main.lang b/htdocs/langs/en_US/main.lang index 72725d3d634..8b47c46cef9 100644 --- a/htdocs/langs/en_US/main.lang +++ b/htdocs/langs/en_US/main.lang @@ -733,6 +733,8 @@ Result=Result ToTest=Test ValidateBefore=Card must be validated before using this feature Visibility=Visibility +Totalizable=Totalizable +TotalizableDesc=This field is totalizable in list Private=Private Hidden=Hidden Resources=Resources