CRM插件计算税收

时间:2015-11-02 06:27:04

标签: c# plugins dynamics-crm crm dynamics-crm-2015

一直困扰我的问题是Microsoft CRM Dynamics不会自动计算税收。 CRM坚持让用户手动输入税额。首先,计算20%的十进制数(2 dp)是很痛苦的,其次,最重要的是,如果我们的销售人员必须单独打开每个报价产品,然后计算税,输入然后保存记录,他们在卖东西时会惨遭失败。我正在尝试使用Amount [“baseamount”]和任何手册开发一个插件,该插件触发创建或更新消息,报价产品的预操作(实体逻辑名称= quotedetail),计算“税”字段折扣[“manualdiscountamount”]。

基本数学是tax =(baseamount - manualdiscountamount)/ 100 * 20

我似乎遇到的麻烦是,如果将变量_tax定义为直接十进制数,例如30,则该值会正确传递给字段但是如果我尝试使用另一个字段的值设置_tax值,值仍为0.00。继承我的代码:

public void Execute(IServiceProvider serviceProvider)
{

    // Sets the context for the plugin environment
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

    // Required for tracing
    ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

    // Create empty entity variable
    Entity entity = null;

    // Check if the InputParameters property contains a target
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
        // Obtain the target entity from the InputParameters
        entity = (Entity)context.InputParameters["Target"];

        // If message type is not Create OR Update AND it is not a Quote Product, exit gracefully
        if (context.MessageName != "Create" 
            || context.MessageName != "Update" 
                && context.PrimaryEntityName != "quotedetail") 
        { 
            return; 
        }
    }
    else
    {
        return;
    }

    try
    {
        // Used to access Data and Metadata for the platform
        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

        // Cast the 2 entity methods to access properties from entity retrived by context
        QuoteDetail currentEntity = entity.ToEntity<QuoteDetail>();

        // Set the variables
        int _taxPercent = 20;
        decimal _amount = currentEntity.BaseAmount.Value;
        decimal _manualDiscount = currentEntity.ManualDiscountAmount.Value;
        decimal _preTaxAmount = _amount - _manualDiscount;

       // Do the math and store in _tax
       decimal _tax = _preTaxAmount / 100 * taxPercent;

        // Set Tax field to value stored in _tax
        currentEntity.Tax = new Money(_tax);

    }

    catch (FaultException<OrganizationServiceFault> ex)
    {
        trace.Trace("Exception: " + ex.Message + " " + ex.StackTrace);
        throw new InvalidPluginExecutionException("An error occured in the plugin.", ex);
    }
}

我认为我的问题是在创建报价产品期间,十进制变量的.Values尚未传递到上下文中,因为这是在操作前发生的。

有没有人知道如何完成这项任务?我很惊讶MS离开了CRM。

这似乎是个大问题。

编辑1:变量的正确语法是:

    Money _amount = new Money(currentEntity.BaseAmount.Value);

    decimal _amount = currentEntity.BaseAmount.Value;

这是因为它们是Money字段,但是,在Create消息的预操作期间,似乎BaseAmount的值为0.00

编辑2:昨晚使用ITracingService调试了我的插件。我发现quotedetail实体也在创建消息后触发了更新消息。我仍然无法解决的问题是当填充现场数据时,即当每单位价格字段实际接收从Produt实体传入的数据时。我有这个工作。我的过程涉及在PreCreate消息期间设置字段值和可空对象值,获取PreUpdate图像并将其传递给在创建消息之后立即触发的PreUpdate消息。

2 个答案:

答案 0 :(得分:2)

例如,如果没有人工折扣,您可能会遇到问题。您是否尝试过调试代码以查看它崩溃的位置? 您还将遇到更新问题,因为只有更改的字段位于属性包中,您还需要检查预映像。

答案 1 :(得分:0)

好的伙计们,我已经得到了答案,但我会在下一位发布一个新问题。借助此视频(https://www.youtube.com/watch?v=54Zibk9v338),我有一个有效的插件 - 计算quotedetail实体的税收字段。

我认为我得到的是,当您向报价添加新产品时,创建消息会触发并将该项添加到表单中,然后更新消息将应用这些值。我正在研究没有更新消息的概念,因为我没有更新表格......

我需要的是触发Create消息的步骤,首先设置变量,如Tax Percentage(20)并初始化其他字段的变量,然后触发Update消息的第二步以获取值并设置税收字段。

我现在拥有的是一个计算税收字段,但如果我更改手动折扣金额字段则不会更新。如果我更改了奇数的金额字段,因为两个字段的代码相同......但我会为此发布一个新问题。

我的脑袋现在受伤了

相关问题