Skip to content

Commit

Permalink
Merge pull request #4 from OpenBuildings/promotion-giftcard-fixes
Browse files Browse the repository at this point in the history
Fixed giftcard promotion to work only on the brand purchases it applies to
  • Loading branch information
dkyosev committed Nov 6, 2015
2 parents bc552ee + dc06d0f commit 4feb29e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
19 changes: 17 additions & 2 deletions classes/Kohana/Model/Promotion/Promocode/Giftcard.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,25 @@ public function validate_purchase(Model_Purchase $purchase)

public function price_for_purchase_item(Model_Purchase_Item $purchase_item)
{
$brand_purchases_count = $purchase_item->brand_purchase->purchase->brand_purchases->count();
$brand_purchase = $purchase_item->get_insist('brand_purchase');
$brand_total = $brand_purchase->total_price('product');

$purchase = $brand_purchase->get_insist('purchase');

$totals = array_map(function ($brand_purchase) {
return $this->applies_to($brand_purchase)
? $brand_purchase->total_price('product')
: 0;
}, $purchase->brand_purchases->as_array());

$total = Jam_Price::sum($totals, $purchase_item->currency(), $purchase_item->monetary());

$multiplier = $total->is(Jam_Price::GREATER_THAN, 0)
? $brand_total->amount() / $total->amount()
: 1;

return $this->amount
->monetary($purchase_item->monetary())
->multiply_by(-1 / $brand_purchases_count);
->multiply_by(-$multiplier);
}
}
21 changes: 17 additions & 4 deletions tests/tests/Model/Promotion/Promocode/GitfcardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ public function test_price_for_purchase_item()
));

$purchase_item
->expects($this->once())
->expects($this->any())
->method('monetary')
->will($this->returnValue($monetary));


$purchase_item->set(array(
'brand_purchase' => array(
'id' => 1,
'purchase' => array(
'brand_purchases' => array(
array('id' => 1),
Expand All @@ -84,12 +86,23 @@ public function test_price_for_purchase_item()
)
));

$promotion = Jam::build('promotion_promocode_giftcard', array(
'amount' => 20,
$promotion = $this->getMock('Model_Promotion_Promocode_Giftcard', array(
'applies_to'
), array(
'promotion'
));

$promotion
->expects($this->exactly(2))
->method('applies_to')
->will($this->onConsecutiveCalls(TRUE, FALSE));

$promotion->set(array(
'amount' => new Jam_Price(20, 'GBP', $monetary, 'GBP'),
'currency' => 'GBP',
));

$expected_price = new Jam_Price(-10, 'GBP', $monetary, 'GBP');
$expected_price = new Jam_Price(-20, 'GBP', $monetary, 'GBP');

$this->assertEquals($expected_price, $promotion->price_for_purchase_item($purchase_item));
}
Expand Down

0 comments on commit 4feb29e

Please sign in to comment.