SugarCRM发票添加逻辑挂钩以计算线价 [英] SugarCRM invoice add logic hook to calculate line price

查看:104
本文介绍了SugarCRM发票添加逻辑挂钩以计算线价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SugarCRM创建发票时,发票明细中包含单位数量和单价.我想自动填充字段价格,这只是上面两个字段的乘积. 这是我在custom/modules/C_Inc_Invoice_Detail目录中添加的内容:

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

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'
    );
    ?>

和autofilllineprice.php:

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;    
    }
    }
?>

你能建议吗?

推荐答案

这篇文章没有直接回答这个问题,但是它记录了完成类似要求所涉及的步骤. SugarCE-6.5.22中的帐户模块具有一个年收入字段,并显示在 DetailView 中,如下所示.

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. 第一步是创建一个非db字段,该字段将保存此新货币的值.为此,我们需要创建两个文件,一个文件定义了字段("custom_fields.php"),另一个文件定义了该字段的特定于语言的显示值("en_us_custom_fields.php").这两个文件将在下面提到的位置中创建.如果尚不存在,请创建所需的目录结构.

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)';

?>

添加这两个文件后,我们需要运行 Admin-> Repair-> Quick Repair and Rebuild .完成后,如果打开 Admin-> Studio 并展开 Accounts-> Layouts-> DetailView ,我们应该会在此处看到我们新创建的字段.我们可以将其拖放到布局中想要显示的位置,然后点击 Save&部署.

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. 下一个任务是填充此新货币的值.这可以使用SugarCRM提供的逻辑挂钩来实现.从数据库中检索到帐户数据后,我们每次都需要计算该字段的值. after_retrieve 事件可用于此目的.首先,让我们创建一个php文件("annual_revenue_hook.php"),其中包含用于在以下位置计算此新货币字段值的代码.
  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; 
        }

    }

?>

要注册上述钩子,我们需要编辑模块的logic_hooks.php文件并添加以下行:

To register the above hook, we need to edit the logic_hooks.php file of the module and add the following lines:

自定义\模块\帐户\ 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. 第一步是获取模块之间的关系信息,在本例中为帐户机会.我们需要打开文件 modules \ Accounts \ vardefs.php 并搜索机会.它应该给我们以下信息.

  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',
),

然后,我们可以打开 Admin-> Studio ,展开 Accounts-> Relationships ,然后找到关系类型,如下所示:

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;
            }
        }

    }
?>

上面的代码现在应该将Annual_revenue乘以100,并且还应将所有与之相关的机会相加,以计算Annual_revenue_inr的值.

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.

这篇关于SugarCRM发票添加逻辑挂钩以计算线价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆