Skip to content

Commit 4feb29e

Browse files
committed
Merge pull request #4 from OpenBuildings/promotion-giftcard-fixes
Fixed giftcard promotion to work only on the brand purchases it applies to
2 parents bc552ee + dc06d0f commit 4feb29e

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

classes/Kohana/Model/Promotion/Promocode/Giftcard.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,25 @@ public function validate_purchase(Model_Purchase $purchase)
3636

3737
public function price_for_purchase_item(Model_Purchase_Item $purchase_item)
3838
{
39-
$brand_purchases_count = $purchase_item->brand_purchase->purchase->brand_purchases->count();
39+
$brand_purchase = $purchase_item->get_insist('brand_purchase');
40+
$brand_total = $brand_purchase->total_price('product');
41+
42+
$purchase = $brand_purchase->get_insist('purchase');
43+
44+
$totals = array_map(function ($brand_purchase) {
45+
return $this->applies_to($brand_purchase)
46+
? $brand_purchase->total_price('product')
47+
: 0;
48+
}, $purchase->brand_purchases->as_array());
49+
50+
$total = Jam_Price::sum($totals, $purchase_item->currency(), $purchase_item->monetary());
51+
52+
$multiplier = $total->is(Jam_Price::GREATER_THAN, 0)
53+
? $brand_total->amount() / $total->amount()
54+
: 1;
4055

4156
return $this->amount
4257
->monetary($purchase_item->monetary())
43-
->multiply_by(-1 / $brand_purchases_count);
58+
->multiply_by(-$multiplier);
4459
}
4560
}

tests/tests/Model/Promotion/Promocode/GitfcardTest.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ public function test_price_for_purchase_item()
6969
));
7070

7171
$purchase_item
72-
->expects($this->once())
72+
->expects($this->any())
7373
->method('monetary')
7474
->will($this->returnValue($monetary));
7575

76+
7677
$purchase_item->set(array(
7778
'brand_purchase' => array(
79+
'id' => 1,
7880
'purchase' => array(
7981
'brand_purchases' => array(
8082
array('id' => 1),
@@ -84,12 +86,23 @@ public function test_price_for_purchase_item()
8486
)
8587
));
8688

87-
$promotion = Jam::build('promotion_promocode_giftcard', array(
88-
'amount' => 20,
89+
$promotion = $this->getMock('Model_Promotion_Promocode_Giftcard', array(
90+
'applies_to'
91+
), array(
92+
'promotion'
93+
));
94+
95+
$promotion
96+
->expects($this->exactly(2))
97+
->method('applies_to')
98+
->will($this->onConsecutiveCalls(TRUE, FALSE));
99+
100+
$promotion->set(array(
101+
'amount' => new Jam_Price(20, 'GBP', $monetary, 'GBP'),
89102
'currency' => 'GBP',
90103
));
91104

92-
$expected_price = new Jam_Price(-10, 'GBP', $monetary, 'GBP');
105+
$expected_price = new Jam_Price(-20, 'GBP', $monetary, 'GBP');
93106

94107
$this->assertEquals($expected_price, $promotion->price_for_purchase_item($purchase_item));
95108
}

0 commit comments

Comments
 (0)