File tree Expand file tree Collapse file tree 7 files changed +79
-101
lines changed
app/models/solidus_promotions Expand file tree Collapse file tree 7 files changed +79
-101
lines changed Original file line number Diff line number Diff line change @@ -20,10 +20,9 @@ def call
2020
2121 DiscountOrder . new ( order , promotions , dry_run : dry_run ) . call
2222
23- unless dry_run
24- order . save!
25- end
26- order
23+ CleanDiscountedOrder . new ( order ) . call
24+
25+ RecalculatePromoTotals . new ( order ) . call
2726 end
2827 end
2928end
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ module SolidusPromotions
4+ class OrderAdjuster
5+ class CleanDiscountedOrder
6+ attr_reader :order
7+
8+ def initialize ( order )
9+ @order = order
10+ end
11+
12+ def call
13+ order . line_items . each do |line_item |
14+ line_item . adjustments . select { _1 . amount . zero? } . each ( &:mark_for_destruction )
15+ end
16+
17+ order . shipments . each do |shipment |
18+ shipment . adjustments . select { _1 . amount . zero? } . each ( &:mark_for_destruction )
19+ shipment . shipping_rates . each do |shipping_rate |
20+ shipping_rate . discounts . select { _1 . amount . zero? } . each ( &:mark_for_destruction )
21+ end
22+ end
23+
24+ order
25+ end
26+ end
27+ end
28+ end
Original file line number Diff line number Diff line change @@ -25,23 +25,6 @@ def call
2525 end
2626 end
2727
28- order . line_items . each do |line_item |
29- line_item . adjustments . select { _1 . amount . zero? } . each ( &:mark_for_destruction )
30- line_item . promo_total = line_item . adjustments . reject ( &:marked_for_destruction? ) . sum ( &:amount )
31- line_item . adjustment_total = line_item . promo_total
32- end
33-
34- order . shipments . each do |shipment |
35- shipment . adjustments . select { _1 . amount . zero? } . each ( &:mark_for_destruction )
36- shipment . promo_total = shipment . adjustments . reject ( &:marked_for_destruction? ) . sum ( &:amount )
37- shipment . adjustment_total = shipment . promo_total
38- end
39-
40- order . item_total = order . line_items . sum ( &:amount )
41- order . item_count = order . line_items . sum ( &:quantity )
42- order . promo_total = ( order . line_items + order . shipments ) . sum ( &:promo_total )
43- order . adjustment_total = order . promo_total
44-
4528 order
4629 end
4730
@@ -81,7 +64,6 @@ def adjust_shipments(benefits)
8164 def adjust_shipping_rates ( benefits )
8265 order . shipments . flat_map ( &:shipping_rates ) . filter_map do |rate |
8366 next unless rate . cost
84-
8567 discounts = generate_discounts ( benefits , rate )
8668 chosen_discounts = SolidusPromotions . config . discount_chooser_class . new ( discounts ) . call
8769 ( rate . current_discounts - chosen_discounts ) . each ( &:mark_for_destruction )
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ module SolidusPromotions
4+ class OrderAdjuster
5+ class RecalculatePromoTotals
6+ attr_reader :order
7+
8+ def initialize ( order )
9+ @order = order
10+ end
11+
12+ def call
13+ order . line_items . each do |line_item |
14+ line_item . promo_total = calculate_promo_total_for_adjustable ( line_item )
15+ end
16+
17+ order . shipments . each do |shipment |
18+ shipment . promo_total = calculate_promo_total_for_adjustable ( shipment )
19+ end
20+
21+ order . item_total = order . line_items . sum ( &:amount )
22+ order . item_count = order . line_items . sum ( &:quantity )
23+ order . promo_total = ( order . line_items + order . shipments ) . sum ( &:promo_total )
24+
25+ order
26+ end
27+
28+ private
29+
30+ def calculate_promo_total_for_adjustable ( adjustable )
31+ adjustable
32+ . adjustments
33+ . select ( &:promotion? )
34+ . reject ( &:marked_for_destruction? )
35+ . sum ( &:amount )
36+ end
37+ end
38+ end
39+ end
Original file line number Diff line number Diff line change 4242 it "creates a new discounted line item" do
4343 expect ( order . adjustments ) . to be_empty
4444 expect ( order . line_items . count ) . to eq ( 3 )
45+ # 19.99 * 2
4546 expect ( order . total ) . to eq ( 39.98 )
47+ # 19.99 * 2 + 4 * 1
4648 expect ( order . item_total ) . to eq ( 43.98 )
4749 expect ( order . item_total_before_tax ) . to eq ( 39.98 )
4850 expect ( order . line_items . flat_map ( &:adjustments ) . length ) . to eq ( 1 )
5658 it "creates a new discounted line item" do
5759 expect ( order . adjustments ) . to be_empty
5860 expect ( order . line_items . count ) . to eq ( 3 )
61+ # 19.99 * 3
5962 expect ( order . total ) . to eq ( 59.97 )
6063 expect ( order . item_total ) . to eq ( 67.97 )
6164 expect ( order . item_total_before_tax ) . to eq ( 59.97 )
Original file line number Diff line number Diff line change 6767 it " will not create the adjustment" do
6868 expect {
6969 subject
70+ order . save!
7071 } . not_to change { adjustable . adjustments . length }
7172 end
7273 end
8384 it "doesn't create an adjustment" do
8485 expect {
8586 subject
87+ order . save!
8688 } . to change { adjustable . adjustments . length } . by ( 0 )
8789 end
8890
9496 adjustable . reload
9597 expect {
9698 subject
99+ order . save!
97100 } . to change { adjustable . reload . adjustments . length } . by ( -1 )
98101 end
99102 end
158161 it "will not create an adjustment on the shipping rate" do
159162 expect do
160163 subject
164+ order . save!
161165 end . not_to change { order . shipments . first . shipping_rates . first . discounts . count }
162166 end
163167 end
173177 expect do
174178 promotion
175179 subject . call
180+ order . save!
176181 end . to change { order . shipments . first . adjustments . count }
177182 end
178183
184189 expect do
185190 promotion
186191 subject . call
192+ order . save!
187193 end . not_to change { order . shipments . first . adjustments . count }
188194 end
189195 end
You can’t perform that action at this time.
0 commit comments