How sum rows in listview

Hi,
I was wondering how I could sum all the rows in listview to appear as a bar just above the list and below the search bar.
Any ideas?
Thanks.

Hi ajgisme ,

To get sum of all the rows in list view for your module : you can create a file called

Example :

suitecrm/custom/modules//views/view.list.php

and in this file you can achieve it by override the below two function

listViewProcess() , display() ;

i hope this help’s !

Thanks for your time and support !

Hi,

In process_record hook you can do sum of all values. You can modify tpl of listview to display it.

You can add block of content via Jquery - after block of search or before pagination block.

Thanks and Regards,

Alpesh Savaliya

Hi,
That sounds like a good idea thanks.

But how would I start the function to sum them? Because doesn’t it do it per record?
‘bean ->subtotal_c’

How do I make it an array in which to sum?
Thanks.

Hi,

process_record hook fire for each record or list of contacts.
so you can have global variable to assign sum of all row.

I am gonna write post about it in sometime. As It is a good idea to have sum of all fields.

Thanks and Regards,
Alpesh Savaliya

Yes but when I try:

array_sum($invoice_id->grossmanual_c;);

It does not work because it’s not array it’s just one number, the process_record logic hook fires for each record yes, so how I get it to some all of them and not each time for each record?
Thanks.

Hi,

Have a look at this code. It is working code for me.


class Hook_Class{
	function do_sum($bean,$event, $args){
		global $opp_amount;
		$opp_amount += $bean->amount_usdollar;
		$GLOBALS['log']->fatal(print_r( $opp_amount,TRUE));
	}
}

Hi,
Thanks a lot it worked, now I’m trying to style it in the listview.tpl file.

However, it doesn’t just show me the number once, it repeats the number for the amount of rows there are.
For example if there are 10 rows of 50 it will display: 500 500 500 500 500 500 500 500 500 500
And if it’s 2 rows of 100 it will display: 200 200

Any idea how to stop this?

I used this:
{php}
global $opp_amount;
echo $opp_amount;
{/php}

Hi,
You have to use another logic hook - after_ui_frame to display total with the help of javascript. it is an application level hook.

But it did display the total, so why do I need another logic hook?

Is echo not the right function to use? How do I stop it repeating?
Thanks.

Hi,

To stop it repeating it is require to do as I have suggested.
Module hook fire after each bean so it repeats, while application hook fire only once for application.

Hi,
Thanks I get you.

I tried this but it won’t work:

<?php
class Totals
{
    function subtotal(&$bean, $event, $arguments)
    {

        $invoice_id = BeanFactory::getBean('AOS_Invoices', $bean->id);
		global $opp_amount;
		$opp_amount += $invoice_id->grossmanual_c;
		
	}
}

It says:
Warning: Missing argument 3 for Totals::subtotal(),…

Hi,

Please have a look at this post.
http://www.simba-sys.com/display-a-sum-of-field-in-the-listview-of-module/

Hi,
Thanks, I have looked at that post and completed it all, but nothing shows up at all.

Perhaps it’s the $total variable that doesn’t seem to be defined anywhere?

Yes. Please replace it with - $opp_amount.
I am gonna modified it on post also.
Thanks.

Hi,
I’ve done that but it’s still not working. I don’t think the javascript is executing as I tested it with an alert and nothing happened then either.

Hi,
Can you please remove conditions and test it ?

Yep already did that and also didn’t work.

Hello ajgisme,

The solution proposed by simbasys is the most elegant and complete option, I believe.

On the other hand, if you want a simple sum of a field of your module in ListView, I’ve found a simpler solution in the Internet and implemented it successfully.

Please check this post where I proposed it to another developer. I’ve included some sample code there too.

https://suitecrm.com/forum/suitecrm-7-0-discussion/3215-add-calculation-total-on-accounts-module#14529

Best regards,

1 Like

thread is from 2015 and one of the links is dead… wondering if there’s a more modern solution now? :slight_smile: