add calculation (total) on Accounts module

Hi,
Sorry for not answering before.

Yes, you can do that in any module.

All you need to do is:

  1. Edit custom/modules/Accounts/logic_hooks.php and add something like:
<?php
// Do not store anything in this file that is not part of the array or the hook version.  This file will        
// be automatically rebuilt in the future. 
$hook_version = 1;
$hook_array = Array();
// position, file, function 
$hook_array['before_save'] = Array();
$hook_array['after_retrieve'] = Array();
$hook_array['process_record'] = Array();
$hook_array['after_ui_frame'] = Array();

// This will make your function be called for every record in a ListView:
$hook_array['process_record'][] = Array(1, 'Some description of what you are doing',     'custom/modules/Accounts/SomeFileName_LogicHook.php','YourPHPClassName', 'functionToSumYourField');

// This will add the sum to the HTML page just before it is displayed
$hook_array['after_ui_frame'][] = Array(1, 'Some description of what you are doing',     'custom/modules/Accounts/SomeFileName_LogicHook.php','YourPHPClassName', 'functionToDisplayTheSum');

?>
  1. Edit custom/modules/Accounts/SomeFileName_LogicHook.php and create your functions to sum up a specific field and show that amount at the header and footer of your ListView:
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class YourPHPClassName {
        // Local static variable to store your sum
        protected static $yourSum = 0;

        function functionToSumYourField(&$focus, $event, $arguments) {
                // do any processing and calculations you want before summing your values here

                // this will sum up the value inside your desired field into your local static variable yourSum
                self::$yourSum += $focus->the_name_of_any_field_inside_your_module_c;

                // do further processing if you want here
        } // end of functionToSumYourField

        function functionToDisplayTheSum(&$focus, $event, $arguments) {
                if ($GLOBALS['action'] == 'index' || $GLOBALS['action'] == 'ListView') {
                        $yourSum = self::$yourSum; // TODO: maybe you want to add some formatting to numbers, dates, etc.
                        echo <<<EOHTML
<script type="text/javascript">
<!--
$('<p align="center" vertical-align="middle"><b>Your sum is: {$yourSum}</b></p>').insertBefore('.paginationChangeButtons');
-->
</script>
EOHTML;
                }
        } // end of functionToDisplayTheSum

} // end of YourPHPClassName

?>

It’s pretty simple to work with Logic Hooks. Please double check PHP syntax above since I did not test this code in my environment.

Don’t forget to do a “Quick Repair” after you create the Logic Hooks files. After you create them and do a Quick Repair once, you can edit them without “Quick Repairing” again.

I hope this helps.

Cheers, Celso.

1 Like