How to calculate margins based on JSON and ONIX (XML) input and calculate points based on them? In a Drupal Commerce distribution based on Drupal 7 we sell books. First I want to calculate a margin price based on several given data. The JSON is imported or can be manually added. The ONIX (in fact XML) data is already imported in an earlier stage. In the ONIX data the tags Price, Weight and Publisher are available as variable. When an order is placed I need a rule that combines these data, makes a calculation and present the result after ordercompletion in the dashboard of the admin as a view. 

After this I want to calculate how many points the buyer gets based on the order price per item in the order in relation with the previously calculated margin. The point that is earned is an equivalent to a maximum percentage of the price of a book AND cannot be more then the maximum % of the calculated margin.

Is there any way or technique in Drupal to do this?
Calculations

You should definitely have a look at the (fairly new) Math Field module, to use it for:


calculating the margin price.
the calculations after order completion.


Some more details about this module (from its project page), which also details when the calculations actually happen:


  The Math Field module defines a new field type to dynamically calculate values on entity forms. This is an alternative to Computed Field
  that does not require the use of PHP Filter.
  
  Math expression fields are dynamically updated via ajax on the entity form as soon as all necessary fields are populated. If JavaScript is disabled, the field will be evaluated when the form is submitted. The result is stored in the database when the form is submitted.


For more details (step-by-step instructions), refer to either of these links:


the (amazing!) Community documentation about the Math Field module.
the interesting article about Introducing the Math Field module, which also includes some screenprints to see it at work (such as the the add/edit form example).


Known issues

As this is a fairly new D7 module, there are still a few (known) issues with it, as mentioned also on its project page:


The cTools math expression library causes an error when using function that take more than one parameter. This effects pow(), min(), max() (#1958538: Improve math expression engine).
Math Field does not yet support multivalue options fields (checkboxes, or multiselect) (#2483453: Add aggregate functions for multivalue fields).
Math Field does not yet support Field Collections (#2573643: Support the Field Collection module).


Bonus: you would not need the "PHP filter" (IMO only that should be a valid reason to investigate this alternative) ...

Dashboard

Depending on your actual requirements/expectations, you might be able to implement your dashboard with only the Views module, possibly complemented one of more add on modules for it. E.g... if you'd also want some sort of charts to be created, a module such as charts could be a good fit.

As an alternative (or possible complement?) you might also want to consider the Forena module (discosure: I'm a co-maintainer), e.g. because it comes with charting out of the box, and it would even allow you to integrate some of your data stored in XML (your ONIX data) without importing them.

Buyer points

These points could be implemented using the Userpoints module (that's obvious). And by using the Goals module (disclosure: I'm its maintainer) you can have the Rules module take care of granting the appropriate number of userpoints. So with that it seems that your question (challenge) is reduced to finding a way to configure the appropriate set of Goals and related Tasks. For a basic introduction about Goals, have a look at:


How to implement gamification in a Drupal site and award incentives?
Its video tutorial.


Bonus: even though your question doesn't seem to include that (yet) as requirements, it would be straight forward to implement some special reward(s) for your frequent buyers. Here is what it would take:


Goal = "New customer", with only 1 Task = "Buy 1 product" (say with machine name = buy_product).
Goal = "Silver customer", with Task = "Buy 10 products, on 10 different days, in a timeframe of 3 months".
Goal = "Gold customer", with Task = "Buy 100 products, on 25 different days, in a timeframe of 12 months".


The interesting part of this is that you'd only have to write 1 rule (related to that machine name = buy_product) to keep track of the progress of any user to become a new/silver/gold customer (check the video for a similar sample).