SugarCRM invoice add logic hook to calculate line price

邮差的信 提交于 2020-01-06 08:16:36

问题


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 ?


回答1:


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.

  1. The first step is to create a non-db field that will hold the value of this new currency. For this, we need to create two files, one that defines the field("custom_fields.php") and the other that defines a language specific dispplay value for this field("en_us_custom_fields.php"). These two files are to be created in the below mentioned location. Please create the required directory structure if not present already.

custom\Extension\modules\Accounts\Ext\Vardefs\custom_fields.php

<?php

    if (!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');

    $dictionary['Account']['fields']['annual_revenue_inr'] = array(
                'name' => 'annual_revenue_inr',
                'vname' => 'LBL_ANNUAL_REVENUE_INR',
                'label'=>'LBL_ANNUAL_REVENUE_INR',
                'type' => 'name',
                'module' => 'Accounts',
                'source' => 'non-db',
                'studio' => array('detailview'=>true),
                'default_value' => '',
                'help' => 'Annual Revenue(INR)',
                'comment' => 'Annual Revenue(INR)',
                'reportable' => true,
                'audited' => false,
                'duplicate_merge' => false,
                'importable' => 'true',         
            );

?>

custom\Extension\modules\Accounts\Ext\Language\en_us_custom_fields.php

<?php

    if (!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');

    $mod_strings['LBL_ANNUAL_REVENUE_INR'] = 'Annual Revenue(INR)';

?>

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.

  1. The next task is to populate the value for this new currency. This can be achieved using logic hooks provided by SugarCRM. We need to compute the value for this field everytime after an account data is retrieved from database. The after_retrieve event can be used for this. First, let's create a php file("annual_revenue_hook.php") with code that computes the value of this new currency field in the following location.

custom\Extension\modules\Accounts\Ext\hooks\annual_revenue_hook.php

<?php

    if (!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');

    class AnnualRevenue {

        function computeAnnualRevenueINR($bean, $event, $arguments){
            $bean->annual_revenue_inr = $bean->annual_revenue * 100; 
        }

    }

?>

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

//Create a new array that holds after_retrieve event hooks if not present already
$hook_array['after_retrieve'] = Array(); 

//Register our annual revenue hook for computing new currency value
$hook_array['after_retrieve'][]= Array(1, 'Compute Annual Revenue in INR', 'custom/Extension/modules/Accounts/Ext/hooks/annual_revenue_hook.php','AnnualRevenue', 'computeAnnualRevenueINR'); 

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:

  1. 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.

    'opportunities' =>
    array (
    'name' => 'opportunities',
    'type' => 'link',
    'relationship' => 'accounts_opportunities',
    'module'=>'Opportunities',
    'bean_name'=>'Opportunity',
    'source'=>'non-db',
        'vname'=>'LBL_OPPORTUNITY',
    ),
    

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

<?php

    if (!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');

    class AnnualRevenue {

        function computeAnnualRevenueINR($bean, $event, $arguments){
            $bean->annual_revenue_inr = $bean->annual_revenue * 100;
            $bean->load_relationship('opportunities');
            $opportunities = $bean->opportunities->getBeans();
            //Every account has multiple opportunities i.e array of Opportunity
            foreach($opportunities as $opportunity){
                $bean->annual_revenue_inr = $bean->annual_revenue_inr + $opportunity->amount;
            }
        }

    }
?>

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.



来源:https://stackoverflow.com/questions/38370971/sugarcrm-invoice-add-logic-hook-to-calculate-line-price

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!