2015-11-02 124 views
0

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

基本数学税=(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; 

这是因为他们的钱场,但是,它似乎BaseAmount的值在0.00创建消息的预操作

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

+0

decimal _taxPercent = 20.0;随后适用舍入应用.... –

回答

0

好人,我有一个答案,有点 - 但我会发布一个新的问题,为下一个位。在此视频的帮助下(https://www.youtube.com/watch?v=54Zibk9v338),我有一个工作插件 - 计算quotedetail实体的税收字段。

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

我需要的是一个步骤,在Create消息上触发,首先设置像Tax Taxage(20)这样的变量并初始化其他字段的变量,但接下来会触发更新消息获取值和设置税收领域。

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

我现在头疼的人

+0

我认为你遇到的问题是看到事情发生的地方。 1)创建,在这里运行所有的东西,因为你想在创建时计算税额,如果它不这样做,你有代码的问题。 2)更新,您还需要在更新时运行代码,因为您可能会更改值。如果您更改手动折扣,您希望发生什么以及实际发生了什么? –

2

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

+0

感谢您的评论。我意识到前映像的需求,但我还没有进入开发阶段。 –

+0

对不起,缩短答案。如果手动折扣大于0.00,if语句可能会运行?我只是通过调试阅读,因为该项目只是一个类库:https://msdn.microsoft.com/en-us/library/aa291243(v=vs.71).aspx –

+0

我不认为那么创建和“小数变量的值尚未传递到上下文中”是问题所在。在创建时,表单中的所有值都会在上下文中传递,但是,如果您没有提供任何税款值,则可能存在问题。你可以有一个“decimal_taxpercent = currentEntity.Attributes.Exists(”new_taxpercent“)?currentEntity.new_taxpercent:0;” 或类似的东西。你有没有在代码上运行任何调试器来查看它为什么变成0? –