External value usage in Datamapper - WSO2 ESB - wso2-esb

A data format with:
Item: A
Id: 001
Price: 100.00
Should be converted into:
ItemName: A
ItemId: 001
Price: 100.00 * (1 + 0.1) ---->(here the price should be calculated with commision)
Here commision is 0.1, should come from a static property (not constant but the commission may change daily) externally. How can I fix this using WSO2 ESB DataMapper?

Related

Shopify Math liquid rounding not returning the same value when using automatic discount

I am using the following code:
{{ product.price | times: 0.90 | money_with_currency}}
I also have a 10% automatic discount enable. The price returned using the code above is not the same as the price returned using automatic discount:
Product cost |$35.49 |$34.41 |$46.59 |$52.99
price using code: |$31.94 |$30.97 |$41.93 |$47.69
10% discount in cart |$31.95 |$30.97 |$41.94 |$47.70
I would like to display the same amount using the code as the prices shown in cart
pic attached

Django queryset field replaced with related table field

I have 2 tables (Product and CustomerProduct)
CustomerProduct is the intermediate table between Customer and Product. It assigns customer specific pricing to certain products.
Product Model
class Product(models.Model):
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits=12, decimal_places=2)
sample data
id
name
price
1
orange
1.5
2
apple
2.2
3
kiwi
3.5
CustomerProduct Model
class CustomerProduct(models.Model):
customer = models.ForeignKey(
"Customer",
on_delete=models.CASCADE,
related_name="customer_customerproducts",
)
product = models.ForeignKey(
"Product",
on_delete=models.CASCADE,
related_name="product_customerproducts",
)
price = models.DecimalField(
max_digits=12,
decimal_places=2,
)
sample data
id
customer_id
product_id
price
1
233
1
1.2
2
233
2
2.0
Expected Result
I want to query the all products but the Product.price adjusted according to CustomerProduct.price if related field exists. The expected data (example in json but want queryset):
[
{
id: 1
name: "orange"
price: 1.2 // The price adjusted according to customer price
}
{
id: 2
name: "apple"
price: 2.0 // The price adjusted according to customer price
}
{
id: 3
name: "kiwi"
price: 3.5 // Price remain original because CustomerProduct not exists.
}
]
The How
I totally have no idea how to accomplish this in Django. How to do that?
You can use Coalesce [Django docs] with Subquery expressions [Django docs] to annotate a value on your queryset which will be the price that you want. Coalesce gives the first non-null value of the expressions passed to it, so we will pass it a subquery that will get the related price from CustomerProduct or the field price itself in that order to achieve what you want:
from django.db.models import F, OuterRef, Subquery
from django.db.models.functions import Coalesce
customer = Customer.objects.get(pk=some_pk) # get the customer for which you want the results
# Last related price, will use it as a subquery
related_price = CustomerProduct.objects.filter(
product=OuterRef('pk'),
customer=customer,
).order_by('-pk').values('price')[:1]
products = Product.objects.annotate(
price_offered=Coalesce(
Subquery(related_price), # If there is a matching object in the subquery use it
F('price'), # Else use the price of the Product itself
)
)
for product in products:
print(product.pk, product.name, product.price_offered)

SQL: How to virtually combine two tables and link to the result?

Imagine three tables:
user:
- id
- name (string)
event:
- id
- description (string)
- points (int)
history:
- user_id
- event_id
Now imagine that the tables are currently filled with the following data:
user:
id: 1, name: foo
id: 2, name: bar
id: 3, name: beef
event:
id: 1, description: "walked 5 miles", points: +10
id: 2, description: "awake the whole night", points: +15
id: 3, description: "wasn't naughty", points: +20
history:
id: 1, user_id: 1, event_id: 1
id: 2, user_id: 1, event_id: 3
id: 3, user_id: 2, event_id: 1
So the schema is something like "how many points does a user have". Currently, e.g., the user 1 has in sum 10+20 = 30 points, right?
I would like to add another table with things the user can "buy" for his/her points.
gift:
- id
- points (int)
- description (string)
which is filled with e.g.
gift:
id: 1, points: -30, description: "a bottle of beer"
id: 2, points: -5, description: "coffee"
My problem:
Currently I am creating a new entry in history when the user gets points. But how would you substract points when he buys a gift?
I thought about something like a combined table which includes the events and the gifts entries:
[USER]-----[HISTORY]-----[COMBINED]--+--[EVENTS]
|
+--[GIFTS]
But I cannot really join them because I need the gift table somewhere else.
I have absolutely no clue how to do that in SQL, it's a long time since I learned it and I unfortunately never used it since then. I hope somebody can point me into the right direction :)
Thanks!

Shopify Math logic not working properly

on my store when using custom template i got a price based on some values
Price * 2 it works
Example.
17.99 * 2
I got in cart: 35.98, this is good.
But when using:
17.99 * 0.5, should be 8.99
i got: 17.99
Why?????????? Any ideas?
Im adding as value this way:
<option value="{{ qty | times:0.50 }}" data-qua="{{ qty | times:0.50 }}">{{ inches }}m</option>
Well, i have found that due to a Shopify limitation you can not use decimals as quantity, so cannot do PRICE * 0.5
If is there a any solution to this let me know.
Sell by unit pricing. As you figured out, no shopping cart lets you sell 0.3456 of something. While you can buy .0005342 of a bitcoin, you cannot sell non-integer anything with Shopify. So adjust your formula to sell 1 thing at $.003456 or whatever... and collect your fortune that way.

MDX calculation has wrong order of precendence

Im having an issue with an MDX query, and I think it boils down to the order of precedence between calculating an aggregate and a calculated member.
Let me start with the underlying data, which revolves around a valuation (which has a date, and some other data such as a member type, a scheme - and crucially for this question; a loading factor) and an associated value.
The data
Valuation Table
Id | Valuation Date | Member Type | Scheme | Loading Factor
=============================================================
1 | 2010-01-01 | TypeA | Scheme X | 0.02
2 | 2010-01-01 | TypeB | Scheme X | 0.02
3 | 2010-01-01 | TypeA | Scheme Y | 0.02
4 | 2010-01-01 | TypeB | Scheme Y | 0.02
ValuationValue table
ValuationId | Value
====================
1 | 1000.0
2 | 2000.0
3 | 3000.0
4 | 4000.0
This, when loaded into a cube has a Valuation dimension with attributes MemberType, Scheme and date. And a cube with Measure group ValuationValue containing Value measure, and a Valuation measure group containing Loading Factor like so:
Cube
-Measure Groups
- Valuation
|_Loading Factor
- ValuationValue
|_Value
- Dimensions
- Valuation
|_MemberType
|_Scheme
|_Date
The question
Loading factor is used to load the Value, think of it like a tax, so 0.02 means "Loading amount is 2% of the value". When returning Value from a query, I need to also calculate the amount to load this value by. A typical query might look like
SELECT
{
[Measures].[Value]
} ON 0,
[Valuation].[Scheme] ON 1
FROM Cube
This would return 2 rows, and as you can see by comparing to the data above it correctly sums across memberType:
Scheme | Value
=================
Scheme X | 3000.0
Scheme Y | 7000.0
Now, if I try to calculate my loading factor in that query, all goes wrong - i'll demonstrate. Given the following query:
WITH MEMBER [Measures].[Loading Value]
AS
(
[Measures].[Value] * [Measures].[Loading Factor]
)
SELECT
{
[Measures].[Value] ,
[Measures].[Loading Value]
} ON 0,
[Valuation].[Scheme] ON 1
FROM Cube
I get the result
Scheme | Value | Loading Value
=================================
Scheme X | 3000.0 | 120.0
Scheme Y | 7000.0 | 280.0
Basically, what is happening is that it is suming my Loading Factor and then multiplying that by the Sum of my values(The first row above should be 1000 * 0.02 + 2000 * 0.02 = 60. Instead it's calculating 3000 * 0.04 = 120).
This is of course a contrived example, my actual structure is a bit more complex - but I think this demonstrates the problem. I was under the impression that the calculated member in the example above should occur on a row-by-row basis, instead of at the end of an aggration of my Value measure.
Thanks for any replies.
Your [Measures].[Loading Factor] - How is that set, is it a SUM?
Calculated members are generally done as per the rows returned if I remember - Unless you specify otherwise.
If you want an example, take a look at the currency conversion wizard output - This does something similar using the LEAVES command - You will need to do this in the MDX script as a SCOPE'd command though.
Given your description, the code could be something like:
CREATE MEMBER [Measures].[Loading Value] AS NULL
Scope( { [Measures].[Loading Value] } );
Scope( Leaves([Valuation]) );
This = [Measures].[Value] * [Measures].[Loading Factor]
Format_String(This) = "#,##0.00;-#,##0.00";
End Scope;
End Scope;
I'm not sure I follow your example completely, but you might try using SOLVE_ORDER and SCOPE_ISOLATION to manipulate the order of the calculations.
For example,
WITH
MEMBER [Measures].[Custom Calculation] AS
'([Measures].[Sales Count] - [Measures].[Unit Returns])',
SOLVE_ORDER = 65535, SCOPE_ISOLATION = CUBE
SELECT
{[Measures].[Custom Calculation]} ON COLUMNS,
NON EMPTY [Time].[YQMD].[Day].AllMembers ON ROWS
FROM [Waremart]
Thes one turned out ot be REALLY easy.
WITH MEMBER [Measures].[Loading Value]
AS
(
[Measures].[Value] * [Measures].[Loading Factor]
)
WITH MEMBER [Measures].[Total Loading Value]
AS
SUM (
EXISTING [Valuation].[Id].[Id],
[Measures].[Loading Value]
)
SELECT
{
[Measures].[Value] ,
[Measures].[Measures].[Total Loading Value]
} ON 0,
[Valuation].[Scheme] ON 1
FROM Cube