Hi drk2009,
I'll sketch out a possible design. See the Developer's Guide and the Wiki for details of how to write logic hooks.
Say your module is named MYMODULE. You can use a before_save logic hook or after_save. This will be defined in two files:
1. <sugarroot>/custom/modules/MYMODULE/logic_hooks.php with content something like:
PHP Code:
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
$hook_version = 1;
$hook_array = Array();
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1, 'calc_stats','custom/modules/MYMODULE/calc_stats.php','calc_stats', 'calc_stats');
?>
2. <sugarroot>custom/modules/MYMODULE/calc_stats.php (Note that this file is referenced in the last line above and must be the same path. It would have contents like:
PHP Code:
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class calc_stats
{
function calc_stats(&$bean, $event, $arguments)
{
// ADD YOUR CODE HERE.
}
}
?>
The names in the array match the class and function name. You will add code that takes the fields and calculates the stat.
I did not see how you are saving your products you want to count. Let's say your stats is dollars sold. You could enter to total dollars into your module, but then there is nothing to calculate. More likely you will enter each sale and there will be a dollar amount for that sale. Each would be in it's own record. Then you can use the logic hook to calculate the total using a query to get the records.
You can access the database from your logic hook. The $bean variable contains all the fields in this module and lets you get to related modules. There are lots of examples in the forums.
Hopefully that gets you started. Post any specific followup questions.
Phil
Bookmarks