難しい質問で助けが必要です。これは、クーポンが Spree Commerce プラットフォームに実装される複雑な方法に関係しています。
未払いのクーポンの合計額が必要なだけです。
テーブルは次のとおりです。
環境設定
id | value | key
---+---------------------------------------------
1 | 25 | spree/calculator/flat_rate/amount/5
電卓
id | calculable_id | calculable_type
----+---------------+-----------------
5 | 3 | promotion_action
プロモーションアクション
id | activator_id
----+-------------
3 | 2
アクティベーター
id | expires_at | usage_limit
---+------------+------------
2 | 2013-12-01 | 4
調整
originator_type | originator_id | amount
-----------------+---------------+-------
promotion_action | 3 | -25
Preferences.key の最後の数字は、電卓の ID に対応します。
まず、preferences.amount の合計に activators.usage_limit (activators.expires_at < 今日を除く) を掛けたものが必要です。ここで、preferences.key は '%calculator/flat_rate%' のようです。
結果は、この金額から対応する調整の合計を差し引いたものになります。
私はどこまでも
select
(select sum(value) from spree_preferences
where `key` like "%calculator/flat_rate%")
+
(select sum(amount) from spree_adjustments
where originator_type = 'promotion_action') as total;
、しかし、これはexpires_atとusage_limitを考慮していません.
更新された酒宴の場合、答えは次のとおりです。
select sum(subAggregate.outstanding)
from
(
select (subDetail.value * subDetail.multiplier) + subDetail.adjustmentAmount as outstanding
from
(
select p.value,
case
when a.expires_at > curDate() then a.usage_limit
else 1
end as multiplier ,
ifNull(adj.amount,0) as adjustmentAmount
from spree_preferences p
left outer join spree_calculators c
on replace(p.key,'spree/calculator/flat_rate/amount/','') = c.id
left outer join spree_promotion_actions pa
on c.calculable_id = pa.id
and c.calculable_type = 'Spree::PromotionAction'
left outer join spree_promotions a
on pa.promotion_id = a.id
left outer join spree_adjustments adj
on pa.id = adj.source_id
and pa.type = 'Spree::PromotionAction'
) subDetail
) subAggregate