When creating an invoice with SugarCRM, in the invoice detail there's number of unit and unit price. I would like to populate the field line price automatically, which is simply the product of those two fields above. Here is what I added in the custom/modules/C_Inc_Invoice_Detail directory :
logic_hook.php
<?php
$hook_version = 1;
$hook_array = array();
$hook_array['after_save'] = array();
$hook_array['after_save'][] = array(
1,
'Auto Fill Line price',
'custom/modules/C_Inv_Invoice_Detail/autofilllineprice.php',
'AutoFillLinePrice',
'autofilllineprice'
);
?>
and the autofilllineprice.php :
<?php
//prevents directly accessing this file from a web browser
if
(!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');
class AutoFillLinePrice {
function autofilllineprice($bean, $event, $arguments){
$line_price = $bean->unit_price * $bean->number_units;
}
}
?>
Could you advise ?
This post does not answer this question directly, but it documents the steps involved in accomplishing a similar requirement. The Accounts module in SugarCE-6.5.22 has a field Annual Revenue and it's displayed in the DetailView as shown below.
The following are the steps involved in adding a new field that displays the value of this field converted to some other currency.
custom\Extension\modules\Accounts\Ext\Vardefs\custom_fields.php
custom\Extension\modules\Accounts\Ext\Language\en_us_custom_fields.php
After adding these two files, we need to run Admin-->Repair-->Quick Repair and Rebuild. After it's complete, if we open Admin-->Studio and expand Accounts-->Layouts-->DetailView, we should see our newly created field available there. We can drag and drop it inside the layout wherever we want it to appear and click Save & Deploy.
Now, this new field should be visible in the detail view of the account.
custom\Extension\modules\Accounts\Ext\hooks\annual_revenue_hook.php
To register the above hook, we need to edit the logic_hooks.php file of the module and add the following lines:
custom\modules\Accounts\logic_hooks.php
After completing these steps, we should see the value of the new currency populated in the detail view as shown below:
It's also possible to get data from associated modules. For example, let's say we need to calculate the sum of all Opportunities associated with this Account and add to our new field. The following are the steps to accomplish it:
The first step is to get the relationship information between the modules, in this case between Accounts and Opportunities. We need to open the file modules\Accounts\vardefs.php and search for Opportunities. It should give us the following information.
Then, we can open Admin-->Studio, expand Accounts-->Relationships and find the type of relationship as shown below:
Now that we have identified the relationship information, we can edit our existing hook as shown below:
custom\Extension\modules\Accounts\Ext\hooks\annual_revenue_hook.php
The above code should now multiply annual_revenue by 100 and also sum up all the associated Opportunities with it to calculate the value of annual_revenue_inr.